xref: /netbsd-src/sys/net/bpf.c (revision 8e33eff89e26cf71871ead62f0d5063e1313c33a)
1 /*	$NetBSD: bpf.c,v 1.257 2024/08/19 07:47:16 ozaki-r Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from the Stanford/CMU enet packet filter,
8  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10  * Berkeley Laboratory.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)bpf.c	8.4 (Berkeley) 1/9/95
37  * static char rcsid[] =
38  * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp ";
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.257 2024/08/19 07:47:16 ozaki-r Exp $");
43 
44 #if defined(_KERNEL_OPT)
45 #include "opt_bpf.h"
46 #include "sl.h"
47 #include "opt_net_mpsafe.h"
48 #endif
49 
50 #include <sys/param.h>
51 
52 #include <sys/atomic.h>
53 #include <sys/buf.h>
54 #include <sys/conf.h>
55 #include <sys/cpu.h>
56 #include <sys/errno.h>
57 #include <sys/file.h>
58 #include <sys/filedesc.h>
59 #include <sys/ioctl.h>
60 #include <sys/kauth.h>
61 #include <sys/kernel.h>
62 #include <sys/lwp.h>
63 #include <sys/mbuf.h>
64 #include <sys/module.h>
65 #include <sys/percpu.h>
66 #include <sys/poll.h>
67 #include <sys/proc.h>
68 #include <sys/protosw.h>
69 #include <sys/pserialize.h>
70 #include <sys/queue.h>
71 #include <sys/socket.h>
72 #include <sys/stat.h>
73 #include <sys/sysctl.h>
74 #include <sys/syslog.h>
75 #include <sys/systm.h>
76 #include <sys/time.h>
77 #include <sys/tty.h>
78 #include <sys/uio.h>
79 #include <sys/vnode.h>
80 #include <sys/xcall.h>
81 
82 #include <net/bpf.h>
83 #include <net/bpfdesc.h>
84 #include <net/bpfjit.h>
85 #include <net/if.h>
86 #include <net/if_arc.h>
87 #include <net/if_ether.h>
88 #include <net/if_types.h>
89 #include <net/slip.h>
90 
91 #include <netinet/if_inarp.h>
92 #include <netinet/in.h>
93 
94 #include <compat/sys/sockio.h>
95 
96 #ifndef BPF_BUFSIZE
97 /*
98  * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet
99  * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k).
100  */
101 # define BPF_BUFSIZE 32768
102 #endif
103 
104 #define PRINET  26			/* interruptible */
105 
106 /*
107  * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able.
108  * XXX the default values should be computed dynamically based
109  * on available memory size and available mbuf clusters.
110  */
111 static int bpf_bufsize = BPF_BUFSIZE;
112 static int bpf_maxbufsize = BPF_DFLTBUFSIZE;	/* XXX set dynamically, see above */
113 static bool bpf_jit = false;
114 
115 struct bpfjit_ops bpfjit_module_ops = {
116 	.bj_generate_code = NULL,
117 	.bj_free_code = NULL
118 };
119 
120 /*
121  * Global BPF statistics returned by net.bpf.stats sysctl.
122  */
123 static struct percpu	*bpf_gstats_percpu; /* struct bpf_stat */
124 
125 #define BPF_STATINC(id)					\
126 	{						\
127 		struct bpf_stat *__stats =		\
128 		    percpu_getref(bpf_gstats_percpu);	\
129 		__stats->bs_##id++;			\
130 		percpu_putref(bpf_gstats_percpu);	\
131 	}
132 
133 /*
134  * Locking notes:
135  * - bpf_mtx (adaptive mutex) protects:
136  *   - Gobal lists: bpf_iflist and bpf_dlist
137  *   - struct bpf_if
138  *   - bpf_close
139  *   - bpf_psz (pserialize)
140  * - struct bpf_d has two mutexes:
141  *   - bd_buf_mtx (spin mutex) protects the buffers that can be accessed
142  *     on packet tapping
143  *   - bd_mtx (adaptive mutex) protects member variables other than the buffers
144  * - Locking order: bpf_mtx => bpf_d#bd_mtx => bpf_d#bd_buf_mtx
145  * - struct bpf_d obtained via fp->f_bpf in bpf_read and bpf_write is
146  *   never freed because struct bpf_d is only freed in bpf_close and
147  *   bpf_close never be called while executing bpf_read and bpf_write
148  * - A filter that is assigned to bpf_d can be replaced with another filter
149  *   while tapping packets, so it needs to be done atomically
150  * - struct bpf_d is iterated on bpf_dlist with psz
151  * - struct bpf_if is iterated on bpf_iflist with psz or psref
152  */
153 /*
154  * Use a mutex to avoid a race condition between gathering the stats/peers
155  * and opening/closing the device.
156  */
157 static kmutex_t bpf_mtx;
158 
159 static struct psref_class	*bpf_psref_class __read_mostly;
160 static pserialize_t		bpf_psz;
161 
162 static inline void
163 bpf_if_acquire(struct bpf_if *bp, struct psref *psref)
164 {
165 
166 	psref_acquire(psref, &bp->bif_psref, bpf_psref_class);
167 }
168 
169 static inline void
170 bpf_if_release(struct bpf_if *bp, struct psref *psref)
171 {
172 
173 	psref_release(psref, &bp->bif_psref, bpf_psref_class);
174 }
175 
176 /*
177  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
178  *  bpf_dtab holds the descriptors, indexed by minor device #
179  */
180 static struct pslist_head bpf_iflist;
181 static struct pslist_head bpf_dlist;
182 
183 /* Macros for bpf_d on bpf_dlist */
184 #define BPF_DLIST_WRITER_INSERT_HEAD(__d)				\
185 	PSLIST_WRITER_INSERT_HEAD(&bpf_dlist, (__d), bd_bpf_dlist_entry)
186 #define BPF_DLIST_READER_FOREACH(__d)					\
187 	PSLIST_READER_FOREACH((__d), &bpf_dlist, struct bpf_d,		\
188 	    bd_bpf_dlist_entry)
189 #define BPF_DLIST_WRITER_FOREACH(__d)					\
190 	PSLIST_WRITER_FOREACH((__d), &bpf_dlist, struct bpf_d,		\
191 	    bd_bpf_dlist_entry)
192 #define BPF_DLIST_ENTRY_INIT(__d)					\
193 	PSLIST_ENTRY_INIT((__d), bd_bpf_dlist_entry)
194 #define BPF_DLIST_WRITER_REMOVE(__d)					\
195 	PSLIST_WRITER_REMOVE((__d), bd_bpf_dlist_entry)
196 #define BPF_DLIST_ENTRY_DESTROY(__d)					\
197 	PSLIST_ENTRY_DESTROY((__d), bd_bpf_dlist_entry)
198 
199 /* Macros for bpf_if on bpf_iflist */
200 #define BPF_IFLIST_WRITER_INSERT_HEAD(__bp)				\
201 	PSLIST_WRITER_INSERT_HEAD(&bpf_iflist, (__bp), bif_iflist_entry)
202 #define BPF_IFLIST_READER_FOREACH(__bp)					\
203 	PSLIST_READER_FOREACH((__bp), &bpf_iflist, struct bpf_if,	\
204 	    bif_iflist_entry)
205 #define BPF_IFLIST_WRITER_FOREACH(__bp)					\
206 	PSLIST_WRITER_FOREACH((__bp), &bpf_iflist, struct bpf_if,	\
207 	    bif_iflist_entry)
208 #define BPF_IFLIST_WRITER_REMOVE(__bp)					\
209 	PSLIST_WRITER_REMOVE((__bp), bif_iflist_entry)
210 #define BPF_IFLIST_ENTRY_INIT(__bp)					\
211 	PSLIST_ENTRY_INIT((__bp), bif_iflist_entry)
212 #define BPF_IFLIST_ENTRY_DESTROY(__bp)					\
213 	PSLIST_ENTRY_DESTROY((__bp), bif_iflist_entry)
214 
215 /* Macros for bpf_d on bpf_if#bif_dlist_pslist */
216 #define BPFIF_DLIST_READER_FOREACH(__d, __bp)				\
217 	PSLIST_READER_FOREACH((__d), &(__bp)->bif_dlist_head, struct bpf_d, \
218 	    bd_bif_dlist_entry)
219 #define BPFIF_DLIST_WRITER_INSERT_HEAD(__bp, __d)			\
220 	PSLIST_WRITER_INSERT_HEAD(&(__bp)->bif_dlist_head, (__d),	\
221 	    bd_bif_dlist_entry)
222 #define BPFIF_DLIST_WRITER_REMOVE(__d)					\
223 	PSLIST_WRITER_REMOVE((__d), bd_bif_dlist_entry)
224 #define BPFIF_DLIST_ENTRY_INIT(__d)					\
225 	PSLIST_ENTRY_INIT((__d), bd_bif_dlist_entry)
226 #define	BPFIF_DLIST_READER_EMPTY(__bp)					\
227 	(PSLIST_READER_FIRST(&(__bp)->bif_dlist_head, struct bpf_d,	\
228 	    bd_bif_dlist_entry) == NULL)
229 #define	BPFIF_DLIST_WRITER_EMPTY(__bp)					\
230 	(PSLIST_WRITER_FIRST(&(__bp)->bif_dlist_head, struct bpf_d,	\
231 	    bd_bif_dlist_entry) == NULL)
232 #define BPFIF_DLIST_ENTRY_DESTROY(__d)					\
233 	PSLIST_ENTRY_DESTROY((__d), bd_bif_dlist_entry)
234 
235 static int	bpf_allocbufs(struct bpf_d *);
236 static u_int	bpf_xfilter(struct bpf_filter **, void *, u_int, u_int);
237 static void	bpf_deliver(struct bpf_if *,
238 		    void *(*cpfn)(void *, const void *, size_t),
239 		    void *, u_int, u_int, const u_int);
240 static void	bpf_freed(struct bpf_d *);
241 static void	bpf_free_filter(struct bpf_filter *);
242 static void	bpf_ifname(struct ifnet *, struct ifreq *);
243 static void	*bpf_mcpy(void *, const void *, size_t);
244 static int	bpf_movein(struct ifnet *, struct uio *, int, uint64_t,
245 		    struct mbuf **, struct sockaddr *,
246 		    struct bpf_filter **);
247 static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
248 static void	bpf_detachd(struct bpf_d *);
249 static int	bpf_setif(struct bpf_d *, struct ifreq *);
250 static int	bpf_setf(struct bpf_d *, struct bpf_program *, u_long);
251 static void	bpf_timed_out(void *);
252 static inline void
253 		bpf_wakeup(struct bpf_d *);
254 static int	bpf_hdrlen(struct bpf_d *);
255 static void	catchpacket(struct bpf_d *, u_char *, u_int, u_int,
256 		    void *(*)(void *, const void *, size_t),
257 		    struct timespec *);
258 static void	reset_d(struct bpf_d *);
259 static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
260 static int	bpf_setdlt(struct bpf_d *, u_int);
261 
262 static int	bpf_read(struct file *, off_t *, struct uio *, kauth_cred_t,
263 		    int);
264 static int	bpf_write(struct file *, off_t *, struct uio *, kauth_cred_t,
265 		    int);
266 static int	bpf_ioctl(struct file *, u_long, void *);
267 static int	bpf_poll(struct file *, int);
268 static int	bpf_stat(struct file *, struct stat *);
269 static int	bpf_close(struct file *);
270 static int	bpf_kqfilter(struct file *, struct knote *);
271 static void	bpf_softintr(void *);
272 
273 static const struct fileops bpf_fileops = {
274 	.fo_name = "bpf",
275 	.fo_read = bpf_read,
276 	.fo_write = bpf_write,
277 	.fo_ioctl = bpf_ioctl,
278 	.fo_fcntl = fnullop_fcntl,
279 	.fo_poll = bpf_poll,
280 	.fo_stat = bpf_stat,
281 	.fo_close = bpf_close,
282 	.fo_kqfilter = bpf_kqfilter,
283 	.fo_restart = fnullop_restart,
284 };
285 
286 dev_type_open(bpfopen);
287 
288 const struct cdevsw bpf_cdevsw = {
289 	.d_open = bpfopen,
290 	.d_close = noclose,
291 	.d_read = noread,
292 	.d_write = nowrite,
293 	.d_ioctl = noioctl,
294 	.d_stop = nostop,
295 	.d_tty = notty,
296 	.d_poll = nopoll,
297 	.d_mmap = nommap,
298 	.d_kqfilter = nokqfilter,
299 	.d_discard = nodiscard,
300 	.d_flag = D_OTHER | D_MPSAFE
301 };
302 
303 bpfjit_func_t
304 bpf_jit_generate(bpf_ctx_t *bc, void *code, size_t size)
305 {
306 	struct bpfjit_ops *ops = &bpfjit_module_ops;
307 	bpfjit_func_t (*generate_code)(const bpf_ctx_t *,
308 	    const struct bpf_insn *, size_t);
309 
310 	generate_code = atomic_load_acquire(&ops->bj_generate_code);
311 	if (generate_code != NULL) {
312 		return generate_code(bc, code, size);
313 	}
314 	return NULL;
315 }
316 
317 void
318 bpf_jit_freecode(bpfjit_func_t jcode)
319 {
320 	KASSERT(bpfjit_module_ops.bj_free_code != NULL);
321 	bpfjit_module_ops.bj_free_code(jcode);
322 }
323 
324 static int
325 bpf_movein(struct ifnet *ifp, struct uio *uio, int linktype, uint64_t mtu,
326     struct mbuf **mp, struct sockaddr *sockp, struct bpf_filter **wfilter)
327 {
328 	struct mbuf *m, *m0, *n;
329 	int error;
330 	size_t len;
331 	size_t hlen;
332 	size_t align;
333 	u_int slen;
334 
335 	/*
336 	 * Build a sockaddr based on the data link layer type.
337 	 * We do this at this level because the ethernet header
338 	 * is copied directly into the data field of the sockaddr.
339 	 * In the case of SLIP, there is no header and the packet
340 	 * is forwarded as is.
341 	 * Also, we are careful to leave room at the front of the mbuf
342 	 * for the link level header.
343 	 */
344 	switch (linktype) {
345 
346 	case DLT_SLIP:
347 		sockp->sa_family = AF_INET;
348 		hlen = 0;
349 		align = 0;
350 		break;
351 
352 	case DLT_PPP:
353 		sockp->sa_family = AF_UNSPEC;
354 		hlen = 0;
355 		align = 0;
356 		break;
357 
358 	case DLT_EN10MB:
359 		sockp->sa_family = AF_UNSPEC;
360 		/* XXX Would MAXLINKHDR be better? */
361  		/* 6(dst)+6(src)+2(type) */
362 		hlen = sizeof(struct ether_header);
363 		align = 2;
364 		break;
365 
366 	case DLT_ARCNET:
367 		sockp->sa_family = AF_UNSPEC;
368 		hlen = ARC_HDRLEN;
369 		align = 5;
370 		break;
371 
372 	case DLT_FDDI:
373 		sockp->sa_family = AF_LINK;
374 		/* XXX 4(FORMAC)+6(dst)+6(src) */
375 		hlen = 16;
376 		align = 0;
377 		break;
378 
379 	case DLT_ECONET:
380 		sockp->sa_family = AF_UNSPEC;
381 		hlen = 6;
382 		align = 2;
383 		break;
384 
385 	case DLT_NULL:
386 		sockp->sa_family = AF_UNSPEC;
387 		if (ifp->if_type == IFT_LOOP) {
388 			/* Set here to apply the following validations */
389 			hlen = sizeof(uint32_t);
390 		} else
391 			hlen = 0;
392 		align = 0;
393 		break;
394 
395 	default:
396 		return (EIO);
397 	}
398 
399 	len = uio->uio_resid;
400 	/*
401 	 * If there aren't enough bytes for a link level header or the
402 	 * packet length exceeds the interface mtu, return an error.
403 	 */
404 	if (len - hlen > mtu)
405 		return (EMSGSIZE);
406 
407 	m0 = m = m_gethdr(M_WAIT, MT_DATA);
408 	m_reset_rcvif(m);
409 	m->m_pkthdr.len = (int)(len - hlen);
410 	if (len + align > MHLEN) {
411 		m_clget(m, M_WAIT);
412 		if ((m->m_flags & M_EXT) == 0) {
413 			error = ENOBUFS;
414 			goto bad;
415 		}
416 	}
417 
418 	/* Ensure the data is properly aligned */
419 	if (align > 0)
420 		m->m_data += align;
421 
422 	for (;;) {
423 		len = M_TRAILINGSPACE(m);
424 		if (len > uio->uio_resid)
425 			len = uio->uio_resid;
426 		error = uiomove(mtod(m, void *), len, uio);
427 		if (error)
428 			goto bad;
429 		m->m_len = len;
430 
431 		if (uio->uio_resid == 0)
432 			break;
433 
434 		n = m_get(M_WAIT, MT_DATA);
435 		m_clget(n, M_WAIT);	/* if fails, there is no problem */
436 		m->m_next = n;
437 		m = n;
438 	}
439 
440 	slen = bpf_xfilter(wfilter, mtod(m, u_char *), len, len);
441 	if (slen == 0) {
442 		error = EPERM;
443 		goto bad;
444 	}
445 
446 	if (hlen != 0) {
447 		if (linktype == DLT_NULL && ifp->if_type == IFT_LOOP) {
448 			uint32_t af;
449 			/* the link header indicates the address family */
450 			memcpy(&af, mtod(m0, void *), sizeof(af));
451 			sockp->sa_family = af;
452 		} else {
453 			/* move link level header in the top of mbuf to sa_data */
454 			memcpy(sockp->sa_data, mtod(m0, void *), hlen);
455 		}
456 		m0->m_data += hlen;
457 		m0->m_len -= hlen;
458 	}
459 
460 	*mp = m0;
461 	return (0);
462 
463 bad:
464 	m_freem(m0);
465 	return (error);
466 }
467 
468 /*
469  * Attach file to the bpf interface, i.e. make d listen on bp.
470  */
471 static void
472 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
473 {
474 	struct bpf_event_tracker *t;
475 
476 	KASSERT(mutex_owned(&bpf_mtx));
477 	KASSERT(mutex_owned(d->bd_mtx));
478 	/*
479 	 * Point d at bp, and add d to the interface's list of listeners.
480 	 * Finally, point the driver's bpf cookie at the interface so
481 	 * it will divert packets to bpf.
482 	 */
483 	d->bd_bif = bp;
484 	BPFIF_DLIST_WRITER_INSERT_HEAD(bp, d);
485 
486 	*bp->bif_driverp = bp;
487 
488 	SLIST_FOREACH(t, &bp->bif_trackers, bet_entries) {
489 		t->bet_notify(bp, bp->bif_ifp, bp->bif_dlt,
490 		    BPF_TRACK_EVENT_ATTACH);
491 	}
492 }
493 
494 /*
495  * Detach a file from its interface.
496  */
497 static void
498 bpf_detachd(struct bpf_d *d)
499 {
500 	struct bpf_if *bp;
501 	struct bpf_event_tracker *t;
502 
503 	KASSERT(mutex_owned(&bpf_mtx));
504 	KASSERT(mutex_owned(d->bd_mtx));
505 
506 	bp = d->bd_bif;
507 	/*
508 	 * Check if this descriptor had requested promiscuous mode.
509 	 * If so, turn it off.
510 	 */
511 	if (d->bd_promisc) {
512 		int error __diagused;
513 
514 		d->bd_promisc = 0;
515 		/*
516 		 * Take device out of promiscuous mode.  Since we were
517 		 * able to enter promiscuous mode, we should be able
518 		 * to turn it off.  But we can get an error if
519 		 * the interface was configured down, so only panic
520 		 * if we don't get an unexpected error.
521 		 */
522 		KERNEL_LOCK_UNLESS_NET_MPSAFE();
523   		error = ifpromisc(bp->bif_ifp, 0);
524 		KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
525 #ifdef DIAGNOSTIC
526 		if (error)
527 			printf("%s: ifpromisc failed: %d", __func__, error);
528 #endif
529 	}
530 
531 	/* Remove d from the interface's descriptor list. */
532 	BPFIF_DLIST_WRITER_REMOVE(d);
533 
534 	pserialize_perform(bpf_psz);
535 
536 	if (BPFIF_DLIST_WRITER_EMPTY(bp)) {
537 		/*
538 		 * Let the driver know that there are no more listeners.
539 		 */
540 		*d->bd_bif->bif_driverp = NULL;
541 	}
542 
543 	d->bd_bif = NULL;
544 
545 	SLIST_FOREACH(t, &bp->bif_trackers, bet_entries) {
546 		t->bet_notify(bp, bp->bif_ifp, bp->bif_dlt,
547 		    BPF_TRACK_EVENT_DETACH);
548 	}
549 }
550 
551 static void
552 bpf_init(void)
553 {
554 
555 	mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE);
556 	bpf_psz = pserialize_create();
557 	bpf_psref_class = psref_class_create("bpf", IPL_SOFTNET);
558 
559 	PSLIST_INIT(&bpf_iflist);
560 	PSLIST_INIT(&bpf_dlist);
561 
562 	bpf_gstats_percpu = percpu_alloc(sizeof(struct bpf_stat));
563 
564 	return;
565 }
566 
567 /*
568  * bpfilterattach() is called at boot time.  We don't need to do anything
569  * here, since any initialization will happen as part of module init code.
570  */
571 /* ARGSUSED */
572 void
573 bpfilterattach(int n)
574 {
575 
576 }
577 
578 /*
579  * Open ethernet device. Clones.
580  */
581 /* ARGSUSED */
582 int
583 bpfopen(dev_t dev, int flag, int mode, struct lwp *l)
584 {
585 	struct bpf_d *d;
586 	struct file *fp;
587 	int error, fd;
588 
589 	/* falloc() will fill in the descriptor for us. */
590 	if ((error = fd_allocfile(&fp, &fd)) != 0)
591 		return error;
592 
593 	d = kmem_zalloc(sizeof(*d), KM_SLEEP);
594 	d->bd_bufsize = bpf_bufsize;
595 	d->bd_direction = BPF_D_INOUT;
596 	d->bd_feedback = 0;
597 	d->bd_pid = l->l_proc->p_pid;
598 #ifdef _LP64
599 	if (curproc->p_flag & PK_32)
600 		d->bd_compat32 = 1;
601 #endif
602 	getnanotime(&d->bd_btime);
603 	d->bd_atime = d->bd_mtime = d->bd_btime;
604 	callout_init(&d->bd_callout, CALLOUT_MPSAFE);
605 	selinit(&d->bd_sel);
606 	d->bd_sih = softint_establish(SOFTINT_CLOCK, bpf_softintr, d);
607 	d->bd_jitcode = NULL;
608 	d->bd_rfilter = NULL;
609 	d->bd_wfilter = NULL;
610 	d->bd_locked = 0;
611 	BPF_DLIST_ENTRY_INIT(d);
612 	BPFIF_DLIST_ENTRY_INIT(d);
613 	d->bd_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
614 	d->bd_buf_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
615 	cv_init(&d->bd_cv, "bpf");
616 
617 	mutex_enter(&bpf_mtx);
618 	BPF_DLIST_WRITER_INSERT_HEAD(d);
619 	mutex_exit(&bpf_mtx);
620 
621 	return fd_clone(fp, fd, flag, &bpf_fileops, d);
622 }
623 
624 /*
625  * Close the descriptor by detaching it from its interface,
626  * deallocating its buffers, and marking it free.
627  */
628 /* ARGSUSED */
629 static int
630 bpf_close(struct file *fp)
631 {
632 	struct bpf_d *d;
633 
634 	mutex_enter(&bpf_mtx);
635 
636 	if ((d = fp->f_bpf) == NULL) {
637 		mutex_exit(&bpf_mtx);
638 		return 0;
639 	}
640 
641 	/*
642 	 * Refresh the PID associated with this bpf file.
643 	 */
644 	d->bd_pid = curproc->p_pid;
645 
646 	mutex_enter(d->bd_buf_mtx);
647 	if (d->bd_state == BPF_WAITING)
648 		callout_halt(&d->bd_callout, d->bd_buf_mtx);
649 	d->bd_state = BPF_IDLE;
650 	mutex_exit(d->bd_buf_mtx);
651 	mutex_enter(d->bd_mtx);
652 	if (d->bd_bif)
653 		bpf_detachd(d);
654 	mutex_exit(d->bd_mtx);
655 
656 	BPF_DLIST_WRITER_REMOVE(d);
657 
658 	pserialize_perform(bpf_psz);
659 	mutex_exit(&bpf_mtx);
660 
661 	BPFIF_DLIST_ENTRY_DESTROY(d);
662 	BPF_DLIST_ENTRY_DESTROY(d);
663 	fp->f_bpf = NULL;
664 	bpf_freed(d);
665 	callout_destroy(&d->bd_callout);
666 	seldestroy(&d->bd_sel);
667 	softint_disestablish(d->bd_sih);
668 	mutex_obj_free(d->bd_mtx);
669 	mutex_obj_free(d->bd_buf_mtx);
670 	cv_destroy(&d->bd_cv);
671 
672 	kmem_free(d, sizeof(*d));
673 
674 	return (0);
675 }
676 
677 /*
678  * Rotate the packet buffers in descriptor d.  Move the store buffer
679  * into the hold slot, and the free buffer into the store slot.
680  * Zero the length of the new store buffer.
681  */
682 #define ROTATE_BUFFERS(d) \
683 	(d)->bd_hbuf = (d)->bd_sbuf; \
684 	(d)->bd_hlen = (d)->bd_slen; \
685 	(d)->bd_sbuf = (d)->bd_fbuf; \
686 	(d)->bd_slen = 0; \
687 	(d)->bd_fbuf = NULL;
688 /*
689  *  bpfread - read next chunk of packets from buffers
690  */
691 static int
692 bpf_read(struct file *fp, off_t *offp, struct uio *uio,
693     kauth_cred_t cred, int flags)
694 {
695 	struct bpf_d *d = fp->f_bpf;
696 	int timed_out;
697 	int error;
698 
699 	/*
700 	 * Refresh the PID associated with this bpf file.
701 	 */
702 	d->bd_pid = curproc->p_pid;
703 
704 	getnanotime(&d->bd_atime);
705 	/*
706 	 * Restrict application to use a buffer the same size as
707 	 * the kernel buffers.
708 	 */
709 	if (uio->uio_resid != d->bd_bufsize)
710 		return (EINVAL);
711 
712 	mutex_enter(d->bd_buf_mtx);
713 	if (d->bd_state == BPF_WAITING)
714 		callout_halt(&d->bd_callout, d->bd_buf_mtx);
715 	timed_out = (d->bd_state == BPF_TIMED_OUT);
716 	d->bd_state = BPF_IDLE;
717 	mutex_exit(d->bd_buf_mtx);
718 	/*
719 	 * If the hold buffer is empty, then do a timed sleep, which
720 	 * ends when the timeout expires or when enough packets
721 	 * have arrived to fill the store buffer.
722 	 */
723 	mutex_enter(d->bd_buf_mtx);
724 	while (d->bd_hbuf == NULL) {
725 		if (fp->f_flag & FNONBLOCK) {
726 			if (d->bd_slen == 0) {
727 				error = EWOULDBLOCK;
728 				goto out;
729 			}
730 			ROTATE_BUFFERS(d);
731 			break;
732 		}
733 
734 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
735 			/*
736 			 * A packet(s) either arrived since the previous
737 			 * read or arrived while we were asleep.
738 			 * Rotate the buffers and return what's here.
739 			 */
740 			ROTATE_BUFFERS(d);
741 			break;
742 		}
743 
744 		error = cv_timedwait_sig(&d->bd_cv, d->bd_buf_mtx, d->bd_rtout);
745 
746 		if (error == EINTR || error == ERESTART)
747 			goto out;
748 
749 		if (error == EWOULDBLOCK) {
750 			/*
751 			 * On a timeout, return what's in the buffer,
752 			 * which may be nothing.  If there is something
753 			 * in the store buffer, we can rotate the buffers.
754 			 */
755 			if (d->bd_hbuf)
756 				/*
757 				 * We filled up the buffer in between
758 				 * getting the timeout and arriving
759 				 * here, so we don't need to rotate.
760 				 */
761 				break;
762 
763 			if (d->bd_slen == 0) {
764 				error = 0;
765 				goto out;
766 			}
767 			ROTATE_BUFFERS(d);
768 			break;
769 		}
770 		if (error != 0)
771 			goto out;
772 	}
773 	/*
774 	 * At this point, we know we have something in the hold slot.
775 	 */
776 	mutex_exit(d->bd_buf_mtx);
777 
778 	/*
779 	 * Move data from hold buffer into user space.
780 	 * We know the entire buffer is transferred since
781 	 * we checked above that the read buffer is bpf_bufsize bytes.
782 	 */
783 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
784 
785 	mutex_enter(d->bd_buf_mtx);
786 	d->bd_fbuf = d->bd_hbuf;
787 	d->bd_hbuf = NULL;
788 	d->bd_hlen = 0;
789 out:
790 	mutex_exit(d->bd_buf_mtx);
791 	return (error);
792 }
793 
794 /*
795  * If there are processes sleeping on this descriptor, wake them up.
796  */
797 static inline void
798 bpf_wakeup(struct bpf_d *d)
799 {
800 
801 	KASSERT(mutex_owned(d->bd_buf_mtx));
802 
803 	cv_broadcast(&d->bd_cv);
804 
805 	if (d->bd_async)
806 		softint_schedule(d->bd_sih);
807 	selnotify(&d->bd_sel, 0, NOTE_SUBMIT);
808 }
809 
810 static void
811 bpf_softintr(void *cookie)
812 {
813 	struct bpf_d *d;
814 
815 	d = cookie;
816 	if (d->bd_async)
817 		fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL);
818 }
819 
820 static void
821 bpf_timed_out(void *arg)
822 {
823 	struct bpf_d *d = arg;
824 
825 	mutex_enter(d->bd_buf_mtx);
826 	if (d->bd_state == BPF_WAITING) {
827 		d->bd_state = BPF_TIMED_OUT;
828 		if (d->bd_slen != 0)
829 			bpf_wakeup(d);
830 	}
831 	mutex_exit(d->bd_buf_mtx);
832 }
833 
834 static int
835 bpf_write(struct file *fp, off_t *offp, struct uio *uio,
836     kauth_cred_t cred, int flags)
837 {
838 	struct bpf_d *d = fp->f_bpf;
839 	struct bpf_if *bp;
840 	struct ifnet *ifp;
841 	struct mbuf *m, *mc;
842 	int error;
843 	static struct sockaddr_storage dst;
844 	struct psref psref;
845 	int bound;
846 
847 	/*
848 	 * Refresh the PID associated with this bpf file.
849 	 */
850 	d->bd_pid = curproc->p_pid;
851 
852 	m = NULL;	/* XXX gcc */
853 
854 	bound = curlwp_bind();
855 	mutex_enter(d->bd_mtx);
856 	bp = d->bd_bif;
857 	if (bp == NULL) {
858 		mutex_exit(d->bd_mtx);
859 		error = ENXIO;
860 		goto out_bindx;
861 	}
862 	bpf_if_acquire(bp, &psref);
863 	mutex_exit(d->bd_mtx);
864 
865 	getnanotime(&d->bd_mtime);
866 
867 	ifp = bp->bif_ifp;
868 	if (if_is_deactivated(ifp)) {
869 		error = ENXIO;
870 		goto out;
871 	}
872 
873 	if (uio->uio_resid == 0) {
874 		error = 0;
875 		goto out;
876 	}
877 
878 	error = bpf_movein(ifp, uio, (int)bp->bif_dlt, ifp->if_mtu, &m,
879 	    (struct sockaddr *) &dst, &d->bd_wfilter);
880 	if (error)
881 		goto out;
882 
883 	if (m->m_pkthdr.len > ifp->if_mtu) {
884 		m_freem(m);
885 		error = EMSGSIZE;
886 		goto out;
887 	}
888 
889 	/*
890 	 * If writing to a loopback interface, the address family has
891 	 * already been specially computed in bpf_movein(), so don't
892 	 * clobber it, or the loopback will reject it in looutput().
893 	 */
894 	if (d->bd_hdrcmplt && ifp->if_type != IFT_LOOP)
895 		dst.ss_family = pseudo_AF_HDRCMPLT;
896 
897 	if (d->bd_feedback) {
898 		mc = m_dup(m, 0, M_COPYALL, M_NOWAIT);
899 		if (mc != NULL)
900 			m_set_rcvif(mc, ifp);
901 		/* Set M_PROMISC for outgoing packets to be discarded. */
902 		if (1 /*d->bd_direction == BPF_D_INOUT*/)
903 			m->m_flags |= M_PROMISC;
904 	} else
905 		mc = NULL;
906 
907 	error = if_output_lock(ifp, ifp, m, (struct sockaddr *) &dst, NULL);
908 
909 	if (mc != NULL) {
910 		if (error == 0) {
911 			int s = splsoftnet();
912 			KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp);
913 			ifp->_if_input(ifp, mc);
914 			KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp);
915 			splx(s);
916 		} else
917 			m_freem(mc);
918 	}
919 	/*
920 	 * The driver frees the mbuf.
921 	 */
922 out:
923 	bpf_if_release(bp, &psref);
924 out_bindx:
925 	curlwp_bindx(bound);
926 	return error;
927 }
928 
929 /*
930  * Reset a descriptor by flushing its packet buffer and clearing the
931  * receive and drop counts.
932  */
933 static void
934 reset_d(struct bpf_d *d)
935 {
936 
937 	KASSERT(mutex_owned(d->bd_mtx));
938 
939 	mutex_enter(d->bd_buf_mtx);
940 	if (d->bd_hbuf) {
941 		/* Free the hold buffer. */
942 		d->bd_fbuf = d->bd_hbuf;
943 		d->bd_hbuf = NULL;
944 	}
945 	d->bd_slen = 0;
946 	d->bd_hlen = 0;
947 	d->bd_rcount = 0;
948 	d->bd_dcount = 0;
949 	d->bd_ccount = 0;
950 	mutex_exit(d->bd_buf_mtx);
951 }
952 
953 /*
954  *  FIONREAD		Check for read packet available.
955  *  BIOCGBLEN		Get buffer len [for read()].
956  *  BIOCSETF		Set ethernet read filter.
957  *  BIOCFLUSH		Flush read packet buffer.
958  *  BIOCPROMISC		Put interface into promiscuous mode.
959  *  BIOCGDLT		Get link layer type.
960  *  BIOCGETIF		Get interface name.
961  *  BIOCSETIF		Set interface.
962  *  BIOCSRTIMEOUT	Set read timeout.
963  *  BIOCGRTIMEOUT	Get read timeout.
964  *  BIOCGSTATS		Get packet stats.
965  *  BIOCIMMEDIATE	Set immediate mode.
966  *  BIOCVERSION		Get filter language version.
967  *  BIOCGHDRCMPLT	Get "header already complete" flag.
968  *  BIOCSHDRCMPLT	Set "header already complete" flag.
969  *  BIOCSFEEDBACK	Set packet feedback mode.
970  *  BIOCGFEEDBACK	Get packet feedback mode.
971  *  BIOCGDIRECTION	Get packet direction flag
972  *  BIOCSDIRECTION	Set packet direction flag
973  */
974 /* ARGSUSED */
975 static int
976 bpf_ioctl(struct file *fp, u_long cmd, void *addr)
977 {
978 	struct bpf_d *d = fp->f_bpf;
979 	int error = 0;
980 
981 	/*
982 	 * Refresh the PID associated with this bpf file.
983 	 */
984 	d->bd_pid = curproc->p_pid;
985 #ifdef _LP64
986 	if (curproc->p_flag & PK_32)
987 		d->bd_compat32 = 1;
988 	else
989 		d->bd_compat32 = 0;
990 #endif
991 
992 	mutex_enter(d->bd_buf_mtx);
993 	if (d->bd_state == BPF_WAITING)
994 		callout_halt(&d->bd_callout, d->bd_buf_mtx);
995 	d->bd_state = BPF_IDLE;
996 	mutex_exit(d->bd_buf_mtx);
997 
998 	if (d->bd_locked) {
999 		switch (cmd) {
1000 		case BIOCGBLEN:		/* FALLTHROUGH */
1001 		case BIOCFLUSH:		/* FALLTHROUGH */
1002 		case BIOCGDLT:		/* FALLTHROUGH */
1003 		case BIOCGDLTLIST:	/* FALLTHROUGH */
1004 		case BIOCGETIF:		/* FALLTHROUGH */
1005 		case BIOCGRTIMEOUT:	/* FALLTHROUGH */
1006 		case BIOCGSTATS:	/* FALLTHROUGH */
1007 		case BIOCVERSION:	/* FALLTHROUGH */
1008 		case BIOCGHDRCMPLT:	/* FALLTHROUGH */
1009 		case FIONREAD:		/* FALLTHROUGH */
1010 		case BIOCLOCK:		/* FALLTHROUGH */
1011 		case BIOCSRTIMEOUT:	/* FALLTHROUGH */
1012 		case BIOCIMMEDIATE:	/* FALLTHROUGH */
1013 		case TIOCGPGRP:
1014 			break;
1015 		default:
1016 			return EPERM;
1017 		}
1018 	}
1019 
1020 	switch (cmd) {
1021 
1022 	default:
1023 		error = EINVAL;
1024 		break;
1025 
1026 	/*
1027 	 * Check for read packet available.
1028 	 */
1029 	case FIONREAD: {
1030 		int n;
1031 
1032 		mutex_enter(d->bd_buf_mtx);
1033 		n = d->bd_slen;
1034 		if (d->bd_hbuf)
1035 			n += d->bd_hlen;
1036 		mutex_exit(d->bd_buf_mtx);
1037 
1038 		*(int *)addr = n;
1039 		break;
1040 	}
1041 
1042 	/*
1043 	 * Get buffer len [for read()].
1044 	 */
1045 	case BIOCGBLEN:
1046 		*(u_int *)addr = d->bd_bufsize;
1047 		break;
1048 
1049 	/*
1050 	 * Set buffer length.
1051 	 */
1052 	case BIOCSBLEN:
1053 		/*
1054 		 * Forbid to change the buffer length if buffers are already
1055 		 * allocated.
1056 		 */
1057 		mutex_enter(d->bd_mtx);
1058 		mutex_enter(d->bd_buf_mtx);
1059 		if (d->bd_bif != NULL || d->bd_sbuf != NULL)
1060 			error = EINVAL;
1061 		else {
1062 			u_int size = *(u_int *)addr;
1063 
1064 			if (size > bpf_maxbufsize)
1065 				*(u_int *)addr = size = bpf_maxbufsize;
1066 			else if (size < BPF_MINBUFSIZE)
1067 				*(u_int *)addr = size = BPF_MINBUFSIZE;
1068 			d->bd_bufsize = size;
1069 		}
1070 		mutex_exit(d->bd_buf_mtx);
1071 		mutex_exit(d->bd_mtx);
1072 		break;
1073 
1074 	/*
1075 	 * Set link layer read filter.
1076 	 */
1077 	case BIOCSETF:		/* FALLTHROUGH */
1078 	case BIOCSETWF:
1079 		error = bpf_setf(d, addr, cmd);
1080 		break;
1081 
1082 	case BIOCLOCK:
1083 		d->bd_locked = 1;
1084 		break;
1085 
1086 	/*
1087 	 * Flush read packet buffer.
1088 	 */
1089 	case BIOCFLUSH:
1090 		mutex_enter(d->bd_mtx);
1091 		reset_d(d);
1092 		mutex_exit(d->bd_mtx);
1093 		break;
1094 
1095 	/*
1096 	 * Put interface into promiscuous mode.
1097 	 */
1098 	case BIOCPROMISC:
1099 		mutex_enter(d->bd_mtx);
1100 		if (d->bd_bif == NULL) {
1101 			mutex_exit(d->bd_mtx);
1102 			/*
1103 			 * No interface attached yet.
1104 			 */
1105 			error = EINVAL;
1106 			break;
1107 		}
1108 		if (d->bd_promisc == 0) {
1109 			KERNEL_LOCK_UNLESS_NET_MPSAFE();
1110 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
1111 			KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1112 			if (error == 0)
1113 				d->bd_promisc = 1;
1114 		}
1115 		mutex_exit(d->bd_mtx);
1116 		break;
1117 
1118 	/*
1119 	 * Get device parameters.
1120 	 */
1121 	case BIOCGDLT:
1122 		mutex_enter(d->bd_mtx);
1123 		if (d->bd_bif == NULL)
1124 			error = EINVAL;
1125 		else
1126 			*(u_int *)addr = d->bd_bif->bif_dlt;
1127 		mutex_exit(d->bd_mtx);
1128 		break;
1129 
1130 	/*
1131 	 * Get a list of supported device parameters.
1132 	 */
1133 	case BIOCGDLTLIST:
1134 		mutex_enter(d->bd_mtx);
1135 		if (d->bd_bif == NULL)
1136 			error = EINVAL;
1137 		else
1138 			error = bpf_getdltlist(d, addr);
1139 		mutex_exit(d->bd_mtx);
1140 		break;
1141 
1142 	/*
1143 	 * Set device parameters.
1144 	 */
1145 	case BIOCSDLT:
1146 		mutex_enter(&bpf_mtx);
1147 		mutex_enter(d->bd_mtx);
1148 		if (d->bd_bif == NULL)
1149 			error = EINVAL;
1150 		else
1151 			error = bpf_setdlt(d, *(u_int *)addr);
1152 		mutex_exit(d->bd_mtx);
1153 		mutex_exit(&bpf_mtx);
1154 		break;
1155 
1156 	/*
1157 	 * Set interface name.
1158 	 */
1159 #ifdef OBIOCGETIF
1160 	case OBIOCGETIF:
1161 #endif
1162 	case BIOCGETIF:
1163 		mutex_enter(d->bd_mtx);
1164 		if (d->bd_bif == NULL)
1165 			error = EINVAL;
1166 		else
1167 			bpf_ifname(d->bd_bif->bif_ifp, addr);
1168 		mutex_exit(d->bd_mtx);
1169 		break;
1170 
1171 	/*
1172 	 * Set interface.
1173 	 */
1174 #ifdef OBIOCSETIF
1175 	case OBIOCSETIF:
1176 #endif
1177 	case BIOCSETIF:
1178 		mutex_enter(&bpf_mtx);
1179 		error = bpf_setif(d, addr);
1180 		mutex_exit(&bpf_mtx);
1181 		break;
1182 
1183 	/*
1184 	 * Set read timeout.
1185 	 */
1186 	case BIOCSRTIMEOUT: {
1187 		struct timeval *tv = addr;
1188 
1189 		/* Compute number of ticks. */
1190 		if (tv->tv_sec < 0 ||
1191 		    tv->tv_usec < 0 || tv->tv_usec >= 1000000) {
1192 			error = EINVAL;
1193 			break;
1194 		} else if (tv->tv_sec > INT_MAX/hz - 1) {
1195 			d->bd_rtout = INT_MAX;
1196 		} else {
1197 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
1198 		}
1199 		if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
1200 			d->bd_rtout = 1;
1201 		break;
1202 	}
1203 
1204 #ifdef BIOCGORTIMEOUT
1205 	/*
1206 	 * Get read timeout.
1207 	 */
1208 	case BIOCGORTIMEOUT: {
1209 		struct timeval50 *tv = addr;
1210 
1211 		tv->tv_sec = d->bd_rtout / hz;
1212 		tv->tv_usec = (d->bd_rtout % hz) * tick;
1213 		break;
1214 	}
1215 #endif
1216 
1217 #ifdef BIOCSORTIMEOUT
1218 	/*
1219 	 * Set read timeout.
1220 	 */
1221 	case BIOCSORTIMEOUT: {
1222 		struct timeval50 *tv = addr;
1223 
1224 		/* Compute number of ticks. */
1225 		if (tv->tv_sec < 0 ||
1226 		    tv->tv_usec < 0 || tv->tv_usec >= 1000000) {
1227 			error = EINVAL;
1228 			break;
1229 		} else if (tv->tv_sec > INT_MAX/hz - 1) {
1230 			d->bd_rtout = INT_MAX;
1231 		} else {
1232 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
1233 		}
1234 		if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
1235 			d->bd_rtout = 1;
1236 		break;
1237 	}
1238 #endif
1239 
1240 	/*
1241 	 * Get read timeout.
1242 	 */
1243 	case BIOCGRTIMEOUT: {
1244 		struct timeval *tv = addr;
1245 
1246 		tv->tv_sec = d->bd_rtout / hz;
1247 		tv->tv_usec = (d->bd_rtout % hz) * tick;
1248 		break;
1249 	}
1250 	/*
1251 	 * Get packet stats.
1252 	 */
1253 	case BIOCGSTATS: {
1254 		struct bpf_stat *bs = addr;
1255 
1256 		bs->bs_recv = d->bd_rcount;
1257 		bs->bs_drop = d->bd_dcount;
1258 		bs->bs_capt = d->bd_ccount;
1259 		break;
1260 	}
1261 
1262 	case BIOCGSTATS_30: {
1263 		struct bpf_stat30 *bs = addr;
1264 
1265 		bs->bs_recv = d->bd_rcount;
1266 		bs->bs_drop = d->bd_dcount;
1267 		break;
1268 	}
1269 
1270 	/*
1271 	 * Set immediate mode.
1272 	 */
1273 	case BIOCIMMEDIATE:
1274 		d->bd_immediate = *(u_int *)addr;
1275 		break;
1276 
1277 	case BIOCVERSION: {
1278 		struct bpf_version *bv = addr;
1279 
1280 		bv->bv_major = BPF_MAJOR_VERSION;
1281 		bv->bv_minor = BPF_MINOR_VERSION;
1282 		break;
1283 	}
1284 
1285 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
1286 		*(u_int *)addr = d->bd_hdrcmplt;
1287 		break;
1288 
1289 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
1290 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1291 		break;
1292 
1293 	/*
1294 	 * Get packet direction flag
1295 	 */
1296 	case BIOCGDIRECTION:
1297 		*(u_int *)addr = d->bd_direction;
1298 		break;
1299 
1300 	/*
1301 	 * Set packet direction flag
1302 	 */
1303 	case BIOCSDIRECTION: {
1304 		u_int	direction;
1305 
1306 		direction = *(u_int *)addr;
1307 		switch (direction) {
1308 		case BPF_D_IN:
1309 		case BPF_D_INOUT:
1310 		case BPF_D_OUT:
1311 			d->bd_direction = direction;
1312 			break;
1313 		default:
1314 			error = EINVAL;
1315 		}
1316 	}
1317 	break;
1318 
1319 	/*
1320 	 * Set "feed packets from bpf back to input" mode
1321 	 */
1322 	case BIOCSFEEDBACK:
1323 		d->bd_feedback = *(u_int *)addr;
1324 		break;
1325 
1326 	/*
1327 	 * Get "feed packets from bpf back to input" mode
1328 	 */
1329 	case BIOCGFEEDBACK:
1330 		*(u_int *)addr = d->bd_feedback;
1331 		break;
1332 
1333 	case FIONBIO:		/* Non-blocking I/O */
1334 		/*
1335 		 * No need to do anything special as we use IO_NDELAY in
1336 		 * bpfread() as an indication of whether or not to block
1337 		 * the read.
1338 		 */
1339 		break;
1340 
1341 	case FIOASYNC:		/* Send signal on receive packets */
1342 		mutex_enter(d->bd_mtx);
1343 		d->bd_async = *(int *)addr;
1344 		mutex_exit(d->bd_mtx);
1345 		break;
1346 
1347 	case TIOCSPGRP:		/* Process or group to send signals to */
1348 	case FIOSETOWN:
1349 		error = fsetown(&d->bd_pgid, cmd, addr);
1350 		break;
1351 
1352 	case TIOCGPGRP:
1353 	case FIOGETOWN:
1354 		error = fgetown(d->bd_pgid, cmd, addr);
1355 		break;
1356 	}
1357 	return (error);
1358 }
1359 
1360 /*
1361  * Set d's packet filter program to fp.  If this file already has a filter,
1362  * free it and replace it.  Returns EINVAL for bogus requests.
1363  */
1364 static int
1365 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1366 {
1367 	struct bpf_insn *fcode;
1368 	bpfjit_func_t jcode;
1369 	size_t flen, size = 0;
1370 	struct bpf_filter *oldf, *newf, **storef;
1371 
1372 	jcode = NULL;
1373 	flen = fp->bf_len;
1374 
1375 	if ((fp->bf_insns == NULL && flen) || flen > BPF_MAXINSNS) {
1376 		return EINVAL;
1377 	}
1378 
1379 	if (flen) {
1380 		/*
1381 		 * Allocate the buffer, copy the byte-code from
1382 		 * userspace and validate it.
1383 		 */
1384 		size = flen * sizeof(*fp->bf_insns);
1385 		fcode = kmem_alloc(size, KM_SLEEP);
1386 		if (copyin(fp->bf_insns, fcode, size) != 0 ||
1387 		    !bpf_validate(fcode, (int)flen)) {
1388 			kmem_free(fcode, size);
1389 			return EINVAL;
1390 		}
1391 		if (bpf_jit)
1392 			jcode = bpf_jit_generate(NULL, fcode, flen);
1393 	} else {
1394 		fcode = NULL;
1395 	}
1396 
1397 	newf = kmem_alloc(sizeof(*newf), KM_SLEEP);
1398 	newf->bf_insn = fcode;
1399 	newf->bf_size = size;
1400 	newf->bf_jitcode = jcode;
1401 	if (cmd == BIOCSETF)
1402 		d->bd_jitcode = jcode; /* XXX just for kvm(3) users */
1403 
1404 	/* Need to hold bpf_mtx for pserialize_perform */
1405 	mutex_enter(&bpf_mtx);
1406 	mutex_enter(d->bd_mtx);
1407 	if (cmd == BIOCSETWF) {
1408 		oldf = d->bd_wfilter;
1409 		storef = &d->bd_wfilter;
1410 	} else {
1411 		oldf = d->bd_rfilter;
1412 		storef = &d->bd_rfilter;
1413 	}
1414 	atomic_store_release(storef, newf);
1415 	reset_d(d);
1416 	pserialize_perform(bpf_psz);
1417 	mutex_exit(d->bd_mtx);
1418 	mutex_exit(&bpf_mtx);
1419 
1420 	if (oldf != NULL)
1421 		bpf_free_filter(oldf);
1422 
1423 	return 0;
1424 }
1425 
1426 /*
1427  * Detach a file from its current interface (if attached at all) and attach
1428  * to the interface indicated by the name stored in ifr.
1429  * Return an errno or 0.
1430  */
1431 static int
1432 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1433 {
1434 	struct bpf_if *bp;
1435 	char *cp;
1436 	int unit_seen, i, error;
1437 
1438 	KASSERT(mutex_owned(&bpf_mtx));
1439 	/*
1440 	 * Make sure the provided name has a unit number, and default
1441 	 * it to '0' if not specified.
1442 	 * XXX This is ugly ... do this differently?
1443 	 */
1444 	unit_seen = 0;
1445 	cp = ifr->ifr_name;
1446 	cp[sizeof(ifr->ifr_name) - 1] = '\0';	/* sanity */
1447 	while (*cp++)
1448 		if (*cp >= '0' && *cp <= '9')
1449 			unit_seen = 1;
1450 	if (!unit_seen) {
1451 		/* Make sure to leave room for the '\0'. */
1452 		for (i = 0; i < (IFNAMSIZ - 1); ++i) {
1453 			if ((ifr->ifr_name[i] >= 'a' &&
1454 				ifr->ifr_name[i] <= 'z') ||
1455 			    (ifr->ifr_name[i] >= 'A' &&
1456 				ifr->ifr_name[i] <= 'Z'))
1457 				continue;
1458 			ifr->ifr_name[i] = '0';
1459 		}
1460 	}
1461 
1462 	/*
1463 	 * Look through attached interfaces for the named one.
1464 	 */
1465 	BPF_IFLIST_WRITER_FOREACH(bp) {
1466 		struct ifnet *ifp = bp->bif_ifp;
1467 
1468 		if (ifp == NULL ||
1469 		    strcmp(ifp->if_xname, ifr->ifr_name) != 0)
1470 			continue;
1471 		/* skip additional entry */
1472 		if (bp->bif_driverp != &ifp->if_bpf)
1473 			continue;
1474 		/*
1475 		 * We found the requested interface.
1476 		 * Allocate the packet buffers if we need to.
1477 		 * If we're already attached to requested interface,
1478 		 * just flush the buffer.
1479 		 */
1480 		/*
1481 		 * bpf_allocbufs is called only here. bpf_mtx ensures that
1482 		 * no race condition happen on d->bd_sbuf.
1483 		 */
1484 		if (d->bd_sbuf == NULL) {
1485 			error = bpf_allocbufs(d);
1486 			if (error != 0)
1487 				return (error);
1488 		}
1489 		mutex_enter(d->bd_mtx);
1490 		if (bp != d->bd_bif) {
1491 			if (d->bd_bif) {
1492 				/*
1493 				 * Detach if attached to something else.
1494 				 */
1495 				bpf_detachd(d);
1496 				BPFIF_DLIST_ENTRY_INIT(d);
1497 			}
1498 
1499 			bpf_attachd(d, bp);
1500 		}
1501 		reset_d(d);
1502 		mutex_exit(d->bd_mtx);
1503 		return (0);
1504 	}
1505 	/* Not found. */
1506 	return (ENXIO);
1507 }
1508 
1509 /*
1510  * Copy the interface name to the ifreq.
1511  */
1512 static void
1513 bpf_ifname(struct ifnet *ifp, struct ifreq *ifr)
1514 {
1515 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
1516 }
1517 
1518 static int
1519 bpf_stat(struct file *fp, struct stat *st)
1520 {
1521 	struct bpf_d *d = fp->f_bpf;
1522 
1523 	(void)memset(st, 0, sizeof(*st));
1524 	mutex_enter(d->bd_mtx);
1525 	st->st_dev = makedev(cdevsw_lookup_major(&bpf_cdevsw), d->bd_pid);
1526 	st->st_atimespec = d->bd_atime;
1527 	st->st_mtimespec = d->bd_mtime;
1528 	st->st_ctimespec = st->st_birthtimespec = d->bd_btime;
1529 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
1530 	st->st_gid = kauth_cred_getegid(fp->f_cred);
1531 	st->st_mode = S_IFCHR;
1532 	mutex_exit(d->bd_mtx);
1533 	return 0;
1534 }
1535 
1536 /*
1537  * Support for poll() system call
1538  *
1539  * Return true iff the specific operation will not block indefinitely - with
1540  * the assumption that it is safe to positively acknowledge a request for the
1541  * ability to write to the BPF device.
1542  * Otherwise, return false but make a note that a selnotify() must be done.
1543  */
1544 static int
1545 bpf_poll(struct file *fp, int events)
1546 {
1547 	struct bpf_d *d = fp->f_bpf;
1548 	int revents;
1549 
1550 	/*
1551 	 * Refresh the PID associated with this bpf file.
1552 	 */
1553 	mutex_enter(&bpf_mtx);
1554 	d->bd_pid = curproc->p_pid;
1555 
1556 	revents = events & (POLLOUT | POLLWRNORM);
1557 	if (events & (POLLIN | POLLRDNORM)) {
1558 		/*
1559 		 * An imitation of the FIONREAD ioctl code.
1560 		 */
1561 		mutex_enter(d->bd_mtx);
1562 		mutex_enter(d->bd_buf_mtx);
1563 		if (d->bd_hlen != 0 ||
1564 		    ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1565 			d->bd_slen != 0)) {
1566 			revents |= events & (POLLIN | POLLRDNORM);
1567 		} else {
1568 			selrecord(curlwp, &d->bd_sel);
1569 			/* Start the read timeout if necessary */
1570 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1571 				callout_reset(&d->bd_callout, d->bd_rtout,
1572 				    bpf_timed_out, d);
1573 				d->bd_state = BPF_WAITING;
1574 			}
1575 		}
1576 		mutex_exit(d->bd_buf_mtx);
1577 		mutex_exit(d->bd_mtx);
1578 	}
1579 
1580 	mutex_exit(&bpf_mtx);
1581 	return (revents);
1582 }
1583 
1584 static void
1585 filt_bpfrdetach(struct knote *kn)
1586 {
1587 	struct bpf_d *d = kn->kn_hook;
1588 
1589 	mutex_enter(d->bd_buf_mtx);
1590 	selremove_knote(&d->bd_sel, kn);
1591 	mutex_exit(d->bd_buf_mtx);
1592 }
1593 
1594 static int
1595 filt_bpfread(struct knote *kn, long hint)
1596 {
1597 	struct bpf_d *d = kn->kn_hook;
1598 	int rv;
1599 
1600 	/*
1601 	 * Refresh the PID associated with this bpf file.
1602 	 */
1603 	d->bd_pid = curproc->p_pid;
1604 
1605 	if (hint & NOTE_SUBMIT)
1606 		KASSERT(mutex_owned(d->bd_buf_mtx));
1607 	else
1608 		mutex_enter(d->bd_buf_mtx);
1609 	kn->kn_data = d->bd_hlen;
1610 	if (d->bd_immediate)
1611 		kn->kn_data += d->bd_slen;
1612 	rv = (kn->kn_data > 0);
1613 	if (hint & NOTE_SUBMIT)
1614 		KASSERT(mutex_owned(d->bd_buf_mtx));
1615 	else
1616 		mutex_exit(d->bd_buf_mtx);
1617 	return rv;
1618 }
1619 
1620 static const struct filterops bpfread_filtops = {
1621 	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
1622 	.f_attach = NULL,
1623 	.f_detach = filt_bpfrdetach,
1624 	.f_event = filt_bpfread,
1625 };
1626 
1627 static int
1628 bpf_kqfilter(struct file *fp, struct knote *kn)
1629 {
1630 	struct bpf_d *d = fp->f_bpf;
1631 
1632 	switch (kn->kn_filter) {
1633 	case EVFILT_READ:
1634 		kn->kn_fop = &bpfread_filtops;
1635 		break;
1636 
1637 	default:
1638 		return (EINVAL);
1639 	}
1640 
1641 	kn->kn_hook = d;
1642 
1643 	mutex_enter(d->bd_buf_mtx);
1644 	selrecord_knote(&d->bd_sel, kn);
1645 	mutex_exit(d->bd_buf_mtx);
1646 
1647 	return (0);
1648 }
1649 
1650 /*
1651  * Copy data from an mbuf chain into a buffer.  This code is derived
1652  * from m_copydata in sys/uipc_mbuf.c.
1653  */
1654 static void *
1655 bpf_mcpy(void *dst_arg, const void *src_arg, size_t len)
1656 {
1657 	const struct mbuf *m;
1658 	u_int count;
1659 	u_char *dst;
1660 
1661 	m = src_arg;
1662 	dst = dst_arg;
1663 	while (len > 0) {
1664 		if (m == NULL)
1665 			panic("bpf_mcpy");
1666 		count = uimin(m->m_len, len);
1667 		memcpy(dst, mtod(m, const void *), count);
1668 		m = m->m_next;
1669 		dst += count;
1670 		len -= count;
1671 	}
1672 	return dst_arg;
1673 }
1674 
1675 static inline u_int
1676 bpf_xfilter(struct bpf_filter **filter, void *pkt, u_int pktlen, u_int buflen)
1677 {
1678 	struct bpf_filter *filt;
1679 	uint32_t mem[BPF_MEMWORDS];
1680 	bpf_args_t args = {
1681 		.pkt = (const uint8_t *)pkt,
1682 		.wirelen = pktlen,
1683 		.buflen = buflen,
1684 		.mem = mem,
1685 		.arg = NULL
1686 	};
1687 	u_int slen;
1688 
1689 	filt = atomic_load_consume(filter);
1690 	if (filt == NULL) /* No filter means accept all. */
1691 		return (u_int)-1;
1692 
1693 	if (filt->bf_jitcode != NULL)
1694 		slen = filt->bf_jitcode(NULL, &args);
1695 	else
1696 		slen = bpf_filter_ext(NULL, filt->bf_insn, &args);
1697 	return slen;
1698 }
1699 
1700 /*
1701  * Dispatch a packet to all the listeners on interface bp.
1702  *
1703  * pkt       pointer to the packet, either a data buffer or an mbuf chain
1704  * buflen    buffer length, if pkt is a data buffer
1705  * cpfn      a function that can copy pkt into the listener's buffer
1706  * pktlen    length of the packet
1707  * direction BPF_D_IN or BPF_D_OUT
1708  */
1709 static inline void
1710 bpf_deliver(struct bpf_if *bp, void *(*cpfn)(void *, const void *, size_t),
1711     void *pkt, u_int pktlen, u_int buflen, const u_int direction)
1712 {
1713 	bool gottime = false;
1714 	struct timespec ts;
1715 	struct bpf_d *d;
1716 	int s;
1717 	u_int slen;
1718 
1719 	KASSERT(!cpu_intr_p());
1720 
1721 	/*
1722 	 * Note that the IPL does not have to be raised at this point.
1723 	 * The only problem that could arise here is that if two different
1724 	 * interfaces shared any data.  This is not the case.
1725 	 */
1726 	s = pserialize_read_enter();
1727 	BPFIF_DLIST_READER_FOREACH(d, bp) {
1728 		if (direction == BPF_D_IN) {
1729 			if (d->bd_direction == BPF_D_OUT)
1730 				continue;
1731 		} else { /* BPF_D_OUT */
1732 			if (d->bd_direction == BPF_D_IN)
1733 				continue;
1734 		}
1735 
1736 		atomic_inc_ulong(&d->bd_rcount);
1737 		BPF_STATINC(recv);
1738 
1739 		slen = bpf_xfilter(&d->bd_rfilter, pkt, pktlen, buflen);
1740 		if (slen == 0)
1741 			continue;
1742 
1743 		if (!gottime) {
1744 			gottime = true;
1745 			nanotime(&ts);
1746 		}
1747 		/* Assume catchpacket doesn't sleep */
1748 		catchpacket(d, pkt, pktlen, slen, cpfn, &ts);
1749 	}
1750 	pserialize_read_exit(s);
1751 }
1752 
1753 /*
1754  * Incoming linkage from device drivers, when the head of the packet is in
1755  * a buffer, and the tail is in an mbuf chain.
1756  */
1757 static void
1758 _bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m,
1759     u_int direction)
1760 {
1761 	u_int pktlen;
1762 	struct mbuf mb;
1763 
1764 	/* Skip outgoing duplicate packets. */
1765 	if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif_index == 0) {
1766 		m->m_flags &= ~M_PROMISC;
1767 		return;
1768 	}
1769 
1770 	pktlen = m_length(m) + dlen;
1771 
1772 	/*
1773 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1774 	 * Note that we cut corners here; we only set up what's
1775 	 * absolutely needed--this mbuf should never go anywhere else.
1776 	 */
1777 	(void)memset(&mb, 0, sizeof(mb));
1778 	mb.m_type = MT_DATA;
1779 	mb.m_next = m;
1780 	mb.m_data = data;
1781 	mb.m_len = dlen;
1782 
1783 	bpf_deliver(bp, bpf_mcpy, &mb, pktlen, 0, direction);
1784 }
1785 
1786 /*
1787  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1788  */
1789 static void
1790 _bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction)
1791 {
1792 	void *(*cpfn)(void *, const void *, size_t);
1793 	u_int pktlen, buflen;
1794 	void *marg;
1795 
1796 	/* Skip outgoing duplicate packets. */
1797 	if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif_index == 0) {
1798 		m->m_flags &= ~M_PROMISC;
1799 		return;
1800 	}
1801 
1802 	pktlen = m_length(m);
1803 
1804 	/* Skip zero-sized packets. */
1805 	if (__predict_false(pktlen == 0)) {
1806 		return;
1807 	}
1808 
1809 	if (pktlen == m->m_len) {
1810 		cpfn = (void *)memcpy;
1811 		marg = mtod(m, void *);
1812 		buflen = pktlen;
1813 		KASSERT(buflen != 0);
1814 	} else {
1815 		cpfn = bpf_mcpy;
1816 		marg = m;
1817 		buflen = 0;
1818 	}
1819 
1820 	bpf_deliver(bp, cpfn, marg, pktlen, buflen, direction);
1821 }
1822 
1823 /*
1824  * We need to prepend the address family as
1825  * a four byte field.  Cons up a dummy header
1826  * to pacify bpf.  This is safe because bpf
1827  * will only read from the mbuf (i.e., it won't
1828  * try to free it or keep a pointer a to it).
1829  */
1830 static void
1831 _bpf_mtap_af(struct bpf_if *bp, uint32_t af, struct mbuf *m, u_int direction)
1832 {
1833 	struct mbuf m0;
1834 
1835 	m0.m_type = MT_DATA;
1836 	m0.m_flags = 0;
1837 	m0.m_next = m;
1838 	m0.m_nextpkt = NULL;
1839 	m0.m_owner = NULL;
1840 	m0.m_len = 4;
1841 	m0.m_data = (char *)&af;
1842 
1843 	_bpf_mtap(bp, &m0, direction);
1844 }
1845 
1846 /*
1847  * Put the SLIP pseudo-"link header" in place.
1848  * Note this M_PREPEND() should never fail,
1849  * since we know we always have enough space
1850  * in the input buffer.
1851  */
1852 static void
1853 _bpf_mtap_sl_in(struct bpf_if *bp, u_char *chdr, struct mbuf **m)
1854 {
1855 	u_char *hp;
1856 
1857 	M_PREPEND(*m, SLIP_HDRLEN, M_DONTWAIT);
1858 	if (*m == NULL)
1859 		return;
1860 
1861 	hp = mtod(*m, u_char *);
1862 	hp[SLX_DIR] = SLIPDIR_IN;
1863 	(void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
1864 
1865 	_bpf_mtap(bp, *m, BPF_D_IN);
1866 
1867 	m_adj(*m, SLIP_HDRLEN);
1868 }
1869 
1870 /*
1871  * Put the SLIP pseudo-"link header" in
1872  * place.  The compressed header is now
1873  * at the beginning of the mbuf.
1874  */
1875 static void
1876 _bpf_mtap_sl_out(struct bpf_if *bp, u_char *chdr, struct mbuf *m)
1877 {
1878 	struct mbuf m0;
1879 	u_char *hp;
1880 
1881 	m0.m_type = MT_DATA;
1882 	m0.m_flags = 0;
1883 	m0.m_next = m;
1884 	m0.m_nextpkt = NULL;
1885 	m0.m_owner = NULL;
1886 	m0.m_data = m0.m_dat;
1887 	m0.m_len = SLIP_HDRLEN;
1888 
1889 	hp = mtod(&m0, u_char *);
1890 
1891 	hp[SLX_DIR] = SLIPDIR_OUT;
1892 	(void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
1893 
1894 	_bpf_mtap(bp, &m0, BPF_D_OUT);
1895 	m_freem(m);
1896 }
1897 
1898 static struct mbuf *
1899 bpf_mbuf_enqueue(struct bpf_if *bp, struct mbuf *m)
1900 {
1901 	struct mbuf *dup;
1902 
1903 	dup = m_dup(m, 0, M_COPYALL, M_NOWAIT);
1904 	if (dup == NULL)
1905 		return NULL;
1906 
1907 	if (bp->bif_mbuf_tail != NULL) {
1908 		bp->bif_mbuf_tail->m_nextpkt = dup;
1909 	} else {
1910 		bp->bif_mbuf_head = dup;
1911 	}
1912 	bp->bif_mbuf_tail = dup;
1913 #ifdef BPF_MTAP_SOFTINT_DEBUG
1914 	log(LOG_DEBUG, "%s: enqueued mbuf=%p to %s\n",
1915 	    __func__, dup, bp->bif_ifp->if_xname);
1916 #endif
1917 
1918 	return dup;
1919 }
1920 
1921 static struct mbuf *
1922 bpf_mbuf_dequeue(struct bpf_if *bp)
1923 {
1924 	struct mbuf *m;
1925 	int s;
1926 
1927 	/* XXX NOMPSAFE: assumed running on one CPU */
1928 	s = splnet();
1929 	m = bp->bif_mbuf_head;
1930 	if (m != NULL) {
1931 		bp->bif_mbuf_head = m->m_nextpkt;
1932 		m->m_nextpkt = NULL;
1933 
1934 		if (bp->bif_mbuf_head == NULL)
1935 			bp->bif_mbuf_tail = NULL;
1936 #ifdef BPF_MTAP_SOFTINT_DEBUG
1937 		log(LOG_DEBUG, "%s: dequeued mbuf=%p from %s\n",
1938 		    __func__, m, bp->bif_ifp->if_xname);
1939 #endif
1940 	}
1941 	splx(s);
1942 
1943 	return m;
1944 }
1945 
1946 static void
1947 bpf_mtap_si(void *arg)
1948 {
1949 	struct bpf_if *bp = arg;
1950 	struct mbuf *m;
1951 
1952 	while ((m = bpf_mbuf_dequeue(bp)) != NULL) {
1953 #ifdef BPF_MTAP_SOFTINT_DEBUG
1954 		log(LOG_DEBUG, "%s: tapping mbuf=%p on %s\n",
1955 		    __func__, m, bp->bif_ifp->if_xname);
1956 #endif
1957 		bpf_ops->bpf_mtap(bp, m, BPF_D_IN);
1958 		m_freem(m);
1959 	}
1960 }
1961 
1962 static void
1963 _bpf_mtap_softint(struct ifnet *ifp, struct mbuf *m)
1964 {
1965 	struct bpf_if *bp = ifp->if_bpf;
1966 	struct mbuf *dup;
1967 
1968 	KASSERT(cpu_intr_p());
1969 
1970 	/* To avoid extra invocations of the softint */
1971 	if (BPFIF_DLIST_READER_EMPTY(bp))
1972 		return;
1973 	KASSERT(bp->bif_si != NULL);
1974 
1975 	dup = bpf_mbuf_enqueue(bp, m);
1976 	if (dup != NULL)
1977 		softint_schedule(bp->bif_si);
1978 }
1979 
1980 static int
1981 bpf_hdrlen(struct bpf_d *d)
1982 {
1983 	int hdrlen = d->bd_bif->bif_hdrlen;
1984 	/*
1985 	 * Compute the length of the bpf header.  This is not necessarily
1986 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1987 	 * that the network layer header begins on a longword boundary (for
1988 	 * performance reasons and to alleviate alignment restrictions).
1989 	 */
1990 #ifdef _LP64
1991 	if (d->bd_compat32)
1992 		return (BPF_WORDALIGN32(hdrlen + SIZEOF_BPF_HDR32) - hdrlen);
1993 	else
1994 #endif
1995 		return (BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen);
1996 }
1997 
1998 /*
1999  * Move the packet data from interface memory (pkt) into the
2000  * store buffer. Call the wakeup functions if it's time to wake up
2001  * a listener (buffer full), "cpfn" is the routine called to do the
2002  * actual data transfer. memcpy is passed in to copy contiguous chunks,
2003  * while bpf_mcpy is passed in to copy mbuf chains.  In the latter case,
2004  * pkt is really an mbuf.
2005  */
2006 static void
2007 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
2008     void *(*cpfn)(void *, const void *, size_t), struct timespec *ts)
2009 {
2010 	char *h;
2011 	int totlen, curlen, caplen;
2012 	int hdrlen = bpf_hdrlen(d);
2013 	int do_wakeup = 0;
2014 
2015 	atomic_inc_ulong(&d->bd_ccount);
2016 	BPF_STATINC(capt);
2017 	/*
2018 	 * Figure out how many bytes to move.  If the packet is
2019 	 * greater or equal to the snapshot length, transfer that
2020 	 * much.  Otherwise, transfer the whole packet (unless
2021 	 * we hit the buffer size limit).
2022 	 */
2023 	totlen = hdrlen + uimin(snaplen, pktlen);
2024 	if (totlen > d->bd_bufsize)
2025 		totlen = d->bd_bufsize;
2026 	/*
2027 	 * If we adjusted totlen to fit the bufsize, it could be that
2028 	 * totlen is smaller than hdrlen because of the link layer header.
2029 	 */
2030 	caplen = totlen - hdrlen;
2031 	if (caplen < 0)
2032 		caplen = 0;
2033 
2034 	mutex_enter(d->bd_buf_mtx);
2035 	/*
2036 	 * Round up the end of the previous packet to the next longword.
2037 	 */
2038 #ifdef _LP64
2039 	if (d->bd_compat32)
2040 		curlen = BPF_WORDALIGN32(d->bd_slen);
2041 	else
2042 #endif
2043 		curlen = BPF_WORDALIGN(d->bd_slen);
2044 	if (curlen + totlen > d->bd_bufsize) {
2045 		/*
2046 		 * This packet will overflow the storage buffer.
2047 		 * Rotate the buffers if we can, then wakeup any
2048 		 * pending reads.
2049 		 */
2050 		if (d->bd_fbuf == NULL) {
2051 			mutex_exit(d->bd_buf_mtx);
2052 			/*
2053 			 * We haven't completed the previous read yet,
2054 			 * so drop the packet.
2055 			 */
2056 			atomic_inc_ulong(&d->bd_dcount);
2057 			BPF_STATINC(drop);
2058 			return;
2059 		}
2060 		ROTATE_BUFFERS(d);
2061 		do_wakeup = 1;
2062 		curlen = 0;
2063 	} else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) {
2064 		/*
2065 		 * Immediate mode is set, or the read timeout has
2066 		 * already expired during a select call.  A packet
2067 		 * arrived, so the reader should be woken up.
2068 		 */
2069 		do_wakeup = 1;
2070 	}
2071 
2072 	/*
2073 	 * Append the bpf header.
2074 	 */
2075 	h = (char *)d->bd_sbuf + curlen;
2076 #ifdef _LP64
2077 	if (d->bd_compat32) {
2078 		struct bpf_hdr32 *hp32;
2079 
2080 		hp32 = (struct bpf_hdr32 *)h;
2081 		hp32->bh_tstamp.tv_sec = ts->tv_sec;
2082 		hp32->bh_tstamp.tv_usec = ts->tv_nsec / 1000;
2083 		hp32->bh_datalen = pktlen;
2084 		hp32->bh_hdrlen = hdrlen;
2085 		hp32->bh_caplen = caplen;
2086 	} else
2087 #endif
2088 	{
2089 		struct bpf_hdr *hp;
2090 
2091 		hp = (struct bpf_hdr *)h;
2092 		hp->bh_tstamp.tv_sec = ts->tv_sec;
2093 		hp->bh_tstamp.tv_usec = ts->tv_nsec / 1000;
2094 		hp->bh_datalen = pktlen;
2095 		hp->bh_hdrlen = hdrlen;
2096 		hp->bh_caplen = caplen;
2097 	}
2098 
2099 	/*
2100 	 * Copy the packet data into the store buffer and update its length.
2101 	 */
2102 	(*cpfn)(h + hdrlen, pkt, caplen);
2103 	d->bd_slen = curlen + totlen;
2104 
2105 	/*
2106 	 * Call bpf_wakeup after bd_slen has been updated so that kevent(2)
2107 	 * will cause filt_bpfread() to be called with it adjusted.
2108 	 */
2109 	if (do_wakeup)
2110 		bpf_wakeup(d);
2111 
2112 	mutex_exit(d->bd_buf_mtx);
2113 }
2114 
2115 /*
2116  * Initialize all nonzero fields of a descriptor.
2117  */
2118 static int
2119 bpf_allocbufs(struct bpf_d *d)
2120 {
2121 
2122 	d->bd_fbuf = kmem_zalloc(d->bd_bufsize, KM_NOSLEEP);
2123 	if (!d->bd_fbuf)
2124 		return (ENOBUFS);
2125 	d->bd_sbuf = kmem_zalloc(d->bd_bufsize, KM_NOSLEEP);
2126 	if (!d->bd_sbuf) {
2127 		kmem_free(d->bd_fbuf, d->bd_bufsize);
2128 		return (ENOBUFS);
2129 	}
2130 	d->bd_slen = 0;
2131 	d->bd_hlen = 0;
2132 	return (0);
2133 }
2134 
2135 static void
2136 bpf_free_filter(struct bpf_filter *filter)
2137 {
2138 
2139 	KASSERT(filter != NULL);
2140 
2141 	if (filter->bf_insn != NULL)
2142 		kmem_free(filter->bf_insn, filter->bf_size);
2143 	if (filter->bf_jitcode != NULL)
2144 		bpf_jit_freecode(filter->bf_jitcode);
2145 	kmem_free(filter, sizeof(*filter));
2146 }
2147 
2148 /*
2149  * Free buffers currently in use by a descriptor.
2150  * Called on close.
2151  */
2152 static void
2153 bpf_freed(struct bpf_d *d)
2154 {
2155 	/*
2156 	 * We don't need to lock out interrupts since this descriptor has
2157 	 * been detached from its interface and it yet hasn't been marked
2158 	 * free.
2159 	 */
2160 	if (d->bd_sbuf != NULL) {
2161 		kmem_free(d->bd_sbuf, d->bd_bufsize);
2162 		if (d->bd_hbuf != NULL)
2163 			kmem_free(d->bd_hbuf, d->bd_bufsize);
2164 		if (d->bd_fbuf != NULL)
2165 			kmem_free(d->bd_fbuf, d->bd_bufsize);
2166 	}
2167 	if (d->bd_rfilter != NULL) {
2168 		bpf_free_filter(d->bd_rfilter);
2169 		d->bd_rfilter = NULL;
2170 	}
2171 	if (d->bd_wfilter != NULL) {
2172 		bpf_free_filter(d->bd_wfilter);
2173 		d->bd_wfilter = NULL;
2174 	}
2175 	d->bd_jitcode = NULL;
2176 }
2177 
2178 /*
2179  * Attach an interface to bpf.  dlt is the link layer type;
2180  * hdrlen is the fixed size of the link header for the specified dlt
2181  * (variable length headers not yet supported).
2182  */
2183 static void
2184 _bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
2185 {
2186 	struct bpf_if *bp;
2187 
2188 	bp = kmem_alloc(sizeof(*bp), KM_SLEEP);
2189 
2190 	mutex_enter(&bpf_mtx);
2191 	bp->bif_driverp = driverp;
2192 	bp->bif_ifp = ifp;
2193 	bp->bif_dlt = dlt;
2194 	bp->bif_si = NULL;
2195 	BPF_IFLIST_ENTRY_INIT(bp);
2196 	PSLIST_INIT(&bp->bif_dlist_head);
2197 	psref_target_init(&bp->bif_psref, bpf_psref_class);
2198 	SLIST_INIT(&bp->bif_trackers);
2199 
2200 	BPF_IFLIST_WRITER_INSERT_HEAD(bp);
2201 
2202 	*bp->bif_driverp = NULL;
2203 
2204 	bp->bif_hdrlen = hdrlen;
2205 	mutex_exit(&bpf_mtx);
2206 #if 0
2207 	printf("bpf: %s attached with dlt %x\n", ifp->if_xname, dlt);
2208 #endif
2209 }
2210 
2211 static void
2212 _bpf_mtap_softint_init(struct ifnet *ifp)
2213 {
2214 	struct bpf_if *bp;
2215 
2216 	mutex_enter(&bpf_mtx);
2217 	BPF_IFLIST_WRITER_FOREACH(bp) {
2218 		if (bp->bif_ifp != ifp)
2219 			continue;
2220 
2221 		bp->bif_mbuf_head = NULL;
2222 		bp->bif_mbuf_tail = NULL;
2223 		bp->bif_si = softint_establish(SOFTINT_NET, bpf_mtap_si, bp);
2224 		if (bp->bif_si == NULL)
2225 			panic("%s: softint_establish() failed", __func__);
2226 		break;
2227 	}
2228 	mutex_exit(&bpf_mtx);
2229 
2230 	if (bp == NULL)
2231 		panic("%s: no bpf_if found for %s", __func__, ifp->if_xname);
2232 }
2233 
2234 /*
2235  * Remove an interface from bpf.
2236  */
2237 static void
2238 _bpfdetach(struct ifnet *ifp)
2239 {
2240 	struct bpf_if *bp;
2241 	struct bpf_d *d;
2242 	int s;
2243 
2244 	mutex_enter(&bpf_mtx);
2245 	/* Nuke the vnodes for any open instances */
2246 again_d:
2247 	BPF_DLIST_WRITER_FOREACH(d) {
2248 		mutex_enter(d->bd_mtx);
2249 		if (d->bd_bif != NULL && d->bd_bif->bif_ifp == ifp) {
2250 			/*
2251 			 * Detach the descriptor from an interface now.
2252 			 * It will be free'ed later by close routine.
2253 			 */
2254 			bpf_detachd(d);
2255 			mutex_exit(d->bd_mtx);
2256 			goto again_d;
2257 		}
2258 		mutex_exit(d->bd_mtx);
2259 	}
2260 
2261 again:
2262 	BPF_IFLIST_WRITER_FOREACH(bp) {
2263 		if (bp->bif_ifp == ifp) {
2264 			BPF_IFLIST_WRITER_REMOVE(bp);
2265 
2266 			pserialize_perform(bpf_psz);
2267 			psref_target_destroy(&bp->bif_psref, bpf_psref_class);
2268 
2269 			while (!SLIST_EMPTY(&bp->bif_trackers)) {
2270 				struct bpf_event_tracker *t =
2271 				    SLIST_FIRST(&bp->bif_trackers);
2272 				SLIST_REMOVE_HEAD(&bp->bif_trackers,
2273 				    bet_entries);
2274 				kmem_free(t, sizeof(*t));
2275 			}
2276 
2277 			BPF_IFLIST_ENTRY_DESTROY(bp);
2278 			if (bp->bif_si != NULL) {
2279 				/* XXX NOMPSAFE: assumed running on one CPU */
2280 				s = splnet();
2281 				while (bp->bif_mbuf_head != NULL) {
2282 					struct mbuf *m = bp->bif_mbuf_head;
2283 					bp->bif_mbuf_head = m->m_nextpkt;
2284 					m_freem(m);
2285 				}
2286 				splx(s);
2287 				softint_disestablish(bp->bif_si);
2288 			}
2289 			kmem_free(bp, sizeof(*bp));
2290 			goto again;
2291 		}
2292 	}
2293 	mutex_exit(&bpf_mtx);
2294 }
2295 
2296 /*
2297  * Change the data link type of a interface.
2298  */
2299 static void
2300 _bpf_change_type(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2301 {
2302 	struct bpf_if *bp;
2303 
2304 	mutex_enter(&bpf_mtx);
2305 	BPF_IFLIST_WRITER_FOREACH(bp) {
2306 		if (bp->bif_driverp == &ifp->if_bpf)
2307 			break;
2308 	}
2309 	if (bp == NULL)
2310 		panic("bpf_change_type");
2311 
2312 	bp->bif_dlt = dlt;
2313 
2314 	bp->bif_hdrlen = hdrlen;
2315 	mutex_exit(&bpf_mtx);
2316 }
2317 
2318 /*
2319  * Get a list of available data link type of the interface.
2320  */
2321 static int
2322 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
2323 {
2324 	int n, error;
2325 	struct ifnet *ifp;
2326 	struct bpf_if *bp;
2327 	int s, bound;
2328 
2329 	KASSERT(mutex_owned(d->bd_mtx));
2330 
2331 	ifp = d->bd_bif->bif_ifp;
2332 	n = 0;
2333 	error = 0;
2334 
2335 	bound = curlwp_bind();
2336 	s = pserialize_read_enter();
2337 	BPF_IFLIST_READER_FOREACH(bp) {
2338 		if (bp->bif_ifp != ifp)
2339 			continue;
2340 		if (bfl->bfl_list != NULL) {
2341 			struct psref psref;
2342 
2343 			if (n >= bfl->bfl_len) {
2344 				pserialize_read_exit(s);
2345 				return ENOMEM;
2346 			}
2347 
2348 			bpf_if_acquire(bp, &psref);
2349 			pserialize_read_exit(s);
2350 
2351 			error = copyout(&bp->bif_dlt,
2352 			    bfl->bfl_list + n, sizeof(u_int));
2353 
2354 			s = pserialize_read_enter();
2355 			bpf_if_release(bp, &psref);
2356 		}
2357 		n++;
2358 	}
2359 	pserialize_read_exit(s);
2360 	curlwp_bindx(bound);
2361 
2362 	bfl->bfl_len = n;
2363 	return error;
2364 }
2365 
2366 /*
2367  * Set the data link type of a BPF instance.
2368  */
2369 static int
2370 bpf_setdlt(struct bpf_d *d, u_int dlt)
2371 {
2372 	int error, opromisc;
2373 	struct ifnet *ifp;
2374 	struct bpf_if *bp;
2375 
2376 	KASSERT(mutex_owned(&bpf_mtx));
2377 	KASSERT(mutex_owned(d->bd_mtx));
2378 
2379 	if (d->bd_bif->bif_dlt == dlt)
2380 		return 0;
2381 	ifp = d->bd_bif->bif_ifp;
2382 	BPF_IFLIST_WRITER_FOREACH(bp) {
2383 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
2384 			break;
2385 	}
2386 	if (bp == NULL)
2387 		return EINVAL;
2388 	opromisc = d->bd_promisc;
2389 	bpf_detachd(d);
2390 	BPFIF_DLIST_ENTRY_INIT(d);
2391 	bpf_attachd(d, bp);
2392 	reset_d(d);
2393 	if (opromisc) {
2394 		KERNEL_LOCK_UNLESS_NET_MPSAFE();
2395 		error = ifpromisc(bp->bif_ifp, 1);
2396 		KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2397 		if (error)
2398 			printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
2399 			    bp->bif_ifp->if_xname, error);
2400 		else
2401 			d->bd_promisc = 1;
2402 	}
2403 	return 0;
2404 }
2405 
2406 static int
2407 sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS)
2408 {
2409 	int newsize, error;
2410 	struct sysctlnode node;
2411 
2412 	node = *rnode;
2413 	node.sysctl_data = &newsize;
2414 	newsize = bpf_maxbufsize;
2415 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2416 	if (error || newp == NULL)
2417 		return (error);
2418 
2419 	if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE)
2420 		return (EINVAL);
2421 
2422 	bpf_maxbufsize = newsize;
2423 
2424 	return (0);
2425 }
2426 
2427 #if defined(MODULAR) || defined(BPFJIT)
2428 static int
2429 sysctl_net_bpf_jit(SYSCTLFN_ARGS)
2430 {
2431 	bool newval;
2432 	int error;
2433 	struct sysctlnode node;
2434 
2435 	node = *rnode;
2436 	node.sysctl_data = &newval;
2437 	newval = bpf_jit;
2438 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2439 	if (error != 0 || newp == NULL)
2440 		return error;
2441 
2442 	bpf_jit = newval;
2443 	if (newval && bpfjit_module_ops.bj_generate_code == NULL) {
2444 		printf("JIT compilation is postponed "
2445 		    "until after bpfjit module is loaded\n");
2446 	}
2447 
2448 	return 0;
2449 }
2450 #endif
2451 
2452 static int
2453 sysctl_net_bpf_peers(SYSCTLFN_ARGS)
2454 {
2455 	int    error, elem_count;
2456 	struct bpf_d	 *dp;
2457 	struct bpf_d_ext  dpe;
2458 	size_t len, needed, elem_size, out_size;
2459 	char   *sp;
2460 
2461 	if (namelen == 1 && name[0] == CTL_QUERY)
2462 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
2463 
2464 	if (namelen != 2)
2465 		return (EINVAL);
2466 
2467 	/* BPF peers is privileged information. */
2468 	error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
2469 	    KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, NULL, NULL, NULL);
2470 	if (error)
2471 		return (EPERM);
2472 
2473 	len = (oldp != NULL) ? *oldlenp : 0;
2474 	sp = oldp;
2475 	elem_size = name[0];
2476 	elem_count = name[1];
2477 	out_size = MIN(sizeof(dpe), elem_size);
2478 	needed = 0;
2479 
2480 	if (elem_size < 1 || elem_count < 0)
2481 		return (EINVAL);
2482 
2483 	mutex_enter(&bpf_mtx);
2484 	BPF_DLIST_WRITER_FOREACH(dp) {
2485 		if (len >= elem_size && elem_count > 0) {
2486 #define BPF_EXT(field)	dpe.bde_ ## field = dp->bd_ ## field
2487 			BPF_EXT(bufsize);
2488 			BPF_EXT(promisc);
2489 			BPF_EXT(state);
2490 			BPF_EXT(immediate);
2491 			BPF_EXT(hdrcmplt);
2492 			BPF_EXT(direction);
2493 			BPF_EXT(pid);
2494 			BPF_EXT(rcount);
2495 			BPF_EXT(dcount);
2496 			BPF_EXT(ccount);
2497 #undef BPF_EXT
2498 			mutex_enter(dp->bd_mtx);
2499 			if (dp->bd_bif)
2500 				(void)strlcpy(dpe.bde_ifname,
2501 				    dp->bd_bif->bif_ifp->if_xname,
2502 				    IFNAMSIZ - 1);
2503 			else
2504 				dpe.bde_ifname[0] = '\0';
2505 			dpe.bde_locked = dp->bd_locked;
2506 			mutex_exit(dp->bd_mtx);
2507 
2508 			error = copyout(&dpe, sp, out_size);
2509 			if (error)
2510 				break;
2511 			sp += elem_size;
2512 			len -= elem_size;
2513 		}
2514 		needed += elem_size;
2515 		if (elem_count > 0 && elem_count != INT_MAX)
2516 			elem_count--;
2517 	}
2518 	mutex_exit(&bpf_mtx);
2519 
2520 	*oldlenp = needed;
2521 
2522 	return (error);
2523 }
2524 
2525 static void
2526 bpf_stats(void *p, void *arg, struct cpu_info *ci __unused)
2527 {
2528 	struct bpf_stat *const stats = p;
2529 	struct bpf_stat *sum = arg;
2530 
2531 	int s = splnet();
2532 
2533 	sum->bs_recv += stats->bs_recv;
2534 	sum->bs_drop += stats->bs_drop;
2535 	sum->bs_capt += stats->bs_capt;
2536 
2537 	splx(s);
2538 }
2539 
2540 static int
2541 bpf_sysctl_gstats_handler(SYSCTLFN_ARGS)
2542 {
2543 	struct sysctlnode node;
2544 	int error;
2545 	struct bpf_stat sum;
2546 
2547 	memset(&sum, 0, sizeof(sum));
2548 	node = *rnode;
2549 
2550 	percpu_foreach_xcall(bpf_gstats_percpu, XC_HIGHPRI_IPL(IPL_SOFTNET),
2551 	    bpf_stats, &sum);
2552 
2553 	node.sysctl_data = &sum;
2554 	node.sysctl_size = sizeof(sum);
2555 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2556 	if (error != 0 || newp == NULL)
2557 		return error;
2558 
2559 	return 0;
2560 }
2561 
2562 SYSCTL_SETUP(sysctl_net_bpf_setup, "bpf sysctls")
2563 {
2564 	const struct sysctlnode *node;
2565 
2566 	node = NULL;
2567 	sysctl_createv(clog, 0, NULL, &node,
2568 	    CTLFLAG_PERMANENT,
2569 	    CTLTYPE_NODE, "bpf",
2570 	    SYSCTL_DESCR("BPF options"),
2571 	    NULL, 0, NULL, 0,
2572 	    CTL_NET, CTL_CREATE, CTL_EOL);
2573 	if (node != NULL) {
2574 #if defined(MODULAR) || defined(BPFJIT)
2575 		sysctl_createv(clog, 0, NULL, NULL,
2576 		    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2577 		    CTLTYPE_BOOL, "jit",
2578 		    SYSCTL_DESCR("Toggle Just-In-Time compilation"),
2579 		    sysctl_net_bpf_jit, 0, &bpf_jit, 0,
2580 		    CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2581 #endif
2582 		sysctl_createv(clog, 0, NULL, NULL,
2583 		    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2584 		    CTLTYPE_INT, "maxbufsize",
2585 		    SYSCTL_DESCR("Maximum size for data capture buffer"),
2586 		    sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0,
2587 		    CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2588 		sysctl_createv(clog, 0, NULL, NULL,
2589 		    CTLFLAG_PERMANENT,
2590 		    CTLTYPE_STRUCT, "stats",
2591 		    SYSCTL_DESCR("BPF stats"),
2592 		    bpf_sysctl_gstats_handler, 0, NULL, 0,
2593 		    CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2594 		sysctl_createv(clog, 0, NULL, NULL,
2595 		    CTLFLAG_PERMANENT,
2596 		    CTLTYPE_STRUCT, "peers",
2597 		    SYSCTL_DESCR("BPF peers"),
2598 		    sysctl_net_bpf_peers, 0, NULL, 0,
2599 		    CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2600 	}
2601 
2602 }
2603 
2604 static int
2605 _bpf_register_track_event(struct bpf_if **driverp,
2606     void (*_fun)(struct bpf_if *, struct ifnet *, int, int))
2607 {
2608 	struct bpf_if *bp;
2609 	struct bpf_event_tracker *t;
2610 	int ret = ENOENT;
2611 
2612 	t = kmem_zalloc(sizeof(*t), KM_SLEEP);
2613 	if (!t)
2614 		return ENOMEM;
2615 	t->bet_notify = _fun;
2616 
2617 	mutex_enter(&bpf_mtx);
2618 	BPF_IFLIST_WRITER_FOREACH(bp) {
2619 		if (bp->bif_driverp != driverp)
2620 			continue;
2621 		SLIST_INSERT_HEAD(&bp->bif_trackers, t, bet_entries);
2622 		ret = 0;
2623 		break;
2624 	}
2625 	mutex_exit(&bpf_mtx);
2626 
2627 	return ret;
2628 }
2629 
2630 static int
2631 _bpf_deregister_track_event(struct bpf_if **driverp,
2632     void (*_fun)(struct bpf_if *, struct ifnet *, int, int))
2633 {
2634 	struct bpf_if *bp;
2635 	struct bpf_event_tracker *t = NULL;
2636 	int ret = ENOENT;
2637 
2638 	mutex_enter(&bpf_mtx);
2639 	BPF_IFLIST_WRITER_FOREACH(bp) {
2640 		if (bp->bif_driverp != driverp)
2641 			continue;
2642 		SLIST_FOREACH(t, &bp->bif_trackers, bet_entries) {
2643 			if (t->bet_notify == _fun) {
2644 				ret = 0;
2645 				break;
2646 			}
2647 		}
2648 		if (ret == 0)
2649 			break;
2650 	}
2651 	if (ret == 0 && t && t->bet_notify == _fun) {
2652 		SLIST_REMOVE(&bp->bif_trackers, t, bpf_event_tracker,
2653 		    bet_entries);
2654 	}
2655 	mutex_exit(&bpf_mtx);
2656 	if (ret == 0)
2657 		kmem_free(t, sizeof(*t));
2658 	return ret;
2659 }
2660 
2661 struct bpf_ops bpf_ops_kernel = {
2662 	.bpf_attach =		_bpfattach,
2663 	.bpf_detach =		_bpfdetach,
2664 	.bpf_change_type =	_bpf_change_type,
2665 	.bpf_register_track_event = _bpf_register_track_event,
2666 	.bpf_deregister_track_event = _bpf_deregister_track_event,
2667 
2668 	.bpf_mtap =		_bpf_mtap,
2669 	.bpf_mtap2 =		_bpf_mtap2,
2670 	.bpf_mtap_af =		_bpf_mtap_af,
2671 	.bpf_mtap_sl_in =	_bpf_mtap_sl_in,
2672 	.bpf_mtap_sl_out =	_bpf_mtap_sl_out,
2673 
2674 	.bpf_mtap_softint =		_bpf_mtap_softint,
2675 	.bpf_mtap_softint_init =	_bpf_mtap_softint_init,
2676 };
2677 
2678 MODULE(MODULE_CLASS_DRIVER, bpf, "bpf_filter");
2679 
2680 static int
2681 bpf_modcmd(modcmd_t cmd, void *arg)
2682 {
2683 #ifdef _MODULE
2684 	devmajor_t bmajor, cmajor;
2685 #endif
2686 	int error = 0;
2687 
2688 	switch (cmd) {
2689 	case MODULE_CMD_INIT:
2690 		bpf_init();
2691 #ifdef _MODULE
2692 		bmajor = cmajor = NODEVMAJOR;
2693 		error = devsw_attach("bpf", NULL, &bmajor,
2694 		    &bpf_cdevsw, &cmajor);
2695 		if (error)
2696 			break;
2697 #endif
2698 
2699 		bpf_ops_handover_enter(&bpf_ops_kernel);
2700 		atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel);
2701 		bpf_ops_handover_exit();
2702 		break;
2703 
2704 	case MODULE_CMD_FINI:
2705 		/*
2706 		 * While there is no reference counting for bpf callers,
2707 		 * unload could at least in theory be done similarly to
2708 		 * system call disestablishment.  This should even be
2709 		 * a little simpler:
2710 		 *
2711 		 * 1) replace op vector with stubs
2712 		 * 2) post update to all cpus with xc
2713 		 * 3) check that nobody is in bpf anymore
2714 		 *    (it's doubtful we'd want something like l_sysent,
2715 		 *     but we could do something like *signed* percpu
2716 		 *     counters.  if the sum is 0, we're good).
2717 		 * 4) if fail, unroll changes
2718 		 *
2719 		 * NOTE: change won't be atomic to the outside.  some
2720 		 * packets may be not captured even if unload is
2721 		 * not successful.  I think packet capture not working
2722 		 * is a perfectly logical consequence of trying to
2723 		 * disable packet capture.
2724 		 */
2725 		error = EOPNOTSUPP;
2726 		break;
2727 
2728 	default:
2729 		error = ENOTTY;
2730 		break;
2731 	}
2732 
2733 	return error;
2734 }
2735