xref: /netbsd-src/sys/net/bpf.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: bpf.c,v 1.89 2004/01/22 00:32:41 jonathan Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from the Stanford/CMU enet packet filter,
8  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10  * Berkeley Laboratory.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)bpf.c	8.4 (Berkeley) 1/9/95
37  * static char rcsid[] =
38  * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp ";
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.89 2004/01/22 00:32:41 jonathan Exp $");
43 
44 #include "bpfilter.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/buf.h>
50 #include <sys/time.h>
51 #include <sys/proc.h>
52 #include <sys/user.h>
53 #include <sys/ioctl.h>
54 #include <sys/conf.h>
55 #include <sys/vnode.h>
56 
57 #include <sys/file.h>
58 #include <sys/tty.h>
59 #include <sys/uio.h>
60 
61 #include <sys/protosw.h>
62 #include <sys/socket.h>
63 #include <sys/errno.h>
64 #include <sys/kernel.h>
65 #include <sys/poll.h>
66 #include <sys/sysctl.h>
67 
68 #include <net/if.h>
69 
70 #include <net/bpf.h>
71 #include <net/bpfdesc.h>
72 
73 #include <net/if_arc.h>
74 #include <net/if_ether.h>
75 
76 #include <netinet/in.h>
77 #include <netinet/if_inarp.h>
78 
79 #if defined(_KERNEL_OPT)
80 #include "opt_bpf.h"
81 #endif
82 
83 #ifndef BPF_BUFSIZE
84 /*
85  * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet
86  * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k).
87  */
88 # define BPF_BUFSIZE 32768
89 #endif
90 
91 #define PRINET  26			/* interruptible */
92 
93 /*
94  * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able.
95  * XXX the default values should be computed dynamically based
96  * on available memory size and available mbuf clusters.
97  */
98 int bpf_bufsize = BPF_BUFSIZE;
99 int bpf_maxbufsize = BPF_DFLTBUFSIZE;	/* XXX set dynamically, see above */
100 
101 /*
102  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
103  *  bpf_dtab holds the descriptors, indexed by minor device #
104  */
105 struct bpf_if	*bpf_iflist;
106 struct bpf_d	bpf_dtab[NBPFILTER];
107 
108 static int	bpf_allocbufs __P((struct bpf_d *));
109 static void	bpf_freed __P((struct bpf_d *));
110 static void	bpf_ifname __P((struct ifnet *, struct ifreq *));
111 static void	*bpf_mcpy __P((void *, const void *, size_t));
112 static int	bpf_movein __P((struct uio *, int, int,
113 			        struct mbuf **, struct sockaddr *));
114 static void	bpf_attachd __P((struct bpf_d *, struct bpf_if *));
115 static void	bpf_detachd __P((struct bpf_d *));
116 static int	bpf_setif __P((struct bpf_d *, struct ifreq *));
117 static __inline void
118 		bpf_wakeup __P((struct bpf_d *));
119 static void	catchpacket __P((struct bpf_d *, u_char *, u_int, u_int,
120 				 void *(*)(void *, const void *, size_t)));
121 static void	reset_d __P((struct bpf_d *));
122 static int	bpf_getdltlist __P((struct bpf_d *, struct bpf_dltlist *));
123 static int	bpf_setdlt __P((struct bpf_d *, u_int));
124 
125 dev_type_open(bpfopen);
126 dev_type_close(bpfclose);
127 dev_type_read(bpfread);
128 dev_type_write(bpfwrite);
129 dev_type_ioctl(bpfioctl);
130 dev_type_poll(bpfpoll);
131 dev_type_kqfilter(bpfkqfilter);
132 
133 const struct cdevsw bpf_cdevsw = {
134 	bpfopen, bpfclose, bpfread, bpfwrite, bpfioctl,
135 	nostop, notty, bpfpoll, nommap, bpfkqfilter,
136 };
137 
138 static int
139 bpf_movein(uio, linktype, mtu, mp, sockp)
140 	struct uio *uio;
141 	int linktype;
142 	int mtu;
143 	struct mbuf **mp;
144 	struct sockaddr *sockp;
145 {
146 	struct mbuf *m;
147 	int error;
148 	int len;
149 	int hlen;
150 	int align;
151 
152 	/*
153 	 * Build a sockaddr based on the data link layer type.
154 	 * We do this at this level because the ethernet header
155 	 * is copied directly into the data field of the sockaddr.
156 	 * In the case of SLIP, there is no header and the packet
157 	 * is forwarded as is.
158 	 * Also, we are careful to leave room at the front of the mbuf
159 	 * for the link level header.
160 	 */
161 	switch (linktype) {
162 
163 	case DLT_SLIP:
164 		sockp->sa_family = AF_INET;
165 		hlen = 0;
166 		align = 0;
167 		break;
168 
169 	case DLT_PPP:
170 		sockp->sa_family = AF_UNSPEC;
171 		hlen = 0;
172 		align = 0;
173 		break;
174 
175 	case DLT_EN10MB:
176 		sockp->sa_family = AF_UNSPEC;
177 		/* XXX Would MAXLINKHDR be better? */
178  		/* 6(dst)+6(src)+2(type) */
179 		hlen = sizeof(struct ether_header);
180 		align = 2;
181 		break;
182 
183 	case DLT_ARCNET:
184 		sockp->sa_family = AF_UNSPEC;
185 		hlen = ARC_HDRLEN;
186 		align = 5;
187 		break;
188 
189 	case DLT_FDDI:
190 		sockp->sa_family = AF_LINK;
191 		/* XXX 4(FORMAC)+6(dst)+6(src) */
192 		hlen = 16;
193 		align = 0;
194 		break;
195 
196 	case DLT_ECONET:
197 		sockp->sa_family = AF_UNSPEC;
198 		hlen = 6;
199 		align = 2;
200 		break;
201 
202 	case DLT_NULL:
203 		sockp->sa_family = AF_UNSPEC;
204 		hlen = 0;
205 		align = 0;
206 		break;
207 
208 	default:
209 		return (EIO);
210 	}
211 
212 	len = uio->uio_resid;
213 	/*
214 	 * If there aren't enough bytes for a link level header or the
215 	 * packet length exceeds the interface mtu, return an error.
216 	 */
217 	if (len < hlen || len - hlen > mtu)
218 		return (EMSGSIZE);
219 
220 	/*
221 	 * XXX Avoid complicated buffer chaining ---
222 	 * bail if it won't fit in a single mbuf.
223 	 * (Take into account possible alignment bytes)
224 	 */
225 	if ((unsigned)len > MCLBYTES - align)
226 		return (EIO);
227 
228 	m = m_gethdr(M_WAIT, MT_DATA);
229 	m->m_pkthdr.rcvif = 0;
230 	m->m_pkthdr.len = len - hlen;
231 	if (len > MHLEN - align) {
232 		m_clget(m, M_WAIT);
233 		if ((m->m_flags & M_EXT) == 0) {
234 			error = ENOBUFS;
235 			goto bad;
236 		}
237 	}
238 
239 	/* Insure the data is properly aligned */
240 	if (align > 0) {
241 		m->m_data += align;
242 		m->m_len -= align;
243 	}
244 
245 	error = uiomove(mtod(m, caddr_t), len, uio);
246 	if (error)
247 		goto bad;
248 	if (hlen != 0) {
249 		memcpy(sockp->sa_data, mtod(m, caddr_t), hlen);
250 		m->m_data += hlen; /* XXX */
251 		len -= hlen;
252 	}
253 	m->m_len = len;
254 	*mp = m;
255 	return (0);
256 
257 bad:
258 	m_freem(m);
259 	return (error);
260 }
261 
262 /*
263  * Attach file to the bpf interface, i.e. make d listen on bp.
264  * Must be called at splnet.
265  */
266 static void
267 bpf_attachd(d, bp)
268 	struct bpf_d *d;
269 	struct bpf_if *bp;
270 {
271 	/*
272 	 * Point d at bp, and add d to the interface's list of listeners.
273 	 * Finally, point the driver's bpf cookie at the interface so
274 	 * it will divert packets to bpf.
275 	 */
276 	d->bd_bif = bp;
277 	d->bd_next = bp->bif_dlist;
278 	bp->bif_dlist = d;
279 
280 	*bp->bif_driverp = bp;
281 }
282 
283 /*
284  * Detach a file from its interface.
285  */
286 static void
287 bpf_detachd(d)
288 	struct bpf_d *d;
289 {
290 	struct bpf_d **p;
291 	struct bpf_if *bp;
292 
293 	bp = d->bd_bif;
294 	/*
295 	 * Check if this descriptor had requested promiscuous mode.
296 	 * If so, turn it off.
297 	 */
298 	if (d->bd_promisc) {
299 		int error;
300 
301 		d->bd_promisc = 0;
302 		/*
303 		 * Take device out of promiscuous mode.  Since we were
304 		 * able to enter promiscuous mode, we should be able
305 		 * to turn it off.  But we can get an error if
306 		 * the interface was configured down, so only panic
307 		 * if we don't get an unexpected error.
308 		 */
309   		error = ifpromisc(bp->bif_ifp, 0);
310 		if (error && error != EINVAL)
311 			panic("bpf: ifpromisc failed");
312 	}
313 	/* Remove d from the interface's descriptor list. */
314 	p = &bp->bif_dlist;
315 	while (*p != d) {
316 		p = &(*p)->bd_next;
317 		if (*p == 0)
318 			panic("bpf_detachd: descriptor not in list");
319 	}
320 	*p = (*p)->bd_next;
321 	if (bp->bif_dlist == 0)
322 		/*
323 		 * Let the driver know that there are no more listeners.
324 		 */
325 		*d->bd_bif->bif_driverp = 0;
326 	d->bd_bif = 0;
327 }
328 
329 
330 /*
331  * Mark a descriptor free by making it point to itself.
332  * This is probably cheaper than marking with a constant since
333  * the address should be in a register anyway.
334  */
335 #define D_ISFREE(d) ((d) == (d)->bd_next)
336 #define D_MARKFREE(d) ((d)->bd_next = (d))
337 #define D_MARKUSED(d) ((d)->bd_next = 0)
338 
339 /*
340  * bpfilterattach() is called at boot time.
341  */
342 /* ARGSUSED */
343 void
344 bpfilterattach(n)
345 	int n;
346 {
347 	int i;
348 	/*
349 	 * Mark all the descriptors free.
350 	 */
351 	for (i = 0; i < NBPFILTER; ++i)
352 		D_MARKFREE(&bpf_dtab[i]);
353 
354 }
355 
356 /*
357  * Open ethernet device.  Returns ENXIO for illegal minor device number,
358  * EBUSY if file is open by another process.
359  */
360 /* ARGSUSED */
361 int
362 bpfopen(dev, flag, mode, p)
363 	dev_t dev;
364 	int flag;
365 	int mode;
366 	struct proc *p;
367 {
368 	struct bpf_d *d;
369 
370 	if (minor(dev) >= NBPFILTER)
371 		return (ENXIO);
372 	/*
373 	 * Each minor can be opened by only one process.  If the requested
374 	 * minor is in use, return EBUSY.
375 	 */
376 	d = &bpf_dtab[minor(dev)];
377 	if (!D_ISFREE(d))
378 		return (EBUSY);
379 
380 	/* Mark "free" and do most initialization. */
381 	memset((char *)d, 0, sizeof(*d));
382 	d->bd_bufsize = bpf_bufsize;
383 
384 	return (0);
385 }
386 
387 /*
388  * Close the descriptor by detaching it from its interface,
389  * deallocating its buffers, and marking it free.
390  */
391 /* ARGSUSED */
392 int
393 bpfclose(dev, flag, mode, p)
394 	dev_t dev;
395 	int flag;
396 	int mode;
397 	struct proc *p;
398 {
399 	struct bpf_d *d = &bpf_dtab[minor(dev)];
400 	int s;
401 
402 	s = splnet();
403 	if (d->bd_bif)
404 		bpf_detachd(d);
405 	splx(s);
406 	bpf_freed(d);
407 
408 	return (0);
409 }
410 
411 /*
412  * Rotate the packet buffers in descriptor d.  Move the store buffer
413  * into the hold slot, and the free buffer into the store slot.
414  * Zero the length of the new store buffer.
415  */
416 #define ROTATE_BUFFERS(d) \
417 	(d)->bd_hbuf = (d)->bd_sbuf; \
418 	(d)->bd_hlen = (d)->bd_slen; \
419 	(d)->bd_sbuf = (d)->bd_fbuf; \
420 	(d)->bd_slen = 0; \
421 	(d)->bd_fbuf = 0;
422 /*
423  *  bpfread - read next chunk of packets from buffers
424  */
425 int
426 bpfread(dev, uio, ioflag)
427 	dev_t dev;
428 	struct uio *uio;
429 	int ioflag;
430 {
431 	struct bpf_d *d = &bpf_dtab[minor(dev)];
432 	int error;
433 	int s;
434 
435 	/*
436 	 * Restrict application to use a buffer the same size as
437 	 * as kernel buffers.
438 	 */
439 	if (uio->uio_resid != d->bd_bufsize)
440 		return (EINVAL);
441 
442 	s = splnet();
443 	/*
444 	 * If the hold buffer is empty, then do a timed sleep, which
445 	 * ends when the timeout expires or when enough packets
446 	 * have arrived to fill the store buffer.
447 	 */
448 	while (d->bd_hbuf == 0) {
449 		if (d->bd_immediate) {
450 			if (d->bd_slen == 0) {
451 				splx(s);
452 				return (EWOULDBLOCK);
453 			}
454 			/*
455 			 * A packet(s) either arrived since the previous
456 			 * read or arrived while we were asleep.
457 			 * Rotate the buffers and return what's here.
458 			 */
459 			ROTATE_BUFFERS(d);
460 			break;
461 		}
462 		if (d->bd_rtout != -1)
463 			error = tsleep((caddr_t)d, PRINET|PCATCH, "bpf",
464 					  d->bd_rtout);
465 		else {
466 			if (d->bd_rtout == -1) {
467 				/* User requested non-blocking I/O */
468 				error = EWOULDBLOCK;
469 			} else
470 				error = 0;
471 		}
472 		if (error == EINTR || error == ERESTART) {
473 			splx(s);
474 			return (error);
475 		}
476 		if (error == EWOULDBLOCK) {
477 			/*
478 			 * On a timeout, return what's in the buffer,
479 			 * which may be nothing.  If there is something
480 			 * in the store buffer, we can rotate the buffers.
481 			 */
482 			if (d->bd_hbuf)
483 				/*
484 				 * We filled up the buffer in between
485 				 * getting the timeout and arriving
486 				 * here, so we don't need to rotate.
487 				 */
488 				break;
489 
490 			if (d->bd_slen == 0) {
491 				splx(s);
492 				return (0);
493 			}
494 			ROTATE_BUFFERS(d);
495 			break;
496 		}
497 		if (error != 0)
498 			goto done;
499 	}
500 	/*
501 	 * At this point, we know we have something in the hold slot.
502 	 */
503 	splx(s);
504 
505 	/*
506 	 * Move data from hold buffer into user space.
507 	 * We know the entire buffer is transferred since
508 	 * we checked above that the read buffer is bpf_bufsize bytes.
509 	 */
510 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
511 
512 	s = splnet();
513 	d->bd_fbuf = d->bd_hbuf;
514 	d->bd_hbuf = 0;
515 	d->bd_hlen = 0;
516 done:
517 	splx(s);
518 	return (error);
519 }
520 
521 
522 /*
523  * If there are processes sleeping on this descriptor, wake them up.
524  */
525 static __inline void
526 bpf_wakeup(d)
527 	struct bpf_d *d;
528 {
529 	wakeup((caddr_t)d);
530 	if (d->bd_async)
531 		fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL);
532 
533 	selnotify(&d->bd_sel, 0);
534 	/* XXX */
535 	d->bd_sel.sel_pid = 0;
536 }
537 
538 int
539 bpfwrite(dev, uio, ioflag)
540 	dev_t dev;
541 	struct uio *uio;
542 	int ioflag;
543 {
544 	struct bpf_d *d = &bpf_dtab[minor(dev)];
545 	struct ifnet *ifp;
546 	struct mbuf *m;
547 	int error, s;
548 	static struct sockaddr_storage dst;
549 
550 	if (d->bd_bif == 0)
551 		return (ENXIO);
552 
553 	ifp = d->bd_bif->bif_ifp;
554 
555 	if (uio->uio_resid == 0)
556 		return (0);
557 
558 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m,
559 		(struct sockaddr *) &dst);
560 	if (error)
561 		return (error);
562 
563 	if (m->m_pkthdr.len > ifp->if_mtu)
564 		return (EMSGSIZE);
565 
566 	if (d->bd_hdrcmplt)
567 		dst.ss_family = pseudo_AF_HDRCMPLT;
568 
569 	s = splsoftnet();
570 	error = (*ifp->if_output)(ifp, m, (struct sockaddr *) &dst, NULL);
571 	splx(s);
572 	/*
573 	 * The driver frees the mbuf.
574 	 */
575 	return (error);
576 }
577 
578 /*
579  * Reset a descriptor by flushing its packet buffer and clearing the
580  * receive and drop counts.  Should be called at splnet.
581  */
582 static void
583 reset_d(d)
584 	struct bpf_d *d;
585 {
586 	if (d->bd_hbuf) {
587 		/* Free the hold buffer. */
588 		d->bd_fbuf = d->bd_hbuf;
589 		d->bd_hbuf = 0;
590 	}
591 	d->bd_slen = 0;
592 	d->bd_hlen = 0;
593 	d->bd_rcount = 0;
594 	d->bd_dcount = 0;
595 }
596 
597 #ifdef BPF_KERN_FILTER
598 extern struct bpf_insn *bpf_tcp_filter;
599 extern struct bpf_insn *bpf_udp_filter;
600 #endif
601 
602 /*
603  *  FIONREAD		Check for read packet available.
604  *  BIOCGBLEN		Get buffer len [for read()].
605  *  BIOCSETF		Set ethernet read filter.
606  *  BIOCFLUSH		Flush read packet buffer.
607  *  BIOCPROMISC		Put interface into promiscuous mode.
608  *  BIOCGDLT		Get link layer type.
609  *  BIOCGETIF		Get interface name.
610  *  BIOCSETIF		Set interface.
611  *  BIOCSRTIMEOUT	Set read timeout.
612  *  BIOCGRTIMEOUT	Get read timeout.
613  *  BIOCGSTATS		Get packet stats.
614  *  BIOCIMMEDIATE	Set immediate mode.
615  *  BIOCVERSION		Get filter language version.
616  *  BIOGHDRCMPLT	Get "header already complete" flag.
617  *  BIOSHDRCMPLT	Set "header already complete" flag.
618  */
619 /* ARGSUSED */
620 int
621 bpfioctl(dev, cmd, addr, flag, p)
622 	dev_t dev;
623 	u_long cmd;
624 	caddr_t addr;
625 	int flag;
626 	struct proc *p;
627 {
628 	struct bpf_d *d = &bpf_dtab[minor(dev)];
629 	int s, error = 0;
630 #ifdef BPF_KERN_FILTER
631 	struct bpf_insn **p;
632 #endif
633 
634 	switch (cmd) {
635 
636 	default:
637 		error = EINVAL;
638 		break;
639 
640 	/*
641 	 * Check for read packet available.
642 	 */
643 	case FIONREAD:
644 		{
645 			int n;
646 
647 			s = splnet();
648 			n = d->bd_slen;
649 			if (d->bd_hbuf)
650 				n += d->bd_hlen;
651 			splx(s);
652 
653 			*(int *)addr = n;
654 			break;
655 		}
656 
657 	/*
658 	 * Get buffer len [for read()].
659 	 */
660 	case BIOCGBLEN:
661 		*(u_int *)addr = d->bd_bufsize;
662 		break;
663 
664 	/*
665 	 * Set buffer length.
666 	 */
667 	case BIOCSBLEN:
668 		if (d->bd_bif != 0)
669 			error = EINVAL;
670 		else {
671 			u_int size = *(u_int *)addr;
672 
673 			if (size > bpf_maxbufsize)
674 				*(u_int *)addr = size = bpf_maxbufsize;
675 			else if (size < BPF_MINBUFSIZE)
676 				*(u_int *)addr = size = BPF_MINBUFSIZE;
677 			d->bd_bufsize = size;
678 		}
679 		break;
680 
681 	/*
682 	 * Set link layer read filter.
683 	 */
684 	case BIOCSETF:
685 		error = bpf_setf(d, (struct bpf_program *)addr);
686 		break;
687 
688 #ifdef BPF_KERN_FILTER
689 	/*
690 	 * Set TCP or UDP reject filter.
691 	 */
692 	case BIOCSTCPF:
693 	case BIOCSUDPF:
694 		if (!suser()) {
695 			error = EPERM;
696 			break;
697 		}
698 
699 		/* Validate and store filter */
700 		error = bpf_setf(d, (struct bpf_program *)addr);
701 
702 		/* Free possible old filter */
703 		if (cmd == BIOCSTCPF)
704 			p = &bpf_tcp_filter;
705 		else
706 			p = &bpf_udp_filter;
707 		if (*p != NULL)
708 			free((caddr_t)*p, M_DEVBUF);
709 
710 		/* Steal new filter (noop if error) */
711 		s = splnet();
712 		*p = d->bd_filter;
713 		d->bd_filter = NULL;
714 		splx(s);
715 		break;
716 #endif
717 
718 	/*
719 	 * Flush read packet buffer.
720 	 */
721 	case BIOCFLUSH:
722 		s = splnet();
723 		reset_d(d);
724 		splx(s);
725 		break;
726 
727 	/*
728 	 * Put interface into promiscuous mode.
729 	 */
730 	case BIOCPROMISC:
731 		if (d->bd_bif == 0) {
732 			/*
733 			 * No interface attached yet.
734 			 */
735 			error = EINVAL;
736 			break;
737 		}
738 		s = splnet();
739 		if (d->bd_promisc == 0) {
740 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
741 			if (error == 0)
742 				d->bd_promisc = 1;
743 		}
744 		splx(s);
745 		break;
746 
747 	/*
748 	 * Get device parameters.
749 	 */
750 	case BIOCGDLT:
751 		if (d->bd_bif == 0)
752 			error = EINVAL;
753 		else
754 			*(u_int *)addr = d->bd_bif->bif_dlt;
755 		break;
756 
757 	/*
758 	 * Get a list of supported device parameters.
759 	 */
760 	case BIOCGDLTLIST:
761 		if (d->bd_bif == 0)
762 			error = EINVAL;
763 		else
764 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
765 		break;
766 
767 	/*
768 	 * Set device parameters.
769 	 */
770 	case BIOCSDLT:
771 		if (d->bd_bif == 0)
772 			error = EINVAL;
773 		else
774 			error = bpf_setdlt(d, *(u_int *)addr);
775 		break;
776 
777 	/*
778 	 * Set interface name.
779 	 */
780 	case BIOCGETIF:
781 		if (d->bd_bif == 0)
782 			error = EINVAL;
783 		else
784 			bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
785 		break;
786 
787 	/*
788 	 * Set interface.
789 	 */
790 	case BIOCSETIF:
791 		error = bpf_setif(d, (struct ifreq *)addr);
792 		break;
793 
794 	/*
795 	 * Set read timeout.
796 	 */
797 	case BIOCSRTIMEOUT:
798 		{
799 			struct timeval *tv = (struct timeval *)addr;
800 
801 			/* Compute number of ticks. */
802 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
803 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
804 				d->bd_rtout = 1;
805 			break;
806 		}
807 
808 	/*
809 	 * Get read timeout.
810 	 */
811 	case BIOCGRTIMEOUT:
812 		{
813 			struct timeval *tv = (struct timeval *)addr;
814 
815 			tv->tv_sec = d->bd_rtout / hz;
816 			tv->tv_usec = (d->bd_rtout % hz) * tick;
817 			break;
818 		}
819 
820 	/*
821 	 * Get packet stats.
822 	 */
823 	case BIOCGSTATS:
824 		{
825 			struct bpf_stat *bs = (struct bpf_stat *)addr;
826 
827 			bs->bs_recv = d->bd_rcount;
828 			bs->bs_drop = d->bd_dcount;
829 			break;
830 		}
831 
832 	/*
833 	 * Set immediate mode.
834 	 */
835 	case BIOCIMMEDIATE:
836 		d->bd_immediate = *(u_int *)addr;
837 		break;
838 
839 	case BIOCVERSION:
840 		{
841 			struct bpf_version *bv = (struct bpf_version *)addr;
842 
843 			bv->bv_major = BPF_MAJOR_VERSION;
844 			bv->bv_minor = BPF_MINOR_VERSION;
845 			break;
846 		}
847 
848 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
849 		*(u_int *)addr = d->bd_hdrcmplt;
850 		break;
851 
852 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
853 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
854 		break;
855 
856 	case FIONBIO:		/* Non-blocking I/O */
857 		if (*(int *)addr)
858 			d->bd_rtout = -1;
859 		else
860 			d->bd_rtout = 0;
861 		break;
862 
863 	case FIOASYNC:		/* Send signal on receive packets */
864 		d->bd_async = *(int *)addr;
865 		break;
866 
867 	case TIOCSPGRP:		/* Process or group to send signals to */
868 	case FIOSETOWN:
869 		error = fsetown(p, &d->bd_pgid, cmd, addr);
870 		break;
871 
872 	case TIOCGPGRP:
873 	case FIOGETOWN:
874 		error = fgetown(p, d->bd_pgid, cmd, addr);
875 		break;
876 	}
877 	return (error);
878 }
879 
880 /*
881  * Set d's packet filter program to fp.  If this file already has a filter,
882  * free it and replace it.  Returns EINVAL for bogus requests.
883  */
884 int
885 bpf_setf(d, fp)
886 	struct bpf_d *d;
887 	struct bpf_program *fp;
888 {
889 	struct bpf_insn *fcode, *old;
890 	u_int flen, size;
891 	int s;
892 
893 	old = d->bd_filter;
894 	if (fp->bf_insns == 0) {
895 		if (fp->bf_len != 0)
896 			return (EINVAL);
897 		s = splnet();
898 		d->bd_filter = 0;
899 		reset_d(d);
900 		splx(s);
901 		if (old != 0)
902 			free((caddr_t)old, M_DEVBUF);
903 		return (0);
904 	}
905 	flen = fp->bf_len;
906 	if (flen > BPF_MAXINSNS)
907 		return (EINVAL);
908 
909 	size = flen * sizeof(*fp->bf_insns);
910 	fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
911 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
912 	    bpf_validate(fcode, (int)flen)) {
913 		s = splnet();
914 		d->bd_filter = fcode;
915 		reset_d(d);
916 		splx(s);
917 		if (old != 0)
918 			free((caddr_t)old, M_DEVBUF);
919 
920 		return (0);
921 	}
922 	free((caddr_t)fcode, M_DEVBUF);
923 	return (EINVAL);
924 }
925 
926 /*
927  * Detach a file from its current interface (if attached at all) and attach
928  * to the interface indicated by the name stored in ifr.
929  * Return an errno or 0.
930  */
931 static int
932 bpf_setif(d, ifr)
933 	struct bpf_d *d;
934 	struct ifreq *ifr;
935 {
936 	struct bpf_if *bp;
937 	char *cp;
938 	int unit_seen, i, s, error;
939 
940 	/*
941 	 * Make sure the provided name has a unit number, and default
942 	 * it to '0' if not specified.
943 	 * XXX This is ugly ... do this differently?
944 	 */
945 	unit_seen = 0;
946 	cp = ifr->ifr_name;
947 	cp[sizeof(ifr->ifr_name) - 1] = '\0';	/* sanity */
948 	while (*cp++)
949 		if (*cp >= '0' && *cp <= '9')
950 			unit_seen = 1;
951 	if (!unit_seen) {
952 		/* Make sure to leave room for the '\0'. */
953 		for (i = 0; i < (IFNAMSIZ - 1); ++i) {
954 			if ((ifr->ifr_name[i] >= 'a' &&
955 			     ifr->ifr_name[i] <= 'z') ||
956 			    (ifr->ifr_name[i] >= 'A' &&
957 			     ifr->ifr_name[i] <= 'Z'))
958 				continue;
959 			ifr->ifr_name[i] = '0';
960 		}
961 	}
962 
963 	/*
964 	 * Look through attached interfaces for the named one.
965 	 */
966 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
967 		struct ifnet *ifp = bp->bif_ifp;
968 
969 		if (ifp == 0 ||
970 		    strcmp(ifp->if_xname, ifr->ifr_name) != 0)
971 			continue;
972 		/* skip additional entry */
973 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
974 			continue;
975 		/*
976 		 * We found the requested interface.
977 		 * If it's not up, return an error.
978 		 * Allocate the packet buffers if we need to.
979 		 * If we're already attached to requested interface,
980 		 * just flush the buffer.
981 		 */
982 		if ((ifp->if_flags & IFF_UP) == 0)
983 			return (ENETDOWN);
984 
985 		if (d->bd_sbuf == 0) {
986 			error = bpf_allocbufs(d);
987 			if (error != 0)
988 				return (error);
989 		}
990 		s = splnet();
991 		if (bp != d->bd_bif) {
992 			if (d->bd_bif)
993 				/*
994 				 * Detach if attached to something else.
995 				 */
996 				bpf_detachd(d);
997 
998 			bpf_attachd(d, bp);
999 		}
1000 		reset_d(d);
1001 		splx(s);
1002 		return (0);
1003 	}
1004 	/* Not found. */
1005 	return (ENXIO);
1006 }
1007 
1008 /*
1009  * Copy the interface name to the ifreq.
1010  */
1011 static void
1012 bpf_ifname(ifp, ifr)
1013 	struct ifnet *ifp;
1014 	struct ifreq *ifr;
1015 {
1016 
1017 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
1018 }
1019 
1020 /*
1021  * Support for poll() system call
1022  *
1023  * Return true iff the specific operation will not block indefinitely - with
1024  * the assumption that it is safe to positively acknowledge a request for the
1025  * ability to write to the BPF device.
1026  * Otherwise, return false but make a note that a selwakeup() must be done.
1027  */
1028 int
1029 bpfpoll(dev, events, p)
1030 	dev_t dev;
1031 	int events;
1032 	struct proc *p;
1033 {
1034 	struct bpf_d *d = &bpf_dtab[minor(dev)];
1035 	int s = splnet();
1036 	int revents;
1037 
1038 	revents = events & (POLLOUT | POLLWRNORM);
1039 	if (events & (POLLIN | POLLRDNORM)) {
1040 		/*
1041 		 * An imitation of the FIONREAD ioctl code.
1042 		 */
1043 		if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1044 			revents |= events & (POLLIN | POLLRDNORM);
1045 		else
1046 			selrecord(p, &d->bd_sel);
1047 	}
1048 
1049 	splx(s);
1050 	return (revents);
1051 }
1052 
1053 static void
1054 filt_bpfrdetach(struct knote *kn)
1055 {
1056 	struct bpf_d *d = kn->kn_hook;
1057 	int s;
1058 
1059 	s = splnet();
1060 	SLIST_REMOVE(&d->bd_sel.sel_klist, kn, knote, kn_selnext);
1061 	splx(s);
1062 }
1063 
1064 static int
1065 filt_bpfread(struct knote *kn, long hint)
1066 {
1067 	struct bpf_d *d = kn->kn_hook;
1068 
1069 	kn->kn_data = d->bd_hlen;
1070 	if (d->bd_immediate)
1071 		kn->kn_data += d->bd_slen;
1072 	return (kn->kn_data > 0);
1073 }
1074 
1075 static const struct filterops bpfread_filtops =
1076 	{ 1, NULL, filt_bpfrdetach, filt_bpfread };
1077 
1078 int
1079 bpfkqfilter(dev, kn)
1080 	dev_t dev;
1081 	struct knote *kn;
1082 {
1083 	struct bpf_d *d = &bpf_dtab[minor(dev)];
1084 	struct klist *klist;
1085 	int s;
1086 
1087 	switch (kn->kn_filter) {
1088 	case EVFILT_READ:
1089 		klist = &d->bd_sel.sel_klist;
1090 		kn->kn_fop = &bpfread_filtops;
1091 		break;
1092 
1093 	default:
1094 		return (1);
1095 	}
1096 
1097 	kn->kn_hook = d;
1098 
1099 	s = splnet();
1100 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1101 	splx(s);
1102 
1103 	return (0);
1104 }
1105 
1106 /*
1107  * Incoming linkage from device drivers.  Process the packet pkt, of length
1108  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1109  * by each process' filter, and if accepted, stashed into the corresponding
1110  * buffer.
1111  */
1112 void
1113 bpf_tap(arg, pkt, pktlen)
1114 	caddr_t arg;
1115 	u_char *pkt;
1116 	u_int pktlen;
1117 {
1118 	struct bpf_if *bp;
1119 	struct bpf_d *d;
1120 	u_int slen;
1121 	/*
1122 	 * Note that the ipl does not have to be raised at this point.
1123 	 * The only problem that could arise here is that if two different
1124 	 * interfaces shared any data.  This is not the case.
1125 	 */
1126 	bp = (struct bpf_if *)arg;
1127 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1128 		++d->bd_rcount;
1129 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1130 		if (slen != 0)
1131 			catchpacket(d, pkt, pktlen, slen, memcpy);
1132 	}
1133 }
1134 
1135 /*
1136  * Copy data from an mbuf chain into a buffer.  This code is derived
1137  * from m_copydata in sys/uipc_mbuf.c.
1138  */
1139 static void *
1140 bpf_mcpy(dst_arg, src_arg, len)
1141 	void *dst_arg;
1142 	const void *src_arg;
1143 	size_t len;
1144 {
1145 	const struct mbuf *m;
1146 	u_int count;
1147 	u_char *dst;
1148 
1149 	m = src_arg;
1150 	dst = dst_arg;
1151 	while (len > 0) {
1152 		if (m == 0)
1153 			panic("bpf_mcpy");
1154 		count = min(m->m_len, len);
1155 		memcpy((caddr_t)dst, mtod(m, caddr_t), count);
1156 		m = m->m_next;
1157 		dst += count;
1158 		len -= count;
1159 	}
1160 	return (dst_arg);
1161 }
1162 
1163 /*
1164  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1165  */
1166 void
1167 bpf_mtap(arg, m)
1168 	caddr_t arg;
1169 	struct mbuf *m;
1170 {
1171 	struct bpf_if *bp = (struct bpf_if *)arg;
1172 	struct bpf_d *d;
1173 	u_int pktlen, slen;
1174 	struct mbuf *m0;
1175 
1176 	pktlen = 0;
1177 	for (m0 = m; m0 != 0; m0 = m0->m_next)
1178 		pktlen += m0->m_len;
1179 
1180 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1181 		++d->bd_rcount;
1182 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1183 		if (slen != 0)
1184 			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcpy);
1185 	}
1186 }
1187 
1188 /*
1189  * Move the packet data from interface memory (pkt) into the
1190  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
1191  * otherwise 0.  "copy" is the routine called to do the actual data
1192  * transfer.  memcpy is passed in to copy contiguous chunks, while
1193  * bpf_mcpy is passed in to copy mbuf chains.  In the latter case,
1194  * pkt is really an mbuf.
1195  */
1196 static void
1197 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1198 	struct bpf_d *d;
1199 	u_char *pkt;
1200 	u_int pktlen, snaplen;
1201 	void *(*cpfn) __P((void *, const void *, size_t));
1202 {
1203 	struct bpf_hdr *hp;
1204 	int totlen, curlen;
1205 	int hdrlen = d->bd_bif->bif_hdrlen;
1206 	/*
1207 	 * Figure out how many bytes to move.  If the packet is
1208 	 * greater or equal to the snapshot length, transfer that
1209 	 * much.  Otherwise, transfer the whole packet (unless
1210 	 * we hit the buffer size limit).
1211 	 */
1212 	totlen = hdrlen + min(snaplen, pktlen);
1213 	if (totlen > d->bd_bufsize)
1214 		totlen = d->bd_bufsize;
1215 
1216 	/*
1217 	 * Round up the end of the previous packet to the next longword.
1218 	 */
1219 	curlen = BPF_WORDALIGN(d->bd_slen);
1220 	if (curlen + totlen > d->bd_bufsize) {
1221 		/*
1222 		 * This packet will overflow the storage buffer.
1223 		 * Rotate the buffers if we can, then wakeup any
1224 		 * pending reads.
1225 		 */
1226 		if (d->bd_fbuf == 0) {
1227 			/*
1228 			 * We haven't completed the previous read yet,
1229 			 * so drop the packet.
1230 			 */
1231 			++d->bd_dcount;
1232 			return;
1233 		}
1234 		ROTATE_BUFFERS(d);
1235 		bpf_wakeup(d);
1236 		curlen = 0;
1237 	}
1238 
1239 	/*
1240 	 * Append the bpf header.
1241 	 */
1242 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1243 	microtime(&hp->bh_tstamp);
1244 	hp->bh_datalen = pktlen;
1245 	hp->bh_hdrlen = hdrlen;
1246 	/*
1247 	 * Copy the packet data into the store buffer and update its length.
1248 	 */
1249 	(*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
1250 	d->bd_slen = curlen + totlen;
1251 
1252 	if (d->bd_immediate) {
1253 		/*
1254 		 * Immediate mode is set.  A packet arrived so any
1255 		 * reads should be woken up.
1256 		 */
1257 		bpf_wakeup(d);
1258 	}
1259 }
1260 
1261 /*
1262  * Initialize all nonzero fields of a descriptor.
1263  */
1264 static int
1265 bpf_allocbufs(d)
1266 	struct bpf_d *d;
1267 {
1268 
1269 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
1270 	if (!d->bd_fbuf)
1271 		return (ENOBUFS);
1272 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
1273 	if (!d->bd_sbuf) {
1274 		free(d->bd_fbuf, M_DEVBUF);
1275 		return (ENOBUFS);
1276 	}
1277 	d->bd_slen = 0;
1278 	d->bd_hlen = 0;
1279 	return (0);
1280 }
1281 
1282 /*
1283  * Free buffers currently in use by a descriptor.
1284  * Called on close.
1285  */
1286 static void
1287 bpf_freed(d)
1288 	struct bpf_d *d;
1289 {
1290 	/*
1291 	 * We don't need to lock out interrupts since this descriptor has
1292 	 * been detached from its interface and it yet hasn't been marked
1293 	 * free.
1294 	 */
1295 	if (d->bd_sbuf != 0) {
1296 		free(d->bd_sbuf, M_DEVBUF);
1297 		if (d->bd_hbuf != 0)
1298 			free(d->bd_hbuf, M_DEVBUF);
1299 		if (d->bd_fbuf != 0)
1300 			free(d->bd_fbuf, M_DEVBUF);
1301 	}
1302 	if (d->bd_filter)
1303 		free((caddr_t)d->bd_filter, M_DEVBUF);
1304 
1305 	D_MARKFREE(d);
1306 }
1307 
1308 /*
1309  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
1310  * fixed size of the link header (variable length headers not yet supported).
1311  */
1312 void
1313 bpfattach(ifp, dlt, hdrlen)
1314 	struct ifnet *ifp;
1315 	u_int dlt, hdrlen;
1316 {
1317 
1318 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1319 }
1320 
1321 /*
1322  * Attach additional dlt for a interface to bpf.  dlt is the link layer type;
1323  * hdrlen is the fixed size of the link header for the specified dlt
1324  * (variable length headers not yet supported).
1325  */
1326 void
1327 bpfattach2(ifp, dlt, hdrlen, driverp)
1328 	struct ifnet *ifp;
1329 	u_int dlt, hdrlen;
1330 	caddr_t *driverp;
1331 {
1332 	struct bpf_if *bp;
1333 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
1334 	if (bp == 0)
1335 		panic("bpfattach");
1336 
1337 	bp->bif_dlist = 0;
1338 	bp->bif_driverp = (struct bpf_if **)driverp;
1339 	bp->bif_ifp = ifp;
1340 	bp->bif_dlt = dlt;
1341 
1342 	bp->bif_next = bpf_iflist;
1343 	bpf_iflist = bp;
1344 
1345 	*bp->bif_driverp = 0;
1346 
1347 	/*
1348 	 * Compute the length of the bpf header.  This is not necessarily
1349 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1350 	 * that the network layer header begins on a longword boundary (for
1351 	 * performance reasons and to alleviate alignment restrictions).
1352 	 */
1353 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1354 
1355 #if 0
1356 	printf("bpf: %s attached\n", ifp->if_xname);
1357 #endif
1358 }
1359 
1360 /*
1361  * Remove an interface from bpf.
1362  */
1363 void
1364 bpfdetach(ifp)
1365 	struct ifnet *ifp;
1366 {
1367 	struct bpf_if *bp, **pbp;
1368 	struct bpf_d *d;
1369 	int i, s, cmaj;
1370 
1371 	/* locate the major number */
1372 	cmaj = cdevsw_lookup_major(&bpf_cdevsw);
1373 
1374 	/* Nuke the vnodes for any open instances */
1375 	for (i = 0; i < NBPFILTER; ++i) {
1376 		d = &bpf_dtab[i];
1377 		if (!D_ISFREE(d) && d->bd_bif != NULL &&
1378 		    d->bd_bif->bif_ifp == ifp) {
1379 			/*
1380 			 * Detach the descriptor from an interface now.
1381 			 * It will be free'ed later by close routine.
1382 			 */
1383 			s = splnet();
1384 			d->bd_promisc = 0;	/* we can't touch device. */
1385 			bpf_detachd(d);
1386 			splx(s);
1387 			vdevgone(cmaj, i, i, VCHR);
1388 		}
1389 	}
1390 
1391   again:
1392 	for (bp = bpf_iflist, pbp = &bpf_iflist;
1393 	     bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
1394 		if (bp->bif_ifp == ifp) {
1395 			*pbp = bp->bif_next;
1396 			free(bp, M_DEVBUF);
1397 			goto again;
1398 		}
1399 	}
1400 }
1401 
1402 /*
1403  * Change the data link type of a interface.
1404  */
1405 void
1406 bpf_change_type(ifp, dlt, hdrlen)
1407 	struct ifnet *ifp;
1408 	u_int dlt, hdrlen;
1409 {
1410 	struct bpf_if *bp;
1411 
1412 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1413 		if (bp->bif_driverp == (struct bpf_if **)&ifp->if_bpf)
1414 			break;
1415 	}
1416 	if (bp == NULL)
1417 		panic("bpf_change_type");
1418 
1419 	bp->bif_dlt = dlt;
1420 
1421 	/*
1422 	 * Compute the length of the bpf header.  This is not necessarily
1423 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1424 	 * that the network layer header begins on a longword boundary (for
1425 	 * performance reasons and to alleviate alignment restrictions).
1426 	 */
1427 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1428 }
1429 
1430 /*
1431  * Get a list of available data link type of the interface.
1432  */
1433 static int
1434 bpf_getdltlist(d, bfl)
1435 	struct bpf_d *d;
1436 	struct bpf_dltlist *bfl;
1437 {
1438 	int n, error;
1439 	struct ifnet *ifp;
1440 	struct bpf_if *bp;
1441 
1442 	ifp = d->bd_bif->bif_ifp;
1443 	n = 0;
1444 	error = 0;
1445 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1446 		if (bp->bif_ifp != ifp)
1447 			continue;
1448 		if (bfl->bfl_list != NULL) {
1449 			if (n >= bfl->bfl_len)
1450 				return ENOMEM;
1451 			error = copyout(&bp->bif_dlt,
1452 			    bfl->bfl_list + n, sizeof(u_int));
1453 		}
1454 		n++;
1455 	}
1456 	bfl->bfl_len = n;
1457 	return error;
1458 }
1459 
1460 /*
1461  * Set the data link type of a BPF instance.
1462  */
1463 static int
1464 bpf_setdlt(d, dlt)
1465 	struct bpf_d *d;
1466 	u_int dlt;
1467 {
1468 	int s, error, opromisc;
1469 	struct ifnet *ifp;
1470 	struct bpf_if *bp;
1471 
1472 	if (d->bd_bif->bif_dlt == dlt)
1473 		return 0;
1474 	ifp = d->bd_bif->bif_ifp;
1475 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1476 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1477 			break;
1478 	}
1479 	if (bp == NULL)
1480 		return EINVAL;
1481 	s = splnet();
1482 	opromisc = d->bd_promisc;
1483 	bpf_detachd(d);
1484 	bpf_attachd(d, bp);
1485 	reset_d(d);
1486 	if (opromisc) {
1487 		error = ifpromisc(bp->bif_ifp, 1);
1488 		if (error)
1489 			printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
1490 			    bp->bif_ifp->if_xname, error);
1491 		else
1492 			d->bd_promisc = 1;
1493 	}
1494 	splx(s);
1495 	return 0;
1496 }
1497 
1498 static int
1499 sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS)
1500 {
1501 	int newsize, error;
1502 	struct sysctlnode node;
1503 
1504 	node = *rnode;
1505 	node.sysctl_data = &newsize;
1506 	newsize = bpf_maxbufsize;
1507 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1508 	if (error || newp == NULL)
1509 		return (error);
1510 
1511 	if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE)
1512 		return (EINVAL);
1513 
1514 	bpf_maxbufsize = newsize;
1515 
1516 	return (0);
1517 }
1518 
1519 SYSCTL_SETUP(sysctl_net_bfp_setup, "sysctl net.bpf subtree setup")
1520 {
1521 	struct sysctlnode *node;
1522 
1523 	sysctl_createv(SYSCTL_PERMANENT,
1524 		       CTLTYPE_NODE, "net", NULL,
1525 		       NULL, 0, NULL, 0,
1526 		       CTL_NET, CTL_EOL);
1527 
1528 	node = NULL;
1529 	sysctl_createv(SYSCTL_PERMANENT,
1530 		       CTLTYPE_NODE, "bpf", &node,
1531 		       NULL, 0, NULL, 0,
1532 		       CTL_NET, CTL_CREATE, CTL_EOL);
1533 	if (node != NULL)
1534 		sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
1535 			CTLTYPE_INT, "maxbufsize", NULL,
1536 			sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0,
1537 			CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1538 }
1539