bpf.c revision 138950
1/*
2 * Copyright (c) 1990, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from the Stanford/CMU enet packet filter,
6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8 * Berkeley Laboratory.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
35 *
36 * $FreeBSD: head/sys/net/bpf.c 138950 2004-12-17 03:21:46Z jmg $
37 */
38
39#include "opt_bpf.h"
40#include "opt_mac.h"
41#include "opt_netgraph.h"
42
43#include <sys/types.h>
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/conf.h>
47#include <sys/mac.h>
48#include <sys/malloc.h>
49#include <sys/mbuf.h>
50#include <sys/time.h>
51#include <sys/proc.h>
52#include <sys/signalvar.h>
53#include <sys/filio.h>
54#include <sys/sockio.h>
55#include <sys/ttycom.h>
56#include <sys/filedesc.h>
57
58#include <sys/event.h>
59#include <sys/file.h>
60#include <sys/poll.h>
61#include <sys/proc.h>
62
63#include <sys/socket.h>
64#include <sys/vnode.h>
65
66#include <net/if.h>
67#include <net/bpf.h>
68#include <net/bpfdesc.h>
69
70#include <netinet/in.h>
71#include <netinet/if_ether.h>
72#include <sys/kernel.h>
73#include <sys/sysctl.h>
74
75static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
76
77#if defined(DEV_BPF) || defined(NETGRAPH_BPF)
78
79#define PRINET  26			/* interruptible */
80
81/*
82 * The default read buffer size is patchable.
83 */
84static int bpf_bufsize = 4096;
85SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
86	&bpf_bufsize, 0, "");
87static int bpf_maxbufsize = BPF_MAXBUFSIZE;
88SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
89	&bpf_maxbufsize, 0, "");
90
91/*
92 *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
93 */
94static LIST_HEAD(, bpf_if)	bpf_iflist;
95static struct mtx	bpf_mtx;		/* bpf global lock */
96
97static int	bpf_allocbufs(struct bpf_d *);
98static void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
99static void	bpf_detachd(struct bpf_d *d);
100static void	bpf_freed(struct bpf_d *);
101static void	bpf_mcopy(const void *, void *, size_t);
102static int	bpf_movein(struct uio *, int,
103		    struct mbuf **, struct sockaddr *, int *);
104static int	bpf_setif(struct bpf_d *, struct ifreq *);
105static void	bpf_timed_out(void *);
106static __inline void
107		bpf_wakeup(struct bpf_d *);
108static void	catchpacket(struct bpf_d *, u_char *, u_int,
109		    u_int, void (*)(const void *, void *, size_t));
110static void	reset_d(struct bpf_d *);
111static int	 bpf_setf(struct bpf_d *, struct bpf_program *);
112static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
113static int	bpf_setdlt(struct bpf_d *, u_int);
114static void	filt_bpfdetach(struct knote *);
115static int	filt_bpfread(struct knote *, long);
116
117static	d_open_t	bpfopen;
118static	d_close_t	bpfclose;
119static	d_read_t	bpfread;
120static	d_write_t	bpfwrite;
121static	d_ioctl_t	bpfioctl;
122static	d_poll_t	bpfpoll;
123static	d_kqfilter_t	bpfkqfilter;
124
125static struct cdevsw bpf_cdevsw = {
126	.d_version =	D_VERSION,
127	.d_flags =	D_NEEDGIANT,
128	.d_open =	bpfopen,
129	.d_close =	bpfclose,
130	.d_read =	bpfread,
131	.d_write =	bpfwrite,
132	.d_ioctl =	bpfioctl,
133	.d_poll =	bpfpoll,
134	.d_name =	"bpf",
135	.d_kqfilter =	bpfkqfilter,
136};
137
138static struct filterops bpfread_filtops =
139	{ 1, NULL, filt_bpfdetach, filt_bpfread };
140
141static int
142bpf_movein(uio, linktype, mp, sockp, datlen)
143	struct uio *uio;
144	int linktype, *datlen;
145	struct mbuf **mp;
146	struct sockaddr *sockp;
147{
148	struct mbuf *m;
149	int error;
150	int len;
151	int hlen;
152
153	/*
154	 * Build a sockaddr based on the data link layer type.
155	 * We do this at this level because the ethernet header
156	 * is copied directly into the data field of the sockaddr.
157	 * In the case of SLIP, there is no header and the packet
158	 * is forwarded as is.
159	 * Also, we are careful to leave room at the front of the mbuf
160	 * for the link level header.
161	 */
162	switch (linktype) {
163
164	case DLT_SLIP:
165		sockp->sa_family = AF_INET;
166		hlen = 0;
167		break;
168
169	case DLT_EN10MB:
170		sockp->sa_family = AF_UNSPEC;
171		/* XXX Would MAXLINKHDR be better? */
172		hlen = ETHER_HDR_LEN;
173		break;
174
175	case DLT_FDDI:
176		sockp->sa_family = AF_IMPLINK;
177		hlen = 0;
178		break;
179
180	case DLT_RAW:
181	case DLT_NULL:
182		sockp->sa_family = AF_UNSPEC;
183		hlen = 0;
184		break;
185
186	case DLT_ATM_RFC1483:
187		/*
188		 * en atm driver requires 4-byte atm pseudo header.
189		 * though it isn't standard, vpi:vci needs to be
190		 * specified anyway.
191		 */
192		sockp->sa_family = AF_UNSPEC;
193		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
194		break;
195
196	case DLT_PPP:
197		sockp->sa_family = AF_UNSPEC;
198		hlen = 4;	/* This should match PPP_HDRLEN */
199		break;
200
201	default:
202		return (EIO);
203	}
204
205	len = uio->uio_resid;
206	*datlen = len - hlen;
207	if ((unsigned)len > MCLBYTES)
208		return (EIO);
209
210	if (len > MHLEN) {
211		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
212	} else {
213		MGETHDR(m, M_TRYWAIT, MT_DATA);
214	}
215	if (m == NULL)
216		return (ENOBUFS);
217	m->m_pkthdr.len = m->m_len = len;
218	m->m_pkthdr.rcvif = NULL;
219	*mp = m;
220
221	/*
222	 * Make room for link header.
223	 */
224	if (hlen != 0) {
225		m->m_pkthdr.len -= hlen;
226		m->m_len -= hlen;
227#if BSD >= 199103
228		m->m_data += hlen; /* XXX */
229#else
230		m->m_off += hlen;
231#endif
232		error = uiomove(sockp->sa_data, hlen, uio);
233		if (error)
234			goto bad;
235	}
236	error = uiomove(mtod(m, void *), len - hlen, uio);
237	if (!error)
238		return (0);
239bad:
240	m_freem(m);
241	return (error);
242}
243
244/*
245 * Attach file to the bpf interface, i.e. make d listen on bp.
246 */
247static void
248bpf_attachd(d, bp)
249	struct bpf_d *d;
250	struct bpf_if *bp;
251{
252	/*
253	 * Point d at bp, and add d to the interface's list of listeners.
254	 * Finally, point the driver's bpf cookie at the interface so
255	 * it will divert packets to bpf.
256	 */
257	BPFIF_LOCK(bp);
258	d->bd_bif = bp;
259	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
260
261	*bp->bif_driverp = bp;
262	BPFIF_UNLOCK(bp);
263}
264
265/*
266 * Detach a file from its interface.
267 */
268static void
269bpf_detachd(d)
270	struct bpf_d *d;
271{
272	int error;
273	struct bpf_if *bp;
274	struct ifnet *ifp;
275
276	bp = d->bd_bif;
277	BPFIF_LOCK(bp);
278	BPFD_LOCK(d);
279	ifp = d->bd_bif->bif_ifp;
280
281	/*
282	 * Remove d from the interface's descriptor list.
283	 */
284	LIST_REMOVE(d, bd_next);
285
286	/*
287	 * Let the driver know that there are no more listeners.
288	 */
289	if (LIST_EMPTY(&bp->bif_dlist))
290		*bp->bif_driverp = NULL;
291
292	d->bd_bif = NULL;
293	BPFD_UNLOCK(d);
294	BPFIF_UNLOCK(bp);
295
296	/*
297	 * Check if this descriptor had requested promiscuous mode.
298	 * If so, turn it off.
299	 */
300	if (d->bd_promisc) {
301		d->bd_promisc = 0;
302		error = ifpromisc(ifp, 0);
303		if (error != 0 && error != ENXIO) {
304			/*
305			 * ENXIO can happen if a pccard is unplugged
306			 * Something is really wrong if we were able to put
307			 * the driver into promiscuous mode, but can't
308			 * take it out.
309			 */
310			if_printf(bp->bif_ifp,
311				"bpf_detach: ifpromisc failed (%d)\n", error);
312		}
313	}
314}
315
316/*
317 * Open ethernet device.  Returns ENXIO for illegal minor device number,
318 * EBUSY if file is open by another process.
319 */
320/* ARGSUSED */
321static	int
322bpfopen(dev, flags, fmt, td)
323	struct cdev *dev;
324	int flags;
325	int fmt;
326	struct thread *td;
327{
328	struct bpf_d *d;
329
330	mtx_lock(&bpf_mtx);
331	d = dev->si_drv1;
332	/*
333	 * Each minor can be opened by only one process.  If the requested
334	 * minor is in use, return EBUSY.
335	 */
336	if (d != NULL) {
337		mtx_unlock(&bpf_mtx);
338		return (EBUSY);
339	}
340	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
341	mtx_unlock(&bpf_mtx);
342
343	if ((dev->si_flags & SI_NAMED) == 0)
344		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
345		    "bpf%d", dev2unit(dev));
346	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
347	dev->si_drv1 = d;
348	d->bd_bufsize = bpf_bufsize;
349	d->bd_sig = SIGIO;
350	d->bd_seesent = 1;
351#ifdef MAC
352	mac_init_bpfdesc(d);
353	mac_create_bpfdesc(td->td_ucred, d);
354#endif
355	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
356	callout_init(&d->bd_callout, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
357	knlist_init(&d->bd_sel.si_note, &d->bd_mtx);
358
359	return (0);
360}
361
362/*
363 * Close the descriptor by detaching it from its interface,
364 * deallocating its buffers, and marking it free.
365 */
366/* ARGSUSED */
367static	int
368bpfclose(dev, flags, fmt, td)
369	struct cdev *dev;
370	int flags;
371	int fmt;
372	struct thread *td;
373{
374	struct bpf_d *d = dev->si_drv1;
375
376	BPFD_LOCK(d);
377	if (d->bd_state == BPF_WAITING)
378		callout_stop(&d->bd_callout);
379	d->bd_state = BPF_IDLE;
380	BPFD_UNLOCK(d);
381	funsetown(&d->bd_sigio);
382	mtx_lock(&bpf_mtx);
383	if (d->bd_bif)
384		bpf_detachd(d);
385	mtx_unlock(&bpf_mtx);
386#ifdef MAC
387	mac_destroy_bpfdesc(d);
388#endif /* MAC */
389	knlist_destroy(&d->bd_sel.si_note);
390	bpf_freed(d);
391	dev->si_drv1 = NULL;
392	free(d, M_BPF);
393
394	return (0);
395}
396
397
398/*
399 * Rotate the packet buffers in descriptor d.  Move the store buffer
400 * into the hold slot, and the free buffer into the store slot.
401 * Zero the length of the new store buffer.
402 */
403#define ROTATE_BUFFERS(d) \
404	(d)->bd_hbuf = (d)->bd_sbuf; \
405	(d)->bd_hlen = (d)->bd_slen; \
406	(d)->bd_sbuf = (d)->bd_fbuf; \
407	(d)->bd_slen = 0; \
408	(d)->bd_fbuf = NULL;
409/*
410 *  bpfread - read next chunk of packets from buffers
411 */
412static	int
413bpfread(dev, uio, ioflag)
414	struct cdev *dev;
415	struct uio *uio;
416	int ioflag;
417{
418	struct bpf_d *d = dev->si_drv1;
419	int timed_out;
420	int error;
421
422	/*
423	 * Restrict application to use a buffer the same size as
424	 * as kernel buffers.
425	 */
426	if (uio->uio_resid != d->bd_bufsize)
427		return (EINVAL);
428
429	BPFD_LOCK(d);
430	if (d->bd_state == BPF_WAITING)
431		callout_stop(&d->bd_callout);
432	timed_out = (d->bd_state == BPF_TIMED_OUT);
433	d->bd_state = BPF_IDLE;
434	/*
435	 * If the hold buffer is empty, then do a timed sleep, which
436	 * ends when the timeout expires or when enough packets
437	 * have arrived to fill the store buffer.
438	 */
439	while (d->bd_hbuf == NULL) {
440		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
441			/*
442			 * A packet(s) either arrived since the previous
443			 * read or arrived while we were asleep.
444			 * Rotate the buffers and return what's here.
445			 */
446			ROTATE_BUFFERS(d);
447			break;
448		}
449
450		/*
451		 * No data is available, check to see if the bpf device
452		 * is still pointed at a real interface.  If not, return
453		 * ENXIO so that the userland process knows to rebind
454		 * it before using it again.
455		 */
456		if (d->bd_bif == NULL) {
457			BPFD_UNLOCK(d);
458			return (ENXIO);
459		}
460
461		if (ioflag & IO_NDELAY) {
462			BPFD_UNLOCK(d);
463			return (EWOULDBLOCK);
464		}
465		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
466		     "bpf", d->bd_rtout);
467		if (error == EINTR || error == ERESTART) {
468			BPFD_UNLOCK(d);
469			return (error);
470		}
471		if (error == EWOULDBLOCK) {
472			/*
473			 * On a timeout, return what's in the buffer,
474			 * which may be nothing.  If there is something
475			 * in the store buffer, we can rotate the buffers.
476			 */
477			if (d->bd_hbuf)
478				/*
479				 * We filled up the buffer in between
480				 * getting the timeout and arriving
481				 * here, so we don't need to rotate.
482				 */
483				break;
484
485			if (d->bd_slen == 0) {
486				BPFD_UNLOCK(d);
487				return (0);
488			}
489			ROTATE_BUFFERS(d);
490			break;
491		}
492	}
493	/*
494	 * At this point, we know we have something in the hold slot.
495	 */
496	BPFD_UNLOCK(d);
497
498	/*
499	 * Move data from hold buffer into user space.
500	 * We know the entire buffer is transferred since
501	 * we checked above that the read buffer is bpf_bufsize bytes.
502	 */
503	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
504
505	BPFD_LOCK(d);
506	d->bd_fbuf = d->bd_hbuf;
507	d->bd_hbuf = NULL;
508	d->bd_hlen = 0;
509	BPFD_UNLOCK(d);
510
511	return (error);
512}
513
514
515/*
516 * If there are processes sleeping on this descriptor, wake them up.
517 */
518static __inline void
519bpf_wakeup(d)
520	struct bpf_d *d;
521{
522	if (d->bd_state == BPF_WAITING) {
523		callout_stop(&d->bd_callout);
524		d->bd_state = BPF_IDLE;
525	}
526	wakeup(d);
527	if (d->bd_async && d->bd_sig && d->bd_sigio)
528		pgsigio(&d->bd_sigio, d->bd_sig, 0);
529
530	selwakeuppri(&d->bd_sel, PRINET);
531	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
532}
533
534static void
535bpf_timed_out(arg)
536	void *arg;
537{
538	struct bpf_d *d = (struct bpf_d *)arg;
539
540	BPFD_LOCK(d);
541	if (d->bd_state == BPF_WAITING) {
542		d->bd_state = BPF_TIMED_OUT;
543		if (d->bd_slen != 0)
544			bpf_wakeup(d);
545	}
546	BPFD_UNLOCK(d);
547}
548
549static	int
550bpfwrite(dev, uio, ioflag)
551	struct cdev *dev;
552	struct uio *uio;
553	int ioflag;
554{
555	struct bpf_d *d = dev->si_drv1;
556	struct ifnet *ifp;
557	struct mbuf *m;
558	int error;
559	struct sockaddr dst;
560	int datlen;
561
562	if (d->bd_bif == NULL)
563		return (ENXIO);
564
565	ifp = d->bd_bif->bif_ifp;
566
567	if ((ifp->if_flags & IFF_UP) == 0)
568		return (ENETDOWN);
569
570	if (uio->uio_resid == 0)
571		return (0);
572
573	bzero(&dst, sizeof(dst));
574	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
575	if (error)
576		return (error);
577
578	if (datlen > ifp->if_mtu)
579		return (EMSGSIZE);
580
581	if (d->bd_hdrcmplt)
582		dst.sa_family = pseudo_AF_HDRCMPLT;
583
584#ifdef MAC
585	BPFD_LOCK(d);
586	mac_create_mbuf_from_bpfdesc(d, m);
587	BPFD_UNLOCK(d);
588#endif
589	NET_LOCK_GIANT();
590	error = (*ifp->if_output)(ifp, m, &dst, NULL);
591	NET_UNLOCK_GIANT();
592	/*
593	 * The driver frees the mbuf.
594	 */
595	return (error);
596}
597
598/*
599 * Reset a descriptor by flushing its packet buffer and clearing the
600 * receive and drop counts.
601 */
602static void
603reset_d(d)
604	struct bpf_d *d;
605{
606
607	mtx_assert(&d->bd_mtx, MA_OWNED);
608	if (d->bd_hbuf) {
609		/* Free the hold buffer. */
610		d->bd_fbuf = d->bd_hbuf;
611		d->bd_hbuf = NULL;
612	}
613	d->bd_slen = 0;
614	d->bd_hlen = 0;
615	d->bd_rcount = 0;
616	d->bd_dcount = 0;
617}
618
619/*
620 *  FIONREAD		Check for read packet available.
621 *  SIOCGIFADDR		Get interface address - convenient hook to driver.
622 *  BIOCGBLEN		Get buffer len [for read()].
623 *  BIOCSETF		Set ethernet read filter.
624 *  BIOCFLUSH		Flush read packet buffer.
625 *  BIOCPROMISC		Put interface into promiscuous mode.
626 *  BIOCGDLT		Get link layer type.
627 *  BIOCGETIF		Get interface name.
628 *  BIOCSETIF		Set interface.
629 *  BIOCSRTIMEOUT	Set read timeout.
630 *  BIOCGRTIMEOUT	Get read timeout.
631 *  BIOCGSTATS		Get packet stats.
632 *  BIOCIMMEDIATE	Set immediate mode.
633 *  BIOCVERSION		Get filter language version.
634 *  BIOCGHDRCMPLT	Get "header already complete" flag
635 *  BIOCSHDRCMPLT	Set "header already complete" flag
636 *  BIOCGSEESENT	Get "see packets sent" flag
637 *  BIOCSSEESENT	Set "see packets sent" flag
638 */
639/* ARGSUSED */
640static	int
641bpfioctl(dev, cmd, addr, flags, td)
642	struct cdev *dev;
643	u_long cmd;
644	caddr_t addr;
645	int flags;
646	struct thread *td;
647{
648	struct bpf_d *d = dev->si_drv1;
649	int error = 0;
650
651	BPFD_LOCK(d);
652	if (d->bd_state == BPF_WAITING)
653		callout_stop(&d->bd_callout);
654	d->bd_state = BPF_IDLE;
655	BPFD_UNLOCK(d);
656
657	switch (cmd) {
658
659	default:
660		error = EINVAL;
661		break;
662
663	/*
664	 * Check for read packet available.
665	 */
666	case FIONREAD:
667		{
668			int n;
669
670			BPFD_LOCK(d);
671			n = d->bd_slen;
672			if (d->bd_hbuf)
673				n += d->bd_hlen;
674			BPFD_UNLOCK(d);
675
676			*(int *)addr = n;
677			break;
678		}
679
680	case SIOCGIFADDR:
681		{
682			struct ifnet *ifp;
683
684			if (d->bd_bif == NULL)
685				error = EINVAL;
686			else {
687				ifp = d->bd_bif->bif_ifp;
688				error = (*ifp->if_ioctl)(ifp, cmd, addr);
689			}
690			break;
691		}
692
693	/*
694	 * Get buffer len [for read()].
695	 */
696	case BIOCGBLEN:
697		*(u_int *)addr = d->bd_bufsize;
698		break;
699
700	/*
701	 * Set buffer length.
702	 */
703	case BIOCSBLEN:
704		if (d->bd_bif != NULL)
705			error = EINVAL;
706		else {
707			u_int size = *(u_int *)addr;
708
709			if (size > bpf_maxbufsize)
710				*(u_int *)addr = size = bpf_maxbufsize;
711			else if (size < BPF_MINBUFSIZE)
712				*(u_int *)addr = size = BPF_MINBUFSIZE;
713			d->bd_bufsize = size;
714		}
715		break;
716
717	/*
718	 * Set link layer read filter.
719	 */
720	case BIOCSETF:
721		error = bpf_setf(d, (struct bpf_program *)addr);
722		break;
723
724	/*
725	 * Flush read packet buffer.
726	 */
727	case BIOCFLUSH:
728		BPFD_LOCK(d);
729		reset_d(d);
730		BPFD_UNLOCK(d);
731		break;
732
733	/*
734	 * Put interface into promiscuous mode.
735	 */
736	case BIOCPROMISC:
737		if (d->bd_bif == NULL) {
738			/*
739			 * No interface attached yet.
740			 */
741			error = EINVAL;
742			break;
743		}
744		if (d->bd_promisc == 0) {
745			mtx_lock(&Giant);
746			error = ifpromisc(d->bd_bif->bif_ifp, 1);
747			mtx_unlock(&Giant);
748			if (error == 0)
749				d->bd_promisc = 1;
750		}
751		break;
752
753	/*
754	 * Get current data link type.
755	 */
756	case BIOCGDLT:
757		if (d->bd_bif == NULL)
758			error = EINVAL;
759		else
760			*(u_int *)addr = d->bd_bif->bif_dlt;
761		break;
762
763	/*
764	 * Get a list of supported data link types.
765	 */
766	case BIOCGDLTLIST:
767		if (d->bd_bif == NULL)
768			error = EINVAL;
769		else
770			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
771		break;
772
773	/*
774	 * Set data link type.
775	 */
776	case BIOCSDLT:
777		if (d->bd_bif == NULL)
778			error = EINVAL;
779		else
780			error = bpf_setdlt(d, *(u_int *)addr);
781		break;
782
783	/*
784	 * Get interface name.
785	 */
786	case BIOCGETIF:
787		if (d->bd_bif == NULL)
788			error = EINVAL;
789		else {
790			struct ifnet *const ifp = d->bd_bif->bif_ifp;
791			struct ifreq *const ifr = (struct ifreq *)addr;
792
793			strlcpy(ifr->ifr_name, ifp->if_xname,
794			    sizeof(ifr->ifr_name));
795		}
796		break;
797
798	/*
799	 * Set interface.
800	 */
801	case BIOCSETIF:
802		error = bpf_setif(d, (struct ifreq *)addr);
803		break;
804
805	/*
806	 * Set read timeout.
807	 */
808	case BIOCSRTIMEOUT:
809		{
810			struct timeval *tv = (struct timeval *)addr;
811
812			/*
813			 * Subtract 1 tick from tvtohz() since this isn't
814			 * a one-shot timer.
815			 */
816			if ((error = itimerfix(tv)) == 0)
817				d->bd_rtout = tvtohz(tv) - 1;
818			break;
819		}
820
821	/*
822	 * Get read timeout.
823	 */
824	case BIOCGRTIMEOUT:
825		{
826			struct timeval *tv = (struct timeval *)addr;
827
828			tv->tv_sec = d->bd_rtout / hz;
829			tv->tv_usec = (d->bd_rtout % hz) * tick;
830			break;
831		}
832
833	/*
834	 * Get packet stats.
835	 */
836	case BIOCGSTATS:
837		{
838			struct bpf_stat *bs = (struct bpf_stat *)addr;
839
840			bs->bs_recv = d->bd_rcount;
841			bs->bs_drop = d->bd_dcount;
842			break;
843		}
844
845	/*
846	 * Set immediate mode.
847	 */
848	case BIOCIMMEDIATE:
849		d->bd_immediate = *(u_int *)addr;
850		break;
851
852	case BIOCVERSION:
853		{
854			struct bpf_version *bv = (struct bpf_version *)addr;
855
856			bv->bv_major = BPF_MAJOR_VERSION;
857			bv->bv_minor = BPF_MINOR_VERSION;
858			break;
859		}
860
861	/*
862	 * Get "header already complete" flag
863	 */
864	case BIOCGHDRCMPLT:
865		*(u_int *)addr = d->bd_hdrcmplt;
866		break;
867
868	/*
869	 * Set "header already complete" flag
870	 */
871	case BIOCSHDRCMPLT:
872		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
873		break;
874
875	/*
876	 * Get "see sent packets" flag
877	 */
878	case BIOCGSEESENT:
879		*(u_int *)addr = d->bd_seesent;
880		break;
881
882	/*
883	 * Set "see sent packets" flag
884	 */
885	case BIOCSSEESENT:
886		d->bd_seesent = *(u_int *)addr;
887		break;
888
889	case FIONBIO:		/* Non-blocking I/O */
890		break;
891
892	case FIOASYNC:		/* Send signal on receive packets */
893		d->bd_async = *(int *)addr;
894		break;
895
896	case FIOSETOWN:
897		error = fsetown(*(int *)addr, &d->bd_sigio);
898		break;
899
900	case FIOGETOWN:
901		*(int *)addr = fgetown(&d->bd_sigio);
902		break;
903
904	/* This is deprecated, FIOSETOWN should be used instead. */
905	case TIOCSPGRP:
906		error = fsetown(-(*(int *)addr), &d->bd_sigio);
907		break;
908
909	/* This is deprecated, FIOGETOWN should be used instead. */
910	case TIOCGPGRP:
911		*(int *)addr = -fgetown(&d->bd_sigio);
912		break;
913
914	case BIOCSRSIG:		/* Set receive signal */
915		{
916			u_int sig;
917
918			sig = *(u_int *)addr;
919
920			if (sig >= NSIG)
921				error = EINVAL;
922			else
923				d->bd_sig = sig;
924			break;
925		}
926	case BIOCGRSIG:
927		*(u_int *)addr = d->bd_sig;
928		break;
929	}
930	return (error);
931}
932
933/*
934 * Set d's packet filter program to fp.  If this file already has a filter,
935 * free it and replace it.  Returns EINVAL for bogus requests.
936 */
937static int
938bpf_setf(d, fp)
939	struct bpf_d *d;
940	struct bpf_program *fp;
941{
942	struct bpf_insn *fcode, *old;
943	u_int flen, size;
944
945	old = d->bd_filter;
946	if (fp->bf_insns == NULL) {
947		if (fp->bf_len != 0)
948			return (EINVAL);
949		BPFD_LOCK(d);
950		d->bd_filter = NULL;
951		reset_d(d);
952		BPFD_UNLOCK(d);
953		if (old != NULL)
954			free((caddr_t)old, M_BPF);
955		return (0);
956	}
957	flen = fp->bf_len;
958	if (flen > BPF_MAXINSNS)
959		return (EINVAL);
960
961	size = flen * sizeof(*fp->bf_insns);
962	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
963	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
964	    bpf_validate(fcode, (int)flen)) {
965		BPFD_LOCK(d);
966		d->bd_filter = fcode;
967		reset_d(d);
968		BPFD_UNLOCK(d);
969		if (old != NULL)
970			free((caddr_t)old, M_BPF);
971
972		return (0);
973	}
974	free((caddr_t)fcode, M_BPF);
975	return (EINVAL);
976}
977
978/*
979 * Detach a file from its current interface (if attached at all) and attach
980 * to the interface indicated by the name stored in ifr.
981 * Return an errno or 0.
982 */
983static int
984bpf_setif(d, ifr)
985	struct bpf_d *d;
986	struct ifreq *ifr;
987{
988	struct bpf_if *bp;
989	int error;
990	struct ifnet *theywant;
991
992	theywant = ifunit(ifr->ifr_name);
993	if (theywant == NULL)
994		return ENXIO;
995
996	/*
997	 * Look through attached interfaces for the named one.
998	 */
999	mtx_lock(&bpf_mtx);
1000	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1001		struct ifnet *ifp = bp->bif_ifp;
1002
1003		if (ifp == NULL || ifp != theywant)
1004			continue;
1005		/* skip additional entry */
1006		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
1007			continue;
1008
1009		mtx_unlock(&bpf_mtx);
1010		/*
1011		 * We found the requested interface.
1012		 * Allocate the packet buffers if we need to.
1013		 * If we're already attached to requested interface,
1014		 * just flush the buffer.
1015		 */
1016		if (d->bd_sbuf == NULL) {
1017			error = bpf_allocbufs(d);
1018			if (error != 0)
1019				return (error);
1020		}
1021		if (bp != d->bd_bif) {
1022			if (d->bd_bif)
1023				/*
1024				 * Detach if attached to something else.
1025				 */
1026				bpf_detachd(d);
1027
1028			bpf_attachd(d, bp);
1029		}
1030		BPFD_LOCK(d);
1031		reset_d(d);
1032		BPFD_UNLOCK(d);
1033		return (0);
1034	}
1035	mtx_unlock(&bpf_mtx);
1036	/* Not found. */
1037	return (ENXIO);
1038}
1039
1040/*
1041 * Support for select() and poll() system calls
1042 *
1043 * Return true iff the specific operation will not block indefinitely.
1044 * Otherwise, return false but make a note that a selwakeup() must be done.
1045 */
1046static int
1047bpfpoll(dev, events, td)
1048	struct cdev *dev;
1049	int events;
1050	struct thread *td;
1051{
1052	struct bpf_d *d;
1053	int revents;
1054
1055	d = dev->si_drv1;
1056	if (d->bd_bif == NULL)
1057		return (ENXIO);
1058
1059	revents = events & (POLLOUT | POLLWRNORM);
1060	BPFD_LOCK(d);
1061	if (events & (POLLIN | POLLRDNORM)) {
1062		if (bpf_ready(d))
1063			revents |= events & (POLLIN | POLLRDNORM);
1064		else {
1065			selrecord(td, &d->bd_sel);
1066			/* Start the read timeout if necessary. */
1067			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1068				callout_reset(&d->bd_callout, d->bd_rtout,
1069				    bpf_timed_out, d);
1070				d->bd_state = BPF_WAITING;
1071			}
1072		}
1073	}
1074	BPFD_UNLOCK(d);
1075	return (revents);
1076}
1077
1078/*
1079 * Support for kevent() system call.  Register EVFILT_READ filters and
1080 * reject all others.
1081 */
1082int
1083bpfkqfilter(dev, kn)
1084	struct cdev *dev;
1085	struct knote *kn;
1086{
1087	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1088
1089	if (kn->kn_filter != EVFILT_READ)
1090		return (1);
1091
1092	kn->kn_fop = &bpfread_filtops;
1093	kn->kn_hook = d;
1094	knlist_add(&d->bd_sel.si_note, kn, 0);
1095
1096	return (0);
1097}
1098
1099static void
1100filt_bpfdetach(kn)
1101	struct knote *kn;
1102{
1103	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1104
1105	knlist_remove(&d->bd_sel.si_note, kn, 0);
1106}
1107
1108static int
1109filt_bpfread(kn, hint)
1110	struct knote *kn;
1111	long hint;
1112{
1113	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1114	int ready;
1115
1116	BPFD_LOCK_ASSERT(d);
1117	ready = bpf_ready(d);
1118	if (ready) {
1119		kn->kn_data = d->bd_slen;
1120		if (d->bd_hbuf)
1121			kn->kn_data += d->bd_hlen;
1122	}
1123	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1124		callout_reset(&d->bd_callout, d->bd_rtout,
1125		    bpf_timed_out, d);
1126		d->bd_state = BPF_WAITING;
1127	}
1128
1129	return (ready);
1130}
1131
1132/*
1133 * Incoming linkage from device drivers.  Process the packet pkt, of length
1134 * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1135 * by each process' filter, and if accepted, stashed into the corresponding
1136 * buffer.
1137 */
1138void
1139bpf_tap(bp, pkt, pktlen)
1140	struct bpf_if *bp;
1141	u_char *pkt;
1142	u_int pktlen;
1143{
1144	struct bpf_d *d;
1145	u_int slen;
1146
1147	/*
1148	 * Lockless read to avoid cost of locking the interface if there are
1149	 * no descriptors attached.
1150	 */
1151	if (LIST_EMPTY(&bp->bif_dlist))
1152		return;
1153
1154	BPFIF_LOCK(bp);
1155	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1156		BPFD_LOCK(d);
1157		++d->bd_rcount;
1158		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1159		if (slen != 0) {
1160#ifdef MAC
1161			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1162#endif
1163				catchpacket(d, pkt, pktlen, slen, bcopy);
1164		}
1165		BPFD_UNLOCK(d);
1166	}
1167	BPFIF_UNLOCK(bp);
1168}
1169
1170/*
1171 * Copy data from an mbuf chain into a buffer.  This code is derived
1172 * from m_copydata in sys/uipc_mbuf.c.
1173 */
1174static void
1175bpf_mcopy(src_arg, dst_arg, len)
1176	const void *src_arg;
1177	void *dst_arg;
1178	size_t len;
1179{
1180	const struct mbuf *m;
1181	u_int count;
1182	u_char *dst;
1183
1184	m = src_arg;
1185	dst = dst_arg;
1186	while (len > 0) {
1187		if (m == NULL)
1188			panic("bpf_mcopy");
1189		count = min(m->m_len, len);
1190		bcopy(mtod(m, void *), dst, count);
1191		m = m->m_next;
1192		dst += count;
1193		len -= count;
1194	}
1195}
1196
1197/*
1198 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1199 */
1200void
1201bpf_mtap(bp, m)
1202	struct bpf_if *bp;
1203	struct mbuf *m;
1204{
1205	struct bpf_d *d;
1206	u_int pktlen, slen;
1207
1208	/*
1209	 * Lockless read to avoid cost of locking the interface if there are
1210	 * no descriptors attached.
1211	 */
1212	if (LIST_EMPTY(&bp->bif_dlist))
1213		return;
1214
1215	pktlen = m_length(m, NULL);
1216	if (pktlen == m->m_len) {
1217		bpf_tap(bp, mtod(m, u_char *), pktlen);
1218		return;
1219	}
1220
1221	BPFIF_LOCK(bp);
1222	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1223		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1224			continue;
1225		BPFD_LOCK(d);
1226		++d->bd_rcount;
1227		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1228		if (slen != 0)
1229#ifdef MAC
1230			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1231#endif
1232				catchpacket(d, (u_char *)m, pktlen, slen,
1233				    bpf_mcopy);
1234		BPFD_UNLOCK(d);
1235	}
1236	BPFIF_UNLOCK(bp);
1237}
1238
1239/*
1240 * Incoming linkage from device drivers, when packet is in
1241 * an mbuf chain and to be prepended by a contiguous header.
1242 */
1243void
1244bpf_mtap2(bp, data, dlen, m)
1245	struct bpf_if *bp;
1246	void *data;
1247	u_int dlen;
1248	struct mbuf *m;
1249{
1250	struct mbuf mb;
1251	struct bpf_d *d;
1252	u_int pktlen, slen;
1253
1254	/*
1255	 * Lockless read to avoid cost of locking the interface if there are
1256	 * no descriptors attached.
1257	 */
1258	if (LIST_EMPTY(&bp->bif_dlist))
1259		return;
1260
1261	pktlen = m_length(m, NULL);
1262	/*
1263	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1264	 * Note that we cut corners here; we only setup what's
1265	 * absolutely needed--this mbuf should never go anywhere else.
1266	 */
1267	mb.m_next = m;
1268	mb.m_data = data;
1269	mb.m_len = dlen;
1270	pktlen += dlen;
1271
1272	BPFIF_LOCK(bp);
1273	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1274		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1275			continue;
1276		BPFD_LOCK(d);
1277		++d->bd_rcount;
1278		slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1279		if (slen != 0)
1280#ifdef MAC
1281			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1282#endif
1283				catchpacket(d, (u_char *)&mb, pktlen, slen,
1284				    bpf_mcopy);
1285		BPFD_UNLOCK(d);
1286	}
1287	BPFIF_UNLOCK(bp);
1288}
1289
1290/*
1291 * Move the packet data from interface memory (pkt) into the
1292 * store buffer.  "cpfn" is the routine called to do the actual data
1293 * transfer.  bcopy is passed in to copy contiguous chunks, while
1294 * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1295 * pkt is really an mbuf.
1296 */
1297static void
1298catchpacket(d, pkt, pktlen, snaplen, cpfn)
1299	struct bpf_d *d;
1300	u_char *pkt;
1301	u_int pktlen, snaplen;
1302	void (*cpfn)(const void *, void *, size_t);
1303{
1304	struct bpf_hdr *hp;
1305	int totlen, curlen;
1306	int hdrlen = d->bd_bif->bif_hdrlen;
1307
1308	/*
1309	 * Figure out how many bytes to move.  If the packet is
1310	 * greater or equal to the snapshot length, transfer that
1311	 * much.  Otherwise, transfer the whole packet (unless
1312	 * we hit the buffer size limit).
1313	 */
1314	totlen = hdrlen + min(snaplen, pktlen);
1315	if (totlen > d->bd_bufsize)
1316		totlen = d->bd_bufsize;
1317
1318	/*
1319	 * Round up the end of the previous packet to the next longword.
1320	 */
1321	curlen = BPF_WORDALIGN(d->bd_slen);
1322	if (curlen + totlen > d->bd_bufsize) {
1323		/*
1324		 * This packet will overflow the storage buffer.
1325		 * Rotate the buffers if we can, then wakeup any
1326		 * pending reads.
1327		 */
1328		if (d->bd_fbuf == NULL) {
1329			/*
1330			 * We haven't completed the previous read yet,
1331			 * so drop the packet.
1332			 */
1333			++d->bd_dcount;
1334			return;
1335		}
1336		ROTATE_BUFFERS(d);
1337		bpf_wakeup(d);
1338		curlen = 0;
1339	}
1340	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1341		/*
1342		 * Immediate mode is set, or the read timeout has
1343		 * already expired during a select call.  A packet
1344		 * arrived, so the reader should be woken up.
1345		 */
1346		bpf_wakeup(d);
1347
1348	/*
1349	 * Append the bpf header.
1350	 */
1351	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1352	microtime(&hp->bh_tstamp);
1353	hp->bh_datalen = pktlen;
1354	hp->bh_hdrlen = hdrlen;
1355	/*
1356	 * Copy the packet data into the store buffer and update its length.
1357	 */
1358	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1359	d->bd_slen = curlen + totlen;
1360}
1361
1362/*
1363 * Initialize all nonzero fields of a descriptor.
1364 */
1365static int
1366bpf_allocbufs(d)
1367	struct bpf_d *d;
1368{
1369	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1370	if (d->bd_fbuf == NULL)
1371		return (ENOBUFS);
1372
1373	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1374	if (d->bd_sbuf == NULL) {
1375		free(d->bd_fbuf, M_BPF);
1376		return (ENOBUFS);
1377	}
1378	d->bd_slen = 0;
1379	d->bd_hlen = 0;
1380	return (0);
1381}
1382
1383/*
1384 * Free buffers currently in use by a descriptor.
1385 * Called on close.
1386 */
1387static void
1388bpf_freed(d)
1389	struct bpf_d *d;
1390{
1391	/*
1392	 * We don't need to lock out interrupts since this descriptor has
1393	 * been detached from its interface and it yet hasn't been marked
1394	 * free.
1395	 */
1396	if (d->bd_sbuf != NULL) {
1397		free(d->bd_sbuf, M_BPF);
1398		if (d->bd_hbuf != NULL)
1399			free(d->bd_hbuf, M_BPF);
1400		if (d->bd_fbuf != NULL)
1401			free(d->bd_fbuf, M_BPF);
1402	}
1403	if (d->bd_filter)
1404		free((caddr_t)d->bd_filter, M_BPF);
1405	mtx_destroy(&d->bd_mtx);
1406}
1407
1408/*
1409 * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
1410 * fixed size of the link header (variable length headers not yet supported).
1411 */
1412void
1413bpfattach(ifp, dlt, hdrlen)
1414	struct ifnet *ifp;
1415	u_int dlt, hdrlen;
1416{
1417
1418	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1419}
1420
1421/*
1422 * Attach an interface to bpf.  ifp is a pointer to the structure
1423 * defining the interface to be attached, dlt is the link layer type,
1424 * and hdrlen is the fixed size of the link header (variable length
1425 * headers are not yet supporrted).
1426 */
1427void
1428bpfattach2(ifp, dlt, hdrlen, driverp)
1429	struct ifnet *ifp;
1430	u_int dlt, hdrlen;
1431	struct bpf_if **driverp;
1432{
1433	struct bpf_if *bp;
1434	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1435	if (bp == NULL)
1436		panic("bpfattach");
1437
1438	LIST_INIT(&bp->bif_dlist);
1439	bp->bif_driverp = driverp;
1440	bp->bif_ifp = ifp;
1441	bp->bif_dlt = dlt;
1442	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1443
1444	mtx_lock(&bpf_mtx);
1445	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1446	mtx_unlock(&bpf_mtx);
1447
1448	*bp->bif_driverp = NULL;
1449
1450	/*
1451	 * Compute the length of the bpf header.  This is not necessarily
1452	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1453	 * that the network layer header begins on a longword boundary (for
1454	 * performance reasons and to alleviate alignment restrictions).
1455	 */
1456	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1457
1458	if (bootverbose)
1459		if_printf(ifp, "bpf attached\n");
1460}
1461
1462/*
1463 * Detach bpf from an interface.  This involves detaching each descriptor
1464 * associated with the interface, and leaving bd_bif NULL.  Notify each
1465 * descriptor as it's detached so that any sleepers wake up and get
1466 * ENXIO.
1467 */
1468void
1469bpfdetach(ifp)
1470	struct ifnet *ifp;
1471{
1472	struct bpf_if	*bp;
1473	struct bpf_d	*d;
1474
1475	/* Locate BPF interface information */
1476	mtx_lock(&bpf_mtx);
1477	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1478		if (ifp == bp->bif_ifp)
1479			break;
1480	}
1481
1482	/* Interface wasn't attached */
1483	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1484		mtx_unlock(&bpf_mtx);
1485		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1486		return;
1487	}
1488
1489	LIST_REMOVE(bp, bif_next);
1490	mtx_unlock(&bpf_mtx);
1491
1492	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1493		bpf_detachd(d);
1494		BPFD_LOCK(d);
1495		bpf_wakeup(d);
1496		BPFD_UNLOCK(d);
1497	}
1498
1499	mtx_destroy(&bp->bif_mtx);
1500	free(bp, M_BPF);
1501}
1502
1503/*
1504 * Get a list of available data link type of the interface.
1505 */
1506static int
1507bpf_getdltlist(d, bfl)
1508	struct bpf_d *d;
1509	struct bpf_dltlist *bfl;
1510{
1511	int n, error;
1512	struct ifnet *ifp;
1513	struct bpf_if *bp;
1514
1515	ifp = d->bd_bif->bif_ifp;
1516	n = 0;
1517	error = 0;
1518	mtx_lock(&bpf_mtx);
1519	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1520		if (bp->bif_ifp != ifp)
1521			continue;
1522		if (bfl->bfl_list != NULL) {
1523			if (n >= bfl->bfl_len) {
1524				mtx_unlock(&bpf_mtx);
1525				return (ENOMEM);
1526			}
1527			error = copyout(&bp->bif_dlt,
1528			    bfl->bfl_list + n, sizeof(u_int));
1529		}
1530		n++;
1531	}
1532	mtx_unlock(&bpf_mtx);
1533	bfl->bfl_len = n;
1534	return (error);
1535}
1536
1537/*
1538 * Set the data link type of a BPF instance.
1539 */
1540static int
1541bpf_setdlt(d, dlt)
1542	struct bpf_d *d;
1543	u_int dlt;
1544{
1545	int error, opromisc;
1546	struct ifnet *ifp;
1547	struct bpf_if *bp;
1548
1549	if (d->bd_bif->bif_dlt == dlt)
1550		return (0);
1551	ifp = d->bd_bif->bif_ifp;
1552	mtx_lock(&bpf_mtx);
1553	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1554		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1555			break;
1556	}
1557	mtx_unlock(&bpf_mtx);
1558	if (bp != NULL) {
1559		opromisc = d->bd_promisc;
1560		bpf_detachd(d);
1561		bpf_attachd(d, bp);
1562		BPFD_LOCK(d);
1563		reset_d(d);
1564		BPFD_UNLOCK(d);
1565		if (opromisc) {
1566			error = ifpromisc(bp->bif_ifp, 1);
1567			if (error)
1568				if_printf(bp->bif_ifp,
1569					"bpf_setdlt: ifpromisc failed (%d)\n",
1570					error);
1571			else
1572				d->bd_promisc = 1;
1573		}
1574	}
1575	return (bp == NULL ? EINVAL : 0);
1576}
1577
1578static void bpf_drvinit(void *unused);
1579
1580static void bpf_clone(void *arg, char *name, int namelen, struct cdev **dev);
1581
1582static void
1583bpf_clone(arg, name, namelen, dev)
1584	void *arg;
1585	char *name;
1586	int namelen;
1587	struct cdev **dev;
1588{
1589	int u;
1590
1591	if (*dev != NULL)
1592		return;
1593	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1594		return;
1595	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1596	    "bpf%d", u);
1597	(*dev)->si_flags |= SI_CHEAPCLONE;
1598	return;
1599}
1600
1601static void
1602bpf_drvinit(unused)
1603	void *unused;
1604{
1605
1606	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1607	LIST_INIT(&bpf_iflist);
1608	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1609}
1610
1611SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
1612
1613#else /* !DEV_BPF && !NETGRAPH_BPF */
1614/*
1615 * NOP stubs to allow bpf-using drivers to load and function.
1616 *
1617 * A 'better' implementation would allow the core bpf functionality
1618 * to be loaded at runtime.
1619 */
1620
1621void
1622bpf_tap(bp, pkt, pktlen)
1623	struct bpf_if *bp;
1624	u_char *pkt;
1625	u_int pktlen;
1626{
1627}
1628
1629void
1630bpf_mtap(bp, m)
1631	struct bpf_if *bp;
1632	struct mbuf *m;
1633{
1634}
1635
1636void
1637bpf_mtap2(bp, d, l, m)
1638	struct bpf_if *bp;
1639	void *d;
1640	u_int l;
1641	struct mbuf *m;
1642{
1643}
1644
1645void
1646bpfattach(ifp, dlt, hdrlen)
1647	struct ifnet *ifp;
1648	u_int dlt, hdrlen;
1649{
1650}
1651
1652void
1653bpfattach2(ifp, dlt, hdrlen, driverp)
1654	struct ifnet *ifp;
1655	u_int dlt, hdrlen;
1656	struct bpf_if **driverp;
1657{
1658}
1659
1660void
1661bpfdetach(ifp)
1662	struct ifnet *ifp;
1663{
1664}
1665
1666u_int
1667bpf_filter(pc, p, wirelen, buflen)
1668	const struct bpf_insn *pc;
1669	u_char *p;
1670	u_int wirelen;
1671	u_int buflen;
1672{
1673	return -1;	/* "no filter" behaviour */
1674}
1675
1676int
1677bpf_validate(f, len)
1678	const struct bpf_insn *f;
1679	int len;
1680{
1681	return 0;		/* false */
1682}
1683
1684#endif /* !DEV_BPF && !NETGRAPH_BPF */
1685