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