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