bpf.c revision 147611
1193323Sed/*-
2193323Sed * Copyright (c) 1990, 1991, 1993
3193323Sed *	The Regents of the University of California.  All rights reserved.
4193323Sed *
5193323Sed * This code is derived from the Stanford/CMU enet packet filter,
6193323Sed * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7193323Sed * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8193323Sed * Berkeley Laboratory.
9193323Sed *
10193323Sed * Redistribution and use in source and binary forms, with or without
11193323Sed * modification, are permitted provided that the following conditions
12193323Sed * are met:
13193323Sed * 1. Redistributions of source code must retain the above copyright
14193323Sed *    notice, this list of conditions and the following disclaimer.
15193323Sed * 2. Redistributions in binary form must reproduce the above copyright
16193323Sed *    notice, this list of conditions and the following disclaimer in the
17193323Sed *    documentation and/or other materials provided with the distribution.
18193323Sed * 4. Neither the name of the University nor the names of its contributors
19193323Sed *    may be used to endorse or promote products derived from this software
20193323Sed *    without specific prior written permission.
21193323Sed *
22193323Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29198396Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32198090Srdivacky * SUCH DAMAGE.
33198090Srdivacky *
34198090Srdivacky *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
35198090Srdivacky *
36193323Sed * $FreeBSD: head/sys/net/bpf.c 147611 2005-06-26 18:11:11Z dwmalone $
37193323Sed */
38198396Srdivacky
39198396Srdivacky#include "opt_bpf.h"
40198396Srdivacky#include "opt_mac.h"
41198396Srdivacky#include "opt_netgraph.h"
42198396Srdivacky
43198396Srdivacky#include <sys/types.h>
44198396Srdivacky#include <sys/param.h>
45198396Srdivacky#include <sys/systm.h>
46198090Srdivacky#include <sys/conf.h>
47198090Srdivacky#include <sys/fcntl.h>
48199481Srdivacky#include <sys/mac.h>
49199481Srdivacky#include <sys/malloc.h>
50199481Srdivacky#include <sys/mbuf.h>
51199481Srdivacky#include <sys/time.h>
52199481Srdivacky#include <sys/proc.h>
53199481Srdivacky#include <sys/signalvar.h>
54199481Srdivacky#include <sys/filio.h>
55199481Srdivacky#include <sys/sockio.h>
56199481Srdivacky#include <sys/ttycom.h>
57199481Srdivacky#include <sys/uio.h>
58199481Srdivacky
59199481Srdivacky#include <sys/event.h>
60199481Srdivacky#include <sys/file.h>
61199481Srdivacky#include <sys/poll.h>
62199481Srdivacky#include <sys/proc.h>
63199481Srdivacky
64199481Srdivacky#include <sys/socket.h>
65199481Srdivacky
66199481Srdivacky#include <net/if.h>
67199481Srdivacky#include <net/bpf.h>
68199481Srdivacky#include <net/bpfdesc.h>
69199481Srdivacky
70199481Srdivacky#include <netinet/in.h>
71199481Srdivacky#include <netinet/if_ether.h>
72199481Srdivacky#include <sys/kernel.h>
73199481Srdivacky#include <sys/sysctl.h>
74199481Srdivacky
75199481Srdivackystatic MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
76199481Srdivacky
77199481Srdivacky#if defined(DEV_BPF) || defined(NETGRAPH_BPF)
78199481Srdivacky
79199481Srdivacky#define PRINET  26			/* interruptible */
80199481Srdivacky
81199481Srdivacky/*
82199481Srdivacky * The default read buffer size is patchable.
83199481Srdivacky */
84199481SrdivackySYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
85199481Srdivackystatic int bpf_bufsize = 4096;
86199481SrdivackySYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
87199481Srdivacky    &bpf_bufsize, 0, "");
88199481Srdivackystatic int bpf_maxbufsize = BPF_MAXBUFSIZE;
89200581SrdivackySYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
90199481Srdivacky    &bpf_maxbufsize, 0, "");
91199481Srdivackystatic int bpf_maxinsns = BPF_MAXINSNS;
92200581SrdivackySYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
93199481Srdivacky    &bpf_maxinsns, 0, "Maximum bpf program instructions");
94199481Srdivacky
95199481Srdivacky/*
96199481Srdivacky * bpf_iflist is a list of BPF interface structures, each corresponding to a
97199481Srdivacky * specific DLT.  The same network interface might have several BPF interface
98199481Srdivacky * structures registered by different layers in the stack (i.e., 802.11
99199481Srdivacky * frames, ethernet frames, etc).
100199481Srdivacky */
101199481Srdivackystatic LIST_HEAD(, bpf_if)	bpf_iflist;
102199481Srdivackystatic struct mtx	bpf_mtx;		/* bpf global lock */
103199481Srdivacky
104199481Srdivackystatic int	bpf_allocbufs(struct bpf_d *);
105199481Srdivackystatic void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
106199481Srdivackystatic void	bpf_detachd(struct bpf_d *d);
107199481Srdivackystatic void	bpf_freed(struct bpf_d *);
108199481Srdivackystatic void	bpf_mcopy(const void *, void *, size_t);
109199481Srdivackystatic int	bpf_movein(struct uio *, int, int,
110199481Srdivacky		    struct mbuf **, struct sockaddr *);
111199481Srdivackystatic int	bpf_setif(struct bpf_d *, struct ifreq *);
112193323Sedstatic void	bpf_timed_out(void *);
113193323Sedstatic __inline void
114193323Sed		bpf_wakeup(struct bpf_d *);
115193323Sedstatic void	catchpacket(struct bpf_d *, u_char *, u_int,
116193323Sed		    u_int, void (*)(const void *, void *, size_t));
117193323Sedstatic void	reset_d(struct bpf_d *);
118193323Sedstatic int	 bpf_setf(struct bpf_d *, struct bpf_program *);
119193323Sedstatic int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
120193323Sedstatic int	bpf_setdlt(struct bpf_d *, u_int);
121193323Sedstatic void	filt_bpfdetach(struct knote *);
122193323Sedstatic int	filt_bpfread(struct knote *, long);
123193323Sedstatic void	bpf_drvinit(void *);
124193323Sedstatic void	bpf_clone(void *, char *, int, struct cdev **);
125193323Sed
126193323Sedstatic	d_open_t	bpfopen;
127193323Sedstatic	d_close_t	bpfclose;
128193323Sedstatic	d_read_t	bpfread;
129193323Sedstatic	d_write_t	bpfwrite;
130193323Sedstatic	d_ioctl_t	bpfioctl;
131193323Sedstatic	d_poll_t	bpfpoll;
132199481Srdivackystatic	d_kqfilter_t	bpfkqfilter;
133199481Srdivacky
134199481Srdivackystatic struct cdevsw bpf_cdevsw = {
135199481Srdivacky	.d_version =	D_VERSION,
136198396Srdivacky	.d_flags =	D_NEEDGIANT,
137198396Srdivacky	.d_open =	bpfopen,
138198396Srdivacky	.d_close =	bpfclose,
139198090Srdivacky	.d_read =	bpfread,
140198090Srdivacky	.d_write =	bpfwrite,
141198090Srdivacky	.d_ioctl =	bpfioctl,
142198090Srdivacky	.d_poll =	bpfpoll,
143193323Sed	.d_name =	"bpf",
144193323Sed	.d_kqfilter =	bpfkqfilter,
145193323Sed};
146193323Sed
147198396Srdivackystatic struct filterops bpfread_filtops =
148198090Srdivacky	{ 1, NULL, filt_bpfdetach, filt_bpfread };
149202375Srdivacky
150202375Srdivackystatic int
151202375Srdivackybpf_movein(uio, linktype, mtu, mp, sockp)
152199481Srdivacky	struct uio *uio;
153199481Srdivacky	int linktype;
154202375Srdivacky	int mtu;
155193323Sed	struct mbuf **mp;
156193323Sed	struct sockaddr *sockp;
157193323Sed{
158193323Sed	struct mbuf *m;
159193323Sed	int error;
160193323Sed	int len;
161193323Sed	int hlen;
162193323Sed
163193323Sed	/*
164193323Sed	 * Build a sockaddr based on the data link layer type.
165193323Sed	 * We do this at this level because the ethernet header
166193323Sed	 * is copied directly into the data field of the sockaddr.
167193323Sed	 * In the case of SLIP, there is no header and the packet
168193323Sed	 * is forwarded as is.
169193323Sed	 * Also, we are careful to leave room at the front of the mbuf
170199481Srdivacky	 * for the link level header.
171199481Srdivacky	 */
172199481Srdivacky	switch (linktype) {
173199481Srdivacky
174199481Srdivacky	case DLT_SLIP:
175199481Srdivacky		sockp->sa_family = AF_INET;
176199481Srdivacky		hlen = 0;
177199481Srdivacky		break;
178193323Sed
179199481Srdivacky	case DLT_EN10MB:
180199481Srdivacky		sockp->sa_family = AF_UNSPEC;
181199481Srdivacky		/* XXX Would MAXLINKHDR be better? */
182199481Srdivacky		hlen = ETHER_HDR_LEN;
183199481Srdivacky		break;
184199481Srdivacky
185199481Srdivacky	case DLT_FDDI:
186199481Srdivacky		sockp->sa_family = AF_IMPLINK;
187199481Srdivacky		hlen = 0;
188199481Srdivacky		break;
189199481Srdivacky
190199481Srdivacky	case DLT_RAW:
191199481Srdivacky		sockp->sa_family = AF_UNSPEC;
192199481Srdivacky		hlen = 0;
193199481Srdivacky		break;
194199481Srdivacky
195199481Srdivacky	case DLT_NULL:
196199481Srdivacky		/*
197199481Srdivacky		 * null interface types require a 4 byte pseudo header which
198199481Srdivacky		 * corresponds to the address family of the packet.
199199481Srdivacky		 */
200193323Sed		sockp->sa_family = AF_UNSPEC;
201199481Srdivacky		hlen = 4;
202199481Srdivacky		break;
203193323Sed
204199481Srdivacky	case DLT_ATM_RFC1483:
205199481Srdivacky		/*
206199481Srdivacky		 * en atm driver requires 4-byte atm pseudo header.
207199481Srdivacky		 * though it isn't standard, vpi:vci needs to be
208199481Srdivacky		 * specified anyway.
209199481Srdivacky		 */
210199481Srdivacky		sockp->sa_family = AF_UNSPEC;
211199481Srdivacky		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
212193323Sed		break;
213199481Srdivacky
214199481Srdivacky	case DLT_PPP:
215199481Srdivacky		sockp->sa_family = AF_UNSPEC;
216199481Srdivacky		hlen = 4;	/* This should match PPP_HDRLEN */
217199481Srdivacky		break;
218199481Srdivacky
219199481Srdivacky	default:
220199481Srdivacky		return (EIO);
221199481Srdivacky	}
222199481Srdivacky
223199481Srdivacky	len = uio->uio_resid;
224199481Srdivacky
225199481Srdivacky	if (len - hlen > mtu)
226199481Srdivacky		return (EMSGSIZE);
227199481Srdivacky
228199481Srdivacky	if ((unsigned)len > MCLBYTES)
229199481Srdivacky		return (EIO);
230199481Srdivacky
231199481Srdivacky	if (len > MHLEN) {
232199481Srdivacky		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
233199481Srdivacky	} else {
234199481Srdivacky		MGETHDR(m, M_TRYWAIT, MT_DATA);
235199481Srdivacky	}
236199481Srdivacky	if (m == NULL)
237199481Srdivacky		return (ENOBUFS);
238199481Srdivacky	m->m_pkthdr.len = m->m_len = len;
239199481Srdivacky	m->m_pkthdr.rcvif = NULL;
240199481Srdivacky	*mp = m;
241199481Srdivacky
242199481Srdivacky	/*
243199481Srdivacky	 * Make room for link header.
244193323Sed	 */
245193323Sed	if (hlen != 0) {
246199481Srdivacky		m->m_pkthdr.len -= hlen;
247199481Srdivacky		m->m_len -= hlen;
248199481Srdivacky#if BSD >= 199103
249199481Srdivacky		m->m_data += hlen; /* XXX */
250199481Srdivacky#else
251199481Srdivacky		m->m_off += hlen;
252199481Srdivacky#endif
253199481Srdivacky		error = uiomove(sockp->sa_data, hlen, uio);
254199481Srdivacky		if (error)
255199481Srdivacky			goto bad;
256199481Srdivacky	}
257199481Srdivacky	error = uiomove(mtod(m, void *), len - hlen, uio);
258199481Srdivacky	if (!error)
259199481Srdivacky		return (0);
260199481Srdivackybad:
261199481Srdivacky	m_freem(m);
262199481Srdivacky	return (error);
263199481Srdivacky}
264199481Srdivacky
265199481Srdivacky/*
266199481Srdivacky * Attach file to the bpf interface, i.e. make d listen on bp.
267199481Srdivacky */
268199481Srdivackystatic void
269199481Srdivackybpf_attachd(d, bp)
270199481Srdivacky	struct bpf_d *d;
271199481Srdivacky	struct bpf_if *bp;
272199481Srdivacky{
273199481Srdivacky	/*
274199481Srdivacky	 * Point d at bp, and add d to the interface's list of listeners.
275199481Srdivacky	 * Finally, point the driver's bpf cookie at the interface so
276193323Sed	 * it will divert packets to bpf.
277193323Sed	 */
278199481Srdivacky	BPFIF_LOCK(bp);
279193323Sed	d->bd_bif = bp;
280199481Srdivacky	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
281199481Srdivacky
282193323Sed	*bp->bif_driverp = bp;
283199481Srdivacky	BPFIF_UNLOCK(bp);
284199481Srdivacky}
285199481Srdivacky
286202375Srdivacky/*
287199481Srdivacky * Detach a file from its interface.
288199481Srdivacky */
289199481Srdivackystatic void
290199481Srdivackybpf_detachd(d)
291199481Srdivacky	struct bpf_d *d;
292199481Srdivacky{
293199481Srdivacky	int error;
294199481Srdivacky	struct bpf_if *bp;
295199481Srdivacky	struct ifnet *ifp;
296199481Srdivacky
297199481Srdivacky	bp = d->bd_bif;
298199481Srdivacky	BPFIF_LOCK(bp);
299199481Srdivacky	BPFD_LOCK(d);
300199481Srdivacky	ifp = d->bd_bif->bif_ifp;
301199481Srdivacky
302199481Srdivacky	/*
303199481Srdivacky	 * Remove d from the interface's descriptor list.
304199481Srdivacky	 */
305199481Srdivacky	LIST_REMOVE(d, bd_next);
306199481Srdivacky
307199481Srdivacky	/*
308199481Srdivacky	 * Let the driver know that there are no more listeners.
309199481Srdivacky	 */
310199481Srdivacky	if (LIST_EMPTY(&bp->bif_dlist))
311199481Srdivacky		*bp->bif_driverp = NULL;
312199481Srdivacky
313199481Srdivacky	d->bd_bif = NULL;
314199989Srdivacky	BPFD_UNLOCK(d);
315199989Srdivacky	BPFIF_UNLOCK(bp);
316199481Srdivacky
317199481Srdivacky	/*
318199481Srdivacky	 * Check if this descriptor had requested promiscuous mode.
319193323Sed	 * If so, turn it off.
320193323Sed	 */
321193323Sed	if (d->bd_promisc) {
322198090Srdivacky		d->bd_promisc = 0;
323198090Srdivacky		error = ifpromisc(ifp, 0);
324198090Srdivacky		if (error != 0 && error != ENXIO) {
325198090Srdivacky			/*
326198090Srdivacky			 * ENXIO can happen if a pccard is unplugged
327198090Srdivacky			 * Something is really wrong if we were able to put
328198090Srdivacky			 * the driver into promiscuous mode, but can't
329202375Srdivacky			 * take it out.
330198090Srdivacky			 */
331198090Srdivacky			if_printf(bp->bif_ifp,
332198090Srdivacky				"bpf_detach: ifpromisc failed (%d)\n", error);
333193323Sed		}
334198090Srdivacky	}
335198090Srdivacky}
336198090Srdivacky
337198090Srdivacky/*
338198090Srdivacky * Open ethernet device.  Returns ENXIO for illegal minor device number,
339198396Srdivacky * EBUSY if file is open by another process.
340198396Srdivacky */
341198396Srdivacky/* ARGSUSED */
342198396Srdivackystatic	int
343198396Srdivackybpfopen(dev, flags, fmt, td)
344198396Srdivacky	struct cdev *dev;
345198396Srdivacky	int flags;
346198396Srdivacky	int fmt;
347198396Srdivacky	struct thread *td;
348198396Srdivacky{
349198396Srdivacky	struct bpf_d *d;
350198396Srdivacky
351198396Srdivacky	mtx_lock(&bpf_mtx);
352198396Srdivacky	d = dev->si_drv1;
353198396Srdivacky	/*
354198396Srdivacky	 * Each minor can be opened by only one process.  If the requested
355198396Srdivacky	 * minor is in use, return EBUSY.
356198396Srdivacky	 */
357198396Srdivacky	if (d != NULL) {
358198396Srdivacky		mtx_unlock(&bpf_mtx);
359198396Srdivacky		return (EBUSY);
360198396Srdivacky	}
361198396Srdivacky	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
362198396Srdivacky	mtx_unlock(&bpf_mtx);
363198396Srdivacky
364198396Srdivacky	if ((dev->si_flags & SI_NAMED) == 0)
365198396Srdivacky		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
366199481Srdivacky		    "bpf%d", dev2unit(dev));
367198396Srdivacky	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
368198396Srdivacky	dev->si_drv1 = d;
369198396Srdivacky	d->bd_bufsize = bpf_bufsize;
370198396Srdivacky	d->bd_sig = SIGIO;
371198396Srdivacky	d->bd_seesent = 1;
372198396Srdivacky#ifdef MAC
373198396Srdivacky	mac_init_bpfdesc(d);
374198396Srdivacky	mac_create_bpfdesc(td->td_ucred, d);
375198090Srdivacky#endif
376198090Srdivacky	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
377198090Srdivacky	callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE);
378198090Srdivacky	knlist_init(&d->bd_sel.si_note, &d->bd_mtx);
379198090Srdivacky
380198090Srdivacky	return (0);
381198090Srdivacky}
382198396Srdivacky
383198396Srdivacky/*
384198396Srdivacky * Close the descriptor by detaching it from its interface,
385198396Srdivacky * deallocating its buffers, and marking it free.
386198396Srdivacky */
387198090Srdivacky/* ARGSUSED */
388198090Srdivackystatic	int
389198090Srdivackybpfclose(dev, flags, fmt, td)
390198090Srdivacky	struct cdev *dev;
391198090Srdivacky	int flags;
392198090Srdivacky	int fmt;
393198090Srdivacky	struct thread *td;
394198396Srdivacky{
395198396Srdivacky	struct bpf_d *d = dev->si_drv1;
396198396Srdivacky
397198396Srdivacky	BPFD_LOCK(d);
398198396Srdivacky	if (d->bd_state == BPF_WAITING)
399198396Srdivacky		callout_stop(&d->bd_callout);
400198396Srdivacky	d->bd_state = BPF_IDLE;
401198396Srdivacky	BPFD_UNLOCK(d);
402198396Srdivacky	funsetown(&d->bd_sigio);
403198396Srdivacky	mtx_lock(&bpf_mtx);
404198396Srdivacky	if (d->bd_bif)
405198396Srdivacky		bpf_detachd(d);
406198396Srdivacky	mtx_unlock(&bpf_mtx);
407198396Srdivacky	selwakeuppri(&d->bd_sel, PRINET);
408198396Srdivacky#ifdef MAC
409198396Srdivacky	mac_destroy_bpfdesc(d);
410198396Srdivacky#endif /* MAC */
411198396Srdivacky	knlist_destroy(&d->bd_sel.si_note);
412198396Srdivacky	bpf_freed(d);
413198396Srdivacky	dev->si_drv1 = NULL;
414198396Srdivacky	free(d, M_BPF);
415198396Srdivacky
416198396Srdivacky	return (0);
417198396Srdivacky}
418198396Srdivacky
419198396Srdivacky
420198396Srdivacky/*
421198396Srdivacky * Rotate the packet buffers in descriptor d.  Move the store buffer
422198396Srdivacky * into the hold slot, and the free buffer into the store slot.
423198396Srdivacky * Zero the length of the new store buffer.
424198396Srdivacky */
425198396Srdivacky#define ROTATE_BUFFERS(d) \
426198396Srdivacky	(d)->bd_hbuf = (d)->bd_sbuf; \
427198396Srdivacky	(d)->bd_hlen = (d)->bd_slen; \
428198396Srdivacky	(d)->bd_sbuf = (d)->bd_fbuf; \
429198396Srdivacky	(d)->bd_slen = 0; \
430198396Srdivacky	(d)->bd_fbuf = NULL;
431198090Srdivacky/*
432198090Srdivacky *  bpfread - read next chunk of packets from buffers
433198090Srdivacky */
434198090Srdivackystatic	int
435198090Srdivackybpfread(dev, uio, ioflag)
436198090Srdivacky	struct cdev *dev;
437198090Srdivacky	struct uio *uio;
438198090Srdivacky	int ioflag;
439198090Srdivacky{
440198090Srdivacky	struct bpf_d *d = dev->si_drv1;
441198090Srdivacky	int timed_out;
442198090Srdivacky	int error;
443198090Srdivacky
444198396Srdivacky	/*
445198396Srdivacky	 * Restrict application to use a buffer the same size as
446198396Srdivacky	 * as kernel buffers.
447198396Srdivacky	 */
448198396Srdivacky	if (uio->uio_resid != d->bd_bufsize)
449198396Srdivacky		return (EINVAL);
450198396Srdivacky
451198396Srdivacky	BPFD_LOCK(d);
452198396Srdivacky	if (d->bd_state == BPF_WAITING)
453198396Srdivacky		callout_stop(&d->bd_callout);
454198396Srdivacky	timed_out = (d->bd_state == BPF_TIMED_OUT);
455198396Srdivacky	d->bd_state = BPF_IDLE;
456198396Srdivacky	/*
457198090Srdivacky	 * If the hold buffer is empty, then do a timed sleep, which
458198090Srdivacky	 * ends when the timeout expires or when enough packets
459198090Srdivacky	 * have arrived to fill the store buffer.
460198090Srdivacky	 */
461198090Srdivacky	while (d->bd_hbuf == NULL) {
462198090Srdivacky		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
463198090Srdivacky			/*
464198396Srdivacky			 * A packet(s) either arrived since the previous
465198396Srdivacky			 * read or arrived while we were asleep.
466198396Srdivacky			 * Rotate the buffers and return what's here.
467198396Srdivacky			 */
468198396Srdivacky			ROTATE_BUFFERS(d);
469198396Srdivacky			break;
470198396Srdivacky		}
471198396Srdivacky
472198396Srdivacky		/*
473198396Srdivacky		 * No data is available, check to see if the bpf device
474198396Srdivacky		 * is still pointed at a real interface.  If not, return
475198396Srdivacky		 * ENXIO so that the userland process knows to rebind
476198396Srdivacky		 * it before using it again.
477198396Srdivacky		 */
478198396Srdivacky		if (d->bd_bif == NULL) {
479198396Srdivacky			BPFD_UNLOCK(d);
480198396Srdivacky			return (ENXIO);
481198396Srdivacky		}
482198396Srdivacky
483198396Srdivacky		if (ioflag & O_NONBLOCK) {
484198396Srdivacky			BPFD_UNLOCK(d);
485198396Srdivacky			return (EWOULDBLOCK);
486198396Srdivacky		}
487198396Srdivacky		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
488198396Srdivacky		     "bpf", d->bd_rtout);
489198396Srdivacky		if (error == EINTR || error == ERESTART) {
490198396Srdivacky			BPFD_UNLOCK(d);
491198396Srdivacky			return (error);
492198396Srdivacky		}
493198396Srdivacky		if (error == EWOULDBLOCK) {
494198396Srdivacky			/*
495198396Srdivacky			 * On a timeout, return what's in the buffer,
496198396Srdivacky			 * which may be nothing.  If there is something
497198396Srdivacky			 * in the store buffer, we can rotate the buffers.
498198396Srdivacky			 */
499198396Srdivacky			if (d->bd_hbuf)
500198396Srdivacky				/*
501198396Srdivacky				 * We filled up the buffer in between
502198396Srdivacky				 * getting the timeout and arriving
503198396Srdivacky				 * here, so we don't need to rotate.
504198396Srdivacky				 */
505198396Srdivacky				break;
506198396Srdivacky
507198396Srdivacky			if (d->bd_slen == 0) {
508198396Srdivacky				BPFD_UNLOCK(d);
509198396Srdivacky				return (0);
510198396Srdivacky			}
511198396Srdivacky			ROTATE_BUFFERS(d);
512198396Srdivacky			break;
513198396Srdivacky		}
514198396Srdivacky	}
515198396Srdivacky	/*
516198396Srdivacky	 * At this point, we know we have something in the hold slot.
517198396Srdivacky	 */
518198396Srdivacky	BPFD_UNLOCK(d);
519198396Srdivacky
520198396Srdivacky	/*
521198396Srdivacky	 * Move data from hold buffer into user space.
522198396Srdivacky	 * We know the entire buffer is transferred since
523198396Srdivacky	 * we checked above that the read buffer is bpf_bufsize bytes.
524198396Srdivacky	 */
525198396Srdivacky	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
526198396Srdivacky
527198396Srdivacky	BPFD_LOCK(d);
528198396Srdivacky	d->bd_fbuf = d->bd_hbuf;
529198396Srdivacky	d->bd_hbuf = NULL;
530198396Srdivacky	d->bd_hlen = 0;
531198090Srdivacky	BPFD_UNLOCK(d);
532198090Srdivacky
533198090Srdivacky	return (error);
534198090Srdivacky}
535198090Srdivacky
536198396Srdivacky
537198090Srdivacky/*
538198090Srdivacky * If there are processes sleeping on this descriptor, wake them up.
539198090Srdivacky */
540198090Srdivackystatic __inline void
541198396Srdivackybpf_wakeup(d)
542198396Srdivacky	struct bpf_d *d;
543198396Srdivacky{
544198396Srdivacky
545198396Srdivacky	BPFD_LOCK_ASSERT(d);
546198090Srdivacky	if (d->bd_state == BPF_WAITING) {
547198090Srdivacky		callout_stop(&d->bd_callout);
548198396Srdivacky		d->bd_state = BPF_IDLE;
549198396Srdivacky	}
550198090Srdivacky	wakeup(d);
551198090Srdivacky	if (d->bd_async && d->bd_sig && d->bd_sigio)
552198396Srdivacky		pgsigio(&d->bd_sigio, d->bd_sig, 0);
553198396Srdivacky
554198396Srdivacky	selwakeuppri(&d->bd_sel, PRINET);
555198396Srdivacky	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
556198396Srdivacky}
557198396Srdivacky
558198396Srdivackystatic void
559198396Srdivackybpf_timed_out(arg)
560198396Srdivacky	void *arg;
561198396Srdivacky{
562198396Srdivacky	struct bpf_d *d = (struct bpf_d *)arg;
563198396Srdivacky
564198396Srdivacky	BPFD_LOCK(d);
565198396Srdivacky	if (d->bd_state == BPF_WAITING) {
566198396Srdivacky		d->bd_state = BPF_TIMED_OUT;
567198396Srdivacky		if (d->bd_slen != 0)
568198396Srdivacky			bpf_wakeup(d);
569198396Srdivacky	}
570198396Srdivacky	BPFD_UNLOCK(d);
571198396Srdivacky}
572198090Srdivacky
573198396Srdivackystatic	int
574198396Srdivackybpfwrite(dev, uio, ioflag)
575198396Srdivacky	struct cdev *dev;
576198396Srdivacky	struct uio *uio;
577198396Srdivacky	int ioflag;
578198396Srdivacky{
579198396Srdivacky	struct bpf_d *d = dev->si_drv1;
580198396Srdivacky	struct ifnet *ifp;
581198396Srdivacky	struct mbuf *m;
582198396Srdivacky	int error;
583198396Srdivacky	struct sockaddr dst;
584198396Srdivacky
585198396Srdivacky	if (d->bd_bif == NULL)
586198090Srdivacky		return (ENXIO);
587198396Srdivacky
588198090Srdivacky	ifp = d->bd_bif->bif_ifp;
589198396Srdivacky
590198090Srdivacky	if ((ifp->if_flags & IFF_UP) == 0)
591198396Srdivacky		return (ENETDOWN);
592198396Srdivacky
593198396Srdivacky	if (uio->uio_resid == 0)
594198396Srdivacky		return (0);
595198396Srdivacky
596198396Srdivacky	bzero(&dst, sizeof(dst));
597198396Srdivacky	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m, &dst);
598198090Srdivacky	if (error)
599198090Srdivacky		return (error);
600198090Srdivacky
601198090Srdivacky	if (d->bd_hdrcmplt)
602199481Srdivacky		dst.sa_family = pseudo_AF_HDRCMPLT;
603199481Srdivacky
604199481Srdivacky#ifdef MAC
605199481Srdivacky	BPFD_LOCK(d);
606199481Srdivacky	mac_create_mbuf_from_bpfdesc(d, m);
607199481Srdivacky	BPFD_UNLOCK(d);
608199481Srdivacky#endif
609199481Srdivacky	NET_LOCK_GIANT();
610199481Srdivacky	error = (*ifp->if_output)(ifp, m, &dst, NULL);
611199481Srdivacky	NET_UNLOCK_GIANT();
612199481Srdivacky	/*
613199481Srdivacky	 * The driver frees the mbuf.
614199481Srdivacky	 */
615199481Srdivacky	return (error);
616199481Srdivacky}
617199481Srdivacky
618199481Srdivacky/*
619199481Srdivacky * Reset a descriptor by flushing its packet buffer and clearing the
620199481Srdivacky * receive and drop counts.
621199481Srdivacky */
622199481Srdivackystatic void
623199481Srdivackyreset_d(d)
624199481Srdivacky	struct bpf_d *d;
625199481Srdivacky{
626199481Srdivacky
627199481Srdivacky	mtx_assert(&d->bd_mtx, MA_OWNED);
628199481Srdivacky	if (d->bd_hbuf) {
629199481Srdivacky		/* Free the hold buffer. */
630202375Srdivacky		d->bd_fbuf = d->bd_hbuf;
631202375Srdivacky		d->bd_hbuf = NULL;
632199481Srdivacky	}
633199481Srdivacky	d->bd_slen = 0;
634199481Srdivacky	d->bd_hlen = 0;
635199481Srdivacky	d->bd_rcount = 0;
636199481Srdivacky	d->bd_dcount = 0;
637199481Srdivacky}
638199481Srdivacky
639199481Srdivacky/*
640199481Srdivacky *  FIONREAD		Check for read packet available.
641199481Srdivacky *  SIOCGIFADDR		Get interface address - convenient hook to driver.
642199481Srdivacky *  BIOCGBLEN		Get buffer len [for read()].
643199481Srdivacky *  BIOCSETF		Set ethernet read filter.
644199481Srdivacky *  BIOCFLUSH		Flush read packet buffer.
645199481Srdivacky *  BIOCPROMISC		Put interface into promiscuous mode.
646199481Srdivacky *  BIOCGDLT		Get link layer type.
647199481Srdivacky *  BIOCGETIF		Get interface name.
648199481Srdivacky *  BIOCSETIF		Set interface.
649202375Srdivacky *  BIOCSRTIMEOUT	Set read timeout.
650199481Srdivacky *  BIOCGRTIMEOUT	Get read timeout.
651199481Srdivacky *  BIOCGSTATS		Get packet stats.
652199481Srdivacky *  BIOCIMMEDIATE	Set immediate mode.
653199481Srdivacky *  BIOCVERSION		Get filter language version.
654202375Srdivacky *  BIOCGHDRCMPLT	Get "header already complete" flag
655199481Srdivacky *  BIOCSHDRCMPLT	Set "header already complete" flag
656199481Srdivacky *  BIOCGSEESENT	Get "see packets sent" flag
657199481Srdivacky *  BIOCSSEESENT	Set "see packets sent" flag
658199481Srdivacky */
659202375Srdivacky/* ARGSUSED */
660199481Srdivackystatic	int
661199481Srdivackybpfioctl(dev, cmd, addr, flags, td)
662199481Srdivacky	struct cdev *dev;
663199481Srdivacky	u_long cmd;
664199481Srdivacky	caddr_t addr;
665199481Srdivacky	int flags;
666199481Srdivacky	struct thread *td;
667199481Srdivacky{
668199481Srdivacky	struct bpf_d *d = dev->si_drv1;
669199481Srdivacky	int error = 0;
670202375Srdivacky
671199481Srdivacky	BPFD_LOCK(d);
672199481Srdivacky	if (d->bd_state == BPF_WAITING)
673199481Srdivacky		callout_stop(&d->bd_callout);
674199481Srdivacky	d->bd_state = BPF_IDLE;
675199481Srdivacky	BPFD_UNLOCK(d);
676199481Srdivacky
677199481Srdivacky	switch (cmd) {
678199481Srdivacky
679199481Srdivacky	default:
680199481Srdivacky		error = EINVAL;
681199481Srdivacky		break;
682199481Srdivacky
683199481Srdivacky	/*
684199481Srdivacky	 * Check for read packet available.
685193323Sed	 */
686193323Sed	case FIONREAD:
687193323Sed		{
688198396Srdivacky			int n;
689198396Srdivacky
690198396Srdivacky			BPFD_LOCK(d);
691198396Srdivacky			n = d->bd_slen;
692198396Srdivacky			if (d->bd_hbuf)
693198090Srdivacky				n += d->bd_hlen;
694198090Srdivacky			BPFD_UNLOCK(d);
695198396Srdivacky
696198396Srdivacky			*(int *)addr = n;
697198090Srdivacky			break;
698198090Srdivacky		}
699198090Srdivacky
700193323Sed	case SIOCGIFADDR:
701198090Srdivacky		{
702198090Srdivacky			struct ifnet *ifp;
703193323Sed
704198090Srdivacky			if (d->bd_bif == NULL)
705193323Sed				error = EINVAL;
706193323Sed			else {
707198396Srdivacky				ifp = d->bd_bif->bif_ifp;
708193323Sed				error = (*ifp->if_ioctl)(ifp, cmd, addr);
709193323Sed			}
710202375Srdivacky			break;
711202375Srdivacky		}
712193323Sed
713193323Sed	/*
714198090Srdivacky	 * Get buffer len [for read()].
715193323Sed	 */
716198090Srdivacky	case BIOCGBLEN:
717198090Srdivacky		*(u_int *)addr = d->bd_bufsize;
718193323Sed		break;
719193323Sed
720193323Sed	/*
721198090Srdivacky	 * Set buffer length.
722198090Srdivacky	 */
723198090Srdivacky	case BIOCSBLEN:
724198090Srdivacky		if (d->bd_bif != NULL)
725193323Sed			error = EINVAL;
726193323Sed		else {
727193323Sed			u_int size = *(u_int *)addr;
728193323Sed
729193323Sed			if (size > bpf_maxbufsize)
730193323Sed				*(u_int *)addr = size = bpf_maxbufsize;
731193323Sed			else if (size < BPF_MINBUFSIZE)
732202375Srdivacky				*(u_int *)addr = size = BPF_MINBUFSIZE;
733193323Sed			d->bd_bufsize = size;
734193323Sed		}
735193323Sed		break;
736193323Sed
737193323Sed	/*
738198090Srdivacky	 * Set link layer read filter.
739198090Srdivacky	 */
740193323Sed	case BIOCSETF:
741199481Srdivacky		error = bpf_setf(d, (struct bpf_program *)addr);
742202375Srdivacky		break;
743199481Srdivacky
744199481Srdivacky	/*
745199481Srdivacky	 * Flush read packet buffer.
746199481Srdivacky	 */
747199481Srdivacky	case BIOCFLUSH:
748202375Srdivacky		BPFD_LOCK(d);
749202375Srdivacky		reset_d(d);
750199481Srdivacky		BPFD_UNLOCK(d);
751199481Srdivacky		break;
752199481Srdivacky
753202375Srdivacky	/*
754199481Srdivacky	 * Put interface into promiscuous mode.
755199481Srdivacky	 */
756199481Srdivacky	case BIOCPROMISC:
757199481Srdivacky		if (d->bd_bif == NULL) {
758199481Srdivacky			/*
759199481Srdivacky			 * No interface attached yet.
760199481Srdivacky			 */
761202375Srdivacky			error = EINVAL;
762202375Srdivacky			break;
763199481Srdivacky		}
764199481Srdivacky		if (d->bd_promisc == 0) {
765199481Srdivacky			mtx_lock(&Giant);
766199481Srdivacky			error = ifpromisc(d->bd_bif->bif_ifp, 1);
767199481Srdivacky			mtx_unlock(&Giant);
768199481Srdivacky			if (error == 0)
769199481Srdivacky				d->bd_promisc = 1;
770202375Srdivacky		}
771202375Srdivacky		break;
772199481Srdivacky
773199481Srdivacky	/*
774199481Srdivacky	 * Get current data link type.
775202375Srdivacky	 */
776199481Srdivacky	case BIOCGDLT:
777199481Srdivacky		if (d->bd_bif == NULL)
778199481Srdivacky			error = EINVAL;
779199481Srdivacky		else
780199481Srdivacky			*(u_int *)addr = d->bd_bif->bif_dlt;
781199481Srdivacky		break;
782199481Srdivacky
783202375Srdivacky	/*
784202375Srdivacky	 * Get a list of supported data link types.
785199481Srdivacky	 */
786199481Srdivacky	case BIOCGDLTLIST:
787199481Srdivacky		if (d->bd_bif == NULL)
788202375Srdivacky			error = EINVAL;
789199481Srdivacky		else
790199481Srdivacky			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
791199481Srdivacky		break;
792199481Srdivacky
793199481Srdivacky	/*
794199481Srdivacky	 * Set data link type.
795199481Srdivacky	 */
796202375Srdivacky	case BIOCSDLT:
797202375Srdivacky		if (d->bd_bif == NULL)
798199481Srdivacky			error = EINVAL;
799199481Srdivacky		else
800199481Srdivacky			error = bpf_setdlt(d, *(u_int *)addr);
801202375Srdivacky		break;
802199481Srdivacky
803199481Srdivacky	/*
804199481Srdivacky	 * Get interface name.
805199481Srdivacky	 */
806199481Srdivacky	case BIOCGETIF:
807193323Sed		if (d->bd_bif == NULL)
808193323Sed			error = EINVAL;
809193323Sed		else {
810202375Srdivacky			struct ifnet *const ifp = d->bd_bif->bif_ifp;
811193323Sed			struct ifreq *const ifr = (struct ifreq *)addr;
812198090Srdivacky
813202375Srdivacky			strlcpy(ifr->ifr_name, ifp->if_xname,
814202375Srdivacky			    sizeof(ifr->ifr_name));
815193323Sed		}
816193323Sed		break;
817198090Srdivacky
818198090Srdivacky	/*
819193323Sed	 * Set interface.
820193323Sed	 */
821193323Sed	case BIOCSETIF:
822		error = bpf_setif(d, (struct ifreq *)addr);
823		break;
824
825	/*
826	 * Set read timeout.
827	 */
828	case BIOCSRTIMEOUT:
829		{
830			struct timeval *tv = (struct timeval *)addr;
831
832			/*
833			 * Subtract 1 tick from tvtohz() since this isn't
834			 * a one-shot timer.
835			 */
836			if ((error = itimerfix(tv)) == 0)
837				d->bd_rtout = tvtohz(tv) - 1;
838			break;
839		}
840
841	/*
842	 * Get read timeout.
843	 */
844	case BIOCGRTIMEOUT:
845		{
846			struct timeval *tv = (struct timeval *)addr;
847
848			tv->tv_sec = d->bd_rtout / hz;
849			tv->tv_usec = (d->bd_rtout % hz) * tick;
850			break;
851		}
852
853	/*
854	 * Get packet stats.
855	 */
856	case BIOCGSTATS:
857		{
858			struct bpf_stat *bs = (struct bpf_stat *)addr;
859
860			bs->bs_recv = d->bd_rcount;
861			bs->bs_drop = d->bd_dcount;
862			break;
863		}
864
865	/*
866	 * Set immediate mode.
867	 */
868	case BIOCIMMEDIATE:
869		d->bd_immediate = *(u_int *)addr;
870		break;
871
872	case BIOCVERSION:
873		{
874			struct bpf_version *bv = (struct bpf_version *)addr;
875
876			bv->bv_major = BPF_MAJOR_VERSION;
877			bv->bv_minor = BPF_MINOR_VERSION;
878			break;
879		}
880
881	/*
882	 * Get "header already complete" flag
883	 */
884	case BIOCGHDRCMPLT:
885		*(u_int *)addr = d->bd_hdrcmplt;
886		break;
887
888	/*
889	 * Set "header already complete" flag
890	 */
891	case BIOCSHDRCMPLT:
892		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
893		break;
894
895	/*
896	 * Get "see sent packets" flag
897	 */
898	case BIOCGSEESENT:
899		*(u_int *)addr = d->bd_seesent;
900		break;
901
902	/*
903	 * Set "see sent packets" flag
904	 */
905	case BIOCSSEESENT:
906		d->bd_seesent = *(u_int *)addr;
907		break;
908
909	case FIONBIO:		/* Non-blocking I/O */
910		break;
911
912	case FIOASYNC:		/* Send signal on receive packets */
913		d->bd_async = *(int *)addr;
914		break;
915
916	case FIOSETOWN:
917		error = fsetown(*(int *)addr, &d->bd_sigio);
918		break;
919
920	case FIOGETOWN:
921		*(int *)addr = fgetown(&d->bd_sigio);
922		break;
923
924	/* This is deprecated, FIOSETOWN should be used instead. */
925	case TIOCSPGRP:
926		error = fsetown(-(*(int *)addr), &d->bd_sigio);
927		break;
928
929	/* This is deprecated, FIOGETOWN should be used instead. */
930	case TIOCGPGRP:
931		*(int *)addr = -fgetown(&d->bd_sigio);
932		break;
933
934	case BIOCSRSIG:		/* Set receive signal */
935		{
936			u_int sig;
937
938			sig = *(u_int *)addr;
939
940			if (sig >= NSIG)
941				error = EINVAL;
942			else
943				d->bd_sig = sig;
944			break;
945		}
946	case BIOCGRSIG:
947		*(u_int *)addr = d->bd_sig;
948		break;
949	}
950	return (error);
951}
952
953/*
954 * Set d's packet filter program to fp.  If this file already has a filter,
955 * free it and replace it.  Returns EINVAL for bogus requests.
956 */
957static int
958bpf_setf(d, fp)
959	struct bpf_d *d;
960	struct bpf_program *fp;
961{
962	struct bpf_insn *fcode, *old;
963	u_int flen, size;
964
965	if (fp->bf_insns == NULL) {
966		if (fp->bf_len != 0)
967			return (EINVAL);
968		BPFD_LOCK(d);
969		old = d->bd_filter;
970		d->bd_filter = NULL;
971		reset_d(d);
972		BPFD_UNLOCK(d);
973		if (old != NULL)
974			free((caddr_t)old, M_BPF);
975		return (0);
976	}
977	flen = fp->bf_len;
978	if (flen > bpf_maxinsns)
979		return (EINVAL);
980
981	size = flen * sizeof(*fp->bf_insns);
982	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
983	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
984	    bpf_validate(fcode, (int)flen)) {
985		BPFD_LOCK(d);
986		old = d->bd_filter;
987		d->bd_filter = fcode;
988		reset_d(d);
989		BPFD_UNLOCK(d);
990		if (old != NULL)
991			free((caddr_t)old, M_BPF);
992
993		return (0);
994	}
995	free((caddr_t)fcode, M_BPF);
996	return (EINVAL);
997}
998
999/*
1000 * Detach a file from its current interface (if attached at all) and attach
1001 * to the interface indicated by the name stored in ifr.
1002 * Return an errno or 0.
1003 */
1004static int
1005bpf_setif(d, ifr)
1006	struct bpf_d *d;
1007	struct ifreq *ifr;
1008{
1009	struct bpf_if *bp;
1010	int error;
1011	struct ifnet *theywant;
1012
1013	theywant = ifunit(ifr->ifr_name);
1014	if (theywant == NULL)
1015		return ENXIO;
1016
1017	/*
1018	 * Look through attached interfaces for the named one.
1019	 */
1020	mtx_lock(&bpf_mtx);
1021	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1022		struct ifnet *ifp = bp->bif_ifp;
1023
1024		if (ifp == NULL || ifp != theywant)
1025			continue;
1026		/* skip additional entry */
1027		if (bp->bif_driverp != &ifp->if_bpf)
1028			continue;
1029
1030		mtx_unlock(&bpf_mtx);
1031		/*
1032		 * We found the requested interface.
1033		 * Allocate the packet buffers if we need to.
1034		 * If we're already attached to requested interface,
1035		 * just flush the buffer.
1036		 */
1037		if (d->bd_sbuf == NULL) {
1038			error = bpf_allocbufs(d);
1039			if (error != 0)
1040				return (error);
1041		}
1042		if (bp != d->bd_bif) {
1043			if (d->bd_bif)
1044				/*
1045				 * Detach if attached to something else.
1046				 */
1047				bpf_detachd(d);
1048
1049			bpf_attachd(d, bp);
1050		}
1051		BPFD_LOCK(d);
1052		reset_d(d);
1053		BPFD_UNLOCK(d);
1054		return (0);
1055	}
1056	mtx_unlock(&bpf_mtx);
1057	/* Not found. */
1058	return (ENXIO);
1059}
1060
1061/*
1062 * Support for select() and poll() system calls
1063 *
1064 * Return true iff the specific operation will not block indefinitely.
1065 * Otherwise, return false but make a note that a selwakeup() must be done.
1066 */
1067static int
1068bpfpoll(dev, events, td)
1069	struct cdev *dev;
1070	int events;
1071	struct thread *td;
1072{
1073	struct bpf_d *d;
1074	int revents;
1075
1076	d = dev->si_drv1;
1077	if (d->bd_bif == NULL)
1078		return (ENXIO);
1079
1080	revents = events & (POLLOUT | POLLWRNORM);
1081	BPFD_LOCK(d);
1082	if (events & (POLLIN | POLLRDNORM)) {
1083		if (bpf_ready(d))
1084			revents |= events & (POLLIN | POLLRDNORM);
1085		else {
1086			selrecord(td, &d->bd_sel);
1087			/* Start the read timeout if necessary. */
1088			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1089				callout_reset(&d->bd_callout, d->bd_rtout,
1090				    bpf_timed_out, d);
1091				d->bd_state = BPF_WAITING;
1092			}
1093		}
1094	}
1095	BPFD_UNLOCK(d);
1096	return (revents);
1097}
1098
1099/*
1100 * Support for kevent() system call.  Register EVFILT_READ filters and
1101 * reject all others.
1102 */
1103int
1104bpfkqfilter(dev, kn)
1105	struct cdev *dev;
1106	struct knote *kn;
1107{
1108	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1109
1110	if (kn->kn_filter != EVFILT_READ)
1111		return (1);
1112
1113	kn->kn_fop = &bpfread_filtops;
1114	kn->kn_hook = d;
1115	knlist_add(&d->bd_sel.si_note, kn, 0);
1116
1117	return (0);
1118}
1119
1120static void
1121filt_bpfdetach(kn)
1122	struct knote *kn;
1123{
1124	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1125
1126	knlist_remove(&d->bd_sel.si_note, kn, 0);
1127}
1128
1129static int
1130filt_bpfread(kn, hint)
1131	struct knote *kn;
1132	long hint;
1133{
1134	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1135	int ready;
1136
1137	BPFD_LOCK_ASSERT(d);
1138	ready = bpf_ready(d);
1139	if (ready) {
1140		kn->kn_data = d->bd_slen;
1141		if (d->bd_hbuf)
1142			kn->kn_data += d->bd_hlen;
1143	}
1144	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1145		callout_reset(&d->bd_callout, d->bd_rtout,
1146		    bpf_timed_out, d);
1147		d->bd_state = BPF_WAITING;
1148	}
1149
1150	return (ready);
1151}
1152
1153/*
1154 * Incoming linkage from device drivers.  Process the packet pkt, of length
1155 * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1156 * by each process' filter, and if accepted, stashed into the corresponding
1157 * buffer.
1158 */
1159void
1160bpf_tap(bp, pkt, pktlen)
1161	struct bpf_if *bp;
1162	u_char *pkt;
1163	u_int pktlen;
1164{
1165	struct bpf_d *d;
1166	u_int slen;
1167
1168	/*
1169	 * Lockless read to avoid cost of locking the interface if there are
1170	 * no descriptors attached.
1171	 */
1172	if (LIST_EMPTY(&bp->bif_dlist))
1173		return;
1174
1175	BPFIF_LOCK(bp);
1176	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1177		BPFD_LOCK(d);
1178		++d->bd_rcount;
1179		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1180		if (slen != 0) {
1181#ifdef MAC
1182			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1183#endif
1184				catchpacket(d, pkt, pktlen, slen, bcopy);
1185		}
1186		BPFD_UNLOCK(d);
1187	}
1188	BPFIF_UNLOCK(bp);
1189}
1190
1191/*
1192 * Copy data from an mbuf chain into a buffer.  This code is derived
1193 * from m_copydata in sys/uipc_mbuf.c.
1194 */
1195static void
1196bpf_mcopy(src_arg, dst_arg, len)
1197	const void *src_arg;
1198	void *dst_arg;
1199	size_t len;
1200{
1201	const struct mbuf *m;
1202	u_int count;
1203	u_char *dst;
1204
1205	m = src_arg;
1206	dst = dst_arg;
1207	while (len > 0) {
1208		if (m == NULL)
1209			panic("bpf_mcopy");
1210		count = min(m->m_len, len);
1211		bcopy(mtod(m, void *), dst, count);
1212		m = m->m_next;
1213		dst += count;
1214		len -= count;
1215	}
1216}
1217
1218/*
1219 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1220 */
1221void
1222bpf_mtap(bp, m)
1223	struct bpf_if *bp;
1224	struct mbuf *m;
1225{
1226	struct bpf_d *d;
1227	u_int pktlen, slen;
1228
1229	/*
1230	 * Lockless read to avoid cost of locking the interface if there are
1231	 * no descriptors attached.
1232	 */
1233	if (LIST_EMPTY(&bp->bif_dlist))
1234		return;
1235
1236	pktlen = m_length(m, NULL);
1237	if (pktlen == m->m_len) {
1238		bpf_tap(bp, mtod(m, u_char *), pktlen);
1239		return;
1240	}
1241
1242	BPFIF_LOCK(bp);
1243	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1244		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1245			continue;
1246		BPFD_LOCK(d);
1247		++d->bd_rcount;
1248		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1249		if (slen != 0)
1250#ifdef MAC
1251			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1252#endif
1253				catchpacket(d, (u_char *)m, pktlen, slen,
1254				    bpf_mcopy);
1255		BPFD_UNLOCK(d);
1256	}
1257	BPFIF_UNLOCK(bp);
1258}
1259
1260/*
1261 * Incoming linkage from device drivers, when packet is in
1262 * an mbuf chain and to be prepended by a contiguous header.
1263 */
1264void
1265bpf_mtap2(bp, data, dlen, m)
1266	struct bpf_if *bp;
1267	void *data;
1268	u_int dlen;
1269	struct mbuf *m;
1270{
1271	struct mbuf mb;
1272	struct bpf_d *d;
1273	u_int pktlen, slen;
1274
1275	/*
1276	 * Lockless read to avoid cost of locking the interface if there are
1277	 * no descriptors attached.
1278	 */
1279	if (LIST_EMPTY(&bp->bif_dlist))
1280		return;
1281
1282	pktlen = m_length(m, NULL);
1283	/*
1284	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1285	 * Note that we cut corners here; we only setup what's
1286	 * absolutely needed--this mbuf should never go anywhere else.
1287	 */
1288	mb.m_next = m;
1289	mb.m_data = data;
1290	mb.m_len = dlen;
1291	pktlen += dlen;
1292
1293	BPFIF_LOCK(bp);
1294	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1295		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1296			continue;
1297		BPFD_LOCK(d);
1298		++d->bd_rcount;
1299		slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1300		if (slen != 0)
1301#ifdef MAC
1302			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1303#endif
1304				catchpacket(d, (u_char *)&mb, pktlen, slen,
1305				    bpf_mcopy);
1306		BPFD_UNLOCK(d);
1307	}
1308	BPFIF_UNLOCK(bp);
1309}
1310
1311/*
1312 * Move the packet data from interface memory (pkt) into the
1313 * store buffer.  "cpfn" is the routine called to do the actual data
1314 * transfer.  bcopy is passed in to copy contiguous chunks, while
1315 * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1316 * pkt is really an mbuf.
1317 */
1318static void
1319catchpacket(d, pkt, pktlen, snaplen, cpfn)
1320	struct bpf_d *d;
1321	u_char *pkt;
1322	u_int pktlen, snaplen;
1323	void (*cpfn)(const void *, void *, size_t);
1324{
1325	struct bpf_hdr *hp;
1326	int totlen, curlen;
1327	int hdrlen = d->bd_bif->bif_hdrlen;
1328	int do_wakeup = 0;
1329
1330	BPFD_LOCK_ASSERT(d);
1331	/*
1332	 * Figure out how many bytes to move.  If the packet is
1333	 * greater or equal to the snapshot length, transfer that
1334	 * much.  Otherwise, transfer the whole packet (unless
1335	 * we hit the buffer size limit).
1336	 */
1337	totlen = hdrlen + min(snaplen, pktlen);
1338	if (totlen > d->bd_bufsize)
1339		totlen = d->bd_bufsize;
1340
1341	/*
1342	 * Round up the end of the previous packet to the next longword.
1343	 */
1344	curlen = BPF_WORDALIGN(d->bd_slen);
1345	if (curlen + totlen > d->bd_bufsize) {
1346		/*
1347		 * This packet will overflow the storage buffer.
1348		 * Rotate the buffers if we can, then wakeup any
1349		 * pending reads.
1350		 */
1351		if (d->bd_fbuf == NULL) {
1352			/*
1353			 * We haven't completed the previous read yet,
1354			 * so drop the packet.
1355			 */
1356			++d->bd_dcount;
1357			return;
1358		}
1359		ROTATE_BUFFERS(d);
1360		do_wakeup = 1;
1361		curlen = 0;
1362	}
1363	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1364		/*
1365		 * Immediate mode is set, or the read timeout has
1366		 * already expired during a select call.  A packet
1367		 * arrived, so the reader should be woken up.
1368		 */
1369		do_wakeup = 1;
1370
1371	/*
1372	 * Append the bpf header.
1373	 */
1374	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1375	microtime(&hp->bh_tstamp);
1376	hp->bh_datalen = pktlen;
1377	hp->bh_hdrlen = hdrlen;
1378	/*
1379	 * Copy the packet data into the store buffer and update its length.
1380	 */
1381	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1382	d->bd_slen = curlen + totlen;
1383
1384	if (do_wakeup)
1385		bpf_wakeup(d);
1386}
1387
1388/*
1389 * Initialize all nonzero fields of a descriptor.
1390 */
1391static int
1392bpf_allocbufs(d)
1393	struct bpf_d *d;
1394{
1395	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1396	if (d->bd_fbuf == NULL)
1397		return (ENOBUFS);
1398
1399	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1400	if (d->bd_sbuf == NULL) {
1401		free(d->bd_fbuf, M_BPF);
1402		return (ENOBUFS);
1403	}
1404	d->bd_slen = 0;
1405	d->bd_hlen = 0;
1406	return (0);
1407}
1408
1409/*
1410 * Free buffers currently in use by a descriptor.
1411 * Called on close.
1412 */
1413static void
1414bpf_freed(d)
1415	struct bpf_d *d;
1416{
1417	/*
1418	 * We don't need to lock out interrupts since this descriptor has
1419	 * been detached from its interface and it yet hasn't been marked
1420	 * free.
1421	 */
1422	if (d->bd_sbuf != NULL) {
1423		free(d->bd_sbuf, M_BPF);
1424		if (d->bd_hbuf != NULL)
1425			free(d->bd_hbuf, M_BPF);
1426		if (d->bd_fbuf != NULL)
1427			free(d->bd_fbuf, M_BPF);
1428	}
1429	if (d->bd_filter)
1430		free((caddr_t)d->bd_filter, M_BPF);
1431	mtx_destroy(&d->bd_mtx);
1432}
1433
1434/*
1435 * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
1436 * fixed size of the link header (variable length headers not yet supported).
1437 */
1438void
1439bpfattach(ifp, dlt, hdrlen)
1440	struct ifnet *ifp;
1441	u_int dlt, hdrlen;
1442{
1443
1444	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1445}
1446
1447/*
1448 * Attach an interface to bpf.  ifp is a pointer to the structure
1449 * defining the interface to be attached, dlt is the link layer type,
1450 * and hdrlen is the fixed size of the link header (variable length
1451 * headers are not yet supporrted).
1452 */
1453void
1454bpfattach2(ifp, dlt, hdrlen, driverp)
1455	struct ifnet *ifp;
1456	u_int dlt, hdrlen;
1457	struct bpf_if **driverp;
1458{
1459	struct bpf_if *bp;
1460	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1461	if (bp == NULL)
1462		panic("bpfattach");
1463
1464	LIST_INIT(&bp->bif_dlist);
1465	bp->bif_driverp = driverp;
1466	bp->bif_ifp = ifp;
1467	bp->bif_dlt = dlt;
1468	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1469
1470	mtx_lock(&bpf_mtx);
1471	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1472	mtx_unlock(&bpf_mtx);
1473
1474	*bp->bif_driverp = NULL;
1475
1476	/*
1477	 * Compute the length of the bpf header.  This is not necessarily
1478	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1479	 * that the network layer header begins on a longword boundary (for
1480	 * performance reasons and to alleviate alignment restrictions).
1481	 */
1482	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1483
1484	if (bootverbose)
1485		if_printf(ifp, "bpf attached\n");
1486}
1487
1488/*
1489 * Detach bpf from an interface.  This involves detaching each descriptor
1490 * associated with the interface, and leaving bd_bif NULL.  Notify each
1491 * descriptor as it's detached so that any sleepers wake up and get
1492 * ENXIO.
1493 */
1494void
1495bpfdetach(ifp)
1496	struct ifnet *ifp;
1497{
1498	struct bpf_if	*bp;
1499	struct bpf_d	*d;
1500
1501	/* Locate BPF interface information */
1502	mtx_lock(&bpf_mtx);
1503	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1504		if (ifp == bp->bif_ifp)
1505			break;
1506	}
1507
1508	/* Interface wasn't attached */
1509	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1510		mtx_unlock(&bpf_mtx);
1511		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1512		return;
1513	}
1514
1515	LIST_REMOVE(bp, bif_next);
1516	mtx_unlock(&bpf_mtx);
1517
1518	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1519		bpf_detachd(d);
1520		BPFD_LOCK(d);
1521		bpf_wakeup(d);
1522		BPFD_UNLOCK(d);
1523	}
1524
1525	mtx_destroy(&bp->bif_mtx);
1526	free(bp, M_BPF);
1527}
1528
1529/*
1530 * Get a list of available data link type of the interface.
1531 */
1532static int
1533bpf_getdltlist(d, bfl)
1534	struct bpf_d *d;
1535	struct bpf_dltlist *bfl;
1536{
1537	int n, error;
1538	struct ifnet *ifp;
1539	struct bpf_if *bp;
1540
1541	ifp = d->bd_bif->bif_ifp;
1542	n = 0;
1543	error = 0;
1544	mtx_lock(&bpf_mtx);
1545	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1546		if (bp->bif_ifp != ifp)
1547			continue;
1548		if (bfl->bfl_list != NULL) {
1549			if (n >= bfl->bfl_len) {
1550				mtx_unlock(&bpf_mtx);
1551				return (ENOMEM);
1552			}
1553			error = copyout(&bp->bif_dlt,
1554			    bfl->bfl_list + n, sizeof(u_int));
1555		}
1556		n++;
1557	}
1558	mtx_unlock(&bpf_mtx);
1559	bfl->bfl_len = n;
1560	return (error);
1561}
1562
1563/*
1564 * Set the data link type of a BPF instance.
1565 */
1566static int
1567bpf_setdlt(d, dlt)
1568	struct bpf_d *d;
1569	u_int dlt;
1570{
1571	int error, opromisc;
1572	struct ifnet *ifp;
1573	struct bpf_if *bp;
1574
1575	if (d->bd_bif->bif_dlt == dlt)
1576		return (0);
1577	ifp = d->bd_bif->bif_ifp;
1578	mtx_lock(&bpf_mtx);
1579	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1580		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1581			break;
1582	}
1583	mtx_unlock(&bpf_mtx);
1584	if (bp != NULL) {
1585		opromisc = d->bd_promisc;
1586		bpf_detachd(d);
1587		bpf_attachd(d, bp);
1588		BPFD_LOCK(d);
1589		reset_d(d);
1590		BPFD_UNLOCK(d);
1591		if (opromisc) {
1592			error = ifpromisc(bp->bif_ifp, 1);
1593			if (error)
1594				if_printf(bp->bif_ifp,
1595					"bpf_setdlt: ifpromisc failed (%d)\n",
1596					error);
1597			else
1598				d->bd_promisc = 1;
1599		}
1600	}
1601	return (bp == NULL ? EINVAL : 0);
1602}
1603
1604static void
1605bpf_clone(arg, name, namelen, dev)
1606	void *arg;
1607	char *name;
1608	int namelen;
1609	struct cdev **dev;
1610{
1611	int u;
1612
1613	if (*dev != NULL)
1614		return;
1615	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1616		return;
1617	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1618	    "bpf%d", u);
1619	dev_ref(*dev);
1620	(*dev)->si_flags |= SI_CHEAPCLONE;
1621	return;
1622}
1623
1624static void
1625bpf_drvinit(unused)
1626	void *unused;
1627{
1628
1629	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1630	LIST_INIT(&bpf_iflist);
1631	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1632}
1633
1634SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
1635
1636#else /* !DEV_BPF && !NETGRAPH_BPF */
1637/*
1638 * NOP stubs to allow bpf-using drivers to load and function.
1639 *
1640 * A 'better' implementation would allow the core bpf functionality
1641 * to be loaded at runtime.
1642 */
1643
1644void
1645bpf_tap(bp, pkt, pktlen)
1646	struct bpf_if *bp;
1647	u_char *pkt;
1648	u_int pktlen;
1649{
1650}
1651
1652void
1653bpf_mtap(bp, m)
1654	struct bpf_if *bp;
1655	struct mbuf *m;
1656{
1657}
1658
1659void
1660bpf_mtap2(bp, d, l, m)
1661	struct bpf_if *bp;
1662	void *d;
1663	u_int l;
1664	struct mbuf *m;
1665{
1666}
1667
1668void
1669bpfattach(ifp, dlt, hdrlen)
1670	struct ifnet *ifp;
1671	u_int dlt, hdrlen;
1672{
1673}
1674
1675void
1676bpfattach2(ifp, dlt, hdrlen, driverp)
1677	struct ifnet *ifp;
1678	u_int dlt, hdrlen;
1679	struct bpf_if **driverp;
1680{
1681}
1682
1683void
1684bpfdetach(ifp)
1685	struct ifnet *ifp;
1686{
1687}
1688
1689u_int
1690bpf_filter(pc, p, wirelen, buflen)
1691	const struct bpf_insn *pc;
1692	u_char *p;
1693	u_int wirelen;
1694	u_int buflen;
1695{
1696	return -1;	/* "no filter" behaviour */
1697}
1698
1699int
1700bpf_validate(f, len)
1701	const struct bpf_insn *f;
1702	int len;
1703{
1704	return 0;		/* false */
1705}
1706
1707#endif /* !DEV_BPF && !NETGRAPH_BPF */
1708