xref: /freebsd/sys/net/bpf.c (revision 572bde2aea65923e1ee1aa98c5aba50c54fd5cd4)
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);
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes 	return (0);
358df8bae1dSRodney W. Grimes }
359df8bae1dSRodney W. Grimes 
360df8bae1dSRodney W. Grimes /*
361df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
362df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
363df8bae1dSRodney W. Grimes  */
364df8bae1dSRodney W. Grimes /* ARGSUSED */
36587f6c662SJulian Elischer static	int
366b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
36789c9c53dSPoul-Henning Kamp 	struct cdev *dev;
36860039670SBruce Evans 	int flags;
36960039670SBruce Evans 	int fmt;
370b40ce416SJulian Elischer 	struct thread *td;
371df8bae1dSRodney W. Grimes {
372e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
373df8bae1dSRodney W. Grimes 
37481bda851SJohn Polstra 	BPFD_LOCK(d);
37581bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
37681bda851SJohn Polstra 		callout_stop(&d->bd_callout);
37781bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
37881bda851SJohn Polstra 	BPFD_UNLOCK(d);
379e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
380e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
381df8bae1dSRodney W. Grimes 	if (d->bd_bif)
382df8bae1dSRodney W. Grimes 		bpf_detachd(d);
383e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
38482f4445dSRobert Watson #ifdef MAC
38582f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
38682f4445dSRobert Watson #endif /* MAC */
387df8bae1dSRodney W. Grimes 	bpf_freed(d);
388bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = 0;
389d722be54SLuigi Rizzo 	free(d, M_BPF);
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes 	return (0);
392df8bae1dSRodney W. Grimes }
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes /*
396df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
397df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
398df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
399df8bae1dSRodney W. Grimes  */
400df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
401df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
402df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
403df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
404df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
405df8bae1dSRodney W. Grimes 	(d)->bd_fbuf = 0;
406df8bae1dSRodney W. Grimes /*
407df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
408df8bae1dSRodney W. Grimes  */
40987f6c662SJulian Elischer static	int
41060039670SBruce Evans bpfread(dev, uio, ioflag)
41189c9c53dSPoul-Henning Kamp 	struct cdev *dev;
4128994a245SDag-Erling Smørgrav 	struct uio *uio;
41360039670SBruce Evans 	int ioflag;
414df8bae1dSRodney W. Grimes {
415e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
41681bda851SJohn Polstra 	int timed_out;
417df8bae1dSRodney W. Grimes 	int error;
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes 	/*
420df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
421df8bae1dSRodney W. Grimes 	 * as kernel buffers.
422df8bae1dSRodney W. Grimes 	 */
423df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
424df8bae1dSRodney W. Grimes 		return (EINVAL);
425df8bae1dSRodney W. Grimes 
426e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
42781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
42881bda851SJohn Polstra 		callout_stop(&d->bd_callout);
42981bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
43081bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
431df8bae1dSRodney W. Grimes 	/*
432df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
433df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
434df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
435df8bae1dSRodney W. Grimes 	 */
436572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
43781bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
438df8bae1dSRodney W. Grimes 			/*
439df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
440df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
441df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
442df8bae1dSRodney W. Grimes 			 */
443df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
444df8bae1dSRodney W. Grimes 			break;
445df8bae1dSRodney W. Grimes 		}
446de5d9935SRobert Watson 
447de5d9935SRobert Watson 		/*
448de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
449de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
450de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
451de5d9935SRobert Watson 		 * it before using it again.
452de5d9935SRobert Watson 		 */
453de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
454e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
455de5d9935SRobert Watson 			return (ENXIO);
456de5d9935SRobert Watson 		}
457de5d9935SRobert Watson 
458fba3cfdeSJohn Polstra 		if (ioflag & IO_NDELAY) {
459e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
460fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
461fba3cfdeSJohn Polstra 		}
462521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
463e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
464df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
465e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
466df8bae1dSRodney W. Grimes 			return (error);
467df8bae1dSRodney W. Grimes 		}
468df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
469df8bae1dSRodney W. Grimes 			/*
470df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
471df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
472df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
473df8bae1dSRodney W. Grimes 			 */
474df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
475df8bae1dSRodney W. Grimes 				/*
476df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
477df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
478df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
479df8bae1dSRodney W. Grimes 				 */
480df8bae1dSRodney W. Grimes 				break;
481df8bae1dSRodney W. Grimes 
482df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
483e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
484df8bae1dSRodney W. Grimes 				return (0);
485df8bae1dSRodney W. Grimes 			}
486df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
487df8bae1dSRodney W. Grimes 			break;
488df8bae1dSRodney W. Grimes 		}
489df8bae1dSRodney W. Grimes 	}
490df8bae1dSRodney W. Grimes 	/*
491df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
492df8bae1dSRodney W. Grimes 	 */
493e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes 	/*
496df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
497df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
498df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
499df8bae1dSRodney W. Grimes 	 */
500e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
501df8bae1dSRodney W. Grimes 
502e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
503df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
504572bde2aSRobert Watson 	d->bd_hbuf = NULL;
505df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
506e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
507df8bae1dSRodney W. Grimes 
508df8bae1dSRodney W. Grimes 	return (error);
509df8bae1dSRodney W. Grimes }
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes 
512df8bae1dSRodney W. Grimes /*
513df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
514df8bae1dSRodney W. Grimes  */
515e7bb21b3SJonathan Lemon static __inline void
516df8bae1dSRodney W. Grimes bpf_wakeup(d)
5178994a245SDag-Erling Smørgrav 	struct bpf_d *d;
518df8bae1dSRodney W. Grimes {
51981bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
52081bda851SJohn Polstra 		callout_stop(&d->bd_callout);
52181bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
52281bda851SJohn Polstra 	}
523521f364bSDag-Erling Smørgrav 	wakeup(d);
524831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
525f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
52600a83887SPaul Traina 
527512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
52895aab9ccSJohn-Mark Gurney 	KNOTE(&d->bd_sel.si_note, 0);
529df8bae1dSRodney W. Grimes }
530df8bae1dSRodney W. Grimes 
53181bda851SJohn Polstra static void
53281bda851SJohn Polstra bpf_timed_out(arg)
53381bda851SJohn Polstra 	void *arg;
53481bda851SJohn Polstra {
53581bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
53681bda851SJohn Polstra 
53781bda851SJohn Polstra 	BPFD_LOCK(d);
53881bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
53981bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
54081bda851SJohn Polstra 		if (d->bd_slen != 0)
54181bda851SJohn Polstra 			bpf_wakeup(d);
54281bda851SJohn Polstra 	}
54381bda851SJohn Polstra 	BPFD_UNLOCK(d);
54481bda851SJohn Polstra }
54581bda851SJohn Polstra 
54687f6c662SJulian Elischer static	int
54760039670SBruce Evans bpfwrite(dev, uio, ioflag)
54889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
549df8bae1dSRodney W. Grimes 	struct uio *uio;
55060039670SBruce Evans 	int ioflag;
551df8bae1dSRodney W. Grimes {
552e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
553df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
554df8bae1dSRodney W. Grimes 	struct mbuf *m;
555e7bb21b3SJonathan Lemon 	int error;
5568240bf1eSRobert Watson 	struct sockaddr dst;
557df8bae1dSRodney W. Grimes 	int datlen;
558df8bae1dSRodney W. Grimes 
559572bde2aSRobert Watson 	if (d->bd_bif == NULL)
560df8bae1dSRodney W. Grimes 		return (ENXIO);
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
565df8bae1dSRodney W. Grimes 		return (0);
566df8bae1dSRodney W. Grimes 
5678240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
568df8bae1dSRodney W. Grimes 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
569df8bae1dSRodney W. Grimes 	if (error)
570df8bae1dSRodney W. Grimes 		return (error);
571df8bae1dSRodney W. Grimes 
572df8bae1dSRodney W. Grimes 	if (datlen > ifp->if_mtu)
573df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
574df8bae1dSRodney W. Grimes 
575114ae644SMike Smith 	if (d->bd_hdrcmplt)
576114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
577114ae644SMike Smith 
57882f4445dSRobert Watson #ifdef MAC
579f747d2ddSRobert Watson 	BPFD_LOCK(d);
58082f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
581f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
58282f4445dSRobert Watson #endif
583b8f9429dSRobert Watson 	NET_LOCK_GIANT();
584572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
585b8f9429dSRobert Watson 	NET_UNLOCK_GIANT();
586df8bae1dSRodney W. Grimes 	/*
587df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
588df8bae1dSRodney W. Grimes 	 */
589df8bae1dSRodney W. Grimes 	return (error);
590df8bae1dSRodney W. Grimes }
591df8bae1dSRodney W. Grimes 
592df8bae1dSRodney W. Grimes /*
593df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
594e7bb21b3SJonathan Lemon  * receive and drop counts.
595df8bae1dSRodney W. Grimes  */
596df8bae1dSRodney W. Grimes static void
597df8bae1dSRodney W. Grimes reset_d(d)
598df8bae1dSRodney W. Grimes 	struct bpf_d *d;
599df8bae1dSRodney W. Grimes {
600e7bb21b3SJonathan Lemon 
601e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
602df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
603df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
604df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
605572bde2aSRobert Watson 		d->bd_hbuf = NULL;
606df8bae1dSRodney W. Grimes 	}
607df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
608df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
609df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
610df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
611df8bae1dSRodney W. Grimes }
612df8bae1dSRodney W. Grimes 
613df8bae1dSRodney W. Grimes /*
614df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
615df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
616df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
617df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
618df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
619df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
620df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
621df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
622df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
623df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
624df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
625df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
626df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
627df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
628114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
629114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6308ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6318ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
632df8bae1dSRodney W. Grimes  */
633df8bae1dSRodney W. Grimes /* ARGSUSED */
63487f6c662SJulian Elischer static	int
635b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
63689c9c53dSPoul-Henning Kamp 	struct cdev *dev;
637ecbb00a2SDoug Rabson 	u_long cmd;
638df8bae1dSRodney W. Grimes 	caddr_t addr;
63960039670SBruce Evans 	int flags;
640b40ce416SJulian Elischer 	struct thread *td;
641df8bae1dSRodney W. Grimes {
642e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
643e7bb21b3SJonathan Lemon 	int error = 0;
644df8bae1dSRodney W. Grimes 
64581bda851SJohn Polstra 	BPFD_LOCK(d);
64681bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
64781bda851SJohn Polstra 		callout_stop(&d->bd_callout);
64881bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
64981bda851SJohn Polstra 	BPFD_UNLOCK(d);
65081bda851SJohn Polstra 
651df8bae1dSRodney W. Grimes 	switch (cmd) {
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes 	default:
654df8bae1dSRodney W. Grimes 		error = EINVAL;
655df8bae1dSRodney W. Grimes 		break;
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes 	/*
658df8bae1dSRodney W. Grimes 	 * Check for read packet available.
659df8bae1dSRodney W. Grimes 	 */
660df8bae1dSRodney W. Grimes 	case FIONREAD:
661df8bae1dSRodney W. Grimes 		{
662df8bae1dSRodney W. Grimes 			int n;
663df8bae1dSRodney W. Grimes 
664e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
665df8bae1dSRodney W. Grimes 			n = d->bd_slen;
666df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
667df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
668e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 			*(int *)addr = n;
671df8bae1dSRodney W. Grimes 			break;
672df8bae1dSRodney W. Grimes 		}
673df8bae1dSRodney W. Grimes 
674df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
675df8bae1dSRodney W. Grimes 		{
676df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
677df8bae1dSRodney W. Grimes 
678572bde2aSRobert Watson 			if (d->bd_bif == NULL)
679df8bae1dSRodney W. Grimes 				error = EINVAL;
680df8bae1dSRodney W. Grimes 			else {
681df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
682df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
683df8bae1dSRodney W. Grimes 			}
684df8bae1dSRodney W. Grimes 			break;
685df8bae1dSRodney W. Grimes 		}
686df8bae1dSRodney W. Grimes 
687df8bae1dSRodney W. Grimes 	/*
688df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
689df8bae1dSRodney W. Grimes 	 */
690df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
691df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
692df8bae1dSRodney W. Grimes 		break;
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes 	/*
695df8bae1dSRodney W. Grimes 	 * Set buffer length.
696df8bae1dSRodney W. Grimes 	 */
697df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
698572bde2aSRobert Watson 		if (d->bd_bif != NULL)
699df8bae1dSRodney W. Grimes 			error = EINVAL;
700df8bae1dSRodney W. Grimes 		else {
7018994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
702df8bae1dSRodney W. Grimes 
703eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
704eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
705df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
706df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
707df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
708df8bae1dSRodney W. Grimes 		}
709df8bae1dSRodney W. Grimes 		break;
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes 	/*
712df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
713df8bae1dSRodney W. Grimes 	 */
714df8bae1dSRodney W. Grimes 	case BIOCSETF:
715df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
716df8bae1dSRodney W. Grimes 		break;
717df8bae1dSRodney W. Grimes 
718df8bae1dSRodney W. Grimes 	/*
719df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
720df8bae1dSRodney W. Grimes 	 */
721df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
722e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
723df8bae1dSRodney W. Grimes 		reset_d(d);
724e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
725df8bae1dSRodney W. Grimes 		break;
726df8bae1dSRodney W. Grimes 
727df8bae1dSRodney W. Grimes 	/*
728df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
729df8bae1dSRodney W. Grimes 	 */
730df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
731572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
732df8bae1dSRodney W. Grimes 			/*
733df8bae1dSRodney W. Grimes 			 * No interface attached yet.
734df8bae1dSRodney W. Grimes 			 */
735df8bae1dSRodney W. Grimes 			error = EINVAL;
736df8bae1dSRodney W. Grimes 			break;
737df8bae1dSRodney W. Grimes 		}
738df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
739e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
740df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
741e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
742df8bae1dSRodney W. Grimes 			if (error == 0)
743df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
744df8bae1dSRodney W. Grimes 		}
745df8bae1dSRodney W. Grimes 		break;
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 	/*
7488eab61f3SSam Leffler 	 * Get current data link type.
749df8bae1dSRodney W. Grimes 	 */
750df8bae1dSRodney W. Grimes 	case BIOCGDLT:
751572bde2aSRobert Watson 		if (d->bd_bif == NULL)
752df8bae1dSRodney W. Grimes 			error = EINVAL;
753df8bae1dSRodney W. Grimes 		else
754df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
755df8bae1dSRodney W. Grimes 		break;
756df8bae1dSRodney W. Grimes 
757df8bae1dSRodney W. Grimes 	/*
7588eab61f3SSam Leffler 	 * Get a list of supported data link types.
7598eab61f3SSam Leffler 	 */
7608eab61f3SSam Leffler 	case BIOCGDLTLIST:
761572bde2aSRobert Watson 		if (d->bd_bif == NULL)
7628eab61f3SSam Leffler 			error = EINVAL;
7638eab61f3SSam Leffler 		else
7648eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
7658eab61f3SSam Leffler 		break;
7668eab61f3SSam Leffler 
7678eab61f3SSam Leffler 	/*
7688eab61f3SSam Leffler 	 * Set data link type.
7698eab61f3SSam Leffler 	 */
7708eab61f3SSam Leffler 	case BIOCSDLT:
771572bde2aSRobert Watson 		if (d->bd_bif == NULL)
7728eab61f3SSam Leffler 			error = EINVAL;
7738eab61f3SSam Leffler 		else
7748eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
7758eab61f3SSam Leffler 		break;
7768eab61f3SSam Leffler 
7778eab61f3SSam Leffler 	/*
7781dd0feaaSArchie Cobbs 	 * Get interface name.
779df8bae1dSRodney W. Grimes 	 */
780df8bae1dSRodney W. Grimes 	case BIOCGETIF:
781572bde2aSRobert Watson 		if (d->bd_bif == NULL)
782df8bae1dSRodney W. Grimes 			error = EINVAL;
7831dd0feaaSArchie Cobbs 		else {
7841dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
7851dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
7861dd0feaaSArchie Cobbs 
7879bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
7889bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
7891dd0feaaSArchie Cobbs 		}
790df8bae1dSRodney W. Grimes 		break;
791df8bae1dSRodney W. Grimes 
792df8bae1dSRodney W. Grimes 	/*
793df8bae1dSRodney W. Grimes 	 * Set interface.
794df8bae1dSRodney W. Grimes 	 */
795df8bae1dSRodney W. Grimes 	case BIOCSETIF:
796df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
797df8bae1dSRodney W. Grimes 		break;
798df8bae1dSRodney W. Grimes 
799df8bae1dSRodney W. Grimes 	/*
800df8bae1dSRodney W. Grimes 	 * Set read timeout.
801df8bae1dSRodney W. Grimes 	 */
802df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
803df8bae1dSRodney W. Grimes 		{
804df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
805df8bae1dSRodney W. Grimes 
806bdc2cdc5SAlexander Langer 			/*
807bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
808bdc2cdc5SAlexander Langer 			 * a one-shot timer.
809bdc2cdc5SAlexander Langer 			 */
810bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
811bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
812df8bae1dSRodney W. Grimes 			break;
813df8bae1dSRodney W. Grimes 		}
814df8bae1dSRodney W. Grimes 
815df8bae1dSRodney W. Grimes 	/*
816df8bae1dSRodney W. Grimes 	 * Get read timeout.
817df8bae1dSRodney W. Grimes 	 */
818df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
819df8bae1dSRodney W. Grimes 		{
820df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
821df8bae1dSRodney W. Grimes 
822bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
823bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
824df8bae1dSRodney W. Grimes 			break;
825df8bae1dSRodney W. Grimes 		}
826df8bae1dSRodney W. Grimes 
827df8bae1dSRodney W. Grimes 	/*
828df8bae1dSRodney W. Grimes 	 * Get packet stats.
829df8bae1dSRodney W. Grimes 	 */
830df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
831df8bae1dSRodney W. Grimes 		{
832df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
835df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
836df8bae1dSRodney W. Grimes 			break;
837df8bae1dSRodney W. Grimes 		}
838df8bae1dSRodney W. Grimes 
839df8bae1dSRodney W. Grimes 	/*
840df8bae1dSRodney W. Grimes 	 * Set immediate mode.
841df8bae1dSRodney W. Grimes 	 */
842df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
843df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
844df8bae1dSRodney W. Grimes 		break;
845df8bae1dSRodney W. Grimes 
846df8bae1dSRodney W. Grimes 	case BIOCVERSION:
847df8bae1dSRodney W. Grimes 		{
848df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
849df8bae1dSRodney W. Grimes 
850df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
851df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
852df8bae1dSRodney W. Grimes 			break;
853df8bae1dSRodney W. Grimes 		}
85400a83887SPaul Traina 
855114ae644SMike Smith 	/*
856114ae644SMike Smith 	 * Get "header already complete" flag
857114ae644SMike Smith 	 */
858114ae644SMike Smith 	case BIOCGHDRCMPLT:
859114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
860114ae644SMike Smith 		break;
861114ae644SMike Smith 
862114ae644SMike Smith 	/*
863114ae644SMike Smith 	 * Set "header already complete" flag
864114ae644SMike Smith 	 */
865114ae644SMike Smith 	case BIOCSHDRCMPLT:
866114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
867114ae644SMike Smith 		break;
868114ae644SMike Smith 
8698ed3828cSRobert Watson 	/*
8708ed3828cSRobert Watson 	 * Get "see sent packets" flag
8718ed3828cSRobert Watson 	 */
8728ed3828cSRobert Watson 	case BIOCGSEESENT:
8738ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
8748ed3828cSRobert Watson 		break;
8758ed3828cSRobert Watson 
8768ed3828cSRobert Watson 	/*
8778ed3828cSRobert Watson 	 * Set "see sent packets" flag
8788ed3828cSRobert Watson 	 */
8798ed3828cSRobert Watson 	case BIOCSSEESENT:
8808ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
8818ed3828cSRobert Watson 		break;
8828ed3828cSRobert Watson 
88300a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
88400a83887SPaul Traina 		break;
88500a83887SPaul Traina 
88600a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
88700a83887SPaul Traina 		d->bd_async = *(int *)addr;
88800a83887SPaul Traina 		break;
88900a83887SPaul Traina 
890831d27a9SDon Lewis 	case FIOSETOWN:
891831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
89200a83887SPaul Traina 		break;
89300a83887SPaul Traina 
894831d27a9SDon Lewis 	case FIOGETOWN:
89591e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
896831d27a9SDon Lewis 		break;
897831d27a9SDon Lewis 
898831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
899831d27a9SDon Lewis 	case TIOCSPGRP:
900831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
901831d27a9SDon Lewis 		break;
902831d27a9SDon Lewis 
903831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
90400a83887SPaul Traina 	case TIOCGPGRP:
90591e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
90600a83887SPaul Traina 		break;
90700a83887SPaul Traina 
90800a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
90900a83887SPaul Traina 		{
91000a83887SPaul Traina 			u_int sig;
91100a83887SPaul Traina 
91200a83887SPaul Traina 			sig = *(u_int *)addr;
91300a83887SPaul Traina 
91400a83887SPaul Traina 			if (sig >= NSIG)
91500a83887SPaul Traina 				error = EINVAL;
91600a83887SPaul Traina 			else
91700a83887SPaul Traina 				d->bd_sig = sig;
91800a83887SPaul Traina 			break;
91900a83887SPaul Traina 		}
92000a83887SPaul Traina 	case BIOCGRSIG:
92100a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
92200a83887SPaul Traina 		break;
923df8bae1dSRodney W. Grimes 	}
924df8bae1dSRodney W. Grimes 	return (error);
925df8bae1dSRodney W. Grimes }
926df8bae1dSRodney W. Grimes 
927df8bae1dSRodney W. Grimes /*
928df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
929df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
930df8bae1dSRodney W. Grimes  */
931f708ef1bSPoul-Henning Kamp static int
932df8bae1dSRodney W. Grimes bpf_setf(d, fp)
933df8bae1dSRodney W. Grimes 	struct bpf_d *d;
934df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
935df8bae1dSRodney W. Grimes {
936df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
937df8bae1dSRodney W. Grimes 	u_int flen, size;
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes 	old = d->bd_filter;
940572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
941df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
942df8bae1dSRodney W. Grimes 			return (EINVAL);
943e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
944572bde2aSRobert Watson 		d->bd_filter = NULL;
945df8bae1dSRodney W. Grimes 		reset_d(d);
946e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
947572bde2aSRobert Watson 		if (old != NULL)
948bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
949df8bae1dSRodney W. Grimes 		return (0);
950df8bae1dSRodney W. Grimes 	}
951df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
952df8bae1dSRodney W. Grimes 	if (flen > BPF_MAXINSNS)
953df8bae1dSRodney W. Grimes 		return (EINVAL);
954df8bae1dSRodney W. Grimes 
955df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
956a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
957df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
958df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
959e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
960df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
961df8bae1dSRodney W. Grimes 		reset_d(d);
962e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
963572bde2aSRobert Watson 		if (old != NULL)
964bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
965df8bae1dSRodney W. Grimes 
966df8bae1dSRodney W. Grimes 		return (0);
967df8bae1dSRodney W. Grimes 	}
968bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
969df8bae1dSRodney W. Grimes 	return (EINVAL);
970df8bae1dSRodney W. Grimes }
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes /*
973df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
974df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
975df8bae1dSRodney W. Grimes  * Return an errno or 0.
976df8bae1dSRodney W. Grimes  */
977df8bae1dSRodney W. Grimes static int
978df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
979df8bae1dSRodney W. Grimes 	struct bpf_d *d;
980df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
981df8bae1dSRodney W. Grimes {
982df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
983e7bb21b3SJonathan Lemon 	int error;
9849b44ff22SGarrett Wollman 	struct ifnet *theywant;
985df8bae1dSRodney W. Grimes 
9869b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
987572bde2aSRobert Watson 	if (theywant == NULL)
9889b44ff22SGarrett Wollman 		return ENXIO;
9899b44ff22SGarrett Wollman 
990df8bae1dSRodney W. Grimes 	/*
991df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
992df8bae1dSRodney W. Grimes 	 */
993e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
994572bde2aSRobert Watson 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
995df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
996df8bae1dSRodney W. Grimes 
997572bde2aSRobert Watson 		if (ifp == NULL || ifp != theywant)
998df8bae1dSRodney W. Grimes 			continue;
99924a229f4SSam Leffler 		/* skip additional entry */
100024a229f4SSam Leffler 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
100124a229f4SSam Leffler 			continue;
1002e7bb21b3SJonathan Lemon 
1003e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1004df8bae1dSRodney W. Grimes 		/*
1005df8bae1dSRodney W. Grimes 		 * We found the requested interface.
1006df8bae1dSRodney W. Grimes 		 * If it's not up, return an error.
1007df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
1008df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
1009df8bae1dSRodney W. Grimes 		 * just flush the buffer.
1010df8bae1dSRodney W. Grimes 		 */
1011df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_UP) == 0)
1012df8bae1dSRodney W. Grimes 			return (ENETDOWN);
1013df8bae1dSRodney W. Grimes 
1014572bde2aSRobert Watson 		if (d->bd_sbuf == NULL) {
1015df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
1016df8bae1dSRodney W. Grimes 			if (error != 0)
1017df8bae1dSRodney W. Grimes 				return (error);
1018df8bae1dSRodney W. Grimes 		}
1019df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
1020df8bae1dSRodney W. Grimes 			if (d->bd_bif)
1021df8bae1dSRodney W. Grimes 				/*
1022df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
1023df8bae1dSRodney W. Grimes 				 */
1024df8bae1dSRodney W. Grimes 				bpf_detachd(d);
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
1027df8bae1dSRodney W. Grimes 		}
1028e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1029df8bae1dSRodney W. Grimes 		reset_d(d);
1030e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1031df8bae1dSRodney W. Grimes 		return (0);
1032df8bae1dSRodney W. Grimes 	}
1033e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1034df8bae1dSRodney W. Grimes 	/* Not found. */
1035df8bae1dSRodney W. Grimes 	return (ENXIO);
1036df8bae1dSRodney W. Grimes }
1037df8bae1dSRodney W. Grimes 
1038df8bae1dSRodney W. Grimes /*
1039243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1040df8bae1dSRodney W. Grimes  *
1041df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1042df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1043df8bae1dSRodney W. Grimes  */
104437c84183SPoul-Henning Kamp static int
1045b40ce416SJulian Elischer bpfpoll(dev, events, td)
104689c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1047243ac7d8SPeter Wemm 	int events;
1048b40ce416SJulian Elischer 	struct thread *td;
1049df8bae1dSRodney W. Grimes {
1050e7bb21b3SJonathan Lemon 	struct bpf_d *d;
10510832fc64SGarance A Drosehn 	int revents;
1052df8bae1dSRodney W. Grimes 
1053bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1054de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1055de5d9935SRobert Watson 		return (ENXIO);
1056de5d9935SRobert Watson 
10570832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1058e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
105975c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
106095aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1061243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
106281bda851SJohn Polstra 		else {
1063ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
106481bda851SJohn Polstra 			/* Start the read timeout if necessary. */
106581bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
106681bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
106781bda851SJohn Polstra 				    bpf_timed_out, d);
106881bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
106981bda851SJohn Polstra 			}
107081bda851SJohn Polstra 		}
107175c13541SPoul-Henning Kamp 	}
1072e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1073243ac7d8SPeter Wemm 	return (revents);
1074df8bae1dSRodney W. Grimes }
1075df8bae1dSRodney W. Grimes 
1076df8bae1dSRodney W. Grimes /*
107795aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
107895aab9ccSJohn-Mark Gurney  * reject all others.
107995aab9ccSJohn-Mark Gurney  */
108095aab9ccSJohn-Mark Gurney int
108195aab9ccSJohn-Mark Gurney bpfkqfilter(dev, kn)
108289c9c53dSPoul-Henning Kamp 	struct cdev *dev;
108395aab9ccSJohn-Mark Gurney 	struct knote *kn;
108495aab9ccSJohn-Mark Gurney {
108595aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
108695aab9ccSJohn-Mark Gurney 
108795aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
108895aab9ccSJohn-Mark Gurney 		return (1);
108995aab9ccSJohn-Mark Gurney 
109095aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
109195aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
109295aab9ccSJohn-Mark Gurney 	BPFD_LOCK(d);
109395aab9ccSJohn-Mark Gurney 	SLIST_INSERT_HEAD(&d->bd_sel.si_note, kn, kn_selnext);
109495aab9ccSJohn-Mark Gurney 	BPFD_UNLOCK(d);
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 
110595aab9ccSJohn-Mark Gurney 	BPFD_LOCK(d);
110695aab9ccSJohn-Mark Gurney 	SLIST_REMOVE(&d->bd_sel.si_note, kn, knote, kn_selnext);
110795aab9ccSJohn-Mark Gurney 	BPFD_UNLOCK(d);
110895aab9ccSJohn-Mark Gurney }
110995aab9ccSJohn-Mark Gurney 
111095aab9ccSJohn-Mark Gurney static int
111195aab9ccSJohn-Mark Gurney filt_bpfread(kn, hint)
111295aab9ccSJohn-Mark Gurney 	struct knote *kn;
111395aab9ccSJohn-Mark Gurney 	long hint;
111495aab9ccSJohn-Mark Gurney {
111595aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
111695aab9ccSJohn-Mark Gurney 	int ready;
111795aab9ccSJohn-Mark Gurney 
111895aab9ccSJohn-Mark Gurney 	BPFD_LOCK(d);
111995aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
112095aab9ccSJohn-Mark Gurney 	if (ready) {
112195aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
112295aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
112395aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
112495aab9ccSJohn-Mark Gurney 	}
112595aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
112695aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
112795aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
112895aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
112995aab9ccSJohn-Mark Gurney 	}
113095aab9ccSJohn-Mark Gurney 	BPFD_UNLOCK(d);
113195aab9ccSJohn-Mark Gurney 
113295aab9ccSJohn-Mark Gurney 	return (ready);
113395aab9ccSJohn-Mark Gurney }
113495aab9ccSJohn-Mark Gurney 
113595aab9ccSJohn-Mark Gurney /*
1136df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1137df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1138df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1139df8bae1dSRodney W. Grimes  * buffer.
1140df8bae1dSRodney W. Grimes  */
1141df8bae1dSRodney W. Grimes void
114224a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
114324a229f4SSam Leffler 	struct bpf_if *bp;
11448994a245SDag-Erling Smørgrav 	u_char *pkt;
11458994a245SDag-Erling Smørgrav 	u_int pktlen;
1146df8bae1dSRodney W. Grimes {
11478994a245SDag-Erling Smørgrav 	struct bpf_d *d;
11488994a245SDag-Erling Smørgrav 	u_int slen;
1149e7bb21b3SJonathan Lemon 
1150e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1151572bde2aSRobert Watson 	for (d = bp->bif_dlist; d != NULL; d = d->bd_next) {
1152e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1153df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1154df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1155ec272d87SRobert Watson 		if (slen != 0) {
1156ec272d87SRobert Watson #ifdef MAC
115724a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1158ec272d87SRobert Watson #endif
1159df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1160ec272d87SRobert Watson 		}
1161e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1162df8bae1dSRodney W. Grimes 	}
1163e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1164df8bae1dSRodney W. Grimes }
1165df8bae1dSRodney W. Grimes 
1166df8bae1dSRodney W. Grimes /*
1167df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1168df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1169df8bae1dSRodney W. Grimes  */
1170df8bae1dSRodney W. Grimes static void
1171df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1172df8bae1dSRodney W. Grimes 	const void *src_arg;
1173df8bae1dSRodney W. Grimes 	void *dst_arg;
11748994a245SDag-Erling Smørgrav 	size_t len;
1175df8bae1dSRodney W. Grimes {
11768994a245SDag-Erling Smørgrav 	const struct mbuf *m;
11778994a245SDag-Erling Smørgrav 	u_int count;
1178df8bae1dSRodney W. Grimes 	u_char *dst;
1179df8bae1dSRodney W. Grimes 
1180df8bae1dSRodney W. Grimes 	m = src_arg;
1181df8bae1dSRodney W. Grimes 	dst = dst_arg;
1182df8bae1dSRodney W. Grimes 	while (len > 0) {
1183572bde2aSRobert Watson 		if (m == NULL)
1184df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1185df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
11860453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1187df8bae1dSRodney W. Grimes 		m = m->m_next;
1188df8bae1dSRodney W. Grimes 		dst += count;
1189df8bae1dSRodney W. Grimes 		len -= count;
1190df8bae1dSRodney W. Grimes 	}
1191df8bae1dSRodney W. Grimes }
1192df8bae1dSRodney W. Grimes 
1193df8bae1dSRodney W. Grimes /*
1194df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1195df8bae1dSRodney W. Grimes  */
1196df8bae1dSRodney W. Grimes void
119724a229f4SSam Leffler bpf_mtap(bp, m)
119824a229f4SSam Leffler 	struct bpf_if *bp;
1199df8bae1dSRodney W. Grimes 	struct mbuf *m;
1200df8bae1dSRodney W. Grimes {
1201df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1202df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1203df8bae1dSRodney W. Grimes 
1204f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
12057b831242SPoul-Henning Kamp 	if (pktlen == m->m_len) {
120624a229f4SSam Leffler 		bpf_tap(bp, mtod(m, u_char *), pktlen);
12077b831242SPoul-Henning Kamp 		return;
12087b831242SPoul-Henning Kamp 	}
1209df8bae1dSRodney W. Grimes 
1210e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1211572bde2aSRobert Watson 	for (d = bp->bif_dlist; d != NULL; d = d->bd_next) {
12128ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
12138ed3828cSRobert Watson 			continue;
1214e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1215df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1216df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1217df8bae1dSRodney W. Grimes 		if (slen != 0)
12180c7fb534SRobert Watson #ifdef MAC
121924a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
12200c7fb534SRobert Watson #endif
12210c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
12220c7fb534SRobert Watson 				    bpf_mcopy);
1223e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1224df8bae1dSRodney W. Grimes 	}
1225e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1226df8bae1dSRodney W. Grimes }
1227df8bae1dSRodney W. Grimes 
1228df8bae1dSRodney W. Grimes /*
1229437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1230437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1231437ffe18SSam Leffler  */
1232437ffe18SSam Leffler void
1233437ffe18SSam Leffler bpf_mtap2(bp, data, dlen, m)
1234437ffe18SSam Leffler 	struct bpf_if *bp;
1235437ffe18SSam Leffler 	void *data;
1236437ffe18SSam Leffler 	u_int dlen;
1237437ffe18SSam Leffler 	struct mbuf *m;
1238437ffe18SSam Leffler {
1239437ffe18SSam Leffler 	struct mbuf mb;
1240437ffe18SSam Leffler 	struct bpf_d *d;
1241437ffe18SSam Leffler 	u_int pktlen, slen;
1242437ffe18SSam Leffler 
1243437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1244437ffe18SSam Leffler 	/*
1245437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1246437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1247437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1248437ffe18SSam Leffler 	 */
1249437ffe18SSam Leffler 	mb.m_next = m;
1250437ffe18SSam Leffler 	mb.m_data = data;
1251437ffe18SSam Leffler 	mb.m_len = dlen;
1252437ffe18SSam Leffler 	pktlen += dlen;
1253437ffe18SSam Leffler 
1254437ffe18SSam Leffler 	BPFIF_LOCK(bp);
1255572bde2aSRobert Watson 	for (d = bp->bif_dlist; d != NULL; d = d->bd_next) {
1256437ffe18SSam Leffler 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1257437ffe18SSam Leffler 			continue;
1258437ffe18SSam Leffler 		BPFD_LOCK(d);
1259437ffe18SSam Leffler 		++d->bd_rcount;
1260437ffe18SSam Leffler 		slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1261437ffe18SSam Leffler 		if (slen != 0)
1262437ffe18SSam Leffler #ifdef MAC
1263437ffe18SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1264437ffe18SSam Leffler #endif
1265437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
1266437ffe18SSam Leffler 				    bpf_mcopy);
1267437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1268437ffe18SSam Leffler 	}
1269437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1270437ffe18SSam Leffler }
1271437ffe18SSam Leffler 
1272437ffe18SSam Leffler /*
1273df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
12749e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1275df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1276df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1277df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1278df8bae1dSRodney W. Grimes  */
1279df8bae1dSRodney W. Grimes static void
1280df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
12818994a245SDag-Erling Smørgrav 	struct bpf_d *d;
12828994a245SDag-Erling Smørgrav 	u_char *pkt;
12838994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
12848994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1285df8bae1dSRodney W. Grimes {
12868994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
12878994a245SDag-Erling Smørgrav 	int totlen, curlen;
12888994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
12899e610888SDag-Erling Smørgrav 
1290df8bae1dSRodney W. Grimes 	/*
1291df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1292df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1293df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1294df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1295df8bae1dSRodney W. Grimes 	 */
1296df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1297df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1298df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1299df8bae1dSRodney W. Grimes 
1300df8bae1dSRodney W. Grimes 	/*
1301df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1302df8bae1dSRodney W. Grimes 	 */
1303df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1304df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1305df8bae1dSRodney W. Grimes 		/*
1306df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1307df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1308df8bae1dSRodney W. Grimes 		 * pending reads.
1309df8bae1dSRodney W. Grimes 		 */
1310572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1311df8bae1dSRodney W. Grimes 			/*
1312df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1313df8bae1dSRodney W. Grimes 			 * so drop the packet.
1314df8bae1dSRodney W. Grimes 			 */
1315df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1316df8bae1dSRodney W. Grimes 			return;
1317df8bae1dSRodney W. Grimes 		}
1318df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
1319df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1320df8bae1dSRodney W. Grimes 		curlen = 0;
1321df8bae1dSRodney W. Grimes 	}
132281bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1323df8bae1dSRodney W. Grimes 		/*
132481bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
132581bda851SJohn Polstra 		 * already expired during a select call.  A packet
132681bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1327df8bae1dSRodney W. Grimes 		 */
1328df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1329df8bae1dSRodney W. Grimes 
1330df8bae1dSRodney W. Grimes 	/*
1331df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1332df8bae1dSRodney W. Grimes 	 */
1333df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1334df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1335df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1336df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1337df8bae1dSRodney W. Grimes 	/*
1338df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1339df8bae1dSRodney W. Grimes 	 */
1340df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1341df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
1342df8bae1dSRodney W. Grimes }
1343df8bae1dSRodney W. Grimes 
1344df8bae1dSRodney W. Grimes /*
1345df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1346df8bae1dSRodney W. Grimes  */
1347df8bae1dSRodney W. Grimes static int
1348df8bae1dSRodney W. Grimes bpf_allocbufs(d)
13498994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1350df8bae1dSRodney W. Grimes {
1351a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1352572bde2aSRobert Watson 	if (d->bd_fbuf == NULL)
1353df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1354df8bae1dSRodney W. Grimes 
1355a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1356572bde2aSRobert Watson 	if (d->bd_sbuf == NULL) {
1357bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1358df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1359df8bae1dSRodney W. Grimes 	}
1360df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1361df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1362df8bae1dSRodney W. Grimes 	return (0);
1363df8bae1dSRodney W. Grimes }
1364df8bae1dSRodney W. Grimes 
1365df8bae1dSRodney W. Grimes /*
1366df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1367df8bae1dSRodney W. Grimes  * Called on close.
1368df8bae1dSRodney W. Grimes  */
1369df8bae1dSRodney W. Grimes static void
1370df8bae1dSRodney W. Grimes bpf_freed(d)
13718994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1372df8bae1dSRodney W. Grimes {
1373df8bae1dSRodney W. Grimes 	/*
1374df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1375df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1376df8bae1dSRodney W. Grimes 	 * free.
1377df8bae1dSRodney W. Grimes 	 */
1378572bde2aSRobert Watson 	if (d->bd_sbuf != NULL) {
1379bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1380572bde2aSRobert Watson 		if (d->bd_hbuf != NULL)
1381bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1382572bde2aSRobert Watson 		if (d->bd_fbuf != NULL)
1383bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1384df8bae1dSRodney W. Grimes 	}
1385df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1386bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1387e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1388df8bae1dSRodney W. Grimes }
1389df8bae1dSRodney W. Grimes 
1390df8bae1dSRodney W. Grimes /*
139124a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
139224a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1393df8bae1dSRodney W. Grimes  */
1394df8bae1dSRodney W. Grimes void
13959b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1396df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1397df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1398df8bae1dSRodney W. Grimes {
139924a229f4SSam Leffler 
140024a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
140124a229f4SSam Leffler }
140224a229f4SSam Leffler 
140324a229f4SSam Leffler /*
140424a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
140524a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
140624a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
140724a229f4SSam Leffler  * headers are not yet supporrted).
140824a229f4SSam Leffler  */
140924a229f4SSam Leffler void
141024a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
141124a229f4SSam Leffler 	struct ifnet *ifp;
141224a229f4SSam Leffler 	u_int dlt, hdrlen;
141324a229f4SSam Leffler 	struct bpf_if **driverp;
141424a229f4SSam Leffler {
1415df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
14166a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1417572bde2aSRobert Watson 	if (bp == NULL)
1418df8bae1dSRodney W. Grimes 		panic("bpfattach");
1419df8bae1dSRodney W. Grimes 
1420572bde2aSRobert Watson 	bp->bif_dlist = NULL;
142124a229f4SSam Leffler 	bp->bif_driverp = driverp;
1422df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1423df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
14246008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1425df8bae1dSRodney W. Grimes 
1426e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
1427df8bae1dSRodney W. Grimes 	bp->bif_next = bpf_iflist;
1428df8bae1dSRodney W. Grimes 	bpf_iflist = bp;
1429e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1430df8bae1dSRodney W. Grimes 
1431572bde2aSRobert Watson 	*bp->bif_driverp = NULL;
1432df8bae1dSRodney W. Grimes 
1433df8bae1dSRodney W. Grimes 	/*
1434df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1435df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1436df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1437df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1438df8bae1dSRodney W. Grimes 	 */
1439df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1440df8bae1dSRodney W. Grimes 
14412eeab939SGarrett Wollman 	if (bootverbose)
144224a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1443df8bae1dSRodney W. Grimes }
144453ac6efbSJulian Elischer 
1445de5d9935SRobert Watson /*
1446de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1447de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1448de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1449de5d9935SRobert Watson  * ENXIO.
1450de5d9935SRobert Watson  */
1451de5d9935SRobert Watson void
1452de5d9935SRobert Watson bpfdetach(ifp)
1453de5d9935SRobert Watson 	struct ifnet *ifp;
1454de5d9935SRobert Watson {
1455de5d9935SRobert Watson 	struct bpf_if	*bp, *bp_prev;
1456de5d9935SRobert Watson 	struct bpf_d	*d;
1457de5d9935SRobert Watson 
1458de5d9935SRobert Watson 	/* Locate BPF interface information */
1459de5d9935SRobert Watson 	bp_prev = NULL;
14608eab61f3SSam Leffler 
14618eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
1462de5d9935SRobert Watson 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1463de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1464de5d9935SRobert Watson 			break;
1465de5d9935SRobert Watson 		bp_prev = bp;
1466de5d9935SRobert Watson 	}
1467de5d9935SRobert Watson 
1468de5d9935SRobert Watson 	/* Interface wasn't attached */
1469d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1470e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
14719bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1472de5d9935SRobert Watson 		return;
1473de5d9935SRobert Watson 	}
1474de5d9935SRobert Watson 
1475de5d9935SRobert Watson 	if (bp_prev) {
1476de5d9935SRobert Watson 		bp_prev->bif_next = bp->bif_next;
1477de5d9935SRobert Watson 	} else {
1478de5d9935SRobert Watson 		bpf_iflist = bp->bif_next;
1479de5d9935SRobert Watson 	}
14808eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1481de5d9935SRobert Watson 
1482e7bb21b3SJonathan Lemon 	while ((d = bp->bif_dlist) != NULL) {
1483e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1484e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1485e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1486e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1487e7bb21b3SJonathan Lemon 	}
1488e7bb21b3SJonathan Lemon 
1489e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1490de5d9935SRobert Watson 	free(bp, M_BPF);
14918eab61f3SSam Leffler }
1492de5d9935SRobert Watson 
14938eab61f3SSam Leffler /*
14948eab61f3SSam Leffler  * Get a list of available data link type of the interface.
14958eab61f3SSam Leffler  */
14968eab61f3SSam Leffler static int
14978eab61f3SSam Leffler bpf_getdltlist(d, bfl)
14988eab61f3SSam Leffler 	struct bpf_d *d;
14998eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
15008eab61f3SSam Leffler {
15018eab61f3SSam Leffler 	int n, error;
15028eab61f3SSam Leffler 	struct ifnet *ifp;
15038eab61f3SSam Leffler 	struct bpf_if *bp;
15048eab61f3SSam Leffler 
15058eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15068eab61f3SSam Leffler 	n = 0;
15078eab61f3SSam Leffler 	error = 0;
15088eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15098eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
15108eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
15118eab61f3SSam Leffler 			continue;
15128eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
15138eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1514e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
15158eab61f3SSam Leffler 				return (ENOMEM);
15168eab61f3SSam Leffler 			}
15178eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
15188eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
15198eab61f3SSam Leffler 		}
15208eab61f3SSam Leffler 		n++;
15218eab61f3SSam Leffler 	}
15228eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15238eab61f3SSam Leffler 	bfl->bfl_len = n;
15248eab61f3SSam Leffler 	return (error);
15258eab61f3SSam Leffler }
15268eab61f3SSam Leffler 
15278eab61f3SSam Leffler /*
15288eab61f3SSam Leffler  * Set the data link type of a BPF instance.
15298eab61f3SSam Leffler  */
15308eab61f3SSam Leffler static int
15318eab61f3SSam Leffler bpf_setdlt(d, dlt)
15328eab61f3SSam Leffler 	struct bpf_d *d;
15338eab61f3SSam Leffler 	u_int dlt;
15348eab61f3SSam Leffler {
15358eab61f3SSam Leffler 	int error, opromisc;
15368eab61f3SSam Leffler 	struct ifnet *ifp;
15378eab61f3SSam Leffler 	struct bpf_if *bp;
15388eab61f3SSam Leffler 
15398eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
15408eab61f3SSam Leffler 		return (0);
15418eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15428eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15438eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
15448eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
15458eab61f3SSam Leffler 			break;
15468eab61f3SSam Leffler 	}
15478eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15488eab61f3SSam Leffler 	if (bp != NULL) {
15498eab61f3SSam Leffler 		BPFD_LOCK(d);
15508eab61f3SSam Leffler 		opromisc = d->bd_promisc;
15518eab61f3SSam Leffler 		bpf_detachd(d);
15528eab61f3SSam Leffler 		bpf_attachd(d, bp);
15538eab61f3SSam Leffler 		reset_d(d);
15548eab61f3SSam Leffler 		BPFD_UNLOCK(d);
15558eab61f3SSam Leffler 		if (opromisc) {
15568eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
15578eab61f3SSam Leffler 			if (error)
15588eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
15598eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
15608eab61f3SSam Leffler 					error);
15618eab61f3SSam Leffler 			else
15628eab61f3SSam Leffler 				d->bd_promisc = 1;
15638eab61f3SSam Leffler 		}
15648eab61f3SSam Leffler 	}
15658eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1566de5d9935SRobert Watson }
1567de5d9935SRobert Watson 
1568929ddbbbSAlfred Perlstein static void bpf_drvinit(void *unused);
1569bd3a5320SPoul-Henning Kamp 
157089c9c53dSPoul-Henning Kamp static void bpf_clone(void *arg, char *name, int namelen, struct cdev **dev);
15713f54a085SPoul-Henning Kamp 
15723f54a085SPoul-Henning Kamp static void
15733f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
15743f54a085SPoul-Henning Kamp 	void *arg;
15753f54a085SPoul-Henning Kamp 	char *name;
15763f54a085SPoul-Henning Kamp 	int namelen;
157789c9c53dSPoul-Henning Kamp 	struct cdev **dev;
15783f54a085SPoul-Henning Kamp {
15793f54a085SPoul-Henning Kamp 	int u;
15803f54a085SPoul-Henning Kamp 
1581f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
15823f54a085SPoul-Henning Kamp 		return;
1583db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
15843f54a085SPoul-Henning Kamp 		return;
1585b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1586b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1587b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
15883f54a085SPoul-Henning Kamp 	return;
15893f54a085SPoul-Henning Kamp }
15903f54a085SPoul-Henning Kamp 
1591514ede09SBruce Evans static void
1592514ede09SBruce Evans bpf_drvinit(unused)
1593514ede09SBruce Evans 	void *unused;
159453ac6efbSJulian Elischer {
159553ac6efbSJulian Elischer 
15966008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1597db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
15987198bf47SJulian Elischer }
159953ac6efbSJulian Elischer 
1600c9c7976fSPoul-Henning Kamp SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
160153ac6efbSJulian Elischer 
16025bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1603f8dc4716SMike Smith /*
1604f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1605f8dc4716SMike Smith  *
1606f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1607f8dc4716SMike Smith  * to be loaded at runtime.
1608f8dc4716SMike Smith  */
1609f8dc4716SMike Smith 
1610f8dc4716SMike Smith void
1611e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1612e5562beeSSam Leffler 	struct bpf_if *bp;
16138994a245SDag-Erling Smørgrav 	u_char *pkt;
16148994a245SDag-Erling Smørgrav 	u_int pktlen;
1615f8dc4716SMike Smith {
1616f8dc4716SMike Smith }
1617f8dc4716SMike Smith 
1618f8dc4716SMike Smith void
1619e5562beeSSam Leffler bpf_mtap(bp, m)
1620e5562beeSSam Leffler 	struct bpf_if *bp;
1621f8dc4716SMike Smith 	struct mbuf *m;
1622f8dc4716SMike Smith {
1623f8dc4716SMike Smith }
1624f8dc4716SMike Smith 
1625f8dc4716SMike Smith void
1626437ffe18SSam Leffler bpf_mtap2(bp, d, l, m)
1627437ffe18SSam Leffler 	struct bpf_if *bp;
1628c1dae2f0STim J. Robbins 	void *d;
1629437ffe18SSam Leffler 	u_int l;
1630437ffe18SSam Leffler 	struct mbuf *m;
1631437ffe18SSam Leffler {
1632437ffe18SSam Leffler }
1633437ffe18SSam Leffler 
1634437ffe18SSam Leffler void
1635f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1636f8dc4716SMike Smith 	struct ifnet *ifp;
1637f8dc4716SMike Smith 	u_int dlt, hdrlen;
1638f8dc4716SMike Smith {
1639f8dc4716SMike Smith }
1640f8dc4716SMike Smith 
1641da626c17SBill Paul void
16425f7a7923SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
16435f7a7923SSam Leffler 	struct ifnet *ifp;
16445f7a7923SSam Leffler 	u_int dlt, hdrlen;
16455f7a7923SSam Leffler 	struct bpf_if **driverp;
16465f7a7923SSam Leffler {
16475f7a7923SSam Leffler }
16485f7a7923SSam Leffler 
16495f7a7923SSam Leffler void
1650da626c17SBill Paul bpfdetach(ifp)
1651da626c17SBill Paul 	struct ifnet *ifp;
1652da626c17SBill Paul {
1653da626c17SBill Paul }
1654da626c17SBill Paul 
1655f8dc4716SMike Smith u_int
1656f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
16578994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
16588994a245SDag-Erling Smørgrav 	u_char *p;
1659f8dc4716SMike Smith 	u_int wirelen;
16608994a245SDag-Erling Smørgrav 	u_int buflen;
1661f8dc4716SMike Smith {
1662f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1663f8dc4716SMike Smith }
1664f8dc4716SMike Smith 
16655bb5f2c9SPeter Wemm int
16665bb5f2c9SPeter Wemm bpf_validate(f, len)
16675bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
16685bb5f2c9SPeter Wemm 	int len;
16695bb5f2c9SPeter Wemm {
16705bb5f2c9SPeter Wemm 	return 0;		/* false */
16715bb5f2c9SPeter Wemm }
16725bb5f2c9SPeter Wemm 
16735bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1674