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