Home | History | Annotate | Line # | Download | only in net
bpf.c revision 1.83
      1 /*	$NetBSD: bpf.c,v 1.83 2003/08/07 16:32:47 agc 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.83 2003/08/07 16:32:47 agc Exp $");
     43 
     44 #include "bpfilter.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/buf.h>
     50 #include <sys/time.h>
     51 #include <sys/proc.h>
     52 #include <sys/user.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/conf.h>
     55 #include <sys/vnode.h>
     56 
     57 #include <sys/file.h>
     58 #include <sys/tty.h>
     59 #include <sys/uio.h>
     60 
     61 #include <sys/protosw.h>
     62 #include <sys/socket.h>
     63 #include <sys/errno.h>
     64 #include <sys/kernel.h>
     65 #include <sys/poll.h>
     66 
     67 #include <net/if.h>
     68 
     69 #include <net/bpf.h>
     70 #include <net/bpfdesc.h>
     71 
     72 #include <net/if_arc.h>
     73 #include <net/if_ether.h>
     74 
     75 #include <netinet/in.h>
     76 #include <netinet/if_inarp.h>
     77 
     78 #if defined(_KERNEL_OPT)
     79 #include "bpf.h"
     80 #endif
     81 
     82 #ifndef BPF_BUFSIZE
     83 # define BPF_BUFSIZE 8192		/* 4096 too small for FDDI frames */
     84 #endif
     85 
     86 #define PRINET  26			/* interruptible */
     87 
     88 /*
     89  * The default read buffer size is patchable.
     90  */
     91 int bpf_bufsize = BPF_BUFSIZE;
     92 
     93 /*
     94  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
     95  *  bpf_dtab holds the descriptors, indexed by minor device #
     96  */
     97 struct bpf_if	*bpf_iflist;
     98 struct bpf_d	bpf_dtab[NBPFILTER];
     99 
    100 static int	bpf_allocbufs __P((struct bpf_d *));
    101 static void	bpf_freed __P((struct bpf_d *));
    102 static void	bpf_ifname __P((struct ifnet *, struct ifreq *));
    103 static void	*bpf_mcpy __P((void *, const void *, size_t));
    104 static int	bpf_movein __P((struct uio *, int, int,
    105 			        struct mbuf **, struct sockaddr *));
    106 static void	bpf_attachd __P((struct bpf_d *, struct bpf_if *));
    107 static void	bpf_detachd __P((struct bpf_d *));
    108 static int	bpf_setif __P((struct bpf_d *, struct ifreq *));
    109 static __inline void
    110 		bpf_wakeup __P((struct bpf_d *));
    111 static void	catchpacket __P((struct bpf_d *, u_char *, u_int, u_int,
    112 				 void *(*)(void *, const void *, size_t)));
    113 static void	reset_d __P((struct bpf_d *));
    114 static int	bpf_getdltlist __P((struct bpf_d *, struct bpf_dltlist *));
    115 static int	bpf_setdlt __P((struct bpf_d *, u_int));
    116 
    117 dev_type_open(bpfopen);
    118 dev_type_close(bpfclose);
    119 dev_type_read(bpfread);
    120 dev_type_write(bpfwrite);
    121 dev_type_ioctl(bpfioctl);
    122 dev_type_poll(bpfpoll);
    123 dev_type_kqfilter(bpfkqfilter);
    124 
    125 const struct cdevsw bpf_cdevsw = {
    126 	bpfopen, bpfclose, bpfread, bpfwrite, bpfioctl,
    127 	nostop, notty, bpfpoll, nommap, bpfkqfilter,
    128 };
    129 
    130 static int
    131 bpf_movein(uio, linktype, mtu, mp, sockp)
    132 	struct uio *uio;
    133 	int linktype;
    134 	int mtu;
    135 	struct mbuf **mp;
    136 	struct sockaddr *sockp;
    137 {
    138 	struct mbuf *m;
    139 	int error;
    140 	int len;
    141 	int hlen;
    142 	int align;
    143 
    144 	/*
    145 	 * Build a sockaddr based on the data link layer type.
    146 	 * We do this at this level because the ethernet header
    147 	 * is copied directly into the data field of the sockaddr.
    148 	 * In the case of SLIP, there is no header and the packet
    149 	 * is forwarded as is.
    150 	 * Also, we are careful to leave room at the front of the mbuf
    151 	 * for the link level header.
    152 	 */
    153 	switch (linktype) {
    154 
    155 	case DLT_SLIP:
    156 		sockp->sa_family = AF_INET;
    157 		hlen = 0;
    158 		align = 0;
    159 		break;
    160 
    161 	case DLT_PPP:
    162 		sockp->sa_family = AF_UNSPEC;
    163 		hlen = 0;
    164 		align = 0;
    165 		break;
    166 
    167 	case DLT_EN10MB:
    168 		sockp->sa_family = AF_UNSPEC;
    169 		/* XXX Would MAXLINKHDR be better? */
    170  		/* 6(dst)+6(src)+2(type) */
    171 		hlen = sizeof(struct ether_header);
    172 		align = 2;
    173 		break;
    174 
    175 	case DLT_ARCNET:
    176 		sockp->sa_family = AF_UNSPEC;
    177 		hlen = ARC_HDRLEN;
    178 		align = 5;
    179 		break;
    180 
    181 	case DLT_FDDI:
    182 		sockp->sa_family = AF_LINK;
    183 		/* XXX 4(FORMAC)+6(dst)+6(src) */
    184 		hlen = 16;
    185 		align = 0;
    186 		break;
    187 
    188 	case DLT_ECONET:
    189 		sockp->sa_family = AF_UNSPEC;
    190 		hlen = 6;
    191 		align = 2;
    192 		break;
    193 
    194 	case DLT_NULL:
    195 		sockp->sa_family = AF_UNSPEC;
    196 		hlen = 0;
    197 		align = 0;
    198 		break;
    199 
    200 	default:
    201 		return (EIO);
    202 	}
    203 
    204 	len = uio->uio_resid;
    205 	/*
    206 	 * If there aren't enough bytes for a link level header or the
    207 	 * packet length exceeds the interface mtu, return an error.
    208 	 */
    209 	if (len < hlen || len - hlen > mtu)
    210 		return (EMSGSIZE);
    211 
    212 	/*
    213 	 * XXX Avoid complicated buffer chaining ---
    214 	 * bail if it won't fit in a single mbuf.
    215 	 * (Take into account possible alignment bytes)
    216 	 */
    217 	if ((unsigned)len > MCLBYTES - align)
    218 		return (EIO);
    219 
    220 	m = m_gethdr(M_WAIT, MT_DATA);
    221 	m->m_pkthdr.rcvif = 0;
    222 	m->m_pkthdr.len = len - hlen;
    223 	if (len > MHLEN - align) {
    224 		m_clget(m, M_WAIT);
    225 		if ((m->m_flags & M_EXT) == 0) {
    226 			error = ENOBUFS;
    227 			goto bad;
    228 		}
    229 	}
    230 
    231 	/* Insure the data is properly aligned */
    232 	if (align > 0) {
    233 		m->m_data += align;
    234 		m->m_len -= align;
    235 	}
    236 
    237 	error = uiomove(mtod(m, caddr_t), len, uio);
    238 	if (error)
    239 		goto bad;
    240 	if (hlen != 0) {
    241 		memcpy(sockp->sa_data, mtod(m, caddr_t), hlen);
    242 		m->m_data += hlen; /* XXX */
    243 		len -= hlen;
    244 	}
    245 	m->m_len = len;
    246 	*mp = m;
    247 	return (0);
    248 
    249 bad:
    250 	m_freem(m);
    251 	return (error);
    252 }
    253 
    254 /*
    255  * Attach file to the bpf interface, i.e. make d listen on bp.
    256  * Must be called at splnet.
    257  */
    258 static void
    259 bpf_attachd(d, bp)
    260 	struct bpf_d *d;
    261 	struct bpf_if *bp;
    262 {
    263 	/*
    264 	 * Point d at bp, and add d to the interface's list of listeners.
    265 	 * Finally, point the driver's bpf cookie at the interface so
    266 	 * it will divert packets to bpf.
    267 	 */
    268 	d->bd_bif = bp;
    269 	d->bd_next = bp->bif_dlist;
    270 	bp->bif_dlist = d;
    271 
    272 	*bp->bif_driverp = bp;
    273 }
    274 
    275 /*
    276  * Detach a file from its interface.
    277  */
    278 static void
    279 bpf_detachd(d)
    280 	struct bpf_d *d;
    281 {
    282 	struct bpf_d **p;
    283 	struct bpf_if *bp;
    284 
    285 	bp = d->bd_bif;
    286 	/*
    287 	 * Check if this descriptor had requested promiscuous mode.
    288 	 * If so, turn it off.
    289 	 */
    290 	if (d->bd_promisc) {
    291 		int error;
    292 
    293 		d->bd_promisc = 0;
    294 		/*
    295 		 * Take device out of promiscuous mode.  Since we were
    296 		 * able to enter promiscuous mode, we should be able
    297 		 * to turn it off.  But we can get an error if
    298 		 * the interface was configured down, so only panic
    299 		 * if we don't get an unexpected error.
    300 		 */
    301   		error = ifpromisc(bp->bif_ifp, 0);
    302 		if (error && error != EINVAL)
    303 			panic("bpf: ifpromisc failed");
    304 	}
    305 	/* Remove d from the interface's descriptor list. */
    306 	p = &bp->bif_dlist;
    307 	while (*p != d) {
    308 		p = &(*p)->bd_next;
    309 		if (*p == 0)
    310 			panic("bpf_detachd: descriptor not in list");
    311 	}
    312 	*p = (*p)->bd_next;
    313 	if (bp->bif_dlist == 0)
    314 		/*
    315 		 * Let the driver know that there are no more listeners.
    316 		 */
    317 		*d->bd_bif->bif_driverp = 0;
    318 	d->bd_bif = 0;
    319 }
    320 
    321 
    322 /*
    323  * Mark a descriptor free by making it point to itself.
    324  * This is probably cheaper than marking with a constant since
    325  * the address should be in a register anyway.
    326  */
    327 #define D_ISFREE(d) ((d) == (d)->bd_next)
    328 #define D_MARKFREE(d) ((d)->bd_next = (d))
    329 #define D_MARKUSED(d) ((d)->bd_next = 0)
    330 
    331 /*
    332  * bpfilterattach() is called at boot time.
    333  */
    334 /* ARGSUSED */
    335 void
    336 bpfilterattach(n)
    337 	int n;
    338 {
    339 	int i;
    340 	/*
    341 	 * Mark all the descriptors free.
    342 	 */
    343 	for (i = 0; i < NBPFILTER; ++i)
    344 		D_MARKFREE(&bpf_dtab[i]);
    345 
    346 }
    347 
    348 /*
    349  * Open ethernet device.  Returns ENXIO for illegal minor device number,
    350  * EBUSY if file is open by another process.
    351  */
    352 /* ARGSUSED */
    353 int
    354 bpfopen(dev, flag, mode, p)
    355 	dev_t dev;
    356 	int flag;
    357 	int mode;
    358 	struct proc *p;
    359 {
    360 	struct bpf_d *d;
    361 
    362 	if (minor(dev) >= NBPFILTER)
    363 		return (ENXIO);
    364 	/*
    365 	 * Each minor can be opened by only one process.  If the requested
    366 	 * minor is in use, return EBUSY.
    367 	 */
    368 	d = &bpf_dtab[minor(dev)];
    369 	if (!D_ISFREE(d))
    370 		return (EBUSY);
    371 
    372 	/* Mark "free" and do most initialization. */
    373 	memset((char *)d, 0, sizeof(*d));
    374 	d->bd_bufsize = bpf_bufsize;
    375 
    376 	return (0);
    377 }
    378 
    379 /*
    380  * Close the descriptor by detaching it from its interface,
    381  * deallocating its buffers, and marking it free.
    382  */
    383 /* ARGSUSED */
    384 int
    385 bpfclose(dev, flag, mode, p)
    386 	dev_t dev;
    387 	int flag;
    388 	int mode;
    389 	struct proc *p;
    390 {
    391 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    392 	int s;
    393 
    394 	s = splnet();
    395 	if (d->bd_bif)
    396 		bpf_detachd(d);
    397 	splx(s);
    398 	bpf_freed(d);
    399 
    400 	return (0);
    401 }
    402 
    403 /*
    404  * Rotate the packet buffers in descriptor d.  Move the store buffer
    405  * into the hold slot, and the free buffer into the store slot.
    406  * Zero the length of the new store buffer.
    407  */
    408 #define ROTATE_BUFFERS(d) \
    409 	(d)->bd_hbuf = (d)->bd_sbuf; \
    410 	(d)->bd_hlen = (d)->bd_slen; \
    411 	(d)->bd_sbuf = (d)->bd_fbuf; \
    412 	(d)->bd_slen = 0; \
    413 	(d)->bd_fbuf = 0;
    414 /*
    415  *  bpfread - read next chunk of packets from buffers
    416  */
    417 int
    418 bpfread(dev, uio, ioflag)
    419 	dev_t dev;
    420 	struct uio *uio;
    421 	int ioflag;
    422 {
    423 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    424 	int error;
    425 	int s;
    426 
    427 	/*
    428 	 * Restrict application to use a buffer the same size as
    429 	 * as kernel buffers.
    430 	 */
    431 	if (uio->uio_resid != d->bd_bufsize)
    432 		return (EINVAL);
    433 
    434 	s = splnet();
    435 	/*
    436 	 * If the hold buffer is empty, then do a timed sleep, which
    437 	 * ends when the timeout expires or when enough packets
    438 	 * have arrived to fill the store buffer.
    439 	 */
    440 	while (d->bd_hbuf == 0) {
    441 		if (d->bd_immediate) {
    442 			if (d->bd_slen == 0) {
    443 				splx(s);
    444 				return (EWOULDBLOCK);
    445 			}
    446 			/*
    447 			 * A packet(s) either arrived since the previous
    448 			 * read or arrived while we were asleep.
    449 			 * Rotate the buffers and return what's here.
    450 			 */
    451 			ROTATE_BUFFERS(d);
    452 			break;
    453 		}
    454 		if (d->bd_rtout != -1)
    455 			error = tsleep((caddr_t)d, PRINET|PCATCH, "bpf",
    456 					  d->bd_rtout);
    457 		else {
    458 			if (d->bd_rtout == -1) {
    459 				/* User requested non-blocking I/O */
    460 				error = EWOULDBLOCK;
    461 			} else
    462 				error = 0;
    463 		}
    464 		if (error == EINTR || error == ERESTART) {
    465 			splx(s);
    466 			return (error);
    467 		}
    468 		if (error == EWOULDBLOCK) {
    469 			/*
    470 			 * On a timeout, return what's in the buffer,
    471 			 * which may be nothing.  If there is something
    472 			 * in the store buffer, we can rotate the buffers.
    473 			 */
    474 			if (d->bd_hbuf)
    475 				/*
    476 				 * We filled up the buffer in between
    477 				 * getting the timeout and arriving
    478 				 * here, so we don't need to rotate.
    479 				 */
    480 				break;
    481 
    482 			if (d->bd_slen == 0) {
    483 				splx(s);
    484 				return (0);
    485 			}
    486 			ROTATE_BUFFERS(d);
    487 			break;
    488 		}
    489 		if (error != 0)
    490 			goto done;
    491 	}
    492 	/*
    493 	 * At this point, we know we have something in the hold slot.
    494 	 */
    495 	splx(s);
    496 
    497 	/*
    498 	 * Move data from hold buffer into user space.
    499 	 * We know the entire buffer is transferred since
    500 	 * we checked above that the read buffer is bpf_bufsize bytes.
    501 	 */
    502 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
    503 
    504 	s = splnet();
    505 	d->bd_fbuf = d->bd_hbuf;
    506 	d->bd_hbuf = 0;
    507 	d->bd_hlen = 0;
    508 done:
    509 	splx(s);
    510 	return (error);
    511 }
    512 
    513 
    514 /*
    515  * If there are processes sleeping on this descriptor, wake them up.
    516  */
    517 static __inline void
    518 bpf_wakeup(d)
    519 	struct bpf_d *d;
    520 {
    521 	struct proc *p;
    522 
    523 	wakeup((caddr_t)d);
    524 	if (d->bd_async) {
    525 		if (d->bd_pgid > 0)
    526 			gsignal (d->bd_pgid, SIGIO);
    527 		else if (d->bd_pgid && (p = pfind (-d->bd_pgid)) != NULL)
    528 			psignal (p, SIGIO);
    529 	}
    530 
    531 	selnotify(&d->bd_sel, 0);
    532 	/* XXX */
    533 	d->bd_sel.sel_pid = 0;
    534 }
    535 
    536 int
    537 bpfwrite(dev, uio, ioflag)
    538 	dev_t dev;
    539 	struct uio *uio;
    540 	int ioflag;
    541 {
    542 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    543 	struct ifnet *ifp;
    544 	struct mbuf *m;
    545 	int error, s;
    546 	static struct sockaddr_storage dst;
    547 
    548 	if (d->bd_bif == 0)
    549 		return (ENXIO);
    550 
    551 	ifp = d->bd_bif->bif_ifp;
    552 
    553 	if (uio->uio_resid == 0)
    554 		return (0);
    555 
    556 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m,
    557 		(struct sockaddr *) &dst);
    558 	if (error)
    559 		return (error);
    560 
    561 	if (m->m_pkthdr.len > ifp->if_mtu)
    562 		return (EMSGSIZE);
    563 
    564 	if (d->bd_hdrcmplt)
    565 		dst.ss_family = pseudo_AF_HDRCMPLT;
    566 
    567 	s = splsoftnet();
    568 	error = (*ifp->if_output)(ifp, m, (struct sockaddr *) &dst, NULL);
    569 	splx(s);
    570 	/*
    571 	 * The driver frees the mbuf.
    572 	 */
    573 	return (error);
    574 }
    575 
    576 /*
    577  * Reset a descriptor by flushing its packet buffer and clearing the
    578  * receive and drop counts.  Should be called at splnet.
    579  */
    580 static void
    581 reset_d(d)
    582 	struct bpf_d *d;
    583 {
    584 	if (d->bd_hbuf) {
    585 		/* Free the hold buffer. */
    586 		d->bd_fbuf = d->bd_hbuf;
    587 		d->bd_hbuf = 0;
    588 	}
    589 	d->bd_slen = 0;
    590 	d->bd_hlen = 0;
    591 	d->bd_rcount = 0;
    592 	d->bd_dcount = 0;
    593 }
    594 
    595 #ifdef BPF_KERN_FILTER
    596 extern struct bpf_insn *bpf_tcp_filter;
    597 extern struct bpf_insn *bpf_udp_filter;
    598 #endif
    599 
    600 /*
    601  *  FIONREAD		Check for read packet available.
    602  *  BIOCGBLEN		Get buffer len [for read()].
    603  *  BIOCSETF		Set ethernet read filter.
    604  *  BIOCFLUSH		Flush read packet buffer.
    605  *  BIOCPROMISC		Put interface into promiscuous mode.
    606  *  BIOCGDLT		Get link layer type.
    607  *  BIOCGETIF		Get interface name.
    608  *  BIOCSETIF		Set interface.
    609  *  BIOCSRTIMEOUT	Set read timeout.
    610  *  BIOCGRTIMEOUT	Get read timeout.
    611  *  BIOCGSTATS		Get packet stats.
    612  *  BIOCIMMEDIATE	Set immediate mode.
    613  *  BIOCVERSION		Get filter language version.
    614  *  BIOGHDRCMPLT	Get "header already complete" flag.
    615  *  BIOSHDRCMPLT	Set "header already complete" flag.
    616  */
    617 /* ARGSUSED */
    618 int
    619 bpfioctl(dev, cmd, addr, flag, p)
    620 	dev_t dev;
    621 	u_long cmd;
    622 	caddr_t addr;
    623 	int flag;
    624 	struct proc *p;
    625 {
    626 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    627 	int s, error = 0;
    628 	pid_t pgid;
    629 #ifdef BPF_KERN_FILTER
    630 	struct bpf_insn **p;
    631 #endif
    632 
    633 	switch (cmd) {
    634 
    635 	default:
    636 		error = EINVAL;
    637 		break;
    638 
    639 	/*
    640 	 * Check for read packet available.
    641 	 */
    642 	case FIONREAD:
    643 		{
    644 			int n;
    645 
    646 			s = splnet();
    647 			n = d->bd_slen;
    648 			if (d->bd_hbuf)
    649 				n += d->bd_hlen;
    650 			splx(s);
    651 
    652 			*(int *)addr = n;
    653 			break;
    654 		}
    655 
    656 	/*
    657 	 * Get buffer len [for read()].
    658 	 */
    659 	case BIOCGBLEN:
    660 		*(u_int *)addr = d->bd_bufsize;
    661 		break;
    662 
    663 	/*
    664 	 * Set buffer length.
    665 	 */
    666 	case BIOCSBLEN:
    667 		if (d->bd_bif != 0)
    668 			error = EINVAL;
    669 		else {
    670 			u_int size = *(u_int *)addr;
    671 
    672 			if (size > BPF_MAXBUFSIZE)
    673 				*(u_int *)addr = size = BPF_MAXBUFSIZE;
    674 			else if (size < BPF_MINBUFSIZE)
    675 				*(u_int *)addr = size = BPF_MINBUFSIZE;
    676 			d->bd_bufsize = size;
    677 		}
    678 		break;
    679 
    680 	/*
    681 	 * Set link layer read filter.
    682 	 */
    683 	case BIOCSETF:
    684 		error = bpf_setf(d, (struct bpf_program *)addr);
    685 		break;
    686 
    687 #ifdef BPF_KERN_FILTER
    688 	/*
    689 	 * Set TCP or UDP reject filter.
    690 	 */
    691 	case BIOCSTCPF:
    692 	case BIOCSUDPF:
    693 		if (!suser()) {
    694 			error = EPERM;
    695 			break;
    696 		}
    697 
    698 		/* Validate and store filter */
    699 		error = bpf_setf(d, (struct bpf_program *)addr);
    700 
    701 		/* Free possible old filter */
    702 		if (cmd == BIOCSTCPF)
    703 			p = &bpf_tcp_filter;
    704 		else
    705 			p = &bpf_udp_filter;
    706 		if (*p != NULL)
    707 			free((caddr_t)*p, M_DEVBUF);
    708 
    709 		/* Steal new filter (noop if error) */
    710 		s = splnet();
    711 		*p = d->bd_filter;
    712 		d->bd_filter = NULL;
    713 		splx(s);
    714 		break;
    715 #endif
    716 
    717 	/*
    718 	 * Flush read packet buffer.
    719 	 */
    720 	case BIOCFLUSH:
    721 		s = splnet();
    722 		reset_d(d);
    723 		splx(s);
    724 		break;
    725 
    726 	/*
    727 	 * Put interface into promiscuous mode.
    728 	 */
    729 	case BIOCPROMISC:
    730 		if (d->bd_bif == 0) {
    731 			/*
    732 			 * No interface attached yet.
    733 			 */
    734 			error = EINVAL;
    735 			break;
    736 		}
    737 		s = splnet();
    738 		if (d->bd_promisc == 0) {
    739 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
    740 			if (error == 0)
    741 				d->bd_promisc = 1;
    742 		}
    743 		splx(s);
    744 		break;
    745 
    746 	/*
    747 	 * Get device parameters.
    748 	 */
    749 	case BIOCGDLT:
    750 		if (d->bd_bif == 0)
    751 			error = EINVAL;
    752 		else
    753 			*(u_int *)addr = d->bd_bif->bif_dlt;
    754 		break;
    755 
    756 	/*
    757 	 * Get a list of supported device parameters.
    758 	 */
    759 	case BIOCGDLTLIST:
    760 		if (d->bd_bif == 0)
    761 			error = EINVAL;
    762 		else
    763 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
    764 		break;
    765 
    766 	/*
    767 	 * Set device parameters.
    768 	 */
    769 	case BIOCSDLT:
    770 		if (d->bd_bif == 0)
    771 			error = EINVAL;
    772 		else
    773 			error = bpf_setdlt(d, *(u_int *)addr);
    774 		break;
    775 
    776 	/*
    777 	 * Set interface name.
    778 	 */
    779 	case BIOCGETIF:
    780 		if (d->bd_bif == 0)
    781 			error = EINVAL;
    782 		else
    783 			bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
    784 		break;
    785 
    786 	/*
    787 	 * Set interface.
    788 	 */
    789 	case BIOCSETIF:
    790 		error = bpf_setif(d, (struct ifreq *)addr);
    791 		break;
    792 
    793 	/*
    794 	 * Set read timeout.
    795 	 */
    796 	case BIOCSRTIMEOUT:
    797 		{
    798 			struct timeval *tv = (struct timeval *)addr;
    799 
    800 			/* Compute number of ticks. */
    801 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
    802 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
    803 				d->bd_rtout = 1;
    804 			break;
    805 		}
    806 
    807 	/*
    808 	 * Get read timeout.
    809 	 */
    810 	case BIOCGRTIMEOUT:
    811 		{
    812 			struct timeval *tv = (struct timeval *)addr;
    813 
    814 			tv->tv_sec = d->bd_rtout / hz;
    815 			tv->tv_usec = (d->bd_rtout % hz) * tick;
    816 			break;
    817 		}
    818 
    819 	/*
    820 	 * Get packet stats.
    821 	 */
    822 	case BIOCGSTATS:
    823 		{
    824 			struct bpf_stat *bs = (struct bpf_stat *)addr;
    825 
    826 			bs->bs_recv = d->bd_rcount;
    827 			bs->bs_drop = d->bd_dcount;
    828 			break;
    829 		}
    830 
    831 	/*
    832 	 * Set immediate mode.
    833 	 */
    834 	case BIOCIMMEDIATE:
    835 		d->bd_immediate = *(u_int *)addr;
    836 		break;
    837 
    838 	case BIOCVERSION:
    839 		{
    840 			struct bpf_version *bv = (struct bpf_version *)addr;
    841 
    842 			bv->bv_major = BPF_MAJOR_VERSION;
    843 			bv->bv_minor = BPF_MINOR_VERSION;
    844 			break;
    845 		}
    846 
    847 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
    848 		*(u_int *)addr = d->bd_hdrcmplt;
    849 		break;
    850 
    851 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
    852 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
    853 		break;
    854 
    855 	case FIONBIO:		/* Non-blocking I/O */
    856 		if (*(int *)addr)
    857 			d->bd_rtout = -1;
    858 		else
    859 			d->bd_rtout = 0;
    860 		break;
    861 
    862 	case FIOASYNC:		/* Send signal on receive packets */
    863 		d->bd_async = *(int *)addr;
    864 		break;
    865 
    866 	/*
    867 	 * N.B.  ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing
    868 	 * the equivalent of a TIOCSPGRP and hence end up here.  *However*
    869 	 * TIOCSPGRP's arg is a process group if it's positive and a process
    870 	 * id if it's negative.  This is exactly the opposite of what the
    871 	 * other two functions want!
    872 	 * There is code in ioctl and fcntl to make the SETOWN calls do
    873 	 * a TIOCSPGRP with the pgid of the process if a pid is given.
    874 	 */
    875 	case TIOCSPGRP:		/* Process or group to send signals to */
    876 		pgid = *(int *)addr;
    877 		if (pgid != 0)
    878 			error = pgid_in_session(p, pgid);
    879 		if (error == 0)
    880 			d->bd_pgid = pgid;
    881 		break;
    882 
    883 	case TIOCGPGRP:
    884 		*(int *)addr = d->bd_pgid;
    885 		break;
    886 	}
    887 	return (error);
    888 }
    889 
    890 /*
    891  * Set d's packet filter program to fp.  If this file already has a filter,
    892  * free it and replace it.  Returns EINVAL for bogus requests.
    893  */
    894 int
    895 bpf_setf(d, fp)
    896 	struct bpf_d *d;
    897 	struct bpf_program *fp;
    898 {
    899 	struct bpf_insn *fcode, *old;
    900 	u_int flen, size;
    901 	int s;
    902 
    903 	old = d->bd_filter;
    904 	if (fp->bf_insns == 0) {
    905 		if (fp->bf_len != 0)
    906 			return (EINVAL);
    907 		s = splnet();
    908 		d->bd_filter = 0;
    909 		reset_d(d);
    910 		splx(s);
    911 		if (old != 0)
    912 			free((caddr_t)old, M_DEVBUF);
    913 		return (0);
    914 	}
    915 	flen = fp->bf_len;
    916 	if (flen > BPF_MAXINSNS)
    917 		return (EINVAL);
    918 
    919 	size = flen * sizeof(*fp->bf_insns);
    920 	fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
    921 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
    922 	    bpf_validate(fcode, (int)flen)) {
    923 		s = splnet();
    924 		d->bd_filter = fcode;
    925 		reset_d(d);
    926 		splx(s);
    927 		if (old != 0)
    928 			free((caddr_t)old, M_DEVBUF);
    929 
    930 		return (0);
    931 	}
    932 	free((caddr_t)fcode, M_DEVBUF);
    933 	return (EINVAL);
    934 }
    935 
    936 /*
    937  * Detach a file from its current interface (if attached at all) and attach
    938  * to the interface indicated by the name stored in ifr.
    939  * Return an errno or 0.
    940  */
    941 static int
    942 bpf_setif(d, ifr)
    943 	struct bpf_d *d;
    944 	struct ifreq *ifr;
    945 {
    946 	struct bpf_if *bp;
    947 	char *cp;
    948 	int unit_seen, i, s, error;
    949 
    950 	/*
    951 	 * Make sure the provided name has a unit number, and default
    952 	 * it to '0' if not specified.
    953 	 * XXX This is ugly ... do this differently?
    954 	 */
    955 	unit_seen = 0;
    956 	cp = ifr->ifr_name;
    957 	cp[sizeof(ifr->ifr_name) - 1] = '\0';	/* sanity */
    958 	while (*cp++)
    959 		if (*cp >= '0' && *cp <= '9')
    960 			unit_seen = 1;
    961 	if (!unit_seen) {
    962 		/* Make sure to leave room for the '\0'. */
    963 		for (i = 0; i < (IFNAMSIZ - 1); ++i) {
    964 			if ((ifr->ifr_name[i] >= 'a' &&
    965 			     ifr->ifr_name[i] <= 'z') ||
    966 			    (ifr->ifr_name[i] >= 'A' &&
    967 			     ifr->ifr_name[i] <= 'Z'))
    968 				continue;
    969 			ifr->ifr_name[i] = '0';
    970 		}
    971 	}
    972 
    973 	/*
    974 	 * Look through attached interfaces for the named one.
    975 	 */
    976 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
    977 		struct ifnet *ifp = bp->bif_ifp;
    978 
    979 		if (ifp == 0 ||
    980 		    strcmp(ifp->if_xname, ifr->ifr_name) != 0)
    981 			continue;
    982 		/* skip additional entry */
    983 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
    984 			continue;
    985 		/*
    986 		 * We found the requested interface.
    987 		 * If it's not up, return an error.
    988 		 * Allocate the packet buffers if we need to.
    989 		 * If we're already attached to requested interface,
    990 		 * just flush the buffer.
    991 		 */
    992 		if ((ifp->if_flags & IFF_UP) == 0)
    993 			return (ENETDOWN);
    994 
    995 		if (d->bd_sbuf == 0) {
    996 			error = bpf_allocbufs(d);
    997 			if (error != 0)
    998 				return (error);
    999 		}
   1000 		s = splnet();
   1001 		if (bp != d->bd_bif) {
   1002 			if (d->bd_bif)
   1003 				/*
   1004 				 * Detach if attached to something else.
   1005 				 */
   1006 				bpf_detachd(d);
   1007 
   1008 			bpf_attachd(d, bp);
   1009 		}
   1010 		reset_d(d);
   1011 		splx(s);
   1012 		return (0);
   1013 	}
   1014 	/* Not found. */
   1015 	return (ENXIO);
   1016 }
   1017 
   1018 /*
   1019  * Copy the interface name to the ifreq.
   1020  */
   1021 static void
   1022 bpf_ifname(ifp, ifr)
   1023 	struct ifnet *ifp;
   1024 	struct ifreq *ifr;
   1025 {
   1026 
   1027 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
   1028 }
   1029 
   1030 /*
   1031  * Support for poll() system call
   1032  *
   1033  * Return true iff the specific operation will not block indefinitely - with
   1034  * the assumption that it is safe to positively acknowledge a request for the
   1035  * ability to write to the BPF device.
   1036  * Otherwise, return false but make a note that a selwakeup() must be done.
   1037  */
   1038 int
   1039 bpfpoll(dev, events, p)
   1040 	dev_t dev;
   1041 	int events;
   1042 	struct proc *p;
   1043 {
   1044 	struct bpf_d *d = &bpf_dtab[minor(dev)];
   1045 	int s = splnet();
   1046 	int revents;
   1047 
   1048 	revents = events & (POLLOUT | POLLWRNORM);
   1049 	if (events & (POLLIN | POLLRDNORM)) {
   1050 		/*
   1051 		 * An imitation of the FIONREAD ioctl code.
   1052 		 */
   1053 		if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
   1054 			revents |= events & (POLLIN | POLLRDNORM);
   1055 		else
   1056 			selrecord(p, &d->bd_sel);
   1057 	}
   1058 
   1059 	splx(s);
   1060 	return (revents);
   1061 }
   1062 
   1063 static void
   1064 filt_bpfrdetach(struct knote *kn)
   1065 {
   1066 	struct bpf_d *d = kn->kn_hook;
   1067 	int s;
   1068 
   1069 	s = splnet();
   1070 	SLIST_REMOVE(&d->bd_sel.sel_klist, kn, knote, kn_selnext);
   1071 	splx(s);
   1072 }
   1073 
   1074 static int
   1075 filt_bpfread(struct knote *kn, long hint)
   1076 {
   1077 	struct bpf_d *d = kn->kn_hook;
   1078 
   1079 	kn->kn_data = d->bd_hlen;
   1080 	if (d->bd_immediate)
   1081 		kn->kn_data += d->bd_slen;
   1082 	return (kn->kn_data > 0);
   1083 }
   1084 
   1085 static const struct filterops bpfread_filtops =
   1086 	{ 1, NULL, filt_bpfrdetach, filt_bpfread };
   1087 
   1088 int
   1089 bpfkqfilter(dev, kn)
   1090 	dev_t dev;
   1091 	struct knote *kn;
   1092 {
   1093 	struct bpf_d *d = &bpf_dtab[minor(dev)];
   1094 	struct klist *klist;
   1095 	int s;
   1096 
   1097 	switch (kn->kn_filter) {
   1098 	case EVFILT_READ:
   1099 		klist = &d->bd_sel.sel_klist;
   1100 		kn->kn_fop = &bpfread_filtops;
   1101 		break;
   1102 
   1103 	default:
   1104 		return (1);
   1105 	}
   1106 
   1107 	kn->kn_hook = d;
   1108 
   1109 	s = splnet();
   1110 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1111 	splx(s);
   1112 
   1113 	return (0);
   1114 }
   1115 
   1116 /*
   1117  * Incoming linkage from device drivers.  Process the packet pkt, of length
   1118  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
   1119  * by each process' filter, and if accepted, stashed into the corresponding
   1120  * buffer.
   1121  */
   1122 void
   1123 bpf_tap(arg, pkt, pktlen)
   1124 	caddr_t arg;
   1125 	u_char *pkt;
   1126 	u_int pktlen;
   1127 {
   1128 	struct bpf_if *bp;
   1129 	struct bpf_d *d;
   1130 	u_int slen;
   1131 	/*
   1132 	 * Note that the ipl does not have to be raised at this point.
   1133 	 * The only problem that could arise here is that if two different
   1134 	 * interfaces shared any data.  This is not the case.
   1135 	 */
   1136 	bp = (struct bpf_if *)arg;
   1137 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1138 		++d->bd_rcount;
   1139 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
   1140 		if (slen != 0)
   1141 			catchpacket(d, pkt, pktlen, slen, memcpy);
   1142 	}
   1143 }
   1144 
   1145 /*
   1146  * Copy data from an mbuf chain into a buffer.  This code is derived
   1147  * from m_copydata in sys/uipc_mbuf.c.
   1148  */
   1149 static void *
   1150 bpf_mcpy(dst_arg, src_arg, len)
   1151 	void *dst_arg;
   1152 	const void *src_arg;
   1153 	size_t len;
   1154 {
   1155 	const struct mbuf *m;
   1156 	u_int count;
   1157 	u_char *dst;
   1158 
   1159 	m = src_arg;
   1160 	dst = dst_arg;
   1161 	while (len > 0) {
   1162 		if (m == 0)
   1163 			panic("bpf_mcpy");
   1164 		count = min(m->m_len, len);
   1165 		memcpy((caddr_t)dst, mtod(m, caddr_t), count);
   1166 		m = m->m_next;
   1167 		dst += count;
   1168 		len -= count;
   1169 	}
   1170 	return (dst_arg);
   1171 }
   1172 
   1173 /*
   1174  * Incoming linkage from device drivers, when packet is in an mbuf chain.
   1175  */
   1176 void
   1177 bpf_mtap(arg, m)
   1178 	caddr_t arg;
   1179 	struct mbuf *m;
   1180 {
   1181 	struct bpf_if *bp = (struct bpf_if *)arg;
   1182 	struct bpf_d *d;
   1183 	u_int pktlen, slen;
   1184 	struct mbuf *m0;
   1185 
   1186 	pktlen = 0;
   1187 	for (m0 = m; m0 != 0; m0 = m0->m_next)
   1188 		pktlen += m0->m_len;
   1189 
   1190 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1191 		++d->bd_rcount;
   1192 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
   1193 		if (slen != 0)
   1194 			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcpy);
   1195 	}
   1196 }
   1197 
   1198 /*
   1199  * Move the packet data from interface memory (pkt) into the
   1200  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
   1201  * otherwise 0.  "copy" is the routine called to do the actual data
   1202  * transfer.  memcpy is passed in to copy contiguous chunks, while
   1203  * bpf_mcpy is passed in to copy mbuf chains.  In the latter case,
   1204  * pkt is really an mbuf.
   1205  */
   1206 static void
   1207 catchpacket(d, pkt, pktlen, snaplen, cpfn)
   1208 	struct bpf_d *d;
   1209 	u_char *pkt;
   1210 	u_int pktlen, snaplen;
   1211 	void *(*cpfn) __P((void *, const void *, size_t));
   1212 {
   1213 	struct bpf_hdr *hp;
   1214 	int totlen, curlen;
   1215 	int hdrlen = d->bd_bif->bif_hdrlen;
   1216 	/*
   1217 	 * Figure out how many bytes to move.  If the packet is
   1218 	 * greater or equal to the snapshot length, transfer that
   1219 	 * much.  Otherwise, transfer the whole packet (unless
   1220 	 * we hit the buffer size limit).
   1221 	 */
   1222 	totlen = hdrlen + min(snaplen, pktlen);
   1223 	if (totlen > d->bd_bufsize)
   1224 		totlen = d->bd_bufsize;
   1225 
   1226 	/*
   1227 	 * Round up the end of the previous packet to the next longword.
   1228 	 */
   1229 	curlen = BPF_WORDALIGN(d->bd_slen);
   1230 	if (curlen + totlen > d->bd_bufsize) {
   1231 		/*
   1232 		 * This packet will overflow the storage buffer.
   1233 		 * Rotate the buffers if we can, then wakeup any
   1234 		 * pending reads.
   1235 		 */
   1236 		if (d->bd_fbuf == 0) {
   1237 			/*
   1238 			 * We haven't completed the previous read yet,
   1239 			 * so drop the packet.
   1240 			 */
   1241 			++d->bd_dcount;
   1242 			return;
   1243 		}
   1244 		ROTATE_BUFFERS(d);
   1245 		bpf_wakeup(d);
   1246 		curlen = 0;
   1247 	}
   1248 
   1249 	/*
   1250 	 * Append the bpf header.
   1251 	 */
   1252 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
   1253 	microtime(&hp->bh_tstamp);
   1254 	hp->bh_datalen = pktlen;
   1255 	hp->bh_hdrlen = hdrlen;
   1256 	/*
   1257 	 * Copy the packet data into the store buffer and update its length.
   1258 	 */
   1259 	(*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
   1260 	d->bd_slen = curlen + totlen;
   1261 
   1262 	if (d->bd_immediate) {
   1263 		/*
   1264 		 * Immediate mode is set.  A packet arrived so any
   1265 		 * reads should be woken up.
   1266 		 */
   1267 		bpf_wakeup(d);
   1268 	}
   1269 }
   1270 
   1271 /*
   1272  * Initialize all nonzero fields of a descriptor.
   1273  */
   1274 static int
   1275 bpf_allocbufs(d)
   1276 	struct bpf_d *d;
   1277 {
   1278 
   1279 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
   1280 	if (!d->bd_fbuf)
   1281 		return (ENOBUFS);
   1282 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
   1283 	if (!d->bd_sbuf) {
   1284 		free(d->bd_fbuf, M_DEVBUF);
   1285 		return (ENOBUFS);
   1286 	}
   1287 	d->bd_slen = 0;
   1288 	d->bd_hlen = 0;
   1289 	return (0);
   1290 }
   1291 
   1292 /*
   1293  * Free buffers currently in use by a descriptor.
   1294  * Called on close.
   1295  */
   1296 static void
   1297 bpf_freed(d)
   1298 	struct bpf_d *d;
   1299 {
   1300 	/*
   1301 	 * We don't need to lock out interrupts since this descriptor has
   1302 	 * been detached from its interface and it yet hasn't been marked
   1303 	 * free.
   1304 	 */
   1305 	if (d->bd_sbuf != 0) {
   1306 		free(d->bd_sbuf, M_DEVBUF);
   1307 		if (d->bd_hbuf != 0)
   1308 			free(d->bd_hbuf, M_DEVBUF);
   1309 		if (d->bd_fbuf != 0)
   1310 			free(d->bd_fbuf, M_DEVBUF);
   1311 	}
   1312 	if (d->bd_filter)
   1313 		free((caddr_t)d->bd_filter, M_DEVBUF);
   1314 
   1315 	D_MARKFREE(d);
   1316 }
   1317 
   1318 /*
   1319  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
   1320  * fixed size of the link header (variable length headers not yet supported).
   1321  */
   1322 void
   1323 bpfattach(ifp, dlt, hdrlen)
   1324 	struct ifnet *ifp;
   1325 	u_int dlt, hdrlen;
   1326 {
   1327 
   1328 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
   1329 }
   1330 
   1331 /*
   1332  * Attach additional dlt for a interface to bpf.  dlt is the link layer type;
   1333  * hdrlen is the fixed size of the link header for the specified dlt
   1334  * (variable length headers not yet supported).
   1335  */
   1336 void
   1337 bpfattach2(ifp, dlt, hdrlen, driverp)
   1338 	struct ifnet *ifp;
   1339 	u_int dlt, hdrlen;
   1340 	caddr_t *driverp;
   1341 {
   1342 	struct bpf_if *bp;
   1343 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
   1344 	if (bp == 0)
   1345 		panic("bpfattach");
   1346 
   1347 	bp->bif_dlist = 0;
   1348 	bp->bif_driverp = (struct bpf_if **)driverp;
   1349 	bp->bif_ifp = ifp;
   1350 	bp->bif_dlt = dlt;
   1351 
   1352 	bp->bif_next = bpf_iflist;
   1353 	bpf_iflist = bp;
   1354 
   1355 	*bp->bif_driverp = 0;
   1356 
   1357 	/*
   1358 	 * Compute the length of the bpf header.  This is not necessarily
   1359 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1360 	 * that the network layer header begins on a longword boundary (for
   1361 	 * performance reasons and to alleviate alignment restrictions).
   1362 	 */
   1363 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1364 
   1365 #if 0
   1366 	printf("bpf: %s attached\n", ifp->if_xname);
   1367 #endif
   1368 }
   1369 
   1370 /*
   1371  * Remove an interface from bpf.
   1372  */
   1373 void
   1374 bpfdetach(ifp)
   1375 	struct ifnet *ifp;
   1376 {
   1377 	struct bpf_if *bp, **pbp;
   1378 	struct bpf_d *d;
   1379 	int i, s, cmaj;
   1380 
   1381 	/* locate the major number */
   1382 	cmaj = cdevsw_lookup_major(&bpf_cdevsw);
   1383 
   1384 	/* Nuke the vnodes for any open instances */
   1385 	for (i = 0; i < NBPFILTER; ++i) {
   1386 		d = &bpf_dtab[i];
   1387 		if (!D_ISFREE(d) && d->bd_bif != NULL &&
   1388 		    d->bd_bif->bif_ifp == ifp) {
   1389 			/*
   1390 			 * Detach the descriptor from an interface now.
   1391 			 * It will be free'ed later by close routine.
   1392 			 */
   1393 			s = splnet();
   1394 			d->bd_promisc = 0;	/* we can't touch device. */
   1395 			bpf_detachd(d);
   1396 			splx(s);
   1397 			vdevgone(cmaj, i, i, VCHR);
   1398 		}
   1399 	}
   1400 
   1401   again:
   1402 	for (bp = bpf_iflist, pbp = &bpf_iflist;
   1403 	     bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
   1404 		if (bp->bif_ifp == ifp) {
   1405 			*pbp = bp->bif_next;
   1406 			free(bp, M_DEVBUF);
   1407 			goto again;
   1408 		}
   1409 	}
   1410 }
   1411 
   1412 /*
   1413  * Change the data link type of a interface.
   1414  */
   1415 void
   1416 bpf_change_type(ifp, dlt, hdrlen)
   1417 	struct ifnet *ifp;
   1418 	u_int dlt, hdrlen;
   1419 {
   1420 	struct bpf_if *bp;
   1421 
   1422 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1423 		if (bp->bif_driverp == (struct bpf_if **)&ifp->if_bpf)
   1424 			break;
   1425 	}
   1426 	if (bp == NULL)
   1427 		panic("bpf_change_type");
   1428 
   1429 	bp->bif_dlt = dlt;
   1430 
   1431 	/*
   1432 	 * Compute the length of the bpf header.  This is not necessarily
   1433 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1434 	 * that the network layer header begins on a longword boundary (for
   1435 	 * performance reasons and to alleviate alignment restrictions).
   1436 	 */
   1437 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1438 }
   1439 
   1440 /*
   1441  * Get a list of available data link type of the interface.
   1442  */
   1443 static int
   1444 bpf_getdltlist(d, bfl)
   1445 	struct bpf_d *d;
   1446 	struct bpf_dltlist *bfl;
   1447 {
   1448 	int n, error;
   1449 	struct ifnet *ifp;
   1450 	struct bpf_if *bp;
   1451 
   1452 	ifp = d->bd_bif->bif_ifp;
   1453 	n = 0;
   1454 	error = 0;
   1455 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1456 		if (bp->bif_ifp != ifp)
   1457 			continue;
   1458 		if (bfl->bfl_list != NULL) {
   1459 			if (n >= bfl->bfl_len)
   1460 				return ENOMEM;
   1461 			error = copyout(&bp->bif_dlt,
   1462 			    bfl->bfl_list + n, sizeof(u_int));
   1463 		}
   1464 		n++;
   1465 	}
   1466 	bfl->bfl_len = n;
   1467 	return error;
   1468 }
   1469 
   1470 /*
   1471  * Set the data link type of a BPF instance.
   1472  */
   1473 static int
   1474 bpf_setdlt(d, dlt)
   1475 	struct bpf_d *d;
   1476 	u_int dlt;
   1477 {
   1478 	int s, error, opromisc;
   1479 	struct ifnet *ifp;
   1480 	struct bpf_if *bp;
   1481 
   1482 	if (d->bd_bif->bif_dlt == dlt)
   1483 		return 0;
   1484 	ifp = d->bd_bif->bif_ifp;
   1485 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1486 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
   1487 			break;
   1488 	}
   1489 	if (bp == NULL)
   1490 		return EINVAL;
   1491 	s = splnet();
   1492 	opromisc = d->bd_promisc;
   1493 	bpf_detachd(d);
   1494 	bpf_attachd(d, bp);
   1495 	reset_d(d);
   1496 	if (opromisc) {
   1497 		error = ifpromisc(bp->bif_ifp, 1);
   1498 		if (error)
   1499 			printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
   1500 			    bp->bif_ifp->if_xname, error);
   1501 		else
   1502 			d->bd_promisc = 1;
   1503 	}
   1504 	splx(s);
   1505 	return 0;
   1506 }
   1507