xref: /freebsd/sys/net/bpf.c (revision 24a229f4668ed54c5a84b136cddfcf08e5fef9fe)
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 *);
112df8bae1dSRodney W. Grimes 
11387f6c662SJulian Elischer static	d_open_t	bpfopen;
11487f6c662SJulian Elischer static	d_close_t	bpfclose;
11587f6c662SJulian Elischer static	d_read_t	bpfread;
11687f6c662SJulian Elischer static	d_write_t	bpfwrite;
11787f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
118243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
11987f6c662SJulian Elischer 
12087f6c662SJulian Elischer #define CDEV_MAJOR 23
1214e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
1224e2f199eSPoul-Henning Kamp 	/* open */	bpfopen,
1234e2f199eSPoul-Henning Kamp 	/* close */	bpfclose,
1244e2f199eSPoul-Henning Kamp 	/* read */	bpfread,
1254e2f199eSPoul-Henning Kamp 	/* write */	bpfwrite,
1264e2f199eSPoul-Henning Kamp 	/* ioctl */	bpfioctl,
1274e2f199eSPoul-Henning Kamp 	/* poll */	bpfpoll,
1284e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
1294e2f199eSPoul-Henning Kamp 	/* strategy */	nostrategy,
1304e2f199eSPoul-Henning Kamp 	/* name */	"bpf",
1314e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
1324e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1334e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1344e2f199eSPoul-Henning Kamp 	/* flags */	0,
1354e2f199eSPoul-Henning Kamp };
13687f6c662SJulian Elischer 
13787f6c662SJulian Elischer 
138df8bae1dSRodney W. Grimes static int
139df8bae1dSRodney W. Grimes bpf_movein(uio, linktype, mp, sockp, datlen)
140df8bae1dSRodney W. Grimes 	register struct uio *uio;
141df8bae1dSRodney W. Grimes 	int linktype, *datlen;
142df8bae1dSRodney W. Grimes 	register struct mbuf **mp;
143df8bae1dSRodney W. Grimes 	register struct sockaddr *sockp;
144df8bae1dSRodney W. Grimes {
145df8bae1dSRodney W. Grimes 	struct mbuf *m;
146df8bae1dSRodney W. Grimes 	int error;
147df8bae1dSRodney W. Grimes 	int len;
148df8bae1dSRodney W. Grimes 	int hlen;
149df8bae1dSRodney W. Grimes 
150df8bae1dSRodney W. Grimes 	/*
151df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
152df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
153df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
154df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
155df8bae1dSRodney W. Grimes 	 * is forwarded as is.
156df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
157df8bae1dSRodney W. Grimes 	 * for the link level header.
158df8bae1dSRodney W. Grimes 	 */
159df8bae1dSRodney W. Grimes 	switch (linktype) {
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes 	case DLT_SLIP:
162df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
163df8bae1dSRodney W. Grimes 		hlen = 0;
164df8bae1dSRodney W. Grimes 		break;
165df8bae1dSRodney W. Grimes 
166df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
167df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
168df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
169df8bae1dSRodney W. Grimes 		hlen = sizeof(struct ether_header);
170df8bae1dSRodney W. Grimes 		break;
171df8bae1dSRodney W. Grimes 
172df8bae1dSRodney W. Grimes 	case DLT_FDDI:
173d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
174d41f24e7SDavid Greenman 		hlen = 0;
175df8bae1dSRodney W. Grimes 		break;
176df8bae1dSRodney W. Grimes 
17722f05c43SAndrey A. Chernov 	case DLT_RAW:
178df8bae1dSRodney W. Grimes 	case DLT_NULL:
179df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
180df8bae1dSRodney W. Grimes 		hlen = 0;
181df8bae1dSRodney W. Grimes 		break;
182df8bae1dSRodney W. Grimes 
1834f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
1844f53e3ccSKenjiro Cho 		/*
1854f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
1864f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
1874f53e3ccSKenjiro Cho 		 * specified anyway.
1884f53e3ccSKenjiro Cho 		 */
1894f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
1904f53e3ccSKenjiro Cho 		hlen = 12; 	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
1914f53e3ccSKenjiro Cho 		break;
1924f53e3ccSKenjiro Cho 
19330fa52a6SBrian Somers 	case DLT_PPP:
19430fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
19530fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
19630fa52a6SBrian Somers 		break;
19730fa52a6SBrian Somers 
198df8bae1dSRodney W. Grimes 	default:
199df8bae1dSRodney W. Grimes 		return (EIO);
200df8bae1dSRodney W. Grimes 	}
201df8bae1dSRodney W. Grimes 
202df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
203df8bae1dSRodney W. Grimes 	*datlen = len - hlen;
204df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
205df8bae1dSRodney W. Grimes 		return (EIO);
206df8bae1dSRodney W. Grimes 
207963e4c2aSGarrett Wollman 	if (len > MHLEN) {
20824a229f4SSam Leffler 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
20924a229f4SSam Leffler 	} else {
21024a229f4SSam Leffler 		MGETHDR(m, M_TRYWAIT, MT_DATA);
211df8bae1dSRodney W. Grimes 	}
21224a229f4SSam Leffler 	if (m == NULL)
21324a229f4SSam Leffler 		return (ENOBUFS);
214963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
215963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
216df8bae1dSRodney W. Grimes 	*mp = m;
21724a229f4SSam Leffler 
218df8bae1dSRodney W. Grimes 	/*
219df8bae1dSRodney W. Grimes 	 * Make room for link header.
220df8bae1dSRodney W. Grimes 	 */
221df8bae1dSRodney W. Grimes 	if (hlen != 0) {
2224f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
223df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
224df8bae1dSRodney W. Grimes #if BSD >= 199103
225df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
226df8bae1dSRodney W. Grimes #else
227df8bae1dSRodney W. Grimes 		m->m_off += hlen;
228df8bae1dSRodney W. Grimes #endif
229e7bb21b3SJonathan Lemon 		error = uiomove((caddr_t)sockp->sa_data, hlen, uio);
230df8bae1dSRodney W. Grimes 		if (error)
231df8bae1dSRodney W. Grimes 			goto bad;
232df8bae1dSRodney W. Grimes 	}
233e7bb21b3SJonathan Lemon 	error = uiomove(mtod(m, caddr_t), len - hlen, uio);
234df8bae1dSRodney W. Grimes 	if (!error)
235df8bae1dSRodney W. Grimes 		return (0);
236df8bae1dSRodney W. Grimes bad:
237df8bae1dSRodney W. Grimes 	m_freem(m);
238df8bae1dSRodney W. Grimes 	return (error);
239df8bae1dSRodney W. Grimes }
240df8bae1dSRodney W. Grimes 
241df8bae1dSRodney W. Grimes /*
242df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
243df8bae1dSRodney W. Grimes  */
244df8bae1dSRodney W. Grimes static void
245df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
246df8bae1dSRodney W. Grimes 	struct bpf_d *d;
247df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
248df8bae1dSRodney W. Grimes {
249df8bae1dSRodney W. Grimes 	/*
250df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
251df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
252df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
253df8bae1dSRodney W. Grimes 	 */
254e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
255df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
256df8bae1dSRodney W. Grimes 	d->bd_next = bp->bif_dlist;
257df8bae1dSRodney W. Grimes 	bp->bif_dlist = d;
258df8bae1dSRodney W. Grimes 
25924a229f4SSam Leffler 	*bp->bif_driverp = bp;
260e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
261df8bae1dSRodney W. Grimes }
262df8bae1dSRodney W. Grimes 
263df8bae1dSRodney W. Grimes /*
264df8bae1dSRodney W. Grimes  * Detach a file from its interface.
265df8bae1dSRodney W. Grimes  */
266df8bae1dSRodney W. Grimes static void
267df8bae1dSRodney W. Grimes bpf_detachd(d)
268df8bae1dSRodney W. Grimes 	struct bpf_d *d;
269df8bae1dSRodney W. Grimes {
2706e891d64SPoul-Henning Kamp 	int error;
271df8bae1dSRodney W. Grimes 	struct bpf_d **p;
272df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
273df8bae1dSRodney W. Grimes 
274df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
275df8bae1dSRodney W. Grimes 	/*
276df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
277df8bae1dSRodney W. Grimes 	 * If so, turn it off.
278df8bae1dSRodney W. Grimes 	 */
279df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
280df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
2816e891d64SPoul-Henning Kamp 		error = ifpromisc(bp->bif_ifp, 0);
2826e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
283df8bae1dSRodney W. Grimes 			/*
2846e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
285df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
286df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
287df8bae1dSRodney W. Grimes 			 * take it out.
288df8bae1dSRodney W. Grimes 			 */
28929e1b85fSBrooks Davis 			if_printf(bp->bif_ifp, "ifpromisc failed %d\n", error);
2906e891d64SPoul-Henning Kamp 		}
291df8bae1dSRodney W. Grimes 	}
292df8bae1dSRodney W. Grimes 	/* Remove d from the interface's descriptor list. */
293e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
294df8bae1dSRodney W. Grimes 	p = &bp->bif_dlist;
295df8bae1dSRodney W. Grimes 	while (*p != d) {
296df8bae1dSRodney W. Grimes 		p = &(*p)->bd_next;
297df8bae1dSRodney W. Grimes 		if (*p == 0)
298df8bae1dSRodney W. Grimes 			panic("bpf_detachd: descriptor not in list");
299df8bae1dSRodney W. Grimes 	}
300df8bae1dSRodney W. Grimes 	*p = (*p)->bd_next;
301df8bae1dSRodney W. Grimes 	if (bp->bif_dlist == 0)
302df8bae1dSRodney W. Grimes 		/*
303df8bae1dSRodney W. Grimes 		 * Let the driver know that there are no more listeners.
304df8bae1dSRodney W. Grimes 		 */
30524a229f4SSam Leffler 		*d->bd_bif->bif_driverp = 0;
306e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
307df8bae1dSRodney W. Grimes 	d->bd_bif = 0;
308df8bae1dSRodney W. Grimes }
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes /*
311df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
312df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
313df8bae1dSRodney W. Grimes  */
314df8bae1dSRodney W. Grimes /* ARGSUSED */
31587f6c662SJulian Elischer static	int
316b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
317df8bae1dSRodney W. Grimes 	dev_t dev;
31860039670SBruce Evans 	int flags;
31960039670SBruce Evans 	int fmt;
320b40ce416SJulian Elischer 	struct thread *td;
321df8bae1dSRodney W. Grimes {
322e7bb21b3SJonathan Lemon 	struct bpf_d *d;
323df8bae1dSRodney W. Grimes 
324e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
325bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
326df8bae1dSRodney W. Grimes 	/*
327df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
328df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
329df8bae1dSRodney W. Grimes 	 */
330e7bb21b3SJonathan Lemon 	if (d) {
331e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
332df8bae1dSRodney W. Grimes 		return (EBUSY);
333e7bb21b3SJonathan Lemon 	}
334e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
335e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
336e7bb21b3SJonathan Lemon 
337d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
338b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
339b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
3407cc0979fSDavid Malone 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
341bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
342df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
34300a83887SPaul Traina 	d->bd_sig = SIGIO;
3448ed3828cSRobert Watson 	d->bd_seesent = 1;
34582f4445dSRobert Watson #ifdef MAC
34682f4445dSRobert Watson 	mac_init_bpfdesc(d);
34782f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
34882f4445dSRobert Watson #endif
3496008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
35081bda851SJohn Polstra 	callout_init(&d->bd_callout, 1);
351df8bae1dSRodney W. Grimes 
352df8bae1dSRodney W. Grimes 	return (0);
353df8bae1dSRodney W. Grimes }
354df8bae1dSRodney W. Grimes 
355df8bae1dSRodney W. Grimes /*
356df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
357df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
358df8bae1dSRodney W. Grimes  */
359df8bae1dSRodney W. Grimes /* ARGSUSED */
36087f6c662SJulian Elischer static	int
361b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
362df8bae1dSRodney W. Grimes 	dev_t dev;
36360039670SBruce Evans 	int flags;
36460039670SBruce Evans 	int fmt;
365b40ce416SJulian Elischer 	struct thread *td;
366df8bae1dSRodney W. Grimes {
367e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
368df8bae1dSRodney W. Grimes 
36981bda851SJohn Polstra 	BPFD_LOCK(d);
37081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
37181bda851SJohn Polstra 		callout_stop(&d->bd_callout);
37281bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
37381bda851SJohn Polstra 	BPFD_UNLOCK(d);
374e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
375e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
376df8bae1dSRodney W. Grimes 	if (d->bd_bif)
377df8bae1dSRodney W. Grimes 		bpf_detachd(d);
378e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
37982f4445dSRobert Watson #ifdef MAC
38082f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
38182f4445dSRobert Watson #endif /* MAC */
382df8bae1dSRodney W. Grimes 	bpf_freed(d);
383bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = 0;
384d722be54SLuigi Rizzo 	free(d, M_BPF);
385df8bae1dSRodney W. Grimes 
386df8bae1dSRodney W. Grimes 	return (0);
387df8bae1dSRodney W. Grimes }
388df8bae1dSRodney W. Grimes 
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes /*
391df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
392df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
393df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
394df8bae1dSRodney W. Grimes  */
395df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
396df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
397df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
398df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
399df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
400df8bae1dSRodney W. Grimes 	(d)->bd_fbuf = 0;
401df8bae1dSRodney W. Grimes /*
402df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
403df8bae1dSRodney W. Grimes  */
40487f6c662SJulian Elischer static	int
40560039670SBruce Evans bpfread(dev, uio, ioflag)
406df8bae1dSRodney W. Grimes 	dev_t dev;
407df8bae1dSRodney W. Grimes 	register struct uio *uio;
40860039670SBruce Evans 	int ioflag;
409df8bae1dSRodney W. Grimes {
410e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
41181bda851SJohn Polstra 	int timed_out;
412df8bae1dSRodney W. Grimes 	int error;
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes 	/*
415df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
416df8bae1dSRodney W. Grimes 	 * as kernel buffers.
417df8bae1dSRodney W. Grimes 	 */
418df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
419df8bae1dSRodney W. Grimes 		return (EINVAL);
420df8bae1dSRodney W. Grimes 
421e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
42281bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
42381bda851SJohn Polstra 		callout_stop(&d->bd_callout);
42481bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
42581bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
426df8bae1dSRodney W. Grimes 	/*
427df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
428df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
429df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
430df8bae1dSRodney W. Grimes 	 */
431df8bae1dSRodney W. Grimes 	while (d->bd_hbuf == 0) {
43281bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
433df8bae1dSRodney W. Grimes 			/*
434df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
435df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
436df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
437df8bae1dSRodney W. Grimes 			 */
438df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
439df8bae1dSRodney W. Grimes 			break;
440df8bae1dSRodney W. Grimes 		}
441de5d9935SRobert Watson 
442de5d9935SRobert Watson 		/*
443de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
444de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
445de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
446de5d9935SRobert Watson 		 * it before using it again.
447de5d9935SRobert Watson 		 */
448de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
449e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
450de5d9935SRobert Watson 			return (ENXIO);
451de5d9935SRobert Watson 		}
452de5d9935SRobert Watson 
453fba3cfdeSJohn Polstra 		if (ioflag & IO_NDELAY) {
454e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
455fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
456fba3cfdeSJohn Polstra 		}
457e7bb21b3SJonathan Lemon 		error = msleep((caddr_t)d, &d->bd_mtx, PRINET|PCATCH,
458e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
459df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
460e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
461df8bae1dSRodney W. Grimes 			return (error);
462df8bae1dSRodney W. Grimes 		}
463df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
464df8bae1dSRodney W. Grimes 			/*
465df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
466df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
467df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
468df8bae1dSRodney W. Grimes 			 */
469df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
470df8bae1dSRodney W. Grimes 				/*
471df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
472df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
473df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
474df8bae1dSRodney W. Grimes 				 */
475df8bae1dSRodney W. Grimes 				break;
476df8bae1dSRodney W. Grimes 
477df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
478e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
479df8bae1dSRodney W. Grimes 				return (0);
480df8bae1dSRodney W. Grimes 			}
481df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
482df8bae1dSRodney W. Grimes 			break;
483df8bae1dSRodney W. Grimes 		}
484df8bae1dSRodney W. Grimes 	}
485df8bae1dSRodney W. Grimes 	/*
486df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
487df8bae1dSRodney W. Grimes 	 */
488e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
489df8bae1dSRodney W. Grimes 
490df8bae1dSRodney W. Grimes 	/*
491df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
492df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
493df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
494df8bae1dSRodney W. Grimes 	 */
495e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
496df8bae1dSRodney W. Grimes 
497e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
498df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
499df8bae1dSRodney W. Grimes 	d->bd_hbuf = 0;
500df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
501e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
502df8bae1dSRodney W. Grimes 
503df8bae1dSRodney W. Grimes 	return (error);
504df8bae1dSRodney W. Grimes }
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 
507df8bae1dSRodney W. Grimes /*
508df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
509df8bae1dSRodney W. Grimes  */
510e7bb21b3SJonathan Lemon static __inline void
511df8bae1dSRodney W. Grimes bpf_wakeup(d)
512df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
513df8bae1dSRodney W. Grimes {
51481bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
51581bda851SJohn Polstra 		callout_stop(&d->bd_callout);
51681bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
51781bda851SJohn Polstra 	}
518df8bae1dSRodney W. Grimes 	wakeup((caddr_t)d);
519831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
520f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
52100a83887SPaul Traina 
522df8bae1dSRodney W. Grimes 	selwakeup(&d->bd_sel);
523df8bae1dSRodney W. Grimes }
524df8bae1dSRodney W. Grimes 
52581bda851SJohn Polstra static void
52681bda851SJohn Polstra bpf_timed_out(arg)
52781bda851SJohn Polstra 	void *arg;
52881bda851SJohn Polstra {
52981bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
53081bda851SJohn Polstra 
53181bda851SJohn Polstra 	BPFD_LOCK(d);
53281bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
53381bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
53481bda851SJohn Polstra 		if (d->bd_slen != 0)
53581bda851SJohn Polstra 			bpf_wakeup(d);
53681bda851SJohn Polstra 	}
53781bda851SJohn Polstra 	BPFD_UNLOCK(d);
53881bda851SJohn Polstra }
53981bda851SJohn Polstra 
54087f6c662SJulian Elischer static	int
54160039670SBruce Evans bpfwrite(dev, uio, ioflag)
542df8bae1dSRodney W. Grimes 	dev_t dev;
543df8bae1dSRodney W. Grimes 	struct uio *uio;
54460039670SBruce Evans 	int ioflag;
545df8bae1dSRodney W. Grimes {
546e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
547df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
548df8bae1dSRodney W. Grimes 	struct mbuf *m;
549e7bb21b3SJonathan Lemon 	int error;
550df8bae1dSRodney W. Grimes 	static struct sockaddr dst;
551df8bae1dSRodney W. Grimes 	int datlen;
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes 	if (d->bd_bif == 0)
554df8bae1dSRodney W. Grimes 		return (ENXIO);
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
557df8bae1dSRodney W. Grimes 
558df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
559df8bae1dSRodney W. Grimes 		return (0);
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
562df8bae1dSRodney W. Grimes 	if (error)
563df8bae1dSRodney W. Grimes 		return (error);
564df8bae1dSRodney W. Grimes 
565df8bae1dSRodney W. Grimes 	if (datlen > ifp->if_mtu)
566df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
567df8bae1dSRodney W. Grimes 
568114ae644SMike Smith 	if (d->bd_hdrcmplt)
569114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
570114ae644SMike Smith 
571e7bb21b3SJonathan Lemon 	mtx_lock(&Giant);
57282f4445dSRobert Watson #ifdef MAC
57382f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
57482f4445dSRobert Watson #endif
575df8bae1dSRodney W. Grimes 	error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
576e7bb21b3SJonathan Lemon 	mtx_unlock(&Giant);
577df8bae1dSRodney W. Grimes 	/*
578df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
579df8bae1dSRodney W. Grimes 	 */
580df8bae1dSRodney W. Grimes 	return (error);
581df8bae1dSRodney W. Grimes }
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes /*
584df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
585e7bb21b3SJonathan Lemon  * receive and drop counts.
586df8bae1dSRodney W. Grimes  */
587df8bae1dSRodney W. Grimes static void
588df8bae1dSRodney W. Grimes reset_d(d)
589df8bae1dSRodney W. Grimes 	struct bpf_d *d;
590df8bae1dSRodney W. Grimes {
591e7bb21b3SJonathan Lemon 
592e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
593df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
594df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
595df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
596df8bae1dSRodney W. Grimes 		d->bd_hbuf = 0;
597df8bae1dSRodney W. Grimes 	}
598df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
599df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
600df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
601df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
602df8bae1dSRodney W. Grimes }
603df8bae1dSRodney W. Grimes 
604df8bae1dSRodney W. Grimes /*
605df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
606df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
607df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
608df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
609df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
610df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
611df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
612df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
613df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
614df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
615df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
616df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
617df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
618df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
619114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
620114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6218ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6228ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
623df8bae1dSRodney W. Grimes  */
624df8bae1dSRodney W. Grimes /* ARGSUSED */
62587f6c662SJulian Elischer static	int
626b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
627df8bae1dSRodney W. Grimes 	dev_t dev;
628ecbb00a2SDoug Rabson 	u_long cmd;
629df8bae1dSRodney W. Grimes 	caddr_t addr;
63060039670SBruce Evans 	int flags;
631b40ce416SJulian Elischer 	struct thread *td;
632df8bae1dSRodney W. Grimes {
633e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
634e7bb21b3SJonathan Lemon 	int error = 0;
635df8bae1dSRodney W. Grimes 
63681bda851SJohn Polstra 	BPFD_LOCK(d);
63781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
63881bda851SJohn Polstra 		callout_stop(&d->bd_callout);
63981bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
64081bda851SJohn Polstra 	BPFD_UNLOCK(d);
64181bda851SJohn Polstra 
642df8bae1dSRodney W. Grimes 	switch (cmd) {
643df8bae1dSRodney W. Grimes 
644df8bae1dSRodney W. Grimes 	default:
645df8bae1dSRodney W. Grimes 		error = EINVAL;
646df8bae1dSRodney W. Grimes 		break;
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes 	/*
649df8bae1dSRodney W. Grimes 	 * Check for read packet available.
650df8bae1dSRodney W. Grimes 	 */
651df8bae1dSRodney W. Grimes 	case FIONREAD:
652df8bae1dSRodney W. Grimes 		{
653df8bae1dSRodney W. Grimes 			int n;
654df8bae1dSRodney W. Grimes 
655e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
656df8bae1dSRodney W. Grimes 			n = d->bd_slen;
657df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
658df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
659e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
660df8bae1dSRodney W. Grimes 
661df8bae1dSRodney W. Grimes 			*(int *)addr = n;
662df8bae1dSRodney W. Grimes 			break;
663df8bae1dSRodney W. Grimes 		}
664df8bae1dSRodney W. Grimes 
665df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
666df8bae1dSRodney W. Grimes 		{
667df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
668df8bae1dSRodney W. Grimes 
669df8bae1dSRodney W. Grimes 			if (d->bd_bif == 0)
670df8bae1dSRodney W. Grimes 				error = EINVAL;
671df8bae1dSRodney W. Grimes 			else {
672df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
673df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
674df8bae1dSRodney W. Grimes 			}
675df8bae1dSRodney W. Grimes 			break;
676df8bae1dSRodney W. Grimes 		}
677df8bae1dSRodney W. Grimes 
678df8bae1dSRodney W. Grimes 	/*
679df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
680df8bae1dSRodney W. Grimes 	 */
681df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
682df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
683df8bae1dSRodney W. Grimes 		break;
684df8bae1dSRodney W. Grimes 
685df8bae1dSRodney W. Grimes 	/*
686df8bae1dSRodney W. Grimes 	 * Set buffer length.
687df8bae1dSRodney W. Grimes 	 */
688df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
689df8bae1dSRodney W. Grimes 		if (d->bd_bif != 0)
690df8bae1dSRodney W. Grimes 			error = EINVAL;
691df8bae1dSRodney W. Grimes 		else {
692df8bae1dSRodney W. Grimes 			register u_int size = *(u_int *)addr;
693df8bae1dSRodney W. Grimes 
694eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
695eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
696df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
697df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
698df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
699df8bae1dSRodney W. Grimes 		}
700df8bae1dSRodney W. Grimes 		break;
701df8bae1dSRodney W. Grimes 
702df8bae1dSRodney W. Grimes 	/*
703df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
704df8bae1dSRodney W. Grimes 	 */
705df8bae1dSRodney W. Grimes 	case BIOCSETF:
706df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
707df8bae1dSRodney W. Grimes 		break;
708df8bae1dSRodney W. Grimes 
709df8bae1dSRodney W. Grimes 	/*
710df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
711df8bae1dSRodney W. Grimes 	 */
712df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
713e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
714df8bae1dSRodney W. Grimes 		reset_d(d);
715e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
716df8bae1dSRodney W. Grimes 		break;
717df8bae1dSRodney W. Grimes 
718df8bae1dSRodney W. Grimes 	/*
719df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
720df8bae1dSRodney W. Grimes 	 */
721df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
722df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0) {
723df8bae1dSRodney W. Grimes 			/*
724df8bae1dSRodney W. Grimes 			 * No interface attached yet.
725df8bae1dSRodney W. Grimes 			 */
726df8bae1dSRodney W. Grimes 			error = EINVAL;
727df8bae1dSRodney W. Grimes 			break;
728df8bae1dSRodney W. Grimes 		}
729df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
730e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
731df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
732e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
733df8bae1dSRodney W. Grimes 			if (error == 0)
734df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
735df8bae1dSRodney W. Grimes 		}
736df8bae1dSRodney W. Grimes 		break;
737df8bae1dSRodney W. Grimes 
738df8bae1dSRodney W. Grimes 	/*
739df8bae1dSRodney W. Grimes 	 * Get device parameters.
740df8bae1dSRodney W. Grimes 	 */
741df8bae1dSRodney W. Grimes 	case BIOCGDLT:
742df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
743df8bae1dSRodney W. Grimes 			error = EINVAL;
744df8bae1dSRodney W. Grimes 		else
745df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
746df8bae1dSRodney W. Grimes 		break;
747df8bae1dSRodney W. Grimes 
748df8bae1dSRodney W. Grimes 	/*
7491dd0feaaSArchie Cobbs 	 * Get interface name.
750df8bae1dSRodney W. Grimes 	 */
751df8bae1dSRodney W. Grimes 	case BIOCGETIF:
752df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
753df8bae1dSRodney W. Grimes 			error = EINVAL;
7541dd0feaaSArchie Cobbs 		else {
7551dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
7561dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
7571dd0feaaSArchie Cobbs 
7581dd0feaaSArchie Cobbs 			snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
7591dd0feaaSArchie Cobbs 			    "%s%d", ifp->if_name, ifp->if_unit);
7601dd0feaaSArchie Cobbs 		}
761df8bae1dSRodney W. Grimes 		break;
762df8bae1dSRodney W. Grimes 
763df8bae1dSRodney W. Grimes 	/*
764df8bae1dSRodney W. Grimes 	 * Set interface.
765df8bae1dSRodney W. Grimes 	 */
766df8bae1dSRodney W. Grimes 	case BIOCSETIF:
767df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
768df8bae1dSRodney W. Grimes 		break;
769df8bae1dSRodney W. Grimes 
770df8bae1dSRodney W. Grimes 	/*
771df8bae1dSRodney W. Grimes 	 * Set read timeout.
772df8bae1dSRodney W. Grimes 	 */
773df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
774df8bae1dSRodney W. Grimes 		{
775df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
776df8bae1dSRodney W. Grimes 
777bdc2cdc5SAlexander Langer 			/*
778bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
779bdc2cdc5SAlexander Langer 			 * a one-shot timer.
780bdc2cdc5SAlexander Langer 			 */
781bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
782bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
783df8bae1dSRodney W. Grimes 			break;
784df8bae1dSRodney W. Grimes 		}
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes 	/*
787df8bae1dSRodney W. Grimes 	 * Get read timeout.
788df8bae1dSRodney W. Grimes 	 */
789df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
790df8bae1dSRodney W. Grimes 		{
791df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
792df8bae1dSRodney W. Grimes 
793bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
794bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
795df8bae1dSRodney W. Grimes 			break;
796df8bae1dSRodney W. Grimes 		}
797df8bae1dSRodney W. Grimes 
798df8bae1dSRodney W. Grimes 	/*
799df8bae1dSRodney W. Grimes 	 * Get packet stats.
800df8bae1dSRodney W. Grimes 	 */
801df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
802df8bae1dSRodney W. Grimes 		{
803df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
806df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
807df8bae1dSRodney W. Grimes 			break;
808df8bae1dSRodney W. Grimes 		}
809df8bae1dSRodney W. Grimes 
810df8bae1dSRodney W. Grimes 	/*
811df8bae1dSRodney W. Grimes 	 * Set immediate mode.
812df8bae1dSRodney W. Grimes 	 */
813df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
814df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
815df8bae1dSRodney W. Grimes 		break;
816df8bae1dSRodney W. Grimes 
817df8bae1dSRodney W. Grimes 	case BIOCVERSION:
818df8bae1dSRodney W. Grimes 		{
819df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
820df8bae1dSRodney W. Grimes 
821df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
822df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
823df8bae1dSRodney W. Grimes 			break;
824df8bae1dSRodney W. Grimes 		}
82500a83887SPaul Traina 
826114ae644SMike Smith 	/*
827114ae644SMike Smith 	 * Get "header already complete" flag
828114ae644SMike Smith 	 */
829114ae644SMike Smith 	case BIOCGHDRCMPLT:
830114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
831114ae644SMike Smith 		break;
832114ae644SMike Smith 
833114ae644SMike Smith 	/*
834114ae644SMike Smith 	 * Set "header already complete" flag
835114ae644SMike Smith 	 */
836114ae644SMike Smith 	case BIOCSHDRCMPLT:
837114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
838114ae644SMike Smith 		break;
839114ae644SMike Smith 
8408ed3828cSRobert Watson 	/*
8418ed3828cSRobert Watson 	 * Get "see sent packets" flag
8428ed3828cSRobert Watson 	 */
8438ed3828cSRobert Watson 	case BIOCGSEESENT:
8448ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
8458ed3828cSRobert Watson 		break;
8468ed3828cSRobert Watson 
8478ed3828cSRobert Watson 	/*
8488ed3828cSRobert Watson 	 * Set "see sent packets" flag
8498ed3828cSRobert Watson 	 */
8508ed3828cSRobert Watson 	case BIOCSSEESENT:
8518ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
8528ed3828cSRobert Watson 		break;
8538ed3828cSRobert Watson 
85400a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
85500a83887SPaul Traina 		break;
85600a83887SPaul Traina 
85700a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
85800a83887SPaul Traina 		d->bd_async = *(int *)addr;
85900a83887SPaul Traina 		break;
86000a83887SPaul Traina 
861831d27a9SDon Lewis 	case FIOSETOWN:
862831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
86300a83887SPaul Traina 		break;
86400a83887SPaul Traina 
865831d27a9SDon Lewis 	case FIOGETOWN:
86691e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
867831d27a9SDon Lewis 		break;
868831d27a9SDon Lewis 
869831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
870831d27a9SDon Lewis 	case TIOCSPGRP:
871831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
872831d27a9SDon Lewis 		break;
873831d27a9SDon Lewis 
874831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
87500a83887SPaul Traina 	case TIOCGPGRP:
87691e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
87700a83887SPaul Traina 		break;
87800a83887SPaul Traina 
87900a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
88000a83887SPaul Traina 		{
88100a83887SPaul Traina 		 	u_int sig;
88200a83887SPaul Traina 
88300a83887SPaul Traina 			sig = *(u_int *)addr;
88400a83887SPaul Traina 
88500a83887SPaul Traina 			if (sig >= NSIG)
88600a83887SPaul Traina 				error = EINVAL;
88700a83887SPaul Traina 			else
88800a83887SPaul Traina 				d->bd_sig = sig;
88900a83887SPaul Traina 			break;
89000a83887SPaul Traina 		}
89100a83887SPaul Traina 	case BIOCGRSIG:
89200a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
89300a83887SPaul Traina 		break;
894df8bae1dSRodney W. Grimes 	}
895df8bae1dSRodney W. Grimes 	return (error);
896df8bae1dSRodney W. Grimes }
897df8bae1dSRodney W. Grimes 
898df8bae1dSRodney W. Grimes /*
899df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
900df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
901df8bae1dSRodney W. Grimes  */
902f708ef1bSPoul-Henning Kamp static int
903df8bae1dSRodney W. Grimes bpf_setf(d, fp)
904df8bae1dSRodney W. Grimes 	struct bpf_d *d;
905df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
906df8bae1dSRodney W. Grimes {
907df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
908df8bae1dSRodney W. Grimes 	u_int flen, size;
909df8bae1dSRodney W. Grimes 
910df8bae1dSRodney W. Grimes 	old = d->bd_filter;
911df8bae1dSRodney W. Grimes 	if (fp->bf_insns == 0) {
912df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
913df8bae1dSRodney W. Grimes 			return (EINVAL);
914e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
915df8bae1dSRodney W. Grimes 		d->bd_filter = 0;
916df8bae1dSRodney W. Grimes 		reset_d(d);
917e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
918df8bae1dSRodney W. Grimes 		if (old != 0)
919bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
920df8bae1dSRodney W. Grimes 		return (0);
921df8bae1dSRodney W. Grimes 	}
922df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
923df8bae1dSRodney W. Grimes 	if (flen > BPF_MAXINSNS)
924df8bae1dSRodney W. Grimes 		return (EINVAL);
925df8bae1dSRodney W. Grimes 
926df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
927bd3a5320SPoul-Henning Kamp 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
928df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
929df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
930e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
931df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
932df8bae1dSRodney W. Grimes 		reset_d(d);
933e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
934df8bae1dSRodney W. Grimes 		if (old != 0)
935bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 		return (0);
938df8bae1dSRodney W. Grimes 	}
939bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
940df8bae1dSRodney W. Grimes 	return (EINVAL);
941df8bae1dSRodney W. Grimes }
942df8bae1dSRodney W. Grimes 
943df8bae1dSRodney W. Grimes /*
944df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
945df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
946df8bae1dSRodney W. Grimes  * Return an errno or 0.
947df8bae1dSRodney W. Grimes  */
948df8bae1dSRodney W. Grimes static int
949df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
950df8bae1dSRodney W. Grimes 	struct bpf_d *d;
951df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
952df8bae1dSRodney W. Grimes {
953df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
954e7bb21b3SJonathan Lemon 	int error;
9559b44ff22SGarrett Wollman 	struct ifnet *theywant;
956df8bae1dSRodney W. Grimes 
9579b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
9589b44ff22SGarrett Wollman 	if (theywant == 0)
9599b44ff22SGarrett Wollman 		return ENXIO;
9609b44ff22SGarrett Wollman 
961df8bae1dSRodney W. Grimes 	/*
962df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
963df8bae1dSRodney W. Grimes 	 */
964e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
965df8bae1dSRodney W. Grimes 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
966df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
967df8bae1dSRodney W. Grimes 
9689b44ff22SGarrett Wollman 		if (ifp == 0 || ifp != theywant)
969df8bae1dSRodney W. Grimes 			continue;
97024a229f4SSam Leffler 		/* skip additional entry */
97124a229f4SSam Leffler 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
97224a229f4SSam Leffler 			continue;
973e7bb21b3SJonathan Lemon 
974e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
975df8bae1dSRodney W. Grimes 		/*
976df8bae1dSRodney W. Grimes 		 * We found the requested interface.
977df8bae1dSRodney W. Grimes 		 * If it's not up, return an error.
978df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
979df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
980df8bae1dSRodney W. Grimes 		 * just flush the buffer.
981df8bae1dSRodney W. Grimes 		 */
982df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_UP) == 0)
983df8bae1dSRodney W. Grimes 			return (ENETDOWN);
984df8bae1dSRodney W. Grimes 
985df8bae1dSRodney W. Grimes 		if (d->bd_sbuf == 0) {
986df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
987df8bae1dSRodney W. Grimes 			if (error != 0)
988df8bae1dSRodney W. Grimes 				return (error);
989df8bae1dSRodney W. Grimes 		}
990df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
991df8bae1dSRodney W. Grimes 			if (d->bd_bif)
992df8bae1dSRodney W. Grimes 				/*
993df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
994df8bae1dSRodney W. Grimes 				 */
995df8bae1dSRodney W. Grimes 				bpf_detachd(d);
996df8bae1dSRodney W. Grimes 
997df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
998df8bae1dSRodney W. Grimes 		}
999e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1000df8bae1dSRodney W. Grimes 		reset_d(d);
1001e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1002df8bae1dSRodney W. Grimes 		return (0);
1003df8bae1dSRodney W. Grimes 	}
1004e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1005df8bae1dSRodney W. Grimes 	/* Not found. */
1006df8bae1dSRodney W. Grimes 	return (ENXIO);
1007df8bae1dSRodney W. Grimes }
1008df8bae1dSRodney W. Grimes 
1009df8bae1dSRodney W. Grimes /*
1010243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1011df8bae1dSRodney W. Grimes  *
1012df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1013df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1014df8bae1dSRodney W. Grimes  */
101537c84183SPoul-Henning Kamp static int
1016b40ce416SJulian Elischer bpfpoll(dev, events, td)
1017df8bae1dSRodney W. Grimes 	register dev_t dev;
1018243ac7d8SPeter Wemm 	int events;
1019b40ce416SJulian Elischer 	struct thread *td;
1020df8bae1dSRodney W. Grimes {
1021e7bb21b3SJonathan Lemon 	struct bpf_d *d;
10220832fc64SGarance A Drosehn 	int revents;
1023df8bae1dSRodney W. Grimes 
1024bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1025de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1026de5d9935SRobert Watson 		return (ENXIO);
1027de5d9935SRobert Watson 
10280832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1029e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
103075c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
10310832fc64SGarance A Drosehn 		/*
10320832fc64SGarance A Drosehn 		 * An imitation of the FIONREAD ioctl code.
10330832fc64SGarance A Drosehn 		 * XXX not quite.  An exact imitation:
10340832fc64SGarance A Drosehn 		 *	if (d->b_slen != 0 ||
10350832fc64SGarance A Drosehn 		 *	    (d->bd_hbuf != NULL && d->bd_hlen != 0)
10360832fc64SGarance A Drosehn 		 */
103781bda851SJohn Polstra 		if (d->bd_hlen != 0 ||
103881bda851SJohn Polstra 		    ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
103981bda851SJohn Polstra 		    d->bd_slen != 0))
1040243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
104181bda851SJohn Polstra 		else {
1042ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
104381bda851SJohn Polstra 			/* Start the read timeout if necessary. */
104481bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
104581bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
104681bda851SJohn Polstra 				    bpf_timed_out, d);
104781bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
104881bda851SJohn Polstra 			}
104981bda851SJohn Polstra 		}
105075c13541SPoul-Henning Kamp 	}
1051e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1052243ac7d8SPeter Wemm 	return (revents);
1053df8bae1dSRodney W. Grimes }
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes /*
1056df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1057df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1058df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1059df8bae1dSRodney W. Grimes  * buffer.
1060df8bae1dSRodney W. Grimes  */
1061df8bae1dSRodney W. Grimes void
106224a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
106324a229f4SSam Leffler 	struct bpf_if *bp;
1064df8bae1dSRodney W. Grimes 	register u_char *pkt;
1065df8bae1dSRodney W. Grimes 	register u_int pktlen;
1066df8bae1dSRodney W. Grimes {
1067df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
1068df8bae1dSRodney W. Grimes 	register u_int slen;
1069e7bb21b3SJonathan Lemon 
1070e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1071df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1072e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1073df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1074df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1075ec272d87SRobert Watson 		if (slen != 0) {
1076ec272d87SRobert Watson #ifdef MAC
107724a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1078ec272d87SRobert Watson #endif
1079df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1080ec272d87SRobert Watson 		}
1081e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1082df8bae1dSRodney W. Grimes 	}
1083e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1084df8bae1dSRodney W. Grimes }
1085df8bae1dSRodney W. Grimes 
1086df8bae1dSRodney W. Grimes /*
1087df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1088df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1089df8bae1dSRodney W. Grimes  */
1090df8bae1dSRodney W. Grimes static void
1091df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1092df8bae1dSRodney W. Grimes 	const void *src_arg;
1093df8bae1dSRodney W. Grimes 	void *dst_arg;
10948bcbc7dfSAlexander Langer 	register size_t len;
1095df8bae1dSRodney W. Grimes {
1096df8bae1dSRodney W. Grimes 	register const struct mbuf *m;
1097df8bae1dSRodney W. Grimes 	register u_int count;
1098df8bae1dSRodney W. Grimes 	u_char *dst;
1099df8bae1dSRodney W. Grimes 
1100df8bae1dSRodney W. Grimes 	m = src_arg;
1101df8bae1dSRodney W. Grimes 	dst = dst_arg;
1102df8bae1dSRodney W. Grimes 	while (len > 0) {
1103df8bae1dSRodney W. Grimes 		if (m == 0)
1104df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1105df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
11060453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1107df8bae1dSRodney W. Grimes 		m = m->m_next;
1108df8bae1dSRodney W. Grimes 		dst += count;
1109df8bae1dSRodney W. Grimes 		len -= count;
1110df8bae1dSRodney W. Grimes 	}
1111df8bae1dSRodney W. Grimes }
1112df8bae1dSRodney W. Grimes 
1113df8bae1dSRodney W. Grimes /*
1114df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1115df8bae1dSRodney W. Grimes  */
1116df8bae1dSRodney W. Grimes void
111724a229f4SSam Leffler bpf_mtap(bp, m)
111824a229f4SSam Leffler 	struct bpf_if *bp;
1119df8bae1dSRodney W. Grimes 	struct mbuf *m;
1120df8bae1dSRodney W. Grimes {
1121df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1122df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1123df8bae1dSRodney W. Grimes 
1124f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
11257b831242SPoul-Henning Kamp 	if (pktlen == m->m_len) {
112624a229f4SSam Leffler 		bpf_tap(bp, mtod(m, u_char *), pktlen);
11277b831242SPoul-Henning Kamp 		return;
11287b831242SPoul-Henning Kamp 	}
1129df8bae1dSRodney W. Grimes 
1130e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1131df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
11328ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
11338ed3828cSRobert Watson 			continue;
1134e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1135df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1136df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1137df8bae1dSRodney W. Grimes 		if (slen != 0)
11380c7fb534SRobert Watson #ifdef MAC
113924a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
11400c7fb534SRobert Watson #endif
11410c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
11420c7fb534SRobert Watson 				    bpf_mcopy);
1143e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1144df8bae1dSRodney W. Grimes 	}
1145e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1146df8bae1dSRodney W. Grimes }
1147df8bae1dSRodney W. Grimes 
1148df8bae1dSRodney W. Grimes /*
1149df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
1150df8bae1dSRodney W. Grimes  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
1151df8bae1dSRodney W. Grimes  * otherwise 0.  "copy" is the routine called to do the actual data
1152df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1153df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1154df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1155df8bae1dSRodney W. Grimes  */
1156df8bae1dSRodney W. Grimes static void
1157df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
1158df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
1159df8bae1dSRodney W. Grimes 	register u_char *pkt;
1160df8bae1dSRodney W. Grimes 	register u_int pktlen, snaplen;
1161929ddbbbSAlfred Perlstein 	register void (*cpfn)(const void *, void *, size_t);
1162df8bae1dSRodney W. Grimes {
1163df8bae1dSRodney W. Grimes 	register struct bpf_hdr *hp;
1164df8bae1dSRodney W. Grimes 	register int totlen, curlen;
1165df8bae1dSRodney W. Grimes 	register int hdrlen = d->bd_bif->bif_hdrlen;
1166df8bae1dSRodney W. Grimes 	/*
1167df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1168df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1169df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1170df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1171df8bae1dSRodney W. Grimes 	 */
1172df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1173df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1174df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1175df8bae1dSRodney W. Grimes 
1176df8bae1dSRodney W. Grimes 	/*
1177df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1178df8bae1dSRodney W. Grimes 	 */
1179df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1180df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1181df8bae1dSRodney W. Grimes 		/*
1182df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1183df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1184df8bae1dSRodney W. Grimes 		 * pending reads.
1185df8bae1dSRodney W. Grimes 		 */
1186df8bae1dSRodney W. Grimes 		if (d->bd_fbuf == 0) {
1187df8bae1dSRodney W. Grimes 			/*
1188df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1189df8bae1dSRodney W. Grimes 			 * so drop the packet.
1190df8bae1dSRodney W. Grimes 			 */
1191df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1192df8bae1dSRodney W. Grimes 			return;
1193df8bae1dSRodney W. Grimes 		}
1194df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
1195df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1196df8bae1dSRodney W. Grimes 		curlen = 0;
1197df8bae1dSRodney W. Grimes 	}
119881bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1199df8bae1dSRodney W. Grimes 		/*
120081bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
120181bda851SJohn Polstra 		 * already expired during a select call.  A packet
120281bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1203df8bae1dSRodney W. Grimes 		 */
1204df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1205df8bae1dSRodney W. Grimes 
1206df8bae1dSRodney W. Grimes 	/*
1207df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1208df8bae1dSRodney W. Grimes 	 */
1209df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1210df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1211df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1212df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1213df8bae1dSRodney W. Grimes 	/*
1214df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1215df8bae1dSRodney W. Grimes 	 */
1216df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1217df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
1218df8bae1dSRodney W. Grimes }
1219df8bae1dSRodney W. Grimes 
1220df8bae1dSRodney W. Grimes /*
1221df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1222df8bae1dSRodney W. Grimes  */
1223df8bae1dSRodney W. Grimes static int
1224df8bae1dSRodney W. Grimes bpf_allocbufs(d)
1225df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
1226df8bae1dSRodney W. Grimes {
1227bd3a5320SPoul-Henning Kamp 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1228df8bae1dSRodney W. Grimes 	if (d->bd_fbuf == 0)
1229df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1230df8bae1dSRodney W. Grimes 
1231bd3a5320SPoul-Henning Kamp 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1232df8bae1dSRodney W. Grimes 	if (d->bd_sbuf == 0) {
1233bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1234df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1235df8bae1dSRodney W. Grimes 	}
1236df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1237df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1238df8bae1dSRodney W. Grimes 	return (0);
1239df8bae1dSRodney W. Grimes }
1240df8bae1dSRodney W. Grimes 
1241df8bae1dSRodney W. Grimes /*
1242df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1243df8bae1dSRodney W. Grimes  * Called on close.
1244df8bae1dSRodney W. Grimes  */
1245df8bae1dSRodney W. Grimes static void
1246df8bae1dSRodney W. Grimes bpf_freed(d)
1247df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
1248df8bae1dSRodney W. Grimes {
1249df8bae1dSRodney W. Grimes 	/*
1250df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1251df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1252df8bae1dSRodney W. Grimes 	 * free.
1253df8bae1dSRodney W. Grimes 	 */
1254df8bae1dSRodney W. Grimes 	if (d->bd_sbuf != 0) {
1255bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1256df8bae1dSRodney W. Grimes 		if (d->bd_hbuf != 0)
1257bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1258df8bae1dSRodney W. Grimes 		if (d->bd_fbuf != 0)
1259bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1260df8bae1dSRodney W. Grimes 	}
1261df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1262bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1263e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1264df8bae1dSRodney W. Grimes }
1265df8bae1dSRodney W. Grimes 
1266df8bae1dSRodney W. Grimes /*
126724a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
126824a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1269df8bae1dSRodney W. Grimes  */
1270df8bae1dSRodney W. Grimes void
12719b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1272df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1273df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1274df8bae1dSRodney W. Grimes {
127524a229f4SSam Leffler 
127624a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
127724a229f4SSam Leffler }
127824a229f4SSam Leffler 
127924a229f4SSam Leffler /*
128024a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
128124a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
128224a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
128324a229f4SSam Leffler  * headers are not yet supporrted).
128424a229f4SSam Leffler  */
128524a229f4SSam Leffler void
128624a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
128724a229f4SSam Leffler 	struct ifnet *ifp;
128824a229f4SSam Leffler 	u_int dlt, hdrlen;
128924a229f4SSam Leffler 	struct bpf_if **driverp;
129024a229f4SSam Leffler {
1291df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
12926a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1293df8bae1dSRodney W. Grimes 	if (bp == 0)
1294df8bae1dSRodney W. Grimes 		panic("bpfattach");
1295df8bae1dSRodney W. Grimes 
129624a229f4SSam Leffler 	bp->bif_dlist = 0;
129724a229f4SSam Leffler 	bp->bif_driverp = driverp;
1298df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1299df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
13006008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1301df8bae1dSRodney W. Grimes 
1302e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
1303df8bae1dSRodney W. Grimes 	bp->bif_next = bpf_iflist;
1304df8bae1dSRodney W. Grimes 	bpf_iflist = bp;
1305e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1306df8bae1dSRodney W. Grimes 
130724a229f4SSam Leffler 	*bp->bif_driverp = 0;
1308df8bae1dSRodney W. Grimes 
1309df8bae1dSRodney W. Grimes 	/*
1310df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1311df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1312df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1313df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1314df8bae1dSRodney W. Grimes 	 */
1315df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1316df8bae1dSRodney W. Grimes 
13172eeab939SGarrett Wollman 	if (bootverbose)
131824a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1319df8bae1dSRodney W. Grimes }
132053ac6efbSJulian Elischer 
1321de5d9935SRobert Watson /*
1322de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1323de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1324de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1325de5d9935SRobert Watson  * ENXIO.
1326de5d9935SRobert Watson  */
1327de5d9935SRobert Watson void
1328de5d9935SRobert Watson bpfdetach(ifp)
1329de5d9935SRobert Watson 	struct ifnet *ifp;
1330de5d9935SRobert Watson {
1331de5d9935SRobert Watson 	struct bpf_if	*bp, *bp_prev;
1332de5d9935SRobert Watson 	struct bpf_d	*d;
1333de5d9935SRobert Watson 
1334e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
1335de5d9935SRobert Watson 
1336de5d9935SRobert Watson 	/* Locate BPF interface information */
1337de5d9935SRobert Watson 	bp_prev = NULL;
1338de5d9935SRobert Watson 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1339de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1340de5d9935SRobert Watson 			break;
1341de5d9935SRobert Watson 		bp_prev = bp;
1342de5d9935SRobert Watson 	}
1343de5d9935SRobert Watson 
1344de5d9935SRobert Watson 	/* Interface wasn't attached */
1345de5d9935SRobert Watson 	if (bp->bif_ifp == NULL) {
1346e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1347de5d9935SRobert Watson 		printf("bpfdetach: %s%d was not attached\n", ifp->if_name,
1348de5d9935SRobert Watson 		    ifp->if_unit);
1349de5d9935SRobert Watson 		return;
1350de5d9935SRobert Watson 	}
1351de5d9935SRobert Watson 
1352de5d9935SRobert Watson 	if (bp_prev) {
1353de5d9935SRobert Watson 		bp_prev->bif_next = bp->bif_next;
1354de5d9935SRobert Watson 	} else {
1355de5d9935SRobert Watson 		bpf_iflist = bp->bif_next;
1356de5d9935SRobert Watson 	}
1357de5d9935SRobert Watson 
1358e7bb21b3SJonathan Lemon 	while ((d = bp->bif_dlist) != NULL) {
1359e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1360e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1361e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1362e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1363e7bb21b3SJonathan Lemon 	}
1364e7bb21b3SJonathan Lemon 
1365e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1366de5d9935SRobert Watson 	free(bp, M_BPF);
1367de5d9935SRobert Watson 
1368e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1369de5d9935SRobert Watson }
1370de5d9935SRobert Watson 
1371929ddbbbSAlfred Perlstein static void bpf_drvinit(void *unused);
1372bd3a5320SPoul-Henning Kamp 
1373929ddbbbSAlfred Perlstein static void bpf_clone(void *arg, char *name, int namelen, dev_t *dev);
13743f54a085SPoul-Henning Kamp 
13753f54a085SPoul-Henning Kamp static void
13763f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
13773f54a085SPoul-Henning Kamp 	void *arg;
13783f54a085SPoul-Henning Kamp 	char *name;
13793f54a085SPoul-Henning Kamp 	int namelen;
13803f54a085SPoul-Henning Kamp 	dev_t *dev;
13813f54a085SPoul-Henning Kamp {
13823f54a085SPoul-Henning Kamp 	int u;
13833f54a085SPoul-Henning Kamp 
13843f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
13853f54a085SPoul-Henning Kamp 		return;
1386db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
13873f54a085SPoul-Henning Kamp 		return;
1388b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1389b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1390b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
13913f54a085SPoul-Henning Kamp 	return;
13923f54a085SPoul-Henning Kamp }
13933f54a085SPoul-Henning Kamp 
1394514ede09SBruce Evans static void
1395514ede09SBruce Evans bpf_drvinit(unused)
1396514ede09SBruce Evans 	void *unused;
139753ac6efbSJulian Elischer {
139853ac6efbSJulian Elischer 
13996008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1400db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
14012447bec8SPoul-Henning Kamp 	cdevsw_add(&bpf_cdevsw);
14027198bf47SJulian Elischer }
140353ac6efbSJulian Elischer 
140453ac6efbSJulian Elischer SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
140553ac6efbSJulian Elischer 
14065bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1407f8dc4716SMike Smith /*
1408f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1409f8dc4716SMike Smith  *
1410f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1411f8dc4716SMike Smith  * to be loaded at runtime.
1412f8dc4716SMike Smith  */
1413f8dc4716SMike Smith 
1414f8dc4716SMike Smith void
1415f8dc4716SMike Smith bpf_tap(ifp, pkt, pktlen)
1416f8dc4716SMike Smith 	struct ifnet *ifp;
1417f8dc4716SMike Smith 	register u_char *pkt;
1418f8dc4716SMike Smith 	register u_int pktlen;
1419f8dc4716SMike Smith {
1420f8dc4716SMike Smith }
1421f8dc4716SMike Smith 
1422f8dc4716SMike Smith void
1423f8dc4716SMike Smith bpf_mtap(ifp, m)
1424f8dc4716SMike Smith 	struct ifnet *ifp;
1425f8dc4716SMike Smith 	struct mbuf *m;
1426f8dc4716SMike Smith {
1427f8dc4716SMike Smith }
1428f8dc4716SMike Smith 
1429f8dc4716SMike Smith void
1430f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1431f8dc4716SMike Smith 	struct ifnet *ifp;
1432f8dc4716SMike Smith 	u_int dlt, hdrlen;
1433f8dc4716SMike Smith {
1434f8dc4716SMike Smith }
1435f8dc4716SMike Smith 
1436da626c17SBill Paul void
1437da626c17SBill Paul bpfdetach(ifp)
1438da626c17SBill Paul 	struct ifnet *ifp;
1439da626c17SBill Paul {
1440da626c17SBill Paul }
1441da626c17SBill Paul 
1442f8dc4716SMike Smith u_int
1443f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
14441f8ffa4bSJulian Elischer 	register const struct bpf_insn *pc;
1445f8dc4716SMike Smith 	register u_char *p;
1446f8dc4716SMike Smith 	u_int wirelen;
1447f8dc4716SMike Smith 	register u_int buflen;
1448f8dc4716SMike Smith {
1449f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1450f8dc4716SMike Smith }
1451f8dc4716SMike Smith 
14525bb5f2c9SPeter Wemm int
14535bb5f2c9SPeter Wemm bpf_validate(f, len)
14545bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
14555bb5f2c9SPeter Wemm 	int len;
14565bb5f2c9SPeter Wemm {
14575bb5f2c9SPeter Wemm 	return 0;		/* false */
14585bb5f2c9SPeter Wemm }
14595bb5f2c9SPeter Wemm 
14605bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1461