xref: /freebsd/sys/net/bpf.c (revision a3272e3ce3a0a7cb4b6b567a110da545fb6a0524)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from the Stanford/CMU enet packet filter,
6df8bae1dSRodney W. Grimes  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7df8bae1dSRodney W. Grimes  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8df8bae1dSRodney W. Grimes  * Berkeley Laboratory.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
344f252c4dSRuslan Ermilov  *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
35df8bae1dSRodney W. Grimes  *
36c3aac50fSPeter Wemm  * $FreeBSD$
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
395bb5f2c9SPeter Wemm #include "opt_bpf.h"
4082f4445dSRobert Watson #include "opt_mac.h"
415bb5f2c9SPeter Wemm #include "opt_netgraph.h"
42df8bae1dSRodney W. Grimes 
4395aab9ccSJohn-Mark Gurney #include <sys/types.h>
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46ce7609a4SBruce Evans #include <sys/conf.h>
47e76eee55SPoul-Henning Kamp #include <sys/fcntl.h>
4882f4445dSRobert Watson #include <sys/mac.h>
494d1d4912SBruce Evans #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
51df8bae1dSRodney W. Grimes #include <sys/time.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
530310c19fSBruce Evans #include <sys/signalvar.h>
54528f627fSBruce Evans #include <sys/filio.h>
55528f627fSBruce Evans #include <sys/sockio.h>
56528f627fSBruce Evans #include <sys/ttycom.h>
57e76eee55SPoul-Henning Kamp #include <sys/uio.h>
58df8bae1dSRodney W. Grimes 
5995aab9ccSJohn-Mark Gurney #include <sys/event.h>
6095aab9ccSJohn-Mark Gurney #include <sys/file.h>
61243ac7d8SPeter Wemm #include <sys/poll.h>
6295aab9ccSJohn-Mark Gurney #include <sys/proc.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <sys/socket.h>
65df8bae1dSRodney W. Grimes 
66fba9235dSBruce Evans #include <net/if.h>
67df8bae1dSRodney W. Grimes #include <net/bpf.h>
68df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <netinet/in.h>
71df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
72df8bae1dSRodney W. Grimes #include <sys/kernel.h>
73f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
747b778b5eSEivind Eklund 
75959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
7687f6c662SJulian Elischer 
775bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
7853ac6efbSJulian Elischer 
79df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes /*
82df8bae1dSRodney W. Grimes  * The default read buffer size is patchable.
83df8bae1dSRodney W. Grimes  */
84a3272e3cSChristian S.J. Peron SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
85e7bb21b3SJonathan Lemon static int bpf_bufsize = 4096;
86a3272e3cSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
87f708ef1bSPoul-Henning Kamp     &bpf_bufsize, 0, "");
88eba2a1aeSPoul-Henning Kamp static int bpf_maxbufsize = BPF_MAXBUFSIZE;
89a3272e3cSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
90eba2a1aeSPoul-Henning Kamp     &bpf_maxbufsize, 0, "");
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes /*
93d1a67300SRobert Watson  * bpf_iflist is a list of BPF interface structures, each corresponding to a
94d1a67300SRobert Watson  * specific DLT.  The same network interface might have several BPF interface
95d1a67300SRobert Watson  * structures registered by different layers in the stack (i.e., 802.11
96d1a67300SRobert Watson  * frames, ethernet frames, etc).
97df8bae1dSRodney W. Grimes  */
984a3feeaaSRobert Watson static LIST_HEAD(, bpf_if)	bpf_iflist;
99e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
100df8bae1dSRodney W. Grimes 
101929ddbbbSAlfred Perlstein static int	bpf_allocbufs(struct bpf_d *);
102929ddbbbSAlfred Perlstein static void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
103929ddbbbSAlfred Perlstein static void	bpf_detachd(struct bpf_d *d);
104929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
105929ddbbbSAlfred Perlstein static void	bpf_mcopy(const void *, void *, size_t);
106929ddbbbSAlfred Perlstein static int	bpf_movein(struct uio *, int,
107929ddbbbSAlfred Perlstein 		    struct mbuf **, struct sockaddr *, int *);
108929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
109929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
110e7bb21b3SJonathan Lemon static __inline void
111929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
112929ddbbbSAlfred Perlstein static void	catchpacket(struct bpf_d *, u_char *, u_int,
113929ddbbbSAlfred Perlstein 		    u_int, void (*)(const void *, void *, size_t));
114929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
115929ddbbbSAlfred Perlstein static int	 bpf_setf(struct bpf_d *, struct bpf_program *);
1168eab61f3SSam Leffler static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
1178eab61f3SSam Leffler static int	bpf_setdlt(struct bpf_d *, u_int);
11895aab9ccSJohn-Mark Gurney static void	filt_bpfdetach(struct knote *);
11995aab9ccSJohn-Mark Gurney static int	filt_bpfread(struct knote *, long);
120a3272e3cSChristian S.J. Peron static void	bpf_drvinit(void *);
121a3272e3cSChristian S.J. Peron static void	bpf_clone(void *, char *, int, struct cdev **);
122df8bae1dSRodney W. Grimes 
12387f6c662SJulian Elischer static	d_open_t	bpfopen;
12487f6c662SJulian Elischer static	d_close_t	bpfclose;
12587f6c662SJulian Elischer static	d_read_t	bpfread;
12687f6c662SJulian Elischer static	d_write_t	bpfwrite;
12787f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
128243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
12995aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
13087f6c662SJulian Elischer 
1314e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
132dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
133dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
1347ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1357ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1367ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1377ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1387ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1397ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1407ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
14195aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1424e2f199eSPoul-Henning Kamp };
14387f6c662SJulian Elischer 
14495aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
14595aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
14687f6c662SJulian Elischer 
147df8bae1dSRodney W. Grimes static int
148df8bae1dSRodney W. Grimes bpf_movein(uio, linktype, mp, sockp, datlen)
1498994a245SDag-Erling Smørgrav 	struct uio *uio;
150df8bae1dSRodney W. Grimes 	int linktype, *datlen;
1518994a245SDag-Erling Smørgrav 	struct mbuf **mp;
1528994a245SDag-Erling Smørgrav 	struct sockaddr *sockp;
153df8bae1dSRodney W. Grimes {
154df8bae1dSRodney W. Grimes 	struct mbuf *m;
155df8bae1dSRodney W. Grimes 	int error;
156df8bae1dSRodney W. Grimes 	int len;
157df8bae1dSRodney W. Grimes 	int hlen;
158df8bae1dSRodney W. Grimes 
159df8bae1dSRodney W. Grimes 	/*
160df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
161df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
162df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
163df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
164df8bae1dSRodney W. Grimes 	 * is forwarded as is.
165df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
166df8bae1dSRodney W. Grimes 	 * for the link level header.
167df8bae1dSRodney W. Grimes 	 */
168df8bae1dSRodney W. Grimes 	switch (linktype) {
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes 	case DLT_SLIP:
171df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
172df8bae1dSRodney W. Grimes 		hlen = 0;
173df8bae1dSRodney W. Grimes 		break;
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
176df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
177df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
178797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
179df8bae1dSRodney W. Grimes 		break;
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes 	case DLT_FDDI:
182d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
183d41f24e7SDavid Greenman 		hlen = 0;
184df8bae1dSRodney W. Grimes 		break;
185df8bae1dSRodney W. Grimes 
18622f05c43SAndrey A. Chernov 	case DLT_RAW:
187df8bae1dSRodney W. Grimes 	case DLT_NULL:
188df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
189df8bae1dSRodney W. Grimes 		hlen = 0;
190df8bae1dSRodney W. Grimes 		break;
191df8bae1dSRodney W. Grimes 
1924f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
1934f53e3ccSKenjiro Cho 		/*
1944f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
1954f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
1964f53e3ccSKenjiro Cho 		 * specified anyway.
1974f53e3ccSKenjiro Cho 		 */
1984f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
1994f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
2004f53e3ccSKenjiro Cho 		break;
2014f53e3ccSKenjiro Cho 
20230fa52a6SBrian Somers 	case DLT_PPP:
20330fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
20430fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
20530fa52a6SBrian Somers 		break;
20630fa52a6SBrian Somers 
207df8bae1dSRodney W. Grimes 	default:
208df8bae1dSRodney W. Grimes 		return (EIO);
209df8bae1dSRodney W. Grimes 	}
210df8bae1dSRodney W. Grimes 
211df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
212df8bae1dSRodney W. Grimes 	*datlen = len - hlen;
213df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
214df8bae1dSRodney W. Grimes 		return (EIO);
215df8bae1dSRodney W. Grimes 
216963e4c2aSGarrett Wollman 	if (len > MHLEN) {
217a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
21824a229f4SSam Leffler 	} else {
219a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
220df8bae1dSRodney W. Grimes 	}
22124a229f4SSam Leffler 	if (m == NULL)
22224a229f4SSam Leffler 		return (ENOBUFS);
223963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
224963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
225df8bae1dSRodney W. Grimes 	*mp = m;
22624a229f4SSam Leffler 
227df8bae1dSRodney W. Grimes 	/*
228df8bae1dSRodney W. Grimes 	 * Make room for link header.
229df8bae1dSRodney W. Grimes 	 */
230df8bae1dSRodney W. Grimes 	if (hlen != 0) {
2314f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
232df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
233df8bae1dSRodney W. Grimes #if BSD >= 199103
234df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
235df8bae1dSRodney W. Grimes #else
236df8bae1dSRodney W. Grimes 		m->m_off += hlen;
237df8bae1dSRodney W. Grimes #endif
238c9524588SDag-Erling Smørgrav 		error = uiomove(sockp->sa_data, hlen, uio);
239df8bae1dSRodney W. Grimes 		if (error)
240df8bae1dSRodney W. Grimes 			goto bad;
241df8bae1dSRodney W. Grimes 	}
242c9524588SDag-Erling Smørgrav 	error = uiomove(mtod(m, void *), len - hlen, uio);
243df8bae1dSRodney W. Grimes 	if (!error)
244df8bae1dSRodney W. Grimes 		return (0);
245df8bae1dSRodney W. Grimes bad:
246df8bae1dSRodney W. Grimes 	m_freem(m);
247df8bae1dSRodney W. Grimes 	return (error);
248df8bae1dSRodney W. Grimes }
249df8bae1dSRodney W. Grimes 
250df8bae1dSRodney W. Grimes /*
251df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
252df8bae1dSRodney W. Grimes  */
253df8bae1dSRodney W. Grimes static void
254df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
255df8bae1dSRodney W. Grimes 	struct bpf_d *d;
256df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
257df8bae1dSRodney W. Grimes {
258df8bae1dSRodney W. Grimes 	/*
259df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
260df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
261df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
262df8bae1dSRodney W. Grimes 	 */
263e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
264df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
2654a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
266df8bae1dSRodney W. Grimes 
26724a229f4SSam Leffler 	*bp->bif_driverp = bp;
268e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
269df8bae1dSRodney W. Grimes }
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes /*
272df8bae1dSRodney W. Grimes  * Detach a file from its interface.
273df8bae1dSRodney W. Grimes  */
274df8bae1dSRodney W. Grimes static void
275df8bae1dSRodney W. Grimes bpf_detachd(d)
276df8bae1dSRodney W. Grimes 	struct bpf_d *d;
277df8bae1dSRodney W. Grimes {
2786e891d64SPoul-Henning Kamp 	int error;
279df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
28046448b5aSRobert Watson 	struct ifnet *ifp;
281df8bae1dSRodney W. Grimes 
282df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
28346448b5aSRobert Watson 	BPFIF_LOCK(bp);
28446448b5aSRobert Watson 	BPFD_LOCK(d);
28546448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
28646448b5aSRobert Watson 
28746448b5aSRobert Watson 	/*
28846448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
28946448b5aSRobert Watson 	 */
29046448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
29146448b5aSRobert Watson 
29246448b5aSRobert Watson 	/*
29346448b5aSRobert Watson 	 * Let the driver know that there are no more listeners.
29446448b5aSRobert Watson 	 */
29546448b5aSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
29646448b5aSRobert Watson 		*bp->bif_driverp = NULL;
29746448b5aSRobert Watson 
298572bde2aSRobert Watson 	d->bd_bif = NULL;
29946448b5aSRobert Watson 	BPFD_UNLOCK(d);
30046448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
30146448b5aSRobert Watson 
302df8bae1dSRodney W. Grimes 	/*
303df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
304df8bae1dSRodney W. Grimes 	 * If so, turn it off.
305df8bae1dSRodney W. Grimes 	 */
306df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
307df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
30846448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
3096e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
310df8bae1dSRodney W. Grimes 			/*
3116e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
312df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
313df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
314df8bae1dSRodney W. Grimes 			 * take it out.
315df8bae1dSRodney W. Grimes 			 */
3168eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
3178eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
3186e891d64SPoul-Henning Kamp 		}
319df8bae1dSRodney W. Grimes 	}
320df8bae1dSRodney W. Grimes }
321df8bae1dSRodney W. Grimes 
322df8bae1dSRodney W. Grimes /*
323df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
324df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
325df8bae1dSRodney W. Grimes  */
326df8bae1dSRodney W. Grimes /* ARGSUSED */
32787f6c662SJulian Elischer static	int
328b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
32989c9c53dSPoul-Henning Kamp 	struct cdev *dev;
33060039670SBruce Evans 	int flags;
33160039670SBruce Evans 	int fmt;
332b40ce416SJulian Elischer 	struct thread *td;
333df8bae1dSRodney W. Grimes {
334e7bb21b3SJonathan Lemon 	struct bpf_d *d;
335df8bae1dSRodney W. Grimes 
336e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
337bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
338df8bae1dSRodney W. Grimes 	/*
339df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
340df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
341df8bae1dSRodney W. Grimes 	 */
342d17d8184SRobert Watson 	if (d != NULL) {
343e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
344df8bae1dSRodney W. Grimes 		return (EBUSY);
345e7bb21b3SJonathan Lemon 	}
346e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
347e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
348e7bb21b3SJonathan Lemon 
349d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
350b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
351b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
352a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
353bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
354df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
35500a83887SPaul Traina 	d->bd_sig = SIGIO;
3568ed3828cSRobert Watson 	d->bd_seesent = 1;
35782f4445dSRobert Watson #ifdef MAC
35882f4445dSRobert Watson 	mac_init_bpfdesc(d);
35982f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
36082f4445dSRobert Watson #endif
3616008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
36231199c84SGleb Smirnoff 	callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE);
363ad3b9257SJohn-Mark Gurney 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx);
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes 	return (0);
366df8bae1dSRodney W. Grimes }
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes /*
369df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
370df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
371df8bae1dSRodney W. Grimes  */
372df8bae1dSRodney W. Grimes /* ARGSUSED */
37387f6c662SJulian Elischer static	int
374b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
37589c9c53dSPoul-Henning Kamp 	struct cdev *dev;
37660039670SBruce Evans 	int flags;
37760039670SBruce Evans 	int fmt;
378b40ce416SJulian Elischer 	struct thread *td;
379df8bae1dSRodney W. Grimes {
380e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
381df8bae1dSRodney W. Grimes 
38281bda851SJohn Polstra 	BPFD_LOCK(d);
38381bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
38481bda851SJohn Polstra 		callout_stop(&d->bd_callout);
38581bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
38681bda851SJohn Polstra 	BPFD_UNLOCK(d);
387e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
388e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
389df8bae1dSRodney W. Grimes 	if (d->bd_bif)
390df8bae1dSRodney W. Grimes 		bpf_detachd(d);
391e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
3924549709fSBrian Feldman 	selwakeuppri(&d->bd_sel, PRINET);
39382f4445dSRobert Watson #ifdef MAC
39482f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
39582f4445dSRobert Watson #endif /* MAC */
396ad3b9257SJohn-Mark Gurney 	knlist_destroy(&d->bd_sel.si_note);
397df8bae1dSRodney W. Grimes 	bpf_freed(d);
398d17d8184SRobert Watson 	dev->si_drv1 = NULL;
399d722be54SLuigi Rizzo 	free(d, M_BPF);
400df8bae1dSRodney W. Grimes 
401df8bae1dSRodney W. Grimes 	return (0);
402df8bae1dSRodney W. Grimes }
403df8bae1dSRodney W. Grimes 
404df8bae1dSRodney W. Grimes 
405df8bae1dSRodney W. Grimes /*
406df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
407df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
408df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
409df8bae1dSRodney W. Grimes  */
410df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
411df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
412df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
413df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
414df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
415d17d8184SRobert Watson 	(d)->bd_fbuf = NULL;
416df8bae1dSRodney W. Grimes /*
417df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
418df8bae1dSRodney W. Grimes  */
41987f6c662SJulian Elischer static	int
42060039670SBruce Evans bpfread(dev, uio, ioflag)
42189c9c53dSPoul-Henning Kamp 	struct cdev *dev;
4228994a245SDag-Erling Smørgrav 	struct uio *uio;
42360039670SBruce Evans 	int ioflag;
424df8bae1dSRodney W. Grimes {
425e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
42681bda851SJohn Polstra 	int timed_out;
427df8bae1dSRodney W. Grimes 	int error;
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes 	/*
430df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
431df8bae1dSRodney W. Grimes 	 * as kernel buffers.
432df8bae1dSRodney W. Grimes 	 */
433df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
434df8bae1dSRodney W. Grimes 		return (EINVAL);
435df8bae1dSRodney W. Grimes 
436e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
43781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
43881bda851SJohn Polstra 		callout_stop(&d->bd_callout);
43981bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
44081bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
441df8bae1dSRodney W. Grimes 	/*
442df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
443df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
444df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
445df8bae1dSRodney W. Grimes 	 */
446572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
44781bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
448df8bae1dSRodney W. Grimes 			/*
449df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
450df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
451df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
452df8bae1dSRodney W. Grimes 			 */
453df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
454df8bae1dSRodney W. Grimes 			break;
455df8bae1dSRodney W. Grimes 		}
456de5d9935SRobert Watson 
457de5d9935SRobert Watson 		/*
458de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
459de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
460de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
461de5d9935SRobert Watson 		 * it before using it again.
462de5d9935SRobert Watson 		 */
463de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
464e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
465de5d9935SRobert Watson 			return (ENXIO);
466de5d9935SRobert Watson 		}
467de5d9935SRobert Watson 
468e76eee55SPoul-Henning Kamp 		if (ioflag & O_NONBLOCK) {
469e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
470fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
471fba3cfdeSJohn Polstra 		}
472521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
473e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
474df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
475e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
476df8bae1dSRodney W. Grimes 			return (error);
477df8bae1dSRodney W. Grimes 		}
478df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
479df8bae1dSRodney W. Grimes 			/*
480df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
481df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
482df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
483df8bae1dSRodney W. Grimes 			 */
484df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
485df8bae1dSRodney W. Grimes 				/*
486df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
487df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
488df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
489df8bae1dSRodney W. Grimes 				 */
490df8bae1dSRodney W. Grimes 				break;
491df8bae1dSRodney W. Grimes 
492df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
493e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
494df8bae1dSRodney W. Grimes 				return (0);
495df8bae1dSRodney W. Grimes 			}
496df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
497df8bae1dSRodney W. Grimes 			break;
498df8bae1dSRodney W. Grimes 		}
499df8bae1dSRodney W. Grimes 	}
500df8bae1dSRodney W. Grimes 	/*
501df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
502df8bae1dSRodney W. Grimes 	 */
503e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes 	/*
506df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
507df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
508df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
509df8bae1dSRodney W. Grimes 	 */
510e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
511df8bae1dSRodney W. Grimes 
512e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
513df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
514572bde2aSRobert Watson 	d->bd_hbuf = NULL;
515df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
516e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
517df8bae1dSRodney W. Grimes 
518df8bae1dSRodney W. Grimes 	return (error);
519df8bae1dSRodney W. Grimes }
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 
522df8bae1dSRodney W. Grimes /*
523df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
524df8bae1dSRodney W. Grimes  */
525e7bb21b3SJonathan Lemon static __inline void
526df8bae1dSRodney W. Grimes bpf_wakeup(d)
5278994a245SDag-Erling Smørgrav 	struct bpf_d *d;
528df8bae1dSRodney W. Grimes {
529a3272e3cSChristian S.J. Peron 
530a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
53181bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
53281bda851SJohn Polstra 		callout_stop(&d->bd_callout);
53381bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
53481bda851SJohn Polstra 	}
535521f364bSDag-Erling Smørgrav 	wakeup(d);
536831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
537f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
53800a83887SPaul Traina 
539512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
540ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
541df8bae1dSRodney W. Grimes }
542df8bae1dSRodney W. Grimes 
54381bda851SJohn Polstra static void
54481bda851SJohn Polstra bpf_timed_out(arg)
54581bda851SJohn Polstra 	void *arg;
54681bda851SJohn Polstra {
54781bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
54881bda851SJohn Polstra 
54981bda851SJohn Polstra 	BPFD_LOCK(d);
55081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
55181bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
55281bda851SJohn Polstra 		if (d->bd_slen != 0)
55381bda851SJohn Polstra 			bpf_wakeup(d);
55481bda851SJohn Polstra 	}
55581bda851SJohn Polstra 	BPFD_UNLOCK(d);
55681bda851SJohn Polstra }
55781bda851SJohn Polstra 
55887f6c662SJulian Elischer static	int
55960039670SBruce Evans bpfwrite(dev, uio, ioflag)
56089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
561df8bae1dSRodney W. Grimes 	struct uio *uio;
56260039670SBruce Evans 	int ioflag;
563df8bae1dSRodney W. Grimes {
564e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
565df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
566df8bae1dSRodney W. Grimes 	struct mbuf *m;
567e7bb21b3SJonathan Lemon 	int error;
5688240bf1eSRobert Watson 	struct sockaddr dst;
569df8bae1dSRodney W. Grimes 	int datlen;
570df8bae1dSRodney W. Grimes 
571572bde2aSRobert Watson 	if (d->bd_bif == NULL)
572df8bae1dSRodney W. Grimes 		return (ENXIO);
573df8bae1dSRodney W. Grimes 
574df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
575df8bae1dSRodney W. Grimes 
5763518d220SSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0)
5773518d220SSam Leffler 		return (ENETDOWN);
5783518d220SSam Leffler 
579df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
580df8bae1dSRodney W. Grimes 		return (0);
581df8bae1dSRodney W. Grimes 
5828240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
583df8bae1dSRodney W. Grimes 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
584df8bae1dSRodney W. Grimes 	if (error)
585df8bae1dSRodney W. Grimes 		return (error);
586df8bae1dSRodney W. Grimes 
58777fc70c1SPawel Jakub Dawidek 	if (datlen > ifp->if_mtu) {
58877fc70c1SPawel Jakub Dawidek 		m_freem(m);
589df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
59077fc70c1SPawel Jakub Dawidek 	}
591df8bae1dSRodney W. Grimes 
592114ae644SMike Smith 	if (d->bd_hdrcmplt)
593114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
594114ae644SMike Smith 
59582f4445dSRobert Watson #ifdef MAC
596f747d2ddSRobert Watson 	BPFD_LOCK(d);
59782f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
598f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
59982f4445dSRobert Watson #endif
600b8f9429dSRobert Watson 	NET_LOCK_GIANT();
601572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
602b8f9429dSRobert Watson 	NET_UNLOCK_GIANT();
603df8bae1dSRodney W. Grimes 	/*
604df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
605df8bae1dSRodney W. Grimes 	 */
606df8bae1dSRodney W. Grimes 	return (error);
607df8bae1dSRodney W. Grimes }
608df8bae1dSRodney W. Grimes 
609df8bae1dSRodney W. Grimes /*
610df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
611e7bb21b3SJonathan Lemon  * receive and drop counts.
612df8bae1dSRodney W. Grimes  */
613df8bae1dSRodney W. Grimes static void
614df8bae1dSRodney W. Grimes reset_d(d)
615df8bae1dSRodney W. Grimes 	struct bpf_d *d;
616df8bae1dSRodney W. Grimes {
617e7bb21b3SJonathan Lemon 
618e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
619df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
620df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
621df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
622572bde2aSRobert Watson 		d->bd_hbuf = NULL;
623df8bae1dSRodney W. Grimes 	}
624df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
625df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
626df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
627df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
628df8bae1dSRodney W. Grimes }
629df8bae1dSRodney W. Grimes 
630df8bae1dSRodney W. Grimes /*
631df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
632df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
633df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
634df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
635df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
636df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
637df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
638df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
639df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
640df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
641df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
642df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
643df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
644df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
645114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
646114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6478ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6488ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
649df8bae1dSRodney W. Grimes  */
650df8bae1dSRodney W. Grimes /* ARGSUSED */
65187f6c662SJulian Elischer static	int
652b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
65389c9c53dSPoul-Henning Kamp 	struct cdev *dev;
654ecbb00a2SDoug Rabson 	u_long cmd;
655df8bae1dSRodney W. Grimes 	caddr_t addr;
65660039670SBruce Evans 	int flags;
657b40ce416SJulian Elischer 	struct thread *td;
658df8bae1dSRodney W. Grimes {
659e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
660e7bb21b3SJonathan Lemon 	int error = 0;
661df8bae1dSRodney W. Grimes 
66281bda851SJohn Polstra 	BPFD_LOCK(d);
66381bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
66481bda851SJohn Polstra 		callout_stop(&d->bd_callout);
66581bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
66681bda851SJohn Polstra 	BPFD_UNLOCK(d);
66781bda851SJohn Polstra 
668df8bae1dSRodney W. Grimes 	switch (cmd) {
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 	default:
671df8bae1dSRodney W. Grimes 		error = EINVAL;
672df8bae1dSRodney W. Grimes 		break;
673df8bae1dSRodney W. Grimes 
674df8bae1dSRodney W. Grimes 	/*
675df8bae1dSRodney W. Grimes 	 * Check for read packet available.
676df8bae1dSRodney W. Grimes 	 */
677df8bae1dSRodney W. Grimes 	case FIONREAD:
678df8bae1dSRodney W. Grimes 		{
679df8bae1dSRodney W. Grimes 			int n;
680df8bae1dSRodney W. Grimes 
681e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
682df8bae1dSRodney W. Grimes 			n = d->bd_slen;
683df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
684df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
685e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
686df8bae1dSRodney W. Grimes 
687df8bae1dSRodney W. Grimes 			*(int *)addr = n;
688df8bae1dSRodney W. Grimes 			break;
689df8bae1dSRodney W. Grimes 		}
690df8bae1dSRodney W. Grimes 
691df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
692df8bae1dSRodney W. Grimes 		{
693df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
694df8bae1dSRodney W. Grimes 
695572bde2aSRobert Watson 			if (d->bd_bif == NULL)
696df8bae1dSRodney W. Grimes 				error = EINVAL;
697df8bae1dSRodney W. Grimes 			else {
698df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
699df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
700df8bae1dSRodney W. Grimes 			}
701df8bae1dSRodney W. Grimes 			break;
702df8bae1dSRodney W. Grimes 		}
703df8bae1dSRodney W. Grimes 
704df8bae1dSRodney W. Grimes 	/*
705df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
706df8bae1dSRodney W. Grimes 	 */
707df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
708df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
709df8bae1dSRodney W. Grimes 		break;
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes 	/*
712df8bae1dSRodney W. Grimes 	 * Set buffer length.
713df8bae1dSRodney W. Grimes 	 */
714df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
715572bde2aSRobert Watson 		if (d->bd_bif != NULL)
716df8bae1dSRodney W. Grimes 			error = EINVAL;
717df8bae1dSRodney W. Grimes 		else {
7188994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
719df8bae1dSRodney W. Grimes 
720eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
721eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
722df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
723df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
724df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
725df8bae1dSRodney W. Grimes 		}
726df8bae1dSRodney W. Grimes 		break;
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes 	/*
729df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
730df8bae1dSRodney W. Grimes 	 */
731df8bae1dSRodney W. Grimes 	case BIOCSETF:
732df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
733df8bae1dSRodney W. Grimes 		break;
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes 	/*
736df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
737df8bae1dSRodney W. Grimes 	 */
738df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
739e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
740df8bae1dSRodney W. Grimes 		reset_d(d);
741e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
742df8bae1dSRodney W. Grimes 		break;
743df8bae1dSRodney W. Grimes 
744df8bae1dSRodney W. Grimes 	/*
745df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
746df8bae1dSRodney W. Grimes 	 */
747df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
748572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
749df8bae1dSRodney W. Grimes 			/*
750df8bae1dSRodney W. Grimes 			 * No interface attached yet.
751df8bae1dSRodney W. Grimes 			 */
752df8bae1dSRodney W. Grimes 			error = EINVAL;
753df8bae1dSRodney W. Grimes 			break;
754df8bae1dSRodney W. Grimes 		}
755df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
756e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
757df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
758e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
759df8bae1dSRodney W. Grimes 			if (error == 0)
760df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
761df8bae1dSRodney W. Grimes 		}
762df8bae1dSRodney W. Grimes 		break;
763df8bae1dSRodney W. Grimes 
764df8bae1dSRodney W. Grimes 	/*
7658eab61f3SSam Leffler 	 * Get current data link type.
766df8bae1dSRodney W. Grimes 	 */
767df8bae1dSRodney W. Grimes 	case BIOCGDLT:
768572bde2aSRobert Watson 		if (d->bd_bif == NULL)
769df8bae1dSRodney W. Grimes 			error = EINVAL;
770df8bae1dSRodney W. Grimes 		else
771df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
772df8bae1dSRodney W. Grimes 		break;
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes 	/*
7758eab61f3SSam Leffler 	 * Get a list of supported data link types.
7768eab61f3SSam Leffler 	 */
7778eab61f3SSam Leffler 	case BIOCGDLTLIST:
778572bde2aSRobert Watson 		if (d->bd_bif == NULL)
7798eab61f3SSam Leffler 			error = EINVAL;
7808eab61f3SSam Leffler 		else
7818eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
7828eab61f3SSam Leffler 		break;
7838eab61f3SSam Leffler 
7848eab61f3SSam Leffler 	/*
7858eab61f3SSam Leffler 	 * Set data link type.
7868eab61f3SSam Leffler 	 */
7878eab61f3SSam Leffler 	case BIOCSDLT:
788572bde2aSRobert Watson 		if (d->bd_bif == NULL)
7898eab61f3SSam Leffler 			error = EINVAL;
7908eab61f3SSam Leffler 		else
7918eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
7928eab61f3SSam Leffler 		break;
7938eab61f3SSam Leffler 
7948eab61f3SSam Leffler 	/*
7951dd0feaaSArchie Cobbs 	 * Get interface name.
796df8bae1dSRodney W. Grimes 	 */
797df8bae1dSRodney W. Grimes 	case BIOCGETIF:
798572bde2aSRobert Watson 		if (d->bd_bif == NULL)
799df8bae1dSRodney W. Grimes 			error = EINVAL;
8001dd0feaaSArchie Cobbs 		else {
8011dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
8021dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
8031dd0feaaSArchie Cobbs 
8049bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
8059bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
8061dd0feaaSArchie Cobbs 		}
807df8bae1dSRodney W. Grimes 		break;
808df8bae1dSRodney W. Grimes 
809df8bae1dSRodney W. Grimes 	/*
810df8bae1dSRodney W. Grimes 	 * Set interface.
811df8bae1dSRodney W. Grimes 	 */
812df8bae1dSRodney W. Grimes 	case BIOCSETIF:
813df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
814df8bae1dSRodney W. Grimes 		break;
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 	/*
817df8bae1dSRodney W. Grimes 	 * Set read timeout.
818df8bae1dSRodney W. Grimes 	 */
819df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
820df8bae1dSRodney W. Grimes 		{
821df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
822df8bae1dSRodney W. Grimes 
823bdc2cdc5SAlexander Langer 			/*
824bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
825bdc2cdc5SAlexander Langer 			 * a one-shot timer.
826bdc2cdc5SAlexander Langer 			 */
827bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
828bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
829df8bae1dSRodney W. Grimes 			break;
830df8bae1dSRodney W. Grimes 		}
831df8bae1dSRodney W. Grimes 
832df8bae1dSRodney W. Grimes 	/*
833df8bae1dSRodney W. Grimes 	 * Get read timeout.
834df8bae1dSRodney W. Grimes 	 */
835df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
836df8bae1dSRodney W. Grimes 		{
837df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
838df8bae1dSRodney W. Grimes 
839bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
840bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
841df8bae1dSRodney W. Grimes 			break;
842df8bae1dSRodney W. Grimes 		}
843df8bae1dSRodney W. Grimes 
844df8bae1dSRodney W. Grimes 	/*
845df8bae1dSRodney W. Grimes 	 * Get packet stats.
846df8bae1dSRodney W. Grimes 	 */
847df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
848df8bae1dSRodney W. Grimes 		{
849df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
850df8bae1dSRodney W. Grimes 
851df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
852df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
853df8bae1dSRodney W. Grimes 			break;
854df8bae1dSRodney W. Grimes 		}
855df8bae1dSRodney W. Grimes 
856df8bae1dSRodney W. Grimes 	/*
857df8bae1dSRodney W. Grimes 	 * Set immediate mode.
858df8bae1dSRodney W. Grimes 	 */
859df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
860df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
861df8bae1dSRodney W. Grimes 		break;
862df8bae1dSRodney W. Grimes 
863df8bae1dSRodney W. Grimes 	case BIOCVERSION:
864df8bae1dSRodney W. Grimes 		{
865df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
866df8bae1dSRodney W. Grimes 
867df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
868df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
869df8bae1dSRodney W. Grimes 			break;
870df8bae1dSRodney W. Grimes 		}
87100a83887SPaul Traina 
872114ae644SMike Smith 	/*
873114ae644SMike Smith 	 * Get "header already complete" flag
874114ae644SMike Smith 	 */
875114ae644SMike Smith 	case BIOCGHDRCMPLT:
876114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
877114ae644SMike Smith 		break;
878114ae644SMike Smith 
879114ae644SMike Smith 	/*
880114ae644SMike Smith 	 * Set "header already complete" flag
881114ae644SMike Smith 	 */
882114ae644SMike Smith 	case BIOCSHDRCMPLT:
883114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
884114ae644SMike Smith 		break;
885114ae644SMike Smith 
8868ed3828cSRobert Watson 	/*
8878ed3828cSRobert Watson 	 * Get "see sent packets" flag
8888ed3828cSRobert Watson 	 */
8898ed3828cSRobert Watson 	case BIOCGSEESENT:
8908ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
8918ed3828cSRobert Watson 		break;
8928ed3828cSRobert Watson 
8938ed3828cSRobert Watson 	/*
8948ed3828cSRobert Watson 	 * Set "see sent packets" flag
8958ed3828cSRobert Watson 	 */
8968ed3828cSRobert Watson 	case BIOCSSEESENT:
8978ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
8988ed3828cSRobert Watson 		break;
8998ed3828cSRobert Watson 
90000a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
90100a83887SPaul Traina 		break;
90200a83887SPaul Traina 
90300a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
90400a83887SPaul Traina 		d->bd_async = *(int *)addr;
90500a83887SPaul Traina 		break;
90600a83887SPaul Traina 
907831d27a9SDon Lewis 	case FIOSETOWN:
908831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
90900a83887SPaul Traina 		break;
91000a83887SPaul Traina 
911831d27a9SDon Lewis 	case FIOGETOWN:
91291e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
913831d27a9SDon Lewis 		break;
914831d27a9SDon Lewis 
915831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
916831d27a9SDon Lewis 	case TIOCSPGRP:
917831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
918831d27a9SDon Lewis 		break;
919831d27a9SDon Lewis 
920831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
92100a83887SPaul Traina 	case TIOCGPGRP:
92291e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
92300a83887SPaul Traina 		break;
92400a83887SPaul Traina 
92500a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
92600a83887SPaul Traina 		{
92700a83887SPaul Traina 			u_int sig;
92800a83887SPaul Traina 
92900a83887SPaul Traina 			sig = *(u_int *)addr;
93000a83887SPaul Traina 
93100a83887SPaul Traina 			if (sig >= NSIG)
93200a83887SPaul Traina 				error = EINVAL;
93300a83887SPaul Traina 			else
93400a83887SPaul Traina 				d->bd_sig = sig;
93500a83887SPaul Traina 			break;
93600a83887SPaul Traina 		}
93700a83887SPaul Traina 	case BIOCGRSIG:
93800a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
93900a83887SPaul Traina 		break;
940df8bae1dSRodney W. Grimes 	}
941df8bae1dSRodney W. Grimes 	return (error);
942df8bae1dSRodney W. Grimes }
943df8bae1dSRodney W. Grimes 
944df8bae1dSRodney W. Grimes /*
945df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
946df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
947df8bae1dSRodney W. Grimes  */
948f708ef1bSPoul-Henning Kamp static int
949df8bae1dSRodney W. Grimes bpf_setf(d, fp)
950df8bae1dSRodney W. Grimes 	struct bpf_d *d;
951df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
952df8bae1dSRodney W. Grimes {
953df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
954df8bae1dSRodney W. Grimes 	u_int flen, size;
955df8bae1dSRodney W. Grimes 
956572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
957df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
958df8bae1dSRodney W. Grimes 			return (EINVAL);
959e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
960a8e93fb7SRobert Watson 		old = d->bd_filter;
961572bde2aSRobert Watson 		d->bd_filter = NULL;
962df8bae1dSRodney W. Grimes 		reset_d(d);
963e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
964572bde2aSRobert Watson 		if (old != NULL)
965bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
966df8bae1dSRodney W. Grimes 		return (0);
967df8bae1dSRodney W. Grimes 	}
968df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
969df8bae1dSRodney W. Grimes 	if (flen > BPF_MAXINSNS)
970df8bae1dSRodney W. Grimes 		return (EINVAL);
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
973a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
974df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
975df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
976e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
977a8e93fb7SRobert Watson 		old = d->bd_filter;
978df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
979df8bae1dSRodney W. Grimes 		reset_d(d);
980e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
981572bde2aSRobert Watson 		if (old != NULL)
982bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
983df8bae1dSRodney W. Grimes 
984df8bae1dSRodney W. Grimes 		return (0);
985df8bae1dSRodney W. Grimes 	}
986bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
987df8bae1dSRodney W. Grimes 	return (EINVAL);
988df8bae1dSRodney W. Grimes }
989df8bae1dSRodney W. Grimes 
990df8bae1dSRodney W. Grimes /*
991df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
992df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
993df8bae1dSRodney W. Grimes  * Return an errno or 0.
994df8bae1dSRodney W. Grimes  */
995df8bae1dSRodney W. Grimes static int
996df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
997df8bae1dSRodney W. Grimes 	struct bpf_d *d;
998df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
999df8bae1dSRodney W. Grimes {
1000df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
1001e7bb21b3SJonathan Lemon 	int error;
10029b44ff22SGarrett Wollman 	struct ifnet *theywant;
1003df8bae1dSRodney W. Grimes 
10049b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
1005572bde2aSRobert Watson 	if (theywant == NULL)
10069b44ff22SGarrett Wollman 		return ENXIO;
10079b44ff22SGarrett Wollman 
1008df8bae1dSRodney W. Grimes 	/*
1009df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
1010df8bae1dSRodney W. Grimes 	 */
1011e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
10124a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1013df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
1014df8bae1dSRodney W. Grimes 
1015572bde2aSRobert Watson 		if (ifp == NULL || ifp != theywant)
1016df8bae1dSRodney W. Grimes 			continue;
101724a229f4SSam Leffler 		/* skip additional entry */
101824a229f4SSam Leffler 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
101924a229f4SSam Leffler 			continue;
1020e7bb21b3SJonathan Lemon 
1021e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1022df8bae1dSRodney W. Grimes 		/*
1023df8bae1dSRodney W. Grimes 		 * We found the requested interface.
1024df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
1025df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
1026df8bae1dSRodney W. Grimes 		 * just flush the buffer.
1027df8bae1dSRodney W. Grimes 		 */
1028572bde2aSRobert Watson 		if (d->bd_sbuf == NULL) {
1029df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
1030df8bae1dSRodney W. Grimes 			if (error != 0)
1031df8bae1dSRodney W. Grimes 				return (error);
1032df8bae1dSRodney W. Grimes 		}
1033df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
1034df8bae1dSRodney W. Grimes 			if (d->bd_bif)
1035df8bae1dSRodney W. Grimes 				/*
1036df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
1037df8bae1dSRodney W. Grimes 				 */
1038df8bae1dSRodney W. Grimes 				bpf_detachd(d);
1039df8bae1dSRodney W. Grimes 
1040df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
1041df8bae1dSRodney W. Grimes 		}
1042e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1043df8bae1dSRodney W. Grimes 		reset_d(d);
1044e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1045df8bae1dSRodney W. Grimes 		return (0);
1046df8bae1dSRodney W. Grimes 	}
1047e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1048df8bae1dSRodney W. Grimes 	/* Not found. */
1049df8bae1dSRodney W. Grimes 	return (ENXIO);
1050df8bae1dSRodney W. Grimes }
1051df8bae1dSRodney W. Grimes 
1052df8bae1dSRodney W. Grimes /*
1053243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1054df8bae1dSRodney W. Grimes  *
1055df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1056df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1057df8bae1dSRodney W. Grimes  */
105837c84183SPoul-Henning Kamp static int
1059b40ce416SJulian Elischer bpfpoll(dev, events, td)
106089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1061243ac7d8SPeter Wemm 	int events;
1062b40ce416SJulian Elischer 	struct thread *td;
1063df8bae1dSRodney W. Grimes {
1064e7bb21b3SJonathan Lemon 	struct bpf_d *d;
10650832fc64SGarance A Drosehn 	int revents;
1066df8bae1dSRodney W. Grimes 
1067bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1068de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1069de5d9935SRobert Watson 		return (ENXIO);
1070de5d9935SRobert Watson 
10710832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1072e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
107375c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
107495aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1075243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
107681bda851SJohn Polstra 		else {
1077ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
107881bda851SJohn Polstra 			/* Start the read timeout if necessary. */
107981bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
108081bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
108181bda851SJohn Polstra 				    bpf_timed_out, d);
108281bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
108381bda851SJohn Polstra 			}
108481bda851SJohn Polstra 		}
108575c13541SPoul-Henning Kamp 	}
1086e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1087243ac7d8SPeter Wemm 	return (revents);
1088df8bae1dSRodney W. Grimes }
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes /*
109195aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
109295aab9ccSJohn-Mark Gurney  * reject all others.
109395aab9ccSJohn-Mark Gurney  */
109495aab9ccSJohn-Mark Gurney int
109595aab9ccSJohn-Mark Gurney bpfkqfilter(dev, kn)
109689c9c53dSPoul-Henning Kamp 	struct cdev *dev;
109795aab9ccSJohn-Mark Gurney 	struct knote *kn;
109895aab9ccSJohn-Mark Gurney {
109995aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
110095aab9ccSJohn-Mark Gurney 
110195aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
110295aab9ccSJohn-Mark Gurney 		return (1);
110395aab9ccSJohn-Mark Gurney 
110495aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
110595aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
1106ad3b9257SJohn-Mark Gurney 	knlist_add(&d->bd_sel.si_note, kn, 0);
110795aab9ccSJohn-Mark Gurney 
110895aab9ccSJohn-Mark Gurney 	return (0);
110995aab9ccSJohn-Mark Gurney }
111095aab9ccSJohn-Mark Gurney 
111195aab9ccSJohn-Mark Gurney static void
111295aab9ccSJohn-Mark Gurney filt_bpfdetach(kn)
111395aab9ccSJohn-Mark Gurney 	struct knote *kn;
111495aab9ccSJohn-Mark Gurney {
111595aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
111695aab9ccSJohn-Mark Gurney 
1117ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
111895aab9ccSJohn-Mark Gurney }
111995aab9ccSJohn-Mark Gurney 
112095aab9ccSJohn-Mark Gurney static int
112195aab9ccSJohn-Mark Gurney filt_bpfread(kn, hint)
112295aab9ccSJohn-Mark Gurney 	struct knote *kn;
112395aab9ccSJohn-Mark Gurney 	long hint;
112495aab9ccSJohn-Mark Gurney {
112595aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
112695aab9ccSJohn-Mark Gurney 	int ready;
112795aab9ccSJohn-Mark Gurney 
112886c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(d);
112995aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
113095aab9ccSJohn-Mark Gurney 	if (ready) {
113195aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
113295aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
113395aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
113495aab9ccSJohn-Mark Gurney 	}
113595aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
113695aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
113795aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
113895aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
113995aab9ccSJohn-Mark Gurney 	}
114095aab9ccSJohn-Mark Gurney 
114195aab9ccSJohn-Mark Gurney 	return (ready);
114295aab9ccSJohn-Mark Gurney }
114395aab9ccSJohn-Mark Gurney 
114495aab9ccSJohn-Mark Gurney /*
1145df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1146df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1147df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1148df8bae1dSRodney W. Grimes  * buffer.
1149df8bae1dSRodney W. Grimes  */
1150df8bae1dSRodney W. Grimes void
115124a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
115224a229f4SSam Leffler 	struct bpf_if *bp;
11538994a245SDag-Erling Smørgrav 	u_char *pkt;
11548994a245SDag-Erling Smørgrav 	u_int pktlen;
1155df8bae1dSRodney W. Grimes {
11568994a245SDag-Erling Smørgrav 	struct bpf_d *d;
11578994a245SDag-Erling Smørgrav 	u_int slen;
1158e7bb21b3SJonathan Lemon 
115946691dd8SRobert Watson 	/*
116046691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
116146691dd8SRobert Watson 	 * no descriptors attached.
116246691dd8SRobert Watson 	 */
11634a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
116446691dd8SRobert Watson 		return;
116546691dd8SRobert Watson 
1166e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
11674a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1168e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1169df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1170df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1171ec272d87SRobert Watson 		if (slen != 0) {
1172ec272d87SRobert Watson #ifdef MAC
117324a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1174ec272d87SRobert Watson #endif
1175df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1176ec272d87SRobert Watson 		}
1177e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1178df8bae1dSRodney W. Grimes 	}
1179e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1180df8bae1dSRodney W. Grimes }
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes /*
1183df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1184df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1185df8bae1dSRodney W. Grimes  */
1186df8bae1dSRodney W. Grimes static void
1187df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1188df8bae1dSRodney W. Grimes 	const void *src_arg;
1189df8bae1dSRodney W. Grimes 	void *dst_arg;
11908994a245SDag-Erling Smørgrav 	size_t len;
1191df8bae1dSRodney W. Grimes {
11928994a245SDag-Erling Smørgrav 	const struct mbuf *m;
11938994a245SDag-Erling Smørgrav 	u_int count;
1194df8bae1dSRodney W. Grimes 	u_char *dst;
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes 	m = src_arg;
1197df8bae1dSRodney W. Grimes 	dst = dst_arg;
1198df8bae1dSRodney W. Grimes 	while (len > 0) {
1199572bde2aSRobert Watson 		if (m == NULL)
1200df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1201df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
12020453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1203df8bae1dSRodney W. Grimes 		m = m->m_next;
1204df8bae1dSRodney W. Grimes 		dst += count;
1205df8bae1dSRodney W. Grimes 		len -= count;
1206df8bae1dSRodney W. Grimes 	}
1207df8bae1dSRodney W. Grimes }
1208df8bae1dSRodney W. Grimes 
1209df8bae1dSRodney W. Grimes /*
1210df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1211df8bae1dSRodney W. Grimes  */
1212df8bae1dSRodney W. Grimes void
121324a229f4SSam Leffler bpf_mtap(bp, m)
121424a229f4SSam Leffler 	struct bpf_if *bp;
1215df8bae1dSRodney W. Grimes 	struct mbuf *m;
1216df8bae1dSRodney W. Grimes {
1217df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1218df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1219df8bae1dSRodney W. Grimes 
122046691dd8SRobert Watson 	/*
122146691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
122246691dd8SRobert Watson 	 * no descriptors attached.
122346691dd8SRobert Watson 	 */
12244a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
122546691dd8SRobert Watson 		return;
122646691dd8SRobert Watson 
1227f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
12287b831242SPoul-Henning Kamp 	if (pktlen == m->m_len) {
122924a229f4SSam Leffler 		bpf_tap(bp, mtod(m, u_char *), pktlen);
12307b831242SPoul-Henning Kamp 		return;
12317b831242SPoul-Henning Kamp 	}
1232df8bae1dSRodney W. Grimes 
1233e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
12344a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
12358ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
12368ed3828cSRobert Watson 			continue;
1237e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1238df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1239df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1240df8bae1dSRodney W. Grimes 		if (slen != 0)
12410c7fb534SRobert Watson #ifdef MAC
124224a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
12430c7fb534SRobert Watson #endif
12440c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
12450c7fb534SRobert Watson 				    bpf_mcopy);
1246e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1247df8bae1dSRodney W. Grimes 	}
1248e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1249df8bae1dSRodney W. Grimes }
1250df8bae1dSRodney W. Grimes 
1251df8bae1dSRodney W. Grimes /*
1252437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1253437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1254437ffe18SSam Leffler  */
1255437ffe18SSam Leffler void
1256437ffe18SSam Leffler bpf_mtap2(bp, data, dlen, m)
1257437ffe18SSam Leffler 	struct bpf_if *bp;
1258437ffe18SSam Leffler 	void *data;
1259437ffe18SSam Leffler 	u_int dlen;
1260437ffe18SSam Leffler 	struct mbuf *m;
1261437ffe18SSam Leffler {
1262437ffe18SSam Leffler 	struct mbuf mb;
1263437ffe18SSam Leffler 	struct bpf_d *d;
1264437ffe18SSam Leffler 	u_int pktlen, slen;
1265437ffe18SSam Leffler 
126646691dd8SRobert Watson 	/*
126746691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
126846691dd8SRobert Watson 	 * no descriptors attached.
126946691dd8SRobert Watson 	 */
12704a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
127146691dd8SRobert Watson 		return;
127246691dd8SRobert Watson 
1273437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1274437ffe18SSam Leffler 	/*
1275437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1276437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1277437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1278437ffe18SSam Leffler 	 */
1279437ffe18SSam Leffler 	mb.m_next = m;
1280437ffe18SSam Leffler 	mb.m_data = data;
1281437ffe18SSam Leffler 	mb.m_len = dlen;
1282437ffe18SSam Leffler 	pktlen += dlen;
1283437ffe18SSam Leffler 
1284437ffe18SSam Leffler 	BPFIF_LOCK(bp);
12854a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1286437ffe18SSam Leffler 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1287437ffe18SSam Leffler 			continue;
1288437ffe18SSam Leffler 		BPFD_LOCK(d);
1289437ffe18SSam Leffler 		++d->bd_rcount;
1290437ffe18SSam Leffler 		slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1291437ffe18SSam Leffler 		if (slen != 0)
1292437ffe18SSam Leffler #ifdef MAC
1293437ffe18SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1294437ffe18SSam Leffler #endif
1295437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
1296437ffe18SSam Leffler 				    bpf_mcopy);
1297437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1298437ffe18SSam Leffler 	}
1299437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1300437ffe18SSam Leffler }
1301437ffe18SSam Leffler 
1302437ffe18SSam Leffler /*
1303df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
13049e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1305df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1306df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1307df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1308df8bae1dSRodney W. Grimes  */
1309df8bae1dSRodney W. Grimes static void
1310df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
13118994a245SDag-Erling Smørgrav 	struct bpf_d *d;
13128994a245SDag-Erling Smørgrav 	u_char *pkt;
13138994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
13148994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1315df8bae1dSRodney W. Grimes {
13168994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
13178994a245SDag-Erling Smørgrav 	int totlen, curlen;
13188994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
13197819da79SJohn-Mark Gurney 	int do_wakeup = 0;
13209e610888SDag-Erling Smørgrav 
1321a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1322df8bae1dSRodney W. Grimes 	/*
1323df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1324df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1325df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1326df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1327df8bae1dSRodney W. Grimes 	 */
1328df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1329df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1330df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1331df8bae1dSRodney W. Grimes 
1332df8bae1dSRodney W. Grimes 	/*
1333df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1334df8bae1dSRodney W. Grimes 	 */
1335df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1336df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1337df8bae1dSRodney W. Grimes 		/*
1338df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1339df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1340df8bae1dSRodney W. Grimes 		 * pending reads.
1341df8bae1dSRodney W. Grimes 		 */
1342572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1343df8bae1dSRodney W. Grimes 			/*
1344df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1345df8bae1dSRodney W. Grimes 			 * so drop the packet.
1346df8bae1dSRodney W. Grimes 			 */
1347df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1348df8bae1dSRodney W. Grimes 			return;
1349df8bae1dSRodney W. Grimes 		}
1350df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
13517819da79SJohn-Mark Gurney 		do_wakeup = 1;
1352df8bae1dSRodney W. Grimes 		curlen = 0;
1353df8bae1dSRodney W. Grimes 	}
135481bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1355df8bae1dSRodney W. Grimes 		/*
135681bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
135781bda851SJohn Polstra 		 * already expired during a select call.  A packet
135881bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1359df8bae1dSRodney W. Grimes 		 */
13607819da79SJohn-Mark Gurney 		do_wakeup = 1;
1361df8bae1dSRodney W. Grimes 
1362df8bae1dSRodney W. Grimes 	/*
1363df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1364df8bae1dSRodney W. Grimes 	 */
1365df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1366df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1367df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1368df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1369df8bae1dSRodney W. Grimes 	/*
1370df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1371df8bae1dSRodney W. Grimes 	 */
1372df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1373df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
13747819da79SJohn-Mark Gurney 
13757819da79SJohn-Mark Gurney 	if (do_wakeup)
13767819da79SJohn-Mark Gurney 		bpf_wakeup(d);
1377df8bae1dSRodney W. Grimes }
1378df8bae1dSRodney W. Grimes 
1379df8bae1dSRodney W. Grimes /*
1380df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1381df8bae1dSRodney W. Grimes  */
1382df8bae1dSRodney W. Grimes static int
1383df8bae1dSRodney W. Grimes bpf_allocbufs(d)
13848994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1385df8bae1dSRodney W. Grimes {
1386a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1387572bde2aSRobert Watson 	if (d->bd_fbuf == NULL)
1388df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1389df8bae1dSRodney W. Grimes 
1390a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1391572bde2aSRobert Watson 	if (d->bd_sbuf == NULL) {
1392bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1393df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1394df8bae1dSRodney W. Grimes 	}
1395df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1396df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1397df8bae1dSRodney W. Grimes 	return (0);
1398df8bae1dSRodney W. Grimes }
1399df8bae1dSRodney W. Grimes 
1400df8bae1dSRodney W. Grimes /*
1401df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1402df8bae1dSRodney W. Grimes  * Called on close.
1403df8bae1dSRodney W. Grimes  */
1404df8bae1dSRodney W. Grimes static void
1405df8bae1dSRodney W. Grimes bpf_freed(d)
14068994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1407df8bae1dSRodney W. Grimes {
1408df8bae1dSRodney W. Grimes 	/*
1409df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1410df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1411df8bae1dSRodney W. Grimes 	 * free.
1412df8bae1dSRodney W. Grimes 	 */
1413572bde2aSRobert Watson 	if (d->bd_sbuf != NULL) {
1414bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1415572bde2aSRobert Watson 		if (d->bd_hbuf != NULL)
1416bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1417572bde2aSRobert Watson 		if (d->bd_fbuf != NULL)
1418bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1419df8bae1dSRodney W. Grimes 	}
1420df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1421bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1422e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1423df8bae1dSRodney W. Grimes }
1424df8bae1dSRodney W. Grimes 
1425df8bae1dSRodney W. Grimes /*
142624a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
142724a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1428df8bae1dSRodney W. Grimes  */
1429df8bae1dSRodney W. Grimes void
14309b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1431df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1432df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1433df8bae1dSRodney W. Grimes {
143424a229f4SSam Leffler 
143524a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
143624a229f4SSam Leffler }
143724a229f4SSam Leffler 
143824a229f4SSam Leffler /*
143924a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
144024a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
144124a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
144224a229f4SSam Leffler  * headers are not yet supporrted).
144324a229f4SSam Leffler  */
144424a229f4SSam Leffler void
144524a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
144624a229f4SSam Leffler 	struct ifnet *ifp;
144724a229f4SSam Leffler 	u_int dlt, hdrlen;
144824a229f4SSam Leffler 	struct bpf_if **driverp;
144924a229f4SSam Leffler {
1450df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
14516a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1452572bde2aSRobert Watson 	if (bp == NULL)
1453df8bae1dSRodney W. Grimes 		panic("bpfattach");
1454df8bae1dSRodney W. Grimes 
14554a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
145624a229f4SSam Leffler 	bp->bif_driverp = driverp;
1457df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1458df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
14596008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1460df8bae1dSRodney W. Grimes 
1461e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
14624a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1463e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1464df8bae1dSRodney W. Grimes 
1465572bde2aSRobert Watson 	*bp->bif_driverp = NULL;
1466df8bae1dSRodney W. Grimes 
1467df8bae1dSRodney W. Grimes 	/*
1468df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1469df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1470df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1471df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1472df8bae1dSRodney W. Grimes 	 */
1473df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1474df8bae1dSRodney W. Grimes 
14752eeab939SGarrett Wollman 	if (bootverbose)
147624a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1477df8bae1dSRodney W. Grimes }
147853ac6efbSJulian Elischer 
1479de5d9935SRobert Watson /*
1480de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1481de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1482de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1483de5d9935SRobert Watson  * ENXIO.
1484de5d9935SRobert Watson  */
1485de5d9935SRobert Watson void
1486de5d9935SRobert Watson bpfdetach(ifp)
1487de5d9935SRobert Watson 	struct ifnet *ifp;
1488de5d9935SRobert Watson {
14894a3feeaaSRobert Watson 	struct bpf_if	*bp;
1490de5d9935SRobert Watson 	struct bpf_d	*d;
1491de5d9935SRobert Watson 
1492de5d9935SRobert Watson 	/* Locate BPF interface information */
14938eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
14944a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1495de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1496de5d9935SRobert Watson 			break;
1497de5d9935SRobert Watson 	}
1498de5d9935SRobert Watson 
1499de5d9935SRobert Watson 	/* Interface wasn't attached */
1500d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1501e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
15029bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1503de5d9935SRobert Watson 		return;
1504de5d9935SRobert Watson 	}
1505de5d9935SRobert Watson 
15064a3feeaaSRobert Watson 	LIST_REMOVE(bp, bif_next);
15078eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1508de5d9935SRobert Watson 
15094a3feeaaSRobert Watson 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1510e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1511e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1512e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1513e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1514e7bb21b3SJonathan Lemon 	}
1515e7bb21b3SJonathan Lemon 
1516e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1517de5d9935SRobert Watson 	free(bp, M_BPF);
15188eab61f3SSam Leffler }
1519de5d9935SRobert Watson 
15208eab61f3SSam Leffler /*
15218eab61f3SSam Leffler  * Get a list of available data link type of the interface.
15228eab61f3SSam Leffler  */
15238eab61f3SSam Leffler static int
15248eab61f3SSam Leffler bpf_getdltlist(d, bfl)
15258eab61f3SSam Leffler 	struct bpf_d *d;
15268eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
15278eab61f3SSam Leffler {
15288eab61f3SSam Leffler 	int n, error;
15298eab61f3SSam Leffler 	struct ifnet *ifp;
15308eab61f3SSam Leffler 	struct bpf_if *bp;
15318eab61f3SSam Leffler 
15328eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15338eab61f3SSam Leffler 	n = 0;
15348eab61f3SSam Leffler 	error = 0;
15358eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15364a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
15378eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
15388eab61f3SSam Leffler 			continue;
15398eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
15408eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1541e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
15428eab61f3SSam Leffler 				return (ENOMEM);
15438eab61f3SSam Leffler 			}
15448eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
15458eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
15468eab61f3SSam Leffler 		}
15478eab61f3SSam Leffler 		n++;
15488eab61f3SSam Leffler 	}
15498eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15508eab61f3SSam Leffler 	bfl->bfl_len = n;
15518eab61f3SSam Leffler 	return (error);
15528eab61f3SSam Leffler }
15538eab61f3SSam Leffler 
15548eab61f3SSam Leffler /*
15558eab61f3SSam Leffler  * Set the data link type of a BPF instance.
15568eab61f3SSam Leffler  */
15578eab61f3SSam Leffler static int
15588eab61f3SSam Leffler bpf_setdlt(d, dlt)
15598eab61f3SSam Leffler 	struct bpf_d *d;
15608eab61f3SSam Leffler 	u_int dlt;
15618eab61f3SSam Leffler {
15628eab61f3SSam Leffler 	int error, opromisc;
15638eab61f3SSam Leffler 	struct ifnet *ifp;
15648eab61f3SSam Leffler 	struct bpf_if *bp;
15658eab61f3SSam Leffler 
15668eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
15678eab61f3SSam Leffler 		return (0);
15688eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15698eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15704a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
15718eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
15728eab61f3SSam Leffler 			break;
15738eab61f3SSam Leffler 	}
15748eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15758eab61f3SSam Leffler 	if (bp != NULL) {
15768eab61f3SSam Leffler 		opromisc = d->bd_promisc;
15778eab61f3SSam Leffler 		bpf_detachd(d);
15788eab61f3SSam Leffler 		bpf_attachd(d, bp);
157993daabddSBrian Feldman 		BPFD_LOCK(d);
15808eab61f3SSam Leffler 		reset_d(d);
15818eab61f3SSam Leffler 		BPFD_UNLOCK(d);
15828eab61f3SSam Leffler 		if (opromisc) {
15838eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
15848eab61f3SSam Leffler 			if (error)
15858eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
15868eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
15878eab61f3SSam Leffler 					error);
15888eab61f3SSam Leffler 			else
15898eab61f3SSam Leffler 				d->bd_promisc = 1;
15908eab61f3SSam Leffler 		}
15918eab61f3SSam Leffler 	}
15928eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1593de5d9935SRobert Watson }
1594de5d9935SRobert Watson 
15953f54a085SPoul-Henning Kamp static void
15963f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
15973f54a085SPoul-Henning Kamp 	void *arg;
15983f54a085SPoul-Henning Kamp 	char *name;
15993f54a085SPoul-Henning Kamp 	int namelen;
160089c9c53dSPoul-Henning Kamp 	struct cdev **dev;
16013f54a085SPoul-Henning Kamp {
16023f54a085SPoul-Henning Kamp 	int u;
16033f54a085SPoul-Henning Kamp 
1604f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
16053f54a085SPoul-Henning Kamp 		return;
1606db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
16073f54a085SPoul-Henning Kamp 		return;
1608b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1609b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1610f4f6abcbSPoul-Henning Kamp 	dev_ref(*dev);
1611b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
16123f54a085SPoul-Henning Kamp 	return;
16133f54a085SPoul-Henning Kamp }
16143f54a085SPoul-Henning Kamp 
1615514ede09SBruce Evans static void
1616514ede09SBruce Evans bpf_drvinit(unused)
1617514ede09SBruce Evans 	void *unused;
161853ac6efbSJulian Elischer {
161953ac6efbSJulian Elischer 
16206008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
16214a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
1622db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
16237198bf47SJulian Elischer }
162453ac6efbSJulian Elischer 
1625c9c7976fSPoul-Henning Kamp SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
162653ac6efbSJulian Elischer 
16275bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1628f8dc4716SMike Smith /*
1629f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1630f8dc4716SMike Smith  *
1631f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1632f8dc4716SMike Smith  * to be loaded at runtime.
1633f8dc4716SMike Smith  */
1634f8dc4716SMike Smith 
1635f8dc4716SMike Smith void
1636e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1637e5562beeSSam Leffler 	struct bpf_if *bp;
16388994a245SDag-Erling Smørgrav 	u_char *pkt;
16398994a245SDag-Erling Smørgrav 	u_int pktlen;
1640f8dc4716SMike Smith {
1641f8dc4716SMike Smith }
1642f8dc4716SMike Smith 
1643f8dc4716SMike Smith void
1644e5562beeSSam Leffler bpf_mtap(bp, m)
1645e5562beeSSam Leffler 	struct bpf_if *bp;
1646f8dc4716SMike Smith 	struct mbuf *m;
1647f8dc4716SMike Smith {
1648f8dc4716SMike Smith }
1649f8dc4716SMike Smith 
1650f8dc4716SMike Smith void
1651437ffe18SSam Leffler bpf_mtap2(bp, d, l, m)
1652437ffe18SSam Leffler 	struct bpf_if *bp;
1653c1dae2f0STim J. Robbins 	void *d;
1654437ffe18SSam Leffler 	u_int l;
1655437ffe18SSam Leffler 	struct mbuf *m;
1656437ffe18SSam Leffler {
1657437ffe18SSam Leffler }
1658437ffe18SSam Leffler 
1659437ffe18SSam Leffler void
1660f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1661f8dc4716SMike Smith 	struct ifnet *ifp;
1662f8dc4716SMike Smith 	u_int dlt, hdrlen;
1663f8dc4716SMike Smith {
1664f8dc4716SMike Smith }
1665f8dc4716SMike Smith 
1666da626c17SBill Paul void
16675f7a7923SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
16685f7a7923SSam Leffler 	struct ifnet *ifp;
16695f7a7923SSam Leffler 	u_int dlt, hdrlen;
16705f7a7923SSam Leffler 	struct bpf_if **driverp;
16715f7a7923SSam Leffler {
16725f7a7923SSam Leffler }
16735f7a7923SSam Leffler 
16745f7a7923SSam Leffler void
1675da626c17SBill Paul bpfdetach(ifp)
1676da626c17SBill Paul 	struct ifnet *ifp;
1677da626c17SBill Paul {
1678da626c17SBill Paul }
1679da626c17SBill Paul 
1680f8dc4716SMike Smith u_int
1681f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
16828994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
16838994a245SDag-Erling Smørgrav 	u_char *p;
1684f8dc4716SMike Smith 	u_int wirelen;
16858994a245SDag-Erling Smørgrav 	u_int buflen;
1686f8dc4716SMike Smith {
1687f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1688f8dc4716SMike Smith }
1689f8dc4716SMike Smith 
16905bb5f2c9SPeter Wemm int
16915bb5f2c9SPeter Wemm bpf_validate(f, len)
16925bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
16935bb5f2c9SPeter Wemm 	int len;
16945bb5f2c9SPeter Wemm {
16955bb5f2c9SPeter Wemm 	return 0;		/* false */
16965bb5f2c9SPeter Wemm }
16975bb5f2c9SPeter Wemm 
16985bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1699