bpf.c revision 175903
1100966Siwasaki/*-
2100966Siwasaki * Copyright (c) 1990, 1991, 1993
3100966Siwasaki *	The Regents of the University of California.  All rights reserved.
4100966Siwasaki *
5100966Siwasaki * This code is derived from the Stanford/CMU enet packet filter,
6100966Siwasaki * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7100966Siwasaki * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8217365Sjkim * Berkeley Laboratory.
9245582Sjkim *
10100966Siwasaki * Redistribution and use in source and binary forms, with or without
11100966Siwasaki * modification, are permitted provided that the following conditions
12217365Sjkim * are met:
13217365Sjkim * 1. Redistributions of source code must retain the above copyright
14217365Sjkim *    notice, this list of conditions and the following disclaimer.
15217365Sjkim * 2. Redistributions in binary form must reproduce the above copyright
16217365Sjkim *    notice, this list of conditions and the following disclaimer in the
17217365Sjkim *    documentation and/or other materials provided with the distribution.
18217365Sjkim * 4. Neither the name of the University nor the names of its contributors
19217365Sjkim *    may be used to endorse or promote products derived from this software
20217365Sjkim *    without specific prior written permission.
21217365Sjkim *
22217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23217365Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24217365Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25217365Sjkim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26100966Siwasaki * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30100966Siwasaki * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31217365Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32217365Sjkim * SUCH DAMAGE.
33217365Sjkim *
34217365Sjkim *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
35217365Sjkim */
36217365Sjkim
37217365Sjkim#include <sys/cdefs.h>
38217365Sjkim__FBSDID("$FreeBSD: head/sys/net/bpf.c 175903 2008-02-02 20:35:05Z rwatson $");
39217365Sjkim
40217365Sjkim#include "opt_bpf.h"
41217365Sjkim#include "opt_mac.h"
42217365Sjkim#include "opt_netgraph.h"
43217365Sjkim
44100966Siwasaki#include <sys/types.h>
45100966Siwasaki#include <sys/param.h>
46100966Siwasaki#include <sys/systm.h>
47100966Siwasaki#include <sys/conf.h>
48193341Sjkim#include <sys/fcntl.h>
49193341Sjkim#include <sys/malloc.h>
50193341Sjkim#include <sys/mbuf.h>
51193341Sjkim#include <sys/time.h>
52100966Siwasaki#include <sys/priv.h>
53100966Siwasaki#include <sys/proc.h>
54100966Siwasaki#include <sys/signalvar.h>
55100966Siwasaki#include <sys/filio.h>
56100966Siwasaki#include <sys/sockio.h>
57193267Sjkim#include <sys/ttycom.h>
58100966Siwasaki#include <sys/uio.h>
59193267Sjkim
60193267Sjkim#include <sys/event.h>
61193267Sjkim#include <sys/file.h>
62193267Sjkim#include <sys/poll.h>
63193267Sjkim#include <sys/proc.h>
64100966Siwasaki
65100966Siwasaki#include <sys/socket.h>
66100966Siwasaki
67100966Siwasaki#include <net/if.h>
68100966Siwasaki#include <net/bpf.h>
69151937Sjkim#ifdef BPF_JITTER
70151937Sjkim#include <net/bpf_jitter.h>
71241973Sjkim#endif
72100966Siwasaki#include <net/bpfdesc.h>
73151937Sjkim
74241973Sjkim#include <netinet/in.h>
75100966Siwasaki#include <netinet/if_ether.h>
76100966Siwasaki#include <sys/kernel.h>
77100966Siwasaki#include <sys/sysctl.h>
78100966Siwasaki
79100966Siwasaki#include <net80211/ieee80211_freebsd.h>
80241973Sjkim
81100966Siwasaki#include <security/mac/mac_framework.h>
82100966Siwasaki
83100966Siwasakistatic MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
84100966Siwasaki
85100966Siwasaki#if defined(DEV_BPF) || defined(NETGRAPH_BPF)
86100966Siwasaki
87100966Siwasaki#define PRINET  26			/* interruptible */
88100966Siwasaki
89100966Siwasaki#define	M_SKIP_BPF	M_SKIP_FIREWALL
90100966Siwasaki
91100966Siwasaki/*
92100966Siwasaki * bpf_iflist is a list of BPF interface structures, each corresponding to a
93100966Siwasaki * specific DLT.  The same network interface might have several BPF interface
94100966Siwasaki * structures registered by different layers in the stack (i.e., 802.11
95100966Siwasaki * frames, ethernet frames, etc).
96100966Siwasaki */
97167802Sjkimstatic LIST_HEAD(, bpf_if)	bpf_iflist;
98100966Siwasakistatic struct mtx	bpf_mtx;		/* bpf global lock */
99100966Siwasakistatic int		bpf_bpfd_cnt;
100100966Siwasaki
101100966Siwasakistatic void	bpf_allocbufs(struct bpf_d *);
102100966Siwasakistatic void	bpf_attachd(struct bpf_d *, struct bpf_if *);
103100966Siwasakistatic void	bpf_detachd(struct bpf_d *);
104100966Siwasakistatic void	bpf_freed(struct bpf_d *);
105100966Siwasakistatic void	bpf_mcopy(const void *, void *, size_t);
106100966Siwasakistatic int	bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
107100966Siwasaki		    struct sockaddr *, int *, struct bpf_insn *);
108100966Siwasakistatic int	bpf_setif(struct bpf_d *, struct ifreq *);
109100966Siwasakistatic void	bpf_timed_out(void *);
110100966Siwasakistatic __inline void
111100966Siwasaki		bpf_wakeup(struct bpf_d *);
112100966Siwasakistatic void	catchpacket(struct bpf_d *, u_char *, u_int,
113100966Siwasaki		    u_int, void (*)(const void *, void *, size_t),
114100966Siwasaki		    struct timeval *);
115100966Siwasakistatic void	reset_d(struct bpf_d *);
116100966Siwasakistatic int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
117100966Siwasakistatic int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
118100966Siwasakistatic int	bpf_setdlt(struct bpf_d *, u_int);
119100966Siwasakistatic void	filt_bpfdetach(struct knote *);
120100966Siwasakistatic int	filt_bpfread(struct knote *, long);
121100966Siwasakistatic void	bpf_drvinit(void *);
122100966Siwasakistatic void	bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
123100966Siwasakistatic int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
124100966Siwasaki
125100966SiwasakiSYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
126100966Siwasakistatic int bpf_bufsize = 4096;
127100966SiwasakiSYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
128100966Siwasaki    &bpf_bufsize, 0, "Default bpf buffer size");
129100966Siwasakistatic int bpf_maxbufsize = BPF_MAXBUFSIZE;
130100966SiwasakiSYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
131167802Sjkim    &bpf_maxbufsize, 0, "Maximum bpf buffer size");
132100966Siwasakistatic int bpf_maxinsns = BPF_MAXINSNS;
133100966SiwasakiSYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
134100966Siwasaki    &bpf_maxinsns, 0, "Maximum bpf program instructions");
135100966SiwasakiSYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
136100966Siwasaki    bpf_stats_sysctl, "bpf statistics portal");
137100966Siwasaki
138100966Siwasakistatic	d_open_t	bpfopen;
139100966Siwasakistatic	d_close_t	bpfclose;
140100966Siwasakistatic	d_read_t	bpfread;
141100966Siwasakistatic	d_write_t	bpfwrite;
142100966Siwasakistatic	d_ioctl_t	bpfioctl;
143100966Siwasakistatic	d_poll_t	bpfpoll;
144167802Sjkimstatic	d_kqfilter_t	bpfkqfilter;
145167802Sjkim
146100966Siwasakistatic struct cdevsw bpf_cdevsw = {
147100966Siwasaki	.d_version =	D_VERSION,
148100966Siwasaki	.d_open =	bpfopen,
149100966Siwasaki	.d_close =	bpfclose,
150100966Siwasaki	.d_read =	bpfread,
151100966Siwasaki	.d_write =	bpfwrite,
152100966Siwasaki	.d_ioctl =	bpfioctl,
153100966Siwasaki	.d_poll =	bpfpoll,
154100966Siwasaki	.d_name =	"bpf",
155100966Siwasaki	.d_kqfilter =	bpfkqfilter,
156100966Siwasaki};
157100966Siwasaki
158100966Siwasakistatic struct filterops bpfread_filtops =
159100966Siwasaki	{ 1, NULL, filt_bpfdetach, filt_bpfread };
160100966Siwasaki
161167802Sjkimstatic int
162100966Siwasakibpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
163167802Sjkim    struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
164100966Siwasaki{
165100966Siwasaki	const struct ieee80211_bpf_params *p;
166100966Siwasaki	struct ether_header *eh;
167100966Siwasaki	struct mbuf *m;
168100966Siwasaki	int error;
169128212Snjl	int len;
170128212Snjl	int hlen;
171241973Sjkim	int slen;
172100966Siwasaki
173128212Snjl	/*
174241973Sjkim	 * Build a sockaddr based on the data link layer type.
175100966Siwasaki	 * We do this at this level because the ethernet header
176100966Siwasaki	 * is copied directly into the data field of the sockaddr.
177100966Siwasaki	 * In the case of SLIP, there is no header and the packet
178100966Siwasaki	 * is forwarded as is.
179241973Sjkim	 * Also, we are careful to leave room at the front of the mbuf
180100966Siwasaki	 * for the link level header.
181100966Siwasaki	 */
182100966Siwasaki	switch (linktype) {
183100966Siwasaki
184100966Siwasaki	case DLT_SLIP:
185100966Siwasaki		sockp->sa_family = AF_INET;
186100966Siwasaki		hlen = 0;
187100966Siwasaki		break;
188100966Siwasaki
189100966Siwasaki	case DLT_EN10MB:
190100966Siwasaki		sockp->sa_family = AF_UNSPEC;
191100966Siwasaki		/* XXX Would MAXLINKHDR be better? */
192167802Sjkim		hlen = ETHER_HDR_LEN;
193100966Siwasaki		break;
194100966Siwasaki
195100966Siwasaki	case DLT_FDDI:
196100966Siwasaki		sockp->sa_family = AF_IMPLINK;
197167802Sjkim		hlen = 0;
198100966Siwasaki		break;
199100966Siwasaki
200167802Sjkim	case DLT_RAW:
201129684Snjl		sockp->sa_family = AF_UNSPEC;
202167802Sjkim		hlen = 0;
203167802Sjkim		break;
204167802Sjkim
205167802Sjkim	case DLT_NULL:
206167802Sjkim		/*
207167802Sjkim		 * null interface types require a 4 byte pseudo header which
208167802Sjkim		 * corresponds to the address family of the packet.
209167802Sjkim		 */
210200553Sjkim		sockp->sa_family = AF_UNSPEC;
211167802Sjkim		hlen = 4;
212167802Sjkim		break;
213167802Sjkim
214167802Sjkim	case DLT_ATM_RFC1483:
215167802Sjkim		/*
216167802Sjkim		 * en atm driver requires 4-byte atm pseudo header.
217100966Siwasaki		 * though it isn't standard, vpi:vci needs to be
218249663Sjkim		 * specified anyway.
219249663Sjkim		 */
220249663Sjkim		sockp->sa_family = AF_UNSPEC;
221249663Sjkim		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
222249663Sjkim		break;
223249663Sjkim
224100966Siwasaki	case DLT_PPP:
225249663Sjkim		sockp->sa_family = AF_UNSPEC;
226249663Sjkim		hlen = 4;	/* This should match PPP_HDRLEN */
227249663Sjkim		break;
228249663Sjkim
229249663Sjkim	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
230249663Sjkim		sockp->sa_family = AF_IEEE80211;
231249663Sjkim		hlen = 0;
232249663Sjkim		break;
233249663Sjkim
234249663Sjkim	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
235249663Sjkim		sockp->sa_family = AF_IEEE80211;
236249663Sjkim		sockp->sa_len = 12;	/* XXX != 0 */
237249663Sjkim		hlen = sizeof(struct ieee80211_bpf_params);
238249663Sjkim		break;
239249663Sjkim
240249663Sjkim	default:
241249663Sjkim		return (EIO);
242249663Sjkim	}
243249663Sjkim
244249663Sjkim	len = uio->uio_resid;
245249663Sjkim
246249663Sjkim	if (len - hlen > ifp->if_mtu)
247249663Sjkim		return (EMSGSIZE);
248249663Sjkim
249249663Sjkim	if ((unsigned)len > MCLBYTES)
250249663Sjkim		return (EIO);
251249663Sjkim
252249663Sjkim	if (len > MHLEN) {
253249663Sjkim		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
254249663Sjkim	} else {
255249663Sjkim		MGETHDR(m, M_TRYWAIT, MT_DATA);
256249663Sjkim	}
257249663Sjkim	if (m == NULL)
258249663Sjkim		return (ENOBUFS);
259249663Sjkim	m->m_pkthdr.len = m->m_len = len;
260100966Siwasaki	m->m_pkthdr.rcvif = NULL;
261100966Siwasaki	*mp = m;
262249663Sjkim
263249663Sjkim	if (m->m_len < hlen) {
264249663Sjkim		error = EPERM;
265249663Sjkim		goto bad;
266249663Sjkim	}
267249663Sjkim
268249663Sjkim	error = uiomove(mtod(m, u_char *), len, uio);
269249663Sjkim	if (error)
270249663Sjkim		goto bad;
271249663Sjkim
272249663Sjkim	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
273249663Sjkim	if (slen == 0) {
274249663Sjkim		error = EPERM;
275100966Siwasaki		goto bad;
276100966Siwasaki	}
277100966Siwasaki
278100966Siwasaki	/* Check for multicast destination */
279167802Sjkim	switch (linktype) {
280249663Sjkim	case DLT_EN10MB:
281167802Sjkim		eh = mtod(m, struct ether_header *);
282100966Siwasaki		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
283167802Sjkim			if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
284167802Sjkim			    ETHER_ADDR_LEN) == 0)
285100966Siwasaki				m->m_flags |= M_BCAST;
286100966Siwasaki			else
287167802Sjkim				m->m_flags |= M_MCAST;
288167802Sjkim		}
289249663Sjkim		break;
290100966Siwasaki	}
291167802Sjkim
292249663Sjkim	/*
293100966Siwasaki	 * Make room for link header, and copy it to sockaddr
294100966Siwasaki	 */
295167802Sjkim	if (hlen != 0) {
296100966Siwasaki		if (sockp->sa_family == AF_IEEE80211) {
297100966Siwasaki			/*
298249663Sjkim			 * Collect true length from the parameter header
299249663Sjkim			 * NB: sockp is known to be zero'd so if we do a
300100966Siwasaki			 *     short copy unspecified parameters will be
301100966Siwasaki			 *     zero.
302249663Sjkim			 * NB: packet may not be aligned after stripping
303249663Sjkim			 *     bpf params
304249663Sjkim			 * XXX check ibp_vers
305100966Siwasaki			 */
306249663Sjkim			p = mtod(m, const struct ieee80211_bpf_params *);
307249663Sjkim			hlen = p->ibp_len;
308100966Siwasaki			if (hlen > sizeof(sockp->sa_data)) {
309249663Sjkim				error = EINVAL;
310249663Sjkim				goto bad;
311100966Siwasaki			}
312249663Sjkim		}
313167802Sjkim		bcopy(m->m_data, sockp->sa_data, hlen);
314249663Sjkim	}
315249663Sjkim	*hdrlen = hlen;
316249663Sjkim
317249663Sjkim	return (0);
318249663Sjkimbad:
319249663Sjkim	m_freem(m);
320249663Sjkim	return (error);
321249663Sjkim}
322249663Sjkim
323249663Sjkim/*
324249663Sjkim * Attach file to the bpf interface, i.e. make d listen on bp.
325249663Sjkim */
326249663Sjkimstatic void
327249663Sjkimbpf_attachd(struct bpf_d *d, struct bpf_if *bp)
328249663Sjkim{
329249663Sjkim	/*
330249663Sjkim	 * Point d at bp, and add d to the interface's list of listeners.
331249663Sjkim	 * Finally, point the driver's bpf cookie at the interface so
332249663Sjkim	 * it will divert packets to bpf.
333249663Sjkim	 */
334249663Sjkim	BPFIF_LOCK(bp);
335249663Sjkim	d->bd_bif = bp;
336249663Sjkim	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
337249663Sjkim
338249663Sjkim	bpf_bpfd_cnt++;
339100966Siwasaki	BPFIF_UNLOCK(bp);
340249663Sjkim}
341249663Sjkim
342100966Siwasaki/*
343249663Sjkim * Detach a file from its interface.
344100966Siwasaki */
345249663Sjkimstatic void
346249663Sjkimbpf_detachd(struct bpf_d *d)
347249663Sjkim{
348249663Sjkim	int error;
349249663Sjkim	struct bpf_if *bp;
350249663Sjkim	struct ifnet *ifp;
351249663Sjkim
352249663Sjkim	bp = d->bd_bif;
353249663Sjkim	BPFIF_LOCK(bp);
354249663Sjkim	BPFD_LOCK(d);
355249663Sjkim	ifp = d->bd_bif->bif_ifp;
356249663Sjkim
357249663Sjkim	/*
358249663Sjkim	 * Remove d from the interface's descriptor list.
359249663Sjkim	 */
360249663Sjkim	LIST_REMOVE(d, bd_next);
361249663Sjkim
362249663Sjkim	bpf_bpfd_cnt--;
363249663Sjkim	d->bd_bif = NULL;
364249663Sjkim	BPFD_UNLOCK(d);
365249663Sjkim	BPFIF_UNLOCK(bp);
366249663Sjkim
367249663Sjkim	/*
368249663Sjkim	 * Check if this descriptor had requested promiscuous mode.
369249663Sjkim	 * If so, turn it off.
370100966Siwasaki	 */
371249663Sjkim	if (d->bd_promisc) {
372249663Sjkim		d->bd_promisc = 0;
373249663Sjkim		error = ifpromisc(ifp, 0);
374249663Sjkim		if (error != 0 && error != ENXIO) {
375249663Sjkim			/*
376249663Sjkim			 * ENXIO can happen if a pccard is unplugged
377249663Sjkim			 * Something is really wrong if we were able to put
378100966Siwasaki			 * the driver into promiscuous mode, but can't
379249663Sjkim			 * take it out.
380249663Sjkim			 */
381249663Sjkim			if_printf(bp->bif_ifp,
382100966Siwasaki				"bpf_detach: ifpromisc failed (%d)\n", error);
383249663Sjkim		}
384100966Siwasaki	}
385167802Sjkim}
386249663Sjkim
387100966Siwasaki/*
388249663Sjkim * Open ethernet device.  Returns ENXIO for illegal minor device number,
389249663Sjkim * EBUSY if file is open by another process.
390249663Sjkim */
391249663Sjkim/* ARGSUSED */
392249663Sjkimstatic	int
393100966Siwasakibpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
394100966Siwasaki{
395100966Siwasaki	struct bpf_d *d;
396100966Siwasaki
397100966Siwasaki	mtx_lock(&bpf_mtx);
398100966Siwasaki	d = dev->si_drv1;
399167802Sjkim	/*
400100966Siwasaki	 * Each minor can be opened by only one process.  If the requested
401100966Siwasaki	 * minor is in use, return EBUSY.
402100966Siwasaki	 */
403100966Siwasaki	if (d != NULL) {
404100966Siwasaki		mtx_unlock(&bpf_mtx);
405167802Sjkim		return (EBUSY);
406167802Sjkim	}
407100966Siwasaki	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
408100966Siwasaki	mtx_unlock(&bpf_mtx);
409100966Siwasaki
410100966Siwasaki	if ((dev->si_flags & SI_NAMED) == 0)
411100966Siwasaki		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
412100966Siwasaki		    "bpf%d", dev2unit(dev));
413100966Siwasaki	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
414100966Siwasaki	dev->si_drv1 = d;
415100966Siwasaki	d->bd_bufsize = bpf_bufsize;
416100966Siwasaki	d->bd_sig = SIGIO;
417167802Sjkim	d->bd_direction = BPF_D_INOUT;
418100966Siwasaki	d->bd_pid = td->td_proc->p_pid;
419100966Siwasaki#ifdef MAC
420100966Siwasaki	mac_bpfdesc_init(d);
421100966Siwasaki	mac_bpfdesc_create(td->td_ucred, d);
422100966Siwasaki#endif
423193267Sjkim	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
424193267Sjkim	callout_init(&d->bd_callout, CALLOUT_MPSAFE);
425193267Sjkim	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
426193267Sjkim
427167802Sjkim	return (0);
428167802Sjkim}
429167802Sjkim
430167802Sjkim/*
431100966Siwasaki * Close the descriptor by detaching it from its interface,
432100966Siwasaki * deallocating its buffers, and marking it free.
433100966Siwasaki */
434100966Siwasaki/* ARGSUSED */
435151937Sjkimstatic	int
436167802Sjkimbpfclose(struct cdev *dev, int flags, int fmt, struct thread *td)
437100966Siwasaki{
438100966Siwasaki	struct bpf_d *d = dev->si_drv1;
439100966Siwasaki
440167802Sjkim	BPFD_LOCK(d);
441167802Sjkim	if (d->bd_state == BPF_WAITING)
442100966Siwasaki		callout_stop(&d->bd_callout);
443100966Siwasaki	d->bd_state = BPF_IDLE;
444100966Siwasaki	BPFD_UNLOCK(d);
445129684Snjl	funsetown(&d->bd_sigio);
446129684Snjl	mtx_lock(&bpf_mtx);
447100966Siwasaki	if (d->bd_bif)
448100966Siwasaki		bpf_detachd(d);
449100966Siwasaki	mtx_unlock(&bpf_mtx);
450167802Sjkim	selwakeuppri(&d->bd_sel, PRINET);
451167802Sjkim#ifdef MAC
452167802Sjkim	mac_bpfdesc_destroy(d);
453167802Sjkim#endif /* MAC */
454100966Siwasaki	knlist_destroy(&d->bd_sel.si_note);
455100966Siwasaki	bpf_freed(d);
456100966Siwasaki	dev->si_drv1 = NULL;
457100966Siwasaki	free(d, M_BPF);
458100966Siwasaki
459100966Siwasaki	return (0);
460167802Sjkim}
461100966Siwasaki
462129684Snjl
463167802Sjkim/*
464167802Sjkim * Rotate the packet buffers in descriptor d.  Move the store buffer
465100966Siwasaki * into the hold slot, and the free buffer into the store slot.
466167802Sjkim * Zero the length of the new store buffer.
467167802Sjkim */
468167802Sjkim#define ROTATE_BUFFERS(d) \
469167802Sjkim	(d)->bd_hbuf = (d)->bd_sbuf; \
470167802Sjkim	(d)->bd_hlen = (d)->bd_slen; \
471167802Sjkim	(d)->bd_sbuf = (d)->bd_fbuf; \
472100966Siwasaki	(d)->bd_slen = 0; \
473100966Siwasaki	(d)->bd_fbuf = NULL;
474167802Sjkim/*
475167802Sjkim *  bpfread - read next chunk of packets from buffers
476167802Sjkim */
477167802Sjkimstatic	int
478167802Sjkimbpfread(struct cdev *dev, struct uio *uio, int ioflag)
479167802Sjkim{
480100966Siwasaki	struct bpf_d *d = dev->si_drv1;
481100966Siwasaki	int timed_out;
482100966Siwasaki	int error;
483167802Sjkim
484100966Siwasaki	/*
485100966Siwasaki	 * Restrict application to use a buffer the same size as
486167802Sjkim	 * as kernel buffers.
487100966Siwasaki	 */
488100966Siwasaki	if (uio->uio_resid != d->bd_bufsize)
489100966Siwasaki		return (EINVAL);
490167802Sjkim
491100966Siwasaki	BPFD_LOCK(d);
492167802Sjkim	d->bd_pid = curthread->td_proc->p_pid;
493100966Siwasaki	if (d->bd_state == BPF_WAITING)
494100966Siwasaki		callout_stop(&d->bd_callout);
495193267Sjkim	timed_out = (d->bd_state == BPF_TIMED_OUT);
496193267Sjkim	d->bd_state = BPF_IDLE;
497193267Sjkim	/*
498193267Sjkim	 * If the hold buffer is empty, then do a timed sleep, which
499193267Sjkim	 * ends when the timeout expires or when enough packets
500193267Sjkim	 * have arrived to fill the store buffer.
501193267Sjkim	 */
502193267Sjkim	while (d->bd_hbuf == NULL) {
503193267Sjkim		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
504193267Sjkim			/*
505193267Sjkim			 * A packet(s) either arrived since the previous
506193267Sjkim			 * read or arrived while we were asleep.
507193267Sjkim			 * Rotate the buffers and return what's here.
508193267Sjkim			 */
509193267Sjkim			ROTATE_BUFFERS(d);
510193267Sjkim			break;
511193267Sjkim		}
512193267Sjkim
513193267Sjkim		/*
514193267Sjkim		 * No data is available, check to see if the bpf device
515193267Sjkim		 * is still pointed at a real interface.  If not, return
516193267Sjkim		 * ENXIO so that the userland process knows to rebind
517193267Sjkim		 * it before using it again.
518193267Sjkim		 */
519193267Sjkim		if (d->bd_bif == NULL) {
520193267Sjkim			BPFD_UNLOCK(d);
521193267Sjkim			return (ENXIO);
522193267Sjkim		}
523193267Sjkim
524193267Sjkim		if (ioflag & O_NONBLOCK) {
525193267Sjkim			BPFD_UNLOCK(d);
526193267Sjkim			return (EWOULDBLOCK);
527193267Sjkim		}
528193267Sjkim		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
529193267Sjkim		     "bpf", d->bd_rtout);
530193267Sjkim		if (error == EINTR || error == ERESTART) {
531193267Sjkim			BPFD_UNLOCK(d);
532193267Sjkim			return (error);
533193267Sjkim		}
534193267Sjkim		if (error == EWOULDBLOCK) {
535193267Sjkim			/*
536193267Sjkim			 * On a timeout, return what's in the buffer,
537193267Sjkim			 * which may be nothing.  If there is something
538193267Sjkim			 * in the store buffer, we can rotate the buffers.
539193267Sjkim			 */
540193267Sjkim			if (d->bd_hbuf)
541193267Sjkim				/*
542193267Sjkim				 * We filled up the buffer in between
543193267Sjkim				 * getting the timeout and arriving
544193267Sjkim				 * here, so we don't need to rotate.
545193267Sjkim				 */
546193267Sjkim				break;
547193267Sjkim
548193267Sjkim			if (d->bd_slen == 0) {
549193267Sjkim				BPFD_UNLOCK(d);
550193267Sjkim				return (0);
551193267Sjkim			}
552193267Sjkim			ROTATE_BUFFERS(d);
553193267Sjkim			break;
554193267Sjkim		}
555193267Sjkim	}
556193267Sjkim	/*
557193267Sjkim	 * At this point, we know we have something in the hold slot.
558193267Sjkim	 */
559193267Sjkim	BPFD_UNLOCK(d);
560193267Sjkim
561193267Sjkim	/*
562193267Sjkim	 * Move data from hold buffer into user space.
563193267Sjkim	 * We know the entire buffer is transferred since
564193267Sjkim	 * we checked above that the read buffer is bpf_bufsize bytes.
565193267Sjkim	 *
566193267Sjkim	 * XXXRW: More synchronization needed here: what if a second thread
567193267Sjkim	 * issues a read on the same fd at the same time?  Don't want this
568193267Sjkim	 * getting invalidated.
569193267Sjkim	 */
570193267Sjkim	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
571100966Siwasaki
572100966Siwasaki	BPFD_LOCK(d);
573100966Siwasaki	d->bd_fbuf = d->bd_hbuf;
574100966Siwasaki	d->bd_hbuf = NULL;
575100966Siwasaki	d->bd_hlen = 0;
576199337Sjkim	BPFD_UNLOCK(d);
577199337Sjkim
578199337Sjkim	return (error);
579199337Sjkim}
580199337Sjkim
581100966Siwasaki/*
582100966Siwasaki * If there are processes sleeping on this descriptor, wake them up.
583100966Siwasaki */
584100966Siwasakistatic __inline void
585100966Siwasakibpf_wakeup(struct bpf_d *d)
586100966Siwasaki{
587100966Siwasaki
588100966Siwasaki	BPFD_LOCK_ASSERT(d);
589199337Sjkim	if (d->bd_state == BPF_WAITING) {
590199337Sjkim		callout_stop(&d->bd_callout);
591100966Siwasaki		d->bd_state = BPF_IDLE;
592100966Siwasaki	}
593100966Siwasaki	wakeup(d);
594100966Siwasaki	if (d->bd_async && d->bd_sig && d->bd_sigio)
595100966Siwasaki		pgsigio(&d->bd_sigio, d->bd_sig, 0);
596199337Sjkim
597199337Sjkim	selwakeuppri(&d->bd_sel, PRINET);
598199337Sjkim	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
599100966Siwasaki}
600100966Siwasaki
601100966Siwasakistatic void
602100966Siwasakibpf_timed_out(void *arg)
603100966Siwasaki{
604100966Siwasaki	struct bpf_d *d = (struct bpf_d *)arg;
605100966Siwasaki
606100966Siwasaki	BPFD_LOCK(d);
607199337Sjkim	if (d->bd_state == BPF_WAITING) {
608199337Sjkim		d->bd_state = BPF_TIMED_OUT;
609100966Siwasaki		if (d->bd_slen != 0)
610100966Siwasaki			bpf_wakeup(d);
611100966Siwasaki	}
612100966Siwasaki	BPFD_UNLOCK(d);
613100966Siwasaki}
614100966Siwasaki
615167802Sjkimstatic int
616100966Siwasakibpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
617100966Siwasaki{
618100966Siwasaki	struct bpf_d *d = dev->si_drv1;
619100966Siwasaki	struct ifnet *ifp;
620167802Sjkim	struct mbuf *m, *mc;
621167802Sjkim	struct sockaddr dst;
622199337Sjkim	int error, hlen;
623100966Siwasaki
624100966Siwasaki	d->bd_pid = curthread->td_proc->p_pid;
625100966Siwasaki	if (d->bd_bif == NULL)
626100966Siwasaki		return (ENXIO);
627100966Siwasaki
628193267Sjkim	ifp = d->bd_bif->bif_ifp;
629193267Sjkim
630193267Sjkim	if ((ifp->if_flags & IFF_UP) == 0)
631193267Sjkim		return (ENETDOWN);
632193267Sjkim
633193267Sjkim	if (uio->uio_resid == 0)
634193267Sjkim		return (0);
635193267Sjkim
636193267Sjkim	bzero(&dst, sizeof(dst));
637100966Siwasaki	m = NULL;
638193267Sjkim	hlen = 0;
639193267Sjkim	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp,
640193267Sjkim	    &m, &dst, &hlen, d->bd_wfilter);
641241973Sjkim	if (error)
642193267Sjkim		return (error);
643193267Sjkim
644193267Sjkim	if (d->bd_hdrcmplt)
645193267Sjkim		dst.sa_family = pseudo_AF_HDRCMPLT;
646193267Sjkim
647193267Sjkim	if (d->bd_feedback) {
648193267Sjkim		mc = m_dup(m, M_DONTWAIT);
649193267Sjkim		if (mc != NULL)
650100966Siwasaki			mc->m_pkthdr.rcvif = ifp;
651100966Siwasaki		/* XXX Do not return the same packet twice. */
652100966Siwasaki		if (d->bd_direction == BPF_D_INOUT)
653193267Sjkim			m->m_flags |= M_SKIP_BPF;
654100966Siwasaki	} else
655100966Siwasaki		mc = NULL;
656151937Sjkim
657199337Sjkim	m->m_pkthdr.len -= hlen;
658199337Sjkim	m->m_len -= hlen;
659100966Siwasaki	m->m_data += hlen;	/* XXX */
660100966Siwasaki
661193267Sjkim#ifdef MAC
662193267Sjkim	BPFD_LOCK(d);
663193267Sjkim	mac_bpfdesc_create_mbuf(d, m);
664100966Siwasaki	if (mc != NULL)
665100966Siwasaki		mac_bpfdesc_create_mbuf(d, mc);
666100966Siwasaki	BPFD_UNLOCK(d);
667167802Sjkim#endif
668100966Siwasaki
669167802Sjkim	error = (*ifp->if_output)(ifp, m, &dst, NULL);
670100966Siwasaki
671100966Siwasaki	if (mc != NULL) {
672100966Siwasaki		if (error == 0)
673100966Siwasaki			(*ifp->if_input)(ifp, mc);
674100966Siwasaki		else
675100966Siwasaki			m_freem(mc);
676100966Siwasaki	}
677100966Siwasaki
678100966Siwasaki	return (error);
679100966Siwasaki}
680100966Siwasaki
681100966Siwasaki/*
682100966Siwasaki * Reset a descriptor by flushing its packet buffer and clearing the
683100966Siwasaki * receive and drop counts.
684100966Siwasaki */
685100966Siwasakistatic void
686100966Siwasakireset_d(struct bpf_d *d)
687100966Siwasaki{
688100966Siwasaki
689100966Siwasaki	mtx_assert(&d->bd_mtx, MA_OWNED);
690100966Siwasaki	if (d->bd_hbuf) {
691117521Snjl		/* Free the hold buffer. */
692100966Siwasaki		d->bd_fbuf = d->bd_hbuf;
693100966Siwasaki		d->bd_hbuf = NULL;
694100966Siwasaki	}
695241973Sjkim	d->bd_slen = 0;
696241973Sjkim	d->bd_hlen = 0;
697193267Sjkim	d->bd_rcount = 0;
698193267Sjkim	d->bd_dcount = 0;
699197104Sjkim	d->bd_fcount = 0;
700100966Siwasaki}
701100966Siwasaki
702100966Siwasaki/*
703100966Siwasaki *  FIONREAD		Check for read packet available.
704100966Siwasaki *  SIOCGIFADDR		Get interface address - convenient hook to driver.
705100966Siwasaki *  BIOCGBLEN		Get buffer len [for read()].
706100966Siwasaki *  BIOCSETF		Set ethernet read filter.
707100966Siwasaki *  BIOCSETWF		Set ethernet write filter.
708200553Sjkim *  BIOCFLUSH		Flush read packet buffer.
709100966Siwasaki *  BIOCPROMISC		Put interface into promiscuous mode.
710100966Siwasaki *  BIOCGDLT		Get link layer type.
711100966Siwasaki *  BIOCGETIF		Get interface name.
712100966Siwasaki *  BIOCSETIF		Set interface.
713100966Siwasaki *  BIOCSRTIMEOUT	Set read timeout.
714100966Siwasaki *  BIOCGRTIMEOUT	Get read timeout.
715100966Siwasaki *  BIOCGSTATS		Get packet stats.
716100966Siwasaki *  BIOCIMMEDIATE	Set immediate mode.
717100966Siwasaki *  BIOCVERSION		Get filter language version.
718100966Siwasaki *  BIOCGHDRCMPLT	Get "header already complete" flag
719100966Siwasaki *  BIOCSHDRCMPLT	Set "header already complete" flag
720202771Sjkim *  BIOCGDIRECTION	Get packet direction flag
721202771Sjkim *  BIOCSDIRECTION	Set packet direction flag
722202771Sjkim *  BIOCLOCK		Set "locked" flag
723202771Sjkim *  BIOCFEEDBACK	Set packet feedback mode.
724202771Sjkim */
725202771Sjkim/* ARGSUSED */
726202771Sjkimstatic	int
727202771Sjkimbpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
728202771Sjkim    struct thread *td)
729202771Sjkim{
730202771Sjkim	struct bpf_d *d = dev->si_drv1;
731202771Sjkim	int error = 0;
732202771Sjkim
733202771Sjkim	/*
734100966Siwasaki	 * Refresh PID associated with this descriptor.
735100966Siwasaki	 */
736100966Siwasaki	BPFD_LOCK(d);
737100966Siwasaki	d->bd_pid = td->td_proc->p_pid;
738100966Siwasaki	if (d->bd_state == BPF_WAITING)
739100966Siwasaki		callout_stop(&d->bd_callout);
740100966Siwasaki	d->bd_state = BPF_IDLE;
741100966Siwasaki	BPFD_UNLOCK(d);
742100966Siwasaki
743100966Siwasaki	if (d->bd_locked == 1) {
744100966Siwasaki		switch (cmd) {
745100966Siwasaki		case BIOCGBLEN:
746197104Sjkim		case BIOCFLUSH:
747197104Sjkim		case BIOCGDLT:
748197104Sjkim		case BIOCGDLTLIST:
749197104Sjkim		case BIOCGETIF:
750100966Siwasaki		case BIOCGRTIMEOUT:
751193267Sjkim		case BIOCGSTATS:
752193267Sjkim		case BIOCVERSION:
753193267Sjkim		case BIOCGRSIG:
754193267Sjkim		case BIOCGHDRCMPLT:
755100966Siwasaki		case BIOCFEEDBACK:
756100966Siwasaki		case FIONREAD:
757100966Siwasaki		case BIOCLOCK:
758100966Siwasaki		case BIOCSRTIMEOUT:
759100966Siwasaki		case BIOCIMMEDIATE:
760100966Siwasaki		case TIOCGPGRP:
761100966Siwasaki			break;
762100966Siwasaki		default:
763100966Siwasaki			return (EPERM);
764100966Siwasaki		}
765117521Snjl	}
766100966Siwasaki	switch (cmd) {
767193267Sjkim
768117521Snjl	default:
769100966Siwasaki		error = EINVAL;
770197104Sjkim		break;
771117521Snjl
772193267Sjkim	/*
773193267Sjkim	 * Check for read packet available.
774193267Sjkim	 */
775193267Sjkim	case FIONREAD:
776117521Snjl		{
777100966Siwasaki			int n;
778193267Sjkim
779167802Sjkim			BPFD_LOCK(d);
780193267Sjkim			n = d->bd_slen;
781193267Sjkim			if (d->bd_hbuf)
782193267Sjkim				n += d->bd_hlen;
783193267Sjkim			BPFD_UNLOCK(d);
784100966Siwasaki
785100966Siwasaki			*(int *)addr = n;
786100966Siwasaki			break;
787202771Sjkim		}
788202771Sjkim
789202771Sjkim	case SIOCGIFADDR:
790202771Sjkim		{
791202771Sjkim			struct ifnet *ifp;
792202771Sjkim
793202771Sjkim			if (d->bd_bif == NULL)
794202771Sjkim				error = EINVAL;
795202771Sjkim			else {
796202771Sjkim				ifp = d->bd_bif->bif_ifp;
797202771Sjkim				error = (*ifp->if_ioctl)(ifp, cmd, addr);
798202771Sjkim			}
799202771Sjkim			break;
800202771Sjkim		}
801202771Sjkim
802202771Sjkim	/*
803202771Sjkim	 * Get buffer len [for read()].
804202771Sjkim	 */
805202771Sjkim	case BIOCGBLEN:
806193267Sjkim		*(u_int *)addr = d->bd_bufsize;
807193267Sjkim		break;
808151937Sjkim
809151937Sjkim	/*
810100966Siwasaki	 * Set buffer length.
811100966Siwasaki	 */
812100966Siwasaki	case BIOCSBLEN:
813100966Siwasaki		if (d->bd_bif != NULL)
814100966Siwasaki			error = EINVAL;
815100966Siwasaki		else {
816100966Siwasaki			u_int size = *(u_int *)addr;
817100966Siwasaki
818100966Siwasaki			if (size > bpf_maxbufsize)
819100966Siwasaki				*(u_int *)addr = size = bpf_maxbufsize;
820100966Siwasaki			else if (size < BPF_MINBUFSIZE)
821100966Siwasaki				*(u_int *)addr = size = BPF_MINBUFSIZE;
822100966Siwasaki			d->bd_bufsize = size;
823100966Siwasaki		}
824100966Siwasaki		break;
825100966Siwasaki
826100966Siwasaki	/*
827100966Siwasaki	 * Set link layer read filter.
828100966Siwasaki	 */
829117521Snjl	case BIOCSETF:
830241973Sjkim	case BIOCSETWF:
831100966Siwasaki		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
832100966Siwasaki		break;
833100966Siwasaki
834100966Siwasaki	/*
835193267Sjkim	 * Flush read packet buffer.
836100966Siwasaki	 */
837100966Siwasaki	case BIOCFLUSH:
838100966Siwasaki		BPFD_LOCK(d);
839100966Siwasaki		reset_d(d);
840100966Siwasaki		BPFD_UNLOCK(d);
841114237Snjl		break;
842100966Siwasaki
843100966Siwasaki	/*
844100966Siwasaki	 * Put interface into promiscuous mode.
845100966Siwasaki	 */
846100966Siwasaki	case BIOCPROMISC:
847100966Siwasaki		if (d->bd_bif == NULL) {
848100966Siwasaki			/*
849100966Siwasaki			 * No interface attached yet.
850167802Sjkim			 */
851100966Siwasaki			error = EINVAL;
852100966Siwasaki			break;
853100966Siwasaki		}
854100966Siwasaki		if (d->bd_promisc == 0) {
855100966Siwasaki			error = ifpromisc(d->bd_bif->bif_ifp, 1);
856100966Siwasaki			if (error == 0)
857100966Siwasaki				d->bd_promisc = 1;
858100966Siwasaki		}
859100966Siwasaki		break;
860100966Siwasaki
861100966Siwasaki	/*
862100966Siwasaki	 * Get current data link type.
863100966Siwasaki	 */
864167802Sjkim	case BIOCGDLT:
865100966Siwasaki		if (d->bd_bif == NULL)
866100966Siwasaki			error = EINVAL;
867100966Siwasaki		else
868100966Siwasaki			*(u_int *)addr = d->bd_bif->bif_dlt;
869100966Siwasaki		break;
870100966Siwasaki
871100966Siwasaki	/*
872100966Siwasaki	 * Get a list of supported data link types.
873100966Siwasaki	 */
874100966Siwasaki	case BIOCGDLTLIST:
875100966Siwasaki		if (d->bd_bif == NULL)
876100966Siwasaki			error = EINVAL;
877100966Siwasaki		else
878100966Siwasaki			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
879100966Siwasaki		break;
880167802Sjkim
881167802Sjkim	/*
882199337Sjkim	 * Set data link type.
883100966Siwasaki	 */
884100966Siwasaki	case BIOCSDLT:
885100966Siwasaki		if (d->bd_bif == NULL)
886100966Siwasaki			error = EINVAL;
887100966Siwasaki		else
888167802Sjkim			error = bpf_setdlt(d, *(u_int *)addr);
889100966Siwasaki		break;
890167802Sjkim
891100966Siwasaki	/*
892100966Siwasaki	 * Get interface name.
893100966Siwasaki	 */
894100966Siwasaki	case BIOCGETIF:
895114237Snjl		if (d->bd_bif == NULL)
896107325Siwasaki			error = EINVAL;
897107325Siwasaki		else {
898100966Siwasaki			struct ifnet *const ifp = d->bd_bif->bif_ifp;
899100966Siwasaki			struct ifreq *const ifr = (struct ifreq *)addr;
900100966Siwasaki
901107325Siwasaki			strlcpy(ifr->ifr_name, ifp->if_xname,
902100966Siwasaki			    sizeof(ifr->ifr_name));
903100966Siwasaki		}
904100966Siwasaki		break;
905100966Siwasaki
906100966Siwasaki	/*
907100966Siwasaki	 * Set interface.
908100966Siwasaki	 */
909100966Siwasaki	case BIOCSETIF:
910100966Siwasaki		error = bpf_setif(d, (struct ifreq *)addr);
911100966Siwasaki		break;
912100966Siwasaki
913100966Siwasaki	/*
914100966Siwasaki	 * Set read timeout.
915100966Siwasaki	 */
916100966Siwasaki	case BIOCSRTIMEOUT:
917100966Siwasaki		{
918100966Siwasaki			struct timeval *tv = (struct timeval *)addr;
919100966Siwasaki
920100966Siwasaki			/*
921100966Siwasaki			 * Subtract 1 tick from tvtohz() since this isn't
922100966Siwasaki			 * a one-shot timer.
923100966Siwasaki			 */
924100966Siwasaki			if ((error = itimerfix(tv)) == 0)
925100966Siwasaki				d->bd_rtout = tvtohz(tv) - 1;
926100966Siwasaki			break;
927100966Siwasaki		}
928100966Siwasaki
929100966Siwasaki	/*
930100966Siwasaki	 * Get read timeout.
931100966Siwasaki	 */
932200553Sjkim	case BIOCGRTIMEOUT:
933100966Siwasaki		{
934100966Siwasaki			struct timeval *tv = (struct timeval *)addr;
935100966Siwasaki
936100966Siwasaki			tv->tv_sec = d->bd_rtout / hz;
937100966Siwasaki			tv->tv_usec = (d->bd_rtout % hz) * tick;
938100966Siwasaki			break;
939100966Siwasaki		}
940100966Siwasaki
941100966Siwasaki	/*
942100966Siwasaki	 * Get packet stats.
943100966Siwasaki	 */
944100966Siwasaki	case BIOCGSTATS:
945100966Siwasaki		{
946167802Sjkim			struct bpf_stat *bs = (struct bpf_stat *)addr;
947100966Siwasaki
948167802Sjkim			bs->bs_recv = d->bd_rcount;
949100966Siwasaki			bs->bs_drop = d->bd_dcount;
950100966Siwasaki			break;
951100966Siwasaki		}
952100966Siwasaki
953107325Siwasaki	/*
954107325Siwasaki	 * Set immediate mode.
955100966Siwasaki	 */
956100966Siwasaki	case BIOCIMMEDIATE:
957100966Siwasaki		d->bd_immediate = *(u_int *)addr;
958107325Siwasaki		break;
959100966Siwasaki
960100966Siwasaki	case BIOCVERSION:
961100966Siwasaki		{
962100966Siwasaki			struct bpf_version *bv = (struct bpf_version *)addr;
963100966Siwasaki
964100966Siwasaki			bv->bv_major = BPF_MAJOR_VERSION;
965100966Siwasaki			bv->bv_minor = BPF_MINOR_VERSION;
966100966Siwasaki			break;
967100966Siwasaki		}
968100966Siwasaki
969100966Siwasaki	/*
970100966Siwasaki	 * Get "header already complete" flag
971100966Siwasaki	 */
972100966Siwasaki	case BIOCGHDRCMPLT:
973100966Siwasaki		*(u_int *)addr = d->bd_hdrcmplt;
974100966Siwasaki		break;
975100966Siwasaki
976100966Siwasaki	/*
977100966Siwasaki	 * Set "header already complete" flag
978100966Siwasaki	 */
979100966Siwasaki	case BIOCSHDRCMPLT:
980100966Siwasaki		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
981100966Siwasaki		break;
982100966Siwasaki
983100966Siwasaki	/*
984100966Siwasaki	 * Get packet direction flag
985100966Siwasaki	 */
986100966Siwasaki	case BIOCGDIRECTION:
987200553Sjkim		*(u_int *)addr = d->bd_direction;
988100966Siwasaki		break;
989100966Siwasaki
990100966Siwasaki	/*
991100966Siwasaki	 * Set packet direction flag
992100966Siwasaki	 */
993100966Siwasaki	case BIOCSDIRECTION:
994100966Siwasaki		{
995100966Siwasaki			u_int	direction;
996100966Siwasaki
997100966Siwasaki			direction = *(u_int *)addr;
998100966Siwasaki			switch (direction) {
999100966Siwasaki			case BPF_D_IN:
1000100966Siwasaki			case BPF_D_INOUT:
1001167802Sjkim			case BPF_D_OUT:
1002100966Siwasaki				d->bd_direction = direction;
1003167802Sjkim				break;
1004100966Siwasaki			default:
1005100966Siwasaki				error = EINVAL;
1006100966Siwasaki			}
1007100966Siwasaki		}
1008107325Siwasaki		break;
1009107325Siwasaki
1010107325Siwasaki	case BIOCFEEDBACK:
1011100966Siwasaki		d->bd_feedback = *(u_int *)addr;
1012100966Siwasaki		break;
1013100966Siwasaki
1014107325Siwasaki	case BIOCLOCK:
1015100966Siwasaki		d->bd_locked = 1;
1016100966Siwasaki		break;
1017100966Siwasaki
1018100966Siwasaki	case FIONBIO:		/* Non-blocking I/O */
1019100966Siwasaki		break;
1020100966Siwasaki
1021100966Siwasaki	case FIOASYNC:		/* Send signal on receive packets */
1022100966Siwasaki		d->bd_async = *(int *)addr;
1023100966Siwasaki		break;
1024100966Siwasaki
1025100966Siwasaki	case FIOSETOWN:
1026100966Siwasaki		error = fsetown(*(int *)addr, &d->bd_sigio);
1027100966Siwasaki		break;
1028100966Siwasaki
1029100966Siwasaki	case FIOGETOWN:
1030100966Siwasaki		*(int *)addr = fgetown(&d->bd_sigio);
1031100966Siwasaki		break;
1032100966Siwasaki
1033100966Siwasaki	/* This is deprecated, FIOSETOWN should be used instead. */
1034100966Siwasaki	case TIOCSPGRP:
1035100966Siwasaki		error = fsetown(-(*(int *)addr), &d->bd_sigio);
1036100966Siwasaki		break;
1037100966Siwasaki
1038100966Siwasaki	/* This is deprecated, FIOGETOWN should be used instead. */
1039100966Siwasaki	case TIOCGPGRP:
1040100966Siwasaki		*(int *)addr = -fgetown(&d->bd_sigio);
1041100966Siwasaki		break;
1042100966Siwasaki
1043100966Siwasaki	case BIOCSRSIG:		/* Set receive signal */
1044100966Siwasaki		{
1045200553Sjkim			u_int sig;
1046100966Siwasaki
1047100966Siwasaki			sig = *(u_int *)addr;
1048100966Siwasaki
1049100966Siwasaki			if (sig >= NSIG)
1050100966Siwasaki				error = EINVAL;
1051100966Siwasaki			else
1052100966Siwasaki				d->bd_sig = sig;
1053100966Siwasaki			break;
1054100966Siwasaki		}
1055100966Siwasaki	case BIOCGRSIG:
1056100966Siwasaki		*(u_int *)addr = d->bd_sig;
1057100966Siwasaki		break;
1058100966Siwasaki	}
1059167802Sjkim	return (error);
1060}
1061
1062/*
1063 * Set d's packet filter program to fp.  If this file already has a filter,
1064 * free it and replace it.  Returns EINVAL for bogus requests.
1065 */
1066static int
1067bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1068{
1069	struct bpf_insn *fcode, *old;
1070	u_int wfilter, flen, size;
1071#ifdef BPF_JITTER
1072	bpf_jit_filter *ofunc;
1073#endif
1074
1075	if (cmd == BIOCSETWF) {
1076		old = d->bd_wfilter;
1077		wfilter = 1;
1078#ifdef BPF_JITTER
1079		ofunc = NULL;
1080#endif
1081	} else {
1082		wfilter = 0;
1083		old = d->bd_rfilter;
1084#ifdef BPF_JITTER
1085		ofunc = d->bd_bfilter;
1086#endif
1087	}
1088	if (fp->bf_insns == NULL) {
1089		if (fp->bf_len != 0)
1090			return (EINVAL);
1091		BPFD_LOCK(d);
1092		if (wfilter)
1093			d->bd_wfilter = NULL;
1094		else {
1095			d->bd_rfilter = NULL;
1096#ifdef BPF_JITTER
1097			d->bd_bfilter = NULL;
1098#endif
1099		}
1100		reset_d(d);
1101		BPFD_UNLOCK(d);
1102		if (old != NULL)
1103			free((caddr_t)old, M_BPF);
1104#ifdef BPF_JITTER
1105		if (ofunc != NULL)
1106			bpf_destroy_jit_filter(ofunc);
1107#endif
1108		return (0);
1109	}
1110	flen = fp->bf_len;
1111	if (flen > bpf_maxinsns)
1112		return (EINVAL);
1113
1114	size = flen * sizeof(*fp->bf_insns);
1115	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1116	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1117	    bpf_validate(fcode, (int)flen)) {
1118		BPFD_LOCK(d);
1119		if (wfilter)
1120			d->bd_wfilter = fcode;
1121		else {
1122			d->bd_rfilter = fcode;
1123#ifdef BPF_JITTER
1124			d->bd_bfilter = bpf_jitter(fcode, flen);
1125#endif
1126		}
1127		reset_d(d);
1128		BPFD_UNLOCK(d);
1129		if (old != NULL)
1130			free((caddr_t)old, M_BPF);
1131#ifdef BPF_JITTER
1132		if (ofunc != NULL)
1133			bpf_destroy_jit_filter(ofunc);
1134#endif
1135
1136		return (0);
1137	}
1138	free((caddr_t)fcode, M_BPF);
1139	return (EINVAL);
1140}
1141
1142/*
1143 * Detach a file from its current interface (if attached at all) and attach
1144 * to the interface indicated by the name stored in ifr.
1145 * Return an errno or 0.
1146 */
1147static int
1148bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1149{
1150	struct bpf_if *bp;
1151	struct ifnet *theywant;
1152
1153	theywant = ifunit(ifr->ifr_name);
1154	if (theywant == NULL || theywant->if_bpf == NULL)
1155		return (ENXIO);
1156
1157	bp = theywant->if_bpf;
1158	/*
1159	 * Allocate the packet buffers if we need to.
1160	 * If we're already attached to requested interface,
1161	 * just flush the buffer.
1162	 */
1163	if (d->bd_sbuf == NULL)
1164		bpf_allocbufs(d);
1165	if (bp != d->bd_bif) {
1166		if (d->bd_bif)
1167			/*
1168			 * Detach if attached to something else.
1169			 */
1170			bpf_detachd(d);
1171
1172		bpf_attachd(d, bp);
1173	}
1174	BPFD_LOCK(d);
1175	reset_d(d);
1176	BPFD_UNLOCK(d);
1177	return (0);
1178}
1179
1180/*
1181 * Support for select() and poll() system calls
1182 *
1183 * Return true iff the specific operation will not block indefinitely.
1184 * Otherwise, return false but make a note that a selwakeup() must be done.
1185 */
1186static int
1187bpfpoll(struct cdev *dev, int events, struct thread *td)
1188{
1189	struct bpf_d *d;
1190	int revents;
1191
1192	d = dev->si_drv1;
1193	if (d->bd_bif == NULL)
1194		return (ENXIO);
1195
1196	/*
1197	 * Refresh PID associated with this descriptor.
1198	 */
1199	revents = events & (POLLOUT | POLLWRNORM);
1200	BPFD_LOCK(d);
1201	d->bd_pid = td->td_proc->p_pid;
1202	if (events & (POLLIN | POLLRDNORM)) {
1203		if (bpf_ready(d))
1204			revents |= events & (POLLIN | POLLRDNORM);
1205		else {
1206			selrecord(td, &d->bd_sel);
1207			/* Start the read timeout if necessary. */
1208			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1209				callout_reset(&d->bd_callout, d->bd_rtout,
1210				    bpf_timed_out, d);
1211				d->bd_state = BPF_WAITING;
1212			}
1213		}
1214	}
1215	BPFD_UNLOCK(d);
1216	return (revents);
1217}
1218
1219/*
1220 * Support for kevent() system call.  Register EVFILT_READ filters and
1221 * reject all others.
1222 */
1223int
1224bpfkqfilter(struct cdev *dev, struct knote *kn)
1225{
1226	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1227
1228	if (kn->kn_filter != EVFILT_READ)
1229		return (1);
1230
1231	/*
1232	 * Refresh PID associated with this descriptor.
1233	 */
1234	BPFD_LOCK(d);
1235	d->bd_pid = curthread->td_proc->p_pid;
1236	kn->kn_fop = &bpfread_filtops;
1237	kn->kn_hook = d;
1238	knlist_add(&d->bd_sel.si_note, kn, 1);
1239	BPFD_UNLOCK(d);
1240
1241	return (0);
1242}
1243
1244static void
1245filt_bpfdetach(struct knote *kn)
1246{
1247	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1248
1249	knlist_remove(&d->bd_sel.si_note, kn, 0);
1250}
1251
1252static int
1253filt_bpfread(struct knote *kn, long hint)
1254{
1255	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1256	int ready;
1257
1258	BPFD_LOCK_ASSERT(d);
1259	ready = bpf_ready(d);
1260	if (ready) {
1261		kn->kn_data = d->bd_slen;
1262		if (d->bd_hbuf)
1263			kn->kn_data += d->bd_hlen;
1264	}
1265	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1266		callout_reset(&d->bd_callout, d->bd_rtout,
1267		    bpf_timed_out, d);
1268		d->bd_state = BPF_WAITING;
1269	}
1270
1271	return (ready);
1272}
1273
1274/*
1275 * Incoming linkage from device drivers.  Process the packet pkt, of length
1276 * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1277 * by each process' filter, and if accepted, stashed into the corresponding
1278 * buffer.
1279 */
1280void
1281bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1282{
1283	struct bpf_d *d;
1284	u_int slen;
1285	int gottime;
1286	struct timeval tv;
1287
1288	gottime = 0;
1289	BPFIF_LOCK(bp);
1290	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1291		BPFD_LOCK(d);
1292		++d->bd_rcount;
1293#ifdef BPF_JITTER
1294		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1295			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1296		else
1297#endif
1298		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1299		if (slen != 0) {
1300			d->bd_fcount++;
1301			if (!gottime) {
1302				microtime(&tv);
1303				gottime = 1;
1304			}
1305#ifdef MAC
1306			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1307#endif
1308				catchpacket(d, pkt, pktlen, slen, bcopy, &tv);
1309		}
1310		BPFD_UNLOCK(d);
1311	}
1312	BPFIF_UNLOCK(bp);
1313}
1314
1315/*
1316 * Copy data from an mbuf chain into a buffer.  This code is derived
1317 * from m_copydata in sys/uipc_mbuf.c.
1318 */
1319static void
1320bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1321{
1322	const struct mbuf *m;
1323	u_int count;
1324	u_char *dst;
1325
1326	m = src_arg;
1327	dst = dst_arg;
1328	while (len > 0) {
1329		if (m == NULL)
1330			panic("bpf_mcopy");
1331		count = min(m->m_len, len);
1332		bcopy(mtod(m, void *), dst, count);
1333		m = m->m_next;
1334		dst += count;
1335		len -= count;
1336	}
1337}
1338
1339#define	BPF_CHECK_DIRECTION(d, m) \
1340	if (((d)->bd_direction == BPF_D_IN && (m)->m_pkthdr.rcvif == NULL) || \
1341	    ((d)->bd_direction == BPF_D_OUT && (m)->m_pkthdr.rcvif != NULL))
1342
1343/*
1344 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1345 */
1346void
1347bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1348{
1349	struct bpf_d *d;
1350	u_int pktlen, slen;
1351	int gottime;
1352	struct timeval tv;
1353
1354	if (m->m_flags & M_SKIP_BPF) {
1355		m->m_flags &= ~M_SKIP_BPF;
1356		return;
1357	}
1358
1359	gottime = 0;
1360
1361	pktlen = m_length(m, NULL);
1362
1363	BPFIF_LOCK(bp);
1364	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1365		BPF_CHECK_DIRECTION(d, m)
1366			continue;
1367		BPFD_LOCK(d);
1368		++d->bd_rcount;
1369#ifdef BPF_JITTER
1370		/* XXX We cannot handle multiple mbufs. */
1371		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1372		    m->m_next == NULL)
1373			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1374			    pktlen, pktlen);
1375		else
1376#endif
1377		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1378		if (slen != 0) {
1379			d->bd_fcount++;
1380			if (!gottime) {
1381				microtime(&tv);
1382				gottime = 1;
1383			}
1384#ifdef MAC
1385			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1386#endif
1387				catchpacket(d, (u_char *)m, pktlen, slen,
1388				    bpf_mcopy, &tv);
1389		}
1390		BPFD_UNLOCK(d);
1391	}
1392	BPFIF_UNLOCK(bp);
1393}
1394
1395/*
1396 * Incoming linkage from device drivers, when packet is in
1397 * an mbuf chain and to be prepended by a contiguous header.
1398 */
1399void
1400bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1401{
1402	struct mbuf mb;
1403	struct bpf_d *d;
1404	u_int pktlen, slen;
1405	int gottime;
1406	struct timeval tv;
1407
1408	if (m->m_flags & M_SKIP_BPF) {
1409		m->m_flags &= ~M_SKIP_BPF;
1410		return;
1411	}
1412
1413	gottime = 0;
1414
1415	pktlen = m_length(m, NULL);
1416	/*
1417	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1418	 * Note that we cut corners here; we only setup what's
1419	 * absolutely needed--this mbuf should never go anywhere else.
1420	 */
1421	mb.m_next = m;
1422	mb.m_data = data;
1423	mb.m_len = dlen;
1424	pktlen += dlen;
1425
1426	BPFIF_LOCK(bp);
1427	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1428		BPF_CHECK_DIRECTION(d, m)
1429			continue;
1430		BPFD_LOCK(d);
1431		++d->bd_rcount;
1432		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
1433		if (slen != 0) {
1434			d->bd_fcount++;
1435			if (!gottime) {
1436				microtime(&tv);
1437				gottime = 1;
1438			}
1439#ifdef MAC
1440			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1441#endif
1442				catchpacket(d, (u_char *)&mb, pktlen, slen,
1443				    bpf_mcopy, &tv);
1444		}
1445		BPFD_UNLOCK(d);
1446	}
1447	BPFIF_UNLOCK(bp);
1448}
1449
1450#undef	BPF_CHECK_DIRECTION
1451
1452/*
1453 * Move the packet data from interface memory (pkt) into the
1454 * store buffer.  "cpfn" is the routine called to do the actual data
1455 * transfer.  bcopy is passed in to copy contiguous chunks, while
1456 * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1457 * pkt is really an mbuf.
1458 */
1459static void
1460catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1461    void (*cpfn)(const void *, void *, size_t), struct timeval *tv)
1462{
1463	struct bpf_hdr *hp;
1464	int totlen, curlen;
1465	int hdrlen = d->bd_bif->bif_hdrlen;
1466	int do_wakeup = 0;
1467
1468	BPFD_LOCK_ASSERT(d);
1469	/*
1470	 * Figure out how many bytes to move.  If the packet is
1471	 * greater or equal to the snapshot length, transfer that
1472	 * much.  Otherwise, transfer the whole packet (unless
1473	 * we hit the buffer size limit).
1474	 */
1475	totlen = hdrlen + min(snaplen, pktlen);
1476	if (totlen > d->bd_bufsize)
1477		totlen = d->bd_bufsize;
1478
1479	/*
1480	 * Round up the end of the previous packet to the next longword.
1481	 */
1482	curlen = BPF_WORDALIGN(d->bd_slen);
1483	if (curlen + totlen > d->bd_bufsize) {
1484		/*
1485		 * This packet will overflow the storage buffer.
1486		 * Rotate the buffers if we can, then wakeup any
1487		 * pending reads.
1488		 */
1489		if (d->bd_fbuf == NULL) {
1490			/*
1491			 * We haven't completed the previous read yet,
1492			 * so drop the packet.
1493			 */
1494			++d->bd_dcount;
1495			return;
1496		}
1497		ROTATE_BUFFERS(d);
1498		do_wakeup = 1;
1499		curlen = 0;
1500	}
1501	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1502		/*
1503		 * Immediate mode is set, or the read timeout has
1504		 * already expired during a select call.  A packet
1505		 * arrived, so the reader should be woken up.
1506		 */
1507		do_wakeup = 1;
1508
1509	/*
1510	 * Append the bpf header.
1511	 */
1512	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1513	hp->bh_tstamp = *tv;
1514	hp->bh_datalen = pktlen;
1515	hp->bh_hdrlen = hdrlen;
1516	/*
1517	 * Copy the packet data into the store buffer and update its length.
1518	 */
1519	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1520	d->bd_slen = curlen + totlen;
1521
1522	if (do_wakeup)
1523		bpf_wakeup(d);
1524}
1525
1526/*
1527 * Initialize all nonzero fields of a descriptor.
1528 */
1529static void
1530bpf_allocbufs(struct bpf_d *d)
1531{
1532
1533	KASSERT(d->bd_fbuf == NULL, ("bpf_allocbufs: bd_fbuf != NULL"));
1534	KASSERT(d->bd_sbuf == NULL, ("bpf_allocbufs: bd_sbuf != NULL"));
1535	KASSERT(d->bd_hbuf == NULL, ("bpf_allocbufs: bd_hbuf != NULL"));
1536
1537	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1538	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1539	d->bd_slen = 0;
1540	d->bd_hlen = 0;
1541}
1542
1543/*
1544 * Free buffers currently in use by a descriptor.
1545 * Called on close.
1546 */
1547static void
1548bpf_freed(struct bpf_d *d)
1549{
1550	/*
1551	 * We don't need to lock out interrupts since this descriptor has
1552	 * been detached from its interface and it yet hasn't been marked
1553	 * free.
1554	 */
1555	if (d->bd_sbuf != NULL) {
1556		free(d->bd_sbuf, M_BPF);
1557		if (d->bd_hbuf != NULL)
1558			free(d->bd_hbuf, M_BPF);
1559		if (d->bd_fbuf != NULL)
1560			free(d->bd_fbuf, M_BPF);
1561	}
1562	if (d->bd_rfilter) {
1563		free((caddr_t)d->bd_rfilter, M_BPF);
1564#ifdef BPF_JITTER
1565		bpf_destroy_jit_filter(d->bd_bfilter);
1566#endif
1567	}
1568	if (d->bd_wfilter)
1569		free((caddr_t)d->bd_wfilter, M_BPF);
1570	mtx_destroy(&d->bd_mtx);
1571}
1572
1573/*
1574 * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
1575 * fixed size of the link header (variable length headers not yet supported).
1576 */
1577void
1578bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1579{
1580
1581	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1582}
1583
1584/*
1585 * Attach an interface to bpf.  ifp is a pointer to the structure
1586 * defining the interface to be attached, dlt is the link layer type,
1587 * and hdrlen is the fixed size of the link header (variable length
1588 * headers are not yet supporrted).
1589 */
1590void
1591bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1592{
1593	struct bpf_if *bp;
1594
1595	bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1596	if (bp == NULL)
1597		panic("bpfattach");
1598
1599	LIST_INIT(&bp->bif_dlist);
1600	bp->bif_ifp = ifp;
1601	bp->bif_dlt = dlt;
1602	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1603	KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
1604	*driverp = bp;
1605
1606	mtx_lock(&bpf_mtx);
1607	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1608	mtx_unlock(&bpf_mtx);
1609
1610	/*
1611	 * Compute the length of the bpf header.  This is not necessarily
1612	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1613	 * that the network layer header begins on a longword boundary (for
1614	 * performance reasons and to alleviate alignment restrictions).
1615	 */
1616	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1617
1618	if (bootverbose)
1619		if_printf(ifp, "bpf attached\n");
1620}
1621
1622/*
1623 * Detach bpf from an interface.  This involves detaching each descriptor
1624 * associated with the interface, and leaving bd_bif NULL.  Notify each
1625 * descriptor as it's detached so that any sleepers wake up and get
1626 * ENXIO.
1627 */
1628void
1629bpfdetach(struct ifnet *ifp)
1630{
1631	struct bpf_if	*bp;
1632	struct bpf_d	*d;
1633
1634	/* Locate BPF interface information */
1635	mtx_lock(&bpf_mtx);
1636	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1637		if (ifp == bp->bif_ifp)
1638			break;
1639	}
1640
1641	/* Interface wasn't attached */
1642	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1643		mtx_unlock(&bpf_mtx);
1644		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1645		return;
1646	}
1647
1648	LIST_REMOVE(bp, bif_next);
1649	mtx_unlock(&bpf_mtx);
1650
1651	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1652		bpf_detachd(d);
1653		BPFD_LOCK(d);
1654		bpf_wakeup(d);
1655		BPFD_UNLOCK(d);
1656	}
1657
1658	mtx_destroy(&bp->bif_mtx);
1659	free(bp, M_BPF);
1660}
1661
1662/*
1663 * Get a list of available data link type of the interface.
1664 */
1665static int
1666bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1667{
1668	int n, error;
1669	struct ifnet *ifp;
1670	struct bpf_if *bp;
1671
1672	ifp = d->bd_bif->bif_ifp;
1673	n = 0;
1674	error = 0;
1675	mtx_lock(&bpf_mtx);
1676	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1677		if (bp->bif_ifp != ifp)
1678			continue;
1679		if (bfl->bfl_list != NULL) {
1680			if (n >= bfl->bfl_len) {
1681				mtx_unlock(&bpf_mtx);
1682				return (ENOMEM);
1683			}
1684			error = copyout(&bp->bif_dlt,
1685			    bfl->bfl_list + n, sizeof(u_int));
1686		}
1687		n++;
1688	}
1689	mtx_unlock(&bpf_mtx);
1690	bfl->bfl_len = n;
1691	return (error);
1692}
1693
1694/*
1695 * Set the data link type of a BPF instance.
1696 */
1697static int
1698bpf_setdlt(struct bpf_d *d, u_int dlt)
1699{
1700	int error, opromisc;
1701	struct ifnet *ifp;
1702	struct bpf_if *bp;
1703
1704	if (d->bd_bif->bif_dlt == dlt)
1705		return (0);
1706	ifp = d->bd_bif->bif_ifp;
1707	mtx_lock(&bpf_mtx);
1708	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1709		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1710			break;
1711	}
1712	mtx_unlock(&bpf_mtx);
1713	if (bp != NULL) {
1714		opromisc = d->bd_promisc;
1715		bpf_detachd(d);
1716		bpf_attachd(d, bp);
1717		BPFD_LOCK(d);
1718		reset_d(d);
1719		BPFD_UNLOCK(d);
1720		if (opromisc) {
1721			error = ifpromisc(bp->bif_ifp, 1);
1722			if (error)
1723				if_printf(bp->bif_ifp,
1724					"bpf_setdlt: ifpromisc failed (%d)\n",
1725					error);
1726			else
1727				d->bd_promisc = 1;
1728		}
1729	}
1730	return (bp == NULL ? EINVAL : 0);
1731}
1732
1733static void
1734bpf_clone(void *arg, struct ucred *cred, char *name, int namelen,
1735    struct cdev **dev)
1736{
1737	int u;
1738
1739	if (*dev != NULL)
1740		return;
1741	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1742		return;
1743	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1744	    "bpf%d", u);
1745	dev_ref(*dev);
1746	(*dev)->si_flags |= SI_CHEAPCLONE;
1747	return;
1748}
1749
1750static void
1751bpf_drvinit(void *unused)
1752{
1753
1754	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1755	LIST_INIT(&bpf_iflist);
1756	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1757}
1758
1759static void
1760bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
1761{
1762
1763	bzero(d, sizeof(*d));
1764	BPFD_LOCK_ASSERT(bd);
1765	d->bd_immediate = bd->bd_immediate;
1766	d->bd_promisc = bd->bd_promisc;
1767	d->bd_hdrcmplt = bd->bd_hdrcmplt;
1768	d->bd_direction = bd->bd_direction;
1769	d->bd_feedback = bd->bd_feedback;
1770	d->bd_async = bd->bd_async;
1771	d->bd_rcount = bd->bd_rcount;
1772	d->bd_dcount = bd->bd_dcount;
1773	d->bd_fcount = bd->bd_fcount;
1774	d->bd_sig = bd->bd_sig;
1775	d->bd_slen = bd->bd_slen;
1776	d->bd_hlen = bd->bd_hlen;
1777	d->bd_bufsize = bd->bd_bufsize;
1778	d->bd_pid = bd->bd_pid;
1779	strlcpy(d->bd_ifname,
1780	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
1781	d->bd_locked = bd->bd_locked;
1782}
1783
1784static int
1785bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
1786{
1787	struct xbpf_d *xbdbuf, *xbd;
1788	int index, error;
1789	struct bpf_if *bp;
1790	struct bpf_d *bd;
1791
1792	/*
1793	 * XXX This is not technically correct. It is possible for non
1794	 * privileged users to open bpf devices. It would make sense
1795	 * if the users who opened the devices were able to retrieve
1796	 * the statistics for them, too.
1797	 */
1798	error = priv_check(req->td, PRIV_NET_BPF);
1799	if (error)
1800		return (error);
1801	if (req->oldptr == NULL)
1802		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
1803	if (bpf_bpfd_cnt == 0)
1804		return (SYSCTL_OUT(req, 0, 0));
1805	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
1806	mtx_lock(&bpf_mtx);
1807	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
1808		mtx_unlock(&bpf_mtx);
1809		free(xbdbuf, M_BPF);
1810		return (ENOMEM);
1811	}
1812	index = 0;
1813	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1814		BPFIF_LOCK(bp);
1815		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
1816			xbd = &xbdbuf[index++];
1817			BPFD_LOCK(bd);
1818			bpfstats_fill_xbpf(xbd, bd);
1819			BPFD_UNLOCK(bd);
1820		}
1821		BPFIF_UNLOCK(bp);
1822	}
1823	mtx_unlock(&bpf_mtx);
1824	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
1825	free(xbdbuf, M_BPF);
1826	return (error);
1827}
1828
1829SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
1830
1831#else /* !DEV_BPF && !NETGRAPH_BPF */
1832/*
1833 * NOP stubs to allow bpf-using drivers to load and function.
1834 *
1835 * A 'better' implementation would allow the core bpf functionality
1836 * to be loaded at runtime.
1837 */
1838static struct bpf_if bp_null;
1839
1840void
1841bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1842{
1843}
1844
1845void
1846bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1847{
1848}
1849
1850void
1851bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
1852{
1853}
1854
1855void
1856bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1857{
1858
1859	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1860}
1861
1862void
1863bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1864{
1865
1866	*driverp = &bp_null;
1867}
1868
1869void
1870bpfdetach(struct ifnet *ifp)
1871{
1872}
1873
1874u_int
1875bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1876{
1877	return -1;	/* "no filter" behaviour */
1878}
1879
1880int
1881bpf_validate(const struct bpf_insn *f, int len)
1882{
1883	return 0;		/* false */
1884}
1885
1886#endif /* !DEV_BPF && !NETGRAPH_BPF */
1887