xref: /freebsd/sys/net/bpf.c (revision a4eb85b6acb49cb60c72c2cab0d0d3f00eaa6d46)
1 /*-
2  * Copyright (c) 1990, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from the Stanford/CMU enet packet filter,
6  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8  * Berkeley Laboratory.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
35  *
36  * $FreeBSD$
37  */
38 
39 #include "opt_bpf.h"
40 #include "opt_mac.h"
41 #include "opt_netgraph.h"
42 
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/fcntl.h>
48 #include <sys/mac.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/time.h>
52 #include <sys/proc.h>
53 #include <sys/signalvar.h>
54 #include <sys/filio.h>
55 #include <sys/sockio.h>
56 #include <sys/ttycom.h>
57 #include <sys/uio.h>
58 
59 #include <sys/event.h>
60 #include <sys/file.h>
61 #include <sys/poll.h>
62 #include <sys/proc.h>
63 
64 #include <sys/socket.h>
65 
66 #include <net/if.h>
67 #include <net/bpf.h>
68 #ifdef BPF_JITTER
69 #include <net/bpf_jitter.h>
70 #endif
71 #include <net/bpfdesc.h>
72 
73 #include <netinet/in.h>
74 #include <netinet/if_ether.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 
78 #include <net80211/ieee80211_freebsd.h>
79 
80 static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
81 
82 #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
83 
84 #define PRINET  26			/* interruptible */
85 
86 /*
87  * bpf_iflist is a list of BPF interface structures, each corresponding to a
88  * specific DLT.  The same network interface might have several BPF interface
89  * structures registered by different layers in the stack (i.e., 802.11
90  * frames, ethernet frames, etc).
91  */
92 static LIST_HEAD(, bpf_if)	bpf_iflist;
93 static struct mtx	bpf_mtx;		/* bpf global lock */
94 static int		bpf_bpfd_cnt;
95 
96 static int	bpf_allocbufs(struct bpf_d *);
97 static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
98 static void	bpf_detachd(struct bpf_d *);
99 static void	bpf_freed(struct bpf_d *);
100 static void	bpf_mcopy(const void *, void *, size_t);
101 static int	bpf_movein(struct uio *, int, int,
102 		    struct mbuf **, struct sockaddr *, struct bpf_insn *);
103 static int	bpf_setif(struct bpf_d *, struct ifreq *);
104 static void	bpf_timed_out(void *);
105 static __inline void
106 		bpf_wakeup(struct bpf_d *);
107 static void	catchpacket(struct bpf_d *, u_char *, u_int,
108 		    u_int, void (*)(const void *, void *, size_t),
109 		    struct timeval *);
110 static void	reset_d(struct bpf_d *);
111 static int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
112 static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
113 static int	bpf_setdlt(struct bpf_d *, u_int);
114 static void	filt_bpfdetach(struct knote *);
115 static int	filt_bpfread(struct knote *, long);
116 static void	bpf_drvinit(void *);
117 static void	bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
118 static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
119 
120 /*
121  * The default read buffer size is patchable.
122  */
123 SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
124 static int bpf_bufsize = 4096;
125 SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
126     &bpf_bufsize, 0, "");
127 static int bpf_maxbufsize = BPF_MAXBUFSIZE;
128 SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
129     &bpf_maxbufsize, 0, "");
130 static int bpf_maxinsns = BPF_MAXINSNS;
131 SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
132     &bpf_maxinsns, 0, "Maximum bpf program instructions");
133 SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
134     bpf_stats_sysctl, "bpf statistics portal");
135 
136 static	d_open_t	bpfopen;
137 static	d_close_t	bpfclose;
138 static	d_read_t	bpfread;
139 static	d_write_t	bpfwrite;
140 static	d_ioctl_t	bpfioctl;
141 static	d_poll_t	bpfpoll;
142 static	d_kqfilter_t	bpfkqfilter;
143 
144 static struct cdevsw bpf_cdevsw = {
145 	.d_version =	D_VERSION,
146 	.d_flags =	D_NEEDGIANT,
147 	.d_open =	bpfopen,
148 	.d_close =	bpfclose,
149 	.d_read =	bpfread,
150 	.d_write =	bpfwrite,
151 	.d_ioctl =	bpfioctl,
152 	.d_poll =	bpfpoll,
153 	.d_name =	"bpf",
154 	.d_kqfilter =	bpfkqfilter,
155 };
156 
157 static struct filterops bpfread_filtops =
158 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
159 
160 static int
161 bpf_movein(struct uio *uio, int linktype, int mtu, struct mbuf **mp,
162     struct sockaddr *sockp, struct bpf_insn *wfilter)
163 {
164 	const struct ieee80211_bpf_params *p;
165 	struct mbuf *m;
166 	int error;
167 	int len;
168 	int hlen;
169 	int slen;
170 
171 	/*
172 	 * Build a sockaddr based on the data link layer type.
173 	 * We do this at this level because the ethernet header
174 	 * is copied directly into the data field of the sockaddr.
175 	 * In the case of SLIP, there is no header and the packet
176 	 * is forwarded as is.
177 	 * Also, we are careful to leave room at the front of the mbuf
178 	 * for the link level header.
179 	 */
180 	switch (linktype) {
181 
182 	case DLT_SLIP:
183 		sockp->sa_family = AF_INET;
184 		hlen = 0;
185 		break;
186 
187 	case DLT_EN10MB:
188 		sockp->sa_family = AF_UNSPEC;
189 		/* XXX Would MAXLINKHDR be better? */
190 		hlen = ETHER_HDR_LEN;
191 		break;
192 
193 	case DLT_FDDI:
194 		sockp->sa_family = AF_IMPLINK;
195 		hlen = 0;
196 		break;
197 
198 	case DLT_RAW:
199 		sockp->sa_family = AF_UNSPEC;
200 		hlen = 0;
201 		break;
202 
203 	case DLT_NULL:
204 		/*
205 		 * null interface types require a 4 byte pseudo header which
206 		 * corresponds to the address family of the packet.
207 		 */
208 		sockp->sa_family = AF_UNSPEC;
209 		hlen = 4;
210 		break;
211 
212 	case DLT_ATM_RFC1483:
213 		/*
214 		 * en atm driver requires 4-byte atm pseudo header.
215 		 * though it isn't standard, vpi:vci needs to be
216 		 * specified anyway.
217 		 */
218 		sockp->sa_family = AF_UNSPEC;
219 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
220 		break;
221 
222 	case DLT_PPP:
223 		sockp->sa_family = AF_UNSPEC;
224 		hlen = 4;	/* This should match PPP_HDRLEN */
225 		break;
226 
227 	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
228 		sockp->sa_family = AF_IEEE80211;
229 		hlen = 0;
230 		break;
231 
232 	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
233 		sockp->sa_family = AF_IEEE80211;
234 		sockp->sa_len = 12;	/* XXX != 0 */
235 		hlen = sizeof(struct ieee80211_bpf_params);
236 		break;
237 
238 	default:
239 		return (EIO);
240 	}
241 
242 	len = uio->uio_resid;
243 
244 	if (len - hlen > mtu)
245 		return (EMSGSIZE);
246 
247 	if ((unsigned)len > MCLBYTES)
248 		return (EIO);
249 
250 	if (len > MHLEN) {
251 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
252 	} else {
253 		MGETHDR(m, M_TRYWAIT, MT_DATA);
254 	}
255 	if (m == NULL)
256 		return (ENOBUFS);
257 	m->m_pkthdr.len = m->m_len = len;
258 	m->m_pkthdr.rcvif = NULL;
259 	*mp = m;
260 
261 	if (m->m_len < hlen) {
262 		error = EPERM;
263 		goto bad;
264 	}
265 
266 	error = uiomove(mtod(m, u_char *), len, uio);
267 	if (error)
268 		goto bad;
269 
270 	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
271 	if (slen == 0) {
272 		error = EPERM;
273 		goto bad;
274 	}
275 
276 	/*
277 	 * Make room for link header, and copy it to sockaddr
278 	 */
279 	if (hlen != 0) {
280 		if (sockp->sa_family == AF_IEEE80211) {
281 			/*
282 			 * Collect true length from the parameter header
283 			 * NB: sockp is known to be zero'd so if we do a
284 			 *     short copy unspecified parameters will be
285 			 *     zero.
286 			 * NB: packet may not be aligned after stripping
287 			 *     bpf params
288 			 * XXX check ibp_vers
289 			 */
290 			p = mtod(m, const struct ieee80211_bpf_params *);
291 			hlen = p->ibp_len;
292 			if (hlen > sizeof(sockp->sa_data)) {
293 				error = EINVAL;
294 				goto bad;
295 			}
296 		}
297 		bcopy(m->m_data, sockp->sa_data, hlen);
298 		m->m_pkthdr.len -= hlen;
299 		m->m_len -= hlen;
300 #if BSD >= 199103
301 		m->m_data += hlen; /* XXX */
302 #else
303 		m->m_off += hlen;
304 #endif
305 	}
306 
307 	return (0);
308 bad:
309 	m_freem(m);
310 	return (error);
311 }
312 
313 /*
314  * Attach file to the bpf interface, i.e. make d listen on bp.
315  */
316 static void
317 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
318 {
319 	/*
320 	 * Point d at bp, and add d to the interface's list of listeners.
321 	 * Finally, point the driver's bpf cookie at the interface so
322 	 * it will divert packets to bpf.
323 	 */
324 	BPFIF_LOCK(bp);
325 	d->bd_bif = bp;
326 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
327 
328 	bpf_bpfd_cnt++;
329 	BPFIF_UNLOCK(bp);
330 }
331 
332 /*
333  * Detach a file from its interface.
334  */
335 static void
336 bpf_detachd(struct bpf_d *d)
337 {
338 	int error;
339 	struct bpf_if *bp;
340 	struct ifnet *ifp;
341 
342 	bp = d->bd_bif;
343 	BPFIF_LOCK(bp);
344 	BPFD_LOCK(d);
345 	ifp = d->bd_bif->bif_ifp;
346 
347 	/*
348 	 * Remove d from the interface's descriptor list.
349 	 */
350 	LIST_REMOVE(d, bd_next);
351 
352 	bpf_bpfd_cnt--;
353 	d->bd_bif = NULL;
354 	BPFD_UNLOCK(d);
355 	BPFIF_UNLOCK(bp);
356 
357 	/*
358 	 * Check if this descriptor had requested promiscuous mode.
359 	 * If so, turn it off.
360 	 */
361 	if (d->bd_promisc) {
362 		d->bd_promisc = 0;
363 		error = ifpromisc(ifp, 0);
364 		if (error != 0 && error != ENXIO) {
365 			/*
366 			 * ENXIO can happen if a pccard is unplugged
367 			 * Something is really wrong if we were able to put
368 			 * the driver into promiscuous mode, but can't
369 			 * take it out.
370 			 */
371 			if_printf(bp->bif_ifp,
372 				"bpf_detach: ifpromisc failed (%d)\n", error);
373 		}
374 	}
375 }
376 
377 /*
378  * Open ethernet device.  Returns ENXIO for illegal minor device number,
379  * EBUSY if file is open by another process.
380  */
381 /* ARGSUSED */
382 static	int
383 bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
384 {
385 	struct bpf_d *d;
386 
387 	mtx_lock(&bpf_mtx);
388 	d = dev->si_drv1;
389 	/*
390 	 * Each minor can be opened by only one process.  If the requested
391 	 * minor is in use, return EBUSY.
392 	 */
393 	if (d != NULL) {
394 		mtx_unlock(&bpf_mtx);
395 		return (EBUSY);
396 	}
397 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
398 	mtx_unlock(&bpf_mtx);
399 
400 	if ((dev->si_flags & SI_NAMED) == 0)
401 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
402 		    "bpf%d", dev2unit(dev));
403 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
404 	dev->si_drv1 = d;
405 	d->bd_bufsize = bpf_bufsize;
406 	d->bd_sig = SIGIO;
407 	d->bd_seesent = 1;
408 	d->bd_pid = td->td_proc->p_pid;
409 #ifdef MAC
410 	mac_init_bpfdesc(d);
411 	mac_create_bpfdesc(td->td_ucred, d);
412 #endif
413 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
414 	callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE);
415 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
416 
417 	return (0);
418 }
419 
420 /*
421  * Close the descriptor by detaching it from its interface,
422  * deallocating its buffers, and marking it free.
423  */
424 /* ARGSUSED */
425 static	int
426 bpfclose(struct cdev *dev, int flags, int fmt, struct thread *td)
427 {
428 	struct bpf_d *d = dev->si_drv1;
429 
430 	BPFD_LOCK(d);
431 	if (d->bd_state == BPF_WAITING)
432 		callout_stop(&d->bd_callout);
433 	d->bd_state = BPF_IDLE;
434 	BPFD_UNLOCK(d);
435 	funsetown(&d->bd_sigio);
436 	mtx_lock(&bpf_mtx);
437 	if (d->bd_bif)
438 		bpf_detachd(d);
439 	mtx_unlock(&bpf_mtx);
440 	selwakeuppri(&d->bd_sel, PRINET);
441 #ifdef MAC
442 	mac_destroy_bpfdesc(d);
443 #endif /* MAC */
444 	knlist_destroy(&d->bd_sel.si_note);
445 	bpf_freed(d);
446 	dev->si_drv1 = NULL;
447 	free(d, M_BPF);
448 
449 	return (0);
450 }
451 
452 
453 /*
454  * Rotate the packet buffers in descriptor d.  Move the store buffer
455  * into the hold slot, and the free buffer into the store slot.
456  * Zero the length of the new store buffer.
457  */
458 #define ROTATE_BUFFERS(d) \
459 	(d)->bd_hbuf = (d)->bd_sbuf; \
460 	(d)->bd_hlen = (d)->bd_slen; \
461 	(d)->bd_sbuf = (d)->bd_fbuf; \
462 	(d)->bd_slen = 0; \
463 	(d)->bd_fbuf = NULL;
464 /*
465  *  bpfread - read next chunk of packets from buffers
466  */
467 static	int
468 bpfread(struct cdev *dev, struct uio *uio, int ioflag)
469 {
470 	struct bpf_d *d = dev->si_drv1;
471 	int timed_out;
472 	int error;
473 
474 	/*
475 	 * Restrict application to use a buffer the same size as
476 	 * as kernel buffers.
477 	 */
478 	if (uio->uio_resid != d->bd_bufsize)
479 		return (EINVAL);
480 
481 	BPFD_LOCK(d);
482 	if (d->bd_state == BPF_WAITING)
483 		callout_stop(&d->bd_callout);
484 	timed_out = (d->bd_state == BPF_TIMED_OUT);
485 	d->bd_state = BPF_IDLE;
486 	/*
487 	 * If the hold buffer is empty, then do a timed sleep, which
488 	 * ends when the timeout expires or when enough packets
489 	 * have arrived to fill the store buffer.
490 	 */
491 	while (d->bd_hbuf == NULL) {
492 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
493 			/*
494 			 * A packet(s) either arrived since the previous
495 			 * read or arrived while we were asleep.
496 			 * Rotate the buffers and return what's here.
497 			 */
498 			ROTATE_BUFFERS(d);
499 			break;
500 		}
501 
502 		/*
503 		 * No data is available, check to see if the bpf device
504 		 * is still pointed at a real interface.  If not, return
505 		 * ENXIO so that the userland process knows to rebind
506 		 * it before using it again.
507 		 */
508 		if (d->bd_bif == NULL) {
509 			BPFD_UNLOCK(d);
510 			return (ENXIO);
511 		}
512 
513 		if (ioflag & O_NONBLOCK) {
514 			BPFD_UNLOCK(d);
515 			return (EWOULDBLOCK);
516 		}
517 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
518 		     "bpf", d->bd_rtout);
519 		if (error == EINTR || error == ERESTART) {
520 			BPFD_UNLOCK(d);
521 			return (error);
522 		}
523 		if (error == EWOULDBLOCK) {
524 			/*
525 			 * On a timeout, return what's in the buffer,
526 			 * which may be nothing.  If there is something
527 			 * in the store buffer, we can rotate the buffers.
528 			 */
529 			if (d->bd_hbuf)
530 				/*
531 				 * We filled up the buffer in between
532 				 * getting the timeout and arriving
533 				 * here, so we don't need to rotate.
534 				 */
535 				break;
536 
537 			if (d->bd_slen == 0) {
538 				BPFD_UNLOCK(d);
539 				return (0);
540 			}
541 			ROTATE_BUFFERS(d);
542 			break;
543 		}
544 	}
545 	/*
546 	 * At this point, we know we have something in the hold slot.
547 	 */
548 	BPFD_UNLOCK(d);
549 
550 	/*
551 	 * Move data from hold buffer into user space.
552 	 * We know the entire buffer is transferred since
553 	 * we checked above that the read buffer is bpf_bufsize bytes.
554 	 */
555 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
556 
557 	BPFD_LOCK(d);
558 	d->bd_fbuf = d->bd_hbuf;
559 	d->bd_hbuf = NULL;
560 	d->bd_hlen = 0;
561 	BPFD_UNLOCK(d);
562 
563 	return (error);
564 }
565 
566 
567 /*
568  * If there are processes sleeping on this descriptor, wake them up.
569  */
570 static __inline void
571 bpf_wakeup(struct bpf_d *d)
572 {
573 
574 	BPFD_LOCK_ASSERT(d);
575 	if (d->bd_state == BPF_WAITING) {
576 		callout_stop(&d->bd_callout);
577 		d->bd_state = BPF_IDLE;
578 	}
579 	wakeup(d);
580 	if (d->bd_async && d->bd_sig && d->bd_sigio)
581 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
582 
583 	selwakeuppri(&d->bd_sel, PRINET);
584 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
585 }
586 
587 static void
588 bpf_timed_out(void *arg)
589 {
590 	struct bpf_d *d = (struct bpf_d *)arg;
591 
592 	BPFD_LOCK(d);
593 	if (d->bd_state == BPF_WAITING) {
594 		d->bd_state = BPF_TIMED_OUT;
595 		if (d->bd_slen != 0)
596 			bpf_wakeup(d);
597 	}
598 	BPFD_UNLOCK(d);
599 }
600 
601 static int
602 bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
603 {
604 	struct bpf_d *d = dev->si_drv1;
605 	struct ifnet *ifp;
606 	struct mbuf *m;
607 	int error;
608 	struct sockaddr dst;
609 
610 	if (d->bd_bif == NULL)
611 		return (ENXIO);
612 
613 	ifp = d->bd_bif->bif_ifp;
614 
615 	if ((ifp->if_flags & IFF_UP) == 0)
616 		return (ENETDOWN);
617 
618 	if (uio->uio_resid == 0)
619 		return (0);
620 
621 	bzero(&dst, sizeof(dst));
622 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu,
623 	    &m, &dst, d->bd_wfilter);
624 	if (error)
625 		return (error);
626 
627 	if (d->bd_hdrcmplt)
628 		dst.sa_family = pseudo_AF_HDRCMPLT;
629 
630 #ifdef MAC
631 	BPFD_LOCK(d);
632 	mac_create_mbuf_from_bpfdesc(d, m);
633 	BPFD_UNLOCK(d);
634 #endif
635 	NET_LOCK_GIANT();
636 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
637 	NET_UNLOCK_GIANT();
638 	/*
639 	 * The driver frees the mbuf.
640 	 */
641 	return (error);
642 }
643 
644 /*
645  * Reset a descriptor by flushing its packet buffer and clearing the
646  * receive and drop counts.
647  */
648 static void
649 reset_d(struct bpf_d *d)
650 {
651 
652 	mtx_assert(&d->bd_mtx, MA_OWNED);
653 	if (d->bd_hbuf) {
654 		/* Free the hold buffer. */
655 		d->bd_fbuf = d->bd_hbuf;
656 		d->bd_hbuf = NULL;
657 	}
658 	d->bd_slen = 0;
659 	d->bd_hlen = 0;
660 	d->bd_rcount = 0;
661 	d->bd_dcount = 0;
662 	d->bd_fcount = 0;
663 }
664 
665 /*
666  *  FIONREAD		Check for read packet available.
667  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
668  *  BIOCGBLEN		Get buffer len [for read()].
669  *  BIOCSETF		Set ethernet read filter.
670  *  BIOCSETWF		Set ethernet write filter.
671  *  BIOCFLUSH		Flush read packet buffer.
672  *  BIOCPROMISC		Put interface into promiscuous mode.
673  *  BIOCGDLT		Get link layer type.
674  *  BIOCGETIF		Get interface name.
675  *  BIOCSETIF		Set interface.
676  *  BIOCSRTIMEOUT	Set read timeout.
677  *  BIOCGRTIMEOUT	Get read timeout.
678  *  BIOCGSTATS		Get packet stats.
679  *  BIOCIMMEDIATE	Set immediate mode.
680  *  BIOCVERSION		Get filter language version.
681  *  BIOCGHDRCMPLT	Get "header already complete" flag
682  *  BIOCSHDRCMPLT	Set "header already complete" flag
683  *  BIOCGSEESENT	Get "see packets sent" flag
684  *  BIOCSSEESENT	Set "see packets sent" flag
685  *  BIOCLOCK		Set "locked" flag
686  */
687 /* ARGSUSED */
688 static	int
689 bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
690     struct thread *td)
691 {
692 	struct bpf_d *d = dev->si_drv1;
693 	int error = 0;
694 
695 	/*
696 	 * Refresh PID associated with this descriptor.
697 	 */
698 	BPFD_LOCK(d);
699 	d->bd_pid = td->td_proc->p_pid;
700 	if (d->bd_state == BPF_WAITING)
701 		callout_stop(&d->bd_callout);
702 	d->bd_state = BPF_IDLE;
703 	BPFD_UNLOCK(d);
704 
705 	if (d->bd_locked == 1) {
706 		switch (cmd) {
707 		case BIOCGBLEN:
708 		case BIOCFLUSH:
709 		case BIOCGDLT:
710 		case BIOCGDLTLIST:
711 		case BIOCGETIF:
712 		case BIOCGRTIMEOUT:
713 		case BIOCGSTATS:
714 		case BIOCVERSION:
715 		case BIOCGRSIG:
716 		case BIOCGHDRCMPLT:
717 		case FIONREAD:
718 		case BIOCLOCK:
719 		case BIOCSRTIMEOUT:
720 		case BIOCIMMEDIATE:
721 		case TIOCGPGRP:
722 			break;
723 		default:
724 			return (EPERM);
725 		}
726 	}
727 	switch (cmd) {
728 
729 	default:
730 		error = EINVAL;
731 		break;
732 
733 	/*
734 	 * Check for read packet available.
735 	 */
736 	case FIONREAD:
737 		{
738 			int n;
739 
740 			BPFD_LOCK(d);
741 			n = d->bd_slen;
742 			if (d->bd_hbuf)
743 				n += d->bd_hlen;
744 			BPFD_UNLOCK(d);
745 
746 			*(int *)addr = n;
747 			break;
748 		}
749 
750 	case SIOCGIFADDR:
751 		{
752 			struct ifnet *ifp;
753 
754 			if (d->bd_bif == NULL)
755 				error = EINVAL;
756 			else {
757 				ifp = d->bd_bif->bif_ifp;
758 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
759 			}
760 			break;
761 		}
762 
763 	/*
764 	 * Get buffer len [for read()].
765 	 */
766 	case BIOCGBLEN:
767 		*(u_int *)addr = d->bd_bufsize;
768 		break;
769 
770 	/*
771 	 * Set buffer length.
772 	 */
773 	case BIOCSBLEN:
774 		if (d->bd_bif != NULL)
775 			error = EINVAL;
776 		else {
777 			u_int size = *(u_int *)addr;
778 
779 			if (size > bpf_maxbufsize)
780 				*(u_int *)addr = size = bpf_maxbufsize;
781 			else if (size < BPF_MINBUFSIZE)
782 				*(u_int *)addr = size = BPF_MINBUFSIZE;
783 			d->bd_bufsize = size;
784 		}
785 		break;
786 
787 	/*
788 	 * Set link layer read filter.
789 	 */
790 	case BIOCSETF:
791 	case BIOCSETWF:
792 		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
793 		break;
794 
795 	/*
796 	 * Flush read packet buffer.
797 	 */
798 	case BIOCFLUSH:
799 		BPFD_LOCK(d);
800 		reset_d(d);
801 		BPFD_UNLOCK(d);
802 		break;
803 
804 	/*
805 	 * Put interface into promiscuous mode.
806 	 */
807 	case BIOCPROMISC:
808 		if (d->bd_bif == NULL) {
809 			/*
810 			 * No interface attached yet.
811 			 */
812 			error = EINVAL;
813 			break;
814 		}
815 		if (d->bd_promisc == 0) {
816 			mtx_lock(&Giant);
817 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
818 			mtx_unlock(&Giant);
819 			if (error == 0)
820 				d->bd_promisc = 1;
821 		}
822 		break;
823 
824 	/*
825 	 * Get current data link type.
826 	 */
827 	case BIOCGDLT:
828 		if (d->bd_bif == NULL)
829 			error = EINVAL;
830 		else
831 			*(u_int *)addr = d->bd_bif->bif_dlt;
832 		break;
833 
834 	/*
835 	 * Get a list of supported data link types.
836 	 */
837 	case BIOCGDLTLIST:
838 		if (d->bd_bif == NULL)
839 			error = EINVAL;
840 		else
841 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
842 		break;
843 
844 	/*
845 	 * Set data link type.
846 	 */
847 	case BIOCSDLT:
848 		if (d->bd_bif == NULL)
849 			error = EINVAL;
850 		else
851 			error = bpf_setdlt(d, *(u_int *)addr);
852 		break;
853 
854 	/*
855 	 * Get interface name.
856 	 */
857 	case BIOCGETIF:
858 		if (d->bd_bif == NULL)
859 			error = EINVAL;
860 		else {
861 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
862 			struct ifreq *const ifr = (struct ifreq *)addr;
863 
864 			strlcpy(ifr->ifr_name, ifp->if_xname,
865 			    sizeof(ifr->ifr_name));
866 		}
867 		break;
868 
869 	/*
870 	 * Set interface.
871 	 */
872 	case BIOCSETIF:
873 		error = bpf_setif(d, (struct ifreq *)addr);
874 		break;
875 
876 	/*
877 	 * Set read timeout.
878 	 */
879 	case BIOCSRTIMEOUT:
880 		{
881 			struct timeval *tv = (struct timeval *)addr;
882 
883 			/*
884 			 * Subtract 1 tick from tvtohz() since this isn't
885 			 * a one-shot timer.
886 			 */
887 			if ((error = itimerfix(tv)) == 0)
888 				d->bd_rtout = tvtohz(tv) - 1;
889 			break;
890 		}
891 
892 	/*
893 	 * Get read timeout.
894 	 */
895 	case BIOCGRTIMEOUT:
896 		{
897 			struct timeval *tv = (struct timeval *)addr;
898 
899 			tv->tv_sec = d->bd_rtout / hz;
900 			tv->tv_usec = (d->bd_rtout % hz) * tick;
901 			break;
902 		}
903 
904 	/*
905 	 * Get packet stats.
906 	 */
907 	case BIOCGSTATS:
908 		{
909 			struct bpf_stat *bs = (struct bpf_stat *)addr;
910 
911 			bs->bs_recv = d->bd_rcount;
912 			bs->bs_drop = d->bd_dcount;
913 			break;
914 		}
915 
916 	/*
917 	 * Set immediate mode.
918 	 */
919 	case BIOCIMMEDIATE:
920 		d->bd_immediate = *(u_int *)addr;
921 		break;
922 
923 	case BIOCVERSION:
924 		{
925 			struct bpf_version *bv = (struct bpf_version *)addr;
926 
927 			bv->bv_major = BPF_MAJOR_VERSION;
928 			bv->bv_minor = BPF_MINOR_VERSION;
929 			break;
930 		}
931 
932 	/*
933 	 * Get "header already complete" flag
934 	 */
935 	case BIOCGHDRCMPLT:
936 		*(u_int *)addr = d->bd_hdrcmplt;
937 		break;
938 
939 	case BIOCLOCK:
940 		d->bd_locked = 1;
941 		break;
942 	/*
943 	 * Set "header already complete" flag
944 	 */
945 	case BIOCSHDRCMPLT:
946 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
947 		break;
948 
949 	/*
950 	 * Get "see sent packets" flag
951 	 */
952 	case BIOCGSEESENT:
953 		*(u_int *)addr = d->bd_seesent;
954 		break;
955 
956 	/*
957 	 * Set "see sent packets" flag
958 	 */
959 	case BIOCSSEESENT:
960 		d->bd_seesent = *(u_int *)addr;
961 		break;
962 
963 	case FIONBIO:		/* Non-blocking I/O */
964 		break;
965 
966 	case FIOASYNC:		/* Send signal on receive packets */
967 		d->bd_async = *(int *)addr;
968 		break;
969 
970 	case FIOSETOWN:
971 		error = fsetown(*(int *)addr, &d->bd_sigio);
972 		break;
973 
974 	case FIOGETOWN:
975 		*(int *)addr = fgetown(&d->bd_sigio);
976 		break;
977 
978 	/* This is deprecated, FIOSETOWN should be used instead. */
979 	case TIOCSPGRP:
980 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
981 		break;
982 
983 	/* This is deprecated, FIOGETOWN should be used instead. */
984 	case TIOCGPGRP:
985 		*(int *)addr = -fgetown(&d->bd_sigio);
986 		break;
987 
988 	case BIOCSRSIG:		/* Set receive signal */
989 		{
990 			u_int sig;
991 
992 			sig = *(u_int *)addr;
993 
994 			if (sig >= NSIG)
995 				error = EINVAL;
996 			else
997 				d->bd_sig = sig;
998 			break;
999 		}
1000 	case BIOCGRSIG:
1001 		*(u_int *)addr = d->bd_sig;
1002 		break;
1003 	}
1004 	return (error);
1005 }
1006 
1007 /*
1008  * Set d's packet filter program to fp.  If this file already has a filter,
1009  * free it and replace it.  Returns EINVAL for bogus requests.
1010  */
1011 static int
1012 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1013 {
1014 	struct bpf_insn *fcode, *old;
1015 	u_int wfilter, flen, size;
1016 #ifdef BPF_JITTER
1017 	bpf_jit_filter *ofunc;
1018 #endif
1019 
1020 	if (cmd == BIOCSETWF) {
1021 		old = d->bd_wfilter;
1022 		wfilter = 1;
1023 #ifdef BPF_JITTER
1024 		ofunc = NULL;
1025 #endif
1026 	} else {
1027 		wfilter = 0;
1028 		old = d->bd_rfilter;
1029 #ifdef BPF_JITTER
1030 		ofunc = d->bd_bfilter;
1031 #endif
1032 	}
1033 	if (fp->bf_insns == NULL) {
1034 		if (fp->bf_len != 0)
1035 			return (EINVAL);
1036 		BPFD_LOCK(d);
1037 		if (wfilter)
1038 			d->bd_wfilter = NULL;
1039 		else {
1040 			d->bd_rfilter = NULL;
1041 #ifdef BPF_JITTER
1042 			d->bd_bfilter = NULL;
1043 #endif
1044 		}
1045 		reset_d(d);
1046 		BPFD_UNLOCK(d);
1047 		if (old != NULL)
1048 			free((caddr_t)old, M_BPF);
1049 #ifdef BPF_JITTER
1050 		if (ofunc != NULL)
1051 			bpf_destroy_jit_filter(ofunc);
1052 #endif
1053 		return (0);
1054 	}
1055 	flen = fp->bf_len;
1056 	if (flen > bpf_maxinsns)
1057 		return (EINVAL);
1058 
1059 	size = flen * sizeof(*fp->bf_insns);
1060 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1061 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1062 	    bpf_validate(fcode, (int)flen)) {
1063 		BPFD_LOCK(d);
1064 		if (wfilter)
1065 			d->bd_wfilter = fcode;
1066 		else {
1067 			d->bd_rfilter = fcode;
1068 #ifdef BPF_JITTER
1069 			d->bd_bfilter = bpf_jitter(fcode, flen);
1070 #endif
1071 		}
1072 		reset_d(d);
1073 		BPFD_UNLOCK(d);
1074 		if (old != NULL)
1075 			free((caddr_t)old, M_BPF);
1076 #ifdef BPF_JITTER
1077 		if (ofunc != NULL)
1078 			bpf_destroy_jit_filter(ofunc);
1079 #endif
1080 
1081 		return (0);
1082 	}
1083 	free((caddr_t)fcode, M_BPF);
1084 	return (EINVAL);
1085 }
1086 
1087 /*
1088  * Detach a file from its current interface (if attached at all) and attach
1089  * to the interface indicated by the name stored in ifr.
1090  * Return an errno or 0.
1091  */
1092 static int
1093 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1094 {
1095 	struct bpf_if *bp;
1096 	int error;
1097 	struct ifnet *theywant;
1098 
1099 	theywant = ifunit(ifr->ifr_name);
1100 	if (theywant == NULL || theywant->if_bpf == NULL)
1101 		return (ENXIO);
1102 
1103 	bp = theywant->if_bpf;
1104 	/*
1105 	 * Allocate the packet buffers if we need to.
1106 	 * If we're already attached to requested interface,
1107 	 * just flush the buffer.
1108 	 */
1109 	if (d->bd_sbuf == NULL) {
1110 		error = bpf_allocbufs(d);
1111 		if (error != 0)
1112 			return (error);
1113 	}
1114 	if (bp != d->bd_bif) {
1115 		if (d->bd_bif)
1116 			/*
1117 			 * Detach if attached to something else.
1118 			 */
1119 			bpf_detachd(d);
1120 
1121 		bpf_attachd(d, bp);
1122 	}
1123 	BPFD_LOCK(d);
1124 	reset_d(d);
1125 	BPFD_UNLOCK(d);
1126 	return (0);
1127 }
1128 
1129 /*
1130  * Support for select() and poll() system calls
1131  *
1132  * Return true iff the specific operation will not block indefinitely.
1133  * Otherwise, return false but make a note that a selwakeup() must be done.
1134  */
1135 static int
1136 bpfpoll(struct cdev *dev, int events, struct thread *td)
1137 {
1138 	struct bpf_d *d;
1139 	int revents;
1140 
1141 	d = dev->si_drv1;
1142 	if (d->bd_bif == NULL)
1143 		return (ENXIO);
1144 
1145 	/*
1146 	 * Refresh PID associated with this descriptor.
1147 	 */
1148 	revents = events & (POLLOUT | POLLWRNORM);
1149 	BPFD_LOCK(d);
1150 	d->bd_pid = td->td_proc->p_pid;
1151 	if (events & (POLLIN | POLLRDNORM)) {
1152 		if (bpf_ready(d))
1153 			revents |= events & (POLLIN | POLLRDNORM);
1154 		else {
1155 			selrecord(td, &d->bd_sel);
1156 			/* Start the read timeout if necessary. */
1157 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1158 				callout_reset(&d->bd_callout, d->bd_rtout,
1159 				    bpf_timed_out, d);
1160 				d->bd_state = BPF_WAITING;
1161 			}
1162 		}
1163 	}
1164 	BPFD_UNLOCK(d);
1165 	return (revents);
1166 }
1167 
1168 /*
1169  * Support for kevent() system call.  Register EVFILT_READ filters and
1170  * reject all others.
1171  */
1172 int
1173 bpfkqfilter(struct cdev *dev, struct knote *kn)
1174 {
1175 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1176 
1177 	if (kn->kn_filter != EVFILT_READ)
1178 		return (1);
1179 
1180 	/*
1181 	 * Refresh PID associated with this descriptor.
1182 	 */
1183 	BPFD_LOCK(d);
1184 	d->bd_pid = curthread->td_proc->p_pid;
1185 	kn->kn_fop = &bpfread_filtops;
1186 	kn->kn_hook = d;
1187 	knlist_add(&d->bd_sel.si_note, kn, 1);
1188 	BPFD_UNLOCK(d);
1189 
1190 	return (0);
1191 }
1192 
1193 static void
1194 filt_bpfdetach(struct knote *kn)
1195 {
1196 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1197 
1198 	knlist_remove(&d->bd_sel.si_note, kn, 0);
1199 }
1200 
1201 static int
1202 filt_bpfread(struct knote *kn, long hint)
1203 {
1204 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1205 	int ready;
1206 
1207 	BPFD_LOCK_ASSERT(d);
1208 	ready = bpf_ready(d);
1209 	if (ready) {
1210 		kn->kn_data = d->bd_slen;
1211 		if (d->bd_hbuf)
1212 			kn->kn_data += d->bd_hlen;
1213 	}
1214 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1215 		callout_reset(&d->bd_callout, d->bd_rtout,
1216 		    bpf_timed_out, d);
1217 		d->bd_state = BPF_WAITING;
1218 	}
1219 
1220 	return (ready);
1221 }
1222 
1223 /*
1224  * Incoming linkage from device drivers.  Process the packet pkt, of length
1225  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1226  * by each process' filter, and if accepted, stashed into the corresponding
1227  * buffer.
1228  */
1229 void
1230 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1231 {
1232 	struct bpf_d *d;
1233 	u_int slen;
1234 	int gottime;
1235 	struct timeval tv;
1236 
1237 	gottime = 0;
1238 	BPFIF_LOCK(bp);
1239 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1240 		BPFD_LOCK(d);
1241 		++d->bd_rcount;
1242 #ifdef BPF_JITTER
1243 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1244 			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1245 		else
1246 #endif
1247 		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1248 		if (slen != 0) {
1249 			d->bd_fcount++;
1250 			if (!gottime) {
1251 				microtime(&tv);
1252 				gottime = 1;
1253 			}
1254 #ifdef MAC
1255 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1256 #endif
1257 				catchpacket(d, pkt, pktlen, slen, bcopy, &tv);
1258 		}
1259 		BPFD_UNLOCK(d);
1260 	}
1261 	BPFIF_UNLOCK(bp);
1262 }
1263 
1264 /*
1265  * Copy data from an mbuf chain into a buffer.  This code is derived
1266  * from m_copydata in sys/uipc_mbuf.c.
1267  */
1268 static void
1269 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1270 {
1271 	const struct mbuf *m;
1272 	u_int count;
1273 	u_char *dst;
1274 
1275 	m = src_arg;
1276 	dst = dst_arg;
1277 	while (len > 0) {
1278 		if (m == NULL)
1279 			panic("bpf_mcopy");
1280 		count = min(m->m_len, len);
1281 		bcopy(mtod(m, void *), dst, count);
1282 		m = m->m_next;
1283 		dst += count;
1284 		len -= count;
1285 	}
1286 }
1287 
1288 /*
1289  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1290  */
1291 void
1292 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1293 {
1294 	struct bpf_d *d;
1295 	u_int pktlen, slen;
1296 	int gottime;
1297 	struct timeval tv;
1298 
1299 	gottime = 0;
1300 
1301 	pktlen = m_length(m, NULL);
1302 
1303 	BPFIF_LOCK(bp);
1304 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1305 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1306 			continue;
1307 		BPFD_LOCK(d);
1308 		++d->bd_rcount;
1309 #ifdef BPF_JITTER
1310 		/* XXX We cannot handle multiple mbufs. */
1311 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1312 		    m->m_next == NULL)
1313 			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1314 			    pktlen, pktlen);
1315 		else
1316 #endif
1317 		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1318 		if (slen != 0) {
1319 			d->bd_fcount++;
1320 			if (!gottime) {
1321 				microtime(&tv);
1322 				gottime = 1;
1323 			}
1324 #ifdef MAC
1325 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1326 #endif
1327 				catchpacket(d, (u_char *)m, pktlen, slen,
1328 				    bpf_mcopy, &tv);
1329 		}
1330 		BPFD_UNLOCK(d);
1331 	}
1332 	BPFIF_UNLOCK(bp);
1333 }
1334 
1335 /*
1336  * Incoming linkage from device drivers, when packet is in
1337  * an mbuf chain and to be prepended by a contiguous header.
1338  */
1339 void
1340 bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1341 {
1342 	struct mbuf mb;
1343 	struct bpf_d *d;
1344 	u_int pktlen, slen;
1345 	int gottime;
1346 	struct timeval tv;
1347 
1348 	gottime = 0;
1349 
1350 	pktlen = m_length(m, NULL);
1351 	/*
1352 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1353 	 * Note that we cut corners here; we only setup what's
1354 	 * absolutely needed--this mbuf should never go anywhere else.
1355 	 */
1356 	mb.m_next = m;
1357 	mb.m_data = data;
1358 	mb.m_len = dlen;
1359 	pktlen += dlen;
1360 
1361 	BPFIF_LOCK(bp);
1362 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1363 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1364 			continue;
1365 		BPFD_LOCK(d);
1366 		++d->bd_rcount;
1367 		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
1368 		if (slen != 0) {
1369 			d->bd_fcount++;
1370 			if (!gottime) {
1371 				microtime(&tv);
1372 				gottime = 1;
1373 			}
1374 #ifdef MAC
1375 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1376 #endif
1377 				catchpacket(d, (u_char *)&mb, pktlen, slen,
1378 				    bpf_mcopy, &tv);
1379 		}
1380 		BPFD_UNLOCK(d);
1381 	}
1382 	BPFIF_UNLOCK(bp);
1383 }
1384 
1385 /*
1386  * Move the packet data from interface memory (pkt) into the
1387  * store buffer.  "cpfn" is the routine called to do the actual data
1388  * transfer.  bcopy is passed in to copy contiguous chunks, while
1389  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1390  * pkt is really an mbuf.
1391  */
1392 static void
1393 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1394     void (*cpfn)(const void *, void *, size_t), struct timeval *tv)
1395 {
1396 	struct bpf_hdr *hp;
1397 	int totlen, curlen;
1398 	int hdrlen = d->bd_bif->bif_hdrlen;
1399 	int do_wakeup = 0;
1400 
1401 	BPFD_LOCK_ASSERT(d);
1402 	/*
1403 	 * Figure out how many bytes to move.  If the packet is
1404 	 * greater or equal to the snapshot length, transfer that
1405 	 * much.  Otherwise, transfer the whole packet (unless
1406 	 * we hit the buffer size limit).
1407 	 */
1408 	totlen = hdrlen + min(snaplen, pktlen);
1409 	if (totlen > d->bd_bufsize)
1410 		totlen = d->bd_bufsize;
1411 
1412 	/*
1413 	 * Round up the end of the previous packet to the next longword.
1414 	 */
1415 	curlen = BPF_WORDALIGN(d->bd_slen);
1416 	if (curlen + totlen > d->bd_bufsize) {
1417 		/*
1418 		 * This packet will overflow the storage buffer.
1419 		 * Rotate the buffers if we can, then wakeup any
1420 		 * pending reads.
1421 		 */
1422 		if (d->bd_fbuf == NULL) {
1423 			/*
1424 			 * We haven't completed the previous read yet,
1425 			 * so drop the packet.
1426 			 */
1427 			++d->bd_dcount;
1428 			return;
1429 		}
1430 		ROTATE_BUFFERS(d);
1431 		do_wakeup = 1;
1432 		curlen = 0;
1433 	}
1434 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1435 		/*
1436 		 * Immediate mode is set, or the read timeout has
1437 		 * already expired during a select call.  A packet
1438 		 * arrived, so the reader should be woken up.
1439 		 */
1440 		do_wakeup = 1;
1441 
1442 	/*
1443 	 * Append the bpf header.
1444 	 */
1445 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1446 	hp->bh_tstamp = *tv;
1447 	hp->bh_datalen = pktlen;
1448 	hp->bh_hdrlen = hdrlen;
1449 	/*
1450 	 * Copy the packet data into the store buffer and update its length.
1451 	 */
1452 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1453 	d->bd_slen = curlen + totlen;
1454 
1455 	if (do_wakeup)
1456 		bpf_wakeup(d);
1457 }
1458 
1459 /*
1460  * Initialize all nonzero fields of a descriptor.
1461  */
1462 static int
1463 bpf_allocbufs(struct bpf_d *d)
1464 {
1465 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1466 	if (d->bd_fbuf == NULL)
1467 		return (ENOBUFS);
1468 
1469 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1470 	if (d->bd_sbuf == NULL) {
1471 		free(d->bd_fbuf, M_BPF);
1472 		return (ENOBUFS);
1473 	}
1474 	d->bd_slen = 0;
1475 	d->bd_hlen = 0;
1476 	return (0);
1477 }
1478 
1479 /*
1480  * Free buffers currently in use by a descriptor.
1481  * Called on close.
1482  */
1483 static void
1484 bpf_freed(struct bpf_d *d)
1485 {
1486 	/*
1487 	 * We don't need to lock out interrupts since this descriptor has
1488 	 * been detached from its interface and it yet hasn't been marked
1489 	 * free.
1490 	 */
1491 	if (d->bd_sbuf != NULL) {
1492 		free(d->bd_sbuf, M_BPF);
1493 		if (d->bd_hbuf != NULL)
1494 			free(d->bd_hbuf, M_BPF);
1495 		if (d->bd_fbuf != NULL)
1496 			free(d->bd_fbuf, M_BPF);
1497 	}
1498 	if (d->bd_rfilter) {
1499 		free((caddr_t)d->bd_rfilter, M_BPF);
1500 #ifdef BPF_JITTER
1501 		bpf_destroy_jit_filter(d->bd_bfilter);
1502 #endif
1503 	}
1504 	if (d->bd_wfilter)
1505 		free((caddr_t)d->bd_wfilter, M_BPF);
1506 	mtx_destroy(&d->bd_mtx);
1507 }
1508 
1509 /*
1510  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
1511  * fixed size of the link header (variable length headers not yet supported).
1512  */
1513 void
1514 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1515 {
1516 
1517 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1518 }
1519 
1520 /*
1521  * Attach an interface to bpf.  ifp is a pointer to the structure
1522  * defining the interface to be attached, dlt is the link layer type,
1523  * and hdrlen is the fixed size of the link header (variable length
1524  * headers are not yet supporrted).
1525  */
1526 void
1527 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1528 {
1529 	struct bpf_if *bp;
1530 
1531 	bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1532 	if (bp == NULL)
1533 		panic("bpfattach");
1534 
1535 	LIST_INIT(&bp->bif_dlist);
1536 	bp->bif_ifp = ifp;
1537 	bp->bif_dlt = dlt;
1538 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1539 	KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
1540 	*driverp = bp;
1541 
1542 	mtx_lock(&bpf_mtx);
1543 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1544 	mtx_unlock(&bpf_mtx);
1545 
1546 	/*
1547 	 * Compute the length of the bpf header.  This is not necessarily
1548 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1549 	 * that the network layer header begins on a longword boundary (for
1550 	 * performance reasons and to alleviate alignment restrictions).
1551 	 */
1552 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1553 
1554 	if (bootverbose)
1555 		if_printf(ifp, "bpf attached\n");
1556 }
1557 
1558 /*
1559  * Detach bpf from an interface.  This involves detaching each descriptor
1560  * associated with the interface, and leaving bd_bif NULL.  Notify each
1561  * descriptor as it's detached so that any sleepers wake up and get
1562  * ENXIO.
1563  */
1564 void
1565 bpfdetach(struct ifnet *ifp)
1566 {
1567 	struct bpf_if	*bp;
1568 	struct bpf_d	*d;
1569 
1570 	/* Locate BPF interface information */
1571 	mtx_lock(&bpf_mtx);
1572 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1573 		if (ifp == bp->bif_ifp)
1574 			break;
1575 	}
1576 
1577 	/* Interface wasn't attached */
1578 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1579 		mtx_unlock(&bpf_mtx);
1580 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1581 		return;
1582 	}
1583 
1584 	LIST_REMOVE(bp, bif_next);
1585 	mtx_unlock(&bpf_mtx);
1586 
1587 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1588 		bpf_detachd(d);
1589 		BPFD_LOCK(d);
1590 		bpf_wakeup(d);
1591 		BPFD_UNLOCK(d);
1592 	}
1593 
1594 	mtx_destroy(&bp->bif_mtx);
1595 	free(bp, M_BPF);
1596 }
1597 
1598 /*
1599  * Get a list of available data link type of the interface.
1600  */
1601 static int
1602 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1603 {
1604 	int n, error;
1605 	struct ifnet *ifp;
1606 	struct bpf_if *bp;
1607 
1608 	ifp = d->bd_bif->bif_ifp;
1609 	n = 0;
1610 	error = 0;
1611 	mtx_lock(&bpf_mtx);
1612 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1613 		if (bp->bif_ifp != ifp)
1614 			continue;
1615 		if (bfl->bfl_list != NULL) {
1616 			if (n >= bfl->bfl_len) {
1617 				mtx_unlock(&bpf_mtx);
1618 				return (ENOMEM);
1619 			}
1620 			error = copyout(&bp->bif_dlt,
1621 			    bfl->bfl_list + n, sizeof(u_int));
1622 		}
1623 		n++;
1624 	}
1625 	mtx_unlock(&bpf_mtx);
1626 	bfl->bfl_len = n;
1627 	return (error);
1628 }
1629 
1630 /*
1631  * Set the data link type of a BPF instance.
1632  */
1633 static int
1634 bpf_setdlt(struct bpf_d *d, u_int dlt)
1635 {
1636 	int error, opromisc;
1637 	struct ifnet *ifp;
1638 	struct bpf_if *bp;
1639 
1640 	if (d->bd_bif->bif_dlt == dlt)
1641 		return (0);
1642 	ifp = d->bd_bif->bif_ifp;
1643 	mtx_lock(&bpf_mtx);
1644 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1645 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1646 			break;
1647 	}
1648 	mtx_unlock(&bpf_mtx);
1649 	if (bp != NULL) {
1650 		opromisc = d->bd_promisc;
1651 		bpf_detachd(d);
1652 		bpf_attachd(d, bp);
1653 		BPFD_LOCK(d);
1654 		reset_d(d);
1655 		BPFD_UNLOCK(d);
1656 		if (opromisc) {
1657 			error = ifpromisc(bp->bif_ifp, 1);
1658 			if (error)
1659 				if_printf(bp->bif_ifp,
1660 					"bpf_setdlt: ifpromisc failed (%d)\n",
1661 					error);
1662 			else
1663 				d->bd_promisc = 1;
1664 		}
1665 	}
1666 	return (bp == NULL ? EINVAL : 0);
1667 }
1668 
1669 static void
1670 bpf_clone(void *arg, struct ucred *cred, char *name, int namelen,
1671     struct cdev **dev)
1672 {
1673 	int u;
1674 
1675 	if (*dev != NULL)
1676 		return;
1677 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1678 		return;
1679 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1680 	    "bpf%d", u);
1681 	dev_ref(*dev);
1682 	(*dev)->si_flags |= SI_CHEAPCLONE;
1683 	return;
1684 }
1685 
1686 static void
1687 bpf_drvinit(void *unused)
1688 {
1689 
1690 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1691 	LIST_INIT(&bpf_iflist);
1692 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1693 }
1694 
1695 static void
1696 bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
1697 {
1698 
1699 	bzero(d, sizeof(*d));
1700 	BPFD_LOCK_ASSERT(bd);
1701 	d->bd_immediate = bd->bd_immediate;
1702 	d->bd_promisc = bd->bd_promisc;
1703 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
1704 	d->bd_seesent = bd->bd_seesent;
1705 	d->bd_async = bd->bd_async;
1706 	d->bd_rcount = bd->bd_rcount;
1707 	d->bd_dcount = bd->bd_dcount;
1708 	d->bd_fcount = bd->bd_fcount;
1709 	d->bd_sig = bd->bd_sig;
1710 	d->bd_slen = bd->bd_slen;
1711 	d->bd_hlen = bd->bd_hlen;
1712 	d->bd_bufsize = bd->bd_bufsize;
1713 	d->bd_pid = bd->bd_pid;
1714 	strlcpy(d->bd_ifname,
1715 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
1716 	d->bd_locked = bd->bd_locked;
1717 }
1718 
1719 static int
1720 bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
1721 {
1722 	struct xbpf_d *xbdbuf, *xbd;
1723 	int index, error;
1724 	struct bpf_if *bp;
1725 	struct bpf_d *bd;
1726 
1727 	/*
1728 	 * XXX This is not technically correct. It is possible for non
1729 	 * privileged users to open bpf devices. It would make sense
1730 	 * if the users who opened the devices were able to retrieve
1731 	 * the statistics for them, too.
1732 	 */
1733 	error = suser(req->td);
1734 	if (error)
1735 		return (error);
1736 	if (req->oldptr == NULL)
1737 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
1738 	if (bpf_bpfd_cnt == 0)
1739 		return (SYSCTL_OUT(req, 0, 0));
1740 	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
1741 	mtx_lock(&bpf_mtx);
1742 	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
1743 		mtx_unlock(&bpf_mtx);
1744 		free(xbdbuf, M_BPF);
1745 		return (ENOMEM);
1746 	}
1747 	index = 0;
1748 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1749 		BPFIF_LOCK(bp);
1750 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
1751 			xbd = &xbdbuf[index++];
1752 			BPFD_LOCK(bd);
1753 			bpfstats_fill_xbpf(xbd, bd);
1754 			BPFD_UNLOCK(bd);
1755 		}
1756 		BPFIF_UNLOCK(bp);
1757 	}
1758 	mtx_unlock(&bpf_mtx);
1759 	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
1760 	free(xbdbuf, M_BPF);
1761 	return (error);
1762 }
1763 
1764 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
1765 
1766 #else /* !DEV_BPF && !NETGRAPH_BPF */
1767 /*
1768  * NOP stubs to allow bpf-using drivers to load and function.
1769  *
1770  * A 'better' implementation would allow the core bpf functionality
1771  * to be loaded at runtime.
1772  */
1773 static struct bpf_if bp_null;
1774 
1775 void
1776 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1777 {
1778 }
1779 
1780 void
1781 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1782 {
1783 }
1784 
1785 void
1786 bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
1787 {
1788 }
1789 
1790 void
1791 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1792 {
1793 
1794 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1795 }
1796 
1797 void
1798 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1799 {
1800 
1801 	*driverp = &bp_null;
1802 }
1803 
1804 void
1805 bpfdetach(struct ifnet *ifp)
1806 {
1807 }
1808 
1809 u_int
1810 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1811 {
1812 	return -1;	/* "no filter" behaviour */
1813 }
1814 
1815 int
1816 bpf_validate(const struct bpf_insn *f, int len)
1817 {
1818 	return 0;		/* false */
1819 }
1820 
1821 #endif /* !DEV_BPF && !NETGRAPH_BPF */
1822