xref: /freebsd/sys/net/bpf.c (revision ae275efcaee4276954fdf42aeebac61211d81bbb)
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>
68ae275efcSJung-uk Kim #ifdef BPF_JITTER
69ae275efcSJung-uk Kim #include <net/bpf_jitter.h>
70ae275efcSJung-uk Kim #endif
71df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #include <netinet/in.h>
74df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
75df8bae1dSRodney W. Grimes #include <sys/kernel.h>
76f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
777b778b5eSEivind Eklund 
78959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
7987f6c662SJulian Elischer 
805bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
8153ac6efbSJulian Elischer 
82df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
83df8bae1dSRodney W. Grimes 
84df8bae1dSRodney W. Grimes /*
85d1a67300SRobert Watson  * bpf_iflist is a list of BPF interface structures, each corresponding to a
86d1a67300SRobert Watson  * specific DLT.  The same network interface might have several BPF interface
87d1a67300SRobert Watson  * structures registered by different layers in the stack (i.e., 802.11
88d1a67300SRobert Watson  * frames, ethernet frames, etc).
89df8bae1dSRodney W. Grimes  */
904a3feeaaSRobert Watson static LIST_HEAD(, bpf_if)	bpf_iflist;
91e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
9269f7644bSChristian S.J. Peron static int		bpf_bpfd_cnt;
93df8bae1dSRodney W. Grimes 
94929ddbbbSAlfred Perlstein static int	bpf_allocbufs(struct bpf_d *);
95929ddbbbSAlfred Perlstein static void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
96929ddbbbSAlfred Perlstein static void	bpf_detachd(struct bpf_d *d);
97929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
98929ddbbbSAlfred Perlstein static void	bpf_mcopy(const void *, void *, size_t);
9901399f34SDavid Malone static int	bpf_movein(struct uio *, int, int,
10093e39f0bSChristian S.J. Peron 		    struct mbuf **, struct sockaddr *, struct bpf_insn *);
101929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
102929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
103e7bb21b3SJonathan Lemon static __inline void
104929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
105929ddbbbSAlfred Perlstein static void	catchpacket(struct bpf_d *, u_char *, u_int,
106929ddbbbSAlfred Perlstein 		    u_int, void (*)(const void *, void *, size_t));
107929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
10893e39f0bSChristian S.J. Peron static int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
1098eab61f3SSam Leffler static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
1108eab61f3SSam Leffler static int	bpf_setdlt(struct bpf_d *, u_int);
11195aab9ccSJohn-Mark Gurney static void	filt_bpfdetach(struct knote *);
11295aab9ccSJohn-Mark Gurney static int	filt_bpfread(struct knote *, long);
113a3272e3cSChristian S.J. Peron static void	bpf_drvinit(void *);
1146a113b3dSRobert Watson static void	bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
11569f7644bSChristian S.J. Peron static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
11669f7644bSChristian S.J. Peron 
11769f7644bSChristian S.J. Peron /*
11869f7644bSChristian S.J. Peron  * The default read buffer size is patchable.
11969f7644bSChristian S.J. Peron  */
12069f7644bSChristian S.J. Peron SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
12169f7644bSChristian S.J. Peron static int bpf_bufsize = 4096;
12269f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
12369f7644bSChristian S.J. Peron     &bpf_bufsize, 0, "");
12469f7644bSChristian S.J. Peron static int bpf_maxbufsize = BPF_MAXBUFSIZE;
12569f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
12669f7644bSChristian S.J. Peron     &bpf_maxbufsize, 0, "");
12769f7644bSChristian S.J. Peron static int bpf_maxinsns = BPF_MAXINSNS;
12869f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
12969f7644bSChristian S.J. Peron     &bpf_maxinsns, 0, "Maximum bpf program instructions");
13069f7644bSChristian S.J. Peron SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
13169f7644bSChristian S.J. Peron     bpf_stats_sysctl, "bpf statistics portal");
132ae275efcSJung-uk Kim #ifdef BPF_JITTER
133ae275efcSJung-uk Kim SYSCTL_NODE(_net_bpf, OID_AUTO, jitter, CTLFLAG_RW, 0, "bpf jitter sysctl");
134ae275efcSJung-uk Kim static int bpf_jitter_enable = 1;
135ae275efcSJung-uk Kim SYSCTL_INT(_net_bpf_jitter, OID_AUTO, enable, CTLFLAG_RW,
136ae275efcSJung-uk Kim     &bpf_jitter_enable, 0, "bpf JIT compiler");
137ae275efcSJung-uk Kim #endif
138df8bae1dSRodney W. Grimes 
13987f6c662SJulian Elischer static	d_open_t	bpfopen;
14087f6c662SJulian Elischer static	d_close_t	bpfclose;
14187f6c662SJulian Elischer static	d_read_t	bpfread;
14287f6c662SJulian Elischer static	d_write_t	bpfwrite;
14387f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
144243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
14595aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
14687f6c662SJulian Elischer 
1474e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
148dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
149dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
1507ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1517ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1527ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1537ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1547ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1557ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1567ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
15795aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1584e2f199eSPoul-Henning Kamp };
15987f6c662SJulian Elischer 
16095aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
16195aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
16287f6c662SJulian Elischer 
163df8bae1dSRodney W. Grimes static int
16493e39f0bSChristian S.J. Peron bpf_movein(uio, linktype, mtu, mp, sockp, wfilter)
1658994a245SDag-Erling Smørgrav 	struct uio *uio;
16601399f34SDavid Malone 	int linktype;
16701399f34SDavid Malone 	int mtu;
1688994a245SDag-Erling Smørgrav 	struct mbuf **mp;
1698994a245SDag-Erling Smørgrav 	struct sockaddr *sockp;
17093e39f0bSChristian S.J. Peron 	struct bpf_insn *wfilter;
171df8bae1dSRodney W. Grimes {
172df8bae1dSRodney W. Grimes 	struct mbuf *m;
173df8bae1dSRodney W. Grimes 	int error;
174df8bae1dSRodney W. Grimes 	int len;
175df8bae1dSRodney W. Grimes 	int hlen;
17693e39f0bSChristian S.J. Peron 	int slen;
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes 	/*
179df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
180df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
181df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
182df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
183df8bae1dSRodney W. Grimes 	 * is forwarded as is.
184df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
185df8bae1dSRodney W. Grimes 	 * for the link level header.
186df8bae1dSRodney W. Grimes 	 */
187df8bae1dSRodney W. Grimes 	switch (linktype) {
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes 	case DLT_SLIP:
190df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
191df8bae1dSRodney W. Grimes 		hlen = 0;
192df8bae1dSRodney W. Grimes 		break;
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
195df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
196df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
197797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
198df8bae1dSRodney W. Grimes 		break;
199df8bae1dSRodney W. Grimes 
200df8bae1dSRodney W. Grimes 	case DLT_FDDI:
201d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
202d41f24e7SDavid Greenman 		hlen = 0;
203df8bae1dSRodney W. Grimes 		break;
204df8bae1dSRodney W. Grimes 
20522f05c43SAndrey A. Chernov 	case DLT_RAW:
206df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
207df8bae1dSRodney W. Grimes 		hlen = 0;
208df8bae1dSRodney W. Grimes 		break;
209df8bae1dSRodney W. Grimes 
21001399f34SDavid Malone 	case DLT_NULL:
21101399f34SDavid Malone 		/*
21201399f34SDavid Malone 		 * null interface types require a 4 byte pseudo header which
21301399f34SDavid Malone 		 * corresponds to the address family of the packet.
21401399f34SDavid Malone 		 */
21501399f34SDavid Malone 		sockp->sa_family = AF_UNSPEC;
21601399f34SDavid Malone 		hlen = 4;
21701399f34SDavid Malone 		break;
21801399f34SDavid Malone 
2194f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
2204f53e3ccSKenjiro Cho 		/*
2214f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
2224f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
2234f53e3ccSKenjiro Cho 		 * specified anyway.
2244f53e3ccSKenjiro Cho 		 */
2254f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
2264f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
2274f53e3ccSKenjiro Cho 		break;
2284f53e3ccSKenjiro Cho 
22930fa52a6SBrian Somers 	case DLT_PPP:
23030fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
23130fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
23230fa52a6SBrian Somers 		break;
23330fa52a6SBrian Somers 
234df8bae1dSRodney W. Grimes 	default:
235df8bae1dSRodney W. Grimes 		return (EIO);
236df8bae1dSRodney W. Grimes 	}
237df8bae1dSRodney W. Grimes 
238df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
23901399f34SDavid Malone 
24001399f34SDavid Malone 	if (len - hlen > mtu)
24101399f34SDavid Malone 		return (EMSGSIZE);
24201399f34SDavid Malone 
243df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
244df8bae1dSRodney W. Grimes 		return (EIO);
245df8bae1dSRodney W. Grimes 
246963e4c2aSGarrett Wollman 	if (len > MHLEN) {
247a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
24824a229f4SSam Leffler 	} else {
249a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
250df8bae1dSRodney W. Grimes 	}
25124a229f4SSam Leffler 	if (m == NULL)
25224a229f4SSam Leffler 		return (ENOBUFS);
253963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
254963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
255df8bae1dSRodney W. Grimes 	*mp = m;
25624a229f4SSam Leffler 
25793e39f0bSChristian S.J. Peron 	if (m->m_len < hlen) {
25893e39f0bSChristian S.J. Peron 		error = EPERM;
25993e39f0bSChristian S.J. Peron 		goto bad;
26093e39f0bSChristian S.J. Peron 	}
26193e39f0bSChristian S.J. Peron 
26293e39f0bSChristian S.J. Peron 	error = uiomove(mtod(m, u_char *), len, uio);
26393e39f0bSChristian S.J. Peron 	if (error)
26493e39f0bSChristian S.J. Peron 		goto bad;
26593e39f0bSChristian S.J. Peron 
26693e39f0bSChristian S.J. Peron 	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
26793e39f0bSChristian S.J. Peron 	if (slen == 0) {
26893e39f0bSChristian S.J. Peron 		error = EPERM;
26993e39f0bSChristian S.J. Peron 		goto bad;
27093e39f0bSChristian S.J. Peron 	}
27193e39f0bSChristian S.J. Peron 
272df8bae1dSRodney W. Grimes 	/*
27393e39f0bSChristian S.J. Peron 	 * Make room for link header, and copy it to sockaddr
274df8bae1dSRodney W. Grimes 	 */
275df8bae1dSRodney W. Grimes 	if (hlen != 0) {
27693e39f0bSChristian S.J. Peron 		bcopy(m->m_data, sockp->sa_data, hlen);
2774f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
278df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
279df8bae1dSRodney W. Grimes #if BSD >= 199103
280df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
281df8bae1dSRodney W. Grimes #else
282df8bae1dSRodney W. Grimes 		m->m_off += hlen;
283df8bae1dSRodney W. Grimes #endif
284df8bae1dSRodney W. Grimes 	}
28593e39f0bSChristian S.J. Peron 
286df8bae1dSRodney W. Grimes 	return (0);
287df8bae1dSRodney W. Grimes bad:
288df8bae1dSRodney W. Grimes 	m_freem(m);
289df8bae1dSRodney W. Grimes 	return (error);
290df8bae1dSRodney W. Grimes }
291df8bae1dSRodney W. Grimes 
292df8bae1dSRodney W. Grimes /*
293df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
294df8bae1dSRodney W. Grimes  */
295df8bae1dSRodney W. Grimes static void
296df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
297df8bae1dSRodney W. Grimes 	struct bpf_d *d;
298df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
299df8bae1dSRodney W. Grimes {
300df8bae1dSRodney W. Grimes 	/*
301df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
302df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
303df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
304df8bae1dSRodney W. Grimes 	 */
305e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
306df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
3074a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
308df8bae1dSRodney W. Grimes 
30969f7644bSChristian S.J. Peron 	bpf_bpfd_cnt++;
31024a229f4SSam Leffler 	*bp->bif_driverp = bp;
311e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
312df8bae1dSRodney W. Grimes }
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes /*
315df8bae1dSRodney W. Grimes  * Detach a file from its interface.
316df8bae1dSRodney W. Grimes  */
317df8bae1dSRodney W. Grimes static void
318df8bae1dSRodney W. Grimes bpf_detachd(d)
319df8bae1dSRodney W. Grimes 	struct bpf_d *d;
320df8bae1dSRodney W. Grimes {
3216e891d64SPoul-Henning Kamp 	int error;
322df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
32346448b5aSRobert Watson 	struct ifnet *ifp;
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
32646448b5aSRobert Watson 	BPFIF_LOCK(bp);
32746448b5aSRobert Watson 	BPFD_LOCK(d);
32846448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
32946448b5aSRobert Watson 
33046448b5aSRobert Watson 	/*
33146448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
33246448b5aSRobert Watson 	 */
33346448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
33446448b5aSRobert Watson 
33569f7644bSChristian S.J. Peron 	bpf_bpfd_cnt--;
33646448b5aSRobert Watson 	/*
33746448b5aSRobert Watson 	 * Let the driver know that there are no more listeners.
33846448b5aSRobert Watson 	 */
33946448b5aSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
34046448b5aSRobert Watson 		*bp->bif_driverp = NULL;
34146448b5aSRobert Watson 
342572bde2aSRobert Watson 	d->bd_bif = NULL;
34346448b5aSRobert Watson 	BPFD_UNLOCK(d);
34446448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
34546448b5aSRobert Watson 
346df8bae1dSRodney W. Grimes 	/*
347df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
348df8bae1dSRodney W. Grimes 	 * If so, turn it off.
349df8bae1dSRodney W. Grimes 	 */
350df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
351df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
35246448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
3536e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
354df8bae1dSRodney W. Grimes 			/*
3556e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
356df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
357df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
358df8bae1dSRodney W. Grimes 			 * take it out.
359df8bae1dSRodney W. Grimes 			 */
3608eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
3618eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
3626e891d64SPoul-Henning Kamp 		}
363df8bae1dSRodney W. Grimes 	}
364df8bae1dSRodney W. Grimes }
365df8bae1dSRodney W. Grimes 
366df8bae1dSRodney W. Grimes /*
367df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
368df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
369df8bae1dSRodney W. Grimes  */
370df8bae1dSRodney W. Grimes /* ARGSUSED */
37187f6c662SJulian Elischer static	int
372b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
37389c9c53dSPoul-Henning Kamp 	struct cdev *dev;
37460039670SBruce Evans 	int flags;
37560039670SBruce Evans 	int fmt;
376b40ce416SJulian Elischer 	struct thread *td;
377df8bae1dSRodney W. Grimes {
378e7bb21b3SJonathan Lemon 	struct bpf_d *d;
379df8bae1dSRodney W. Grimes 
380e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
381bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
382df8bae1dSRodney W. Grimes 	/*
383df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
384df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
385df8bae1dSRodney W. Grimes 	 */
386d17d8184SRobert Watson 	if (d != NULL) {
387e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
388df8bae1dSRodney W. Grimes 		return (EBUSY);
389e7bb21b3SJonathan Lemon 	}
390e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
391e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
392e7bb21b3SJonathan Lemon 
393d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
394b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
395b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
396a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
397bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
398df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
39900a83887SPaul Traina 	d->bd_sig = SIGIO;
4008ed3828cSRobert Watson 	d->bd_seesent = 1;
40169f7644bSChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
40282f4445dSRobert Watson #ifdef MAC
40382f4445dSRobert Watson 	mac_init_bpfdesc(d);
40482f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
40582f4445dSRobert Watson #endif
4066008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
40731199c84SGleb Smirnoff 	callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE);
408571dcd15SSuleiman Souhlal 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
409df8bae1dSRodney W. Grimes 
410df8bae1dSRodney W. Grimes 	return (0);
411df8bae1dSRodney W. Grimes }
412df8bae1dSRodney W. Grimes 
413df8bae1dSRodney W. Grimes /*
414df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
415df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
416df8bae1dSRodney W. Grimes  */
417df8bae1dSRodney W. Grimes /* ARGSUSED */
41887f6c662SJulian Elischer static	int
419b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
42089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
42160039670SBruce Evans 	int flags;
42260039670SBruce Evans 	int fmt;
423b40ce416SJulian Elischer 	struct thread *td;
424df8bae1dSRodney W. Grimes {
425e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
426df8bae1dSRodney W. Grimes 
42781bda851SJohn Polstra 	BPFD_LOCK(d);
42881bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
42981bda851SJohn Polstra 		callout_stop(&d->bd_callout);
43081bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
43181bda851SJohn Polstra 	BPFD_UNLOCK(d);
432e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
433e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
434df8bae1dSRodney W. Grimes 	if (d->bd_bif)
435df8bae1dSRodney W. Grimes 		bpf_detachd(d);
436e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
4374549709fSBrian Feldman 	selwakeuppri(&d->bd_sel, PRINET);
43882f4445dSRobert Watson #ifdef MAC
43982f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
44082f4445dSRobert Watson #endif /* MAC */
441ad3b9257SJohn-Mark Gurney 	knlist_destroy(&d->bd_sel.si_note);
442df8bae1dSRodney W. Grimes 	bpf_freed(d);
443d17d8184SRobert Watson 	dev->si_drv1 = NULL;
444d722be54SLuigi Rizzo 	free(d, M_BPF);
445df8bae1dSRodney W. Grimes 
446df8bae1dSRodney W. Grimes 	return (0);
447df8bae1dSRodney W. Grimes }
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes /*
451df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
452df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
453df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
454df8bae1dSRodney W. Grimes  */
455df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
456df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
457df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
458df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
459df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
460d17d8184SRobert Watson 	(d)->bd_fbuf = NULL;
461df8bae1dSRodney W. Grimes /*
462df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
463df8bae1dSRodney W. Grimes  */
46487f6c662SJulian Elischer static	int
46560039670SBruce Evans bpfread(dev, uio, ioflag)
46689c9c53dSPoul-Henning Kamp 	struct cdev *dev;
4678994a245SDag-Erling Smørgrav 	struct uio *uio;
46860039670SBruce Evans 	int ioflag;
469df8bae1dSRodney W. Grimes {
470e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
47181bda851SJohn Polstra 	int timed_out;
472df8bae1dSRodney W. Grimes 	int error;
473df8bae1dSRodney W. Grimes 
474df8bae1dSRodney W. Grimes 	/*
475df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
476df8bae1dSRodney W. Grimes 	 * as kernel buffers.
477df8bae1dSRodney W. Grimes 	 */
478df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
479df8bae1dSRodney W. Grimes 		return (EINVAL);
480df8bae1dSRodney W. Grimes 
481e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
48281bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
48381bda851SJohn Polstra 		callout_stop(&d->bd_callout);
48481bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
48581bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
486df8bae1dSRodney W. Grimes 	/*
487df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
488df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
489df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
490df8bae1dSRodney W. Grimes 	 */
491572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
49281bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
493df8bae1dSRodney W. Grimes 			/*
494df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
495df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
496df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
497df8bae1dSRodney W. Grimes 			 */
498df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
499df8bae1dSRodney W. Grimes 			break;
500df8bae1dSRodney W. Grimes 		}
501de5d9935SRobert Watson 
502de5d9935SRobert Watson 		/*
503de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
504de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
505de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
506de5d9935SRobert Watson 		 * it before using it again.
507de5d9935SRobert Watson 		 */
508de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
509e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
510de5d9935SRobert Watson 			return (ENXIO);
511de5d9935SRobert Watson 		}
512de5d9935SRobert Watson 
513e76eee55SPoul-Henning Kamp 		if (ioflag & O_NONBLOCK) {
514e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
515fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
516fba3cfdeSJohn Polstra 		}
517521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
518e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
519df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
520e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
521df8bae1dSRodney W. Grimes 			return (error);
522df8bae1dSRodney W. Grimes 		}
523df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
524df8bae1dSRodney W. Grimes 			/*
525df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
526df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
527df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
528df8bae1dSRodney W. Grimes 			 */
529df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
530df8bae1dSRodney W. Grimes 				/*
531df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
532df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
533df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
534df8bae1dSRodney W. Grimes 				 */
535df8bae1dSRodney W. Grimes 				break;
536df8bae1dSRodney W. Grimes 
537df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
538e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
539df8bae1dSRodney W. Grimes 				return (0);
540df8bae1dSRodney W. Grimes 			}
541df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
542df8bae1dSRodney W. Grimes 			break;
543df8bae1dSRodney W. Grimes 		}
544df8bae1dSRodney W. Grimes 	}
545df8bae1dSRodney W. Grimes 	/*
546df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
547df8bae1dSRodney W. Grimes 	 */
548e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
549df8bae1dSRodney W. Grimes 
550df8bae1dSRodney W. Grimes 	/*
551df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
552df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
553df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
554df8bae1dSRodney W. Grimes 	 */
555e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
556df8bae1dSRodney W. Grimes 
557e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
558df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
559572bde2aSRobert Watson 	d->bd_hbuf = NULL;
560df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
561e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes 	return (error);
564df8bae1dSRodney W. Grimes }
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes 
567df8bae1dSRodney W. Grimes /*
568df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
569df8bae1dSRodney W. Grimes  */
570e7bb21b3SJonathan Lemon static __inline void
571df8bae1dSRodney W. Grimes bpf_wakeup(d)
5728994a245SDag-Erling Smørgrav 	struct bpf_d *d;
573df8bae1dSRodney W. Grimes {
574a3272e3cSChristian S.J. Peron 
575a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
57681bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
57781bda851SJohn Polstra 		callout_stop(&d->bd_callout);
57881bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
57981bda851SJohn Polstra 	}
580521f364bSDag-Erling Smørgrav 	wakeup(d);
581831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
582f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
58300a83887SPaul Traina 
584512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
585ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
586df8bae1dSRodney W. Grimes }
587df8bae1dSRodney W. Grimes 
58881bda851SJohn Polstra static void
58981bda851SJohn Polstra bpf_timed_out(arg)
59081bda851SJohn Polstra 	void *arg;
59181bda851SJohn Polstra {
59281bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
59381bda851SJohn Polstra 
59481bda851SJohn Polstra 	BPFD_LOCK(d);
59581bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
59681bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
59781bda851SJohn Polstra 		if (d->bd_slen != 0)
59881bda851SJohn Polstra 			bpf_wakeup(d);
59981bda851SJohn Polstra 	}
60081bda851SJohn Polstra 	BPFD_UNLOCK(d);
60181bda851SJohn Polstra }
60281bda851SJohn Polstra 
60387f6c662SJulian Elischer static	int
60460039670SBruce Evans bpfwrite(dev, uio, ioflag)
60589c9c53dSPoul-Henning Kamp 	struct cdev *dev;
606df8bae1dSRodney W. Grimes 	struct uio *uio;
60760039670SBruce Evans 	int ioflag;
608df8bae1dSRodney W. Grimes {
609e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
610df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
611df8bae1dSRodney W. Grimes 	struct mbuf *m;
612e7bb21b3SJonathan Lemon 	int error;
6138240bf1eSRobert Watson 	struct sockaddr dst;
614df8bae1dSRodney W. Grimes 
615572bde2aSRobert Watson 	if (d->bd_bif == NULL)
616df8bae1dSRodney W. Grimes 		return (ENXIO);
617df8bae1dSRodney W. Grimes 
618df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
619df8bae1dSRodney W. Grimes 
6203518d220SSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0)
6213518d220SSam Leffler 		return (ENETDOWN);
6223518d220SSam Leffler 
623df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
624df8bae1dSRodney W. Grimes 		return (0);
625df8bae1dSRodney W. Grimes 
6268240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
62793e39f0bSChristian S.J. Peron 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu,
62893e39f0bSChristian S.J. Peron 	    &m, &dst, d->bd_wfilter);
629df8bae1dSRodney W. Grimes 	if (error)
630df8bae1dSRodney W. Grimes 		return (error);
631df8bae1dSRodney W. Grimes 
632114ae644SMike Smith 	if (d->bd_hdrcmplt)
633114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
634114ae644SMike Smith 
63582f4445dSRobert Watson #ifdef MAC
636f747d2ddSRobert Watson 	BPFD_LOCK(d);
63782f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
638f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
63982f4445dSRobert Watson #endif
640b8f9429dSRobert Watson 	NET_LOCK_GIANT();
641572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
642b8f9429dSRobert Watson 	NET_UNLOCK_GIANT();
643df8bae1dSRodney W. Grimes 	/*
644df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
645df8bae1dSRodney W. Grimes 	 */
646df8bae1dSRodney W. Grimes 	return (error);
647df8bae1dSRodney W. Grimes }
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes /*
650df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
651e7bb21b3SJonathan Lemon  * receive and drop counts.
652df8bae1dSRodney W. Grimes  */
653df8bae1dSRodney W. Grimes static void
654df8bae1dSRodney W. Grimes reset_d(d)
655df8bae1dSRodney W. Grimes 	struct bpf_d *d;
656df8bae1dSRodney W. Grimes {
657e7bb21b3SJonathan Lemon 
658e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
659df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
660df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
661df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
662572bde2aSRobert Watson 		d->bd_hbuf = NULL;
663df8bae1dSRodney W. Grimes 	}
664df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
665df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
666df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
667df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
66869f7644bSChristian S.J. Peron 	d->bd_fcount = 0;
669df8bae1dSRodney W. Grimes }
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes /*
672df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
673df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
674df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
675df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
67693e39f0bSChristian S.J. Peron  *  BIOCSETWF		Set ethernet write filter.
677df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
678df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
679df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
680df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
681df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
682df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
683df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
684df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
685df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
686df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
687114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
688114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6898ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6908ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
69193e39f0bSChristian S.J. Peron  *  BIOCLOCK		Set "locked" flag
692df8bae1dSRodney W. Grimes  */
693df8bae1dSRodney W. Grimes /* ARGSUSED */
69487f6c662SJulian Elischer static	int
695b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
69689c9c53dSPoul-Henning Kamp 	struct cdev *dev;
697ecbb00a2SDoug Rabson 	u_long cmd;
698df8bae1dSRodney W. Grimes 	caddr_t addr;
69960039670SBruce Evans 	int flags;
700b40ce416SJulian Elischer 	struct thread *td;
701df8bae1dSRodney W. Grimes {
702e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
703e7bb21b3SJonathan Lemon 	int error = 0;
704df8bae1dSRodney W. Grimes 
705b75a24a0SChristian S.J. Peron 	/*
706b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
707b75a24a0SChristian S.J. Peron 	 */
70881bda851SJohn Polstra 	BPFD_LOCK(d);
709cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
71081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
71181bda851SJohn Polstra 		callout_stop(&d->bd_callout);
71281bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
71381bda851SJohn Polstra 	BPFD_UNLOCK(d);
71481bda851SJohn Polstra 
71593e39f0bSChristian S.J. Peron 	if (d->bd_locked == 1) {
71693e39f0bSChristian S.J. Peron 		switch (cmd) {
71793e39f0bSChristian S.J. Peron 		case BIOCGBLEN:
71893e39f0bSChristian S.J. Peron 		case BIOCFLUSH:
71993e39f0bSChristian S.J. Peron 		case BIOCGDLT:
72093e39f0bSChristian S.J. Peron 		case BIOCGDLTLIST:
72193e39f0bSChristian S.J. Peron 		case BIOCGETIF:
72293e39f0bSChristian S.J. Peron 		case BIOCGRTIMEOUT:
72393e39f0bSChristian S.J. Peron 		case BIOCGSTATS:
72493e39f0bSChristian S.J. Peron 		case BIOCVERSION:
72593e39f0bSChristian S.J. Peron 		case BIOCGRSIG:
72693e39f0bSChristian S.J. Peron 		case BIOCGHDRCMPLT:
72793e39f0bSChristian S.J. Peron 		case FIONREAD:
72893e39f0bSChristian S.J. Peron 		case BIOCLOCK:
72993e39f0bSChristian S.J. Peron 		case BIOCSRTIMEOUT:
73093e39f0bSChristian S.J. Peron 		case BIOCIMMEDIATE:
73193e39f0bSChristian S.J. Peron 		case TIOCGPGRP:
73293e39f0bSChristian S.J. Peron 			break;
73393e39f0bSChristian S.J. Peron 		default:
73493e39f0bSChristian S.J. Peron 			return (EPERM);
73593e39f0bSChristian S.J. Peron 		}
73693e39f0bSChristian S.J. Peron 	}
737df8bae1dSRodney W. Grimes 	switch (cmd) {
738df8bae1dSRodney W. Grimes 
739df8bae1dSRodney W. Grimes 	default:
740df8bae1dSRodney W. Grimes 		error = EINVAL;
741df8bae1dSRodney W. Grimes 		break;
742df8bae1dSRodney W. Grimes 
743df8bae1dSRodney W. Grimes 	/*
744df8bae1dSRodney W. Grimes 	 * Check for read packet available.
745df8bae1dSRodney W. Grimes 	 */
746df8bae1dSRodney W. Grimes 	case FIONREAD:
747df8bae1dSRodney W. Grimes 		{
748df8bae1dSRodney W. Grimes 			int n;
749df8bae1dSRodney W. Grimes 
750e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
751df8bae1dSRodney W. Grimes 			n = d->bd_slen;
752df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
753df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
754e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
755df8bae1dSRodney W. Grimes 
756df8bae1dSRodney W. Grimes 			*(int *)addr = n;
757df8bae1dSRodney W. Grimes 			break;
758df8bae1dSRodney W. Grimes 		}
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
761df8bae1dSRodney W. Grimes 		{
762df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
763df8bae1dSRodney W. Grimes 
764572bde2aSRobert Watson 			if (d->bd_bif == NULL)
765df8bae1dSRodney W. Grimes 				error = EINVAL;
766df8bae1dSRodney W. Grimes 			else {
767df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
768df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
769df8bae1dSRodney W. Grimes 			}
770df8bae1dSRodney W. Grimes 			break;
771df8bae1dSRodney W. Grimes 		}
772df8bae1dSRodney W. Grimes 
773df8bae1dSRodney W. Grimes 	/*
774df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
775df8bae1dSRodney W. Grimes 	 */
776df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
777df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
778df8bae1dSRodney W. Grimes 		break;
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes 	/*
781df8bae1dSRodney W. Grimes 	 * Set buffer length.
782df8bae1dSRodney W. Grimes 	 */
783df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
784572bde2aSRobert Watson 		if (d->bd_bif != NULL)
785df8bae1dSRodney W. Grimes 			error = EINVAL;
786df8bae1dSRodney W. Grimes 		else {
7878994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
788df8bae1dSRodney W. Grimes 
789eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
790eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
791df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
792df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
793df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
794df8bae1dSRodney W. Grimes 		}
795df8bae1dSRodney W. Grimes 		break;
796df8bae1dSRodney W. Grimes 
797df8bae1dSRodney W. Grimes 	/*
798df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
799df8bae1dSRodney W. Grimes 	 */
800df8bae1dSRodney W. Grimes 	case BIOCSETF:
80193e39f0bSChristian S.J. Peron 	case BIOCSETWF:
80293e39f0bSChristian S.J. Peron 		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
803df8bae1dSRodney W. Grimes 		break;
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes 	/*
806df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
807df8bae1dSRodney W. Grimes 	 */
808df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
809e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
810df8bae1dSRodney W. Grimes 		reset_d(d);
811e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
812df8bae1dSRodney W. Grimes 		break;
813df8bae1dSRodney W. Grimes 
814df8bae1dSRodney W. Grimes 	/*
815df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
816df8bae1dSRodney W. Grimes 	 */
817df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
818572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
819df8bae1dSRodney W. Grimes 			/*
820df8bae1dSRodney W. Grimes 			 * No interface attached yet.
821df8bae1dSRodney W. Grimes 			 */
822df8bae1dSRodney W. Grimes 			error = EINVAL;
823df8bae1dSRodney W. Grimes 			break;
824df8bae1dSRodney W. Grimes 		}
825df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
826e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
827df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
828e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
829df8bae1dSRodney W. Grimes 			if (error == 0)
830df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
831df8bae1dSRodney W. Grimes 		}
832df8bae1dSRodney W. Grimes 		break;
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 	/*
8358eab61f3SSam Leffler 	 * Get current data link type.
836df8bae1dSRodney W. Grimes 	 */
837df8bae1dSRodney W. Grimes 	case BIOCGDLT:
838572bde2aSRobert Watson 		if (d->bd_bif == NULL)
839df8bae1dSRodney W. Grimes 			error = EINVAL;
840df8bae1dSRodney W. Grimes 		else
841df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
842df8bae1dSRodney W. Grimes 		break;
843df8bae1dSRodney W. Grimes 
844df8bae1dSRodney W. Grimes 	/*
8458eab61f3SSam Leffler 	 * Get a list of supported data link types.
8468eab61f3SSam Leffler 	 */
8478eab61f3SSam Leffler 	case BIOCGDLTLIST:
848572bde2aSRobert Watson 		if (d->bd_bif == NULL)
8498eab61f3SSam Leffler 			error = EINVAL;
8508eab61f3SSam Leffler 		else
8518eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
8528eab61f3SSam Leffler 		break;
8538eab61f3SSam Leffler 
8548eab61f3SSam Leffler 	/*
8558eab61f3SSam Leffler 	 * Set data link type.
8568eab61f3SSam Leffler 	 */
8578eab61f3SSam Leffler 	case BIOCSDLT:
858572bde2aSRobert Watson 		if (d->bd_bif == NULL)
8598eab61f3SSam Leffler 			error = EINVAL;
8608eab61f3SSam Leffler 		else
8618eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
8628eab61f3SSam Leffler 		break;
8638eab61f3SSam Leffler 
8648eab61f3SSam Leffler 	/*
8651dd0feaaSArchie Cobbs 	 * Get interface name.
866df8bae1dSRodney W. Grimes 	 */
867df8bae1dSRodney W. Grimes 	case BIOCGETIF:
868572bde2aSRobert Watson 		if (d->bd_bif == NULL)
869df8bae1dSRodney W. Grimes 			error = EINVAL;
8701dd0feaaSArchie Cobbs 		else {
8711dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
8721dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
8731dd0feaaSArchie Cobbs 
8749bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
8759bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
8761dd0feaaSArchie Cobbs 		}
877df8bae1dSRodney W. Grimes 		break;
878df8bae1dSRodney W. Grimes 
879df8bae1dSRodney W. Grimes 	/*
880df8bae1dSRodney W. Grimes 	 * Set interface.
881df8bae1dSRodney W. Grimes 	 */
882df8bae1dSRodney W. Grimes 	case BIOCSETIF:
883df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
884df8bae1dSRodney W. Grimes 		break;
885df8bae1dSRodney W. Grimes 
886df8bae1dSRodney W. Grimes 	/*
887df8bae1dSRodney W. Grimes 	 * Set read timeout.
888df8bae1dSRodney W. Grimes 	 */
889df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
890df8bae1dSRodney W. Grimes 		{
891df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
892df8bae1dSRodney W. Grimes 
893bdc2cdc5SAlexander Langer 			/*
894bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
895bdc2cdc5SAlexander Langer 			 * a one-shot timer.
896bdc2cdc5SAlexander Langer 			 */
897bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
898bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
899df8bae1dSRodney W. Grimes 			break;
900df8bae1dSRodney W. Grimes 		}
901df8bae1dSRodney W. Grimes 
902df8bae1dSRodney W. Grimes 	/*
903df8bae1dSRodney W. Grimes 	 * Get read timeout.
904df8bae1dSRodney W. Grimes 	 */
905df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
906df8bae1dSRodney W. Grimes 		{
907df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
908df8bae1dSRodney W. Grimes 
909bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
910bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
911df8bae1dSRodney W. Grimes 			break;
912df8bae1dSRodney W. Grimes 		}
913df8bae1dSRodney W. Grimes 
914df8bae1dSRodney W. Grimes 	/*
915df8bae1dSRodney W. Grimes 	 * Get packet stats.
916df8bae1dSRodney W. Grimes 	 */
917df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
918df8bae1dSRodney W. Grimes 		{
919df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
922df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
923df8bae1dSRodney W. Grimes 			break;
924df8bae1dSRodney W. Grimes 		}
925df8bae1dSRodney W. Grimes 
926df8bae1dSRodney W. Grimes 	/*
927df8bae1dSRodney W. Grimes 	 * Set immediate mode.
928df8bae1dSRodney W. Grimes 	 */
929df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
930df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
931df8bae1dSRodney W. Grimes 		break;
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 	case BIOCVERSION:
934df8bae1dSRodney W. Grimes 		{
935df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
938df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
939df8bae1dSRodney W. Grimes 			break;
940df8bae1dSRodney W. Grimes 		}
94100a83887SPaul Traina 
942114ae644SMike Smith 	/*
943114ae644SMike Smith 	 * Get "header already complete" flag
944114ae644SMike Smith 	 */
945114ae644SMike Smith 	case BIOCGHDRCMPLT:
946114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
947114ae644SMike Smith 		break;
948114ae644SMike Smith 
94993e39f0bSChristian S.J. Peron 	case BIOCLOCK:
95093e39f0bSChristian S.J. Peron 		d->bd_locked = 1;
95193e39f0bSChristian S.J. Peron 		break;
952114ae644SMike Smith 	/*
953114ae644SMike Smith 	 * Set "header already complete" flag
954114ae644SMike Smith 	 */
955114ae644SMike Smith 	case BIOCSHDRCMPLT:
956114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
957114ae644SMike Smith 		break;
958114ae644SMike Smith 
9598ed3828cSRobert Watson 	/*
9608ed3828cSRobert Watson 	 * Get "see sent packets" flag
9618ed3828cSRobert Watson 	 */
9628ed3828cSRobert Watson 	case BIOCGSEESENT:
9638ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
9648ed3828cSRobert Watson 		break;
9658ed3828cSRobert Watson 
9668ed3828cSRobert Watson 	/*
9678ed3828cSRobert Watson 	 * Set "see sent packets" flag
9688ed3828cSRobert Watson 	 */
9698ed3828cSRobert Watson 	case BIOCSSEESENT:
9708ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
9718ed3828cSRobert Watson 		break;
9728ed3828cSRobert Watson 
97300a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
97400a83887SPaul Traina 		break;
97500a83887SPaul Traina 
97600a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
97700a83887SPaul Traina 		d->bd_async = *(int *)addr;
97800a83887SPaul Traina 		break;
97900a83887SPaul Traina 
980831d27a9SDon Lewis 	case FIOSETOWN:
981831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
98200a83887SPaul Traina 		break;
98300a83887SPaul Traina 
984831d27a9SDon Lewis 	case FIOGETOWN:
98591e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
986831d27a9SDon Lewis 		break;
987831d27a9SDon Lewis 
988831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
989831d27a9SDon Lewis 	case TIOCSPGRP:
990831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
991831d27a9SDon Lewis 		break;
992831d27a9SDon Lewis 
993831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
99400a83887SPaul Traina 	case TIOCGPGRP:
99591e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
99600a83887SPaul Traina 		break;
99700a83887SPaul Traina 
99800a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
99900a83887SPaul Traina 		{
100000a83887SPaul Traina 			u_int sig;
100100a83887SPaul Traina 
100200a83887SPaul Traina 			sig = *(u_int *)addr;
100300a83887SPaul Traina 
100400a83887SPaul Traina 			if (sig >= NSIG)
100500a83887SPaul Traina 				error = EINVAL;
100600a83887SPaul Traina 			else
100700a83887SPaul Traina 				d->bd_sig = sig;
100800a83887SPaul Traina 			break;
100900a83887SPaul Traina 		}
101000a83887SPaul Traina 	case BIOCGRSIG:
101100a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
101200a83887SPaul Traina 		break;
1013df8bae1dSRodney W. Grimes 	}
1014df8bae1dSRodney W. Grimes 	return (error);
1015df8bae1dSRodney W. Grimes }
1016df8bae1dSRodney W. Grimes 
1017df8bae1dSRodney W. Grimes /*
1018df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
1019df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
1020df8bae1dSRodney W. Grimes  */
1021f708ef1bSPoul-Henning Kamp static int
102293e39f0bSChristian S.J. Peron bpf_setf(d, fp, cmd)
1023df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1024df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
102593e39f0bSChristian S.J. Peron 	u_long cmd;
1026df8bae1dSRodney W. Grimes {
1027df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
102893e39f0bSChristian S.J. Peron 	u_int wfilter, flen, size;
1029ae275efcSJung-uk Kim #if BPF_JITTER
1030ae275efcSJung-uk Kim 	bpf_jit_filter *ofunc;
1031ae275efcSJung-uk Kim #endif
1032df8bae1dSRodney W. Grimes 
103393e39f0bSChristian S.J. Peron 	if (cmd == BIOCSETWF) {
103493e39f0bSChristian S.J. Peron 		old = d->bd_wfilter;
103593e39f0bSChristian S.J. Peron 		wfilter = 1;
1036ae275efcSJung-uk Kim #if BPF_JITTER
1037ae275efcSJung-uk Kim 		ofunc = NULL;
1038ae275efcSJung-uk Kim #endif
103993e39f0bSChristian S.J. Peron 	} else {
104093e39f0bSChristian S.J. Peron 		wfilter = 0;
104193e39f0bSChristian S.J. Peron 		old = d->bd_rfilter;
1042ae275efcSJung-uk Kim #if BPF_JITTER
1043ae275efcSJung-uk Kim 		ofunc = d->bd_bfilter;
1044ae275efcSJung-uk Kim #endif
104593e39f0bSChristian S.J. Peron 	}
1046572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
1047df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
1048df8bae1dSRodney W. Grimes 			return (EINVAL);
1049e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
105093e39f0bSChristian S.J. Peron 		if (wfilter)
105193e39f0bSChristian S.J. Peron 			d->bd_wfilter = NULL;
1052ae275efcSJung-uk Kim 		else {
105393e39f0bSChristian S.J. Peron 			d->bd_rfilter = NULL;
1054ae275efcSJung-uk Kim #if BPF_JITTER
1055ae275efcSJung-uk Kim 			d->bd_bfilter = NULL;
1056ae275efcSJung-uk Kim #endif
1057ae275efcSJung-uk Kim 		}
1058df8bae1dSRodney W. Grimes 		reset_d(d);
1059e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1060572bde2aSRobert Watson 		if (old != NULL)
1061bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1062ae275efcSJung-uk Kim #if BPF_JITTER
1063ae275efcSJung-uk Kim 		if (ofunc != NULL)
1064ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1065ae275efcSJung-uk Kim #endif
1066df8bae1dSRodney W. Grimes 		return (0);
1067df8bae1dSRodney W. Grimes 	}
1068df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
10690eb20604SChristian S.J. Peron 	if (flen > bpf_maxinsns)
1070df8bae1dSRodney W. Grimes 		return (EINVAL);
1071df8bae1dSRodney W. Grimes 
1072df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
1073a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1074df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1075df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
1076e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
107793e39f0bSChristian S.J. Peron 		if (wfilter)
107893e39f0bSChristian S.J. Peron 			d->bd_wfilter = fcode;
1079ae275efcSJung-uk Kim 		else {
108093e39f0bSChristian S.J. Peron 			d->bd_rfilter = fcode;
1081ae275efcSJung-uk Kim #if BPF_JITTER
1082ae275efcSJung-uk Kim 			d->bd_bfilter = bpf_jitter(fcode, flen);
1083ae275efcSJung-uk Kim #endif
1084ae275efcSJung-uk Kim 		}
1085df8bae1dSRodney W. Grimes 		reset_d(d);
1086e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1087572bde2aSRobert Watson 		if (old != NULL)
1088bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1089ae275efcSJung-uk Kim #if BPF_JITTER
1090ae275efcSJung-uk Kim 		if (ofunc != NULL)
1091ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1092ae275efcSJung-uk Kim #endif
1093df8bae1dSRodney W. Grimes 
1094df8bae1dSRodney W. Grimes 		return (0);
1095df8bae1dSRodney W. Grimes 	}
1096bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
1097df8bae1dSRodney W. Grimes 	return (EINVAL);
1098df8bae1dSRodney W. Grimes }
1099df8bae1dSRodney W. Grimes 
1100df8bae1dSRodney W. Grimes /*
1101df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
1102df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
1103df8bae1dSRodney W. Grimes  * Return an errno or 0.
1104df8bae1dSRodney W. Grimes  */
1105df8bae1dSRodney W. Grimes static int
1106df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
1107df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1108df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
1109df8bae1dSRodney W. Grimes {
1110df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
1111e7bb21b3SJonathan Lemon 	int error;
11129b44ff22SGarrett Wollman 	struct ifnet *theywant;
1113df8bae1dSRodney W. Grimes 
11149b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
1115572bde2aSRobert Watson 	if (theywant == NULL)
11169b44ff22SGarrett Wollman 		return ENXIO;
11179b44ff22SGarrett Wollman 
1118df8bae1dSRodney W. Grimes 	/*
1119df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
1120df8bae1dSRodney W. Grimes 	 */
1121e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
11224a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1123df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
1124df8bae1dSRodney W. Grimes 
1125572bde2aSRobert Watson 		if (ifp == NULL || ifp != theywant)
1126df8bae1dSRodney W. Grimes 			continue;
112724a229f4SSam Leffler 		/* skip additional entry */
1128fc74a9f9SBrooks Davis 		if (bp->bif_driverp != &ifp->if_bpf)
112924a229f4SSam Leffler 			continue;
1130e7bb21b3SJonathan Lemon 
1131e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1132df8bae1dSRodney W. Grimes 		/*
1133df8bae1dSRodney W. Grimes 		 * We found the requested interface.
1134df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
1135df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
1136df8bae1dSRodney W. Grimes 		 * just flush the buffer.
1137df8bae1dSRodney W. Grimes 		 */
1138572bde2aSRobert Watson 		if (d->bd_sbuf == NULL) {
1139df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
1140df8bae1dSRodney W. Grimes 			if (error != 0)
1141df8bae1dSRodney W. Grimes 				return (error);
1142df8bae1dSRodney W. Grimes 		}
1143df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
1144df8bae1dSRodney W. Grimes 			if (d->bd_bif)
1145df8bae1dSRodney W. Grimes 				/*
1146df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
1147df8bae1dSRodney W. Grimes 				 */
1148df8bae1dSRodney W. Grimes 				bpf_detachd(d);
1149df8bae1dSRodney W. Grimes 
1150df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
1151df8bae1dSRodney W. Grimes 		}
1152e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1153df8bae1dSRodney W. Grimes 		reset_d(d);
1154e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1155df8bae1dSRodney W. Grimes 		return (0);
1156df8bae1dSRodney W. Grimes 	}
1157e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1158df8bae1dSRodney W. Grimes 	/* Not found. */
1159df8bae1dSRodney W. Grimes 	return (ENXIO);
1160df8bae1dSRodney W. Grimes }
1161df8bae1dSRodney W. Grimes 
1162df8bae1dSRodney W. Grimes /*
1163243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1164df8bae1dSRodney W. Grimes  *
1165df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1166df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1167df8bae1dSRodney W. Grimes  */
116837c84183SPoul-Henning Kamp static int
1169b40ce416SJulian Elischer bpfpoll(dev, events, td)
117089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1171243ac7d8SPeter Wemm 	int events;
1172b40ce416SJulian Elischer 	struct thread *td;
1173df8bae1dSRodney W. Grimes {
1174e7bb21b3SJonathan Lemon 	struct bpf_d *d;
11750832fc64SGarance A Drosehn 	int revents;
1176df8bae1dSRodney W. Grimes 
1177bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1178de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1179de5d9935SRobert Watson 		return (ENXIO);
1180de5d9935SRobert Watson 
1181b75a24a0SChristian S.J. Peron 	/*
1182b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1183b75a24a0SChristian S.J. Peron 	 */
11840832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1185e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1186cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
118775c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
118895aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1189243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
119081bda851SJohn Polstra 		else {
1191ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
119281bda851SJohn Polstra 			/* Start the read timeout if necessary. */
119381bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
119481bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
119581bda851SJohn Polstra 				    bpf_timed_out, d);
119681bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
119781bda851SJohn Polstra 			}
119881bda851SJohn Polstra 		}
119975c13541SPoul-Henning Kamp 	}
1200e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1201243ac7d8SPeter Wemm 	return (revents);
1202df8bae1dSRodney W. Grimes }
1203df8bae1dSRodney W. Grimes 
1204df8bae1dSRodney W. Grimes /*
120595aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
120695aab9ccSJohn-Mark Gurney  * reject all others.
120795aab9ccSJohn-Mark Gurney  */
120895aab9ccSJohn-Mark Gurney int
120995aab9ccSJohn-Mark Gurney bpfkqfilter(dev, kn)
121089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
121195aab9ccSJohn-Mark Gurney 	struct knote *kn;
121295aab9ccSJohn-Mark Gurney {
121395aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
121495aab9ccSJohn-Mark Gurney 
121595aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
121695aab9ccSJohn-Mark Gurney 		return (1);
121795aab9ccSJohn-Mark Gurney 
1218b75a24a0SChristian S.J. Peron 	/*
1219b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1220b75a24a0SChristian S.J. Peron 	 */
1221cb1d4f92SChristian S.J. Peron 	BPFD_LOCK(d);
1222b75a24a0SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
122395aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
122495aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
1225ad3b9257SJohn-Mark Gurney 	knlist_add(&d->bd_sel.si_note, kn, 0);
1226cb1d4f92SChristian S.J. Peron 	BPFD_UNLOCK(d);
122795aab9ccSJohn-Mark Gurney 
122895aab9ccSJohn-Mark Gurney 	return (0);
122995aab9ccSJohn-Mark Gurney }
123095aab9ccSJohn-Mark Gurney 
123195aab9ccSJohn-Mark Gurney static void
123295aab9ccSJohn-Mark Gurney filt_bpfdetach(kn)
123395aab9ccSJohn-Mark Gurney 	struct knote *kn;
123495aab9ccSJohn-Mark Gurney {
123595aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
123695aab9ccSJohn-Mark Gurney 
1237cb1d4f92SChristian S.J. Peron 	BPFD_LOCK(d);
1238ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
1239cb1d4f92SChristian S.J. Peron 	BPFD_UNLOCK(d);
124095aab9ccSJohn-Mark Gurney }
124195aab9ccSJohn-Mark Gurney 
124295aab9ccSJohn-Mark Gurney static int
124395aab9ccSJohn-Mark Gurney filt_bpfread(kn, hint)
124495aab9ccSJohn-Mark Gurney 	struct knote *kn;
124595aab9ccSJohn-Mark Gurney 	long hint;
124695aab9ccSJohn-Mark Gurney {
124795aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
124895aab9ccSJohn-Mark Gurney 	int ready;
124995aab9ccSJohn-Mark Gurney 
125086c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(d);
125195aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
125295aab9ccSJohn-Mark Gurney 	if (ready) {
125395aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
125495aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
125595aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
125695aab9ccSJohn-Mark Gurney 	}
125795aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
125895aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
125995aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
126095aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
126195aab9ccSJohn-Mark Gurney 	}
126295aab9ccSJohn-Mark Gurney 
126395aab9ccSJohn-Mark Gurney 	return (ready);
126495aab9ccSJohn-Mark Gurney }
126595aab9ccSJohn-Mark Gurney 
126695aab9ccSJohn-Mark Gurney /*
1267df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1268df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1269df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1270df8bae1dSRodney W. Grimes  * buffer.
1271df8bae1dSRodney W. Grimes  */
1272df8bae1dSRodney W. Grimes void
127324a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
127424a229f4SSam Leffler 	struct bpf_if *bp;
12758994a245SDag-Erling Smørgrav 	u_char *pkt;
12768994a245SDag-Erling Smørgrav 	u_int pktlen;
1277df8bae1dSRodney W. Grimes {
12788994a245SDag-Erling Smørgrav 	struct bpf_d *d;
12798994a245SDag-Erling Smørgrav 	u_int slen;
1280e7bb21b3SJonathan Lemon 
128146691dd8SRobert Watson 	/*
128246691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
128346691dd8SRobert Watson 	 * no descriptors attached.
128446691dd8SRobert Watson 	 */
12854a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
128646691dd8SRobert Watson 		return;
128746691dd8SRobert Watson 
1288e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
12894a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1290e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1291df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1292ae275efcSJung-uk Kim #ifdef BPF_JITTER
1293ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1294ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1295ae275efcSJung-uk Kim 		else
1296ae275efcSJung-uk Kim #endif
129793e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1298ec272d87SRobert Watson 		if (slen != 0) {
129969f7644bSChristian S.J. Peron 			d->bd_fcount++;
1300ec272d87SRobert Watson #ifdef MAC
130124a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1302ec272d87SRobert Watson #endif
1303df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1304ec272d87SRobert Watson 		}
1305e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1306df8bae1dSRodney W. Grimes 	}
1307e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1308df8bae1dSRodney W. Grimes }
1309df8bae1dSRodney W. Grimes 
1310df8bae1dSRodney W. Grimes /*
1311df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1312df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1313df8bae1dSRodney W. Grimes  */
1314df8bae1dSRodney W. Grimes static void
1315df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1316df8bae1dSRodney W. Grimes 	const void *src_arg;
1317df8bae1dSRodney W. Grimes 	void *dst_arg;
13188994a245SDag-Erling Smørgrav 	size_t len;
1319df8bae1dSRodney W. Grimes {
13208994a245SDag-Erling Smørgrav 	const struct mbuf *m;
13218994a245SDag-Erling Smørgrav 	u_int count;
1322df8bae1dSRodney W. Grimes 	u_char *dst;
1323df8bae1dSRodney W. Grimes 
1324df8bae1dSRodney W. Grimes 	m = src_arg;
1325df8bae1dSRodney W. Grimes 	dst = dst_arg;
1326df8bae1dSRodney W. Grimes 	while (len > 0) {
1327572bde2aSRobert Watson 		if (m == NULL)
1328df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1329df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
13300453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1331df8bae1dSRodney W. Grimes 		m = m->m_next;
1332df8bae1dSRodney W. Grimes 		dst += count;
1333df8bae1dSRodney W. Grimes 		len -= count;
1334df8bae1dSRodney W. Grimes 	}
1335df8bae1dSRodney W. Grimes }
1336df8bae1dSRodney W. Grimes 
1337df8bae1dSRodney W. Grimes /*
1338df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1339df8bae1dSRodney W. Grimes  */
1340df8bae1dSRodney W. Grimes void
134124a229f4SSam Leffler bpf_mtap(bp, m)
134224a229f4SSam Leffler 	struct bpf_if *bp;
1343df8bae1dSRodney W. Grimes 	struct mbuf *m;
1344df8bae1dSRodney W. Grimes {
1345df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1346df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1347df8bae1dSRodney W. Grimes 
134846691dd8SRobert Watson 	/*
134946691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
135046691dd8SRobert Watson 	 * no descriptors attached.
135146691dd8SRobert Watson 	 */
13524a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
135346691dd8SRobert Watson 		return;
135446691dd8SRobert Watson 
1355f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
1356df8bae1dSRodney W. Grimes 
1357e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
13584a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
13598ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
13608ed3828cSRobert Watson 			continue;
1361e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1362df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1363ae275efcSJung-uk Kim #ifdef BPF_JITTER
1364ae275efcSJung-uk Kim 		/* XXX We cannot handle multiple mbufs. */
1365ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1366ae275efcSJung-uk Kim 		    m->m_next == NULL)
1367ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1368ae275efcSJung-uk Kim 			    pktlen, pktlen);
1369ae275efcSJung-uk Kim 		else
1370ae275efcSJung-uk Kim #endif
137193e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
13724ddfb531SChristian S.J. Peron 		if (slen != 0) {
137369f7644bSChristian S.J. Peron 			d->bd_fcount++;
13740c7fb534SRobert Watson #ifdef MAC
137524a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
13760c7fb534SRobert Watson #endif
13770c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
13780c7fb534SRobert Watson 				    bpf_mcopy);
13794ddfb531SChristian S.J. Peron 		}
1380e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1381df8bae1dSRodney W. Grimes 	}
1382e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1383df8bae1dSRodney W. Grimes }
1384df8bae1dSRodney W. Grimes 
1385df8bae1dSRodney W. Grimes /*
1386437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1387437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1388437ffe18SSam Leffler  */
1389437ffe18SSam Leffler void
1390437ffe18SSam Leffler bpf_mtap2(bp, data, dlen, m)
1391437ffe18SSam Leffler 	struct bpf_if *bp;
1392437ffe18SSam Leffler 	void *data;
1393437ffe18SSam Leffler 	u_int dlen;
1394437ffe18SSam Leffler 	struct mbuf *m;
1395437ffe18SSam Leffler {
1396437ffe18SSam Leffler 	struct mbuf mb;
1397437ffe18SSam Leffler 	struct bpf_d *d;
1398437ffe18SSam Leffler 	u_int pktlen, slen;
1399437ffe18SSam Leffler 
140046691dd8SRobert Watson 	/*
140146691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
140246691dd8SRobert Watson 	 * no descriptors attached.
140346691dd8SRobert Watson 	 */
14044a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
140546691dd8SRobert Watson 		return;
140646691dd8SRobert Watson 
1407437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1408437ffe18SSam Leffler 	/*
1409437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1410437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1411437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1412437ffe18SSam Leffler 	 */
1413437ffe18SSam Leffler 	mb.m_next = m;
1414437ffe18SSam Leffler 	mb.m_data = data;
1415437ffe18SSam Leffler 	mb.m_len = dlen;
1416437ffe18SSam Leffler 	pktlen += dlen;
1417437ffe18SSam Leffler 
1418437ffe18SSam Leffler 	BPFIF_LOCK(bp);
14194a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1420437ffe18SSam Leffler 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1421437ffe18SSam Leffler 			continue;
1422437ffe18SSam Leffler 		BPFD_LOCK(d);
1423437ffe18SSam Leffler 		++d->bd_rcount;
142493e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
14254ddfb531SChristian S.J. Peron 		if (slen != 0) {
142669f7644bSChristian S.J. Peron 			d->bd_fcount++;
1427437ffe18SSam Leffler #ifdef MAC
1428437ffe18SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1429437ffe18SSam Leffler #endif
1430437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
1431437ffe18SSam Leffler 				    bpf_mcopy);
14324ddfb531SChristian S.J. Peron 		}
1433437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1434437ffe18SSam Leffler 	}
1435437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1436437ffe18SSam Leffler }
1437437ffe18SSam Leffler 
1438437ffe18SSam Leffler /*
1439df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
14409e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1441df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1442df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1443df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1444df8bae1dSRodney W. Grimes  */
1445df8bae1dSRodney W. Grimes static void
1446df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
14478994a245SDag-Erling Smørgrav 	struct bpf_d *d;
14488994a245SDag-Erling Smørgrav 	u_char *pkt;
14498994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
14508994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1451df8bae1dSRodney W. Grimes {
14528994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
14538994a245SDag-Erling Smørgrav 	int totlen, curlen;
14548994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
14557819da79SJohn-Mark Gurney 	int do_wakeup = 0;
14569e610888SDag-Erling Smørgrav 
1457a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1458df8bae1dSRodney W. Grimes 	/*
1459df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1460df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1461df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1462df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1463df8bae1dSRodney W. Grimes 	 */
1464df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1465df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1466df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1467df8bae1dSRodney W. Grimes 
1468df8bae1dSRodney W. Grimes 	/*
1469df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1470df8bae1dSRodney W. Grimes 	 */
1471df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1472df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1473df8bae1dSRodney W. Grimes 		/*
1474df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1475df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1476df8bae1dSRodney W. Grimes 		 * pending reads.
1477df8bae1dSRodney W. Grimes 		 */
1478572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1479df8bae1dSRodney W. Grimes 			/*
1480df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1481df8bae1dSRodney W. Grimes 			 * so drop the packet.
1482df8bae1dSRodney W. Grimes 			 */
1483df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1484df8bae1dSRodney W. Grimes 			return;
1485df8bae1dSRodney W. Grimes 		}
1486df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
14877819da79SJohn-Mark Gurney 		do_wakeup = 1;
1488df8bae1dSRodney W. Grimes 		curlen = 0;
1489df8bae1dSRodney W. Grimes 	}
149081bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1491df8bae1dSRodney W. Grimes 		/*
149281bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
149381bda851SJohn Polstra 		 * already expired during a select call.  A packet
149481bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1495df8bae1dSRodney W. Grimes 		 */
14967819da79SJohn-Mark Gurney 		do_wakeup = 1;
1497df8bae1dSRodney W. Grimes 
1498df8bae1dSRodney W. Grimes 	/*
1499df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1500df8bae1dSRodney W. Grimes 	 */
1501df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1502df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1503df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1504df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1505df8bae1dSRodney W. Grimes 	/*
1506df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1507df8bae1dSRodney W. Grimes 	 */
1508df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1509df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
15107819da79SJohn-Mark Gurney 
15117819da79SJohn-Mark Gurney 	if (do_wakeup)
15127819da79SJohn-Mark Gurney 		bpf_wakeup(d);
1513df8bae1dSRodney W. Grimes }
1514df8bae1dSRodney W. Grimes 
1515df8bae1dSRodney W. Grimes /*
1516df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1517df8bae1dSRodney W. Grimes  */
1518df8bae1dSRodney W. Grimes static int
1519df8bae1dSRodney W. Grimes bpf_allocbufs(d)
15208994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1521df8bae1dSRodney W. Grimes {
1522a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1523572bde2aSRobert Watson 	if (d->bd_fbuf == NULL)
1524df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1525df8bae1dSRodney W. Grimes 
1526a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1527572bde2aSRobert Watson 	if (d->bd_sbuf == NULL) {
1528bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1529df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1530df8bae1dSRodney W. Grimes 	}
1531df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1532df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1533df8bae1dSRodney W. Grimes 	return (0);
1534df8bae1dSRodney W. Grimes }
1535df8bae1dSRodney W. Grimes 
1536df8bae1dSRodney W. Grimes /*
1537df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1538df8bae1dSRodney W. Grimes  * Called on close.
1539df8bae1dSRodney W. Grimes  */
1540df8bae1dSRodney W. Grimes static void
1541df8bae1dSRodney W. Grimes bpf_freed(d)
15428994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1543df8bae1dSRodney W. Grimes {
1544df8bae1dSRodney W. Grimes 	/*
1545df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1546df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1547df8bae1dSRodney W. Grimes 	 * free.
1548df8bae1dSRodney W. Grimes 	 */
1549572bde2aSRobert Watson 	if (d->bd_sbuf != NULL) {
1550bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1551572bde2aSRobert Watson 		if (d->bd_hbuf != NULL)
1552bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1553572bde2aSRobert Watson 		if (d->bd_fbuf != NULL)
1554bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1555df8bae1dSRodney W. Grimes 	}
1556ae275efcSJung-uk Kim 	if (d->bd_rfilter) {
155793e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_rfilter, M_BPF);
1558ae275efcSJung-uk Kim #ifdef BPF_JITTER
1559ae275efcSJung-uk Kim 		bpf_destroy_jit_filter(d->bd_bfilter);
1560ae275efcSJung-uk Kim #endif
1561ae275efcSJung-uk Kim 	}
156293e39f0bSChristian S.J. Peron 	if (d->bd_wfilter)
156393e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_wfilter, M_BPF);
1564e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1565df8bae1dSRodney W. Grimes }
1566df8bae1dSRodney W. Grimes 
1567df8bae1dSRodney W. Grimes /*
156824a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
156924a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1570df8bae1dSRodney W. Grimes  */
1571df8bae1dSRodney W. Grimes void
15729b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1573df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1574df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1575df8bae1dSRodney W. Grimes {
157624a229f4SSam Leffler 
157724a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
157824a229f4SSam Leffler }
157924a229f4SSam Leffler 
158024a229f4SSam Leffler /*
158124a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
158224a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
158324a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
158424a229f4SSam Leffler  * headers are not yet supporrted).
158524a229f4SSam Leffler  */
158624a229f4SSam Leffler void
158724a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
158824a229f4SSam Leffler 	struct ifnet *ifp;
158924a229f4SSam Leffler 	u_int dlt, hdrlen;
159024a229f4SSam Leffler 	struct bpf_if **driverp;
159124a229f4SSam Leffler {
1592df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
15936a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1594572bde2aSRobert Watson 	if (bp == NULL)
1595df8bae1dSRodney W. Grimes 		panic("bpfattach");
1596df8bae1dSRodney W. Grimes 
15974a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
159824a229f4SSam Leffler 	bp->bif_driverp = driverp;
1599df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1600df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
16016008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1602df8bae1dSRodney W. Grimes 
1603e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
16044a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1605e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1606df8bae1dSRodney W. Grimes 
1607572bde2aSRobert Watson 	*bp->bif_driverp = NULL;
1608df8bae1dSRodney W. Grimes 
1609df8bae1dSRodney W. Grimes 	/*
1610df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1611df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1612df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1613df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1614df8bae1dSRodney W. Grimes 	 */
1615df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1616df8bae1dSRodney W. Grimes 
16172eeab939SGarrett Wollman 	if (bootverbose)
161824a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1619df8bae1dSRodney W. Grimes }
162053ac6efbSJulian Elischer 
1621de5d9935SRobert Watson /*
1622de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1623de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1624de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1625de5d9935SRobert Watson  * ENXIO.
1626de5d9935SRobert Watson  */
1627de5d9935SRobert Watson void
1628de5d9935SRobert Watson bpfdetach(ifp)
1629de5d9935SRobert Watson 	struct ifnet *ifp;
1630de5d9935SRobert Watson {
16314a3feeaaSRobert Watson 	struct bpf_if	*bp;
1632de5d9935SRobert Watson 	struct bpf_d	*d;
1633de5d9935SRobert Watson 
1634de5d9935SRobert Watson 	/* Locate BPF interface information */
16358eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
16364a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1637de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1638de5d9935SRobert Watson 			break;
1639de5d9935SRobert Watson 	}
1640de5d9935SRobert Watson 
1641de5d9935SRobert Watson 	/* Interface wasn't attached */
1642d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1643e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
16449bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1645de5d9935SRobert Watson 		return;
1646de5d9935SRobert Watson 	}
1647de5d9935SRobert Watson 
16484a3feeaaSRobert Watson 	LIST_REMOVE(bp, bif_next);
16498eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1650de5d9935SRobert Watson 
16514a3feeaaSRobert Watson 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1652e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1653e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1654e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1655e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1656e7bb21b3SJonathan Lemon 	}
1657e7bb21b3SJonathan Lemon 
1658e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1659de5d9935SRobert Watson 	free(bp, M_BPF);
16608eab61f3SSam Leffler }
1661de5d9935SRobert Watson 
16628eab61f3SSam Leffler /*
16638eab61f3SSam Leffler  * Get a list of available data link type of the interface.
16648eab61f3SSam Leffler  */
16658eab61f3SSam Leffler static int
16668eab61f3SSam Leffler bpf_getdltlist(d, bfl)
16678eab61f3SSam Leffler 	struct bpf_d *d;
16688eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
16698eab61f3SSam Leffler {
16708eab61f3SSam Leffler 	int n, error;
16718eab61f3SSam Leffler 	struct ifnet *ifp;
16728eab61f3SSam Leffler 	struct bpf_if *bp;
16738eab61f3SSam Leffler 
16748eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
16758eab61f3SSam Leffler 	n = 0;
16768eab61f3SSam Leffler 	error = 0;
16778eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
16784a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
16798eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
16808eab61f3SSam Leffler 			continue;
16818eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
16828eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1683e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
16848eab61f3SSam Leffler 				return (ENOMEM);
16858eab61f3SSam Leffler 			}
16868eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
16878eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
16888eab61f3SSam Leffler 		}
16898eab61f3SSam Leffler 		n++;
16908eab61f3SSam Leffler 	}
16918eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
16928eab61f3SSam Leffler 	bfl->bfl_len = n;
16938eab61f3SSam Leffler 	return (error);
16948eab61f3SSam Leffler }
16958eab61f3SSam Leffler 
16968eab61f3SSam Leffler /*
16978eab61f3SSam Leffler  * Set the data link type of a BPF instance.
16988eab61f3SSam Leffler  */
16998eab61f3SSam Leffler static int
17008eab61f3SSam Leffler bpf_setdlt(d, dlt)
17018eab61f3SSam Leffler 	struct bpf_d *d;
17028eab61f3SSam Leffler 	u_int dlt;
17038eab61f3SSam Leffler {
17048eab61f3SSam Leffler 	int error, opromisc;
17058eab61f3SSam Leffler 	struct ifnet *ifp;
17068eab61f3SSam Leffler 	struct bpf_if *bp;
17078eab61f3SSam Leffler 
17088eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
17098eab61f3SSam Leffler 		return (0);
17108eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
17118eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
17124a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
17138eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
17148eab61f3SSam Leffler 			break;
17158eab61f3SSam Leffler 	}
17168eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
17178eab61f3SSam Leffler 	if (bp != NULL) {
17188eab61f3SSam Leffler 		opromisc = d->bd_promisc;
17198eab61f3SSam Leffler 		bpf_detachd(d);
17208eab61f3SSam Leffler 		bpf_attachd(d, bp);
172193daabddSBrian Feldman 		BPFD_LOCK(d);
17228eab61f3SSam Leffler 		reset_d(d);
17238eab61f3SSam Leffler 		BPFD_UNLOCK(d);
17248eab61f3SSam Leffler 		if (opromisc) {
17258eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
17268eab61f3SSam Leffler 			if (error)
17278eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
17288eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
17298eab61f3SSam Leffler 					error);
17308eab61f3SSam Leffler 			else
17318eab61f3SSam Leffler 				d->bd_promisc = 1;
17328eab61f3SSam Leffler 		}
17338eab61f3SSam Leffler 	}
17348eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1735de5d9935SRobert Watson }
1736de5d9935SRobert Watson 
17373f54a085SPoul-Henning Kamp static void
17386a113b3dSRobert Watson bpf_clone(arg, cred, name, namelen, dev)
17393f54a085SPoul-Henning Kamp 	void *arg;
17406a113b3dSRobert Watson 	struct ucred *cred;
17413f54a085SPoul-Henning Kamp 	char *name;
17423f54a085SPoul-Henning Kamp 	int namelen;
174389c9c53dSPoul-Henning Kamp 	struct cdev **dev;
17443f54a085SPoul-Henning Kamp {
17453f54a085SPoul-Henning Kamp 	int u;
17463f54a085SPoul-Henning Kamp 
1747f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
17483f54a085SPoul-Henning Kamp 		return;
1749db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
17503f54a085SPoul-Henning Kamp 		return;
1751b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1752b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1753f4f6abcbSPoul-Henning Kamp 	dev_ref(*dev);
1754b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
17553f54a085SPoul-Henning Kamp 	return;
17563f54a085SPoul-Henning Kamp }
17573f54a085SPoul-Henning Kamp 
1758514ede09SBruce Evans static void
1759514ede09SBruce Evans bpf_drvinit(unused)
1760514ede09SBruce Evans 	void *unused;
176153ac6efbSJulian Elischer {
176253ac6efbSJulian Elischer 
17636008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
17644a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
1765db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
17667198bf47SJulian Elischer }
176753ac6efbSJulian Elischer 
176869f7644bSChristian S.J. Peron static void
176969f7644bSChristian S.J. Peron bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
177069f7644bSChristian S.J. Peron {
177169f7644bSChristian S.J. Peron 
177269f7644bSChristian S.J. Peron 	bzero(d, sizeof(*d));
177369f7644bSChristian S.J. Peron 	BPFD_LOCK_ASSERT(bd);
177469f7644bSChristian S.J. Peron 	d->bd_immediate = bd->bd_immediate;
177569f7644bSChristian S.J. Peron 	d->bd_promisc = bd->bd_promisc;
177669f7644bSChristian S.J. Peron 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
177769f7644bSChristian S.J. Peron 	d->bd_seesent = bd->bd_seesent;
177869f7644bSChristian S.J. Peron 	d->bd_async = bd->bd_async;
177969f7644bSChristian S.J. Peron 	d->bd_rcount = bd->bd_rcount;
178069f7644bSChristian S.J. Peron 	d->bd_dcount = bd->bd_dcount;
178169f7644bSChristian S.J. Peron 	d->bd_fcount = bd->bd_fcount;
178269f7644bSChristian S.J. Peron 	d->bd_sig = bd->bd_sig;
178369f7644bSChristian S.J. Peron 	d->bd_slen = bd->bd_slen;
178469f7644bSChristian S.J. Peron 	d->bd_hlen = bd->bd_hlen;
178569f7644bSChristian S.J. Peron 	d->bd_bufsize = bd->bd_bufsize;
178669f7644bSChristian S.J. Peron 	d->bd_pid = bd->bd_pid;
178769f7644bSChristian S.J. Peron 	strlcpy(d->bd_ifname,
178869f7644bSChristian S.J. Peron 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
178993e39f0bSChristian S.J. Peron 	d->bd_locked = bd->bd_locked;
179069f7644bSChristian S.J. Peron }
179169f7644bSChristian S.J. Peron 
179269f7644bSChristian S.J. Peron static int
179369f7644bSChristian S.J. Peron bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
179469f7644bSChristian S.J. Peron {
1795422a63daSChristian S.J. Peron 	struct xbpf_d *xbdbuf, *xbd;
1796422a63daSChristian S.J. Peron 	int index, error;
179769f7644bSChristian S.J. Peron 	struct bpf_if *bp;
179869f7644bSChristian S.J. Peron 	struct bpf_d *bd;
179969f7644bSChristian S.J. Peron 
180069f7644bSChristian S.J. Peron 	/*
180169f7644bSChristian S.J. Peron 	 * XXX This is not technically correct. It is possible for non
180269f7644bSChristian S.J. Peron 	 * privileged users to open bpf devices. It would make sense
180369f7644bSChristian S.J. Peron 	 * if the users who opened the devices were able to retrieve
180469f7644bSChristian S.J. Peron 	 * the statistics for them, too.
180569f7644bSChristian S.J. Peron 	 */
180669f7644bSChristian S.J. Peron 	error = suser(req->td);
180769f7644bSChristian S.J. Peron 	if (error)
180869f7644bSChristian S.J. Peron 		return (error);
180969f7644bSChristian S.J. Peron 	if (req->oldptr == NULL)
1810422a63daSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
181169f7644bSChristian S.J. Peron 	if (bpf_bpfd_cnt == 0)
181269f7644bSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, 0));
1813422a63daSChristian S.J. Peron 	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
181469f7644bSChristian S.J. Peron 	mtx_lock(&bpf_mtx);
1815422a63daSChristian S.J. Peron 	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
1816422a63daSChristian S.J. Peron 		mtx_unlock(&bpf_mtx);
1817422a63daSChristian S.J. Peron 		free(xbdbuf, M_BPF);
1818422a63daSChristian S.J. Peron 		return (ENOMEM);
1819422a63daSChristian S.J. Peron 	}
1820422a63daSChristian S.J. Peron 	index = 0;
182169f7644bSChristian S.J. Peron 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
182269f7644bSChristian S.J. Peron 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
1823422a63daSChristian S.J. Peron 			xbd = &xbdbuf[index++];
182469f7644bSChristian S.J. Peron 			BPFD_LOCK(bd);
1825422a63daSChristian S.J. Peron 			bpfstats_fill_xbpf(xbd, bd);
182669f7644bSChristian S.J. Peron 			BPFD_UNLOCK(bd);
182769f7644bSChristian S.J. Peron 		}
182869f7644bSChristian S.J. Peron 	}
182969f7644bSChristian S.J. Peron 	mtx_unlock(&bpf_mtx);
1830422a63daSChristian S.J. Peron 	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
1831422a63daSChristian S.J. Peron 	free(xbdbuf, M_BPF);
183269f7644bSChristian S.J. Peron 	return (error);
183369f7644bSChristian S.J. Peron }
183469f7644bSChristian S.J. Peron 
1835c9c7976fSPoul-Henning Kamp SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
183653ac6efbSJulian Elischer 
18375bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1838f8dc4716SMike Smith /*
1839f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1840f8dc4716SMike Smith  *
1841f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1842f8dc4716SMike Smith  * to be loaded at runtime.
1843f8dc4716SMike Smith  */
1844f8dc4716SMike Smith 
1845f8dc4716SMike Smith void
1846e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1847e5562beeSSam Leffler 	struct bpf_if *bp;
18488994a245SDag-Erling Smørgrav 	u_char *pkt;
18498994a245SDag-Erling Smørgrav 	u_int pktlen;
1850f8dc4716SMike Smith {
1851f8dc4716SMike Smith }
1852f8dc4716SMike Smith 
1853f8dc4716SMike Smith void
1854e5562beeSSam Leffler bpf_mtap(bp, m)
1855e5562beeSSam Leffler 	struct bpf_if *bp;
1856f8dc4716SMike Smith 	struct mbuf *m;
1857f8dc4716SMike Smith {
1858f8dc4716SMike Smith }
1859f8dc4716SMike Smith 
1860f8dc4716SMike Smith void
1861437ffe18SSam Leffler bpf_mtap2(bp, d, l, m)
1862437ffe18SSam Leffler 	struct bpf_if *bp;
1863c1dae2f0STim J. Robbins 	void *d;
1864437ffe18SSam Leffler 	u_int l;
1865437ffe18SSam Leffler 	struct mbuf *m;
1866437ffe18SSam Leffler {
1867437ffe18SSam Leffler }
1868437ffe18SSam Leffler 
1869437ffe18SSam Leffler void
1870f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1871f8dc4716SMike Smith 	struct ifnet *ifp;
1872f8dc4716SMike Smith 	u_int dlt, hdrlen;
1873f8dc4716SMike Smith {
1874f8dc4716SMike Smith }
1875f8dc4716SMike Smith 
1876da626c17SBill Paul void
18775f7a7923SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
18785f7a7923SSam Leffler 	struct ifnet *ifp;
18795f7a7923SSam Leffler 	u_int dlt, hdrlen;
18805f7a7923SSam Leffler 	struct bpf_if **driverp;
18815f7a7923SSam Leffler {
18825f7a7923SSam Leffler }
18835f7a7923SSam Leffler 
18845f7a7923SSam Leffler void
1885da626c17SBill Paul bpfdetach(ifp)
1886da626c17SBill Paul 	struct ifnet *ifp;
1887da626c17SBill Paul {
1888da626c17SBill Paul }
1889da626c17SBill Paul 
1890f8dc4716SMike Smith u_int
1891f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
18928994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
18938994a245SDag-Erling Smørgrav 	u_char *p;
1894f8dc4716SMike Smith 	u_int wirelen;
18958994a245SDag-Erling Smørgrav 	u_int buflen;
1896f8dc4716SMike Smith {
1897f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1898f8dc4716SMike Smith }
1899f8dc4716SMike Smith 
19005bb5f2c9SPeter Wemm int
19015bb5f2c9SPeter Wemm bpf_validate(f, len)
19025bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
19035bb5f2c9SPeter Wemm 	int len;
19045bb5f2c9SPeter Wemm {
19055bb5f2c9SPeter Wemm 	return 0;		/* false */
19065bb5f2c9SPeter Wemm }
19075bb5f2c9SPeter Wemm 
19085bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1909