xref: /freebsd/sys/net/bpf.c (revision 521f364b804158f257fdd8ad7cbf368a0efe5a44)
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 = {
1244e2f199eSPoul-Henning Kamp 	/* open */	bpfopen,
1254e2f199eSPoul-Henning Kamp 	/* close */	bpfclose,
1264e2f199eSPoul-Henning Kamp 	/* read */	bpfread,
1274e2f199eSPoul-Henning Kamp 	/* write */	bpfwrite,
1284e2f199eSPoul-Henning Kamp 	/* ioctl */	bpfioctl,
1294e2f199eSPoul-Henning Kamp 	/* poll */	bpfpoll,
1304e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
1314e2f199eSPoul-Henning Kamp 	/* strategy */	nostrategy,
1324e2f199eSPoul-Henning Kamp 	/* name */	"bpf",
1334e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
1344e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1354e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1364e2f199eSPoul-Henning Kamp 	/* flags */	0,
1374e2f199eSPoul-Henning Kamp };
13887f6c662SJulian Elischer 
13987f6c662SJulian Elischer 
140df8bae1dSRodney W. Grimes static int
141df8bae1dSRodney W. Grimes bpf_movein(uio, linktype, mp, sockp, datlen)
1428994a245SDag-Erling Smørgrav 	struct uio *uio;
143df8bae1dSRodney W. Grimes 	int linktype, *datlen;
1448994a245SDag-Erling Smørgrav 	struct mbuf **mp;
1458994a245SDag-Erling Smørgrav 	struct sockaddr *sockp;
146df8bae1dSRodney W. Grimes {
147df8bae1dSRodney W. Grimes 	struct mbuf *m;
148df8bae1dSRodney W. Grimes 	int error;
149df8bae1dSRodney W. Grimes 	int len;
150df8bae1dSRodney W. Grimes 	int hlen;
151df8bae1dSRodney W. Grimes 
152df8bae1dSRodney W. Grimes 	/*
153df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
154df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
155df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
156df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
157df8bae1dSRodney W. Grimes 	 * is forwarded as is.
158df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
159df8bae1dSRodney W. Grimes 	 * for the link level header.
160df8bae1dSRodney W. Grimes 	 */
161df8bae1dSRodney W. Grimes 	switch (linktype) {
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes 	case DLT_SLIP:
164df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
165df8bae1dSRodney W. Grimes 		hlen = 0;
166df8bae1dSRodney W. Grimes 		break;
167df8bae1dSRodney W. Grimes 
168df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
169df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
170df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
171df8bae1dSRodney W. Grimes 		hlen = sizeof(struct ether_header);
172df8bae1dSRodney W. Grimes 		break;
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes 	case DLT_FDDI:
175d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
176d41f24e7SDavid Greenman 		hlen = 0;
177df8bae1dSRodney W. Grimes 		break;
178df8bae1dSRodney W. Grimes 
17922f05c43SAndrey A. Chernov 	case DLT_RAW:
180df8bae1dSRodney W. Grimes 	case DLT_NULL:
181df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
182df8bae1dSRodney W. Grimes 		hlen = 0;
183df8bae1dSRodney W. Grimes 		break;
184df8bae1dSRodney W. Grimes 
1854f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
1864f53e3ccSKenjiro Cho 		/*
1874f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
1884f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
1894f53e3ccSKenjiro Cho 		 * specified anyway.
1904f53e3ccSKenjiro Cho 		 */
1914f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
1924f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
1934f53e3ccSKenjiro Cho 		break;
1944f53e3ccSKenjiro Cho 
19530fa52a6SBrian Somers 	case DLT_PPP:
19630fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
19730fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
19830fa52a6SBrian Somers 		break;
19930fa52a6SBrian Somers 
200df8bae1dSRodney W. Grimes 	default:
201df8bae1dSRodney W. Grimes 		return (EIO);
202df8bae1dSRodney W. Grimes 	}
203df8bae1dSRodney W. Grimes 
204df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
205df8bae1dSRodney W. Grimes 	*datlen = len - hlen;
206df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
207df8bae1dSRodney W. Grimes 		return (EIO);
208df8bae1dSRodney W. Grimes 
209963e4c2aSGarrett Wollman 	if (len > MHLEN) {
210a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
21124a229f4SSam Leffler 	} else {
212a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
213df8bae1dSRodney W. Grimes 	}
21424a229f4SSam Leffler 	if (m == NULL)
21524a229f4SSam Leffler 		return (ENOBUFS);
216963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
217963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
218df8bae1dSRodney W. Grimes 	*mp = m;
21924a229f4SSam Leffler 
220df8bae1dSRodney W. Grimes 	/*
221df8bae1dSRodney W. Grimes 	 * Make room for link header.
222df8bae1dSRodney W. Grimes 	 */
223df8bae1dSRodney W. Grimes 	if (hlen != 0) {
2244f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
225df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
226df8bae1dSRodney W. Grimes #if BSD >= 199103
227df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
228df8bae1dSRodney W. Grimes #else
229df8bae1dSRodney W. Grimes 		m->m_off += hlen;
230df8bae1dSRodney W. Grimes #endif
231c9524588SDag-Erling Smørgrav 		error = uiomove(sockp->sa_data, hlen, uio);
232df8bae1dSRodney W. Grimes 		if (error)
233df8bae1dSRodney W. Grimes 			goto bad;
234df8bae1dSRodney W. Grimes 	}
235c9524588SDag-Erling Smørgrav 	error = uiomove(mtod(m, void *), len - hlen, uio);
236df8bae1dSRodney W. Grimes 	if (!error)
237df8bae1dSRodney W. Grimes 		return (0);
238df8bae1dSRodney W. Grimes bad:
239df8bae1dSRodney W. Grimes 	m_freem(m);
240df8bae1dSRodney W. Grimes 	return (error);
241df8bae1dSRodney W. Grimes }
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes /*
244df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
245df8bae1dSRodney W. Grimes  */
246df8bae1dSRodney W. Grimes static void
247df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
248df8bae1dSRodney W. Grimes 	struct bpf_d *d;
249df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
250df8bae1dSRodney W. Grimes {
251df8bae1dSRodney W. Grimes 	/*
252df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
253df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
254df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
255df8bae1dSRodney W. Grimes 	 */
256e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
257df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
258df8bae1dSRodney W. Grimes 	d->bd_next = bp->bif_dlist;
259df8bae1dSRodney W. Grimes 	bp->bif_dlist = d;
260df8bae1dSRodney W. Grimes 
26124a229f4SSam Leffler 	*bp->bif_driverp = bp;
262e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
263df8bae1dSRodney W. Grimes }
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes /*
266df8bae1dSRodney W. Grimes  * Detach a file from its interface.
267df8bae1dSRodney W. Grimes  */
268df8bae1dSRodney W. Grimes static void
269df8bae1dSRodney W. Grimes bpf_detachd(d)
270df8bae1dSRodney W. Grimes 	struct bpf_d *d;
271df8bae1dSRodney W. Grimes {
2726e891d64SPoul-Henning Kamp 	int error;
273df8bae1dSRodney W. Grimes 	struct bpf_d **p;
274df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
277df8bae1dSRodney W. Grimes 	/*
278df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
279df8bae1dSRodney W. Grimes 	 * If so, turn it off.
280df8bae1dSRodney W. Grimes 	 */
281df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
282df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
2836e891d64SPoul-Henning Kamp 		error = ifpromisc(bp->bif_ifp, 0);
2846e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
285df8bae1dSRodney W. Grimes 			/*
2866e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
287df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
288df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
289df8bae1dSRodney W. Grimes 			 * take it out.
290df8bae1dSRodney W. Grimes 			 */
2918eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
2928eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
2936e891d64SPoul-Henning Kamp 		}
294df8bae1dSRodney W. Grimes 	}
295df8bae1dSRodney W. Grimes 	/* Remove d from the interface's descriptor list. */
296e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
297df8bae1dSRodney W. Grimes 	p = &bp->bif_dlist;
298df8bae1dSRodney W. Grimes 	while (*p != d) {
299df8bae1dSRodney W. Grimes 		p = &(*p)->bd_next;
300df8bae1dSRodney W. Grimes 		if (*p == 0)
301df8bae1dSRodney W. Grimes 			panic("bpf_detachd: descriptor not in list");
302df8bae1dSRodney W. Grimes 	}
303df8bae1dSRodney W. Grimes 	*p = (*p)->bd_next;
304df8bae1dSRodney W. Grimes 	if (bp->bif_dlist == 0)
305df8bae1dSRodney W. Grimes 		/*
306df8bae1dSRodney W. Grimes 		 * Let the driver know that there are no more listeners.
307df8bae1dSRodney W. Grimes 		 */
30824a229f4SSam Leffler 		*d->bd_bif->bif_driverp = 0;
309e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
310df8bae1dSRodney W. Grimes 	d->bd_bif = 0;
311df8bae1dSRodney W. Grimes }
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes /*
314df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
315df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
316df8bae1dSRodney W. Grimes  */
317df8bae1dSRodney W. Grimes /* ARGSUSED */
31887f6c662SJulian Elischer static	int
319b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
320df8bae1dSRodney W. Grimes 	dev_t dev;
32160039670SBruce Evans 	int flags;
32260039670SBruce Evans 	int fmt;
323b40ce416SJulian Elischer 	struct thread *td;
324df8bae1dSRodney W. Grimes {
325e7bb21b3SJonathan Lemon 	struct bpf_d *d;
326df8bae1dSRodney W. Grimes 
327e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
328bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
329df8bae1dSRodney W. Grimes 	/*
330df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
331df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
332df8bae1dSRodney W. Grimes 	 */
333e7bb21b3SJonathan Lemon 	if (d) {
334e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
335df8bae1dSRodney W. Grimes 		return (EBUSY);
336e7bb21b3SJonathan Lemon 	}
337e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
338e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
339e7bb21b3SJonathan Lemon 
340d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
341b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
342b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
343a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
344bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
345df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
34600a83887SPaul Traina 	d->bd_sig = SIGIO;
3478ed3828cSRobert Watson 	d->bd_seesent = 1;
34882f4445dSRobert Watson #ifdef MAC
34982f4445dSRobert Watson 	mac_init_bpfdesc(d);
35082f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
35182f4445dSRobert Watson #endif
3526008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
35381bda851SJohn Polstra 	callout_init(&d->bd_callout, 1);
354df8bae1dSRodney W. Grimes 
355df8bae1dSRodney W. Grimes 	return (0);
356df8bae1dSRodney W. Grimes }
357df8bae1dSRodney W. Grimes 
358df8bae1dSRodney W. Grimes /*
359df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
360df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
361df8bae1dSRodney W. Grimes  */
362df8bae1dSRodney W. Grimes /* ARGSUSED */
36387f6c662SJulian Elischer static	int
364b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
365df8bae1dSRodney W. Grimes 	dev_t dev;
36660039670SBruce Evans 	int flags;
36760039670SBruce Evans 	int fmt;
368b40ce416SJulian Elischer 	struct thread *td;
369df8bae1dSRodney W. Grimes {
370e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
371df8bae1dSRodney W. Grimes 
37281bda851SJohn Polstra 	BPFD_LOCK(d);
37381bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
37481bda851SJohn Polstra 		callout_stop(&d->bd_callout);
37581bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
37681bda851SJohn Polstra 	BPFD_UNLOCK(d);
377e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
378e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
379df8bae1dSRodney W. Grimes 	if (d->bd_bif)
380df8bae1dSRodney W. Grimes 		bpf_detachd(d);
381e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
38282f4445dSRobert Watson #ifdef MAC
38382f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
38482f4445dSRobert Watson #endif /* MAC */
385df8bae1dSRodney W. Grimes 	bpf_freed(d);
386bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = 0;
387d722be54SLuigi Rizzo 	free(d, M_BPF);
388df8bae1dSRodney W. Grimes 
389df8bae1dSRodney W. Grimes 	return (0);
390df8bae1dSRodney W. Grimes }
391df8bae1dSRodney W. Grimes 
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes /*
394df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
395df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
396df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
397df8bae1dSRodney W. Grimes  */
398df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
399df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
400df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
401df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
402df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
403df8bae1dSRodney W. Grimes 	(d)->bd_fbuf = 0;
404df8bae1dSRodney W. Grimes /*
405df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
406df8bae1dSRodney W. Grimes  */
40787f6c662SJulian Elischer static	int
40860039670SBruce Evans bpfread(dev, uio, ioflag)
409df8bae1dSRodney W. Grimes 	dev_t dev;
4108994a245SDag-Erling Smørgrav 	struct uio *uio;
41160039670SBruce Evans 	int ioflag;
412df8bae1dSRodney W. Grimes {
413e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
41481bda851SJohn Polstra 	int timed_out;
415df8bae1dSRodney W. Grimes 	int error;
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	/*
418df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
419df8bae1dSRodney W. Grimes 	 * as kernel buffers.
420df8bae1dSRodney W. Grimes 	 */
421df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
422df8bae1dSRodney W. Grimes 		return (EINVAL);
423df8bae1dSRodney W. Grimes 
424e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
42581bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
42681bda851SJohn Polstra 		callout_stop(&d->bd_callout);
42781bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
42881bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
429df8bae1dSRodney W. Grimes 	/*
430df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
431df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
432df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
433df8bae1dSRodney W. Grimes 	 */
434df8bae1dSRodney W. Grimes 	while (d->bd_hbuf == 0) {
43581bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
436df8bae1dSRodney W. Grimes 			/*
437df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
438df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
439df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
440df8bae1dSRodney W. Grimes 			 */
441df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
442df8bae1dSRodney W. Grimes 			break;
443df8bae1dSRodney W. Grimes 		}
444de5d9935SRobert Watson 
445de5d9935SRobert Watson 		/*
446de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
447de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
448de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
449de5d9935SRobert Watson 		 * it before using it again.
450de5d9935SRobert Watson 		 */
451de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
452e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
453de5d9935SRobert Watson 			return (ENXIO);
454de5d9935SRobert Watson 		}
455de5d9935SRobert Watson 
456fba3cfdeSJohn Polstra 		if (ioflag & IO_NDELAY) {
457e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
458fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
459fba3cfdeSJohn Polstra 		}
460521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
461e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
462df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
463e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
464df8bae1dSRodney W. Grimes 			return (error);
465df8bae1dSRodney W. Grimes 		}
466df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
467df8bae1dSRodney W. Grimes 			/*
468df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
469df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
470df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
471df8bae1dSRodney W. Grimes 			 */
472df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
473df8bae1dSRodney W. Grimes 				/*
474df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
475df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
476df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
477df8bae1dSRodney W. Grimes 				 */
478df8bae1dSRodney W. Grimes 				break;
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
481e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
482df8bae1dSRodney W. Grimes 				return (0);
483df8bae1dSRodney W. Grimes 			}
484df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
485df8bae1dSRodney W. Grimes 			break;
486df8bae1dSRodney W. Grimes 		}
487df8bae1dSRodney W. Grimes 	}
488df8bae1dSRodney W. Grimes 	/*
489df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
490df8bae1dSRodney W. Grimes 	 */
491e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
492df8bae1dSRodney W. Grimes 
493df8bae1dSRodney W. Grimes 	/*
494df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
495df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
496df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
497df8bae1dSRodney W. Grimes 	 */
498e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
499df8bae1dSRodney W. Grimes 
500e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
501df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
502df8bae1dSRodney W. Grimes 	d->bd_hbuf = 0;
503df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
504e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 	return (error);
507df8bae1dSRodney W. Grimes }
508df8bae1dSRodney W. Grimes 
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes /*
511df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
512df8bae1dSRodney W. Grimes  */
513e7bb21b3SJonathan Lemon static __inline void
514df8bae1dSRodney W. Grimes bpf_wakeup(d)
5158994a245SDag-Erling Smørgrav 	struct bpf_d *d;
516df8bae1dSRodney W. Grimes {
51781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
51881bda851SJohn Polstra 		callout_stop(&d->bd_callout);
51981bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
52081bda851SJohn Polstra 	}
521521f364bSDag-Erling Smørgrav 	wakeup(d);
522831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
523f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
52400a83887SPaul Traina 
525df8bae1dSRodney W. Grimes 	selwakeup(&d->bd_sel);
526df8bae1dSRodney W. Grimes }
527df8bae1dSRodney W. Grimes 
52881bda851SJohn Polstra static void
52981bda851SJohn Polstra bpf_timed_out(arg)
53081bda851SJohn Polstra 	void *arg;
53181bda851SJohn Polstra {
53281bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
53381bda851SJohn Polstra 
53481bda851SJohn Polstra 	BPFD_LOCK(d);
53581bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
53681bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
53781bda851SJohn Polstra 		if (d->bd_slen != 0)
53881bda851SJohn Polstra 			bpf_wakeup(d);
53981bda851SJohn Polstra 	}
54081bda851SJohn Polstra 	BPFD_UNLOCK(d);
54181bda851SJohn Polstra }
54281bda851SJohn Polstra 
54387f6c662SJulian Elischer static	int
54460039670SBruce Evans bpfwrite(dev, uio, ioflag)
545df8bae1dSRodney W. Grimes 	dev_t dev;
546df8bae1dSRodney W. Grimes 	struct uio *uio;
54760039670SBruce Evans 	int ioflag;
548df8bae1dSRodney W. Grimes {
549e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
550df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
551df8bae1dSRodney W. Grimes 	struct mbuf *m;
552e7bb21b3SJonathan Lemon 	int error;
553df8bae1dSRodney W. Grimes 	static struct sockaddr dst;
554df8bae1dSRodney W. Grimes 	int datlen;
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 	if (d->bd_bif == 0)
557df8bae1dSRodney W. Grimes 		return (ENXIO);
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
562df8bae1dSRodney W. Grimes 		return (0);
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
565df8bae1dSRodney W. Grimes 	if (error)
566df8bae1dSRodney W. Grimes 		return (error);
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes 	if (datlen > ifp->if_mtu)
569df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
570df8bae1dSRodney W. Grimes 
571114ae644SMike Smith 	if (d->bd_hdrcmplt)
572114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
573114ae644SMike Smith 
574e7bb21b3SJonathan Lemon 	mtx_lock(&Giant);
57582f4445dSRobert Watson #ifdef MAC
57682f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
57782f4445dSRobert Watson #endif
578df8bae1dSRodney W. Grimes 	error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
579e7bb21b3SJonathan Lemon 	mtx_unlock(&Giant);
580df8bae1dSRodney W. Grimes 	/*
581df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
582df8bae1dSRodney W. Grimes 	 */
583df8bae1dSRodney W. Grimes 	return (error);
584df8bae1dSRodney W. Grimes }
585df8bae1dSRodney W. Grimes 
586df8bae1dSRodney W. Grimes /*
587df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
588e7bb21b3SJonathan Lemon  * receive and drop counts.
589df8bae1dSRodney W. Grimes  */
590df8bae1dSRodney W. Grimes static void
591df8bae1dSRodney W. Grimes reset_d(d)
592df8bae1dSRodney W. Grimes 	struct bpf_d *d;
593df8bae1dSRodney W. Grimes {
594e7bb21b3SJonathan Lemon 
595e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
596df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
597df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
598df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
599df8bae1dSRodney W. Grimes 		d->bd_hbuf = 0;
600df8bae1dSRodney W. Grimes 	}
601df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
602df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
603df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
604df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
605df8bae1dSRodney W. Grimes }
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes /*
608df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
609df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
610df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
611df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
612df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
613df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
614df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
615df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
616df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
617df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
618df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
619df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
620df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
621df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
622114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
623114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6248ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6258ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
626df8bae1dSRodney W. Grimes  */
627df8bae1dSRodney W. Grimes /* ARGSUSED */
62887f6c662SJulian Elischer static	int
629b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
630df8bae1dSRodney W. Grimes 	dev_t dev;
631ecbb00a2SDoug Rabson 	u_long cmd;
632df8bae1dSRodney W. Grimes 	caddr_t addr;
63360039670SBruce Evans 	int flags;
634b40ce416SJulian Elischer 	struct thread *td;
635df8bae1dSRodney W. Grimes {
636e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
637e7bb21b3SJonathan Lemon 	int error = 0;
638df8bae1dSRodney W. Grimes 
63981bda851SJohn Polstra 	BPFD_LOCK(d);
64081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
64181bda851SJohn Polstra 		callout_stop(&d->bd_callout);
64281bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
64381bda851SJohn Polstra 	BPFD_UNLOCK(d);
64481bda851SJohn Polstra 
645df8bae1dSRodney W. Grimes 	switch (cmd) {
646df8bae1dSRodney W. Grimes 
647df8bae1dSRodney W. Grimes 	default:
648df8bae1dSRodney W. Grimes 		error = EINVAL;
649df8bae1dSRodney W. Grimes 		break;
650df8bae1dSRodney W. Grimes 
651df8bae1dSRodney W. Grimes 	/*
652df8bae1dSRodney W. Grimes 	 * Check for read packet available.
653df8bae1dSRodney W. Grimes 	 */
654df8bae1dSRodney W. Grimes 	case FIONREAD:
655df8bae1dSRodney W. Grimes 		{
656df8bae1dSRodney W. Grimes 			int n;
657df8bae1dSRodney W. Grimes 
658e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
659df8bae1dSRodney W. Grimes 			n = d->bd_slen;
660df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
661df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
662e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes 			*(int *)addr = n;
665df8bae1dSRodney W. Grimes 			break;
666df8bae1dSRodney W. Grimes 		}
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
669df8bae1dSRodney W. Grimes 		{
670df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes 			if (d->bd_bif == 0)
673df8bae1dSRodney W. Grimes 				error = EINVAL;
674df8bae1dSRodney W. Grimes 			else {
675df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
676df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
677df8bae1dSRodney W. Grimes 			}
678df8bae1dSRodney W. Grimes 			break;
679df8bae1dSRodney W. Grimes 		}
680df8bae1dSRodney W. Grimes 
681df8bae1dSRodney W. Grimes 	/*
682df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
683df8bae1dSRodney W. Grimes 	 */
684df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
685df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
686df8bae1dSRodney W. Grimes 		break;
687df8bae1dSRodney W. Grimes 
688df8bae1dSRodney W. Grimes 	/*
689df8bae1dSRodney W. Grimes 	 * Set buffer length.
690df8bae1dSRodney W. Grimes 	 */
691df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
692df8bae1dSRodney W. Grimes 		if (d->bd_bif != 0)
693df8bae1dSRodney W. Grimes 			error = EINVAL;
694df8bae1dSRodney W. Grimes 		else {
6958994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
696df8bae1dSRodney W. Grimes 
697eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
698eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
699df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
700df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
701df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
702df8bae1dSRodney W. Grimes 		}
703df8bae1dSRodney W. Grimes 		break;
704df8bae1dSRodney W. Grimes 
705df8bae1dSRodney W. Grimes 	/*
706df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
707df8bae1dSRodney W. Grimes 	 */
708df8bae1dSRodney W. Grimes 	case BIOCSETF:
709df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
710df8bae1dSRodney W. Grimes 		break;
711df8bae1dSRodney W. Grimes 
712df8bae1dSRodney W. Grimes 	/*
713df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
714df8bae1dSRodney W. Grimes 	 */
715df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
716e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
717df8bae1dSRodney W. Grimes 		reset_d(d);
718e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
719df8bae1dSRodney W. Grimes 		break;
720df8bae1dSRodney W. Grimes 
721df8bae1dSRodney W. Grimes 	/*
722df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
723df8bae1dSRodney W. Grimes 	 */
724df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
725df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0) {
726df8bae1dSRodney W. Grimes 			/*
727df8bae1dSRodney W. Grimes 			 * No interface attached yet.
728df8bae1dSRodney W. Grimes 			 */
729df8bae1dSRodney W. Grimes 			error = EINVAL;
730df8bae1dSRodney W. Grimes 			break;
731df8bae1dSRodney W. Grimes 		}
732df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
733e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
734df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
735e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
736df8bae1dSRodney W. Grimes 			if (error == 0)
737df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
738df8bae1dSRodney W. Grimes 		}
739df8bae1dSRodney W. Grimes 		break;
740df8bae1dSRodney W. Grimes 
741df8bae1dSRodney W. Grimes 	/*
7428eab61f3SSam Leffler 	 * Get current data link type.
743df8bae1dSRodney W. Grimes 	 */
744df8bae1dSRodney W. Grimes 	case BIOCGDLT:
745df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
746df8bae1dSRodney W. Grimes 			error = EINVAL;
747df8bae1dSRodney W. Grimes 		else
748df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
749df8bae1dSRodney W. Grimes 		break;
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes 	/*
7528eab61f3SSam Leffler 	 * Get a list of supported data link types.
7538eab61f3SSam Leffler 	 */
7548eab61f3SSam Leffler 	case BIOCGDLTLIST:
7558eab61f3SSam Leffler 		if (d->bd_bif == 0)
7568eab61f3SSam Leffler 			error = EINVAL;
7578eab61f3SSam Leffler 		else
7588eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
7598eab61f3SSam Leffler 		break;
7608eab61f3SSam Leffler 
7618eab61f3SSam Leffler 	/*
7628eab61f3SSam Leffler 	 * Set data link type.
7638eab61f3SSam Leffler 	 */
7648eab61f3SSam Leffler 	case BIOCSDLT:
7658eab61f3SSam Leffler 		if (d->bd_bif == 0)
7668eab61f3SSam Leffler 			error = EINVAL;
7678eab61f3SSam Leffler 		else
7688eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
7698eab61f3SSam Leffler 		break;
7708eab61f3SSam Leffler 
7718eab61f3SSam Leffler 	/*
7721dd0feaaSArchie Cobbs 	 * Get interface name.
773df8bae1dSRodney W. Grimes 	 */
774df8bae1dSRodney W. Grimes 	case BIOCGETIF:
775df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
776df8bae1dSRodney W. Grimes 			error = EINVAL;
7771dd0feaaSArchie Cobbs 		else {
7781dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
7791dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
7801dd0feaaSArchie Cobbs 
7811dd0feaaSArchie Cobbs 			snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
7821dd0feaaSArchie Cobbs 			    "%s%d", ifp->if_name, ifp->if_unit);
7831dd0feaaSArchie Cobbs 		}
784df8bae1dSRodney W. Grimes 		break;
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes 	/*
787df8bae1dSRodney W. Grimes 	 * Set interface.
788df8bae1dSRodney W. Grimes 	 */
789df8bae1dSRodney W. Grimes 	case BIOCSETIF:
790df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
791df8bae1dSRodney W. Grimes 		break;
792df8bae1dSRodney W. Grimes 
793df8bae1dSRodney W. Grimes 	/*
794df8bae1dSRodney W. Grimes 	 * Set read timeout.
795df8bae1dSRodney W. Grimes 	 */
796df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
797df8bae1dSRodney W. Grimes 		{
798df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
799df8bae1dSRodney W. Grimes 
800bdc2cdc5SAlexander Langer 			/*
801bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
802bdc2cdc5SAlexander Langer 			 * a one-shot timer.
803bdc2cdc5SAlexander Langer 			 */
804bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
805bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
806df8bae1dSRodney W. Grimes 			break;
807df8bae1dSRodney W. Grimes 		}
808df8bae1dSRodney W. Grimes 
809df8bae1dSRodney W. Grimes 	/*
810df8bae1dSRodney W. Grimes 	 * Get read timeout.
811df8bae1dSRodney W. Grimes 	 */
812df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
813df8bae1dSRodney W. Grimes 		{
814df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
815df8bae1dSRodney W. Grimes 
816bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
817bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
818df8bae1dSRodney W. Grimes 			break;
819df8bae1dSRodney W. Grimes 		}
820df8bae1dSRodney W. Grimes 
821df8bae1dSRodney W. Grimes 	/*
822df8bae1dSRodney W. Grimes 	 * Get packet stats.
823df8bae1dSRodney W. Grimes 	 */
824df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
825df8bae1dSRodney W. Grimes 		{
826df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
827df8bae1dSRodney W. Grimes 
828df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
829df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
830df8bae1dSRodney W. Grimes 			break;
831df8bae1dSRodney W. Grimes 		}
832df8bae1dSRodney W. Grimes 
833df8bae1dSRodney W. Grimes 	/*
834df8bae1dSRodney W. Grimes 	 * Set immediate mode.
835df8bae1dSRodney W. Grimes 	 */
836df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
837df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
838df8bae1dSRodney W. Grimes 		break;
839df8bae1dSRodney W. Grimes 
840df8bae1dSRodney W. Grimes 	case BIOCVERSION:
841df8bae1dSRodney W. Grimes 		{
842df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
843df8bae1dSRodney W. Grimes 
844df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
845df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
846df8bae1dSRodney W. Grimes 			break;
847df8bae1dSRodney W. Grimes 		}
84800a83887SPaul Traina 
849114ae644SMike Smith 	/*
850114ae644SMike Smith 	 * Get "header already complete" flag
851114ae644SMike Smith 	 */
852114ae644SMike Smith 	case BIOCGHDRCMPLT:
853114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
854114ae644SMike Smith 		break;
855114ae644SMike Smith 
856114ae644SMike Smith 	/*
857114ae644SMike Smith 	 * Set "header already complete" flag
858114ae644SMike Smith 	 */
859114ae644SMike Smith 	case BIOCSHDRCMPLT:
860114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
861114ae644SMike Smith 		break;
862114ae644SMike Smith 
8638ed3828cSRobert Watson 	/*
8648ed3828cSRobert Watson 	 * Get "see sent packets" flag
8658ed3828cSRobert Watson 	 */
8668ed3828cSRobert Watson 	case BIOCGSEESENT:
8678ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
8688ed3828cSRobert Watson 		break;
8698ed3828cSRobert Watson 
8708ed3828cSRobert Watson 	/*
8718ed3828cSRobert Watson 	 * Set "see sent packets" flag
8728ed3828cSRobert Watson 	 */
8738ed3828cSRobert Watson 	case BIOCSSEESENT:
8748ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
8758ed3828cSRobert Watson 		break;
8768ed3828cSRobert Watson 
87700a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
87800a83887SPaul Traina 		break;
87900a83887SPaul Traina 
88000a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
88100a83887SPaul Traina 		d->bd_async = *(int *)addr;
88200a83887SPaul Traina 		break;
88300a83887SPaul Traina 
884831d27a9SDon Lewis 	case FIOSETOWN:
885831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
88600a83887SPaul Traina 		break;
88700a83887SPaul Traina 
888831d27a9SDon Lewis 	case FIOGETOWN:
88991e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
890831d27a9SDon Lewis 		break;
891831d27a9SDon Lewis 
892831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
893831d27a9SDon Lewis 	case TIOCSPGRP:
894831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
895831d27a9SDon Lewis 		break;
896831d27a9SDon Lewis 
897831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
89800a83887SPaul Traina 	case TIOCGPGRP:
89991e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
90000a83887SPaul Traina 		break;
90100a83887SPaul Traina 
90200a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
90300a83887SPaul Traina 		{
90400a83887SPaul Traina 			u_int sig;
90500a83887SPaul Traina 
90600a83887SPaul Traina 			sig = *(u_int *)addr;
90700a83887SPaul Traina 
90800a83887SPaul Traina 			if (sig >= NSIG)
90900a83887SPaul Traina 				error = EINVAL;
91000a83887SPaul Traina 			else
91100a83887SPaul Traina 				d->bd_sig = sig;
91200a83887SPaul Traina 			break;
91300a83887SPaul Traina 		}
91400a83887SPaul Traina 	case BIOCGRSIG:
91500a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
91600a83887SPaul Traina 		break;
917df8bae1dSRodney W. Grimes 	}
918df8bae1dSRodney W. Grimes 	return (error);
919df8bae1dSRodney W. Grimes }
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes /*
922df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
923df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
924df8bae1dSRodney W. Grimes  */
925f708ef1bSPoul-Henning Kamp static int
926df8bae1dSRodney W. Grimes bpf_setf(d, fp)
927df8bae1dSRodney W. Grimes 	struct bpf_d *d;
928df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
929df8bae1dSRodney W. Grimes {
930df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
931df8bae1dSRodney W. Grimes 	u_int flen, size;
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 	old = d->bd_filter;
934df8bae1dSRodney W. Grimes 	if (fp->bf_insns == 0) {
935df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
936df8bae1dSRodney W. Grimes 			return (EINVAL);
937e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
938df8bae1dSRodney W. Grimes 		d->bd_filter = 0;
939df8bae1dSRodney W. Grimes 		reset_d(d);
940e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
941df8bae1dSRodney W. Grimes 		if (old != 0)
942bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
943df8bae1dSRodney W. Grimes 		return (0);
944df8bae1dSRodney W. Grimes 	}
945df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
946df8bae1dSRodney W. Grimes 	if (flen > BPF_MAXINSNS)
947df8bae1dSRodney W. Grimes 		return (EINVAL);
948df8bae1dSRodney W. Grimes 
949df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
950a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
951df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
952df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
953e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
954df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
955df8bae1dSRodney W. Grimes 		reset_d(d);
956e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
957df8bae1dSRodney W. Grimes 		if (old != 0)
958bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
959df8bae1dSRodney W. Grimes 
960df8bae1dSRodney W. Grimes 		return (0);
961df8bae1dSRodney W. Grimes 	}
962bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
963df8bae1dSRodney W. Grimes 	return (EINVAL);
964df8bae1dSRodney W. Grimes }
965df8bae1dSRodney W. Grimes 
966df8bae1dSRodney W. Grimes /*
967df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
968df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
969df8bae1dSRodney W. Grimes  * Return an errno or 0.
970df8bae1dSRodney W. Grimes  */
971df8bae1dSRodney W. Grimes static int
972df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
973df8bae1dSRodney W. Grimes 	struct bpf_d *d;
974df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
975df8bae1dSRodney W. Grimes {
976df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
977e7bb21b3SJonathan Lemon 	int error;
9789b44ff22SGarrett Wollman 	struct ifnet *theywant;
979df8bae1dSRodney W. Grimes 
9809b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
9819b44ff22SGarrett Wollman 	if (theywant == 0)
9829b44ff22SGarrett Wollman 		return ENXIO;
9839b44ff22SGarrett Wollman 
984df8bae1dSRodney W. Grimes 	/*
985df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
986df8bae1dSRodney W. Grimes 	 */
987e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
988df8bae1dSRodney W. Grimes 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
989df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
990df8bae1dSRodney W. Grimes 
9919b44ff22SGarrett Wollman 		if (ifp == 0 || ifp != theywant)
992df8bae1dSRodney W. Grimes 			continue;
99324a229f4SSam Leffler 		/* skip additional entry */
99424a229f4SSam Leffler 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
99524a229f4SSam Leffler 			continue;
996e7bb21b3SJonathan Lemon 
997e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
998df8bae1dSRodney W. Grimes 		/*
999df8bae1dSRodney W. Grimes 		 * We found the requested interface.
1000df8bae1dSRodney W. Grimes 		 * If it's not up, return an error.
1001df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
1002df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
1003df8bae1dSRodney W. Grimes 		 * just flush the buffer.
1004df8bae1dSRodney W. Grimes 		 */
1005df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_UP) == 0)
1006df8bae1dSRodney W. Grimes 			return (ENETDOWN);
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes 		if (d->bd_sbuf == 0) {
1009df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
1010df8bae1dSRodney W. Grimes 			if (error != 0)
1011df8bae1dSRodney W. Grimes 				return (error);
1012df8bae1dSRodney W. Grimes 		}
1013df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
1014df8bae1dSRodney W. Grimes 			if (d->bd_bif)
1015df8bae1dSRodney W. Grimes 				/*
1016df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
1017df8bae1dSRodney W. Grimes 				 */
1018df8bae1dSRodney W. Grimes 				bpf_detachd(d);
1019df8bae1dSRodney W. Grimes 
1020df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
1021df8bae1dSRodney W. Grimes 		}
1022e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1023df8bae1dSRodney W. Grimes 		reset_d(d);
1024e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1025df8bae1dSRodney W. Grimes 		return (0);
1026df8bae1dSRodney W. Grimes 	}
1027e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1028df8bae1dSRodney W. Grimes 	/* Not found. */
1029df8bae1dSRodney W. Grimes 	return (ENXIO);
1030df8bae1dSRodney W. Grimes }
1031df8bae1dSRodney W. Grimes 
1032df8bae1dSRodney W. Grimes /*
1033243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1034df8bae1dSRodney W. Grimes  *
1035df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1036df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1037df8bae1dSRodney W. Grimes  */
103837c84183SPoul-Henning Kamp static int
1039b40ce416SJulian Elischer bpfpoll(dev, events, td)
10408994a245SDag-Erling Smørgrav 	dev_t dev;
1041243ac7d8SPeter Wemm 	int events;
1042b40ce416SJulian Elischer 	struct thread *td;
1043df8bae1dSRodney W. Grimes {
1044e7bb21b3SJonathan Lemon 	struct bpf_d *d;
10450832fc64SGarance A Drosehn 	int revents;
1046df8bae1dSRodney W. Grimes 
1047bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1048de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1049de5d9935SRobert Watson 		return (ENXIO);
1050de5d9935SRobert Watson 
10510832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1052e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
105375c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
10540832fc64SGarance A Drosehn 		/*
10550832fc64SGarance A Drosehn 		 * An imitation of the FIONREAD ioctl code.
10560832fc64SGarance A Drosehn 		 * XXX not quite.  An exact imitation:
10570832fc64SGarance A Drosehn 		 *	if (d->b_slen != 0 ||
10580832fc64SGarance A Drosehn 		 *	    (d->bd_hbuf != NULL && d->bd_hlen != 0)
10590832fc64SGarance A Drosehn 		 */
106081bda851SJohn Polstra 		if (d->bd_hlen != 0 ||
106181bda851SJohn Polstra 		    ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
106281bda851SJohn Polstra 		    d->bd_slen != 0))
1063243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
106481bda851SJohn Polstra 		else {
1065ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
106681bda851SJohn Polstra 			/* Start the read timeout if necessary. */
106781bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
106881bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
106981bda851SJohn Polstra 				    bpf_timed_out, d);
107081bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
107181bda851SJohn Polstra 			}
107281bda851SJohn Polstra 		}
107375c13541SPoul-Henning Kamp 	}
1074e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1075243ac7d8SPeter Wemm 	return (revents);
1076df8bae1dSRodney W. Grimes }
1077df8bae1dSRodney W. Grimes 
1078df8bae1dSRodney W. Grimes /*
1079df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1080df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1081df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1082df8bae1dSRodney W. Grimes  * buffer.
1083df8bae1dSRodney W. Grimes  */
1084df8bae1dSRodney W. Grimes void
108524a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
108624a229f4SSam Leffler 	struct bpf_if *bp;
10878994a245SDag-Erling Smørgrav 	u_char *pkt;
10888994a245SDag-Erling Smørgrav 	u_int pktlen;
1089df8bae1dSRodney W. Grimes {
10908994a245SDag-Erling Smørgrav 	struct bpf_d *d;
10918994a245SDag-Erling Smørgrav 	u_int slen;
1092e7bb21b3SJonathan Lemon 
1093e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1094df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1095e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1096df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1097df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1098ec272d87SRobert Watson 		if (slen != 0) {
1099ec272d87SRobert Watson #ifdef MAC
110024a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1101ec272d87SRobert Watson #endif
1102df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1103ec272d87SRobert Watson 		}
1104e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1105df8bae1dSRodney W. Grimes 	}
1106e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1107df8bae1dSRodney W. Grimes }
1108df8bae1dSRodney W. Grimes 
1109df8bae1dSRodney W. Grimes /*
1110df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1111df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1112df8bae1dSRodney W. Grimes  */
1113df8bae1dSRodney W. Grimes static void
1114df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1115df8bae1dSRodney W. Grimes 	const void *src_arg;
1116df8bae1dSRodney W. Grimes 	void *dst_arg;
11178994a245SDag-Erling Smørgrav 	size_t len;
1118df8bae1dSRodney W. Grimes {
11198994a245SDag-Erling Smørgrav 	const struct mbuf *m;
11208994a245SDag-Erling Smørgrav 	u_int count;
1121df8bae1dSRodney W. Grimes 	u_char *dst;
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes 	m = src_arg;
1124df8bae1dSRodney W. Grimes 	dst = dst_arg;
1125df8bae1dSRodney W. Grimes 	while (len > 0) {
1126df8bae1dSRodney W. Grimes 		if (m == 0)
1127df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1128df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
11290453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1130df8bae1dSRodney W. Grimes 		m = m->m_next;
1131df8bae1dSRodney W. Grimes 		dst += count;
1132df8bae1dSRodney W. Grimes 		len -= count;
1133df8bae1dSRodney W. Grimes 	}
1134df8bae1dSRodney W. Grimes }
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes /*
1137df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1138df8bae1dSRodney W. Grimes  */
1139df8bae1dSRodney W. Grimes void
114024a229f4SSam Leffler bpf_mtap(bp, m)
114124a229f4SSam Leffler 	struct bpf_if *bp;
1142df8bae1dSRodney W. Grimes 	struct mbuf *m;
1143df8bae1dSRodney W. Grimes {
1144df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1145df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1146df8bae1dSRodney W. Grimes 
1147f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
11487b831242SPoul-Henning Kamp 	if (pktlen == m->m_len) {
114924a229f4SSam Leffler 		bpf_tap(bp, mtod(m, u_char *), pktlen);
11507b831242SPoul-Henning Kamp 		return;
11517b831242SPoul-Henning Kamp 	}
1152df8bae1dSRodney W. Grimes 
1153e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1154df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
11558ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
11568ed3828cSRobert Watson 			continue;
1157e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1158df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1159df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1160df8bae1dSRodney W. Grimes 		if (slen != 0)
11610c7fb534SRobert Watson #ifdef MAC
116224a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
11630c7fb534SRobert Watson #endif
11640c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
11650c7fb534SRobert Watson 				    bpf_mcopy);
1166e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1167df8bae1dSRodney W. Grimes 	}
1168e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1169df8bae1dSRodney W. Grimes }
1170df8bae1dSRodney W. Grimes 
1171df8bae1dSRodney W. Grimes /*
1172df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
1173df8bae1dSRodney W. Grimes  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
1174df8bae1dSRodney W. Grimes  * otherwise 0.  "copy" is the routine called to do the actual data
1175df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1176df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1177df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1178df8bae1dSRodney W. Grimes  */
1179df8bae1dSRodney W. Grimes static void
1180df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
11818994a245SDag-Erling Smørgrav 	struct bpf_d *d;
11828994a245SDag-Erling Smørgrav 	u_char *pkt;
11838994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
11848994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1185df8bae1dSRodney W. Grimes {
11868994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
11878994a245SDag-Erling Smørgrav 	int totlen, curlen;
11888994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
1189df8bae1dSRodney W. Grimes 	/*
1190df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1191df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1192df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1193df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1194df8bae1dSRodney W. Grimes 	 */
1195df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1196df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1197df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1198df8bae1dSRodney W. Grimes 
1199df8bae1dSRodney W. Grimes 	/*
1200df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1201df8bae1dSRodney W. Grimes 	 */
1202df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1203df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1204df8bae1dSRodney W. Grimes 		/*
1205df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1206df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1207df8bae1dSRodney W. Grimes 		 * pending reads.
1208df8bae1dSRodney W. Grimes 		 */
1209df8bae1dSRodney W. Grimes 		if (d->bd_fbuf == 0) {
1210df8bae1dSRodney W. Grimes 			/*
1211df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1212df8bae1dSRodney W. Grimes 			 * so drop the packet.
1213df8bae1dSRodney W. Grimes 			 */
1214df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1215df8bae1dSRodney W. Grimes 			return;
1216df8bae1dSRodney W. Grimes 		}
1217df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
1218df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1219df8bae1dSRodney W. Grimes 		curlen = 0;
1220df8bae1dSRodney W. Grimes 	}
122181bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1222df8bae1dSRodney W. Grimes 		/*
122381bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
122481bda851SJohn Polstra 		 * already expired during a select call.  A packet
122581bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1226df8bae1dSRodney W. Grimes 		 */
1227df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1228df8bae1dSRodney W. Grimes 
1229df8bae1dSRodney W. Grimes 	/*
1230df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1231df8bae1dSRodney W. Grimes 	 */
1232df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1233df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1234df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1235df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1236df8bae1dSRodney W. Grimes 	/*
1237df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1238df8bae1dSRodney W. Grimes 	 */
1239df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1240df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
1241df8bae1dSRodney W. Grimes }
1242df8bae1dSRodney W. Grimes 
1243df8bae1dSRodney W. Grimes /*
1244df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1245df8bae1dSRodney W. Grimes  */
1246df8bae1dSRodney W. Grimes static int
1247df8bae1dSRodney W. Grimes bpf_allocbufs(d)
12488994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1249df8bae1dSRodney W. Grimes {
1250a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1251df8bae1dSRodney W. Grimes 	if (d->bd_fbuf == 0)
1252df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1253df8bae1dSRodney W. Grimes 
1254a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1255df8bae1dSRodney W. Grimes 	if (d->bd_sbuf == 0) {
1256bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1257df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1258df8bae1dSRodney W. Grimes 	}
1259df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1260df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1261df8bae1dSRodney W. Grimes 	return (0);
1262df8bae1dSRodney W. Grimes }
1263df8bae1dSRodney W. Grimes 
1264df8bae1dSRodney W. Grimes /*
1265df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1266df8bae1dSRodney W. Grimes  * Called on close.
1267df8bae1dSRodney W. Grimes  */
1268df8bae1dSRodney W. Grimes static void
1269df8bae1dSRodney W. Grimes bpf_freed(d)
12708994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1271df8bae1dSRodney W. Grimes {
1272df8bae1dSRodney W. Grimes 	/*
1273df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1274df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1275df8bae1dSRodney W. Grimes 	 * free.
1276df8bae1dSRodney W. Grimes 	 */
1277df8bae1dSRodney W. Grimes 	if (d->bd_sbuf != 0) {
1278bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1279df8bae1dSRodney W. Grimes 		if (d->bd_hbuf != 0)
1280bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1281df8bae1dSRodney W. Grimes 		if (d->bd_fbuf != 0)
1282bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1283df8bae1dSRodney W. Grimes 	}
1284df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1285bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1286e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1287df8bae1dSRodney W. Grimes }
1288df8bae1dSRodney W. Grimes 
1289df8bae1dSRodney W. Grimes /*
129024a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
129124a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1292df8bae1dSRodney W. Grimes  */
1293df8bae1dSRodney W. Grimes void
12949b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1295df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1296df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1297df8bae1dSRodney W. Grimes {
129824a229f4SSam Leffler 
129924a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
130024a229f4SSam Leffler }
130124a229f4SSam Leffler 
130224a229f4SSam Leffler /*
130324a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
130424a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
130524a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
130624a229f4SSam Leffler  * headers are not yet supporrted).
130724a229f4SSam Leffler  */
130824a229f4SSam Leffler void
130924a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
131024a229f4SSam Leffler 	struct ifnet *ifp;
131124a229f4SSam Leffler 	u_int dlt, hdrlen;
131224a229f4SSam Leffler 	struct bpf_if **driverp;
131324a229f4SSam Leffler {
1314df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
13156a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1316df8bae1dSRodney W. Grimes 	if (bp == 0)
1317df8bae1dSRodney W. Grimes 		panic("bpfattach");
1318df8bae1dSRodney W. Grimes 
131924a229f4SSam Leffler 	bp->bif_dlist = 0;
132024a229f4SSam Leffler 	bp->bif_driverp = driverp;
1321df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1322df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
13236008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1324df8bae1dSRodney W. Grimes 
1325e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
1326df8bae1dSRodney W. Grimes 	bp->bif_next = bpf_iflist;
1327df8bae1dSRodney W. Grimes 	bpf_iflist = bp;
1328e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1329df8bae1dSRodney W. Grimes 
133024a229f4SSam Leffler 	*bp->bif_driverp = 0;
1331df8bae1dSRodney W. Grimes 
1332df8bae1dSRodney W. Grimes 	/*
1333df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1334df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1335df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1336df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1337df8bae1dSRodney W. Grimes 	 */
1338df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1339df8bae1dSRodney W. Grimes 
13402eeab939SGarrett Wollman 	if (bootverbose)
134124a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1342df8bae1dSRodney W. Grimes }
134353ac6efbSJulian Elischer 
1344de5d9935SRobert Watson /*
1345de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1346de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1347de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1348de5d9935SRobert Watson  * ENXIO.
1349de5d9935SRobert Watson  */
1350de5d9935SRobert Watson void
1351de5d9935SRobert Watson bpfdetach(ifp)
1352de5d9935SRobert Watson 	struct ifnet *ifp;
1353de5d9935SRobert Watson {
1354de5d9935SRobert Watson 	struct bpf_if	*bp, *bp_prev;
1355de5d9935SRobert Watson 	struct bpf_d	*d;
1356de5d9935SRobert Watson 
1357de5d9935SRobert Watson 	/* Locate BPF interface information */
1358de5d9935SRobert Watson 	bp_prev = NULL;
13598eab61f3SSam Leffler 
13608eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
1361de5d9935SRobert Watson 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1362de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1363de5d9935SRobert Watson 			break;
1364de5d9935SRobert Watson 		bp_prev = bp;
1365de5d9935SRobert Watson 	}
1366de5d9935SRobert Watson 
1367de5d9935SRobert Watson 	/* Interface wasn't attached */
1368de5d9935SRobert Watson 	if (bp->bif_ifp == NULL) {
1369e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1370de5d9935SRobert Watson 		printf("bpfdetach: %s%d was not attached\n", ifp->if_name,
1371de5d9935SRobert Watson 		    ifp->if_unit);
1372de5d9935SRobert Watson 		return;
1373de5d9935SRobert Watson 	}
1374de5d9935SRobert Watson 
1375de5d9935SRobert Watson 	if (bp_prev) {
1376de5d9935SRobert Watson 		bp_prev->bif_next = bp->bif_next;
1377de5d9935SRobert Watson 	} else {
1378de5d9935SRobert Watson 		bpf_iflist = bp->bif_next;
1379de5d9935SRobert Watson 	}
13808eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1381de5d9935SRobert Watson 
1382e7bb21b3SJonathan Lemon 	while ((d = bp->bif_dlist) != NULL) {
1383e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1384e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1385e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1386e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1387e7bb21b3SJonathan Lemon 	}
1388e7bb21b3SJonathan Lemon 
1389e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1390de5d9935SRobert Watson 	free(bp, M_BPF);
13918eab61f3SSam Leffler }
1392de5d9935SRobert Watson 
13938eab61f3SSam Leffler /*
13948eab61f3SSam Leffler  * Get a list of available data link type of the interface.
13958eab61f3SSam Leffler  */
13968eab61f3SSam Leffler static int
13978eab61f3SSam Leffler bpf_getdltlist(d, bfl)
13988eab61f3SSam Leffler 	struct bpf_d *d;
13998eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
14008eab61f3SSam Leffler {
14018eab61f3SSam Leffler 	int n, error;
14028eab61f3SSam Leffler 	struct ifnet *ifp;
14038eab61f3SSam Leffler 	struct bpf_if *bp;
14048eab61f3SSam Leffler 
14058eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
14068eab61f3SSam Leffler 	n = 0;
14078eab61f3SSam Leffler 	error = 0;
14088eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
14098eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
14108eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
14118eab61f3SSam Leffler 			continue;
14128eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
14138eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1414e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
14158eab61f3SSam Leffler 				return (ENOMEM);
14168eab61f3SSam Leffler 			}
14178eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
14188eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
14198eab61f3SSam Leffler 		}
14208eab61f3SSam Leffler 		n++;
14218eab61f3SSam Leffler 	}
14228eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
14238eab61f3SSam Leffler 	bfl->bfl_len = n;
14248eab61f3SSam Leffler 	return (error);
14258eab61f3SSam Leffler }
14268eab61f3SSam Leffler 
14278eab61f3SSam Leffler /*
14288eab61f3SSam Leffler  * Set the data link type of a BPF instance.
14298eab61f3SSam Leffler  */
14308eab61f3SSam Leffler static int
14318eab61f3SSam Leffler bpf_setdlt(d, dlt)
14328eab61f3SSam Leffler 	struct bpf_d *d;
14338eab61f3SSam Leffler 	u_int dlt;
14348eab61f3SSam Leffler {
14358eab61f3SSam Leffler 	int error, opromisc;
14368eab61f3SSam Leffler 	struct ifnet *ifp;
14378eab61f3SSam Leffler 	struct bpf_if *bp;
14388eab61f3SSam Leffler 
14398eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
14408eab61f3SSam Leffler 		return (0);
14418eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
14428eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
14438eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
14448eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
14458eab61f3SSam Leffler 			break;
14468eab61f3SSam Leffler 	}
14478eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
14488eab61f3SSam Leffler 	if (bp != NULL) {
14498eab61f3SSam Leffler 		BPFD_LOCK(d);
14508eab61f3SSam Leffler 		opromisc = d->bd_promisc;
14518eab61f3SSam Leffler 		bpf_detachd(d);
14528eab61f3SSam Leffler 		bpf_attachd(d, bp);
14538eab61f3SSam Leffler 		reset_d(d);
14548eab61f3SSam Leffler 		BPFD_UNLOCK(d);
14558eab61f3SSam Leffler 		if (opromisc) {
14568eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
14578eab61f3SSam Leffler 			if (error)
14588eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
14598eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
14608eab61f3SSam Leffler 					error);
14618eab61f3SSam Leffler 			else
14628eab61f3SSam Leffler 				d->bd_promisc = 1;
14638eab61f3SSam Leffler 		}
14648eab61f3SSam Leffler 	}
14658eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1466de5d9935SRobert Watson }
1467de5d9935SRobert Watson 
1468929ddbbbSAlfred Perlstein static void bpf_drvinit(void *unused);
1469bd3a5320SPoul-Henning Kamp 
1470929ddbbbSAlfred Perlstein static void bpf_clone(void *arg, char *name, int namelen, dev_t *dev);
14713f54a085SPoul-Henning Kamp 
14723f54a085SPoul-Henning Kamp static void
14733f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
14743f54a085SPoul-Henning Kamp 	void *arg;
14753f54a085SPoul-Henning Kamp 	char *name;
14763f54a085SPoul-Henning Kamp 	int namelen;
14773f54a085SPoul-Henning Kamp 	dev_t *dev;
14783f54a085SPoul-Henning Kamp {
14793f54a085SPoul-Henning Kamp 	int u;
14803f54a085SPoul-Henning Kamp 
14813f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
14823f54a085SPoul-Henning Kamp 		return;
1483db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
14843f54a085SPoul-Henning Kamp 		return;
1485b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1486b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1487b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
14883f54a085SPoul-Henning Kamp 	return;
14893f54a085SPoul-Henning Kamp }
14903f54a085SPoul-Henning Kamp 
1491514ede09SBruce Evans static void
1492514ede09SBruce Evans bpf_drvinit(unused)
1493514ede09SBruce Evans 	void *unused;
149453ac6efbSJulian Elischer {
149553ac6efbSJulian Elischer 
14966008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1497db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
14987198bf47SJulian Elischer }
149953ac6efbSJulian Elischer 
150053ac6efbSJulian Elischer SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
150153ac6efbSJulian Elischer 
15025bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1503f8dc4716SMike Smith /*
1504f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1505f8dc4716SMike Smith  *
1506f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1507f8dc4716SMike Smith  * to be loaded at runtime.
1508f8dc4716SMike Smith  */
1509f8dc4716SMike Smith 
1510f8dc4716SMike Smith void
1511e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1512e5562beeSSam Leffler 	struct bpf_if *bp;
15138994a245SDag-Erling Smørgrav 	u_char *pkt;
15148994a245SDag-Erling Smørgrav 	u_int pktlen;
1515f8dc4716SMike Smith {
1516f8dc4716SMike Smith }
1517f8dc4716SMike Smith 
1518f8dc4716SMike Smith void
1519e5562beeSSam Leffler bpf_mtap(bp, m)
1520e5562beeSSam Leffler 	struct bpf_if *bp;
1521f8dc4716SMike Smith 	struct mbuf *m;
1522f8dc4716SMike Smith {
1523f8dc4716SMike Smith }
1524f8dc4716SMike Smith 
1525f8dc4716SMike Smith void
1526f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1527f8dc4716SMike Smith 	struct ifnet *ifp;
1528f8dc4716SMike Smith 	u_int dlt, hdrlen;
1529f8dc4716SMike Smith {
1530f8dc4716SMike Smith }
1531f8dc4716SMike Smith 
1532da626c17SBill Paul void
1533da626c17SBill Paul bpfdetach(ifp)
1534da626c17SBill Paul 	struct ifnet *ifp;
1535da626c17SBill Paul {
1536da626c17SBill Paul }
1537da626c17SBill Paul 
1538f8dc4716SMike Smith u_int
1539f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
15408994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
15418994a245SDag-Erling Smørgrav 	u_char *p;
1542f8dc4716SMike Smith 	u_int wirelen;
15438994a245SDag-Erling Smørgrav 	u_int buflen;
1544f8dc4716SMike Smith {
1545f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1546f8dc4716SMike Smith }
1547f8dc4716SMike Smith 
15485bb5f2c9SPeter Wemm int
15495bb5f2c9SPeter Wemm bpf_validate(f, len)
15505bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
15515bb5f2c9SPeter Wemm 	int len;
15525bb5f2c9SPeter Wemm {
15535bb5f2c9SPeter Wemm 	return 0;		/* false */
15545bb5f2c9SPeter Wemm }
15555bb5f2c9SPeter Wemm 
15565bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1557