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