xref: /freebsd/sys/net/bpf.c (revision 237fdd787b20699fc2c01387011b06a9ce746cfa)
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  */
36df8bae1dSRodney W. Grimes 
37c7866007SRobert Watson #include <sys/cdefs.h>
38c7866007SRobert Watson __FBSDID("$FreeBSD$");
39c7866007SRobert Watson 
405bb5f2c9SPeter Wemm #include "opt_bpf.h"
4182f4445dSRobert Watson #include "opt_mac.h"
425bb5f2c9SPeter Wemm #include "opt_netgraph.h"
43df8bae1dSRodney W. Grimes 
4495aab9ccSJohn-Mark Gurney #include <sys/types.h>
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47ce7609a4SBruce Evans #include <sys/conf.h>
48e76eee55SPoul-Henning Kamp #include <sys/fcntl.h>
494d1d4912SBruce Evans #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
51df8bae1dSRodney W. Grimes #include <sys/time.h>
52acd3428bSRobert Watson #include <sys/priv.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
540310c19fSBruce Evans #include <sys/signalvar.h>
55528f627fSBruce Evans #include <sys/filio.h>
56528f627fSBruce Evans #include <sys/sockio.h>
57528f627fSBruce Evans #include <sys/ttycom.h>
58e76eee55SPoul-Henning Kamp #include <sys/uio.h>
59df8bae1dSRodney W. Grimes 
6095aab9ccSJohn-Mark Gurney #include <sys/event.h>
6195aab9ccSJohn-Mark Gurney #include <sys/file.h>
62243ac7d8SPeter Wemm #include <sys/poll.h>
6395aab9ccSJohn-Mark Gurney #include <sys/proc.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <sys/socket.h>
66df8bae1dSRodney W. Grimes 
67fba9235dSBruce Evans #include <net/if.h>
68df8bae1dSRodney W. Grimes #include <net/bpf.h>
69ae275efcSJung-uk Kim #ifdef BPF_JITTER
70ae275efcSJung-uk Kim #include <net/bpf_jitter.h>
71ae275efcSJung-uk Kim #endif
72df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <netinet/in.h>
75df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
76df8bae1dSRodney W. Grimes #include <sys/kernel.h>
77f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
787b778b5eSEivind Eklund 
79246b5467SSam Leffler #include <net80211/ieee80211_freebsd.h>
80246b5467SSam Leffler 
81aed55708SRobert Watson #include <security/mac/mac_framework.h>
82aed55708SRobert Watson 
83959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
8487f6c662SJulian Elischer 
855bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
8653ac6efbSJulian Elischer 
87df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
88df8bae1dSRodney W. Grimes 
89560a54e1SJung-uk Kim #define	M_SKIP_BPF	M_SKIP_FIREWALL
90560a54e1SJung-uk Kim 
91df8bae1dSRodney W. Grimes /*
92d1a67300SRobert Watson  * bpf_iflist is a list of BPF interface structures, each corresponding to a
93d1a67300SRobert Watson  * specific DLT.  The same network interface might have several BPF interface
94d1a67300SRobert Watson  * structures registered by different layers in the stack (i.e., 802.11
95d1a67300SRobert Watson  * frames, ethernet frames, etc).
96df8bae1dSRodney W. Grimes  */
974a3feeaaSRobert Watson static LIST_HEAD(, bpf_if)	bpf_iflist;
98e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
9969f7644bSChristian S.J. Peron static int		bpf_bpfd_cnt;
100df8bae1dSRodney W. Grimes 
101a3594432SRobert Watson static void	bpf_allocbufs(struct bpf_d *);
10219ba8395SChristian S.J. Peron static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
10319ba8395SChristian S.J. Peron static void	bpf_detachd(struct bpf_d *);
104929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
105929ddbbbSAlfred Perlstein static void	bpf_mcopy(const void *, void *, size_t);
106cb44b6dfSAndrew Thompson static int	bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
107560a54e1SJung-uk Kim 		    struct sockaddr *, int *, struct bpf_insn *);
108929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
109929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
110e7bb21b3SJonathan Lemon static __inline void
111929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
112929ddbbbSAlfred Perlstein static void	catchpacket(struct bpf_d *, u_char *, u_int,
11391433904SDavid Malone 		    u_int, void (*)(const void *, void *, size_t),
11491433904SDavid Malone 		    struct timeval *);
115929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
11693e39f0bSChristian S.J. Peron static int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
1178eab61f3SSam Leffler static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
1188eab61f3SSam Leffler static int	bpf_setdlt(struct bpf_d *, u_int);
11995aab9ccSJohn-Mark Gurney static void	filt_bpfdetach(struct knote *);
12095aab9ccSJohn-Mark Gurney static int	filt_bpfread(struct knote *, long);
121a3272e3cSChristian S.J. Peron static void	bpf_drvinit(void *);
1226a113b3dSRobert Watson static void	bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
12369f7644bSChristian S.J. Peron static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
12469f7644bSChristian S.J. Peron 
12569f7644bSChristian S.J. Peron SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
12669f7644bSChristian S.J. Peron static int bpf_bufsize = 4096;
12769f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
1285d1f8283SRobert Watson     &bpf_bufsize, 0, "Default bpf buffer size");
12969f7644bSChristian S.J. Peron static int bpf_maxbufsize = BPF_MAXBUFSIZE;
13069f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
1315d1f8283SRobert Watson     &bpf_maxbufsize, 0, "Maximum bpf buffer size");
13269f7644bSChristian S.J. Peron static int bpf_maxinsns = BPF_MAXINSNS;
13369f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
13469f7644bSChristian S.J. Peron     &bpf_maxinsns, 0, "Maximum bpf program instructions");
13569f7644bSChristian S.J. Peron SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
13669f7644bSChristian S.J. Peron     bpf_stats_sysctl, "bpf statistics portal");
137df8bae1dSRodney W. Grimes 
13887f6c662SJulian Elischer static	d_open_t	bpfopen;
13987f6c662SJulian Elischer static	d_close_t	bpfclose;
14087f6c662SJulian Elischer static	d_read_t	bpfread;
14187f6c662SJulian Elischer static	d_write_t	bpfwrite;
14287f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
143243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
14495aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
14587f6c662SJulian Elischer 
1464e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
147dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1487ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1497ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1507ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1517ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1527ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1537ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1547ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
15595aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1564e2f199eSPoul-Henning Kamp };
15787f6c662SJulian Elischer 
15895aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
15995aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
16087f6c662SJulian Elischer 
161df8bae1dSRodney W. Grimes static int
162cb44b6dfSAndrew Thompson bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
163560a54e1SJung-uk Kim     struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
164df8bae1dSRodney W. Grimes {
165246b5467SSam Leffler 	const struct ieee80211_bpf_params *p;
166cb44b6dfSAndrew Thompson 	struct ether_header *eh;
167df8bae1dSRodney W. Grimes 	struct mbuf *m;
168df8bae1dSRodney W. Grimes 	int error;
169df8bae1dSRodney W. Grimes 	int len;
170df8bae1dSRodney W. Grimes 	int hlen;
17193e39f0bSChristian S.J. Peron 	int slen;
172df8bae1dSRodney W. Grimes 
173df8bae1dSRodney W. Grimes 	/*
174df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
175df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
176df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
177df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
178df8bae1dSRodney W. Grimes 	 * is forwarded as is.
179df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
180df8bae1dSRodney W. Grimes 	 * for the link level header.
181df8bae1dSRodney W. Grimes 	 */
182df8bae1dSRodney W. Grimes 	switch (linktype) {
183df8bae1dSRodney W. Grimes 
184df8bae1dSRodney W. Grimes 	case DLT_SLIP:
185df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
186df8bae1dSRodney W. Grimes 		hlen = 0;
187df8bae1dSRodney W. Grimes 		break;
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
190df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
191df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
192797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
193df8bae1dSRodney W. Grimes 		break;
194df8bae1dSRodney W. Grimes 
195df8bae1dSRodney W. Grimes 	case DLT_FDDI:
196d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
197d41f24e7SDavid Greenman 		hlen = 0;
198df8bae1dSRodney W. Grimes 		break;
199df8bae1dSRodney W. Grimes 
20022f05c43SAndrey A. Chernov 	case DLT_RAW:
201df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
202df8bae1dSRodney W. Grimes 		hlen = 0;
203df8bae1dSRodney W. Grimes 		break;
204df8bae1dSRodney W. Grimes 
20501399f34SDavid Malone 	case DLT_NULL:
20601399f34SDavid Malone 		/*
20701399f34SDavid Malone 		 * null interface types require a 4 byte pseudo header which
20801399f34SDavid Malone 		 * corresponds to the address family of the packet.
20901399f34SDavid Malone 		 */
21001399f34SDavid Malone 		sockp->sa_family = AF_UNSPEC;
21101399f34SDavid Malone 		hlen = 4;
21201399f34SDavid Malone 		break;
21301399f34SDavid Malone 
2144f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
2154f53e3ccSKenjiro Cho 		/*
2164f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
2174f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
2184f53e3ccSKenjiro Cho 		 * specified anyway.
2194f53e3ccSKenjiro Cho 		 */
2204f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
2214f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
2224f53e3ccSKenjiro Cho 		break;
2234f53e3ccSKenjiro Cho 
22430fa52a6SBrian Somers 	case DLT_PPP:
22530fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
22630fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
22730fa52a6SBrian Somers 		break;
22830fa52a6SBrian Somers 
229246b5467SSam Leffler 	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
230246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
231246b5467SSam Leffler 		hlen = 0;
232246b5467SSam Leffler 		break;
233246b5467SSam Leffler 
234246b5467SSam Leffler 	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
235246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
236246b5467SSam Leffler 		sockp->sa_len = 12;	/* XXX != 0 */
237246b5467SSam Leffler 		hlen = sizeof(struct ieee80211_bpf_params);
238246b5467SSam Leffler 		break;
239246b5467SSam Leffler 
240df8bae1dSRodney W. Grimes 	default:
241df8bae1dSRodney W. Grimes 		return (EIO);
242df8bae1dSRodney W. Grimes 	}
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
24501399f34SDavid Malone 
246cb44b6dfSAndrew Thompson 	if (len - hlen > ifp->if_mtu)
24701399f34SDavid Malone 		return (EMSGSIZE);
24801399f34SDavid Malone 
249df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
250df8bae1dSRodney W. Grimes 		return (EIO);
251df8bae1dSRodney W. Grimes 
252963e4c2aSGarrett Wollman 	if (len > MHLEN) {
253a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
25424a229f4SSam Leffler 	} else {
255a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
256df8bae1dSRodney W. Grimes 	}
25724a229f4SSam Leffler 	if (m == NULL)
25824a229f4SSam Leffler 		return (ENOBUFS);
259963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
260963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
261df8bae1dSRodney W. Grimes 	*mp = m;
26224a229f4SSam Leffler 
26393e39f0bSChristian S.J. Peron 	if (m->m_len < hlen) {
26493e39f0bSChristian S.J. Peron 		error = EPERM;
26593e39f0bSChristian S.J. Peron 		goto bad;
26693e39f0bSChristian S.J. Peron 	}
26793e39f0bSChristian S.J. Peron 
26893e39f0bSChristian S.J. Peron 	error = uiomove(mtod(m, u_char *), len, uio);
26993e39f0bSChristian S.J. Peron 	if (error)
27093e39f0bSChristian S.J. Peron 		goto bad;
27193e39f0bSChristian S.J. Peron 
27293e39f0bSChristian S.J. Peron 	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
27393e39f0bSChristian S.J. Peron 	if (slen == 0) {
27493e39f0bSChristian S.J. Peron 		error = EPERM;
27593e39f0bSChristian S.J. Peron 		goto bad;
27693e39f0bSChristian S.J. Peron 	}
27793e39f0bSChristian S.J. Peron 
278cb44b6dfSAndrew Thompson 	/* Check for multicast destination */
279cb44b6dfSAndrew Thompson 	switch (linktype) {
280cb44b6dfSAndrew Thompson 	case DLT_EN10MB:
281cb44b6dfSAndrew Thompson 		eh = mtod(m, struct ether_header *);
282cb44b6dfSAndrew Thompson 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
283cb44b6dfSAndrew Thompson 			if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
284cb44b6dfSAndrew Thompson 			    ETHER_ADDR_LEN) == 0)
285cb44b6dfSAndrew Thompson 				m->m_flags |= M_BCAST;
286cb44b6dfSAndrew Thompson 			else
287cb44b6dfSAndrew Thompson 				m->m_flags |= M_MCAST;
288cb44b6dfSAndrew Thompson 		}
289cb44b6dfSAndrew Thompson 		break;
290cb44b6dfSAndrew Thompson 	}
291cb44b6dfSAndrew Thompson 
292df8bae1dSRodney W. Grimes 	/*
29393e39f0bSChristian S.J. Peron 	 * Make room for link header, and copy it to sockaddr
294df8bae1dSRodney W. Grimes 	 */
295df8bae1dSRodney W. Grimes 	if (hlen != 0) {
296246b5467SSam Leffler 		if (sockp->sa_family == AF_IEEE80211) {
297246b5467SSam Leffler 			/*
298246b5467SSam Leffler 			 * Collect true length from the parameter header
299246b5467SSam Leffler 			 * NB: sockp is known to be zero'd so if we do a
300246b5467SSam Leffler 			 *     short copy unspecified parameters will be
301246b5467SSam Leffler 			 *     zero.
302246b5467SSam Leffler 			 * NB: packet may not be aligned after stripping
303246b5467SSam Leffler 			 *     bpf params
304246b5467SSam Leffler 			 * XXX check ibp_vers
305246b5467SSam Leffler 			 */
306246b5467SSam Leffler 			p = mtod(m, const struct ieee80211_bpf_params *);
307246b5467SSam Leffler 			hlen = p->ibp_len;
308246b5467SSam Leffler 			if (hlen > sizeof(sockp->sa_data)) {
309246b5467SSam Leffler 				error = EINVAL;
310246b5467SSam Leffler 				goto bad;
311246b5467SSam Leffler 			}
312246b5467SSam Leffler 		}
31393e39f0bSChristian S.J. Peron 		bcopy(m->m_data, sockp->sa_data, hlen);
314df8bae1dSRodney W. Grimes 	}
315560a54e1SJung-uk Kim 	*hdrlen = hlen;
31693e39f0bSChristian S.J. Peron 
317df8bae1dSRodney W. Grimes 	return (0);
318df8bae1dSRodney W. Grimes bad:
319df8bae1dSRodney W. Grimes 	m_freem(m);
320df8bae1dSRodney W. Grimes 	return (error);
321df8bae1dSRodney W. Grimes }
322df8bae1dSRodney W. Grimes 
323df8bae1dSRodney W. Grimes /*
324df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
325df8bae1dSRodney W. Grimes  */
326df8bae1dSRodney W. Grimes static void
32719ba8395SChristian S.J. Peron bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
328df8bae1dSRodney W. Grimes {
329df8bae1dSRodney W. Grimes 	/*
330df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
331df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
332df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
333df8bae1dSRodney W. Grimes 	 */
334e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
335df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
3364a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
337df8bae1dSRodney W. Grimes 
33869f7644bSChristian S.J. Peron 	bpf_bpfd_cnt++;
339e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
340df8bae1dSRodney W. Grimes }
341df8bae1dSRodney W. Grimes 
342df8bae1dSRodney W. Grimes /*
343df8bae1dSRodney W. Grimes  * Detach a file from its interface.
344df8bae1dSRodney W. Grimes  */
345df8bae1dSRodney W. Grimes static void
34619ba8395SChristian S.J. Peron bpf_detachd(struct bpf_d *d)
347df8bae1dSRodney W. Grimes {
3486e891d64SPoul-Henning Kamp 	int error;
349df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
35046448b5aSRobert Watson 	struct ifnet *ifp;
351df8bae1dSRodney W. Grimes 
352df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
35346448b5aSRobert Watson 	BPFIF_LOCK(bp);
35446448b5aSRobert Watson 	BPFD_LOCK(d);
35546448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
35646448b5aSRobert Watson 
35746448b5aSRobert Watson 	/*
35846448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
35946448b5aSRobert Watson 	 */
36046448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
36146448b5aSRobert Watson 
36269f7644bSChristian S.J. Peron 	bpf_bpfd_cnt--;
363572bde2aSRobert Watson 	d->bd_bif = NULL;
36446448b5aSRobert Watson 	BPFD_UNLOCK(d);
36546448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
36646448b5aSRobert Watson 
367df8bae1dSRodney W. Grimes 	/*
368df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
369df8bae1dSRodney W. Grimes 	 * If so, turn it off.
370df8bae1dSRodney W. Grimes 	 */
371df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
372df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
37346448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
3746e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
375df8bae1dSRodney W. Grimes 			/*
3766e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
377df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
378df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
379df8bae1dSRodney W. Grimes 			 * take it out.
380df8bae1dSRodney W. Grimes 			 */
3818eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
3828eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
3836e891d64SPoul-Henning Kamp 		}
384df8bae1dSRodney W. Grimes 	}
385df8bae1dSRodney W. Grimes }
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes /*
388df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
389df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
390df8bae1dSRodney W. Grimes  */
391df8bae1dSRodney W. Grimes /* ARGSUSED */
39287f6c662SJulian Elischer static	int
39319ba8395SChristian S.J. Peron bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
394df8bae1dSRodney W. Grimes {
395e7bb21b3SJonathan Lemon 	struct bpf_d *d;
396df8bae1dSRodney W. Grimes 
397e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
398bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
399df8bae1dSRodney W. Grimes 	/*
400df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
401df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
402df8bae1dSRodney W. Grimes 	 */
403d17d8184SRobert Watson 	if (d != NULL) {
404e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
405df8bae1dSRodney W. Grimes 		return (EBUSY);
406e7bb21b3SJonathan Lemon 	}
407e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
408e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
409e7bb21b3SJonathan Lemon 
410d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
411b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
412b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
413a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
414bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
415df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
41600a83887SPaul Traina 	d->bd_sig = SIGIO;
417560a54e1SJung-uk Kim 	d->bd_direction = BPF_D_INOUT;
41869f7644bSChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
41982f4445dSRobert Watson #ifdef MAC
42030d239bcSRobert Watson 	mac_bpfdesc_init(d);
42130d239bcSRobert Watson 	mac_bpfdesc_create(td->td_ucred, d);
42282f4445dSRobert Watson #endif
4236008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
424c6b28997SRobert Watson 	callout_init(&d->bd_callout, CALLOUT_MPSAFE);
425571dcd15SSuleiman Souhlal 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
426df8bae1dSRodney W. Grimes 
427df8bae1dSRodney W. Grimes 	return (0);
428df8bae1dSRodney W. Grimes }
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes /*
431df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
432df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
433df8bae1dSRodney W. Grimes  */
434df8bae1dSRodney W. Grimes /* ARGSUSED */
43587f6c662SJulian Elischer static	int
43619ba8395SChristian S.J. Peron bpfclose(struct cdev *dev, int flags, int fmt, struct thread *td)
437df8bae1dSRodney W. Grimes {
438e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
439df8bae1dSRodney W. Grimes 
44081bda851SJohn Polstra 	BPFD_LOCK(d);
44181bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
44281bda851SJohn Polstra 		callout_stop(&d->bd_callout);
44381bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
44481bda851SJohn Polstra 	BPFD_UNLOCK(d);
445e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
446e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
447df8bae1dSRodney W. Grimes 	if (d->bd_bif)
448df8bae1dSRodney W. Grimes 		bpf_detachd(d);
449e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
4504549709fSBrian Feldman 	selwakeuppri(&d->bd_sel, PRINET);
45182f4445dSRobert Watson #ifdef MAC
45230d239bcSRobert Watson 	mac_bpfdesc_destroy(d);
45382f4445dSRobert Watson #endif /* MAC */
454ad3b9257SJohn-Mark Gurney 	knlist_destroy(&d->bd_sel.si_note);
455df8bae1dSRodney W. Grimes 	bpf_freed(d);
456d17d8184SRobert Watson 	dev->si_drv1 = NULL;
457d722be54SLuigi Rizzo 	free(d, M_BPF);
458df8bae1dSRodney W. Grimes 
459df8bae1dSRodney W. Grimes 	return (0);
460df8bae1dSRodney W. Grimes }
461df8bae1dSRodney W. Grimes 
462df8bae1dSRodney W. Grimes 
463df8bae1dSRodney W. Grimes /*
464df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
465df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
466df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
467df8bae1dSRodney W. Grimes  */
468df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
469df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
470df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
471df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
472df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
473d17d8184SRobert Watson 	(d)->bd_fbuf = NULL;
474df8bae1dSRodney W. Grimes /*
475df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
476df8bae1dSRodney W. Grimes  */
47787f6c662SJulian Elischer static	int
47819ba8395SChristian S.J. Peron bpfread(struct cdev *dev, struct uio *uio, int ioflag)
479df8bae1dSRodney W. Grimes {
480e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
48181bda851SJohn Polstra 	int timed_out;
482df8bae1dSRodney W. Grimes 	int error;
483df8bae1dSRodney W. Grimes 
484df8bae1dSRodney W. Grimes 	/*
485df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
486df8bae1dSRodney W. Grimes 	 * as kernel buffers.
487df8bae1dSRodney W. Grimes 	 */
488df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
489df8bae1dSRodney W. Grimes 		return (EINVAL);
490df8bae1dSRodney W. Grimes 
491e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
49250ed6e07SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
49381bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
49481bda851SJohn Polstra 		callout_stop(&d->bd_callout);
49581bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
49681bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
497df8bae1dSRodney W. Grimes 	/*
498df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
499df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
500df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
501df8bae1dSRodney W. Grimes 	 */
502572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
50381bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
504df8bae1dSRodney W. Grimes 			/*
505df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
506df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
507df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
508df8bae1dSRodney W. Grimes 			 */
509df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
510df8bae1dSRodney W. Grimes 			break;
511df8bae1dSRodney W. Grimes 		}
512de5d9935SRobert Watson 
513de5d9935SRobert Watson 		/*
514de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
515de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
516de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
517de5d9935SRobert Watson 		 * it before using it again.
518de5d9935SRobert Watson 		 */
519de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
520e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
521de5d9935SRobert Watson 			return (ENXIO);
522de5d9935SRobert Watson 		}
523de5d9935SRobert Watson 
524e76eee55SPoul-Henning Kamp 		if (ioflag & O_NONBLOCK) {
525e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
526fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
527fba3cfdeSJohn Polstra 		}
528521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
529e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
530df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
531e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
532df8bae1dSRodney W. Grimes 			return (error);
533df8bae1dSRodney W. Grimes 		}
534df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
535df8bae1dSRodney W. Grimes 			/*
536df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
537df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
538df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
539df8bae1dSRodney W. Grimes 			 */
540df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
541df8bae1dSRodney W. Grimes 				/*
542df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
543df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
544df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
545df8bae1dSRodney W. Grimes 				 */
546df8bae1dSRodney W. Grimes 				break;
547df8bae1dSRodney W. Grimes 
548df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
549e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
550df8bae1dSRodney W. Grimes 				return (0);
551df8bae1dSRodney W. Grimes 			}
552df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
553df8bae1dSRodney W. Grimes 			break;
554df8bae1dSRodney W. Grimes 		}
555df8bae1dSRodney W. Grimes 	}
556df8bae1dSRodney W. Grimes 	/*
557df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
558df8bae1dSRodney W. Grimes 	 */
559e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes 	/*
562df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
563df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
564df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
56531b32e6dSRobert Watson 	 *
56631b32e6dSRobert Watson 	 * XXXRW: More synchronization needed here: what if a second thread
56731b32e6dSRobert Watson 	 * issues a read on the same fd at the same time?  Don't want this
56831b32e6dSRobert Watson 	 * getting invalidated.
569df8bae1dSRodney W. Grimes 	 */
570e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
571df8bae1dSRodney W. Grimes 
572e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
573df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
574572bde2aSRobert Watson 	d->bd_hbuf = NULL;
575df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
576e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
577df8bae1dSRodney W. Grimes 
578df8bae1dSRodney W. Grimes 	return (error);
579df8bae1dSRodney W. Grimes }
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes /*
582df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
583df8bae1dSRodney W. Grimes  */
584e7bb21b3SJonathan Lemon static __inline void
58519ba8395SChristian S.J. Peron bpf_wakeup(struct bpf_d *d)
586df8bae1dSRodney W. Grimes {
587a3272e3cSChristian S.J. Peron 
588a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
58981bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
59081bda851SJohn Polstra 		callout_stop(&d->bd_callout);
59181bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
59281bda851SJohn Polstra 	}
593521f364bSDag-Erling Smørgrav 	wakeup(d);
594831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
595f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
59600a83887SPaul Traina 
597512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
598ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
599df8bae1dSRodney W. Grimes }
600df8bae1dSRodney W. Grimes 
60181bda851SJohn Polstra static void
60219ba8395SChristian S.J. Peron bpf_timed_out(void *arg)
60381bda851SJohn Polstra {
60481bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
60581bda851SJohn Polstra 
60681bda851SJohn Polstra 	BPFD_LOCK(d);
60781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
60881bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
60981bda851SJohn Polstra 		if (d->bd_slen != 0)
61081bda851SJohn Polstra 			bpf_wakeup(d);
61181bda851SJohn Polstra 	}
61281bda851SJohn Polstra 	BPFD_UNLOCK(d);
61381bda851SJohn Polstra }
61481bda851SJohn Polstra 
61587f6c662SJulian Elischer static int
61619ba8395SChristian S.J. Peron bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
617df8bae1dSRodney W. Grimes {
618e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
619df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
620560a54e1SJung-uk Kim 	struct mbuf *m, *mc;
6218240bf1eSRobert Watson 	struct sockaddr dst;
622560a54e1SJung-uk Kim 	int error, hlen;
623df8bae1dSRodney W. Grimes 
62450ed6e07SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
625572bde2aSRobert Watson 	if (d->bd_bif == NULL)
626df8bae1dSRodney W. Grimes 		return (ENXIO);
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
629df8bae1dSRodney W. Grimes 
6303518d220SSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0)
6313518d220SSam Leffler 		return (ENETDOWN);
6323518d220SSam Leffler 
633df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
634df8bae1dSRodney W. Grimes 		return (0);
635df8bae1dSRodney W. Grimes 
6368240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
637d83e603aSChristian S.J. Peron 	m = NULL;
638d83e603aSChristian S.J. Peron 	hlen = 0;
639cb44b6dfSAndrew Thompson 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp,
640560a54e1SJung-uk Kim 	    &m, &dst, &hlen, d->bd_wfilter);
641df8bae1dSRodney W. Grimes 	if (error)
642df8bae1dSRodney W. Grimes 		return (error);
643df8bae1dSRodney W. Grimes 
644114ae644SMike Smith 	if (d->bd_hdrcmplt)
645114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
646114ae644SMike Smith 
647560a54e1SJung-uk Kim 	if (d->bd_feedback) {
648560a54e1SJung-uk Kim 		mc = m_dup(m, M_DONTWAIT);
649560a54e1SJung-uk Kim 		if (mc != NULL)
650560a54e1SJung-uk Kim 			mc->m_pkthdr.rcvif = ifp;
651560a54e1SJung-uk Kim 		/* XXX Do not return the same packet twice. */
652560a54e1SJung-uk Kim 		if (d->bd_direction == BPF_D_INOUT)
653560a54e1SJung-uk Kim 			m->m_flags |= M_SKIP_BPF;
654560a54e1SJung-uk Kim 	} else
655560a54e1SJung-uk Kim 		mc = NULL;
656560a54e1SJung-uk Kim 
657560a54e1SJung-uk Kim 	m->m_pkthdr.len -= hlen;
658560a54e1SJung-uk Kim 	m->m_len -= hlen;
659560a54e1SJung-uk Kim 	m->m_data += hlen;	/* XXX */
660560a54e1SJung-uk Kim 
66182f4445dSRobert Watson #ifdef MAC
662f747d2ddSRobert Watson 	BPFD_LOCK(d);
66330d239bcSRobert Watson 	mac_bpfdesc_create_mbuf(d, m);
664560a54e1SJung-uk Kim 	if (mc != NULL)
66530d239bcSRobert Watson 		mac_bpfdesc_create_mbuf(d, mc);
666f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
66782f4445dSRobert Watson #endif
668560a54e1SJung-uk Kim 
669572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
670560a54e1SJung-uk Kim 
671560a54e1SJung-uk Kim 	if (mc != NULL) {
6720bf686c1SRobert Watson 		if (error == 0)
673560a54e1SJung-uk Kim 			(*ifp->if_input)(ifp, mc);
6740bf686c1SRobert Watson 		else
675560a54e1SJung-uk Kim 			m_freem(mc);
676560a54e1SJung-uk Kim 	}
677560a54e1SJung-uk Kim 
678df8bae1dSRodney W. Grimes 	return (error);
679df8bae1dSRodney W. Grimes }
680df8bae1dSRodney W. Grimes 
681df8bae1dSRodney W. Grimes /*
682df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
683e7bb21b3SJonathan Lemon  * receive and drop counts.
684df8bae1dSRodney W. Grimes  */
685df8bae1dSRodney W. Grimes static void
68619ba8395SChristian S.J. Peron reset_d(struct bpf_d *d)
687df8bae1dSRodney W. Grimes {
688e7bb21b3SJonathan Lemon 
689e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
690df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
691df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
692df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
693572bde2aSRobert Watson 		d->bd_hbuf = NULL;
694df8bae1dSRodney W. Grimes 	}
695df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
696df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
697df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
698df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
69969f7644bSChristian S.J. Peron 	d->bd_fcount = 0;
700df8bae1dSRodney W. Grimes }
701df8bae1dSRodney W. Grimes 
702df8bae1dSRodney W. Grimes /*
703df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
704df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
705df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
706df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
70793e39f0bSChristian S.J. Peron  *  BIOCSETWF		Set ethernet write filter.
708df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
709df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
710df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
711df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
712df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
713df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
714df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
715df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
716df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
717df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
718114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
719114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
720560a54e1SJung-uk Kim  *  BIOCGDIRECTION	Get packet direction flag
721560a54e1SJung-uk Kim  *  BIOCSDIRECTION	Set packet direction flag
72293e39f0bSChristian S.J. Peron  *  BIOCLOCK		Set "locked" flag
723560a54e1SJung-uk Kim  *  BIOCFEEDBACK	Set packet feedback mode.
724df8bae1dSRodney W. Grimes  */
725df8bae1dSRodney W. Grimes /* ARGSUSED */
72687f6c662SJulian Elischer static	int
72719ba8395SChristian S.J. Peron bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
72819ba8395SChristian S.J. Peron     struct thread *td)
729df8bae1dSRodney W. Grimes {
730e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
731e7bb21b3SJonathan Lemon 	int error = 0;
732df8bae1dSRodney W. Grimes 
733b75a24a0SChristian S.J. Peron 	/*
734b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
735b75a24a0SChristian S.J. Peron 	 */
73681bda851SJohn Polstra 	BPFD_LOCK(d);
737cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
73881bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
73981bda851SJohn Polstra 		callout_stop(&d->bd_callout);
74081bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
74181bda851SJohn Polstra 	BPFD_UNLOCK(d);
74281bda851SJohn Polstra 
74393e39f0bSChristian S.J. Peron 	if (d->bd_locked == 1) {
74493e39f0bSChristian S.J. Peron 		switch (cmd) {
74593e39f0bSChristian S.J. Peron 		case BIOCGBLEN:
74693e39f0bSChristian S.J. Peron 		case BIOCFLUSH:
74793e39f0bSChristian S.J. Peron 		case BIOCGDLT:
74893e39f0bSChristian S.J. Peron 		case BIOCGDLTLIST:
74993e39f0bSChristian S.J. Peron 		case BIOCGETIF:
75093e39f0bSChristian S.J. Peron 		case BIOCGRTIMEOUT:
75193e39f0bSChristian S.J. Peron 		case BIOCGSTATS:
75293e39f0bSChristian S.J. Peron 		case BIOCVERSION:
75393e39f0bSChristian S.J. Peron 		case BIOCGRSIG:
75493e39f0bSChristian S.J. Peron 		case BIOCGHDRCMPLT:
755560a54e1SJung-uk Kim 		case BIOCFEEDBACK:
75693e39f0bSChristian S.J. Peron 		case FIONREAD:
75793e39f0bSChristian S.J. Peron 		case BIOCLOCK:
75893e39f0bSChristian S.J. Peron 		case BIOCSRTIMEOUT:
75993e39f0bSChristian S.J. Peron 		case BIOCIMMEDIATE:
76093e39f0bSChristian S.J. Peron 		case TIOCGPGRP:
76193e39f0bSChristian S.J. Peron 			break;
76293e39f0bSChristian S.J. Peron 		default:
76393e39f0bSChristian S.J. Peron 			return (EPERM);
76493e39f0bSChristian S.J. Peron 		}
76593e39f0bSChristian S.J. Peron 	}
766df8bae1dSRodney W. Grimes 	switch (cmd) {
767df8bae1dSRodney W. Grimes 
768df8bae1dSRodney W. Grimes 	default:
769df8bae1dSRodney W. Grimes 		error = EINVAL;
770df8bae1dSRodney W. Grimes 		break;
771df8bae1dSRodney W. Grimes 
772df8bae1dSRodney W. Grimes 	/*
773df8bae1dSRodney W. Grimes 	 * Check for read packet available.
774df8bae1dSRodney W. Grimes 	 */
775df8bae1dSRodney W. Grimes 	case FIONREAD:
776df8bae1dSRodney W. Grimes 		{
777df8bae1dSRodney W. Grimes 			int n;
778df8bae1dSRodney W. Grimes 
779e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
780df8bae1dSRodney W. Grimes 			n = d->bd_slen;
781df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
782df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
783e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
784df8bae1dSRodney W. Grimes 
785df8bae1dSRodney W. Grimes 			*(int *)addr = n;
786df8bae1dSRodney W. Grimes 			break;
787df8bae1dSRodney W. Grimes 		}
788df8bae1dSRodney W. Grimes 
789df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
790df8bae1dSRodney W. Grimes 		{
791df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
792df8bae1dSRodney W. Grimes 
793572bde2aSRobert Watson 			if (d->bd_bif == NULL)
794df8bae1dSRodney W. Grimes 				error = EINVAL;
795df8bae1dSRodney W. Grimes 			else {
796df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
797df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
798df8bae1dSRodney W. Grimes 			}
799df8bae1dSRodney W. Grimes 			break;
800df8bae1dSRodney W. Grimes 		}
801df8bae1dSRodney W. Grimes 
802df8bae1dSRodney W. Grimes 	/*
803df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
804df8bae1dSRodney W. Grimes 	 */
805df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
806df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
807df8bae1dSRodney W. Grimes 		break;
808df8bae1dSRodney W. Grimes 
809df8bae1dSRodney W. Grimes 	/*
810df8bae1dSRodney W. Grimes 	 * Set buffer length.
811df8bae1dSRodney W. Grimes 	 */
812df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
813572bde2aSRobert Watson 		if (d->bd_bif != NULL)
814df8bae1dSRodney W. Grimes 			error = EINVAL;
815df8bae1dSRodney W. Grimes 		else {
8168994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
817df8bae1dSRodney W. Grimes 
818eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
819eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
820df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
821df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
822df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
823df8bae1dSRodney W. Grimes 		}
824df8bae1dSRodney W. Grimes 		break;
825df8bae1dSRodney W. Grimes 
826df8bae1dSRodney W. Grimes 	/*
827df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
828df8bae1dSRodney W. Grimes 	 */
829df8bae1dSRodney W. Grimes 	case BIOCSETF:
83093e39f0bSChristian S.J. Peron 	case BIOCSETWF:
83193e39f0bSChristian S.J. Peron 		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
832df8bae1dSRodney W. Grimes 		break;
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 	/*
835df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
836df8bae1dSRodney W. Grimes 	 */
837df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
838e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
839df8bae1dSRodney W. Grimes 		reset_d(d);
840e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
841df8bae1dSRodney W. Grimes 		break;
842df8bae1dSRodney W. Grimes 
843df8bae1dSRodney W. Grimes 	/*
844df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
845df8bae1dSRodney W. Grimes 	 */
846df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
847572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
848df8bae1dSRodney W. Grimes 			/*
849df8bae1dSRodney W. Grimes 			 * No interface attached yet.
850df8bae1dSRodney W. Grimes 			 */
851df8bae1dSRodney W. Grimes 			error = EINVAL;
852df8bae1dSRodney W. Grimes 			break;
853df8bae1dSRodney W. Grimes 		}
854df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
855df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
856df8bae1dSRodney W. Grimes 			if (error == 0)
857df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
858df8bae1dSRodney W. Grimes 		}
859df8bae1dSRodney W. Grimes 		break;
860df8bae1dSRodney W. Grimes 
861df8bae1dSRodney W. Grimes 	/*
8628eab61f3SSam Leffler 	 * Get current data link type.
863df8bae1dSRodney W. Grimes 	 */
864df8bae1dSRodney W. Grimes 	case BIOCGDLT:
865572bde2aSRobert Watson 		if (d->bd_bif == NULL)
866df8bae1dSRodney W. Grimes 			error = EINVAL;
867df8bae1dSRodney W. Grimes 		else
868df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
869df8bae1dSRodney W. Grimes 		break;
870df8bae1dSRodney W. Grimes 
871df8bae1dSRodney W. Grimes 	/*
8728eab61f3SSam Leffler 	 * Get a list of supported data link types.
8738eab61f3SSam Leffler 	 */
8748eab61f3SSam Leffler 	case BIOCGDLTLIST:
875572bde2aSRobert Watson 		if (d->bd_bif == NULL)
8768eab61f3SSam Leffler 			error = EINVAL;
8778eab61f3SSam Leffler 		else
8788eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
8798eab61f3SSam Leffler 		break;
8808eab61f3SSam Leffler 
8818eab61f3SSam Leffler 	/*
8828eab61f3SSam Leffler 	 * Set data link type.
8838eab61f3SSam Leffler 	 */
8848eab61f3SSam Leffler 	case BIOCSDLT:
885572bde2aSRobert Watson 		if (d->bd_bif == NULL)
8868eab61f3SSam Leffler 			error = EINVAL;
8878eab61f3SSam Leffler 		else
8888eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
8898eab61f3SSam Leffler 		break;
8908eab61f3SSam Leffler 
8918eab61f3SSam Leffler 	/*
8921dd0feaaSArchie Cobbs 	 * Get interface name.
893df8bae1dSRodney W. Grimes 	 */
894df8bae1dSRodney W. Grimes 	case BIOCGETIF:
895572bde2aSRobert Watson 		if (d->bd_bif == NULL)
896df8bae1dSRodney W. Grimes 			error = EINVAL;
8971dd0feaaSArchie Cobbs 		else {
8981dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
8991dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
9001dd0feaaSArchie Cobbs 
9019bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
9029bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
9031dd0feaaSArchie Cobbs 		}
904df8bae1dSRodney W. Grimes 		break;
905df8bae1dSRodney W. Grimes 
906df8bae1dSRodney W. Grimes 	/*
907df8bae1dSRodney W. Grimes 	 * Set interface.
908df8bae1dSRodney W. Grimes 	 */
909df8bae1dSRodney W. Grimes 	case BIOCSETIF:
910df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
911df8bae1dSRodney W. Grimes 		break;
912df8bae1dSRodney W. Grimes 
913df8bae1dSRodney W. Grimes 	/*
914df8bae1dSRodney W. Grimes 	 * Set read timeout.
915df8bae1dSRodney W. Grimes 	 */
916df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
917df8bae1dSRodney W. Grimes 		{
918df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
919df8bae1dSRodney W. Grimes 
920bdc2cdc5SAlexander Langer 			/*
921bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
922bdc2cdc5SAlexander Langer 			 * a one-shot timer.
923bdc2cdc5SAlexander Langer 			 */
924bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
925bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
926df8bae1dSRodney W. Grimes 			break;
927df8bae1dSRodney W. Grimes 		}
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes 	/*
930df8bae1dSRodney W. Grimes 	 * Get read timeout.
931df8bae1dSRodney W. Grimes 	 */
932df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
933df8bae1dSRodney W. Grimes 		{
934df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
935df8bae1dSRodney W. Grimes 
936bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
937bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
938df8bae1dSRodney W. Grimes 			break;
939df8bae1dSRodney W. Grimes 		}
940df8bae1dSRodney W. Grimes 
941df8bae1dSRodney W. Grimes 	/*
942df8bae1dSRodney W. Grimes 	 * Get packet stats.
943df8bae1dSRodney W. Grimes 	 */
944df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
945df8bae1dSRodney W. Grimes 		{
946df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
949df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
950df8bae1dSRodney W. Grimes 			break;
951df8bae1dSRodney W. Grimes 		}
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 	/*
954df8bae1dSRodney W. Grimes 	 * Set immediate mode.
955df8bae1dSRodney W. Grimes 	 */
956df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
957df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
958df8bae1dSRodney W. Grimes 		break;
959df8bae1dSRodney W. Grimes 
960df8bae1dSRodney W. Grimes 	case BIOCVERSION:
961df8bae1dSRodney W. Grimes 		{
962df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
963df8bae1dSRodney W. Grimes 
964df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
965df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
966df8bae1dSRodney W. Grimes 			break;
967df8bae1dSRodney W. Grimes 		}
96800a83887SPaul Traina 
969114ae644SMike Smith 	/*
970114ae644SMike Smith 	 * Get "header already complete" flag
971114ae644SMike Smith 	 */
972114ae644SMike Smith 	case BIOCGHDRCMPLT:
973114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
974114ae644SMike Smith 		break;
975114ae644SMike Smith 
976114ae644SMike Smith 	/*
977114ae644SMike Smith 	 * Set "header already complete" flag
978114ae644SMike Smith 	 */
979114ae644SMike Smith 	case BIOCSHDRCMPLT:
980114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
981114ae644SMike Smith 		break;
982114ae644SMike Smith 
9838ed3828cSRobert Watson 	/*
984560a54e1SJung-uk Kim 	 * Get packet direction flag
9858ed3828cSRobert Watson 	 */
986560a54e1SJung-uk Kim 	case BIOCGDIRECTION:
987560a54e1SJung-uk Kim 		*(u_int *)addr = d->bd_direction;
9888ed3828cSRobert Watson 		break;
9898ed3828cSRobert Watson 
9908ed3828cSRobert Watson 	/*
991560a54e1SJung-uk Kim 	 * Set packet direction flag
9928ed3828cSRobert Watson 	 */
993560a54e1SJung-uk Kim 	case BIOCSDIRECTION:
994560a54e1SJung-uk Kim 		{
995560a54e1SJung-uk Kim 			u_int	direction;
996560a54e1SJung-uk Kim 
997560a54e1SJung-uk Kim 			direction = *(u_int *)addr;
998560a54e1SJung-uk Kim 			switch (direction) {
999560a54e1SJung-uk Kim 			case BPF_D_IN:
1000560a54e1SJung-uk Kim 			case BPF_D_INOUT:
1001560a54e1SJung-uk Kim 			case BPF_D_OUT:
1002560a54e1SJung-uk Kim 				d->bd_direction = direction;
1003560a54e1SJung-uk Kim 				break;
1004560a54e1SJung-uk Kim 			default:
1005560a54e1SJung-uk Kim 				error = EINVAL;
1006560a54e1SJung-uk Kim 			}
1007560a54e1SJung-uk Kim 		}
1008560a54e1SJung-uk Kim 		break;
1009560a54e1SJung-uk Kim 
1010560a54e1SJung-uk Kim 	case BIOCFEEDBACK:
1011560a54e1SJung-uk Kim 		d->bd_feedback = *(u_int *)addr;
1012560a54e1SJung-uk Kim 		break;
1013560a54e1SJung-uk Kim 
1014560a54e1SJung-uk Kim 	case BIOCLOCK:
1015560a54e1SJung-uk Kim 		d->bd_locked = 1;
10168ed3828cSRobert Watson 		break;
10178ed3828cSRobert Watson 
101800a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
101900a83887SPaul Traina 		break;
102000a83887SPaul Traina 
102100a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
102200a83887SPaul Traina 		d->bd_async = *(int *)addr;
102300a83887SPaul Traina 		break;
102400a83887SPaul Traina 
1025831d27a9SDon Lewis 	case FIOSETOWN:
1026831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
102700a83887SPaul Traina 		break;
102800a83887SPaul Traina 
1029831d27a9SDon Lewis 	case FIOGETOWN:
103091e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
1031831d27a9SDon Lewis 		break;
1032831d27a9SDon Lewis 
1033831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
1034831d27a9SDon Lewis 	case TIOCSPGRP:
1035831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
1036831d27a9SDon Lewis 		break;
1037831d27a9SDon Lewis 
1038831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
103900a83887SPaul Traina 	case TIOCGPGRP:
104091e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
104100a83887SPaul Traina 		break;
104200a83887SPaul Traina 
104300a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
104400a83887SPaul Traina 		{
104500a83887SPaul Traina 			u_int sig;
104600a83887SPaul Traina 
104700a83887SPaul Traina 			sig = *(u_int *)addr;
104800a83887SPaul Traina 
104900a83887SPaul Traina 			if (sig >= NSIG)
105000a83887SPaul Traina 				error = EINVAL;
105100a83887SPaul Traina 			else
105200a83887SPaul Traina 				d->bd_sig = sig;
105300a83887SPaul Traina 			break;
105400a83887SPaul Traina 		}
105500a83887SPaul Traina 	case BIOCGRSIG:
105600a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
105700a83887SPaul Traina 		break;
1058df8bae1dSRodney W. Grimes 	}
1059df8bae1dSRodney W. Grimes 	return (error);
1060df8bae1dSRodney W. Grimes }
1061df8bae1dSRodney W. Grimes 
1062df8bae1dSRodney W. Grimes /*
1063df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
1064df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
1065df8bae1dSRodney W. Grimes  */
1066f708ef1bSPoul-Henning Kamp static int
106719ba8395SChristian S.J. Peron bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1068df8bae1dSRodney W. Grimes {
1069df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
107093e39f0bSChristian S.J. Peron 	u_int wfilter, flen, size;
1071293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1072ae275efcSJung-uk Kim 	bpf_jit_filter *ofunc;
1073ae275efcSJung-uk Kim #endif
1074df8bae1dSRodney W. Grimes 
107593e39f0bSChristian S.J. Peron 	if (cmd == BIOCSETWF) {
107693e39f0bSChristian S.J. Peron 		old = d->bd_wfilter;
107793e39f0bSChristian S.J. Peron 		wfilter = 1;
1078293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1079ae275efcSJung-uk Kim 		ofunc = NULL;
1080ae275efcSJung-uk Kim #endif
108193e39f0bSChristian S.J. Peron 	} else {
108293e39f0bSChristian S.J. Peron 		wfilter = 0;
108393e39f0bSChristian S.J. Peron 		old = d->bd_rfilter;
1084293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1085ae275efcSJung-uk Kim 		ofunc = d->bd_bfilter;
1086ae275efcSJung-uk Kim #endif
108793e39f0bSChristian S.J. Peron 	}
1088572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
1089df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
1090df8bae1dSRodney W. Grimes 			return (EINVAL);
1091e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
109293e39f0bSChristian S.J. Peron 		if (wfilter)
109393e39f0bSChristian S.J. Peron 			d->bd_wfilter = NULL;
1094ae275efcSJung-uk Kim 		else {
109593e39f0bSChristian S.J. Peron 			d->bd_rfilter = NULL;
1096293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1097ae275efcSJung-uk Kim 			d->bd_bfilter = NULL;
1098ae275efcSJung-uk Kim #endif
1099ae275efcSJung-uk Kim 		}
1100df8bae1dSRodney W. Grimes 		reset_d(d);
1101e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1102572bde2aSRobert Watson 		if (old != NULL)
1103bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1104293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1105ae275efcSJung-uk Kim 		if (ofunc != NULL)
1106ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1107ae275efcSJung-uk Kim #endif
1108df8bae1dSRodney W. Grimes 		return (0);
1109df8bae1dSRodney W. Grimes 	}
1110df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
11110eb20604SChristian S.J. Peron 	if (flen > bpf_maxinsns)
1112df8bae1dSRodney W. Grimes 		return (EINVAL);
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
1115a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1116df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1117df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
1118e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
111993e39f0bSChristian S.J. Peron 		if (wfilter)
112093e39f0bSChristian S.J. Peron 			d->bd_wfilter = fcode;
1121ae275efcSJung-uk Kim 		else {
112293e39f0bSChristian S.J. Peron 			d->bd_rfilter = fcode;
1123293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1124ae275efcSJung-uk Kim 			d->bd_bfilter = bpf_jitter(fcode, flen);
1125ae275efcSJung-uk Kim #endif
1126ae275efcSJung-uk Kim 		}
1127df8bae1dSRodney W. Grimes 		reset_d(d);
1128e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1129572bde2aSRobert Watson 		if (old != NULL)
1130bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1131293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1132ae275efcSJung-uk Kim 		if (ofunc != NULL)
1133ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1134ae275efcSJung-uk Kim #endif
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes 		return (0);
1137df8bae1dSRodney W. Grimes 	}
1138bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
1139df8bae1dSRodney W. Grimes 	return (EINVAL);
1140df8bae1dSRodney W. Grimes }
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes /*
1143df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
1144df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
1145df8bae1dSRodney W. Grimes  * Return an errno or 0.
1146df8bae1dSRodney W. Grimes  */
1147df8bae1dSRodney W. Grimes static int
114819ba8395SChristian S.J. Peron bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1149df8bae1dSRodney W. Grimes {
1150df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
11519b44ff22SGarrett Wollman 	struct ifnet *theywant;
1152df8bae1dSRodney W. Grimes 
11539b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
115416d878ccSChristian S.J. Peron 	if (theywant == NULL || theywant->if_bpf == NULL)
115516d878ccSChristian S.J. Peron 		return (ENXIO);
11569b44ff22SGarrett Wollman 
115716d878ccSChristian S.J. Peron 	bp = theywant->if_bpf;
1158df8bae1dSRodney W. Grimes 	/*
1159df8bae1dSRodney W. Grimes 	 * Allocate the packet buffers if we need to.
1160df8bae1dSRodney W. Grimes 	 * If we're already attached to requested interface,
1161df8bae1dSRodney W. Grimes 	 * just flush the buffer.
1162df8bae1dSRodney W. Grimes 	 */
1163a3594432SRobert Watson 	if (d->bd_sbuf == NULL)
1164a3594432SRobert Watson 		bpf_allocbufs(d);
1165df8bae1dSRodney W. Grimes 	if (bp != d->bd_bif) {
1166df8bae1dSRodney W. Grimes 		if (d->bd_bif)
1167df8bae1dSRodney W. Grimes 			/*
1168df8bae1dSRodney W. Grimes 			 * Detach if attached to something else.
1169df8bae1dSRodney W. Grimes 			 */
1170df8bae1dSRodney W. Grimes 			bpf_detachd(d);
1171df8bae1dSRodney W. Grimes 
1172df8bae1dSRodney W. Grimes 		bpf_attachd(d, bp);
1173df8bae1dSRodney W. Grimes 	}
1174e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1175df8bae1dSRodney W. Grimes 	reset_d(d);
1176e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1177df8bae1dSRodney W. Grimes 	return (0);
1178df8bae1dSRodney W. Grimes }
1179df8bae1dSRodney W. Grimes 
1180df8bae1dSRodney W. Grimes /*
1181243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1182df8bae1dSRodney W. Grimes  *
1183df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1184df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1185df8bae1dSRodney W. Grimes  */
118637c84183SPoul-Henning Kamp static int
118719ba8395SChristian S.J. Peron bpfpoll(struct cdev *dev, int events, struct thread *td)
1188df8bae1dSRodney W. Grimes {
1189e7bb21b3SJonathan Lemon 	struct bpf_d *d;
11900832fc64SGarance A Drosehn 	int revents;
1191df8bae1dSRodney W. Grimes 
1192bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1193de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1194de5d9935SRobert Watson 		return (ENXIO);
1195de5d9935SRobert Watson 
1196b75a24a0SChristian S.J. Peron 	/*
1197b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1198b75a24a0SChristian S.J. Peron 	 */
11990832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1200e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1201cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
120275c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
120395aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1204243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
120581bda851SJohn Polstra 		else {
1206ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
120781bda851SJohn Polstra 			/* Start the read timeout if necessary. */
120881bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
120981bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
121081bda851SJohn Polstra 				    bpf_timed_out, d);
121181bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
121281bda851SJohn Polstra 			}
121381bda851SJohn Polstra 		}
121475c13541SPoul-Henning Kamp 	}
1215e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1216243ac7d8SPeter Wemm 	return (revents);
1217df8bae1dSRodney W. Grimes }
1218df8bae1dSRodney W. Grimes 
1219df8bae1dSRodney W. Grimes /*
122095aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
122195aab9ccSJohn-Mark Gurney  * reject all others.
122295aab9ccSJohn-Mark Gurney  */
122395aab9ccSJohn-Mark Gurney int
122419ba8395SChristian S.J. Peron bpfkqfilter(struct cdev *dev, struct knote *kn)
122595aab9ccSJohn-Mark Gurney {
122695aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
122795aab9ccSJohn-Mark Gurney 
122895aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
122995aab9ccSJohn-Mark Gurney 		return (1);
123095aab9ccSJohn-Mark Gurney 
1231b75a24a0SChristian S.J. Peron 	/*
1232b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1233b75a24a0SChristian S.J. Peron 	 */
1234cb1d4f92SChristian S.J. Peron 	BPFD_LOCK(d);
1235b75a24a0SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
123695aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
123795aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
12384b19419eSChristian S.J. Peron 	knlist_add(&d->bd_sel.si_note, kn, 1);
1239cb1d4f92SChristian S.J. Peron 	BPFD_UNLOCK(d);
124095aab9ccSJohn-Mark Gurney 
124195aab9ccSJohn-Mark Gurney 	return (0);
124295aab9ccSJohn-Mark Gurney }
124395aab9ccSJohn-Mark Gurney 
124495aab9ccSJohn-Mark Gurney static void
124519ba8395SChristian S.J. Peron filt_bpfdetach(struct knote *kn)
124695aab9ccSJohn-Mark Gurney {
124795aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
124895aab9ccSJohn-Mark Gurney 
1249ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
125095aab9ccSJohn-Mark Gurney }
125195aab9ccSJohn-Mark Gurney 
125295aab9ccSJohn-Mark Gurney static int
125319ba8395SChristian S.J. Peron filt_bpfread(struct knote *kn, long hint)
125495aab9ccSJohn-Mark Gurney {
125595aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
125695aab9ccSJohn-Mark Gurney 	int ready;
125795aab9ccSJohn-Mark Gurney 
125886c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(d);
125995aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
126095aab9ccSJohn-Mark Gurney 	if (ready) {
126195aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
126295aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
126395aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
126495aab9ccSJohn-Mark Gurney 	}
126595aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
126695aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
126795aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
126895aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
126995aab9ccSJohn-Mark Gurney 	}
127095aab9ccSJohn-Mark Gurney 
127195aab9ccSJohn-Mark Gurney 	return (ready);
127295aab9ccSJohn-Mark Gurney }
127395aab9ccSJohn-Mark Gurney 
127495aab9ccSJohn-Mark Gurney /*
1275df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1276df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1277df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1278df8bae1dSRodney W. Grimes  * buffer.
1279df8bae1dSRodney W. Grimes  */
1280df8bae1dSRodney W. Grimes void
128119ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1282df8bae1dSRodney W. Grimes {
12838994a245SDag-Erling Smørgrav 	struct bpf_d *d;
12848994a245SDag-Erling Smørgrav 	u_int slen;
128591433904SDavid Malone 	int gottime;
128691433904SDavid Malone 	struct timeval tv;
1287e7bb21b3SJonathan Lemon 
128891433904SDavid Malone 	gottime = 0;
1289e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
12904a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1291e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1292df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1293ae275efcSJung-uk Kim #ifdef BPF_JITTER
1294ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1295ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1296ae275efcSJung-uk Kim 		else
1297ae275efcSJung-uk Kim #endif
129893e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1299ec272d87SRobert Watson 		if (slen != 0) {
130069f7644bSChristian S.J. Peron 			d->bd_fcount++;
130191433904SDavid Malone 			if (!gottime) {
130291433904SDavid Malone 				microtime(&tv);
130391433904SDavid Malone 				gottime = 1;
130491433904SDavid Malone 			}
1305ec272d87SRobert Watson #ifdef MAC
130630d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1307ec272d87SRobert Watson #endif
130891433904SDavid Malone 				catchpacket(d, pkt, pktlen, slen, bcopy, &tv);
1309ec272d87SRobert Watson 		}
1310e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1311df8bae1dSRodney W. Grimes 	}
1312e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1313df8bae1dSRodney W. Grimes }
1314df8bae1dSRodney W. Grimes 
1315df8bae1dSRodney W. Grimes /*
1316df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1317df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1318df8bae1dSRodney W. Grimes  */
1319df8bae1dSRodney W. Grimes static void
132019ba8395SChristian S.J. Peron bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1321df8bae1dSRodney W. Grimes {
13228994a245SDag-Erling Smørgrav 	const struct mbuf *m;
13238994a245SDag-Erling Smørgrav 	u_int count;
1324df8bae1dSRodney W. Grimes 	u_char *dst;
1325df8bae1dSRodney W. Grimes 
1326df8bae1dSRodney W. Grimes 	m = src_arg;
1327df8bae1dSRodney W. Grimes 	dst = dst_arg;
1328df8bae1dSRodney W. Grimes 	while (len > 0) {
1329572bde2aSRobert Watson 		if (m == NULL)
1330df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1331df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
13320453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1333df8bae1dSRodney W. Grimes 		m = m->m_next;
1334df8bae1dSRodney W. Grimes 		dst += count;
1335df8bae1dSRodney W. Grimes 		len -= count;
1336df8bae1dSRodney W. Grimes 	}
1337df8bae1dSRodney W. Grimes }
1338df8bae1dSRodney W. Grimes 
1339560a54e1SJung-uk Kim #define	BPF_CHECK_DIRECTION(d, m) \
1340560a54e1SJung-uk Kim 	if (((d)->bd_direction == BPF_D_IN && (m)->m_pkthdr.rcvif == NULL) || \
1341560a54e1SJung-uk Kim 	    ((d)->bd_direction == BPF_D_OUT && (m)->m_pkthdr.rcvif != NULL))
1342560a54e1SJung-uk Kim 
1343df8bae1dSRodney W. Grimes /*
1344df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1345df8bae1dSRodney W. Grimes  */
1346df8bae1dSRodney W. Grimes void
134719ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1348df8bae1dSRodney W. Grimes {
1349df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1350df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
135191433904SDavid Malone 	int gottime;
135291433904SDavid Malone 	struct timeval tv;
135391433904SDavid Malone 
1354560a54e1SJung-uk Kim 	if (m->m_flags & M_SKIP_BPF) {
1355560a54e1SJung-uk Kim 		m->m_flags &= ~M_SKIP_BPF;
1356560a54e1SJung-uk Kim 		return;
1357560a54e1SJung-uk Kim 	}
1358560a54e1SJung-uk Kim 
135991433904SDavid Malone 	gottime = 0;
1360df8bae1dSRodney W. Grimes 
1361f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
1362df8bae1dSRodney W. Grimes 
1363e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
13644a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1365560a54e1SJung-uk Kim 		BPF_CHECK_DIRECTION(d, m)
13668ed3828cSRobert Watson 			continue;
1367e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1368df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1369ae275efcSJung-uk Kim #ifdef BPF_JITTER
1370ae275efcSJung-uk Kim 		/* XXX We cannot handle multiple mbufs. */
1371ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1372ae275efcSJung-uk Kim 		    m->m_next == NULL)
1373ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1374ae275efcSJung-uk Kim 			    pktlen, pktlen);
1375ae275efcSJung-uk Kim 		else
1376ae275efcSJung-uk Kim #endif
137793e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
13784ddfb531SChristian S.J. Peron 		if (slen != 0) {
137969f7644bSChristian S.J. Peron 			d->bd_fcount++;
138091433904SDavid Malone 			if (!gottime) {
138191433904SDavid Malone 				microtime(&tv);
138291433904SDavid Malone 				gottime = 1;
138391433904SDavid Malone 			}
13840c7fb534SRobert Watson #ifdef MAC
138530d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
13860c7fb534SRobert Watson #endif
13870c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
138891433904SDavid Malone 				    bpf_mcopy, &tv);
13894ddfb531SChristian S.J. Peron 		}
1390e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1391df8bae1dSRodney W. Grimes 	}
1392e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1393df8bae1dSRodney W. Grimes }
1394df8bae1dSRodney W. Grimes 
1395df8bae1dSRodney W. Grimes /*
1396437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1397437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1398437ffe18SSam Leffler  */
1399437ffe18SSam Leffler void
140019ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1401437ffe18SSam Leffler {
1402437ffe18SSam Leffler 	struct mbuf mb;
1403437ffe18SSam Leffler 	struct bpf_d *d;
1404437ffe18SSam Leffler 	u_int pktlen, slen;
140591433904SDavid Malone 	int gottime;
140691433904SDavid Malone 	struct timeval tv;
140791433904SDavid Malone 
1408560a54e1SJung-uk Kim 	if (m->m_flags & M_SKIP_BPF) {
1409560a54e1SJung-uk Kim 		m->m_flags &= ~M_SKIP_BPF;
1410560a54e1SJung-uk Kim 		return;
1411560a54e1SJung-uk Kim 	}
1412560a54e1SJung-uk Kim 
141391433904SDavid Malone 	gottime = 0;
1414437ffe18SSam Leffler 
1415437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1416437ffe18SSam Leffler 	/*
1417437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1418437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1419437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1420437ffe18SSam Leffler 	 */
1421437ffe18SSam Leffler 	mb.m_next = m;
1422437ffe18SSam Leffler 	mb.m_data = data;
1423437ffe18SSam Leffler 	mb.m_len = dlen;
1424437ffe18SSam Leffler 	pktlen += dlen;
1425437ffe18SSam Leffler 
1426437ffe18SSam Leffler 	BPFIF_LOCK(bp);
14274a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1428560a54e1SJung-uk Kim 		BPF_CHECK_DIRECTION(d, m)
1429437ffe18SSam Leffler 			continue;
1430437ffe18SSam Leffler 		BPFD_LOCK(d);
1431437ffe18SSam Leffler 		++d->bd_rcount;
143293e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
14334ddfb531SChristian S.J. Peron 		if (slen != 0) {
143469f7644bSChristian S.J. Peron 			d->bd_fcount++;
143591433904SDavid Malone 			if (!gottime) {
143691433904SDavid Malone 				microtime(&tv);
143791433904SDavid Malone 				gottime = 1;
143891433904SDavid Malone 			}
1439437ffe18SSam Leffler #ifdef MAC
144030d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1441437ffe18SSam Leffler #endif
1442437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
144391433904SDavid Malone 				    bpf_mcopy, &tv);
14444ddfb531SChristian S.J. Peron 		}
1445437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1446437ffe18SSam Leffler 	}
1447437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1448437ffe18SSam Leffler }
1449437ffe18SSam Leffler 
1450560a54e1SJung-uk Kim #undef	BPF_CHECK_DIRECTION
1451560a54e1SJung-uk Kim 
1452437ffe18SSam Leffler /*
1453df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
14549e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1455df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1456df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1457df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1458df8bae1dSRodney W. Grimes  */
1459df8bae1dSRodney W. Grimes static void
146019ba8395SChristian S.J. Peron catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
146191433904SDavid Malone     void (*cpfn)(const void *, void *, size_t), struct timeval *tv)
1462df8bae1dSRodney W. Grimes {
14638994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
14648994a245SDag-Erling Smørgrav 	int totlen, curlen;
14658994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
14667819da79SJohn-Mark Gurney 	int do_wakeup = 0;
14679e610888SDag-Erling Smørgrav 
1468a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1469df8bae1dSRodney W. Grimes 	/*
1470df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1471df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1472df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1473df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1474df8bae1dSRodney W. Grimes 	 */
1475df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1476df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1477df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1478df8bae1dSRodney W. Grimes 
1479df8bae1dSRodney W. Grimes 	/*
1480df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1481df8bae1dSRodney W. Grimes 	 */
1482df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1483df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1484df8bae1dSRodney W. Grimes 		/*
1485df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1486df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1487df8bae1dSRodney W. Grimes 		 * pending reads.
1488df8bae1dSRodney W. Grimes 		 */
1489572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1490df8bae1dSRodney W. Grimes 			/*
1491df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1492df8bae1dSRodney W. Grimes 			 * so drop the packet.
1493df8bae1dSRodney W. Grimes 			 */
1494df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1495df8bae1dSRodney W. Grimes 			return;
1496df8bae1dSRodney W. Grimes 		}
1497df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
14987819da79SJohn-Mark Gurney 		do_wakeup = 1;
1499df8bae1dSRodney W. Grimes 		curlen = 0;
1500df8bae1dSRodney W. Grimes 	}
150181bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1502df8bae1dSRodney W. Grimes 		/*
150381bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
150481bda851SJohn Polstra 		 * already expired during a select call.  A packet
150581bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1506df8bae1dSRodney W. Grimes 		 */
15077819da79SJohn-Mark Gurney 		do_wakeup = 1;
1508df8bae1dSRodney W. Grimes 
1509df8bae1dSRodney W. Grimes 	/*
1510df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1511df8bae1dSRodney W. Grimes 	 */
1512df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
151391433904SDavid Malone 	hp->bh_tstamp = *tv;
1514df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1515df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1516df8bae1dSRodney W. Grimes 	/*
1517df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1518df8bae1dSRodney W. Grimes 	 */
1519df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1520df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
15217819da79SJohn-Mark Gurney 
15227819da79SJohn-Mark Gurney 	if (do_wakeup)
15237819da79SJohn-Mark Gurney 		bpf_wakeup(d);
1524df8bae1dSRodney W. Grimes }
1525df8bae1dSRodney W. Grimes 
1526df8bae1dSRodney W. Grimes /*
1527df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1528df8bae1dSRodney W. Grimes  */
1529a3594432SRobert Watson static void
153019ba8395SChristian S.J. Peron bpf_allocbufs(struct bpf_d *d)
1531df8bae1dSRodney W. Grimes {
1532df8bae1dSRodney W. Grimes 
1533a3594432SRobert Watson 	KASSERT(d->bd_fbuf == NULL, ("bpf_allocbufs: bd_fbuf != NULL"));
1534a3594432SRobert Watson 	KASSERT(d->bd_sbuf == NULL, ("bpf_allocbufs: bd_sbuf != NULL"));
1535a3594432SRobert Watson 	KASSERT(d->bd_hbuf == NULL, ("bpf_allocbufs: bd_hbuf != NULL"));
1536a3594432SRobert Watson 
1537a3594432SRobert Watson 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1538a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1539df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1540df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1541df8bae1dSRodney W. Grimes }
1542df8bae1dSRodney W. Grimes 
1543df8bae1dSRodney W. Grimes /*
1544df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1545df8bae1dSRodney W. Grimes  * Called on close.
1546df8bae1dSRodney W. Grimes  */
1547df8bae1dSRodney W. Grimes static void
154819ba8395SChristian S.J. Peron bpf_freed(struct bpf_d *d)
1549df8bae1dSRodney W. Grimes {
1550df8bae1dSRodney W. Grimes 	/*
1551df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1552df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1553df8bae1dSRodney W. Grimes 	 * free.
1554df8bae1dSRodney W. Grimes 	 */
1555572bde2aSRobert Watson 	if (d->bd_sbuf != NULL) {
1556bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1557572bde2aSRobert Watson 		if (d->bd_hbuf != NULL)
1558bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1559572bde2aSRobert Watson 		if (d->bd_fbuf != NULL)
1560bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1561df8bae1dSRodney W. Grimes 	}
1562ae275efcSJung-uk Kim 	if (d->bd_rfilter) {
156393e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_rfilter, M_BPF);
1564ae275efcSJung-uk Kim #ifdef BPF_JITTER
1565ae275efcSJung-uk Kim 		bpf_destroy_jit_filter(d->bd_bfilter);
1566ae275efcSJung-uk Kim #endif
1567ae275efcSJung-uk Kim 	}
156893e39f0bSChristian S.J. Peron 	if (d->bd_wfilter)
156993e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_wfilter, M_BPF);
1570e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1571df8bae1dSRodney W. Grimes }
1572df8bae1dSRodney W. Grimes 
1573df8bae1dSRodney W. Grimes /*
157424a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
157524a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1576df8bae1dSRodney W. Grimes  */
1577df8bae1dSRodney W. Grimes void
157819ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1579df8bae1dSRodney W. Grimes {
158024a229f4SSam Leffler 
158124a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
158224a229f4SSam Leffler }
158324a229f4SSam Leffler 
158424a229f4SSam Leffler /*
158524a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
158624a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
158724a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
158824a229f4SSam Leffler  * headers are not yet supporrted).
158924a229f4SSam Leffler  */
159024a229f4SSam Leffler void
159119ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
159224a229f4SSam Leffler {
1593df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
159419ba8395SChristian S.J. Peron 
159519ba8395SChristian S.J. Peron 	bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1596572bde2aSRobert Watson 	if (bp == NULL)
1597df8bae1dSRodney W. Grimes 		panic("bpfattach");
1598df8bae1dSRodney W. Grimes 
15994a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
1600df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1601df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
16026008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
160316d878ccSChristian S.J. Peron 	KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
160416d878ccSChristian S.J. Peron 	*driverp = bp;
1605df8bae1dSRodney W. Grimes 
1606e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
16074a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1608e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1609df8bae1dSRodney W. Grimes 
1610df8bae1dSRodney W. Grimes 	/*
1611df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1612df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1613df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1614df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1615df8bae1dSRodney W. Grimes 	 */
1616df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1617df8bae1dSRodney W. Grimes 
16182eeab939SGarrett Wollman 	if (bootverbose)
161924a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1620df8bae1dSRodney W. Grimes }
162153ac6efbSJulian Elischer 
1622de5d9935SRobert Watson /*
1623de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1624de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1625de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1626de5d9935SRobert Watson  * ENXIO.
1627de5d9935SRobert Watson  */
1628de5d9935SRobert Watson void
162919ba8395SChristian S.J. Peron bpfdetach(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
166619ba8395SChristian S.J. Peron bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
16678eab61f3SSam Leffler {
16688eab61f3SSam Leffler 	int n, error;
16698eab61f3SSam Leffler 	struct ifnet *ifp;
16708eab61f3SSam Leffler 	struct bpf_if *bp;
16718eab61f3SSam Leffler 
16728eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
16738eab61f3SSam Leffler 	n = 0;
16748eab61f3SSam Leffler 	error = 0;
16758eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
16764a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
16778eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
16788eab61f3SSam Leffler 			continue;
16798eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
16808eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1681e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
16828eab61f3SSam Leffler 				return (ENOMEM);
16838eab61f3SSam Leffler 			}
16848eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
16858eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
16868eab61f3SSam Leffler 		}
16878eab61f3SSam Leffler 		n++;
16888eab61f3SSam Leffler 	}
16898eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
16908eab61f3SSam Leffler 	bfl->bfl_len = n;
16918eab61f3SSam Leffler 	return (error);
16928eab61f3SSam Leffler }
16938eab61f3SSam Leffler 
16948eab61f3SSam Leffler /*
16958eab61f3SSam Leffler  * Set the data link type of a BPF instance.
16968eab61f3SSam Leffler  */
16978eab61f3SSam Leffler static int
169819ba8395SChristian S.J. Peron bpf_setdlt(struct bpf_d *d, u_int dlt)
16998eab61f3SSam Leffler {
17008eab61f3SSam Leffler 	int error, opromisc;
17018eab61f3SSam Leffler 	struct ifnet *ifp;
17028eab61f3SSam Leffler 	struct bpf_if *bp;
17038eab61f3SSam Leffler 
17048eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
17058eab61f3SSam Leffler 		return (0);
17068eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
17078eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
17084a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
17098eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
17108eab61f3SSam Leffler 			break;
17118eab61f3SSam Leffler 	}
17128eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
17138eab61f3SSam Leffler 	if (bp != NULL) {
17148eab61f3SSam Leffler 		opromisc = d->bd_promisc;
17158eab61f3SSam Leffler 		bpf_detachd(d);
17168eab61f3SSam Leffler 		bpf_attachd(d, bp);
171793daabddSBrian Feldman 		BPFD_LOCK(d);
17188eab61f3SSam Leffler 		reset_d(d);
17198eab61f3SSam Leffler 		BPFD_UNLOCK(d);
17208eab61f3SSam Leffler 		if (opromisc) {
17218eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
17228eab61f3SSam Leffler 			if (error)
17238eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
17248eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
17258eab61f3SSam Leffler 					error);
17268eab61f3SSam Leffler 			else
17278eab61f3SSam Leffler 				d->bd_promisc = 1;
17288eab61f3SSam Leffler 		}
17298eab61f3SSam Leffler 	}
17308eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1731de5d9935SRobert Watson }
1732de5d9935SRobert Watson 
17333f54a085SPoul-Henning Kamp static void
173419ba8395SChristian S.J. Peron bpf_clone(void *arg, struct ucred *cred, char *name, int namelen,
173519ba8395SChristian S.J. Peron     struct cdev **dev)
17363f54a085SPoul-Henning Kamp {
17373f54a085SPoul-Henning Kamp 	int u;
17383f54a085SPoul-Henning Kamp 
1739f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
17403f54a085SPoul-Henning Kamp 		return;
1741db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
17423f54a085SPoul-Henning Kamp 		return;
1743b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1744b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1745f4f6abcbSPoul-Henning Kamp 	dev_ref(*dev);
1746b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
17473f54a085SPoul-Henning Kamp 	return;
17483f54a085SPoul-Henning Kamp }
17493f54a085SPoul-Henning Kamp 
1750514ede09SBruce Evans static void
175119ba8395SChristian S.J. Peron bpf_drvinit(void *unused)
175253ac6efbSJulian Elischer {
175353ac6efbSJulian Elischer 
17546008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
17554a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
1756db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
17577198bf47SJulian Elischer }
175853ac6efbSJulian Elischer 
175969f7644bSChristian S.J. Peron static void
176069f7644bSChristian S.J. Peron bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
176169f7644bSChristian S.J. Peron {
176269f7644bSChristian S.J. Peron 
176369f7644bSChristian S.J. Peron 	bzero(d, sizeof(*d));
176469f7644bSChristian S.J. Peron 	BPFD_LOCK_ASSERT(bd);
176569f7644bSChristian S.J. Peron 	d->bd_immediate = bd->bd_immediate;
176669f7644bSChristian S.J. Peron 	d->bd_promisc = bd->bd_promisc;
176769f7644bSChristian S.J. Peron 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
1768560a54e1SJung-uk Kim 	d->bd_direction = bd->bd_direction;
1769560a54e1SJung-uk Kim 	d->bd_feedback = bd->bd_feedback;
177069f7644bSChristian S.J. Peron 	d->bd_async = bd->bd_async;
177169f7644bSChristian S.J. Peron 	d->bd_rcount = bd->bd_rcount;
177269f7644bSChristian S.J. Peron 	d->bd_dcount = bd->bd_dcount;
177369f7644bSChristian S.J. Peron 	d->bd_fcount = bd->bd_fcount;
177469f7644bSChristian S.J. Peron 	d->bd_sig = bd->bd_sig;
177569f7644bSChristian S.J. Peron 	d->bd_slen = bd->bd_slen;
177669f7644bSChristian S.J. Peron 	d->bd_hlen = bd->bd_hlen;
177769f7644bSChristian S.J. Peron 	d->bd_bufsize = bd->bd_bufsize;
177869f7644bSChristian S.J. Peron 	d->bd_pid = bd->bd_pid;
177969f7644bSChristian S.J. Peron 	strlcpy(d->bd_ifname,
178069f7644bSChristian S.J. Peron 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
178193e39f0bSChristian S.J. Peron 	d->bd_locked = bd->bd_locked;
178269f7644bSChristian S.J. Peron }
178369f7644bSChristian S.J. Peron 
178469f7644bSChristian S.J. Peron static int
178569f7644bSChristian S.J. Peron bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
178669f7644bSChristian S.J. Peron {
1787422a63daSChristian S.J. Peron 	struct xbpf_d *xbdbuf, *xbd;
1788422a63daSChristian S.J. Peron 	int index, error;
178969f7644bSChristian S.J. Peron 	struct bpf_if *bp;
179069f7644bSChristian S.J. Peron 	struct bpf_d *bd;
179169f7644bSChristian S.J. Peron 
179269f7644bSChristian S.J. Peron 	/*
179369f7644bSChristian S.J. Peron 	 * XXX This is not technically correct. It is possible for non
179469f7644bSChristian S.J. Peron 	 * privileged users to open bpf devices. It would make sense
179569f7644bSChristian S.J. Peron 	 * if the users who opened the devices were able to retrieve
179669f7644bSChristian S.J. Peron 	 * the statistics for them, too.
179769f7644bSChristian S.J. Peron 	 */
1798acd3428bSRobert Watson 	error = priv_check(req->td, PRIV_NET_BPF);
179969f7644bSChristian S.J. Peron 	if (error)
180069f7644bSChristian S.J. Peron 		return (error);
180169f7644bSChristian S.J. Peron 	if (req->oldptr == NULL)
1802422a63daSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
180369f7644bSChristian S.J. Peron 	if (bpf_bpfd_cnt == 0)
180469f7644bSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, 0));
1805422a63daSChristian S.J. Peron 	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
180669f7644bSChristian S.J. Peron 	mtx_lock(&bpf_mtx);
1807422a63daSChristian S.J. Peron 	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
1808422a63daSChristian S.J. Peron 		mtx_unlock(&bpf_mtx);
1809422a63daSChristian S.J. Peron 		free(xbdbuf, M_BPF);
1810422a63daSChristian S.J. Peron 		return (ENOMEM);
1811422a63daSChristian S.J. Peron 	}
1812422a63daSChristian S.J. Peron 	index = 0;
181369f7644bSChristian S.J. Peron 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
18141fc9e387SChristian S.J. Peron 		BPFIF_LOCK(bp);
181569f7644bSChristian S.J. Peron 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
1816422a63daSChristian S.J. Peron 			xbd = &xbdbuf[index++];
181769f7644bSChristian S.J. Peron 			BPFD_LOCK(bd);
1818422a63daSChristian S.J. Peron 			bpfstats_fill_xbpf(xbd, bd);
181969f7644bSChristian S.J. Peron 			BPFD_UNLOCK(bd);
182069f7644bSChristian S.J. Peron 		}
18211fc9e387SChristian S.J. Peron 		BPFIF_UNLOCK(bp);
182269f7644bSChristian S.J. Peron 	}
182369f7644bSChristian S.J. Peron 	mtx_unlock(&bpf_mtx);
1824422a63daSChristian S.J. Peron 	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
1825422a63daSChristian S.J. Peron 	free(xbdbuf, M_BPF);
182669f7644bSChristian S.J. Peron 	return (error);
182769f7644bSChristian S.J. Peron }
182869f7644bSChristian S.J. Peron 
1829237fdd78SRobert Watson SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL);
183053ac6efbSJulian Elischer 
18315bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1832f8dc4716SMike Smith /*
1833f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1834f8dc4716SMike Smith  *
1835f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1836f8dc4716SMike Smith  * to be loaded at runtime.
1837f8dc4716SMike Smith  */
18387eae78a4SChristian S.J. Peron static struct bpf_if bp_null;
1839f8dc4716SMike Smith 
1840f8dc4716SMike Smith void
184119ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1842f8dc4716SMike Smith {
1843f8dc4716SMike Smith }
1844f8dc4716SMike Smith 
1845f8dc4716SMike Smith void
184619ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1847f8dc4716SMike Smith {
1848f8dc4716SMike Smith }
1849f8dc4716SMike Smith 
1850f8dc4716SMike Smith void
185119ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
1852437ffe18SSam Leffler {
1853437ffe18SSam Leffler }
1854437ffe18SSam Leffler 
1855437ffe18SSam Leffler void
185619ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1857f8dc4716SMike Smith {
18587eae78a4SChristian S.J. Peron 
18597eae78a4SChristian S.J. Peron 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1860f8dc4716SMike Smith }
1861f8dc4716SMike Smith 
1862da626c17SBill Paul void
186319ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
18645f7a7923SSam Leffler {
18657eae78a4SChristian S.J. Peron 
18667eae78a4SChristian S.J. Peron 	*driverp = &bp_null;
18675f7a7923SSam Leffler }
18685f7a7923SSam Leffler 
18695f7a7923SSam Leffler void
187019ba8395SChristian S.J. Peron bpfdetach(struct ifnet *ifp)
1871da626c17SBill Paul {
1872da626c17SBill Paul }
1873da626c17SBill Paul 
1874f8dc4716SMike Smith u_int
187519ba8395SChristian S.J. Peron bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1876f8dc4716SMike Smith {
1877f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1878f8dc4716SMike Smith }
1879f8dc4716SMike Smith 
18805bb5f2c9SPeter Wemm int
188119ba8395SChristian S.J. Peron bpf_validate(const struct bpf_insn *f, int len)
18825bb5f2c9SPeter Wemm {
18835bb5f2c9SPeter Wemm 	return 0;		/* false */
18845bb5f2c9SPeter Wemm }
18855bb5f2c9SPeter Wemm 
18865bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1887