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