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