Home | History | Annotate | Line # | Download | only in net
bpf.c revision 1.154
      1 /*	$NetBSD: bpf.c,v 1.154 2010/01/25 22:18:17 pooka 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.154 2010/01/25 22:18:17 pooka Exp $");
     43 
     44 #if defined(_KERNEL_OPT)
     45 #include "opt_bpf.h"
     46 #include "sl.h"
     47 #include "strip.h"
     48 #endif
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/buf.h>
     54 #include <sys/time.h>
     55 #include <sys/proc.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/conf.h>
     58 #include <sys/vnode.h>
     59 #include <sys/queue.h>
     60 #include <sys/stat.h>
     61 #include <sys/module.h>
     62 #include <sys/once.h>
     63 
     64 #include <sys/file.h>
     65 #include <sys/filedesc.h>
     66 #include <sys/tty.h>
     67 #include <sys/uio.h>
     68 
     69 #include <sys/protosw.h>
     70 #include <sys/socket.h>
     71 #include <sys/errno.h>
     72 #include <sys/kernel.h>
     73 #include <sys/poll.h>
     74 #include <sys/sysctl.h>
     75 #include <sys/kauth.h>
     76 
     77 #include <net/if.h>
     78 #include <net/slip.h>
     79 
     80 #include <net/bpf.h>
     81 #include <net/bpfdesc.h>
     82 
     83 #include <net/if_arc.h>
     84 #include <net/if_ether.h>
     85 
     86 #include <netinet/in.h>
     87 #include <netinet/if_inarp.h>
     88 
     89 
     90 #include <compat/sys/sockio.h>
     91 
     92 #ifndef BPF_BUFSIZE
     93 /*
     94  * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet
     95  * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k).
     96  */
     97 # define BPF_BUFSIZE 32768
     98 #endif
     99 
    100 #define PRINET  26			/* interruptible */
    101 
    102 /*
    103  * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able.
    104  * XXX the default values should be computed dynamically based
    105  * on available memory size and available mbuf clusters.
    106  */
    107 int bpf_bufsize = BPF_BUFSIZE;
    108 int bpf_maxbufsize = BPF_DFLTBUFSIZE;	/* XXX set dynamically, see above */
    109 
    110 
    111 /*
    112  * Global BPF statistics returned by net.bpf.stats sysctl.
    113  */
    114 struct bpf_stat	bpf_gstats;
    115 
    116 /*
    117  * Use a mutex to avoid a race condition between gathering the stats/peers
    118  * and opening/closing the device.
    119  */
    120 static kmutex_t bpf_mtx;
    121 
    122 /*
    123  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
    124  *  bpf_dtab holds the descriptors, indexed by minor device #
    125  */
    126 struct bpf_if	*bpf_iflist;
    127 LIST_HEAD(, bpf_d) bpf_list;
    128 
    129 static int	bpf_allocbufs(struct bpf_d *);
    130 static void	bpf_deliver(struct bpf_if *,
    131 		            void *(*cpfn)(void *, const void *, size_t),
    132 			    void *, u_int, u_int, struct ifnet *);
    133 static void	bpf_freed(struct bpf_d *);
    134 static void	bpf_ifname(struct ifnet *, struct ifreq *);
    135 static void	*bpf_mcpy(void *, const void *, size_t);
    136 static int	bpf_movein(struct uio *, int, int,
    137 			        struct mbuf **, struct sockaddr *);
    138 static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
    139 static void	bpf_detachd(struct bpf_d *);
    140 static int	bpf_setif(struct bpf_d *, struct ifreq *);
    141 static void	bpf_timed_out(void *);
    142 static inline void
    143 		bpf_wakeup(struct bpf_d *);
    144 static void	catchpacket(struct bpf_d *, u_char *, u_int, u_int,
    145     void *(*)(void *, const void *, size_t), struct timespec *);
    146 static void	reset_d(struct bpf_d *);
    147 static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
    148 static int	bpf_setdlt(struct bpf_d *, u_int);
    149 
    150 static int	bpf_read(struct file *, off_t *, struct uio *, kauth_cred_t,
    151     int);
    152 static int	bpf_write(struct file *, off_t *, struct uio *, kauth_cred_t,
    153     int);
    154 static int	bpf_ioctl(struct file *, u_long, void *);
    155 static int	bpf_poll(struct file *, int);
    156 static int	bpf_stat(struct file *, struct stat *);
    157 static int	bpf_close(struct file *);
    158 static int	bpf_kqfilter(struct file *, struct knote *);
    159 static void	bpf_softintr(void *);
    160 
    161 static const struct fileops bpf_fileops = {
    162 	.fo_read = bpf_read,
    163 	.fo_write = bpf_write,
    164 	.fo_ioctl = bpf_ioctl,
    165 	.fo_fcntl = fnullop_fcntl,
    166 	.fo_poll = bpf_poll,
    167 	.fo_stat = bpf_stat,
    168 	.fo_close = bpf_close,
    169 	.fo_kqfilter = bpf_kqfilter,
    170 	.fo_restart = fnullop_restart,
    171 };
    172 
    173 dev_type_open(bpfopen);
    174 
    175 const struct cdevsw bpf_cdevsw = {
    176 	bpfopen, noclose, noread, nowrite, noioctl,
    177 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
    178 };
    179 
    180 static int
    181 bpf_movein(struct uio *uio, int linktype, int mtu, struct mbuf **mp,
    182 	   struct sockaddr *sockp)
    183 {
    184 	struct mbuf *m;
    185 	int error;
    186 	int len;
    187 	int hlen;
    188 	int align;
    189 
    190 	/*
    191 	 * Build a sockaddr based on the data link layer type.
    192 	 * We do this at this level because the ethernet header
    193 	 * is copied directly into the data field of the sockaddr.
    194 	 * In the case of SLIP, there is no header and the packet
    195 	 * is forwarded as is.
    196 	 * Also, we are careful to leave room at the front of the mbuf
    197 	 * for the link level header.
    198 	 */
    199 	switch (linktype) {
    200 
    201 	case DLT_SLIP:
    202 		sockp->sa_family = AF_INET;
    203 		hlen = 0;
    204 		align = 0;
    205 		break;
    206 
    207 	case DLT_PPP:
    208 		sockp->sa_family = AF_UNSPEC;
    209 		hlen = 0;
    210 		align = 0;
    211 		break;
    212 
    213 	case DLT_EN10MB:
    214 		sockp->sa_family = AF_UNSPEC;
    215 		/* XXX Would MAXLINKHDR be better? */
    216  		/* 6(dst)+6(src)+2(type) */
    217 		hlen = sizeof(struct ether_header);
    218 		align = 2;
    219 		break;
    220 
    221 	case DLT_ARCNET:
    222 		sockp->sa_family = AF_UNSPEC;
    223 		hlen = ARC_HDRLEN;
    224 		align = 5;
    225 		break;
    226 
    227 	case DLT_FDDI:
    228 		sockp->sa_family = AF_LINK;
    229 		/* XXX 4(FORMAC)+6(dst)+6(src) */
    230 		hlen = 16;
    231 		align = 0;
    232 		break;
    233 
    234 	case DLT_ECONET:
    235 		sockp->sa_family = AF_UNSPEC;
    236 		hlen = 6;
    237 		align = 2;
    238 		break;
    239 
    240 	case DLT_NULL:
    241 		sockp->sa_family = AF_UNSPEC;
    242 		hlen = 0;
    243 		align = 0;
    244 		break;
    245 
    246 	default:
    247 		return (EIO);
    248 	}
    249 
    250 	len = uio->uio_resid;
    251 	/*
    252 	 * If there aren't enough bytes for a link level header or the
    253 	 * packet length exceeds the interface mtu, return an error.
    254 	 */
    255 	if (len < hlen || len - hlen > mtu)
    256 		return (EMSGSIZE);
    257 
    258 	/*
    259 	 * XXX Avoid complicated buffer chaining ---
    260 	 * bail if it won't fit in a single mbuf.
    261 	 * (Take into account possible alignment bytes)
    262 	 */
    263 	if ((unsigned)len > MCLBYTES - align)
    264 		return (EIO);
    265 
    266 	m = m_gethdr(M_WAIT, MT_DATA);
    267 	m->m_pkthdr.rcvif = 0;
    268 	m->m_pkthdr.len = len - hlen;
    269 	if (len > MHLEN - align) {
    270 		m_clget(m, M_WAIT);
    271 		if ((m->m_flags & M_EXT) == 0) {
    272 			error = ENOBUFS;
    273 			goto bad;
    274 		}
    275 	}
    276 
    277 	/* Insure the data is properly aligned */
    278 	if (align > 0) {
    279 		m->m_data += align;
    280 		m->m_len -= align;
    281 	}
    282 
    283 	error = uiomove(mtod(m, void *), len, uio);
    284 	if (error)
    285 		goto bad;
    286 	if (hlen != 0) {
    287 		memcpy(sockp->sa_data, mtod(m, void *), hlen);
    288 		m->m_data += hlen; /* XXX */
    289 		len -= hlen;
    290 	}
    291 	m->m_len = len;
    292 	*mp = m;
    293 	return (0);
    294 
    295 bad:
    296 	m_freem(m);
    297 	return (error);
    298 }
    299 
    300 /*
    301  * Attach file to the bpf interface, i.e. make d listen on bp.
    302  * Must be called at splnet.
    303  */
    304 static void
    305 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
    306 {
    307 	/*
    308 	 * Point d at bp, and add d to the interface's list of listeners.
    309 	 * Finally, point the driver's bpf cookie at the interface so
    310 	 * it will divert packets to bpf.
    311 	 */
    312 	d->bd_bif = bp;
    313 	d->bd_next = bp->bif_dlist;
    314 	bp->bif_dlist = d;
    315 
    316 	*bp->bif_driverp = bp;
    317 }
    318 
    319 /*
    320  * Detach a file from its interface.
    321  */
    322 static void
    323 bpf_detachd(struct bpf_d *d)
    324 {
    325 	struct bpf_d **p;
    326 	struct bpf_if *bp;
    327 
    328 	bp = d->bd_bif;
    329 	/*
    330 	 * Check if this descriptor had requested promiscuous mode.
    331 	 * If so, turn it off.
    332 	 */
    333 	if (d->bd_promisc) {
    334 		int error;
    335 
    336 		d->bd_promisc = 0;
    337 		/*
    338 		 * Take device out of promiscuous mode.  Since we were
    339 		 * able to enter promiscuous mode, we should be able
    340 		 * to turn it off.  But we can get an error if
    341 		 * the interface was configured down, so only panic
    342 		 * if we don't get an unexpected error.
    343 		 */
    344   		error = ifpromisc(bp->bif_ifp, 0);
    345 		if (error && error != EINVAL)
    346 			panic("%s: ifpromisc failed: %d", __func__, error);
    347 	}
    348 	/* Remove d from the interface's descriptor list. */
    349 	p = &bp->bif_dlist;
    350 	while (*p != d) {
    351 		p = &(*p)->bd_next;
    352 		if (*p == 0)
    353 			panic("%s: descriptor not in list", __func__);
    354 	}
    355 	*p = (*p)->bd_next;
    356 	if (bp->bif_dlist == 0)
    357 		/*
    358 		 * Let the driver know that there are no more listeners.
    359 		 */
    360 		*d->bd_bif->bif_driverp = 0;
    361 	d->bd_bif = 0;
    362 }
    363 
    364 static int
    365 doinit(void)
    366 {
    367 
    368 	mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE);
    369 
    370 	LIST_INIT(&bpf_list);
    371 
    372 	bpf_gstats.bs_recv = 0;
    373 	bpf_gstats.bs_drop = 0;
    374 	bpf_gstats.bs_capt = 0;
    375 
    376 	return 0;
    377 }
    378 
    379 /*
    380  * bpfilterattach() is called at boot time.
    381  */
    382 /* ARGSUSED */
    383 void
    384 bpfilterattach(int n)
    385 {
    386 	static ONCE_DECL(control);
    387 
    388 	RUN_ONCE(&control, doinit);
    389 }
    390 
    391 /*
    392  * Open ethernet device. Clones.
    393  */
    394 /* ARGSUSED */
    395 int
    396 bpfopen(dev_t dev, int flag, int mode, struct lwp *l)
    397 {
    398 	struct bpf_d *d;
    399 	struct file *fp;
    400 	int error, fd;
    401 
    402 	/* falloc() will use the descriptor for us. */
    403 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    404 		return error;
    405 
    406 	d = malloc(sizeof(*d), M_DEVBUF, M_WAITOK|M_ZERO);
    407 	d->bd_bufsize = bpf_bufsize;
    408 	d->bd_seesent = 1;
    409 	d->bd_pid = l->l_proc->p_pid;
    410 	getnanotime(&d->bd_btime);
    411 	d->bd_atime = d->bd_mtime = d->bd_btime;
    412 	callout_init(&d->bd_callout, 0);
    413 	selinit(&d->bd_sel);
    414 	d->bd_sih = softint_establish(SOFTINT_CLOCK, bpf_softintr, d);
    415 
    416 	mutex_enter(&bpf_mtx);
    417 	LIST_INSERT_HEAD(&bpf_list, d, bd_list);
    418 	mutex_exit(&bpf_mtx);
    419 
    420 	return fd_clone(fp, fd, flag, &bpf_fileops, d);
    421 }
    422 
    423 /*
    424  * Close the descriptor by detaching it from its interface,
    425  * deallocating its buffers, and marking it free.
    426  */
    427 /* ARGSUSED */
    428 static int
    429 bpf_close(struct file *fp)
    430 {
    431 	struct bpf_d *d = fp->f_data;
    432 	int s;
    433 
    434 	KERNEL_LOCK(1, NULL);
    435 
    436 	/*
    437 	 * Refresh the PID associated with this bpf file.
    438 	 */
    439 	d->bd_pid = curproc->p_pid;
    440 
    441 	s = splnet();
    442 	if (d->bd_state == BPF_WAITING)
    443 		callout_stop(&d->bd_callout);
    444 	d->bd_state = BPF_IDLE;
    445 	if (d->bd_bif)
    446 		bpf_detachd(d);
    447 	splx(s);
    448 	bpf_freed(d);
    449 	mutex_enter(&bpf_mtx);
    450 	LIST_REMOVE(d, bd_list);
    451 	mutex_exit(&bpf_mtx);
    452 	callout_destroy(&d->bd_callout);
    453 	seldestroy(&d->bd_sel);
    454 	softint_disestablish(d->bd_sih);
    455 	free(d, M_DEVBUF);
    456 	fp->f_data = NULL;
    457 
    458 	KERNEL_UNLOCK_ONE(NULL);
    459 
    460 	return (0);
    461 }
    462 
    463 /*
    464  * Rotate the packet buffers in descriptor d.  Move the store buffer
    465  * into the hold slot, and the free buffer into the store slot.
    466  * Zero the length of the new store buffer.
    467  */
    468 #define ROTATE_BUFFERS(d) \
    469 	(d)->bd_hbuf = (d)->bd_sbuf; \
    470 	(d)->bd_hlen = (d)->bd_slen; \
    471 	(d)->bd_sbuf = (d)->bd_fbuf; \
    472 	(d)->bd_slen = 0; \
    473 	(d)->bd_fbuf = 0;
    474 /*
    475  *  bpfread - read next chunk of packets from buffers
    476  */
    477 static int
    478 bpf_read(struct file *fp, off_t *offp, struct uio *uio,
    479     kauth_cred_t cred, int flags)
    480 {
    481 	struct bpf_d *d = fp->f_data;
    482 	int timed_out;
    483 	int error;
    484 	int s;
    485 
    486 	getnanotime(&d->bd_atime);
    487 	/*
    488 	 * Restrict application to use a buffer the same size as
    489 	 * the kernel buffers.
    490 	 */
    491 	if (uio->uio_resid != d->bd_bufsize)
    492 		return (EINVAL);
    493 
    494 	KERNEL_LOCK(1, NULL);
    495 	s = splnet();
    496 	if (d->bd_state == BPF_WAITING)
    497 		callout_stop(&d->bd_callout);
    498 	timed_out = (d->bd_state == BPF_TIMED_OUT);
    499 	d->bd_state = BPF_IDLE;
    500 	/*
    501 	 * If the hold buffer is empty, then do a timed sleep, which
    502 	 * ends when the timeout expires or when enough packets
    503 	 * have arrived to fill the store buffer.
    504 	 */
    505 	while (d->bd_hbuf == 0) {
    506 		if (fp->f_flag & FNONBLOCK) {
    507 			if (d->bd_slen == 0) {
    508 				splx(s);
    509 				KERNEL_UNLOCK_ONE(NULL);
    510 				return (EWOULDBLOCK);
    511 			}
    512 			ROTATE_BUFFERS(d);
    513 			break;
    514 		}
    515 
    516 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
    517 			/*
    518 			 * A packet(s) either arrived since the previous
    519 			 * read or arrived while we were asleep.
    520 			 * Rotate the buffers and return what's here.
    521 			 */
    522 			ROTATE_BUFFERS(d);
    523 			break;
    524 		}
    525 		error = tsleep(d, PRINET|PCATCH, "bpf",
    526 				d->bd_rtout);
    527 		if (error == EINTR || error == ERESTART) {
    528 			splx(s);
    529 			KERNEL_UNLOCK_ONE(NULL);
    530 			return (error);
    531 		}
    532 		if (error == EWOULDBLOCK) {
    533 			/*
    534 			 * On a timeout, return what's in the buffer,
    535 			 * which may be nothing.  If there is something
    536 			 * in the store buffer, we can rotate the buffers.
    537 			 */
    538 			if (d->bd_hbuf)
    539 				/*
    540 				 * We filled up the buffer in between
    541 				 * getting the timeout and arriving
    542 				 * here, so we don't need to rotate.
    543 				 */
    544 				break;
    545 
    546 			if (d->bd_slen == 0) {
    547 				splx(s);
    548 				KERNEL_UNLOCK_ONE(NULL);
    549 				return (0);
    550 			}
    551 			ROTATE_BUFFERS(d);
    552 			break;
    553 		}
    554 		if (error != 0)
    555 			goto done;
    556 	}
    557 	/*
    558 	 * At this point, we know we have something in the hold slot.
    559 	 */
    560 	splx(s);
    561 
    562 	/*
    563 	 * Move data from hold buffer into user space.
    564 	 * We know the entire buffer is transferred since
    565 	 * we checked above that the read buffer is bpf_bufsize bytes.
    566 	 */
    567 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
    568 
    569 	s = splnet();
    570 	d->bd_fbuf = d->bd_hbuf;
    571 	d->bd_hbuf = 0;
    572 	d->bd_hlen = 0;
    573 done:
    574 	splx(s);
    575 	KERNEL_UNLOCK_ONE(NULL);
    576 	return (error);
    577 }
    578 
    579 
    580 /*
    581  * If there are processes sleeping on this descriptor, wake them up.
    582  */
    583 static inline void
    584 bpf_wakeup(struct bpf_d *d)
    585 {
    586 	wakeup(d);
    587 	if (d->bd_async)
    588 		softint_schedule(d->bd_sih);
    589 	selnotify(&d->bd_sel, 0, 0);
    590 }
    591 
    592 static void
    593 bpf_softintr(void *cookie)
    594 {
    595 	struct bpf_d *d;
    596 
    597 	d = cookie;
    598 	if (d->bd_async)
    599 		fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL);
    600 }
    601 
    602 static void
    603 bpf_timed_out(void *arg)
    604 {
    605 	struct bpf_d *d = arg;
    606 	int s;
    607 
    608 	s = splnet();
    609 	if (d->bd_state == BPF_WAITING) {
    610 		d->bd_state = BPF_TIMED_OUT;
    611 		if (d->bd_slen != 0)
    612 			bpf_wakeup(d);
    613 	}
    614 	splx(s);
    615 }
    616 
    617 
    618 static int
    619 bpf_write(struct file *fp, off_t *offp, struct uio *uio,
    620     kauth_cred_t cred, int flags)
    621 {
    622 	struct bpf_d *d = fp->f_data;
    623 	struct ifnet *ifp;
    624 	struct mbuf *m;
    625 	int error, s;
    626 	static struct sockaddr_storage dst;
    627 
    628 	m = NULL;	/* XXX gcc */
    629 
    630 	KERNEL_LOCK(1, NULL);
    631 
    632 	if (d->bd_bif == 0) {
    633 		KERNEL_UNLOCK_ONE(NULL);
    634 		return (ENXIO);
    635 	}
    636 	getnanotime(&d->bd_mtime);
    637 
    638 	ifp = d->bd_bif->bif_ifp;
    639 
    640 	if (uio->uio_resid == 0) {
    641 		KERNEL_UNLOCK_ONE(NULL);
    642 		return (0);
    643 	}
    644 
    645 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m,
    646 		(struct sockaddr *) &dst);
    647 	if (error) {
    648 		KERNEL_UNLOCK_ONE(NULL);
    649 		return (error);
    650 	}
    651 
    652 	if (m->m_pkthdr.len > ifp->if_mtu) {
    653 		KERNEL_UNLOCK_ONE(NULL);
    654 		m_freem(m);
    655 		return (EMSGSIZE);
    656 	}
    657 
    658 	if (d->bd_hdrcmplt)
    659 		dst.ss_family = pseudo_AF_HDRCMPLT;
    660 
    661 	s = splsoftnet();
    662 	error = (*ifp->if_output)(ifp, m, (struct sockaddr *) &dst, NULL);
    663 	splx(s);
    664 	KERNEL_UNLOCK_ONE(NULL);
    665 	/*
    666 	 * The driver frees the mbuf.
    667 	 */
    668 	return (error);
    669 }
    670 
    671 /*
    672  * Reset a descriptor by flushing its packet buffer and clearing the
    673  * receive and drop counts.  Should be called at splnet.
    674  */
    675 static void
    676 reset_d(struct bpf_d *d)
    677 {
    678 	if (d->bd_hbuf) {
    679 		/* Free the hold buffer. */
    680 		d->bd_fbuf = d->bd_hbuf;
    681 		d->bd_hbuf = 0;
    682 	}
    683 	d->bd_slen = 0;
    684 	d->bd_hlen = 0;
    685 	d->bd_rcount = 0;
    686 	d->bd_dcount = 0;
    687 	d->bd_ccount = 0;
    688 }
    689 
    690 /*
    691  *  FIONREAD		Check for read packet available.
    692  *  BIOCGBLEN		Get buffer len [for read()].
    693  *  BIOCSETF		Set ethernet read filter.
    694  *  BIOCFLUSH		Flush read packet buffer.
    695  *  BIOCPROMISC		Put interface into promiscuous mode.
    696  *  BIOCGDLT		Get link layer type.
    697  *  BIOCGETIF		Get interface name.
    698  *  BIOCSETIF		Set interface.
    699  *  BIOCSRTIMEOUT	Set read timeout.
    700  *  BIOCGRTIMEOUT	Get read timeout.
    701  *  BIOCGSTATS		Get packet stats.
    702  *  BIOCIMMEDIATE	Set immediate mode.
    703  *  BIOCVERSION		Get filter language version.
    704  *  BIOCGHDRCMPLT	Get "header already complete" flag.
    705  *  BIOCSHDRCMPLT	Set "header already complete" flag.
    706  */
    707 /* ARGSUSED */
    708 static int
    709 bpf_ioctl(struct file *fp, u_long cmd, void *addr)
    710 {
    711 	struct bpf_d *d = fp->f_data;
    712 	int s, error = 0;
    713 
    714 	/*
    715 	 * Refresh the PID associated with this bpf file.
    716 	 */
    717 	KERNEL_LOCK(1, NULL);
    718 	d->bd_pid = curproc->p_pid;
    719 
    720 	s = splnet();
    721 	if (d->bd_state == BPF_WAITING)
    722 		callout_stop(&d->bd_callout);
    723 	d->bd_state = BPF_IDLE;
    724 	splx(s);
    725 
    726 	switch (cmd) {
    727 
    728 	default:
    729 		error = EINVAL;
    730 		break;
    731 
    732 	/*
    733 	 * Check for read packet available.
    734 	 */
    735 	case FIONREAD:
    736 		{
    737 			int n;
    738 
    739 			s = splnet();
    740 			n = d->bd_slen;
    741 			if (d->bd_hbuf)
    742 				n += d->bd_hlen;
    743 			splx(s);
    744 
    745 			*(int *)addr = n;
    746 			break;
    747 		}
    748 
    749 	/*
    750 	 * Get buffer len [for read()].
    751 	 */
    752 	case BIOCGBLEN:
    753 		*(u_int *)addr = d->bd_bufsize;
    754 		break;
    755 
    756 	/*
    757 	 * Set buffer length.
    758 	 */
    759 	case BIOCSBLEN:
    760 		if (d->bd_bif != 0)
    761 			error = EINVAL;
    762 		else {
    763 			u_int size = *(u_int *)addr;
    764 
    765 			if (size > bpf_maxbufsize)
    766 				*(u_int *)addr = size = bpf_maxbufsize;
    767 			else if (size < BPF_MINBUFSIZE)
    768 				*(u_int *)addr = size = BPF_MINBUFSIZE;
    769 			d->bd_bufsize = size;
    770 		}
    771 		break;
    772 
    773 	/*
    774 	 * Set link layer read filter.
    775 	 */
    776 	case BIOCSETF:
    777 		error = bpf_setf(d, addr);
    778 		break;
    779 
    780 	/*
    781 	 * Flush read packet buffer.
    782 	 */
    783 	case BIOCFLUSH:
    784 		s = splnet();
    785 		reset_d(d);
    786 		splx(s);
    787 		break;
    788 
    789 	/*
    790 	 * Put interface into promiscuous mode.
    791 	 */
    792 	case BIOCPROMISC:
    793 		if (d->bd_bif == 0) {
    794 			/*
    795 			 * No interface attached yet.
    796 			 */
    797 			error = EINVAL;
    798 			break;
    799 		}
    800 		s = splnet();
    801 		if (d->bd_promisc == 0) {
    802 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
    803 			if (error == 0)
    804 				d->bd_promisc = 1;
    805 		}
    806 		splx(s);
    807 		break;
    808 
    809 	/*
    810 	 * Get device parameters.
    811 	 */
    812 	case BIOCGDLT:
    813 		if (d->bd_bif == 0)
    814 			error = EINVAL;
    815 		else
    816 			*(u_int *)addr = d->bd_bif->bif_dlt;
    817 		break;
    818 
    819 	/*
    820 	 * Get a list of supported device parameters.
    821 	 */
    822 	case BIOCGDLTLIST:
    823 		if (d->bd_bif == 0)
    824 			error = EINVAL;
    825 		else
    826 			error = bpf_getdltlist(d, addr);
    827 		break;
    828 
    829 	/*
    830 	 * Set device parameters.
    831 	 */
    832 	case BIOCSDLT:
    833 		if (d->bd_bif == 0)
    834 			error = EINVAL;
    835 		else
    836 			error = bpf_setdlt(d, *(u_int *)addr);
    837 		break;
    838 
    839 	/*
    840 	 * Set interface name.
    841 	 */
    842 #ifdef OBIOCGETIF
    843 	case OBIOCGETIF:
    844 #endif
    845 	case BIOCGETIF:
    846 		if (d->bd_bif == 0)
    847 			error = EINVAL;
    848 		else
    849 			bpf_ifname(d->bd_bif->bif_ifp, addr);
    850 		break;
    851 
    852 	/*
    853 	 * Set interface.
    854 	 */
    855 #ifdef OBIOCSETIF
    856 	case OBIOCSETIF:
    857 #endif
    858 	case BIOCSETIF:
    859 		error = bpf_setif(d, addr);
    860 		break;
    861 
    862 	/*
    863 	 * Set read timeout.
    864 	 */
    865 	case BIOCSRTIMEOUT:
    866 		{
    867 			struct timeval *tv = addr;
    868 
    869 			/* Compute number of ticks. */
    870 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
    871 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
    872 				d->bd_rtout = 1;
    873 			break;
    874 		}
    875 
    876 #ifdef BIOCGORTIMEOUT
    877 	/*
    878 	 * Get read timeout.
    879 	 */
    880 	case BIOCGORTIMEOUT:
    881 		{
    882 			struct timeval50 *tv = addr;
    883 
    884 			tv->tv_sec = d->bd_rtout / hz;
    885 			tv->tv_usec = (d->bd_rtout % hz) * tick;
    886 			break;
    887 		}
    888 #endif
    889 
    890 #ifdef BIOCSORTIMEOUT
    891 	/*
    892 	 * Set read timeout.
    893 	 */
    894 	case BIOCSORTIMEOUT:
    895 		{
    896 			struct timeval50 *tv = addr;
    897 
    898 			/* Compute number of ticks. */
    899 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
    900 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
    901 				d->bd_rtout = 1;
    902 			break;
    903 		}
    904 #endif
    905 
    906 	/*
    907 	 * Get read timeout.
    908 	 */
    909 	case BIOCGRTIMEOUT:
    910 		{
    911 			struct timeval *tv = addr;
    912 
    913 			tv->tv_sec = d->bd_rtout / hz;
    914 			tv->tv_usec = (d->bd_rtout % hz) * tick;
    915 			break;
    916 		}
    917 	/*
    918 	 * Get packet stats.
    919 	 */
    920 	case BIOCGSTATS:
    921 		{
    922 			struct bpf_stat *bs = addr;
    923 
    924 			bs->bs_recv = d->bd_rcount;
    925 			bs->bs_drop = d->bd_dcount;
    926 			bs->bs_capt = d->bd_ccount;
    927 			break;
    928 		}
    929 
    930 	case BIOCGSTATSOLD:
    931 		{
    932 			struct bpf_stat_old *bs = addr;
    933 
    934 			bs->bs_recv = d->bd_rcount;
    935 			bs->bs_drop = d->bd_dcount;
    936 			break;
    937 		}
    938 
    939 	/*
    940 	 * Set immediate mode.
    941 	 */
    942 	case BIOCIMMEDIATE:
    943 		d->bd_immediate = *(u_int *)addr;
    944 		break;
    945 
    946 	case BIOCVERSION:
    947 		{
    948 			struct bpf_version *bv = addr;
    949 
    950 			bv->bv_major = BPF_MAJOR_VERSION;
    951 			bv->bv_minor = BPF_MINOR_VERSION;
    952 			break;
    953 		}
    954 
    955 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
    956 		*(u_int *)addr = d->bd_hdrcmplt;
    957 		break;
    958 
    959 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
    960 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
    961 		break;
    962 
    963 	/*
    964 	 * Get "see sent packets" flag
    965 	 */
    966 	case BIOCGSEESENT:
    967 		*(u_int *)addr = d->bd_seesent;
    968 		break;
    969 
    970 	/*
    971 	 * Set "see sent" packets flag
    972 	 */
    973 	case BIOCSSEESENT:
    974 		d->bd_seesent = *(u_int *)addr;
    975 		break;
    976 
    977 	case FIONBIO:		/* Non-blocking I/O */
    978 		/*
    979 		 * No need to do anything special as we use IO_NDELAY in
    980 		 * bpfread() as an indication of whether or not to block
    981 		 * the read.
    982 		 */
    983 		break;
    984 
    985 	case FIOASYNC:		/* Send signal on receive packets */
    986 		d->bd_async = *(int *)addr;
    987 		break;
    988 
    989 	case TIOCSPGRP:		/* Process or group to send signals to */
    990 	case FIOSETOWN:
    991 		error = fsetown(&d->bd_pgid, cmd, addr);
    992 		break;
    993 
    994 	case TIOCGPGRP:
    995 	case FIOGETOWN:
    996 		error = fgetown(d->bd_pgid, cmd, addr);
    997 		break;
    998 	}
    999 	KERNEL_UNLOCK_ONE(NULL);
   1000 	return (error);
   1001 }
   1002 
   1003 /*
   1004  * Set d's packet filter program to fp.  If this file already has a filter,
   1005  * free it and replace it.  Returns EINVAL for bogus requests.
   1006  */
   1007 int
   1008 bpf_setf(struct bpf_d *d, struct bpf_program *fp)
   1009 {
   1010 	struct bpf_insn *fcode, *old;
   1011 	u_int flen, size;
   1012 	int s;
   1013 
   1014 	old = d->bd_filter;
   1015 	if (fp->bf_insns == 0) {
   1016 		if (fp->bf_len != 0)
   1017 			return (EINVAL);
   1018 		s = splnet();
   1019 		d->bd_filter = 0;
   1020 		reset_d(d);
   1021 		splx(s);
   1022 		if (old != 0)
   1023 			free(old, M_DEVBUF);
   1024 		return (0);
   1025 	}
   1026 	flen = fp->bf_len;
   1027 	if (flen > BPF_MAXINSNS)
   1028 		return (EINVAL);
   1029 
   1030 	size = flen * sizeof(*fp->bf_insns);
   1031 	fcode = malloc(size, M_DEVBUF, M_WAITOK);
   1032 	if (copyin(fp->bf_insns, fcode, size) == 0 &&
   1033 	    bpf_validate(fcode, (int)flen)) {
   1034 		s = splnet();
   1035 		d->bd_filter = fcode;
   1036 		reset_d(d);
   1037 		splx(s);
   1038 		if (old != 0)
   1039 			free(old, M_DEVBUF);
   1040 
   1041 		return (0);
   1042 	}
   1043 	free(fcode, M_DEVBUF);
   1044 	return (EINVAL);
   1045 }
   1046 
   1047 /*
   1048  * Detach a file from its current interface (if attached at all) and attach
   1049  * to the interface indicated by the name stored in ifr.
   1050  * Return an errno or 0.
   1051  */
   1052 static int
   1053 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
   1054 {
   1055 	struct bpf_if *bp;
   1056 	char *cp;
   1057 	int unit_seen, i, s, error;
   1058 
   1059 	/*
   1060 	 * Make sure the provided name has a unit number, and default
   1061 	 * it to '0' if not specified.
   1062 	 * XXX This is ugly ... do this differently?
   1063 	 */
   1064 	unit_seen = 0;
   1065 	cp = ifr->ifr_name;
   1066 	cp[sizeof(ifr->ifr_name) - 1] = '\0';	/* sanity */
   1067 	while (*cp++)
   1068 		if (*cp >= '0' && *cp <= '9')
   1069 			unit_seen = 1;
   1070 	if (!unit_seen) {
   1071 		/* Make sure to leave room for the '\0'. */
   1072 		for (i = 0; i < (IFNAMSIZ - 1); ++i) {
   1073 			if ((ifr->ifr_name[i] >= 'a' &&
   1074 			     ifr->ifr_name[i] <= 'z') ||
   1075 			    (ifr->ifr_name[i] >= 'A' &&
   1076 			     ifr->ifr_name[i] <= 'Z'))
   1077 				continue;
   1078 			ifr->ifr_name[i] = '0';
   1079 		}
   1080 	}
   1081 
   1082 	/*
   1083 	 * Look through attached interfaces for the named one.
   1084 	 */
   1085 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
   1086 		struct ifnet *ifp = bp->bif_ifp;
   1087 
   1088 		if (ifp == 0 ||
   1089 		    strcmp(ifp->if_xname, ifr->ifr_name) != 0)
   1090 			continue;
   1091 		/* skip additional entry */
   1092 		if (bp->bif_driverp != &ifp->if_bpf)
   1093 			continue;
   1094 		/*
   1095 		 * We found the requested interface.
   1096 		 * Allocate the packet buffers if we need to.
   1097 		 * If we're already attached to requested interface,
   1098 		 * just flush the buffer.
   1099 		 */
   1100 		if (d->bd_sbuf == 0) {
   1101 			error = bpf_allocbufs(d);
   1102 			if (error != 0)
   1103 				return (error);
   1104 		}
   1105 		s = splnet();
   1106 		if (bp != d->bd_bif) {
   1107 			if (d->bd_bif)
   1108 				/*
   1109 				 * Detach if attached to something else.
   1110 				 */
   1111 				bpf_detachd(d);
   1112 
   1113 			bpf_attachd(d, bp);
   1114 		}
   1115 		reset_d(d);
   1116 		splx(s);
   1117 		return (0);
   1118 	}
   1119 	/* Not found. */
   1120 	return (ENXIO);
   1121 }
   1122 
   1123 /*
   1124  * Copy the interface name to the ifreq.
   1125  */
   1126 static void
   1127 bpf_ifname(struct ifnet *ifp, struct ifreq *ifr)
   1128 {
   1129 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
   1130 }
   1131 
   1132 static int
   1133 bpf_stat(struct file *fp, struct stat *st)
   1134 {
   1135 	struct bpf_d *d = fp->f_data;
   1136 
   1137 	(void)memset(st, 0, sizeof(*st));
   1138 	KERNEL_LOCK(1, NULL);
   1139 	st->st_dev = makedev(cdevsw_lookup_major(&bpf_cdevsw), d->bd_pid);
   1140 	st->st_atimespec = d->bd_atime;
   1141 	st->st_mtimespec = d->bd_mtime;
   1142 	st->st_ctimespec = st->st_birthtimespec = d->bd_btime;
   1143 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
   1144 	st->st_gid = kauth_cred_getegid(fp->f_cred);
   1145 	KERNEL_UNLOCK_ONE(NULL);
   1146 	return 0;
   1147 }
   1148 
   1149 /*
   1150  * Support for poll() system call
   1151  *
   1152  * Return true iff the specific operation will not block indefinitely - with
   1153  * the assumption that it is safe to positively acknowledge a request for the
   1154  * ability to write to the BPF device.
   1155  * Otherwise, return false but make a note that a selnotify() must be done.
   1156  */
   1157 static int
   1158 bpf_poll(struct file *fp, int events)
   1159 {
   1160 	struct bpf_d *d = fp->f_data;
   1161 	int s = splnet();
   1162 	int revents;
   1163 
   1164 	/*
   1165 	 * Refresh the PID associated with this bpf file.
   1166 	 */
   1167 	KERNEL_LOCK(1, NULL);
   1168 	d->bd_pid = curproc->p_pid;
   1169 
   1170 	revents = events & (POLLOUT | POLLWRNORM);
   1171 	if (events & (POLLIN | POLLRDNORM)) {
   1172 		/*
   1173 		 * An imitation of the FIONREAD ioctl code.
   1174 		 */
   1175 		if (d->bd_hlen != 0 ||
   1176 		    ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
   1177 		     d->bd_slen != 0)) {
   1178 			revents |= events & (POLLIN | POLLRDNORM);
   1179 		} else {
   1180 			selrecord(curlwp, &d->bd_sel);
   1181 			/* Start the read timeout if necessary */
   1182 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
   1183 				callout_reset(&d->bd_callout, d->bd_rtout,
   1184 					      bpf_timed_out, d);
   1185 				d->bd_state = BPF_WAITING;
   1186 			}
   1187 		}
   1188 	}
   1189 
   1190 	KERNEL_UNLOCK_ONE(NULL);
   1191 	splx(s);
   1192 	return (revents);
   1193 }
   1194 
   1195 static void
   1196 filt_bpfrdetach(struct knote *kn)
   1197 {
   1198 	struct bpf_d *d = kn->kn_hook;
   1199 	int s;
   1200 
   1201 	KERNEL_LOCK(1, NULL);
   1202 	s = splnet();
   1203 	SLIST_REMOVE(&d->bd_sel.sel_klist, kn, knote, kn_selnext);
   1204 	splx(s);
   1205 	KERNEL_UNLOCK_ONE(NULL);
   1206 }
   1207 
   1208 static int
   1209 filt_bpfread(struct knote *kn, long hint)
   1210 {
   1211 	struct bpf_d *d = kn->kn_hook;
   1212 	int rv;
   1213 
   1214 	KERNEL_LOCK(1, NULL);
   1215 	kn->kn_data = d->bd_hlen;
   1216 	if (d->bd_immediate)
   1217 		kn->kn_data += d->bd_slen;
   1218 	rv = (kn->kn_data > 0);
   1219 	KERNEL_UNLOCK_ONE(NULL);
   1220 	return rv;
   1221 }
   1222 
   1223 static const struct filterops bpfread_filtops =
   1224 	{ 1, NULL, filt_bpfrdetach, filt_bpfread };
   1225 
   1226 static int
   1227 bpf_kqfilter(struct file *fp, struct knote *kn)
   1228 {
   1229 	struct bpf_d *d = fp->f_data;
   1230 	struct klist *klist;
   1231 	int s;
   1232 
   1233 	KERNEL_LOCK(1, NULL);
   1234 
   1235 	switch (kn->kn_filter) {
   1236 	case EVFILT_READ:
   1237 		klist = &d->bd_sel.sel_klist;
   1238 		kn->kn_fop = &bpfread_filtops;
   1239 		break;
   1240 
   1241 	default:
   1242 		KERNEL_UNLOCK_ONE(NULL);
   1243 		return (EINVAL);
   1244 	}
   1245 
   1246 	kn->kn_hook = d;
   1247 
   1248 	s = splnet();
   1249 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1250 	splx(s);
   1251 	KERNEL_UNLOCK_ONE(NULL);
   1252 
   1253 	return (0);
   1254 }
   1255 
   1256 /*
   1257  * Incoming linkage from device drivers.  Process the packet pkt, of length
   1258  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
   1259  * by each process' filter, and if accepted, stashed into the corresponding
   1260  * buffer.
   1261  */
   1262 static void
   1263 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
   1264 {
   1265 	struct bpf_d *d;
   1266 	u_int slen;
   1267 	struct timespec ts;
   1268 	int gottime=0;
   1269 
   1270 	/*
   1271 	 * Note that the ipl does not have to be raised at this point.
   1272 	 * The only problem that could arise here is that if two different
   1273 	 * interfaces shared any data.  This is not the case.
   1274 	 */
   1275 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1276 		++d->bd_rcount;
   1277 		++bpf_gstats.bs_recv;
   1278 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
   1279 		if (slen != 0) {
   1280 			if (!gottime) {
   1281 				nanotime(&ts);
   1282 				gottime = 1;
   1283 			}
   1284 			catchpacket(d, pkt, pktlen, slen, memcpy, &ts);
   1285 		}
   1286 	}
   1287 }
   1288 
   1289 /*
   1290  * Copy data from an mbuf chain into a buffer.  This code is derived
   1291  * from m_copydata in sys/uipc_mbuf.c.
   1292  */
   1293 static void *
   1294 bpf_mcpy(void *dst_arg, const void *src_arg, size_t len)
   1295 {
   1296 	const struct mbuf *m;
   1297 	u_int count;
   1298 	u_char *dst;
   1299 
   1300 	m = src_arg;
   1301 	dst = dst_arg;
   1302 	while (len > 0) {
   1303 		if (m == NULL)
   1304 			panic("bpf_mcpy");
   1305 		count = min(m->m_len, len);
   1306 		memcpy(dst, mtod(m, const void *), count);
   1307 		m = m->m_next;
   1308 		dst += count;
   1309 		len -= count;
   1310 	}
   1311 	return dst_arg;
   1312 }
   1313 
   1314 /*
   1315  * Dispatch a packet to all the listeners on interface bp.
   1316  *
   1317  * marg    pointer to the packet, either a data buffer or an mbuf chain
   1318  * buflen  buffer length, if marg is a data buffer
   1319  * cpfn    a function that can copy marg into the listener's buffer
   1320  * pktlen  length of the packet
   1321  * rcvif   either NULL or the interface the packet came in on.
   1322  */
   1323 static inline void
   1324 bpf_deliver(struct bpf_if *bp, void *(*cpfn)(void *, const void *, size_t),
   1325 	    void *marg, u_int pktlen, u_int buflen, struct ifnet *rcvif)
   1326 {
   1327 	u_int slen;
   1328 	struct bpf_d *d;
   1329 	struct timespec ts;
   1330 	int gottime = 0;
   1331 
   1332 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1333 		if (!d->bd_seesent && (rcvif == NULL))
   1334 			continue;
   1335 		++d->bd_rcount;
   1336 		++bpf_gstats.bs_recv;
   1337 		slen = bpf_filter(d->bd_filter, marg, pktlen, buflen);
   1338 		if (slen != 0) {
   1339 			if(!gottime) {
   1340 				nanotime(&ts);
   1341 				gottime = 1;
   1342 			}
   1343 			catchpacket(d, marg, pktlen, slen, cpfn, &ts);
   1344 		}
   1345 	}
   1346 }
   1347 
   1348 /*
   1349  * Incoming linkage from device drivers, when the head of the packet is in
   1350  * a buffer, and the tail is in an mbuf chain.
   1351  */
   1352 static void
   1353 bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
   1354 {
   1355 	u_int pktlen;
   1356 	struct mbuf mb;
   1357 
   1358 	pktlen = m_length(m) + dlen;
   1359 
   1360 	/*
   1361 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
   1362 	 * Note that we cut corners here; we only setup what's
   1363 	 * absolutely needed--this mbuf should never go anywhere else.
   1364 	 */
   1365 	(void)memset(&mb, 0, sizeof(mb));
   1366 	mb.m_next = m;
   1367 	mb.m_data = data;
   1368 	mb.m_len = dlen;
   1369 
   1370 	bpf_deliver(bp, bpf_mcpy, &mb, pktlen, 0, m->m_pkthdr.rcvif);
   1371 }
   1372 
   1373 /*
   1374  * Incoming linkage from device drivers, when packet is in an mbuf chain.
   1375  */
   1376 static void
   1377 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
   1378 {
   1379 	void *(*cpfn)(void *, const void *, size_t);
   1380 	u_int pktlen, buflen;
   1381 	void *marg;
   1382 
   1383 	pktlen = m_length(m);
   1384 
   1385 	if (pktlen == m->m_len) {
   1386 		cpfn = (void *)memcpy;
   1387 		marg = mtod(m, void *);
   1388 		buflen = pktlen;
   1389 	} else {
   1390 /*###1299 [cc] warning: assignment from incompatible pointer type%%%*/
   1391 		cpfn = bpf_mcpy;
   1392 		marg = m;
   1393 		buflen = 0;
   1394 	}
   1395 
   1396 	bpf_deliver(bp, cpfn, marg, pktlen, buflen, m->m_pkthdr.rcvif);
   1397 }
   1398 
   1399 /*
   1400  * We need to prepend the address family as
   1401  * a four byte field.  Cons up a dummy header
   1402  * to pacify bpf.  This is safe because bpf
   1403  * will only read from the mbuf (i.e., it won't
   1404  * try to free it or keep a pointer a to it).
   1405  */
   1406 static void
   1407 bpf_mtap_af(struct bpf_if *bp, uint32_t af, struct mbuf *m)
   1408 {
   1409 	struct mbuf m0;
   1410 
   1411 	m0.m_flags = 0;
   1412 	m0.m_next = m;
   1413 	m0.m_len = 4;
   1414 	m0.m_data = (char *)&af;
   1415 
   1416 	bpf_mtap(bp, &m0);
   1417 }
   1418 
   1419 static void
   1420 bpf_mtap_et(struct bpf_if *bp, uint16_t et, struct mbuf *m)
   1421 {
   1422 	struct mbuf m0;
   1423 
   1424 	m0.m_flags = 0;
   1425 	m0.m_next = m;
   1426 	m0.m_len = 14;
   1427 	m0.m_data = m0.m_dat;
   1428 
   1429 	((uint32_t *)m0.m_data)[0] = 0;
   1430 	((uint32_t *)m0.m_data)[1] = 0;
   1431 	((uint32_t *)m0.m_data)[2] = 0;
   1432 	((uint16_t *)m0.m_data)[6] = et;
   1433 
   1434 	bpf_mtap(bp, &m0);
   1435 }
   1436 
   1437 /*
   1438  * Put the SLIP pseudo-"link header" in place.
   1439  * Note this M_PREPEND() should never fail,
   1440  * swince we know we always have enough space
   1441  * in the input buffer.
   1442  */
   1443 static void
   1444 bpf_mtap_sl_in(struct bpf_if *bp, u_char *chdr, struct mbuf **m)
   1445 {
   1446 	int s;
   1447 	u_char *hp;
   1448 
   1449 	M_PREPEND(*m, SLIP_HDRLEN, M_DONTWAIT);
   1450 	if (*m == NULL)
   1451 		return;
   1452 
   1453 	hp = mtod(*m, u_char *);
   1454 	hp[SLX_DIR] = SLIPDIR_IN;
   1455 	(void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
   1456 
   1457 	s = splnet();
   1458 	bpf_mtap(bp, *m);
   1459 	splx(s);
   1460 
   1461 	m_adj(*m, SLIP_HDRLEN);
   1462 }
   1463 
   1464 /*
   1465  * Put the SLIP pseudo-"link header" in
   1466  * place.  The compressed header is now
   1467  * at the beginning of the mbuf.
   1468  */
   1469 static void
   1470 bpf_mtap_sl_out(struct bpf_if *bp, u_char *chdr, struct mbuf *m)
   1471 {
   1472 	struct mbuf m0;
   1473 	u_char *hp;
   1474 	int s;
   1475 
   1476 	m0.m_flags = 0;
   1477 	m0.m_next = m;
   1478 	m0.m_data = m0.m_dat;
   1479 	m0.m_len = SLIP_HDRLEN;
   1480 
   1481 	hp = mtod(&m0, u_char *);
   1482 
   1483 	hp[SLX_DIR] = SLIPDIR_OUT;
   1484 	(void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
   1485 
   1486 	s = splnet();
   1487 	bpf_mtap(bp, &m0);
   1488 	splx(s);
   1489 	m_freem(m);
   1490 }
   1491 
   1492 /*
   1493  * Move the packet data from interface memory (pkt) into the
   1494  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
   1495  * otherwise 0.  "copy" is the routine called to do the actual data
   1496  * transfer.  memcpy is passed in to copy contiguous chunks, while
   1497  * bpf_mcpy is passed in to copy mbuf chains.  In the latter case,
   1498  * pkt is really an mbuf.
   1499  */
   1500 static void
   1501 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
   1502     void *(*cpfn)(void *, const void *, size_t), struct timespec *ts)
   1503 {
   1504 	struct bpf_hdr *hp;
   1505 	int totlen, curlen;
   1506 	int hdrlen = d->bd_bif->bif_hdrlen;
   1507 	int do_wakeup = 0;
   1508 
   1509 	++d->bd_ccount;
   1510 	++bpf_gstats.bs_capt;
   1511 	/*
   1512 	 * Figure out how many bytes to move.  If the packet is
   1513 	 * greater or equal to the snapshot length, transfer that
   1514 	 * much.  Otherwise, transfer the whole packet (unless
   1515 	 * we hit the buffer size limit).
   1516 	 */
   1517 	totlen = hdrlen + min(snaplen, pktlen);
   1518 	if (totlen > d->bd_bufsize)
   1519 		totlen = d->bd_bufsize;
   1520 
   1521 	/*
   1522 	 * Round up the end of the previous packet to the next longword.
   1523 	 */
   1524 	curlen = BPF_WORDALIGN(d->bd_slen);
   1525 	if (curlen + totlen > d->bd_bufsize) {
   1526 		/*
   1527 		 * This packet will overflow the storage buffer.
   1528 		 * Rotate the buffers if we can, then wakeup any
   1529 		 * pending reads.
   1530 		 */
   1531 		if (d->bd_fbuf == 0) {
   1532 			/*
   1533 			 * We haven't completed the previous read yet,
   1534 			 * so drop the packet.
   1535 			 */
   1536 			++d->bd_dcount;
   1537 			++bpf_gstats.bs_drop;
   1538 			return;
   1539 		}
   1540 		ROTATE_BUFFERS(d);
   1541 		do_wakeup = 1;
   1542 		curlen = 0;
   1543 	} else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) {
   1544 		/*
   1545 		 * Immediate mode is set, or the read timeout has
   1546 		 * already expired during a select call.  A packet
   1547 		 * arrived, so the reader should be woken up.
   1548 		 */
   1549 		do_wakeup = 1;
   1550 	}
   1551 
   1552 	/*
   1553 	 * Append the bpf header.
   1554 	 */
   1555 	hp = (struct bpf_hdr *)((char *)d->bd_sbuf + curlen);
   1556 	hp->bh_tstamp.tv_sec = ts->tv_sec;
   1557 	hp->bh_tstamp.tv_usec = ts->tv_nsec / 1000;
   1558 	hp->bh_datalen = pktlen;
   1559 	hp->bh_hdrlen = hdrlen;
   1560 	/*
   1561 	 * Copy the packet data into the store buffer and update its length.
   1562 	 */
   1563 	(*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
   1564 	d->bd_slen = curlen + totlen;
   1565 
   1566 	/*
   1567 	 * Call bpf_wakeup after bd_slen has been updated so that kevent(2)
   1568 	 * will cause filt_bpfread() to be called with it adjusted.
   1569 	 */
   1570 	if (do_wakeup)
   1571 		bpf_wakeup(d);
   1572 }
   1573 
   1574 /*
   1575  * Initialize all nonzero fields of a descriptor.
   1576  */
   1577 static int
   1578 bpf_allocbufs(struct bpf_d *d)
   1579 {
   1580 
   1581 	d->bd_fbuf = malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
   1582 	if (!d->bd_fbuf)
   1583 		return (ENOBUFS);
   1584 	d->bd_sbuf = malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
   1585 	if (!d->bd_sbuf) {
   1586 		free(d->bd_fbuf, M_DEVBUF);
   1587 		return (ENOBUFS);
   1588 	}
   1589 	d->bd_slen = 0;
   1590 	d->bd_hlen = 0;
   1591 	return (0);
   1592 }
   1593 
   1594 /*
   1595  * Free buffers currently in use by a descriptor.
   1596  * Called on close.
   1597  */
   1598 static void
   1599 bpf_freed(struct bpf_d *d)
   1600 {
   1601 	/*
   1602 	 * We don't need to lock out interrupts since this descriptor has
   1603 	 * been detached from its interface and it yet hasn't been marked
   1604 	 * free.
   1605 	 */
   1606 	if (d->bd_sbuf != 0) {
   1607 		free(d->bd_sbuf, M_DEVBUF);
   1608 		if (d->bd_hbuf != 0)
   1609 			free(d->bd_hbuf, M_DEVBUF);
   1610 		if (d->bd_fbuf != 0)
   1611 			free(d->bd_fbuf, M_DEVBUF);
   1612 	}
   1613 	if (d->bd_filter)
   1614 		free(d->bd_filter, M_DEVBUF);
   1615 }
   1616 
   1617 /*
   1618  * Attach an interface to bpf.  dlt is the link layer type;
   1619  * hdrlen is the fixed size of the link header for the specified dlt
   1620  * (variable length headers not yet supported).
   1621  */
   1622 static void
   1623 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
   1624 {
   1625 	struct bpf_if *bp;
   1626 	bp = malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
   1627 	if (bp == 0)
   1628 		panic("bpfattach");
   1629 
   1630 	bp->bif_dlist = 0;
   1631 	bp->bif_driverp = driverp;
   1632 	bp->bif_ifp = ifp;
   1633 	bp->bif_dlt = dlt;
   1634 
   1635 	bp->bif_next = bpf_iflist;
   1636 	bpf_iflist = bp;
   1637 
   1638 	*bp->bif_driverp = 0;
   1639 
   1640 	/*
   1641 	 * Compute the length of the bpf header.  This is not necessarily
   1642 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1643 	 * that the network layer header begins on a longword boundary (for
   1644 	 * performance reasons and to alleviate alignment restrictions).
   1645 	 */
   1646 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1647 
   1648 #if 0
   1649 	printf("bpf: %s attached\n", ifp->if_xname);
   1650 #endif
   1651 }
   1652 
   1653 /*
   1654  * Remove an interface from bpf.
   1655  */
   1656 static void
   1657 bpfdetach(struct ifnet *ifp)
   1658 {
   1659 	struct bpf_if *bp, **pbp;
   1660 	struct bpf_d *d;
   1661 	int s;
   1662 
   1663 	/* Nuke the vnodes for any open instances */
   1664 	LIST_FOREACH(d, &bpf_list, bd_list) {
   1665 		if (d->bd_bif != NULL && d->bd_bif->bif_ifp == ifp) {
   1666 			/*
   1667 			 * Detach the descriptor from an interface now.
   1668 			 * It will be free'ed later by close routine.
   1669 			 */
   1670 			s = splnet();
   1671 			d->bd_promisc = 0;	/* we can't touch device. */
   1672 			bpf_detachd(d);
   1673 			splx(s);
   1674 		}
   1675 	}
   1676 
   1677   again:
   1678 	for (bp = bpf_iflist, pbp = &bpf_iflist;
   1679 	     bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
   1680 		if (bp->bif_ifp == ifp) {
   1681 			*pbp = bp->bif_next;
   1682 			free(bp, M_DEVBUF);
   1683 			goto again;
   1684 		}
   1685 	}
   1686 }
   1687 
   1688 /*
   1689  * Change the data link type of a interface.
   1690  */
   1691 static void
   1692 bpf_change_type(struct ifnet *ifp, u_int dlt, u_int hdrlen)
   1693 {
   1694 	struct bpf_if *bp;
   1695 
   1696 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1697 		if (bp->bif_driverp == &ifp->if_bpf)
   1698 			break;
   1699 	}
   1700 	if (bp == NULL)
   1701 		panic("bpf_change_type");
   1702 
   1703 	bp->bif_dlt = dlt;
   1704 
   1705 	/*
   1706 	 * Compute the length of the bpf header.  This is not necessarily
   1707 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1708 	 * that the network layer header begins on a longword boundary (for
   1709 	 * performance reasons and to alleviate alignment restrictions).
   1710 	 */
   1711 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1712 }
   1713 
   1714 /*
   1715  * Get a list of available data link type of the interface.
   1716  */
   1717 static int
   1718 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
   1719 {
   1720 	int n, error;
   1721 	struct ifnet *ifp;
   1722 	struct bpf_if *bp;
   1723 
   1724 	ifp = d->bd_bif->bif_ifp;
   1725 	n = 0;
   1726 	error = 0;
   1727 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1728 		if (bp->bif_ifp != ifp)
   1729 			continue;
   1730 		if (bfl->bfl_list != NULL) {
   1731 			if (n >= bfl->bfl_len)
   1732 				return ENOMEM;
   1733 			error = copyout(&bp->bif_dlt,
   1734 			    bfl->bfl_list + n, sizeof(u_int));
   1735 		}
   1736 		n++;
   1737 	}
   1738 	bfl->bfl_len = n;
   1739 	return error;
   1740 }
   1741 
   1742 /*
   1743  * Set the data link type of a BPF instance.
   1744  */
   1745 static int
   1746 bpf_setdlt(struct bpf_d *d, u_int dlt)
   1747 {
   1748 	int s, error, opromisc;
   1749 	struct ifnet *ifp;
   1750 	struct bpf_if *bp;
   1751 
   1752 	if (d->bd_bif->bif_dlt == dlt)
   1753 		return 0;
   1754 	ifp = d->bd_bif->bif_ifp;
   1755 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1756 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
   1757 			break;
   1758 	}
   1759 	if (bp == NULL)
   1760 		return EINVAL;
   1761 	s = splnet();
   1762 	opromisc = d->bd_promisc;
   1763 	bpf_detachd(d);
   1764 	bpf_attachd(d, bp);
   1765 	reset_d(d);
   1766 	if (opromisc) {
   1767 		error = ifpromisc(bp->bif_ifp, 1);
   1768 		if (error)
   1769 			printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
   1770 			    bp->bif_ifp->if_xname, error);
   1771 		else
   1772 			d->bd_promisc = 1;
   1773 	}
   1774 	splx(s);
   1775 	return 0;
   1776 }
   1777 
   1778 static int
   1779 sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS)
   1780 {
   1781 	int newsize, error;
   1782 	struct sysctlnode node;
   1783 
   1784 	node = *rnode;
   1785 	node.sysctl_data = &newsize;
   1786 	newsize = bpf_maxbufsize;
   1787 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1788 	if (error || newp == NULL)
   1789 		return (error);
   1790 
   1791 	if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE)
   1792 		return (EINVAL);
   1793 
   1794 	bpf_maxbufsize = newsize;
   1795 
   1796 	return (0);
   1797 }
   1798 
   1799 static int
   1800 sysctl_net_bpf_peers(SYSCTLFN_ARGS)
   1801 {
   1802 	int    error, elem_count;
   1803 	struct bpf_d	 *dp;
   1804 	struct bpf_d_ext  dpe;
   1805 	size_t len, needed, elem_size, out_size;
   1806 	char   *sp;
   1807 
   1808 	if (namelen == 1 && name[0] == CTL_QUERY)
   1809 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1810 
   1811 	if (namelen != 2)
   1812 		return (EINVAL);
   1813 
   1814 	/* BPF peers is privileged information. */
   1815 	error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
   1816 	    KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, NULL, NULL, NULL);
   1817 	if (error)
   1818 		return (EPERM);
   1819 
   1820 	len = (oldp != NULL) ? *oldlenp : 0;
   1821 	sp = oldp;
   1822 	elem_size = name[0];
   1823 	elem_count = name[1];
   1824 	out_size = MIN(sizeof(dpe), elem_size);
   1825 	needed = 0;
   1826 
   1827 	if (elem_size < 1 || elem_count < 0)
   1828 		return (EINVAL);
   1829 
   1830 	mutex_enter(&bpf_mtx);
   1831 	LIST_FOREACH(dp, &bpf_list, bd_list) {
   1832 		if (len >= elem_size && elem_count > 0) {
   1833 #define BPF_EXT(field)	dpe.bde_ ## field = dp->bd_ ## field
   1834 			BPF_EXT(bufsize);
   1835 			BPF_EXT(promisc);
   1836 			BPF_EXT(promisc);
   1837 			BPF_EXT(state);
   1838 			BPF_EXT(immediate);
   1839 			BPF_EXT(hdrcmplt);
   1840 			BPF_EXT(seesent);
   1841 			BPF_EXT(pid);
   1842 			BPF_EXT(rcount);
   1843 			BPF_EXT(dcount);
   1844 			BPF_EXT(ccount);
   1845 #undef BPF_EXT
   1846 			if (dp->bd_bif)
   1847 				(void)strlcpy(dpe.bde_ifname,
   1848 				    dp->bd_bif->bif_ifp->if_xname,
   1849 				    IFNAMSIZ - 1);
   1850 			else
   1851 				dpe.bde_ifname[0] = '\0';
   1852 
   1853 			error = copyout(&dpe, sp, out_size);
   1854 			if (error)
   1855 				break;
   1856 			sp += elem_size;
   1857 			len -= elem_size;
   1858 		}
   1859 		needed += elem_size;
   1860 		if (elem_count > 0 && elem_count != INT_MAX)
   1861 			elem_count--;
   1862 	}
   1863 	mutex_exit(&bpf_mtx);
   1864 
   1865 	*oldlenp = needed;
   1866 
   1867 	return (error);
   1868 }
   1869 
   1870 SYSCTL_SETUP(sysctl_net_bpf_setup, "sysctl net.bpf subtree setup")
   1871 {
   1872 	const struct sysctlnode *node;
   1873 
   1874 	sysctl_createv(clog, 0, NULL, NULL,
   1875 		       CTLFLAG_PERMANENT,
   1876 		       CTLTYPE_NODE, "net", NULL,
   1877 		       NULL, 0, NULL, 0,
   1878 		       CTL_NET, CTL_EOL);
   1879 
   1880 	node = NULL;
   1881 	sysctl_createv(clog, 0, NULL, &node,
   1882 		       CTLFLAG_PERMANENT,
   1883 		       CTLTYPE_NODE, "bpf",
   1884 		       SYSCTL_DESCR("BPF options"),
   1885 		       NULL, 0, NULL, 0,
   1886 		       CTL_NET, CTL_CREATE, CTL_EOL);
   1887 	if (node != NULL) {
   1888 		sysctl_createv(clog, 0, NULL, NULL,
   1889 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1890 			CTLTYPE_INT, "maxbufsize",
   1891 			SYSCTL_DESCR("Maximum size for data capture buffer"),
   1892 			sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0,
   1893 			CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1894 		sysctl_createv(clog, 0, NULL, NULL,
   1895 			CTLFLAG_PERMANENT,
   1896 			CTLTYPE_STRUCT, "stats",
   1897 			SYSCTL_DESCR("BPF stats"),
   1898 			NULL, 0, &bpf_gstats, sizeof(bpf_gstats),
   1899 			CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1900 		sysctl_createv(clog, 0, NULL, NULL,
   1901 			CTLFLAG_PERMANENT,
   1902 			CTLTYPE_STRUCT, "peers",
   1903 			SYSCTL_DESCR("BPF peers"),
   1904 			sysctl_net_bpf_peers, 0, NULL, 0,
   1905 			CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1906 	}
   1907 
   1908 }
   1909 
   1910 struct bpf_ops bpf_ops_kernel = {
   1911 	.bpf_attach =		bpfattach,
   1912 	.bpf_detach =		bpfdetach,
   1913 	.bpf_change_type =	bpf_change_type,
   1914 
   1915 	.bpf_tap =		bpf_tap,
   1916 	.bpf_mtap =		bpf_mtap,
   1917 	.bpf_mtap2 =		bpf_mtap2,
   1918 	.bpf_mtap_af =		bpf_mtap_af,
   1919 	.bpf_mtap_et =		bpf_mtap_et,
   1920 	.bpf_mtap_sl_in =	bpf_mtap_sl_in,
   1921 	.bpf_mtap_sl_out =	bpf_mtap_sl_out,
   1922 };
   1923 
   1924 MODULE(MODULE_CLASS_DRIVER, bpf, NULL);
   1925 
   1926 static int
   1927 bpf_modcmd(modcmd_t cmd, void *arg)
   1928 {
   1929 	devmajor_t bmajor, cmajor;
   1930 	int error;
   1931 
   1932 	bmajor = cmajor = NODEVMAJOR;
   1933 
   1934 	switch (cmd) {
   1935 	case MODULE_CMD_INIT:
   1936 		bpfilterattach(0);
   1937 		error = devsw_attach("bpf", NULL, &bmajor,
   1938 		    &bpf_cdevsw, &cmajor);
   1939 		if (error == EEXIST)
   1940 			error = 0; /* maybe built-in ... improve eventually */
   1941 		if (error)
   1942 			break;
   1943 
   1944 		bpf_ops_handover_enter(&bpf_ops_kernel);
   1945 		atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel);
   1946 		bpf_ops_handover_exit();
   1947 		break;
   1948 
   1949 	case MODULE_CMD_FINI:
   1950 		/*
   1951 		 * bpf_ops is not (yet) referenced in the callers before
   1952 		 * attach.  maybe other issues too.  "safety first".
   1953 		 */
   1954 		error = EOPNOTSUPP;
   1955 		break;
   1956 
   1957 	default:
   1958 		error = ENOTTY;
   1959 		break;
   1960 	}
   1961 
   1962 	return error;
   1963 }
   1964