bpf.c revision 66067
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 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *      @(#)bpf.c	8.2 (Berkeley) 3/28/94
39 *
40 * $FreeBSD: head/sys/net/bpf.c 66067 2000-09-19 10:28:44Z phk $
41 */
42
43#include "bpf.h"
44
45#ifndef __GNUC__
46#define inline
47#else
48#define inline __inline
49#endif
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/conf.h>
54#include <sys/malloc.h>
55#include <sys/mbuf.h>
56#include <sys/time.h>
57#include <sys/proc.h>
58#include <sys/signalvar.h>
59#include <sys/filio.h>
60#include <sys/sockio.h>
61#include <sys/ttycom.h>
62#include <sys/filedesc.h>
63
64#if defined(sparc) && BSD < 199103
65#include <sys/stream.h>
66#endif
67#include <sys/poll.h>
68
69#include <sys/socket.h>
70#include <sys/vnode.h>
71
72#include <net/if.h>
73#include <net/bpf.h>
74#include <net/bpfdesc.h>
75
76#include <netinet/in.h>
77#include <netinet/if_ether.h>
78#include <sys/kernel.h>
79#include <sys/sysctl.h>
80
81MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
82
83#if NBPF > 0
84
85/*
86 * Older BSDs don't have kernel malloc.
87 */
88#if BSD < 199103
89extern bcopy();
90static caddr_t bpf_alloc();
91#include <net/bpf_compat.h>
92#define BPF_BUFSIZE (MCLBYTES-8)
93#define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
94#else
95#define BPF_BUFSIZE 4096
96#define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
97#endif
98
99#define PRINET  26			/* interruptible */
100
101/*
102 * The default read buffer size is patchable.
103 */
104static int bpf_bufsize = BPF_BUFSIZE;
105SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
106	&bpf_bufsize, 0, "");
107static int bpf_maxbufsize = BPF_MAXBUFSIZE;
108SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
109	&bpf_maxbufsize, 0, "");
110
111/*
112 *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
113 */
114static struct bpf_if	*bpf_iflist;
115
116static int	bpf_allocbufs __P((struct bpf_d *));
117static void	bpf_attachd __P((struct bpf_d *d, struct bpf_if *bp));
118static void	bpf_detachd __P((struct bpf_d *d));
119static void	bpf_freed __P((struct bpf_d *));
120static void	bpf_mcopy __P((const void *, void *, size_t));
121static int	bpf_movein __P((struct uio *, int,
122		    struct mbuf **, struct sockaddr *, int *));
123static int	bpf_setif __P((struct bpf_d *, struct ifreq *));
124static inline void
125		bpf_wakeup __P((struct bpf_d *));
126static void	catchpacket __P((struct bpf_d *, u_char *, u_int,
127		    u_int, void (*)(const void *, void *, size_t)));
128static void	reset_d __P((struct bpf_d *));
129static int	 bpf_setf __P((struct bpf_d *, struct bpf_program *));
130
131static	d_open_t	bpfopen;
132static	d_close_t	bpfclose;
133static	d_read_t	bpfread;
134static	d_write_t	bpfwrite;
135static	d_ioctl_t	bpfioctl;
136static	d_poll_t	bpfpoll;
137
138#define CDEV_MAJOR 23
139static struct cdevsw bpf_cdevsw = {
140	/* open */	bpfopen,
141	/* close */	bpfclose,
142	/* read */	bpfread,
143	/* write */	bpfwrite,
144	/* ioctl */	bpfioctl,
145	/* poll */	bpfpoll,
146	/* mmap */	nommap,
147	/* strategy */	nostrategy,
148	/* name */	"bpf",
149	/* maj */	CDEV_MAJOR,
150	/* dump */	nodump,
151	/* psize */	nopsize,
152	/* flags */	0,
153	/* bmaj */	-1
154};
155
156
157static int
158bpf_movein(uio, linktype, mp, sockp, datlen)
159	register struct uio *uio;
160	int linktype, *datlen;
161	register struct mbuf **mp;
162	register struct sockaddr *sockp;
163{
164	struct mbuf *m;
165	int error;
166	int len;
167	int hlen;
168
169	/*
170	 * Build a sockaddr based on the data link layer type.
171	 * We do this at this level because the ethernet header
172	 * is copied directly into the data field of the sockaddr.
173	 * In the case of SLIP, there is no header and the packet
174	 * is forwarded as is.
175	 * Also, we are careful to leave room at the front of the mbuf
176	 * for the link level header.
177	 */
178	switch (linktype) {
179
180	case DLT_SLIP:
181		sockp->sa_family = AF_INET;
182		hlen = 0;
183		break;
184
185	case DLT_EN10MB:
186		sockp->sa_family = AF_UNSPEC;
187		/* XXX Would MAXLINKHDR be better? */
188		hlen = sizeof(struct ether_header);
189		break;
190
191	case DLT_FDDI:
192#if defined(__FreeBSD__) || defined(__bsdi__)
193		sockp->sa_family = AF_IMPLINK;
194		hlen = 0;
195#else
196		sockp->sa_family = AF_UNSPEC;
197		/* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
198		hlen = 24;
199#endif
200		break;
201
202	case DLT_RAW:
203	case DLT_NULL:
204		sockp->sa_family = AF_UNSPEC;
205		hlen = 0;
206		break;
207
208#ifdef __FreeBSD__
209	case DLT_ATM_RFC1483:
210		/*
211		 * en atm driver requires 4-byte atm pseudo header.
212		 * though it isn't standard, vpi:vci needs to be
213		 * specified anyway.
214		 */
215		sockp->sa_family = AF_UNSPEC;
216		hlen = 12; 	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
217		break;
218#endif
219
220	case DLT_PPP:
221		sockp->sa_family = AF_UNSPEC;
222		hlen = 4;	/* This should match PPP_HDRLEN */
223		break;
224
225	default:
226		return (EIO);
227	}
228
229	len = uio->uio_resid;
230	*datlen = len - hlen;
231	if ((unsigned)len > MCLBYTES)
232		return (EIO);
233
234	MGETHDR(m, M_WAIT, MT_DATA);
235	if (m == 0)
236		return (ENOBUFS);
237	if (len > MHLEN) {
238#if BSD >= 199103
239		MCLGET(m, M_WAIT);
240		if ((m->m_flags & M_EXT) == 0) {
241#else
242		MCLGET(m);
243		if (m->m_len != MCLBYTES) {
244#endif
245			error = ENOBUFS;
246			goto bad;
247		}
248	}
249	m->m_pkthdr.len = m->m_len = len;
250	m->m_pkthdr.rcvif = NULL;
251	*mp = m;
252	/*
253	 * Make room for link header.
254	 */
255	if (hlen != 0) {
256		m->m_pkthdr.len -= hlen;
257		m->m_len -= hlen;
258#if BSD >= 199103
259		m->m_data += hlen; /* XXX */
260#else
261		m->m_off += hlen;
262#endif
263		error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio);
264		if (error)
265			goto bad;
266	}
267	error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio);
268	if (!error)
269		return (0);
270 bad:
271	m_freem(m);
272	return (error);
273}
274
275/*
276 * Attach file to the bpf interface, i.e. make d listen on bp.
277 * Must be called at splimp.
278 */
279static void
280bpf_attachd(d, bp)
281	struct bpf_d *d;
282	struct bpf_if *bp;
283{
284	/*
285	 * Point d at bp, and add d to the interface's list of listeners.
286	 * Finally, point the driver's bpf cookie at the interface so
287	 * it will divert packets to bpf.
288	 */
289	d->bd_bif = bp;
290	d->bd_next = bp->bif_dlist;
291	bp->bif_dlist = d;
292
293	bp->bif_ifp->if_bpf = bp;
294}
295
296/*
297 * Detach a file from its interface.
298 */
299static void
300bpf_detachd(d)
301	struct bpf_d *d;
302{
303	int error;
304	struct bpf_d **p;
305	struct bpf_if *bp;
306
307	bp = d->bd_bif;
308	/*
309	 * Check if this descriptor had requested promiscuous mode.
310	 * If so, turn it off.
311	 */
312	if (d->bd_promisc) {
313		d->bd_promisc = 0;
314		error = ifpromisc(bp->bif_ifp, 0);
315		if (error != 0 && error != ENXIO) {
316			/*
317			 * ENXIO can happen if a pccard is unplugged
318			 * Something is really wrong if we were able to put
319			 * the driver into promiscuous mode, but can't
320			 * take it out.
321			 */
322			printf("%s%d: ifpromisc failed %d\n",
323			    bp->bif_ifp->if_name, bp->bif_ifp->if_unit, error);
324		}
325	}
326	/* Remove d from the interface's descriptor list. */
327	p = &bp->bif_dlist;
328	while (*p != d) {
329		p = &(*p)->bd_next;
330		if (*p == 0)
331			panic("bpf_detachd: descriptor not in list");
332	}
333	*p = (*p)->bd_next;
334	if (bp->bif_dlist == 0)
335		/*
336		 * Let the driver know that there are no more listeners.
337		 */
338		d->bd_bif->bif_ifp->if_bpf = 0;
339	d->bd_bif = 0;
340}
341
342/*
343 * Open ethernet device.  Returns ENXIO for illegal minor device number,
344 * EBUSY if file is open by another process.
345 */
346/* ARGSUSED */
347static	int
348bpfopen(dev, flags, fmt, p)
349	dev_t dev;
350	int flags;
351	int fmt;
352	struct proc *p;
353{
354	register struct bpf_d *d;
355
356	if (p->p_prison)
357		return (EPERM);
358
359	d = dev->si_drv1;
360	/*
361	 * Each minor can be opened by only one process.  If the requested
362	 * minor is in use, return EBUSY.
363	 */
364	if (d)
365		return (EBUSY);
366	make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
367	    "bpf%d", dev2unit(dev));
368	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK);
369	bzero(d, sizeof(*d));
370	dev->si_drv1 = d;
371	d->bd_bufsize = bpf_bufsize;
372	d->bd_sig = SIGIO;
373	d->bd_seesent = 1;
374
375	return (0);
376}
377
378/*
379 * Close the descriptor by detaching it from its interface,
380 * deallocating its buffers, and marking it free.
381 */
382/* ARGSUSED */
383static	int
384bpfclose(dev, flags, fmt, p)
385	dev_t dev;
386	int flags;
387	int fmt;
388	struct proc *p;
389{
390	register struct bpf_d *d = dev->si_drv1;
391	register int s;
392
393	funsetown(d->bd_sigio);
394	s = splimp();
395	if (d->bd_bif)
396		bpf_detachd(d);
397	splx(s);
398	bpf_freed(d);
399	dev->si_drv1 = 0;
400	FREE(d, M_BPF);
401
402	return (0);
403}
404
405/*
406 * Support for SunOS, which does not have tsleep.
407 */
408#if BSD < 199103
409static
410bpf_timeout(arg)
411	caddr_t arg;
412{
413	struct bpf_d *d = (struct bpf_d *)arg;
414	d->bd_timedout = 1;
415	wakeup(arg);
416}
417
418#define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
419
420int
421bpf_sleep(d)
422	register struct bpf_d *d;
423{
424	register int rto = d->bd_rtout;
425	register int st;
426
427	if (rto != 0) {
428		d->bd_timedout = 0;
429		timeout(bpf_timeout, (caddr_t)d, rto);
430	}
431	st = sleep((caddr_t)d, PRINET|PCATCH);
432	if (rto != 0) {
433		if (d->bd_timedout == 0)
434			untimeout(bpf_timeout, (caddr_t)d);
435		else if (st == 0)
436			return EWOULDBLOCK;
437	}
438	return (st != 0) ? EINTR : 0;
439}
440#else
441#define BPF_SLEEP tsleep
442#endif
443
444/*
445 * Rotate the packet buffers in descriptor d.  Move the store buffer
446 * into the hold slot, and the free buffer into the store slot.
447 * Zero the length of the new store buffer.
448 */
449#define ROTATE_BUFFERS(d) \
450	(d)->bd_hbuf = (d)->bd_sbuf; \
451	(d)->bd_hlen = (d)->bd_slen; \
452	(d)->bd_sbuf = (d)->bd_fbuf; \
453	(d)->bd_slen = 0; \
454	(d)->bd_fbuf = 0;
455/*
456 *  bpfread - read next chunk of packets from buffers
457 */
458static	int
459bpfread(dev, uio, ioflag)
460	dev_t dev;
461	register struct uio *uio;
462	int ioflag;
463{
464	register struct bpf_d *d = dev->si_drv1;
465	int error;
466	int s;
467
468	/*
469	 * Restrict application to use a buffer the same size as
470	 * as kernel buffers.
471	 */
472	if (uio->uio_resid != d->bd_bufsize)
473		return (EINVAL);
474
475	s = splimp();
476	/*
477	 * If the hold buffer is empty, then do a timed sleep, which
478	 * ends when the timeout expires or when enough packets
479	 * have arrived to fill the store buffer.
480	 */
481	while (d->bd_hbuf == 0) {
482		if (d->bd_immediate && d->bd_slen != 0) {
483			/*
484			 * A packet(s) either arrived since the previous
485			 * read or arrived while we were asleep.
486			 * Rotate the buffers and return what's here.
487			 */
488			ROTATE_BUFFERS(d);
489			break;
490		}
491
492		/*
493		 * No data is available, check to see if the bpf device
494		 * is still pointed at a real interface.  If not, return
495		 * ENXIO so that the userland process knows to rebind
496		 * it before using it again.
497		 */
498		if (d->bd_bif == NULL) {
499			splx(s);
500			return (ENXIO);
501		}
502
503		if (ioflag & IO_NDELAY)
504			error = EWOULDBLOCK;
505		else
506			error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf",
507					  d->bd_rtout);
508		if (error == EINTR || error == ERESTART) {
509			splx(s);
510			return (error);
511		}
512		if (error == EWOULDBLOCK) {
513			/*
514			 * On a timeout, return what's in the buffer,
515			 * which may be nothing.  If there is something
516			 * in the store buffer, we can rotate the buffers.
517			 */
518			if (d->bd_hbuf)
519				/*
520				 * We filled up the buffer in between
521				 * getting the timeout and arriving
522				 * here, so we don't need to rotate.
523				 */
524				break;
525
526			if (d->bd_slen == 0) {
527				splx(s);
528				return (0);
529			}
530			ROTATE_BUFFERS(d);
531			break;
532		}
533	}
534	/*
535	 * At this point, we know we have something in the hold slot.
536	 */
537	splx(s);
538
539	/*
540	 * Move data from hold buffer into user space.
541	 * We know the entire buffer is transferred since
542	 * we checked above that the read buffer is bpf_bufsize bytes.
543	 */
544	error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
545
546	s = splimp();
547	d->bd_fbuf = d->bd_hbuf;
548	d->bd_hbuf = 0;
549	d->bd_hlen = 0;
550	splx(s);
551
552	return (error);
553}
554
555
556/*
557 * If there are processes sleeping on this descriptor, wake them up.
558 */
559static inline void
560bpf_wakeup(d)
561	register struct bpf_d *d;
562{
563	wakeup((caddr_t)d);
564	if (d->bd_async && d->bd_sig && d->bd_sigio)
565		pgsigio(d->bd_sigio, d->bd_sig, 0);
566
567#if BSD >= 199103
568	selwakeup(&d->bd_sel);
569	/* XXX */
570	d->bd_sel.si_pid = 0;
571#else
572	if (d->bd_selproc) {
573		selwakeup(d->bd_selproc, (int)d->bd_selcoll);
574		d->bd_selcoll = 0;
575		d->bd_selproc = 0;
576	}
577#endif
578}
579
580static	int
581bpfwrite(dev, uio, ioflag)
582	dev_t dev;
583	struct uio *uio;
584	int ioflag;
585{
586	register struct bpf_d *d = dev->si_drv1;
587	struct ifnet *ifp;
588	struct mbuf *m;
589	int error, s;
590	static struct sockaddr dst;
591	int datlen;
592
593	if (d->bd_bif == 0)
594		return (ENXIO);
595
596	ifp = d->bd_bif->bif_ifp;
597
598	if (uio->uio_resid == 0)
599		return (0);
600
601	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
602	if (error)
603		return (error);
604
605	if (datlen > ifp->if_mtu)
606		return (EMSGSIZE);
607
608	if (d->bd_hdrcmplt)
609		dst.sa_family = pseudo_AF_HDRCMPLT;
610
611	s = splnet();
612#if BSD >= 199103
613	error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
614#else
615	error = (*ifp->if_output)(ifp, m, &dst);
616#endif
617	splx(s);
618	/*
619	 * The driver frees the mbuf.
620	 */
621	return (error);
622}
623
624/*
625 * Reset a descriptor by flushing its packet buffer and clearing the
626 * receive and drop counts.  Should be called at splimp.
627 */
628static void
629reset_d(d)
630	struct bpf_d *d;
631{
632	if (d->bd_hbuf) {
633		/* Free the hold buffer. */
634		d->bd_fbuf = d->bd_hbuf;
635		d->bd_hbuf = 0;
636	}
637	d->bd_slen = 0;
638	d->bd_hlen = 0;
639	d->bd_rcount = 0;
640	d->bd_dcount = 0;
641}
642
643/*
644 *  FIONREAD		Check for read packet available.
645 *  SIOCGIFADDR		Get interface address - convenient hook to driver.
646 *  BIOCGBLEN		Get buffer len [for read()].
647 *  BIOCSETF		Set ethernet read filter.
648 *  BIOCFLUSH		Flush read packet buffer.
649 *  BIOCPROMISC		Put interface into promiscuous mode.
650 *  BIOCGDLT		Get link layer type.
651 *  BIOCGETIF		Get interface name.
652 *  BIOCSETIF		Set interface.
653 *  BIOCSRTIMEOUT	Set read timeout.
654 *  BIOCGRTIMEOUT	Get read timeout.
655 *  BIOCGSTATS		Get packet stats.
656 *  BIOCIMMEDIATE	Set immediate mode.
657 *  BIOCVERSION		Get filter language version.
658 *  BIOCGHDRCMPLT	Get "header already complete" flag
659 *  BIOCSHDRCMPLT	Set "header already complete" flag
660 *  BIOCGSEESENT	Get "see packets sent" flag
661 *  BIOCSSEESENT	Set "see packets sent" flag
662 */
663/* ARGSUSED */
664static	int
665bpfioctl(dev, cmd, addr, flags, p)
666	dev_t dev;
667	u_long cmd;
668	caddr_t addr;
669	int flags;
670	struct proc *p;
671{
672	register struct bpf_d *d = dev->si_drv1;
673	int s, error = 0;
674
675	switch (cmd) {
676
677	default:
678		error = EINVAL;
679		break;
680
681	/*
682	 * Check for read packet available.
683	 */
684	case FIONREAD:
685		{
686			int n;
687
688			s = splimp();
689			n = d->bd_slen;
690			if (d->bd_hbuf)
691				n += d->bd_hlen;
692			splx(s);
693
694			*(int *)addr = n;
695			break;
696		}
697
698	case SIOCGIFADDR:
699		{
700			struct ifnet *ifp;
701
702			if (d->bd_bif == 0)
703				error = EINVAL;
704			else {
705				ifp = d->bd_bif->bif_ifp;
706				error = (*ifp->if_ioctl)(ifp, cmd, addr);
707			}
708			break;
709		}
710
711	/*
712	 * Get buffer len [for read()].
713	 */
714	case BIOCGBLEN:
715		*(u_int *)addr = d->bd_bufsize;
716		break;
717
718	/*
719	 * Set buffer length.
720	 */
721	case BIOCSBLEN:
722#if BSD < 199103
723		error = EINVAL;
724#else
725		if (d->bd_bif != 0)
726			error = EINVAL;
727		else {
728			register u_int size = *(u_int *)addr;
729
730			if (size > bpf_maxbufsize)
731				*(u_int *)addr = size = bpf_maxbufsize;
732			else if (size < BPF_MINBUFSIZE)
733				*(u_int *)addr = size = BPF_MINBUFSIZE;
734			d->bd_bufsize = size;
735		}
736#endif
737		break;
738
739	/*
740	 * Set link layer read filter.
741	 */
742	case BIOCSETF:
743		error = bpf_setf(d, (struct bpf_program *)addr);
744		break;
745
746	/*
747	 * Flush read packet buffer.
748	 */
749	case BIOCFLUSH:
750		s = splimp();
751		reset_d(d);
752		splx(s);
753		break;
754
755	/*
756	 * Put interface into promiscuous mode.
757	 */
758	case BIOCPROMISC:
759		if (d->bd_bif == 0) {
760			/*
761			 * No interface attached yet.
762			 */
763			error = EINVAL;
764			break;
765		}
766		s = splimp();
767		if (d->bd_promisc == 0) {
768			error = ifpromisc(d->bd_bif->bif_ifp, 1);
769			if (error == 0)
770				d->bd_promisc = 1;
771		}
772		splx(s);
773		break;
774
775	/*
776	 * Get device parameters.
777	 */
778	case BIOCGDLT:
779		if (d->bd_bif == 0)
780			error = EINVAL;
781		else
782			*(u_int *)addr = d->bd_bif->bif_dlt;
783		break;
784
785	/*
786	 * Get interface name.
787	 */
788	case BIOCGETIF:
789		if (d->bd_bif == 0)
790			error = EINVAL;
791		else {
792			struct ifnet *const ifp = d->bd_bif->bif_ifp;
793			struct ifreq *const ifr = (struct ifreq *)addr;
794
795			snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
796			    "%s%d", ifp->if_name, ifp->if_unit);
797		}
798		break;
799
800	/*
801	 * Set interface.
802	 */
803	case BIOCSETIF:
804		error = bpf_setif(d, (struct ifreq *)addr);
805		break;
806
807	/*
808	 * Set read timeout.
809	 */
810	case BIOCSRTIMEOUT:
811		{
812			struct timeval *tv = (struct timeval *)addr;
813
814			/*
815			 * Subtract 1 tick from tvtohz() since this isn't
816			 * a one-shot timer.
817			 */
818			if ((error = itimerfix(tv)) == 0)
819				d->bd_rtout = tvtohz(tv) - 1;
820			break;
821		}
822
823	/*
824	 * Get read timeout.
825	 */
826	case BIOCGRTIMEOUT:
827		{
828			struct timeval *tv = (struct timeval *)addr;
829
830			tv->tv_sec = d->bd_rtout / hz;
831			tv->tv_usec = (d->bd_rtout % hz) * tick;
832			break;
833		}
834
835	/*
836	 * Get packet stats.
837	 */
838	case BIOCGSTATS:
839		{
840			struct bpf_stat *bs = (struct bpf_stat *)addr;
841
842			bs->bs_recv = d->bd_rcount;
843			bs->bs_drop = d->bd_dcount;
844			break;
845		}
846
847	/*
848	 * Set immediate mode.
849	 */
850	case BIOCIMMEDIATE:
851		d->bd_immediate = *(u_int *)addr;
852		break;
853
854	case BIOCVERSION:
855		{
856			struct bpf_version *bv = (struct bpf_version *)addr;
857
858			bv->bv_major = BPF_MAJOR_VERSION;
859			bv->bv_minor = BPF_MINOR_VERSION;
860			break;
861		}
862
863	/*
864	 * Get "header already complete" flag
865	 */
866	case BIOCGHDRCMPLT:
867		*(u_int *)addr = d->bd_hdrcmplt;
868		break;
869
870	/*
871	 * Set "header already complete" flag
872	 */
873	case BIOCSHDRCMPLT:
874		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
875		break;
876
877	/*
878	 * Get "see sent packets" flag
879	 */
880	case BIOCGSEESENT:
881		*(u_int *)addr = d->bd_seesent;
882		break;
883
884	/*
885	 * Set "see sent packets" flag
886	 */
887	case BIOCSSEESENT:
888		d->bd_seesent = *(u_int *)addr;
889		break;
890
891	case FIONBIO:		/* Non-blocking I/O */
892		break;
893
894	case FIOASYNC:		/* Send signal on receive packets */
895		d->bd_async = *(int *)addr;
896		break;
897
898	case FIOSETOWN:
899		error = fsetown(*(int *)addr, &d->bd_sigio);
900		break;
901
902	case FIOGETOWN:
903		*(int *)addr = fgetown(d->bd_sigio);
904		break;
905
906	/* This is deprecated, FIOSETOWN should be used instead. */
907	case TIOCSPGRP:
908		error = fsetown(-(*(int *)addr), &d->bd_sigio);
909		break;
910
911	/* This is deprecated, FIOGETOWN should be used instead. */
912	case TIOCGPGRP:
913		*(int *)addr = -fgetown(d->bd_sigio);
914		break;
915
916	case BIOCSRSIG:		/* Set receive signal */
917		{
918		 	u_int sig;
919
920			sig = *(u_int *)addr;
921
922			if (sig >= NSIG)
923				error = EINVAL;
924			else
925				d->bd_sig = sig;
926			break;
927		}
928	case BIOCGRSIG:
929		*(u_int *)addr = d->bd_sig;
930		break;
931	}
932	return (error);
933}
934
935/*
936 * Set d's packet filter program to fp.  If this file already has a filter,
937 * free it and replace it.  Returns EINVAL for bogus requests.
938 */
939static int
940bpf_setf(d, fp)
941	struct bpf_d *d;
942	struct bpf_program *fp;
943{
944	struct bpf_insn *fcode, *old;
945	u_int flen, size;
946	int s;
947
948	old = d->bd_filter;
949	if (fp->bf_insns == 0) {
950		if (fp->bf_len != 0)
951			return (EINVAL);
952		s = splimp();
953		d->bd_filter = 0;
954		reset_d(d);
955		splx(s);
956		if (old != 0)
957			free((caddr_t)old, M_BPF);
958		return (0);
959	}
960	flen = fp->bf_len;
961	if (flen > BPF_MAXINSNS)
962		return (EINVAL);
963
964	size = flen * sizeof(*fp->bf_insns);
965	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
966	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
967	    bpf_validate(fcode, (int)flen)) {
968		s = splimp();
969		d->bd_filter = fcode;
970		reset_d(d);
971		splx(s);
972		if (old != 0)
973			free((caddr_t)old, M_BPF);
974
975		return (0);
976	}
977	free((caddr_t)fcode, M_BPF);
978	return (EINVAL);
979}
980
981/*
982 * Detach a file from its current interface (if attached at all) and attach
983 * to the interface indicated by the name stored in ifr.
984 * Return an errno or 0.
985 */
986static int
987bpf_setif(d, ifr)
988	struct bpf_d *d;
989	struct ifreq *ifr;
990{
991	struct bpf_if *bp;
992	int s, error;
993	struct ifnet *theywant;
994
995	theywant = ifunit(ifr->ifr_name);
996	if (theywant == 0)
997		return ENXIO;
998
999	/*
1000	 * Look through attached interfaces for the named one.
1001	 */
1002	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
1003		struct ifnet *ifp = bp->bif_ifp;
1004
1005		if (ifp == 0 || ifp != theywant)
1006			continue;
1007		/*
1008		 * We found the requested interface.
1009		 * If it's not up, return an error.
1010		 * Allocate the packet buffers if we need to.
1011		 * If we're already attached to requested interface,
1012		 * just flush the buffer.
1013		 */
1014		if ((ifp->if_flags & IFF_UP) == 0)
1015			return (ENETDOWN);
1016
1017		if (d->bd_sbuf == 0) {
1018			error = bpf_allocbufs(d);
1019			if (error != 0)
1020				return (error);
1021		}
1022		s = splimp();
1023		if (bp != d->bd_bif) {
1024			if (d->bd_bif)
1025				/*
1026				 * Detach if attached to something else.
1027				 */
1028				bpf_detachd(d);
1029
1030			bpf_attachd(d, bp);
1031		}
1032		reset_d(d);
1033		splx(s);
1034		return (0);
1035	}
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 */
1046int
1047bpfpoll(dev, events, p)
1048	register dev_t dev;
1049	int events;
1050	struct proc *p;
1051{
1052	register struct bpf_d *d;
1053	register int s;
1054	int revents = 0;
1055
1056	/*
1057	 * An imitation of the FIONREAD ioctl code.
1058	 */
1059	d = dev->si_drv1;
1060
1061	if (d->bd_bif == NULL)
1062		return (ENXIO);
1063
1064	s = splimp();
1065	if (events & (POLLIN | POLLRDNORM)) {
1066		if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1067			revents |= events & (POLLIN | POLLRDNORM);
1068		else
1069			selrecord(p, &d->bd_sel);
1070	}
1071	splx(s);
1072	return (revents);
1073}
1074
1075/*
1076 * Incoming linkage from device drivers.  Process the packet pkt, of length
1077 * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1078 * by each process' filter, and if accepted, stashed into the corresponding
1079 * buffer.
1080 */
1081void
1082bpf_tap(ifp, pkt, pktlen)
1083	struct ifnet *ifp;
1084	register u_char *pkt;
1085	register u_int pktlen;
1086{
1087	struct bpf_if *bp;
1088	register struct bpf_d *d;
1089	register u_int slen;
1090	/*
1091	 * Note that the ipl does not have to be raised at this point.
1092	 * The only problem that could arise here is that if two different
1093	 * interfaces shared any data.  This is not the case.
1094	 */
1095	bp = ifp->if_bpf;
1096	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1097		++d->bd_rcount;
1098		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1099		if (slen != 0)
1100			catchpacket(d, pkt, pktlen, slen, bcopy);
1101	}
1102}
1103
1104/*
1105 * Copy data from an mbuf chain into a buffer.  This code is derived
1106 * from m_copydata in sys/uipc_mbuf.c.
1107 */
1108static void
1109bpf_mcopy(src_arg, dst_arg, len)
1110	const void *src_arg;
1111	void *dst_arg;
1112	register size_t len;
1113{
1114	register const struct mbuf *m;
1115	register u_int count;
1116	u_char *dst;
1117
1118	m = src_arg;
1119	dst = dst_arg;
1120	while (len > 0) {
1121		if (m == 0)
1122			panic("bpf_mcopy");
1123		count = min(m->m_len, len);
1124		bcopy(mtod(m, void *), dst, count);
1125		m = m->m_next;
1126		dst += count;
1127		len -= count;
1128	}
1129}
1130
1131/*
1132 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1133 */
1134void
1135bpf_mtap(ifp, m)
1136	struct ifnet *ifp;
1137	struct mbuf *m;
1138{
1139	struct bpf_if *bp = ifp->if_bpf;
1140	struct bpf_d *d;
1141	u_int pktlen, slen;
1142	struct mbuf *m0;
1143
1144	pktlen = 0;
1145	for (m0 = m; m0 != 0; m0 = m0->m_next)
1146		pktlen += m0->m_len;
1147
1148	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1149		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1150			continue;
1151		++d->bd_rcount;
1152		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1153		if (slen != 0)
1154			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1155	}
1156}
1157
1158/*
1159 * Move the packet data from interface memory (pkt) into the
1160 * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
1161 * otherwise 0.  "copy" is the routine called to do the actual data
1162 * transfer.  bcopy is passed in to copy contiguous chunks, while
1163 * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1164 * pkt is really an mbuf.
1165 */
1166static void
1167catchpacket(d, pkt, pktlen, snaplen, cpfn)
1168	register struct bpf_d *d;
1169	register u_char *pkt;
1170	register u_int pktlen, snaplen;
1171	register void (*cpfn) __P((const void *, void *, size_t));
1172{
1173	register struct bpf_hdr *hp;
1174	register int totlen, curlen;
1175	register int hdrlen = d->bd_bif->bif_hdrlen;
1176	/*
1177	 * Figure out how many bytes to move.  If the packet is
1178	 * greater or equal to the snapshot length, transfer that
1179	 * much.  Otherwise, transfer the whole packet (unless
1180	 * we hit the buffer size limit).
1181	 */
1182	totlen = hdrlen + min(snaplen, pktlen);
1183	if (totlen > d->bd_bufsize)
1184		totlen = d->bd_bufsize;
1185
1186	/*
1187	 * Round up the end of the previous packet to the next longword.
1188	 */
1189	curlen = BPF_WORDALIGN(d->bd_slen);
1190	if (curlen + totlen > d->bd_bufsize) {
1191		/*
1192		 * This packet will overflow the storage buffer.
1193		 * Rotate the buffers if we can, then wakeup any
1194		 * pending reads.
1195		 */
1196		if (d->bd_fbuf == 0) {
1197			/*
1198			 * We haven't completed the previous read yet,
1199			 * so drop the packet.
1200			 */
1201			++d->bd_dcount;
1202			return;
1203		}
1204		ROTATE_BUFFERS(d);
1205		bpf_wakeup(d);
1206		curlen = 0;
1207	}
1208	else if (d->bd_immediate)
1209		/*
1210		 * Immediate mode is set.  A packet arrived so any
1211		 * reads should be woken up.
1212		 */
1213		bpf_wakeup(d);
1214
1215	/*
1216	 * Append the bpf header.
1217	 */
1218	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1219#if BSD >= 199103
1220	microtime(&hp->bh_tstamp);
1221#elif defined(sun)
1222	uniqtime(&hp->bh_tstamp);
1223#else
1224	hp->bh_tstamp = time;
1225#endif
1226	hp->bh_datalen = pktlen;
1227	hp->bh_hdrlen = hdrlen;
1228	/*
1229	 * Copy the packet data into the store buffer and update its length.
1230	 */
1231	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1232	d->bd_slen = curlen + totlen;
1233}
1234
1235/*
1236 * Initialize all nonzero fields of a descriptor.
1237 */
1238static int
1239bpf_allocbufs(d)
1240	register struct bpf_d *d;
1241{
1242	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1243	if (d->bd_fbuf == 0)
1244		return (ENOBUFS);
1245
1246	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1247	if (d->bd_sbuf == 0) {
1248		free(d->bd_fbuf, M_BPF);
1249		return (ENOBUFS);
1250	}
1251	d->bd_slen = 0;
1252	d->bd_hlen = 0;
1253	return (0);
1254}
1255
1256/*
1257 * Free buffers currently in use by a descriptor.
1258 * Called on close.
1259 */
1260static void
1261bpf_freed(d)
1262	register struct bpf_d *d;
1263{
1264	/*
1265	 * We don't need to lock out interrupts since this descriptor has
1266	 * been detached from its interface and it yet hasn't been marked
1267	 * free.
1268	 */
1269	if (d->bd_sbuf != 0) {
1270		free(d->bd_sbuf, M_BPF);
1271		if (d->bd_hbuf != 0)
1272			free(d->bd_hbuf, M_BPF);
1273		if (d->bd_fbuf != 0)
1274			free(d->bd_fbuf, M_BPF);
1275	}
1276	if (d->bd_filter)
1277		free((caddr_t)d->bd_filter, M_BPF);
1278}
1279
1280/*
1281 * Attach an interface to bpf.  driverp is a pointer to a (struct bpf_if *)
1282 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1283 * size of the link header (variable length headers not yet supported).
1284 */
1285void
1286bpfattach(ifp, dlt, hdrlen)
1287	struct ifnet *ifp;
1288	u_int dlt, hdrlen;
1289{
1290	struct bpf_if *bp;
1291	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_DONTWAIT);
1292	if (bp == 0)
1293		panic("bpfattach");
1294
1295	bp->bif_dlist = 0;
1296	bp->bif_ifp = ifp;
1297	bp->bif_dlt = dlt;
1298
1299	bp->bif_next = bpf_iflist;
1300	bpf_iflist = bp;
1301
1302	bp->bif_ifp->if_bpf = 0;
1303
1304	/*
1305	 * Compute the length of the bpf header.  This is not necessarily
1306	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1307	 * that the network layer header begins on a longword boundary (for
1308	 * performance reasons and to alleviate alignment restrictions).
1309	 */
1310	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1311
1312	if (bootverbose)
1313		printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1314}
1315
1316/*
1317 * Detach bpf from an interface.  This involves detaching each descriptor
1318 * associated with the interface, and leaving bd_bif NULL.  Notify each
1319 * descriptor as it's detached so that any sleepers wake up and get
1320 * ENXIO.
1321 */
1322void
1323bpfdetach(ifp)
1324	struct ifnet *ifp;
1325{
1326	struct bpf_if	*bp, *bp_prev;
1327	struct bpf_d	*d;
1328	int	s;
1329
1330	s = splimp();
1331
1332	/* Locate BPF interface information */
1333	bp_prev = NULL;
1334	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1335		if (ifp == bp->bif_ifp)
1336			break;
1337		bp_prev = bp;
1338	}
1339
1340	/* Interface wasn't attached */
1341	if (bp->bif_ifp == NULL) {
1342		splx(s);
1343		printf("bpfdetach: %s%d was not attached\n", ifp->if_name,
1344		    ifp->if_unit);
1345		return;
1346	}
1347
1348	while ((d = bp->bif_dlist) != NULL) {
1349		bpf_detachd(d);
1350		bpf_wakeup(d);
1351	}
1352
1353	if (bp_prev) {
1354		bp_prev->bif_next = bp->bif_next;
1355	} else {
1356		bpf_iflist = bp->bif_next;
1357	}
1358
1359	free(bp, M_BPF);
1360
1361	splx(s);
1362}
1363
1364static void bpf_drvinit __P((void *unused));
1365
1366static void bpf_clone __P((void *arg, char *name, int namelen, dev_t *dev));
1367
1368static void
1369bpf_clone(arg, name, namelen, dev)
1370	void *arg;
1371	char *name;
1372	int namelen;
1373	dev_t *dev;
1374{
1375	int u;
1376
1377	if (*dev != NODEV)
1378		return;
1379	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1380		return;
1381	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1382	    "bpf%d", u);
1383	(*dev)->si_flags |= SI_CHEAPCLONE;
1384	return;
1385}
1386
1387static void
1388bpf_drvinit(unused)
1389	void *unused;
1390{
1391
1392	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1393	cdevsw_add(&bpf_cdevsw);
1394}
1395
1396SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1397
1398#else /* !BPF */
1399/*
1400 * NOP stubs to allow bpf-using drivers to load and function.
1401 *
1402 * A 'better' implementation would allow the core bpf functionality
1403 * to be loaded at runtime.
1404 */
1405
1406void
1407bpf_tap(ifp, pkt, pktlen)
1408	struct ifnet *ifp;
1409	register u_char *pkt;
1410	register u_int pktlen;
1411{
1412}
1413
1414void
1415bpf_mtap(ifp, m)
1416	struct ifnet *ifp;
1417	struct mbuf *m;
1418{
1419}
1420
1421void
1422bpfattach(ifp, dlt, hdrlen)
1423	struct ifnet *ifp;
1424	u_int dlt, hdrlen;
1425{
1426}
1427
1428void
1429bpfdetach(ifp)
1430	struct ifnet *ifp;
1431{
1432}
1433
1434u_int
1435bpf_filter(pc, p, wirelen, buflen)
1436	register const struct bpf_insn *pc;
1437	register u_char *p;
1438	u_int wirelen;
1439	register u_int buflen;
1440{
1441	return -1;	/* "no filter" behaviour */
1442}
1443
1444#endif /* !BPF */
1445