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