bpf.c revision 31282
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 * $Id: bpf.c,v 1.35 1997/10/03 21:32:05 julian Exp $
41 */
42
43#include "bpfilter.h"
44
45#if NBPFILTER > 0
46
47#ifndef __GNUC__
48#define inline
49#else
50#define inline __inline
51#endif
52
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/conf.h>
56#include <sys/malloc.h>
57#include <sys/mbuf.h>
58#include <sys/time.h>
59#include <sys/proc.h>
60#include <sys/signalvar.h>
61#include <sys/filio.h>
62#include <sys/sockio.h>
63#include <sys/ttycom.h>
64
65#if defined(sparc) && BSD < 199103
66#include <sys/stream.h>
67#endif
68#include <sys/poll.h>
69
70#include <sys/socket.h>
71#include <sys/vnode.h>
72
73#include <net/if.h>
74#include <net/bpf.h>
75#include <net/bpfdesc.h>
76
77#include <netinet/in.h>
78#include <netinet/if_ether.h>
79#include <sys/kernel.h>
80#include <sys/sysctl.h>
81#ifdef DEVFS
82#include <sys/devfsext.h>
83#endif /*DEVFS*/
84
85
86/*
87 * Older BSDs don't have kernel malloc.
88 */
89#if BSD < 199103
90extern bcopy();
91static caddr_t bpf_alloc();
92#include <net/bpf_compat.h>
93#define BPF_BUFSIZE (MCLBYTES-8)
94#define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
95#else
96#define BPF_BUFSIZE 4096
97#define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
98#endif
99
100#define PRINET  26			/* interruptible */
101
102/*
103 * The default read buffer size is patchable.
104 */
105static int bpf_bufsize = BPF_BUFSIZE;
106SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
107	&bpf_bufsize, 0, "");
108
109/*
110 *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
111 *  bpf_dtab holds the descriptors, indexed by minor device #
112 */
113static struct bpf_if	*bpf_iflist;
114static struct bpf_d	bpf_dtab[NBPFILTER];
115static int		bpf_dtab_init;
116
117static int	bpf_allocbufs __P((struct bpf_d *));
118static void	bpf_attachd __P((struct bpf_d *d, struct bpf_if *bp));
119static void	bpf_detachd __P((struct bpf_d *d));
120static void	bpf_freed __P((struct bpf_d *));
121static void	bpf_ifname __P((struct ifnet *, struct ifreq *));
122static void	bpf_mcopy __P((const void *, void *, u_int));
123static int	bpf_movein __P((struct uio *, int,
124		    struct mbuf **, struct sockaddr *, int *));
125static int	bpf_setif __P((struct bpf_d *, struct ifreq *));
126static inline void
127		bpf_wakeup __P((struct bpf_d *));
128static void	catchpacket __P((struct bpf_d *, u_char *, u_int,
129		    u_int, void (*)(const void *, void *, u_int)));
130static void	reset_d __P((struct bpf_d *));
131static int	 bpf_setf __P((struct bpf_d *, struct bpf_program *));
132
133static	d_open_t	bpfopen;
134static	d_close_t	bpfclose;
135static	d_read_t	bpfread;
136static	d_write_t	bpfwrite;
137static	d_ioctl_t	bpfioctl;
138static	d_poll_t	bpfpoll;
139
140#define CDEV_MAJOR 23
141static struct cdevsw bpf_cdevsw =
142 	{ bpfopen,	bpfclose,	bpfread,	bpfwrite,	/*23*/
143 	  bpfioctl,	nostop,		nullreset,	nodevtotty,/* bpf */
144 	  bpfpoll,	nommap,		NULL,	"bpf",	NULL,	-1 };
145
146
147static int
148bpf_movein(uio, linktype, mp, sockp, datlen)
149	register struct uio *uio;
150	int linktype, *datlen;
151	register struct mbuf **mp;
152	register struct sockaddr *sockp;
153{
154	struct mbuf *m;
155	int error;
156	int len;
157	int hlen;
158
159	/*
160	 * Build a sockaddr based on the data link layer type.
161	 * We do this at this level because the ethernet header
162	 * is copied directly into the data field of the sockaddr.
163	 * In the case of SLIP, there is no header and the packet
164	 * is forwarded as is.
165	 * Also, we are careful to leave room at the front of the mbuf
166	 * for the link level header.
167	 */
168	switch (linktype) {
169
170	case DLT_SLIP:
171		sockp->sa_family = AF_INET;
172		hlen = 0;
173		break;
174
175	case DLT_EN10MB:
176		sockp->sa_family = AF_UNSPEC;
177		/* XXX Would MAXLINKHDR be better? */
178		hlen = sizeof(struct ether_header);
179		break;
180
181	case DLT_FDDI:
182#if defined(__FreeBSD__) || defined(__bsdi__)
183		sockp->sa_family = AF_IMPLINK;
184		hlen = 0;
185#else
186		sockp->sa_family = AF_UNSPEC;
187		/* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
188		hlen = 24;
189#endif
190		break;
191
192	case DLT_NULL:
193		sockp->sa_family = AF_UNSPEC;
194		hlen = 0;
195		break;
196
197	default:
198		return (EIO);
199	}
200
201	len = uio->uio_resid;
202	*datlen = len - hlen;
203	if ((unsigned)len > MCLBYTES)
204		return (EIO);
205
206	MGETHDR(m, M_WAIT, MT_DATA);
207	if (m == 0)
208		return (ENOBUFS);
209	if (len > MHLEN) {
210#if BSD >= 199103
211		MCLGET(m, M_WAIT);
212		if ((m->m_flags & M_EXT) == 0) {
213#else
214		MCLGET(m);
215		if (m->m_len != MCLBYTES) {
216#endif
217			error = ENOBUFS;
218			goto bad;
219		}
220	}
221	m->m_pkthdr.len = m->m_len = len;
222	m->m_pkthdr.rcvif = NULL;
223	*mp = m;
224	/*
225	 * Make room for link header.
226	 */
227	if (hlen != 0) {
228		m->m_pkthdr.len -= hlen;
229		m->m_len -= hlen;
230#if BSD >= 199103
231		m->m_data += hlen; /* XXX */
232#else
233		m->m_off += hlen;
234#endif
235		error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio);
236		if (error)
237			goto bad;
238	}
239	error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio);
240	if (!error)
241		return (0);
242 bad:
243	m_freem(m);
244	return (error);
245}
246
247/*
248 * Attach file to the bpf interface, i.e. make d listen on bp.
249 * Must be called at splimp.
250 */
251static void
252bpf_attachd(d, bp)
253	struct bpf_d *d;
254	struct bpf_if *bp;
255{
256	/*
257	 * Point d at bp, and add d to the interface's list of listeners.
258	 * Finally, point the driver's bpf cookie at the interface so
259	 * it will divert packets to bpf.
260	 */
261	d->bd_bif = bp;
262	d->bd_next = bp->bif_dlist;
263	bp->bif_dlist = d;
264
265	bp->bif_ifp->if_bpf = bp;
266}
267
268/*
269 * Detach a file from its interface.
270 */
271static void
272bpf_detachd(d)
273	struct bpf_d *d;
274{
275	struct bpf_d **p;
276	struct bpf_if *bp;
277
278	bp = d->bd_bif;
279	/*
280	 * Check if this descriptor had requested promiscuous mode.
281	 * If so, turn it off.
282	 */
283	if (d->bd_promisc) {
284		d->bd_promisc = 0;
285		if (ifpromisc(bp->bif_ifp, 0))
286			/*
287			 * Something is really wrong if we were able to put
288			 * the driver into promiscuous mode, but can't
289			 * take it out.
290			 */
291			panic("bpf: ifpromisc failed");
292	}
293	/* Remove d from the interface's descriptor list. */
294	p = &bp->bif_dlist;
295	while (*p != d) {
296		p = &(*p)->bd_next;
297		if (*p == 0)
298			panic("bpf_detachd: descriptor not in list");
299	}
300	*p = (*p)->bd_next;
301	if (bp->bif_dlist == 0)
302		/*
303		 * Let the driver know that there are no more listeners.
304		 */
305		d->bd_bif->bif_ifp->if_bpf = 0;
306	d->bd_bif = 0;
307}
308
309
310/*
311 * Mark a descriptor free by making it point to itself.
312 * This is probably cheaper than marking with a constant since
313 * the address should be in a register anyway.
314 */
315#define D_ISFREE(d) ((d) == (d)->bd_next)
316#define D_MARKFREE(d) ((d)->bd_next = (d))
317#define D_MARKUSED(d) ((d)->bd_next = 0)
318
319/*
320 * Open ethernet device.  Returns ENXIO for illegal minor device number,
321 * EBUSY if file is open by another process.
322 */
323/* ARGSUSED */
324static	int
325bpfopen(dev, flags, fmt, p)
326	dev_t dev;
327	int flags;
328	int fmt;
329	struct proc *p;
330{
331	register struct bpf_d *d;
332
333	if (minor(dev) >= NBPFILTER)
334		return (ENXIO);
335	/*
336	 * Each minor can be opened by only one process.  If the requested
337	 * minor is in use, return EBUSY.
338	 */
339	d = &bpf_dtab[minor(dev)];
340	if (!D_ISFREE(d))
341		return (EBUSY);
342
343	/* Mark "free" and do most initialization. */
344	bzero((char *)d, sizeof(*d));
345	d->bd_bufsize = bpf_bufsize;
346	d->bd_sig = SIGIO;
347
348	return (0);
349}
350
351/*
352 * Close the descriptor by detaching it from its interface,
353 * deallocating its buffers, and marking it free.
354 */
355/* ARGSUSED */
356static	int
357bpfclose(dev, flags, fmt, p)
358	dev_t dev;
359	int flags;
360	int fmt;
361	struct proc *p;
362{
363	register struct bpf_d *d = &bpf_dtab[minor(dev)];
364	register int s;
365
366	s = splimp();
367	if (d->bd_bif)
368		bpf_detachd(d);
369	splx(s);
370	bpf_freed(d);
371
372	return (0);
373}
374
375/*
376 * Support for SunOS, which does not have tsleep.
377 */
378#if BSD < 199103
379static
380bpf_timeout(arg)
381	caddr_t arg;
382{
383	struct bpf_d *d = (struct bpf_d *)arg;
384	d->bd_timedout = 1;
385	wakeup(arg);
386}
387
388#define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
389
390int
391bpf_sleep(d)
392	register struct bpf_d *d;
393{
394	register int rto = d->bd_rtout;
395	register int st;
396
397	if (rto != 0) {
398		d->bd_timedout = 0;
399		timeout(bpf_timeout, (caddr_t)d, rto);
400	}
401	st = sleep((caddr_t)d, PRINET|PCATCH);
402	if (rto != 0) {
403		if (d->bd_timedout == 0)
404			untimeout(bpf_timeout, (caddr_t)d);
405		else if (st == 0)
406			return EWOULDBLOCK;
407	}
408	return (st != 0) ? EINTR : 0;
409}
410#else
411#define BPF_SLEEP tsleep
412#endif
413
414/*
415 * Rotate the packet buffers in descriptor d.  Move the store buffer
416 * into the hold slot, and the free buffer into the store slot.
417 * Zero the length of the new store buffer.
418 */
419#define ROTATE_BUFFERS(d) \
420	(d)->bd_hbuf = (d)->bd_sbuf; \
421	(d)->bd_hlen = (d)->bd_slen; \
422	(d)->bd_sbuf = (d)->bd_fbuf; \
423	(d)->bd_slen = 0; \
424	(d)->bd_fbuf = 0;
425/*
426 *  bpfread - read next chunk of packets from buffers
427 */
428static	int
429bpfread(dev, uio, ioflag)
430	dev_t dev;
431	register struct uio *uio;
432	int ioflag;
433{
434	register struct bpf_d *d = &bpf_dtab[minor(dev)];
435	int error;
436	int s;
437
438	/*
439	 * Restrict application to use a buffer the same size as
440	 * as kernel buffers.
441	 */
442	if (uio->uio_resid != d->bd_bufsize)
443		return (EINVAL);
444
445	s = splimp();
446	/*
447	 * If the hold buffer is empty, then do a timed sleep, which
448	 * ends when the timeout expires or when enough packets
449	 * have arrived to fill the store buffer.
450	 */
451	while (d->bd_hbuf == 0) {
452		if (d->bd_immediate && d->bd_slen != 0) {
453			/*
454			 * A packet(s) either arrived since the previous
455			 * read or arrived while we were asleep.
456			 * Rotate the buffers and return what's here.
457			 */
458			ROTATE_BUFFERS(d);
459			break;
460		}
461		if (ioflag & IO_NDELAY)
462			error = EWOULDBLOCK;
463		else
464			error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf",
465					  d->bd_rtout);
466		if (error == EINTR || error == ERESTART) {
467			splx(s);
468			return (error);
469		}
470		if (error == EWOULDBLOCK) {
471			/*
472			 * On a timeout, return what's in the buffer,
473			 * which may be nothing.  If there is something
474			 * in the store buffer, we can rotate the buffers.
475			 */
476			if (d->bd_hbuf)
477				/*
478				 * We filled up the buffer in between
479				 * getting the timeout and arriving
480				 * here, so we don't need to rotate.
481				 */
482				break;
483
484			if (d->bd_slen == 0) {
485				splx(s);
486				return (0);
487			}
488			ROTATE_BUFFERS(d);
489			break;
490		}
491	}
492	/*
493	 * At this point, we know we have something in the hold slot.
494	 */
495	splx(s);
496
497	/*
498	 * Move data from hold buffer into user space.
499	 * We know the entire buffer is transferred since
500	 * we checked above that the read buffer is bpf_bufsize bytes.
501	 */
502	error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
503
504	s = splimp();
505	d->bd_fbuf = d->bd_hbuf;
506	d->bd_hbuf = 0;
507	d->bd_hlen = 0;
508	splx(s);
509
510	return (error);
511}
512
513
514/*
515 * If there are processes sleeping on this descriptor, wake them up.
516 */
517static inline void
518bpf_wakeup(d)
519	register struct bpf_d *d;
520{
521	struct proc *p;
522
523	wakeup((caddr_t)d);
524	if (d->bd_async && d->bd_sig)
525		if (d->bd_pgid > 0)
526			gsignal (d->bd_pgid, d->bd_sig);
527		else if (p = pfind (-d->bd_pgid))
528			psignal (p, d->bd_sig);
529
530#if BSD >= 199103
531	selwakeup(&d->bd_sel);
532	/* XXX */
533	d->bd_sel.si_pid = 0;
534#else
535	if (d->bd_selproc) {
536		selwakeup(d->bd_selproc, (int)d->bd_selcoll);
537		d->bd_selcoll = 0;
538		d->bd_selproc = 0;
539	}
540#endif
541}
542
543static	int
544bpfwrite(dev, uio, ioflag)
545	dev_t dev;
546	struct uio *uio;
547	int ioflag;
548{
549	register struct bpf_d *d = &bpf_dtab[minor(dev)];
550	struct ifnet *ifp;
551	struct mbuf *m;
552	int error, s;
553	static struct sockaddr dst;
554	int datlen;
555
556	if (d->bd_bif == 0)
557		return (ENXIO);
558
559	ifp = d->bd_bif->bif_ifp;
560
561	if (uio->uio_resid == 0)
562		return (0);
563
564	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
565	if (error)
566		return (error);
567
568	if (datlen > ifp->if_mtu)
569		return (EMSGSIZE);
570
571	s = splnet();
572#if BSD >= 199103
573	error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
574#else
575	error = (*ifp->if_output)(ifp, m, &dst);
576#endif
577	splx(s);
578	/*
579	 * The driver frees the mbuf.
580	 */
581	return (error);
582}
583
584/*
585 * Reset a descriptor by flushing its packet buffer and clearing the
586 * receive and drop counts.  Should be called at splimp.
587 */
588static void
589reset_d(d)
590	struct bpf_d *d;
591{
592	if (d->bd_hbuf) {
593		/* Free the hold buffer. */
594		d->bd_fbuf = d->bd_hbuf;
595		d->bd_hbuf = 0;
596	}
597	d->bd_slen = 0;
598	d->bd_hlen = 0;
599	d->bd_rcount = 0;
600	d->bd_dcount = 0;
601}
602
603/*
604 *  FIONREAD		Check for read packet available.
605 *  SIOCGIFADDR		Get interface address - convenient hook to driver.
606 *  BIOCGBLEN		Get buffer len [for read()].
607 *  BIOCSETF		Set ethernet read filter.
608 *  BIOCFLUSH		Flush read packet buffer.
609 *  BIOCPROMISC		Put interface into promiscuous mode.
610 *  BIOCGDLT		Get link layer type.
611 *  BIOCGETIF		Get interface name.
612 *  BIOCSETIF		Set interface.
613 *  BIOCSRTIMEOUT	Set read timeout.
614 *  BIOCGRTIMEOUT	Get read timeout.
615 *  BIOCGSTATS		Get packet stats.
616 *  BIOCIMMEDIATE	Set immediate mode.
617 *  BIOCVERSION		Get filter language version.
618 */
619/* ARGSUSED */
620static	int
621bpfioctl(dev, cmd, addr, flags, p)
622	dev_t dev;
623	int cmd;
624	caddr_t addr;
625	int flags;
626	struct proc *p;
627{
628	register struct bpf_d *d = &bpf_dtab[minor(dev)];
629	int s, error = 0;
630
631	switch (cmd) {
632
633	default:
634		error = EINVAL;
635		break;
636
637	/*
638	 * Check for read packet available.
639	 */
640	case FIONREAD:
641		{
642			int n;
643
644			s = splimp();
645			n = d->bd_slen;
646			if (d->bd_hbuf)
647				n += d->bd_hlen;
648			splx(s);
649
650			*(int *)addr = n;
651			break;
652		}
653
654	case SIOCGIFADDR:
655		{
656			struct ifnet *ifp;
657
658			if (d->bd_bif == 0)
659				error = EINVAL;
660			else {
661				ifp = d->bd_bif->bif_ifp;
662				error = (*ifp->if_ioctl)(ifp, cmd, addr);
663			}
664			break;
665		}
666
667	/*
668	 * Get buffer len [for read()].
669	 */
670	case BIOCGBLEN:
671		*(u_int *)addr = d->bd_bufsize;
672		break;
673
674	/*
675	 * Set buffer length.
676	 */
677	case BIOCSBLEN:
678#if BSD < 199103
679		error = EINVAL;
680#else
681		if (d->bd_bif != 0)
682			error = EINVAL;
683		else {
684			register u_int size = *(u_int *)addr;
685
686			if (size > BPF_MAXBUFSIZE)
687				*(u_int *)addr = size = BPF_MAXBUFSIZE;
688			else if (size < BPF_MINBUFSIZE)
689				*(u_int *)addr = size = BPF_MINBUFSIZE;
690			d->bd_bufsize = size;
691		}
692#endif
693		break;
694
695	/*
696	 * Set link layer read filter.
697	 */
698	case BIOCSETF:
699		error = bpf_setf(d, (struct bpf_program *)addr);
700		break;
701
702	/*
703	 * Flush read packet buffer.
704	 */
705	case BIOCFLUSH:
706		s = splimp();
707		reset_d(d);
708		splx(s);
709		break;
710
711	/*
712	 * Put interface into promiscuous mode.
713	 */
714	case BIOCPROMISC:
715		if (d->bd_bif == 0) {
716			/*
717			 * No interface attached yet.
718			 */
719			error = EINVAL;
720			break;
721		}
722		s = splimp();
723		if (d->bd_promisc == 0) {
724			error = ifpromisc(d->bd_bif->bif_ifp, 1);
725			if (error == 0)
726				d->bd_promisc = 1;
727		}
728		splx(s);
729		break;
730
731	/*
732	 * Get device parameters.
733	 */
734	case BIOCGDLT:
735		if (d->bd_bif == 0)
736			error = EINVAL;
737		else
738			*(u_int *)addr = d->bd_bif->bif_dlt;
739		break;
740
741	/*
742	 * Set interface name.
743	 */
744	case BIOCGETIF:
745		if (d->bd_bif == 0)
746			error = EINVAL;
747		else
748			bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
749		break;
750
751	/*
752	 * Set interface.
753	 */
754	case BIOCSETIF:
755		error = bpf_setif(d, (struct ifreq *)addr);
756		break;
757
758	/*
759	 * Set read timeout.
760	 */
761	case BIOCSRTIMEOUT:
762		{
763			struct timeval *tv = (struct timeval *)addr;
764			u_long msec;
765
766			/* Compute number of milliseconds. */
767			msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
768			/* Scale milliseconds to ticks.  Assume hard
769			   clock has millisecond or greater resolution
770			   (i.e. tick >= 1000).  For 10ms hardclock,
771			   tick/1000 = 10, so rtout<-msec/10. */
772			d->bd_rtout = msec / (tick / 1000);
773			break;
774		}
775
776	/*
777	 * Get read timeout.
778	 */
779	case BIOCGRTIMEOUT:
780		{
781			struct timeval *tv = (struct timeval *)addr;
782			u_long msec = d->bd_rtout;
783
784			msec *= tick / 1000;
785			tv->tv_sec = msec / 1000;
786			tv->tv_usec = msec % 1000;
787			break;
788		}
789
790	/*
791	 * Get packet stats.
792	 */
793	case BIOCGSTATS:
794		{
795			struct bpf_stat *bs = (struct bpf_stat *)addr;
796
797			bs->bs_recv = d->bd_rcount;
798			bs->bs_drop = d->bd_dcount;
799			break;
800		}
801
802	/*
803	 * Set immediate mode.
804	 */
805	case BIOCIMMEDIATE:
806		d->bd_immediate = *(u_int *)addr;
807		break;
808
809	case BIOCVERSION:
810		{
811			struct bpf_version *bv = (struct bpf_version *)addr;
812
813			bv->bv_major = BPF_MAJOR_VERSION;
814			bv->bv_minor = BPF_MINOR_VERSION;
815			break;
816		}
817
818	case FIONBIO:		/* Non-blocking I/O */
819		break;
820
821	case FIOASYNC:		/* Send signal on receive packets */
822		d->bd_async = *(int *)addr;
823		break;
824
825/* N.B.  ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing the
826   equivalent of a TIOCSPGRP and hence end up here.  *However* TIOCSPGRP's arg
827   is a process group if it's positive and a process id if it's negative.  This
828   is exactly the opposite of what the other two functions want!  Therefore
829   there is code in ioctl and fcntl to negate the arg before calling here. */
830
831	case TIOCSPGRP:		/* Process or group to send signals to */
832		d->bd_pgid = *(int *)addr;
833		break;
834
835	case TIOCGPGRP:
836		*(int *)addr = d->bd_pgid;
837		break;
838
839	case BIOCSRSIG:		/* Set receive signal */
840		{
841		 	u_int sig;
842
843			sig = *(u_int *)addr;
844
845			if (sig >= NSIG)
846				error = EINVAL;
847			else
848				d->bd_sig = sig;
849			break;
850		}
851	case BIOCGRSIG:
852		*(u_int *)addr = d->bd_sig;
853		break;
854	}
855	return (error);
856}
857
858/*
859 * Set d's packet filter program to fp.  If this file already has a filter,
860 * free it and replace it.  Returns EINVAL for bogus requests.
861 */
862static int
863bpf_setf(d, fp)
864	struct bpf_d *d;
865	struct bpf_program *fp;
866{
867	struct bpf_insn *fcode, *old;
868	u_int flen, size;
869	int s;
870
871	old = d->bd_filter;
872	if (fp->bf_insns == 0) {
873		if (fp->bf_len != 0)
874			return (EINVAL);
875		s = splimp();
876		d->bd_filter = 0;
877		reset_d(d);
878		splx(s);
879		if (old != 0)
880			free((caddr_t)old, M_DEVBUF);
881		return (0);
882	}
883	flen = fp->bf_len;
884	if (flen > BPF_MAXINSNS)
885		return (EINVAL);
886
887	size = flen * sizeof(*fp->bf_insns);
888	fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
889	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
890	    bpf_validate(fcode, (int)flen)) {
891		s = splimp();
892		d->bd_filter = fcode;
893		reset_d(d);
894		splx(s);
895		if (old != 0)
896			free((caddr_t)old, M_DEVBUF);
897
898		return (0);
899	}
900	free((caddr_t)fcode, M_DEVBUF);
901	return (EINVAL);
902}
903
904/*
905 * Detach a file from its current interface (if attached at all) and attach
906 * to the interface indicated by the name stored in ifr.
907 * Return an errno or 0.
908 */
909static int
910bpf_setif(d, ifr)
911	struct bpf_d *d;
912	struct ifreq *ifr;
913{
914	struct bpf_if *bp;
915	int s, error;
916	struct ifnet *theywant;
917
918	theywant = ifunit(ifr->ifr_name);
919	if (theywant == 0)
920		return ENXIO;
921
922	/*
923	 * Look through attached interfaces for the named one.
924	 */
925	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
926		struct ifnet *ifp = bp->bif_ifp;
927
928		if (ifp == 0 || ifp != theywant)
929			continue;
930		/*
931		 * We found the requested interface.
932		 * If it's not up, return an error.
933		 * Allocate the packet buffers if we need to.
934		 * If we're already attached to requested interface,
935		 * just flush the buffer.
936		 */
937		if ((ifp->if_flags & IFF_UP) == 0)
938			return (ENETDOWN);
939
940		if (d->bd_sbuf == 0) {
941			error = bpf_allocbufs(d);
942			if (error != 0)
943				return (error);
944		}
945		s = splimp();
946		if (bp != d->bd_bif) {
947			if (d->bd_bif)
948				/*
949				 * Detach if attached to something else.
950				 */
951				bpf_detachd(d);
952
953			bpf_attachd(d, bp);
954		}
955		reset_d(d);
956		splx(s);
957		return (0);
958	}
959	/* Not found. */
960	return (ENXIO);
961}
962
963/*
964 * Convert an interface name plus unit number of an ifp to a single
965 * name which is returned in the ifr.
966 */
967static void
968bpf_ifname(ifp, ifr)
969	struct ifnet *ifp;
970	struct ifreq *ifr;
971{
972	char *s = ifp->if_name;
973	char *d = ifr->ifr_name;
974
975	while (*d++ = *s++)
976		continue;
977	d--; /* back to the null */
978	/* XXX Assume that unit number is less than 10. */
979	*d++ = ifp->if_unit + '0';
980	*d = '\0';
981}
982
983/*
984 * Support for select() and poll() system calls
985 *
986 * Return true iff the specific operation will not block indefinitely.
987 * Otherwise, return false but make a note that a selwakeup() must be done.
988 */
989int
990bpfpoll(dev, events, p)
991	register dev_t dev;
992	int events;
993	struct proc *p;
994{
995	register struct bpf_d *d;
996	register int s;
997	int revents = 0;
998
999	/*
1000	 * An imitation of the FIONREAD ioctl code.
1001	 */
1002	d = &bpf_dtab[minor(dev)];
1003
1004	s = splimp();
1005	if (events & (POLLIN | POLLRDNORM))
1006		if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1007			revents |= events & (POLLIN | POLLRDNORM);
1008		else
1009			selrecord(p, &d->bd_sel);
1010
1011	splx(s);
1012	return (revents);
1013}
1014
1015/*
1016 * Incoming linkage from device drivers.  Process the packet pkt, of length
1017 * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1018 * by each process' filter, and if accepted, stashed into the corresponding
1019 * buffer.
1020 */
1021void
1022bpf_tap(ifp, pkt, pktlen)
1023	struct ifnet *ifp;
1024	register u_char *pkt;
1025	register u_int pktlen;
1026{
1027	struct bpf_if *bp;
1028	register struct bpf_d *d;
1029	register u_int slen;
1030	/*
1031	 * Note that the ipl does not have to be raised at this point.
1032	 * The only problem that could arise here is that if two different
1033	 * interfaces shared any data.  This is not the case.
1034	 */
1035	bp = ifp->if_bpf;
1036	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1037		++d->bd_rcount;
1038		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1039		if (slen != 0)
1040			catchpacket(d, pkt, pktlen, slen, bcopy);
1041	}
1042}
1043
1044/*
1045 * Copy data from an mbuf chain into a buffer.  This code is derived
1046 * from m_copydata in sys/uipc_mbuf.c.
1047 */
1048static void
1049bpf_mcopy(src_arg, dst_arg, len)
1050	const void *src_arg;
1051	void *dst_arg;
1052	register u_int len;
1053{
1054	register const struct mbuf *m;
1055	register u_int count;
1056	u_char *dst;
1057
1058	m = src_arg;
1059	dst = dst_arg;
1060	while (len > 0) {
1061		if (m == 0)
1062			panic("bpf_mcopy");
1063		count = min(m->m_len, len);
1064		bcopy(mtod(m, void *), dst, count);
1065		m = m->m_next;
1066		dst += count;
1067		len -= count;
1068	}
1069}
1070
1071/*
1072 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1073 */
1074void
1075bpf_mtap(ifp, m)
1076	struct ifnet *ifp;
1077	struct mbuf *m;
1078{
1079	struct bpf_if *bp = ifp->if_bpf;
1080	struct bpf_d *d;
1081	u_int pktlen, slen;
1082	struct mbuf *m0;
1083
1084	pktlen = 0;
1085	for (m0 = m; m0 != 0; m0 = m0->m_next)
1086		pktlen += m0->m_len;
1087
1088	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1089		++d->bd_rcount;
1090		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1091		if (slen != 0)
1092			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1093	}
1094}
1095
1096/*
1097 * Move the packet data from interface memory (pkt) into the
1098 * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
1099 * otherwise 0.  "copy" is the routine called to do the actual data
1100 * transfer.  bcopy is passed in to copy contiguous chunks, while
1101 * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1102 * pkt is really an mbuf.
1103 */
1104static void
1105catchpacket(d, pkt, pktlen, snaplen, cpfn)
1106	register struct bpf_d *d;
1107	register u_char *pkt;
1108	register u_int pktlen, snaplen;
1109	register void (*cpfn) __P((const void *, void *, u_int));
1110{
1111	register struct bpf_hdr *hp;
1112	register int totlen, curlen;
1113	register int hdrlen = d->bd_bif->bif_hdrlen;
1114	/*
1115	 * Figure out how many bytes to move.  If the packet is
1116	 * greater or equal to the snapshot length, transfer that
1117	 * much.  Otherwise, transfer the whole packet (unless
1118	 * we hit the buffer size limit).
1119	 */
1120	totlen = hdrlen + min(snaplen, pktlen);
1121	if (totlen > d->bd_bufsize)
1122		totlen = d->bd_bufsize;
1123
1124	/*
1125	 * Round up the end of the previous packet to the next longword.
1126	 */
1127	curlen = BPF_WORDALIGN(d->bd_slen);
1128	if (curlen + totlen > d->bd_bufsize) {
1129		/*
1130		 * This packet will overflow the storage buffer.
1131		 * Rotate the buffers if we can, then wakeup any
1132		 * pending reads.
1133		 */
1134		if (d->bd_fbuf == 0) {
1135			/*
1136			 * We haven't completed the previous read yet,
1137			 * so drop the packet.
1138			 */
1139			++d->bd_dcount;
1140			return;
1141		}
1142		ROTATE_BUFFERS(d);
1143		bpf_wakeup(d);
1144		curlen = 0;
1145	}
1146	else if (d->bd_immediate)
1147		/*
1148		 * Immediate mode is set.  A packet arrived so any
1149		 * reads should be woken up.
1150		 */
1151		bpf_wakeup(d);
1152
1153	/*
1154	 * Append the bpf header.
1155	 */
1156	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1157#if BSD >= 199103
1158	microtime(&hp->bh_tstamp);
1159#elif defined(sun)
1160	uniqtime(&hp->bh_tstamp);
1161#else
1162	hp->bh_tstamp = time;
1163#endif
1164	hp->bh_datalen = pktlen;
1165	hp->bh_hdrlen = hdrlen;
1166	/*
1167	 * Copy the packet data into the store buffer and update its length.
1168	 */
1169	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1170	d->bd_slen = curlen + totlen;
1171}
1172
1173/*
1174 * Initialize all nonzero fields of a descriptor.
1175 */
1176static int
1177bpf_allocbufs(d)
1178	register struct bpf_d *d;
1179{
1180	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1181	if (d->bd_fbuf == 0)
1182		return (ENOBUFS);
1183
1184	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1185	if (d->bd_sbuf == 0) {
1186		free(d->bd_fbuf, M_DEVBUF);
1187		return (ENOBUFS);
1188	}
1189	d->bd_slen = 0;
1190	d->bd_hlen = 0;
1191	return (0);
1192}
1193
1194/*
1195 * Free buffers currently in use by a descriptor.
1196 * Called on close.
1197 */
1198static void
1199bpf_freed(d)
1200	register struct bpf_d *d;
1201{
1202	/*
1203	 * We don't need to lock out interrupts since this descriptor has
1204	 * been detached from its interface and it yet hasn't been marked
1205	 * free.
1206	 */
1207	if (d->bd_sbuf != 0) {
1208		free(d->bd_sbuf, M_DEVBUF);
1209		if (d->bd_hbuf != 0)
1210			free(d->bd_hbuf, M_DEVBUF);
1211		if (d->bd_fbuf != 0)
1212			free(d->bd_fbuf, M_DEVBUF);
1213	}
1214	if (d->bd_filter)
1215		free((caddr_t)d->bd_filter, M_DEVBUF);
1216
1217	D_MARKFREE(d);
1218}
1219
1220/*
1221 * Attach an interface to bpf.  driverp is a pointer to a (struct bpf_if *)
1222 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1223 * size of the link header (variable length headers not yet supported).
1224 */
1225void
1226bpfattach(ifp, dlt, hdrlen)
1227	struct ifnet *ifp;
1228	u_int dlt, hdrlen;
1229{
1230	struct bpf_if *bp;
1231	int i;
1232	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
1233	if (bp == 0)
1234		panic("bpfattach");
1235
1236	bp->bif_dlist = 0;
1237	bp->bif_ifp = ifp;
1238	bp->bif_dlt = dlt;
1239
1240	bp->bif_next = bpf_iflist;
1241	bpf_iflist = bp;
1242
1243	bp->bif_ifp->if_bpf = 0;
1244
1245	/*
1246	 * Compute the length of the bpf header.  This is not necessarily
1247	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1248	 * that the network layer header begins on a longword boundary (for
1249	 * performance reasons and to alleviate alignment restrictions).
1250	 */
1251	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1252
1253	/*
1254	 * Mark all the descriptors free if this hasn't been done.
1255	 */
1256	if (!bpf_dtab_init) {
1257		for (i = 0; i < NBPFILTER; ++i)
1258			D_MARKFREE(&bpf_dtab[i]);
1259		bpf_dtab_init = 1;
1260	}
1261
1262	if (bootverbose)
1263		printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1264}
1265
1266#ifdef DEVFS
1267static	void *bpf_devfs_token[NBPFILTER];
1268#endif
1269
1270static bpf_devsw_installed = 0;
1271
1272static void bpf_drvinit __P((void *unused));
1273static void
1274bpf_drvinit(unused)
1275	void *unused;
1276{
1277	dev_t dev;
1278#ifdef DEVFS
1279	int i;
1280#endif
1281
1282	if( ! bpf_devsw_installed ) {
1283		dev = makedev(CDEV_MAJOR, 0);
1284		cdevsw_add(&dev,&bpf_cdevsw, NULL);
1285		bpf_devsw_installed = 1;
1286#ifdef DEVFS
1287
1288		for ( i = 0 ; i < NBPFILTER ; i++ ) {
1289			bpf_devfs_token[i] =
1290				devfs_add_devswf(&bpf_cdevsw, i, DV_CHR, 0, 0,
1291						 0600, "bpf%d", i);
1292		}
1293#endif
1294    	}
1295}
1296
1297SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1298
1299#endif
1300