xref: /freebsd/sys/net/bpf.c (revision 7ac40f5f59dea6e9ab4869e974bdd4026274e921)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from the Stanford/CMU enet packet filter,
6df8bae1dSRodney W. Grimes  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7df8bae1dSRodney W. Grimes  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8df8bae1dSRodney W. Grimes  * Berkeley Laboratory.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
384f252c4dSRuslan Ermilov  *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
39df8bae1dSRodney W. Grimes  *
40c3aac50fSPeter Wemm  * $FreeBSD$
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
435bb5f2c9SPeter Wemm #include "opt_bpf.h"
4482f4445dSRobert Watson #include "opt_mac.h"
455bb5f2c9SPeter Wemm #include "opt_netgraph.h"
46df8bae1dSRodney W. Grimes 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
48df8bae1dSRodney W. Grimes #include <sys/systm.h>
49ce7609a4SBruce Evans #include <sys/conf.h>
5082f4445dSRobert Watson #include <sys/mac.h>
514d1d4912SBruce Evans #include <sys/malloc.h>
52df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
53df8bae1dSRodney W. Grimes #include <sys/time.h>
54df8bae1dSRodney W. Grimes #include <sys/proc.h>
550310c19fSBruce Evans #include <sys/signalvar.h>
56528f627fSBruce Evans #include <sys/filio.h>
57528f627fSBruce Evans #include <sys/sockio.h>
58528f627fSBruce Evans #include <sys/ttycom.h>
59831d27a9SDon Lewis #include <sys/filedesc.h>
60df8bae1dSRodney W. Grimes 
61243ac7d8SPeter Wemm #include <sys/poll.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);
114df8bae1dSRodney W. Grimes 
11587f6c662SJulian Elischer static	d_open_t	bpfopen;
11687f6c662SJulian Elischer static	d_close_t	bpfclose;
11787f6c662SJulian Elischer static	d_read_t	bpfread;
11887f6c662SJulian Elischer static	d_write_t	bpfwrite;
11987f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
120243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
12187f6c662SJulian Elischer 
12287f6c662SJulian Elischer #define CDEV_MAJOR 23
1234e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
1247ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1257ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1267ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1277ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1287ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1297ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1307ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
1317ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
1324e2f199eSPoul-Henning Kamp };
13387f6c662SJulian Elischer 
13487f6c662SJulian Elischer 
135df8bae1dSRodney W. Grimes static int
136df8bae1dSRodney W. Grimes bpf_movein(uio, linktype, mp, sockp, datlen)
1378994a245SDag-Erling Smørgrav 	struct uio *uio;
138df8bae1dSRodney W. Grimes 	int linktype, *datlen;
1398994a245SDag-Erling Smørgrav 	struct mbuf **mp;
1408994a245SDag-Erling Smørgrav 	struct sockaddr *sockp;
141df8bae1dSRodney W. Grimes {
142df8bae1dSRodney W. Grimes 	struct mbuf *m;
143df8bae1dSRodney W. Grimes 	int error;
144df8bae1dSRodney W. Grimes 	int len;
145df8bae1dSRodney W. Grimes 	int hlen;
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes 	/*
148df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
149df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
150df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
151df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
152df8bae1dSRodney W. Grimes 	 * is forwarded as is.
153df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
154df8bae1dSRodney W. Grimes 	 * for the link level header.
155df8bae1dSRodney W. Grimes 	 */
156df8bae1dSRodney W. Grimes 	switch (linktype) {
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes 	case DLT_SLIP:
159df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
160df8bae1dSRodney W. Grimes 		hlen = 0;
161df8bae1dSRodney W. Grimes 		break;
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
164df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
165df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
166797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
167df8bae1dSRodney W. Grimes 		break;
168df8bae1dSRodney W. Grimes 
169df8bae1dSRodney W. Grimes 	case DLT_FDDI:
170d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
171d41f24e7SDavid Greenman 		hlen = 0;
172df8bae1dSRodney W. Grimes 		break;
173df8bae1dSRodney W. Grimes 
17422f05c43SAndrey A. Chernov 	case DLT_RAW:
175df8bae1dSRodney W. Grimes 	case DLT_NULL:
176df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
177df8bae1dSRodney W. Grimes 		hlen = 0;
178df8bae1dSRodney W. Grimes 		break;
179df8bae1dSRodney W. Grimes 
1804f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
1814f53e3ccSKenjiro Cho 		/*
1824f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
1834f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
1844f53e3ccSKenjiro Cho 		 * specified anyway.
1854f53e3ccSKenjiro Cho 		 */
1864f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
1874f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
1884f53e3ccSKenjiro Cho 		break;
1894f53e3ccSKenjiro Cho 
19030fa52a6SBrian Somers 	case DLT_PPP:
19130fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
19230fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
19330fa52a6SBrian Somers 		break;
19430fa52a6SBrian Somers 
195df8bae1dSRodney W. Grimes 	default:
196df8bae1dSRodney W. Grimes 		return (EIO);
197df8bae1dSRodney W. Grimes 	}
198df8bae1dSRodney W. Grimes 
199df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
200df8bae1dSRodney W. Grimes 	*datlen = len - hlen;
201df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
202df8bae1dSRodney W. Grimes 		return (EIO);
203df8bae1dSRodney W. Grimes 
204963e4c2aSGarrett Wollman 	if (len > MHLEN) {
205a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
20624a229f4SSam Leffler 	} else {
207a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
208df8bae1dSRodney W. Grimes 	}
20924a229f4SSam Leffler 	if (m == NULL)
21024a229f4SSam Leffler 		return (ENOBUFS);
211963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
212963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
213df8bae1dSRodney W. Grimes 	*mp = m;
21424a229f4SSam Leffler 
215df8bae1dSRodney W. Grimes 	/*
216df8bae1dSRodney W. Grimes 	 * Make room for link header.
217df8bae1dSRodney W. Grimes 	 */
218df8bae1dSRodney W. Grimes 	if (hlen != 0) {
2194f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
220df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
221df8bae1dSRodney W. Grimes #if BSD >= 199103
222df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
223df8bae1dSRodney W. Grimes #else
224df8bae1dSRodney W. Grimes 		m->m_off += hlen;
225df8bae1dSRodney W. Grimes #endif
226c9524588SDag-Erling Smørgrav 		error = uiomove(sockp->sa_data, hlen, uio);
227df8bae1dSRodney W. Grimes 		if (error)
228df8bae1dSRodney W. Grimes 			goto bad;
229df8bae1dSRodney W. Grimes 	}
230c9524588SDag-Erling Smørgrav 	error = uiomove(mtod(m, void *), len - hlen, uio);
231df8bae1dSRodney W. Grimes 	if (!error)
232df8bae1dSRodney W. Grimes 		return (0);
233df8bae1dSRodney W. Grimes bad:
234df8bae1dSRodney W. Grimes 	m_freem(m);
235df8bae1dSRodney W. Grimes 	return (error);
236df8bae1dSRodney W. Grimes }
237df8bae1dSRodney W. Grimes 
238df8bae1dSRodney W. Grimes /*
239df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
240df8bae1dSRodney W. Grimes  */
241df8bae1dSRodney W. Grimes static void
242df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
243df8bae1dSRodney W. Grimes 	struct bpf_d *d;
244df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
245df8bae1dSRodney W. Grimes {
246df8bae1dSRodney W. Grimes 	/*
247df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
248df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
249df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
250df8bae1dSRodney W. Grimes 	 */
251e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
252df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
253df8bae1dSRodney W. Grimes 	d->bd_next = bp->bif_dlist;
254df8bae1dSRodney W. Grimes 	bp->bif_dlist = d;
255df8bae1dSRodney W. Grimes 
25624a229f4SSam Leffler 	*bp->bif_driverp = bp;
257e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
258df8bae1dSRodney W. Grimes }
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes /*
261df8bae1dSRodney W. Grimes  * Detach a file from its interface.
262df8bae1dSRodney W. Grimes  */
263df8bae1dSRodney W. Grimes static void
264df8bae1dSRodney W. Grimes bpf_detachd(d)
265df8bae1dSRodney W. Grimes 	struct bpf_d *d;
266df8bae1dSRodney W. Grimes {
2676e891d64SPoul-Henning Kamp 	int error;
268df8bae1dSRodney W. Grimes 	struct bpf_d **p;
269df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
272df8bae1dSRodney W. Grimes 	/*
273df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
274df8bae1dSRodney W. Grimes 	 * If so, turn it off.
275df8bae1dSRodney W. Grimes 	 */
276df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
277df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
2786e891d64SPoul-Henning Kamp 		error = ifpromisc(bp->bif_ifp, 0);
2796e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
280df8bae1dSRodney W. Grimes 			/*
2816e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
282df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
283df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
284df8bae1dSRodney W. Grimes 			 * take it out.
285df8bae1dSRodney W. Grimes 			 */
2868eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
2878eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
2886e891d64SPoul-Henning Kamp 		}
289df8bae1dSRodney W. Grimes 	}
290df8bae1dSRodney W. Grimes 	/* Remove d from the interface's descriptor list. */
291e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
292df8bae1dSRodney W. Grimes 	p = &bp->bif_dlist;
293df8bae1dSRodney W. Grimes 	while (*p != d) {
294df8bae1dSRodney W. Grimes 		p = &(*p)->bd_next;
295df8bae1dSRodney W. Grimes 		if (*p == 0)
296df8bae1dSRodney W. Grimes 			panic("bpf_detachd: descriptor not in list");
297df8bae1dSRodney W. Grimes 	}
298df8bae1dSRodney W. Grimes 	*p = (*p)->bd_next;
299df8bae1dSRodney W. Grimes 	if (bp->bif_dlist == 0)
300df8bae1dSRodney W. Grimes 		/*
301df8bae1dSRodney W. Grimes 		 * Let the driver know that there are no more listeners.
302df8bae1dSRodney W. Grimes 		 */
30324a229f4SSam Leffler 		*d->bd_bif->bif_driverp = 0;
304e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
305df8bae1dSRodney W. Grimes 	d->bd_bif = 0;
306df8bae1dSRodney W. Grimes }
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes /*
309df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
310df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
311df8bae1dSRodney W. Grimes  */
312df8bae1dSRodney W. Grimes /* ARGSUSED */
31387f6c662SJulian Elischer static	int
314b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
315df8bae1dSRodney W. Grimes 	dev_t dev;
31660039670SBruce Evans 	int flags;
31760039670SBruce Evans 	int fmt;
318b40ce416SJulian Elischer 	struct thread *td;
319df8bae1dSRodney W. Grimes {
320e7bb21b3SJonathan Lemon 	struct bpf_d *d;
321df8bae1dSRodney W. Grimes 
322e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
323bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
324df8bae1dSRodney W. Grimes 	/*
325df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
326df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
327df8bae1dSRodney W. Grimes 	 */
328e7bb21b3SJonathan Lemon 	if (d) {
329e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
330df8bae1dSRodney W. Grimes 		return (EBUSY);
331e7bb21b3SJonathan Lemon 	}
332e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
333e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
334e7bb21b3SJonathan Lemon 
335d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
336b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
337b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
338a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
339bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
340df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
34100a83887SPaul Traina 	d->bd_sig = SIGIO;
3428ed3828cSRobert Watson 	d->bd_seesent = 1;
34382f4445dSRobert Watson #ifdef MAC
34482f4445dSRobert Watson 	mac_init_bpfdesc(d);
34582f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
34682f4445dSRobert Watson #endif
3476008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
34881bda851SJohn Polstra 	callout_init(&d->bd_callout, 1);
349df8bae1dSRodney W. Grimes 
350df8bae1dSRodney W. Grimes 	return (0);
351df8bae1dSRodney W. Grimes }
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes /*
354df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
355df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
356df8bae1dSRodney W. Grimes  */
357df8bae1dSRodney W. Grimes /* ARGSUSED */
35887f6c662SJulian Elischer static	int
359b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
360df8bae1dSRodney W. Grimes 	dev_t dev;
36160039670SBruce Evans 	int flags;
36260039670SBruce Evans 	int fmt;
363b40ce416SJulian Elischer 	struct thread *td;
364df8bae1dSRodney W. Grimes {
365e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
366df8bae1dSRodney W. Grimes 
36781bda851SJohn Polstra 	BPFD_LOCK(d);
36881bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
36981bda851SJohn Polstra 		callout_stop(&d->bd_callout);
37081bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
37181bda851SJohn Polstra 	BPFD_UNLOCK(d);
372e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
373e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
374df8bae1dSRodney W. Grimes 	if (d->bd_bif)
375df8bae1dSRodney W. Grimes 		bpf_detachd(d);
376e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
37782f4445dSRobert Watson #ifdef MAC
37882f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
37982f4445dSRobert Watson #endif /* MAC */
380df8bae1dSRodney W. Grimes 	bpf_freed(d);
381bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = 0;
382d722be54SLuigi Rizzo 	free(d, M_BPF);
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes 	return (0);
385df8bae1dSRodney W. Grimes }
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes 
388df8bae1dSRodney W. Grimes /*
389df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
390df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
391df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
392df8bae1dSRodney W. Grimes  */
393df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
394df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
395df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
396df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
397df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
398df8bae1dSRodney W. Grimes 	(d)->bd_fbuf = 0;
399df8bae1dSRodney W. Grimes /*
400df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
401df8bae1dSRodney W. Grimes  */
40287f6c662SJulian Elischer static	int
40360039670SBruce Evans bpfread(dev, uio, ioflag)
404df8bae1dSRodney W. Grimes 	dev_t dev;
4058994a245SDag-Erling Smørgrav 	struct uio *uio;
40660039670SBruce Evans 	int ioflag;
407df8bae1dSRodney W. Grimes {
408e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
40981bda851SJohn Polstra 	int timed_out;
410df8bae1dSRodney W. Grimes 	int error;
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	/*
413df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
414df8bae1dSRodney W. Grimes 	 * as kernel buffers.
415df8bae1dSRodney W. Grimes 	 */
416df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
417df8bae1dSRodney W. Grimes 		return (EINVAL);
418df8bae1dSRodney W. Grimes 
419e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
42081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
42181bda851SJohn Polstra 		callout_stop(&d->bd_callout);
42281bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
42381bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
424df8bae1dSRodney W. Grimes 	/*
425df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
426df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
427df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
428df8bae1dSRodney W. Grimes 	 */
429df8bae1dSRodney W. Grimes 	while (d->bd_hbuf == 0) {
43081bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
431df8bae1dSRodney W. Grimes 			/*
432df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
433df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
434df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
435df8bae1dSRodney W. Grimes 			 */
436df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
437df8bae1dSRodney W. Grimes 			break;
438df8bae1dSRodney W. Grimes 		}
439de5d9935SRobert Watson 
440de5d9935SRobert Watson 		/*
441de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
442de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
443de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
444de5d9935SRobert Watson 		 * it before using it again.
445de5d9935SRobert Watson 		 */
446de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
447e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
448de5d9935SRobert Watson 			return (ENXIO);
449de5d9935SRobert Watson 		}
450de5d9935SRobert Watson 
451fba3cfdeSJohn Polstra 		if (ioflag & IO_NDELAY) {
452e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
453fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
454fba3cfdeSJohn Polstra 		}
455521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
456e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
457df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
458e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
459df8bae1dSRodney W. Grimes 			return (error);
460df8bae1dSRodney W. Grimes 		}
461df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
462df8bae1dSRodney W. Grimes 			/*
463df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
464df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
465df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
466df8bae1dSRodney W. Grimes 			 */
467df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
468df8bae1dSRodney W. Grimes 				/*
469df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
470df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
471df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
472df8bae1dSRodney W. Grimes 				 */
473df8bae1dSRodney W. Grimes 				break;
474df8bae1dSRodney W. Grimes 
475df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
476e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
477df8bae1dSRodney W. Grimes 				return (0);
478df8bae1dSRodney W. Grimes 			}
479df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
480df8bae1dSRodney W. Grimes 			break;
481df8bae1dSRodney W. Grimes 		}
482df8bae1dSRodney W. Grimes 	}
483df8bae1dSRodney W. Grimes 	/*
484df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
485df8bae1dSRodney W. Grimes 	 */
486e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
487df8bae1dSRodney W. Grimes 
488df8bae1dSRodney W. Grimes 	/*
489df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
490df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
491df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
492df8bae1dSRodney W. Grimes 	 */
493e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
494df8bae1dSRodney W. Grimes 
495e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
496df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
497df8bae1dSRodney W. Grimes 	d->bd_hbuf = 0;
498df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
499e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
500df8bae1dSRodney W. Grimes 
501df8bae1dSRodney W. Grimes 	return (error);
502df8bae1dSRodney W. Grimes }
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes /*
506df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
507df8bae1dSRodney W. Grimes  */
508e7bb21b3SJonathan Lemon static __inline void
509df8bae1dSRodney W. Grimes bpf_wakeup(d)
5108994a245SDag-Erling Smørgrav 	struct bpf_d *d;
511df8bae1dSRodney W. Grimes {
51281bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
51381bda851SJohn Polstra 		callout_stop(&d->bd_callout);
51481bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
51581bda851SJohn Polstra 	}
516521f364bSDag-Erling Smørgrav 	wakeup(d);
517831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
518f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
51900a83887SPaul Traina 
520df8bae1dSRodney W. Grimes 	selwakeup(&d->bd_sel);
521df8bae1dSRodney W. Grimes }
522df8bae1dSRodney W. Grimes 
52381bda851SJohn Polstra static void
52481bda851SJohn Polstra bpf_timed_out(arg)
52581bda851SJohn Polstra 	void *arg;
52681bda851SJohn Polstra {
52781bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
52881bda851SJohn Polstra 
52981bda851SJohn Polstra 	BPFD_LOCK(d);
53081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
53181bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
53281bda851SJohn Polstra 		if (d->bd_slen != 0)
53381bda851SJohn Polstra 			bpf_wakeup(d);
53481bda851SJohn Polstra 	}
53581bda851SJohn Polstra 	BPFD_UNLOCK(d);
53681bda851SJohn Polstra }
53781bda851SJohn Polstra 
53887f6c662SJulian Elischer static	int
53960039670SBruce Evans bpfwrite(dev, uio, ioflag)
540df8bae1dSRodney W. Grimes 	dev_t dev;
541df8bae1dSRodney W. Grimes 	struct uio *uio;
54260039670SBruce Evans 	int ioflag;
543df8bae1dSRodney W. Grimes {
544e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
545df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
546df8bae1dSRodney W. Grimes 	struct mbuf *m;
547e7bb21b3SJonathan Lemon 	int error;
548df8bae1dSRodney W. Grimes 	static struct sockaddr dst;
549df8bae1dSRodney W. Grimes 	int datlen;
550df8bae1dSRodney W. Grimes 
551df8bae1dSRodney W. Grimes 	if (d->bd_bif == 0)
552df8bae1dSRodney W. Grimes 		return (ENXIO);
553df8bae1dSRodney W. Grimes 
554df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
557df8bae1dSRodney W. Grimes 		return (0);
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
560df8bae1dSRodney W. Grimes 	if (error)
561df8bae1dSRodney W. Grimes 		return (error);
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes 	if (datlen > ifp->if_mtu)
564df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
565df8bae1dSRodney W. Grimes 
566114ae644SMike Smith 	if (d->bd_hdrcmplt)
567114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
568114ae644SMike Smith 
569e7bb21b3SJonathan Lemon 	mtx_lock(&Giant);
57082f4445dSRobert Watson #ifdef MAC
57182f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
57282f4445dSRobert Watson #endif
573df8bae1dSRodney W. Grimes 	error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
574e7bb21b3SJonathan Lemon 	mtx_unlock(&Giant);
575df8bae1dSRodney W. Grimes 	/*
576df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
577df8bae1dSRodney W. Grimes 	 */
578df8bae1dSRodney W. Grimes 	return (error);
579df8bae1dSRodney W. Grimes }
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes /*
582df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
583e7bb21b3SJonathan Lemon  * receive and drop counts.
584df8bae1dSRodney W. Grimes  */
585df8bae1dSRodney W. Grimes static void
586df8bae1dSRodney W. Grimes reset_d(d)
587df8bae1dSRodney W. Grimes 	struct bpf_d *d;
588df8bae1dSRodney W. Grimes {
589e7bb21b3SJonathan Lemon 
590e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
591df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
592df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
593df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
594df8bae1dSRodney W. Grimes 		d->bd_hbuf = 0;
595df8bae1dSRodney W. Grimes 	}
596df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
597df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
598df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
599df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
600df8bae1dSRodney W. Grimes }
601df8bae1dSRodney W. Grimes 
602df8bae1dSRodney W. Grimes /*
603df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
604df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
605df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
606df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
607df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
608df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
609df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
610df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
611df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
612df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
613df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
614df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
615df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
616df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
617114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
618114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6198ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6208ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
621df8bae1dSRodney W. Grimes  */
622df8bae1dSRodney W. Grimes /* ARGSUSED */
62387f6c662SJulian Elischer static	int
624b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
625df8bae1dSRodney W. Grimes 	dev_t dev;
626ecbb00a2SDoug Rabson 	u_long cmd;
627df8bae1dSRodney W. Grimes 	caddr_t addr;
62860039670SBruce Evans 	int flags;
629b40ce416SJulian Elischer 	struct thread *td;
630df8bae1dSRodney W. Grimes {
631e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
632e7bb21b3SJonathan Lemon 	int error = 0;
633df8bae1dSRodney W. Grimes 
63481bda851SJohn Polstra 	BPFD_LOCK(d);
63581bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
63681bda851SJohn Polstra 		callout_stop(&d->bd_callout);
63781bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
63881bda851SJohn Polstra 	BPFD_UNLOCK(d);
63981bda851SJohn Polstra 
640df8bae1dSRodney W. Grimes 	switch (cmd) {
641df8bae1dSRodney W. Grimes 
642df8bae1dSRodney W. Grimes 	default:
643df8bae1dSRodney W. Grimes 		error = EINVAL;
644df8bae1dSRodney W. Grimes 		break;
645df8bae1dSRodney W. Grimes 
646df8bae1dSRodney W. Grimes 	/*
647df8bae1dSRodney W. Grimes 	 * Check for read packet available.
648df8bae1dSRodney W. Grimes 	 */
649df8bae1dSRodney W. Grimes 	case FIONREAD:
650df8bae1dSRodney W. Grimes 		{
651df8bae1dSRodney W. Grimes 			int n;
652df8bae1dSRodney W. Grimes 
653e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
654df8bae1dSRodney W. Grimes 			n = d->bd_slen;
655df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
656df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
657e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
658df8bae1dSRodney W. Grimes 
659df8bae1dSRodney W. Grimes 			*(int *)addr = n;
660df8bae1dSRodney W. Grimes 			break;
661df8bae1dSRodney W. Grimes 		}
662df8bae1dSRodney W. Grimes 
663df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
664df8bae1dSRodney W. Grimes 		{
665df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
666df8bae1dSRodney W. Grimes 
667df8bae1dSRodney W. Grimes 			if (d->bd_bif == 0)
668df8bae1dSRodney W. Grimes 				error = EINVAL;
669df8bae1dSRodney W. Grimes 			else {
670df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
671df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
672df8bae1dSRodney W. Grimes 			}
673df8bae1dSRodney W. Grimes 			break;
674df8bae1dSRodney W. Grimes 		}
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 	/*
677df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
678df8bae1dSRodney W. Grimes 	 */
679df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
680df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
681df8bae1dSRodney W. Grimes 		break;
682df8bae1dSRodney W. Grimes 
683df8bae1dSRodney W. Grimes 	/*
684df8bae1dSRodney W. Grimes 	 * Set buffer length.
685df8bae1dSRodney W. Grimes 	 */
686df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
687df8bae1dSRodney W. Grimes 		if (d->bd_bif != 0)
688df8bae1dSRodney W. Grimes 			error = EINVAL;
689df8bae1dSRodney W. Grimes 		else {
6908994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
691df8bae1dSRodney W. Grimes 
692eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
693eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
694df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
695df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
696df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
697df8bae1dSRodney W. Grimes 		}
698df8bae1dSRodney W. Grimes 		break;
699df8bae1dSRodney W. Grimes 
700df8bae1dSRodney W. Grimes 	/*
701df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
702df8bae1dSRodney W. Grimes 	 */
703df8bae1dSRodney W. Grimes 	case BIOCSETF:
704df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
705df8bae1dSRodney W. Grimes 		break;
706df8bae1dSRodney W. Grimes 
707df8bae1dSRodney W. Grimes 	/*
708df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
709df8bae1dSRodney W. Grimes 	 */
710df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
711e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
712df8bae1dSRodney W. Grimes 		reset_d(d);
713e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
714df8bae1dSRodney W. Grimes 		break;
715df8bae1dSRodney W. Grimes 
716df8bae1dSRodney W. Grimes 	/*
717df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
718df8bae1dSRodney W. Grimes 	 */
719df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
720df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0) {
721df8bae1dSRodney W. Grimes 			/*
722df8bae1dSRodney W. Grimes 			 * No interface attached yet.
723df8bae1dSRodney W. Grimes 			 */
724df8bae1dSRodney W. Grimes 			error = EINVAL;
725df8bae1dSRodney W. Grimes 			break;
726df8bae1dSRodney W. Grimes 		}
727df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
728e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
729df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
730e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
731df8bae1dSRodney W. Grimes 			if (error == 0)
732df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
733df8bae1dSRodney W. Grimes 		}
734df8bae1dSRodney W. Grimes 		break;
735df8bae1dSRodney W. Grimes 
736df8bae1dSRodney W. Grimes 	/*
7378eab61f3SSam Leffler 	 * Get current data link type.
738df8bae1dSRodney W. Grimes 	 */
739df8bae1dSRodney W. Grimes 	case BIOCGDLT:
740df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
741df8bae1dSRodney W. Grimes 			error = EINVAL;
742df8bae1dSRodney W. Grimes 		else
743df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
744df8bae1dSRodney W. Grimes 		break;
745df8bae1dSRodney W. Grimes 
746df8bae1dSRodney W. Grimes 	/*
7478eab61f3SSam Leffler 	 * Get a list of supported data link types.
7488eab61f3SSam Leffler 	 */
7498eab61f3SSam Leffler 	case BIOCGDLTLIST:
7508eab61f3SSam Leffler 		if (d->bd_bif == 0)
7518eab61f3SSam Leffler 			error = EINVAL;
7528eab61f3SSam Leffler 		else
7538eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
7548eab61f3SSam Leffler 		break;
7558eab61f3SSam Leffler 
7568eab61f3SSam Leffler 	/*
7578eab61f3SSam Leffler 	 * Set data link type.
7588eab61f3SSam Leffler 	 */
7598eab61f3SSam Leffler 	case BIOCSDLT:
7608eab61f3SSam Leffler 		if (d->bd_bif == 0)
7618eab61f3SSam Leffler 			error = EINVAL;
7628eab61f3SSam Leffler 		else
7638eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
7648eab61f3SSam Leffler 		break;
7658eab61f3SSam Leffler 
7668eab61f3SSam Leffler 	/*
7671dd0feaaSArchie Cobbs 	 * Get interface name.
768df8bae1dSRodney W. Grimes 	 */
769df8bae1dSRodney W. Grimes 	case BIOCGETIF:
770df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
771df8bae1dSRodney W. Grimes 			error = EINVAL;
7721dd0feaaSArchie Cobbs 		else {
7731dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
7741dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
7751dd0feaaSArchie Cobbs 
7761dd0feaaSArchie Cobbs 			snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
7771dd0feaaSArchie Cobbs 			    "%s%d", ifp->if_name, ifp->if_unit);
7781dd0feaaSArchie Cobbs 		}
779df8bae1dSRodney W. Grimes 		break;
780df8bae1dSRodney W. Grimes 
781df8bae1dSRodney W. Grimes 	/*
782df8bae1dSRodney W. Grimes 	 * Set interface.
783df8bae1dSRodney W. Grimes 	 */
784df8bae1dSRodney W. Grimes 	case BIOCSETIF:
785df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
786df8bae1dSRodney W. Grimes 		break;
787df8bae1dSRodney W. Grimes 
788df8bae1dSRodney W. Grimes 	/*
789df8bae1dSRodney W. Grimes 	 * Set read timeout.
790df8bae1dSRodney W. Grimes 	 */
791df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
792df8bae1dSRodney W. Grimes 		{
793df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
794df8bae1dSRodney W. Grimes 
795bdc2cdc5SAlexander Langer 			/*
796bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
797bdc2cdc5SAlexander Langer 			 * a one-shot timer.
798bdc2cdc5SAlexander Langer 			 */
799bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
800bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
801df8bae1dSRodney W. Grimes 			break;
802df8bae1dSRodney W. Grimes 		}
803df8bae1dSRodney W. Grimes 
804df8bae1dSRodney W. Grimes 	/*
805df8bae1dSRodney W. Grimes 	 * Get read timeout.
806df8bae1dSRodney W. Grimes 	 */
807df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
808df8bae1dSRodney W. Grimes 		{
809df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
810df8bae1dSRodney W. Grimes 
811bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
812bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
813df8bae1dSRodney W. Grimes 			break;
814df8bae1dSRodney W. Grimes 		}
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 	/*
817df8bae1dSRodney W. Grimes 	 * Get packet stats.
818df8bae1dSRodney W. Grimes 	 */
819df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
820df8bae1dSRodney W. Grimes 		{
821df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
822df8bae1dSRodney W. Grimes 
823df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
824df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
825df8bae1dSRodney W. Grimes 			break;
826df8bae1dSRodney W. Grimes 		}
827df8bae1dSRodney W. Grimes 
828df8bae1dSRodney W. Grimes 	/*
829df8bae1dSRodney W. Grimes 	 * Set immediate mode.
830df8bae1dSRodney W. Grimes 	 */
831df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
832df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
833df8bae1dSRodney W. Grimes 		break;
834df8bae1dSRodney W. Grimes 
835df8bae1dSRodney W. Grimes 	case BIOCVERSION:
836df8bae1dSRodney W. Grimes 		{
837df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
838df8bae1dSRodney W. Grimes 
839df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
840df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
841df8bae1dSRodney W. Grimes 			break;
842df8bae1dSRodney W. Grimes 		}
84300a83887SPaul Traina 
844114ae644SMike Smith 	/*
845114ae644SMike Smith 	 * Get "header already complete" flag
846114ae644SMike Smith 	 */
847114ae644SMike Smith 	case BIOCGHDRCMPLT:
848114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
849114ae644SMike Smith 		break;
850114ae644SMike Smith 
851114ae644SMike Smith 	/*
852114ae644SMike Smith 	 * Set "header already complete" flag
853114ae644SMike Smith 	 */
854114ae644SMike Smith 	case BIOCSHDRCMPLT:
855114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
856114ae644SMike Smith 		break;
857114ae644SMike Smith 
8588ed3828cSRobert Watson 	/*
8598ed3828cSRobert Watson 	 * Get "see sent packets" flag
8608ed3828cSRobert Watson 	 */
8618ed3828cSRobert Watson 	case BIOCGSEESENT:
8628ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
8638ed3828cSRobert Watson 		break;
8648ed3828cSRobert Watson 
8658ed3828cSRobert Watson 	/*
8668ed3828cSRobert Watson 	 * Set "see sent packets" flag
8678ed3828cSRobert Watson 	 */
8688ed3828cSRobert Watson 	case BIOCSSEESENT:
8698ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
8708ed3828cSRobert Watson 		break;
8718ed3828cSRobert Watson 
87200a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
87300a83887SPaul Traina 		break;
87400a83887SPaul Traina 
87500a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
87600a83887SPaul Traina 		d->bd_async = *(int *)addr;
87700a83887SPaul Traina 		break;
87800a83887SPaul Traina 
879831d27a9SDon Lewis 	case FIOSETOWN:
880831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
88100a83887SPaul Traina 		break;
88200a83887SPaul Traina 
883831d27a9SDon Lewis 	case FIOGETOWN:
88491e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
885831d27a9SDon Lewis 		break;
886831d27a9SDon Lewis 
887831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
888831d27a9SDon Lewis 	case TIOCSPGRP:
889831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
890831d27a9SDon Lewis 		break;
891831d27a9SDon Lewis 
892831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
89300a83887SPaul Traina 	case TIOCGPGRP:
89491e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
89500a83887SPaul Traina 		break;
89600a83887SPaul Traina 
89700a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
89800a83887SPaul Traina 		{
89900a83887SPaul Traina 			u_int sig;
90000a83887SPaul Traina 
90100a83887SPaul Traina 			sig = *(u_int *)addr;
90200a83887SPaul Traina 
90300a83887SPaul Traina 			if (sig >= NSIG)
90400a83887SPaul Traina 				error = EINVAL;
90500a83887SPaul Traina 			else
90600a83887SPaul Traina 				d->bd_sig = sig;
90700a83887SPaul Traina 			break;
90800a83887SPaul Traina 		}
90900a83887SPaul Traina 	case BIOCGRSIG:
91000a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
91100a83887SPaul Traina 		break;
912df8bae1dSRodney W. Grimes 	}
913df8bae1dSRodney W. Grimes 	return (error);
914df8bae1dSRodney W. Grimes }
915df8bae1dSRodney W. Grimes 
916df8bae1dSRodney W. Grimes /*
917df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
918df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
919df8bae1dSRodney W. Grimes  */
920f708ef1bSPoul-Henning Kamp static int
921df8bae1dSRodney W. Grimes bpf_setf(d, fp)
922df8bae1dSRodney W. Grimes 	struct bpf_d *d;
923df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
924df8bae1dSRodney W. Grimes {
925df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
926df8bae1dSRodney W. Grimes 	u_int flen, size;
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes 	old = d->bd_filter;
929df8bae1dSRodney W. Grimes 	if (fp->bf_insns == 0) {
930df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
931df8bae1dSRodney W. Grimes 			return (EINVAL);
932e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
933df8bae1dSRodney W. Grimes 		d->bd_filter = 0;
934df8bae1dSRodney W. Grimes 		reset_d(d);
935e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
936df8bae1dSRodney W. Grimes 		if (old != 0)
937bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
938df8bae1dSRodney W. Grimes 		return (0);
939df8bae1dSRodney W. Grimes 	}
940df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
941df8bae1dSRodney W. Grimes 	if (flen > BPF_MAXINSNS)
942df8bae1dSRodney W. Grimes 		return (EINVAL);
943df8bae1dSRodney W. Grimes 
944df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
945a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
946df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
947df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
948e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
949df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
950df8bae1dSRodney W. Grimes 		reset_d(d);
951e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
952df8bae1dSRodney W. Grimes 		if (old != 0)
953bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
954df8bae1dSRodney W. Grimes 
955df8bae1dSRodney W. Grimes 		return (0);
956df8bae1dSRodney W. Grimes 	}
957bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
958df8bae1dSRodney W. Grimes 	return (EINVAL);
959df8bae1dSRodney W. Grimes }
960df8bae1dSRodney W. Grimes 
961df8bae1dSRodney W. Grimes /*
962df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
963df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
964df8bae1dSRodney W. Grimes  * Return an errno or 0.
965df8bae1dSRodney W. Grimes  */
966df8bae1dSRodney W. Grimes static int
967df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
968df8bae1dSRodney W. Grimes 	struct bpf_d *d;
969df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
970df8bae1dSRodney W. Grimes {
971df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
972e7bb21b3SJonathan Lemon 	int error;
9739b44ff22SGarrett Wollman 	struct ifnet *theywant;
974df8bae1dSRodney W. Grimes 
9759b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
9769b44ff22SGarrett Wollman 	if (theywant == 0)
9779b44ff22SGarrett Wollman 		return ENXIO;
9789b44ff22SGarrett Wollman 
979df8bae1dSRodney W. Grimes 	/*
980df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
981df8bae1dSRodney W. Grimes 	 */
982e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
983df8bae1dSRodney W. Grimes 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
984df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
985df8bae1dSRodney W. Grimes 
9869b44ff22SGarrett Wollman 		if (ifp == 0 || ifp != theywant)
987df8bae1dSRodney W. Grimes 			continue;
98824a229f4SSam Leffler 		/* skip additional entry */
98924a229f4SSam Leffler 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
99024a229f4SSam Leffler 			continue;
991e7bb21b3SJonathan Lemon 
992e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
993df8bae1dSRodney W. Grimes 		/*
994df8bae1dSRodney W. Grimes 		 * We found the requested interface.
995df8bae1dSRodney W. Grimes 		 * If it's not up, return an error.
996df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
997df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
998df8bae1dSRodney W. Grimes 		 * just flush the buffer.
999df8bae1dSRodney W. Grimes 		 */
1000df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_UP) == 0)
1001df8bae1dSRodney W. Grimes 			return (ENETDOWN);
1002df8bae1dSRodney W. Grimes 
1003df8bae1dSRodney W. Grimes 		if (d->bd_sbuf == 0) {
1004df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
1005df8bae1dSRodney W. Grimes 			if (error != 0)
1006df8bae1dSRodney W. Grimes 				return (error);
1007df8bae1dSRodney W. Grimes 		}
1008df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
1009df8bae1dSRodney W. Grimes 			if (d->bd_bif)
1010df8bae1dSRodney W. Grimes 				/*
1011df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
1012df8bae1dSRodney W. Grimes 				 */
1013df8bae1dSRodney W. Grimes 				bpf_detachd(d);
1014df8bae1dSRodney W. Grimes 
1015df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
1016df8bae1dSRodney W. Grimes 		}
1017e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1018df8bae1dSRodney W. Grimes 		reset_d(d);
1019e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1020df8bae1dSRodney W. Grimes 		return (0);
1021df8bae1dSRodney W. Grimes 	}
1022e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1023df8bae1dSRodney W. Grimes 	/* Not found. */
1024df8bae1dSRodney W. Grimes 	return (ENXIO);
1025df8bae1dSRodney W. Grimes }
1026df8bae1dSRodney W. Grimes 
1027df8bae1dSRodney W. Grimes /*
1028243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1029df8bae1dSRodney W. Grimes  *
1030df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1031df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1032df8bae1dSRodney W. Grimes  */
103337c84183SPoul-Henning Kamp static int
1034b40ce416SJulian Elischer bpfpoll(dev, events, td)
10358994a245SDag-Erling Smørgrav 	dev_t dev;
1036243ac7d8SPeter Wemm 	int events;
1037b40ce416SJulian Elischer 	struct thread *td;
1038df8bae1dSRodney W. Grimes {
1039e7bb21b3SJonathan Lemon 	struct bpf_d *d;
10400832fc64SGarance A Drosehn 	int revents;
1041df8bae1dSRodney W. Grimes 
1042bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1043de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1044de5d9935SRobert Watson 		return (ENXIO);
1045de5d9935SRobert Watson 
10460832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1047e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
104875c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
10490832fc64SGarance A Drosehn 		/*
10500832fc64SGarance A Drosehn 		 * An imitation of the FIONREAD ioctl code.
10510832fc64SGarance A Drosehn 		 * XXX not quite.  An exact imitation:
10520832fc64SGarance A Drosehn 		 *	if (d->b_slen != 0 ||
10530832fc64SGarance A Drosehn 		 *	    (d->bd_hbuf != NULL && d->bd_hlen != 0)
10540832fc64SGarance A Drosehn 		 */
105581bda851SJohn Polstra 		if (d->bd_hlen != 0 ||
105681bda851SJohn Polstra 		    ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
105781bda851SJohn Polstra 		    d->bd_slen != 0))
1058243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
105981bda851SJohn Polstra 		else {
1060ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
106181bda851SJohn Polstra 			/* Start the read timeout if necessary. */
106281bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
106381bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
106481bda851SJohn Polstra 				    bpf_timed_out, d);
106581bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
106681bda851SJohn Polstra 			}
106781bda851SJohn Polstra 		}
106875c13541SPoul-Henning Kamp 	}
1069e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1070243ac7d8SPeter Wemm 	return (revents);
1071df8bae1dSRodney W. Grimes }
1072df8bae1dSRodney W. Grimes 
1073df8bae1dSRodney W. Grimes /*
1074df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1075df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1076df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1077df8bae1dSRodney W. Grimes  * buffer.
1078df8bae1dSRodney W. Grimes  */
1079df8bae1dSRodney W. Grimes void
108024a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
108124a229f4SSam Leffler 	struct bpf_if *bp;
10828994a245SDag-Erling Smørgrav 	u_char *pkt;
10838994a245SDag-Erling Smørgrav 	u_int pktlen;
1084df8bae1dSRodney W. Grimes {
10858994a245SDag-Erling Smørgrav 	struct bpf_d *d;
10868994a245SDag-Erling Smørgrav 	u_int slen;
1087e7bb21b3SJonathan Lemon 
1088e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1089df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1090e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1091df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1092df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1093ec272d87SRobert Watson 		if (slen != 0) {
1094ec272d87SRobert Watson #ifdef MAC
109524a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1096ec272d87SRobert Watson #endif
1097df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1098ec272d87SRobert Watson 		}
1099e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1100df8bae1dSRodney W. Grimes 	}
1101e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1102df8bae1dSRodney W. Grimes }
1103df8bae1dSRodney W. Grimes 
1104df8bae1dSRodney W. Grimes /*
1105df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1106df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1107df8bae1dSRodney W. Grimes  */
1108df8bae1dSRodney W. Grimes static void
1109df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1110df8bae1dSRodney W. Grimes 	const void *src_arg;
1111df8bae1dSRodney W. Grimes 	void *dst_arg;
11128994a245SDag-Erling Smørgrav 	size_t len;
1113df8bae1dSRodney W. Grimes {
11148994a245SDag-Erling Smørgrav 	const struct mbuf *m;
11158994a245SDag-Erling Smørgrav 	u_int count;
1116df8bae1dSRodney W. Grimes 	u_char *dst;
1117df8bae1dSRodney W. Grimes 
1118df8bae1dSRodney W. Grimes 	m = src_arg;
1119df8bae1dSRodney W. Grimes 	dst = dst_arg;
1120df8bae1dSRodney W. Grimes 	while (len > 0) {
1121df8bae1dSRodney W. Grimes 		if (m == 0)
1122df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1123df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
11240453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1125df8bae1dSRodney W. Grimes 		m = m->m_next;
1126df8bae1dSRodney W. Grimes 		dst += count;
1127df8bae1dSRodney W. Grimes 		len -= count;
1128df8bae1dSRodney W. Grimes 	}
1129df8bae1dSRodney W. Grimes }
1130df8bae1dSRodney W. Grimes 
1131df8bae1dSRodney W. Grimes /*
1132df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1133df8bae1dSRodney W. Grimes  */
1134df8bae1dSRodney W. Grimes void
113524a229f4SSam Leffler bpf_mtap(bp, m)
113624a229f4SSam Leffler 	struct bpf_if *bp;
1137df8bae1dSRodney W. Grimes 	struct mbuf *m;
1138df8bae1dSRodney W. Grimes {
1139df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1140df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1141df8bae1dSRodney W. Grimes 
1142f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
11437b831242SPoul-Henning Kamp 	if (pktlen == m->m_len) {
114424a229f4SSam Leffler 		bpf_tap(bp, mtod(m, u_char *), pktlen);
11457b831242SPoul-Henning Kamp 		return;
11467b831242SPoul-Henning Kamp 	}
1147df8bae1dSRodney W. Grimes 
1148e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1149df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
11508ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
11518ed3828cSRobert Watson 			continue;
1152e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1153df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1154df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1155df8bae1dSRodney W. Grimes 		if (slen != 0)
11560c7fb534SRobert Watson #ifdef MAC
115724a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
11580c7fb534SRobert Watson #endif
11590c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
11600c7fb534SRobert Watson 				    bpf_mcopy);
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  * Move the packet data from interface memory (pkt) into the
1168df8bae1dSRodney W. Grimes  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
1169df8bae1dSRodney W. Grimes  * otherwise 0.  "copy" is the routine called to do the actual data
1170df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1171df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1172df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1173df8bae1dSRodney W. Grimes  */
1174df8bae1dSRodney W. Grimes static void
1175df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
11768994a245SDag-Erling Smørgrav 	struct bpf_d *d;
11778994a245SDag-Erling Smørgrav 	u_char *pkt;
11788994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
11798994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1180df8bae1dSRodney W. Grimes {
11818994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
11828994a245SDag-Erling Smørgrav 	int totlen, curlen;
11838994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
1184df8bae1dSRodney W. Grimes 	/*
1185df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1186df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1187df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1188df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1189df8bae1dSRodney W. Grimes 	 */
1190df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1191df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1192df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1193df8bae1dSRodney W. Grimes 
1194df8bae1dSRodney W. Grimes 	/*
1195df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1196df8bae1dSRodney W. Grimes 	 */
1197df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1198df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1199df8bae1dSRodney W. Grimes 		/*
1200df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1201df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1202df8bae1dSRodney W. Grimes 		 * pending reads.
1203df8bae1dSRodney W. Grimes 		 */
1204df8bae1dSRodney W. Grimes 		if (d->bd_fbuf == 0) {
1205df8bae1dSRodney W. Grimes 			/*
1206df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1207df8bae1dSRodney W. Grimes 			 * so drop the packet.
1208df8bae1dSRodney W. Grimes 			 */
1209df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1210df8bae1dSRodney W. Grimes 			return;
1211df8bae1dSRodney W. Grimes 		}
1212df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
1213df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1214df8bae1dSRodney W. Grimes 		curlen = 0;
1215df8bae1dSRodney W. Grimes 	}
121681bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1217df8bae1dSRodney W. Grimes 		/*
121881bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
121981bda851SJohn Polstra 		 * already expired during a select call.  A packet
122081bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1221df8bae1dSRodney W. Grimes 		 */
1222df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1223df8bae1dSRodney W. Grimes 
1224df8bae1dSRodney W. Grimes 	/*
1225df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1226df8bae1dSRodney W. Grimes 	 */
1227df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1228df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1229df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1230df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1231df8bae1dSRodney W. Grimes 	/*
1232df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1233df8bae1dSRodney W. Grimes 	 */
1234df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1235df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
1236df8bae1dSRodney W. Grimes }
1237df8bae1dSRodney W. Grimes 
1238df8bae1dSRodney W. Grimes /*
1239df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1240df8bae1dSRodney W. Grimes  */
1241df8bae1dSRodney W. Grimes static int
1242df8bae1dSRodney W. Grimes bpf_allocbufs(d)
12438994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1244df8bae1dSRodney W. Grimes {
1245a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1246df8bae1dSRodney W. Grimes 	if (d->bd_fbuf == 0)
1247df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1248df8bae1dSRodney W. Grimes 
1249a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1250df8bae1dSRodney W. Grimes 	if (d->bd_sbuf == 0) {
1251bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1252df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1253df8bae1dSRodney W. Grimes 	}
1254df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1255df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1256df8bae1dSRodney W. Grimes 	return (0);
1257df8bae1dSRodney W. Grimes }
1258df8bae1dSRodney W. Grimes 
1259df8bae1dSRodney W. Grimes /*
1260df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1261df8bae1dSRodney W. Grimes  * Called on close.
1262df8bae1dSRodney W. Grimes  */
1263df8bae1dSRodney W. Grimes static void
1264df8bae1dSRodney W. Grimes bpf_freed(d)
12658994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1266df8bae1dSRodney W. Grimes {
1267df8bae1dSRodney W. Grimes 	/*
1268df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1269df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1270df8bae1dSRodney W. Grimes 	 * free.
1271df8bae1dSRodney W. Grimes 	 */
1272df8bae1dSRodney W. Grimes 	if (d->bd_sbuf != 0) {
1273bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1274df8bae1dSRodney W. Grimes 		if (d->bd_hbuf != 0)
1275bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1276df8bae1dSRodney W. Grimes 		if (d->bd_fbuf != 0)
1277bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1278df8bae1dSRodney W. Grimes 	}
1279df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1280bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1281e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1282df8bae1dSRodney W. Grimes }
1283df8bae1dSRodney W. Grimes 
1284df8bae1dSRodney W. Grimes /*
128524a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
128624a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1287df8bae1dSRodney W. Grimes  */
1288df8bae1dSRodney W. Grimes void
12899b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1290df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1291df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1292df8bae1dSRodney W. Grimes {
129324a229f4SSam Leffler 
129424a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
129524a229f4SSam Leffler }
129624a229f4SSam Leffler 
129724a229f4SSam Leffler /*
129824a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
129924a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
130024a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
130124a229f4SSam Leffler  * headers are not yet supporrted).
130224a229f4SSam Leffler  */
130324a229f4SSam Leffler void
130424a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
130524a229f4SSam Leffler 	struct ifnet *ifp;
130624a229f4SSam Leffler 	u_int dlt, hdrlen;
130724a229f4SSam Leffler 	struct bpf_if **driverp;
130824a229f4SSam Leffler {
1309df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
13106a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1311df8bae1dSRodney W. Grimes 	if (bp == 0)
1312df8bae1dSRodney W. Grimes 		panic("bpfattach");
1313df8bae1dSRodney W. Grimes 
131424a229f4SSam Leffler 	bp->bif_dlist = 0;
131524a229f4SSam Leffler 	bp->bif_driverp = driverp;
1316df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1317df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
13186008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1319df8bae1dSRodney W. Grimes 
1320e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
1321df8bae1dSRodney W. Grimes 	bp->bif_next = bpf_iflist;
1322df8bae1dSRodney W. Grimes 	bpf_iflist = bp;
1323e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1324df8bae1dSRodney W. Grimes 
132524a229f4SSam Leffler 	*bp->bif_driverp = 0;
1326df8bae1dSRodney W. Grimes 
1327df8bae1dSRodney W. Grimes 	/*
1328df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1329df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1330df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1331df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1332df8bae1dSRodney W. Grimes 	 */
1333df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1334df8bae1dSRodney W. Grimes 
13352eeab939SGarrett Wollman 	if (bootverbose)
133624a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1337df8bae1dSRodney W. Grimes }
133853ac6efbSJulian Elischer 
1339de5d9935SRobert Watson /*
1340de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1341de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1342de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1343de5d9935SRobert Watson  * ENXIO.
1344de5d9935SRobert Watson  */
1345de5d9935SRobert Watson void
1346de5d9935SRobert Watson bpfdetach(ifp)
1347de5d9935SRobert Watson 	struct ifnet *ifp;
1348de5d9935SRobert Watson {
1349de5d9935SRobert Watson 	struct bpf_if	*bp, *bp_prev;
1350de5d9935SRobert Watson 	struct bpf_d	*d;
1351de5d9935SRobert Watson 
1352de5d9935SRobert Watson 	/* Locate BPF interface information */
1353de5d9935SRobert Watson 	bp_prev = NULL;
13548eab61f3SSam Leffler 
13558eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
1356de5d9935SRobert Watson 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1357de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1358de5d9935SRobert Watson 			break;
1359de5d9935SRobert Watson 		bp_prev = bp;
1360de5d9935SRobert Watson 	}
1361de5d9935SRobert Watson 
1362de5d9935SRobert Watson 	/* Interface wasn't attached */
1363de5d9935SRobert Watson 	if (bp->bif_ifp == NULL) {
1364e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1365de5d9935SRobert Watson 		printf("bpfdetach: %s%d was not attached\n", ifp->if_name,
1366de5d9935SRobert Watson 		    ifp->if_unit);
1367de5d9935SRobert Watson 		return;
1368de5d9935SRobert Watson 	}
1369de5d9935SRobert Watson 
1370de5d9935SRobert Watson 	if (bp_prev) {
1371de5d9935SRobert Watson 		bp_prev->bif_next = bp->bif_next;
1372de5d9935SRobert Watson 	} else {
1373de5d9935SRobert Watson 		bpf_iflist = bp->bif_next;
1374de5d9935SRobert Watson 	}
13758eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1376de5d9935SRobert Watson 
1377e7bb21b3SJonathan Lemon 	while ((d = bp->bif_dlist) != NULL) {
1378e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1379e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1380e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1381e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1382e7bb21b3SJonathan Lemon 	}
1383e7bb21b3SJonathan Lemon 
1384e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1385de5d9935SRobert Watson 	free(bp, M_BPF);
13868eab61f3SSam Leffler }
1387de5d9935SRobert Watson 
13888eab61f3SSam Leffler /*
13898eab61f3SSam Leffler  * Get a list of available data link type of the interface.
13908eab61f3SSam Leffler  */
13918eab61f3SSam Leffler static int
13928eab61f3SSam Leffler bpf_getdltlist(d, bfl)
13938eab61f3SSam Leffler 	struct bpf_d *d;
13948eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
13958eab61f3SSam Leffler {
13968eab61f3SSam Leffler 	int n, error;
13978eab61f3SSam Leffler 	struct ifnet *ifp;
13988eab61f3SSam Leffler 	struct bpf_if *bp;
13998eab61f3SSam Leffler 
14008eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
14018eab61f3SSam Leffler 	n = 0;
14028eab61f3SSam Leffler 	error = 0;
14038eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
14048eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
14058eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
14068eab61f3SSam Leffler 			continue;
14078eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
14088eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1409e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
14108eab61f3SSam Leffler 				return (ENOMEM);
14118eab61f3SSam Leffler 			}
14128eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
14138eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
14148eab61f3SSam Leffler 		}
14158eab61f3SSam Leffler 		n++;
14168eab61f3SSam Leffler 	}
14178eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
14188eab61f3SSam Leffler 	bfl->bfl_len = n;
14198eab61f3SSam Leffler 	return (error);
14208eab61f3SSam Leffler }
14218eab61f3SSam Leffler 
14228eab61f3SSam Leffler /*
14238eab61f3SSam Leffler  * Set the data link type of a BPF instance.
14248eab61f3SSam Leffler  */
14258eab61f3SSam Leffler static int
14268eab61f3SSam Leffler bpf_setdlt(d, dlt)
14278eab61f3SSam Leffler 	struct bpf_d *d;
14288eab61f3SSam Leffler 	u_int dlt;
14298eab61f3SSam Leffler {
14308eab61f3SSam Leffler 	int error, opromisc;
14318eab61f3SSam Leffler 	struct ifnet *ifp;
14328eab61f3SSam Leffler 	struct bpf_if *bp;
14338eab61f3SSam Leffler 
14348eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
14358eab61f3SSam Leffler 		return (0);
14368eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
14378eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
14388eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
14398eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
14408eab61f3SSam Leffler 			break;
14418eab61f3SSam Leffler 	}
14428eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
14438eab61f3SSam Leffler 	if (bp != NULL) {
14448eab61f3SSam Leffler 		BPFD_LOCK(d);
14458eab61f3SSam Leffler 		opromisc = d->bd_promisc;
14468eab61f3SSam Leffler 		bpf_detachd(d);
14478eab61f3SSam Leffler 		bpf_attachd(d, bp);
14488eab61f3SSam Leffler 		reset_d(d);
14498eab61f3SSam Leffler 		BPFD_UNLOCK(d);
14508eab61f3SSam Leffler 		if (opromisc) {
14518eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
14528eab61f3SSam Leffler 			if (error)
14538eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
14548eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
14558eab61f3SSam Leffler 					error);
14568eab61f3SSam Leffler 			else
14578eab61f3SSam Leffler 				d->bd_promisc = 1;
14588eab61f3SSam Leffler 		}
14598eab61f3SSam Leffler 	}
14608eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1461de5d9935SRobert Watson }
1462de5d9935SRobert Watson 
1463929ddbbbSAlfred Perlstein static void bpf_drvinit(void *unused);
1464bd3a5320SPoul-Henning Kamp 
1465929ddbbbSAlfred Perlstein static void bpf_clone(void *arg, char *name, int namelen, dev_t *dev);
14663f54a085SPoul-Henning Kamp 
14673f54a085SPoul-Henning Kamp static void
14683f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
14693f54a085SPoul-Henning Kamp 	void *arg;
14703f54a085SPoul-Henning Kamp 	char *name;
14713f54a085SPoul-Henning Kamp 	int namelen;
14723f54a085SPoul-Henning Kamp 	dev_t *dev;
14733f54a085SPoul-Henning Kamp {
14743f54a085SPoul-Henning Kamp 	int u;
14753f54a085SPoul-Henning Kamp 
14763f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
14773f54a085SPoul-Henning Kamp 		return;
1478db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
14793f54a085SPoul-Henning Kamp 		return;
1480b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1481b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1482b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
14833f54a085SPoul-Henning Kamp 	return;
14843f54a085SPoul-Henning Kamp }
14853f54a085SPoul-Henning Kamp 
1486514ede09SBruce Evans static void
1487514ede09SBruce Evans bpf_drvinit(unused)
1488514ede09SBruce Evans 	void *unused;
148953ac6efbSJulian Elischer {
149053ac6efbSJulian Elischer 
14916008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1492db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
14937198bf47SJulian Elischer }
149453ac6efbSJulian Elischer 
149553ac6efbSJulian Elischer SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
149653ac6efbSJulian Elischer 
14975bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1498f8dc4716SMike Smith /*
1499f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1500f8dc4716SMike Smith  *
1501f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1502f8dc4716SMike Smith  * to be loaded at runtime.
1503f8dc4716SMike Smith  */
1504f8dc4716SMike Smith 
1505f8dc4716SMike Smith void
1506e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1507e5562beeSSam Leffler 	struct bpf_if *bp;
15088994a245SDag-Erling Smørgrav 	u_char *pkt;
15098994a245SDag-Erling Smørgrav 	u_int pktlen;
1510f8dc4716SMike Smith {
1511f8dc4716SMike Smith }
1512f8dc4716SMike Smith 
1513f8dc4716SMike Smith void
1514e5562beeSSam Leffler bpf_mtap(bp, m)
1515e5562beeSSam Leffler 	struct bpf_if *bp;
1516f8dc4716SMike Smith 	struct mbuf *m;
1517f8dc4716SMike Smith {
1518f8dc4716SMike Smith }
1519f8dc4716SMike Smith 
1520f8dc4716SMike Smith void
1521f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1522f8dc4716SMike Smith 	struct ifnet *ifp;
1523f8dc4716SMike Smith 	u_int dlt, hdrlen;
1524f8dc4716SMike Smith {
1525f8dc4716SMike Smith }
1526f8dc4716SMike Smith 
1527da626c17SBill Paul void
1528da626c17SBill Paul bpfdetach(ifp)
1529da626c17SBill Paul 	struct ifnet *ifp;
1530da626c17SBill Paul {
1531da626c17SBill Paul }
1532da626c17SBill Paul 
1533f8dc4716SMike Smith u_int
1534f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
15358994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
15368994a245SDag-Erling Smørgrav 	u_char *p;
1537f8dc4716SMike Smith 	u_int wirelen;
15388994a245SDag-Erling Smørgrav 	u_int buflen;
1539f8dc4716SMike Smith {
1540f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1541f8dc4716SMike Smith }
1542f8dc4716SMike Smith 
15435bb5f2c9SPeter Wemm int
15445bb5f2c9SPeter Wemm bpf_validate(f, len)
15455bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
15465bb5f2c9SPeter Wemm 	int len;
15475bb5f2c9SPeter Wemm {
15485bb5f2c9SPeter Wemm 	return 0;		/* false */
15495bb5f2c9SPeter Wemm }
15505bb5f2c9SPeter Wemm 
15515bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1552