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