xref: /csrg-svn/sys/net/bpf.c (revision 50417)
149343Sbostic /*-
249343Sbostic  * Copyright (c) 1991 The Regents of the University of California.
347584Smccanne  * All rights reserved.
447584Smccanne  *
549343Sbostic  * This code is derived from the Stanford/CMU enet packet filter,
649343Sbostic  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
749343Sbostic  * to Berkeley by Steven McCanne of Lawrence Berkeley Laboratory.
847584Smccanne  *
949343Sbostic  * %sccs.include.redist.c%
1049343Sbostic  *
11*50417Smccanne  *	@(#)bpf.c	7.5 (Berkeley) 07/15/91
1249343Sbostic  *
1349343Sbostic  * static char rcsid[] =
1449343Sbostic  * "$Header: bpf.c,v 1.23 91/01/30 18:22:13 mccanne Exp $";
1547584Smccanne  */
1647584Smccanne 
1747584Smccanne #include "bpfilter.h"
1847584Smccanne 
1947584Smccanne #if (NBPFILTER > 0)
2047584Smccanne 
2147584Smccanne #include <sys/param.h>
2247584Smccanne #include <sys/systm.h>
2347584Smccanne #include <sys/mbuf.h>
2447584Smccanne #include <sys/buf.h>
2547584Smccanne #include <sys/dir.h>
2648932Smccanne #include <sys/proc.h>
2747584Smccanne #include <sys/user.h>
2847584Smccanne #include <sys/ioctl.h>
2947584Smccanne #include <sys/map.h>
3047584Smccanne 
3147584Smccanne #include <sys/file.h>
3247584Smccanne #ifdef sparc
3347584Smccanne #include <sys/stream.h>
3447584Smccanne #endif
3547584Smccanne #include <sys/tty.h>
3647584Smccanne #include <sys/uio.h>
3747584Smccanne 
3847584Smccanne #include <sys/protosw.h>
3947584Smccanne #include <sys/socket.h>
4047584Smccanne #include <net/if.h>
4147584Smccanne 
4247584Smccanne #include <net/bpf.h>
4347584Smccanne #include <net/bpfdesc.h>
4447584Smccanne 
4547584Smccanne #include <sys/errno.h>
4647584Smccanne 
4747584Smccanne #include <netinet/in.h>
4847584Smccanne #include <netinet/if_ether.h>
4947584Smccanne #include <sys/kernel.h>
5047584Smccanne 
5147584Smccanne #define PRINET  26			/* interruptible */
5247584Smccanne 
5347584Smccanne /*
5448932Smccanne  * The default read buffer size is patchable.
5548932Smccanne  */
5648932Smccanne int bpf_bufsize = MCLBYTES;
5748932Smccanne 
5848932Smccanne /*
5949202Smccanne  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
6049202Smccanne  *  bpf_dtab holds the descriptors, indexed by minor device #
6147584Smccanne  *
6247584Smccanne  * We really don't need NBPFILTER bpf_if entries, but this eliminates
6347584Smccanne  * the need to account for all possible drivers here.
6447584Smccanne  * This problem will go away when these structures are allocated dynamically.
6547584Smccanne  */
6649202Smccanne static struct bpf_if 	*bpf_iflist;
6747584Smccanne static struct bpf_d	bpf_dtab[NBPFILTER];
6847584Smccanne 
6947584Smccanne static void	bpf_ifname();
7047584Smccanne static void	catchpacket();
7147584Smccanne static int	bpf_setif();
7247584Smccanne static int	bpf_initd();
7347584Smccanne 
7447584Smccanne static int
7547584Smccanne bpf_movein(uio, linktype, mp, sockp)
7647584Smccanne 	register struct uio *uio;
7747584Smccanne 	int linktype;
7847584Smccanne 	register struct mbuf **mp;
7947584Smccanne 	register struct sockaddr *sockp;
8047584Smccanne {
8147584Smccanne 	struct mbuf *m;
8247584Smccanne 	int error;
8347584Smccanne 	int len;
8447584Smccanne 	int hlen;
8547584Smccanne 
8647584Smccanne 	/*
8747584Smccanne 	 * Build a sockaddr based on the data link layer type.
8847584Smccanne 	 * We do this at this level because the ethernet header
8947584Smccanne 	 * is copied directly into the data field of the sockaddr.
9047584Smccanne 	 * In the case of SLIP, there is no header and the packet
9147584Smccanne 	 * is forwarded as is.
9247584Smccanne 	 * Also, we are careful to leave room at the front of the mbuf
9347584Smccanne 	 * for the link level header.
9447584Smccanne 	 */
9547584Smccanne 	switch (linktype) {
9647584Smccanne 	case DLT_SLIP:
9747584Smccanne 		sockp->sa_family = AF_INET;
9847584Smccanne 		hlen = 0;
9947584Smccanne 		break;
10047584Smccanne 
10147584Smccanne 	case DLT_EN10MB:
10247584Smccanne 		sockp->sa_family = AF_UNSPEC;
10347584Smccanne 		/* XXX Would MAXLINKHDR be better? */
10447584Smccanne 		hlen = sizeof(struct ether_header);
10547584Smccanne 		break;
10647584Smccanne 
10747584Smccanne        case DLT_FDDI:
10847584Smccanne 		sockp->sa_family = AF_UNSPEC;
10947584Smccanne 		/* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
11047584Smccanne 		hlen = 24;
11147584Smccanne 		break;
11247584Smccanne 
11347584Smccanne 	default:
11448932Smccanne 		return (EIO);
11547584Smccanne 	}
11647584Smccanne 
11747584Smccanne 	len = uio->uio_resid;
11847584Smccanne 	if ((unsigned)len > MCLBYTES)
11948932Smccanne 		return (EIO);
12047584Smccanne 
12147584Smccanne 	MGET(m, M_WAIT, MT_DATA);
12247584Smccanne 	if (m == 0)
12348932Smccanne 		return (ENOBUFS);
12447584Smccanne 	if (len > MLEN) {
12548932Smccanne 		MCLGET(m, M_WAIT);
12648932Smccanne 		if ((m->m_flags & M_EXT) == 0) {
12747584Smccanne 			error = ENOBUFS;
12847584Smccanne 			goto bad;
12947584Smccanne 		}
13047584Smccanne 	}
13147584Smccanne 	m->m_len = len;
13247584Smccanne 	*mp = m;
13347584Smccanne 	/*
13447584Smccanne 	 * Make room for link header.
13547584Smccanne 	 */
13647584Smccanne 	if (hlen) {
13747584Smccanne 		m->m_len -= hlen;
13848932Smccanne 		m->m_data += hlen; /* XXX */
13947584Smccanne 
14048932Smccanne 		error = uiomove((caddr_t)sockp->sa_data, hlen, uio);
14147584Smccanne 		if (error)
14247584Smccanne 			goto bad;
14347584Smccanne 	}
14448932Smccanne 	error = uiomove(mtod(m, caddr_t), len - hlen, uio);
14547584Smccanne 	if (!error)
14648932Smccanne 		return (0);
14747584Smccanne  bad:
14847584Smccanne 	m_freem(m);
14948932Smccanne 	return (error);
15047584Smccanne }
15147584Smccanne 
15247584Smccanne /*
15347584Smccanne  * Attach 'd' to the bpf interface 'bp', i.e. make 'd' listen on 'bp'.
15447584Smccanne  * Must be called at splimp.
15547584Smccanne  */
15647584Smccanne static void
15747584Smccanne bpf_attachd(d, bp)
15847584Smccanne 	struct bpf_d *d;
15947584Smccanne 	struct bpf_if *bp;
16047584Smccanne {
16148932Smccanne 	/* Point d at bp. */
16247584Smccanne 	d->bd_bif = bp;
16347584Smccanne 
16448932Smccanne 	/* Add d to bp's list of listeners. */
16547584Smccanne 	d->bd_next = bp->bif_dlist;
16647584Smccanne 	bp->bif_dlist = d;
16747584Smccanne 
16847584Smccanne 	/*
16947584Smccanne 	 * Let the driver know we're here (if it doesn't already).
17047584Smccanne 	 */
17147584Smccanne 	*bp->bif_driverp = bp;
17247584Smccanne }
17347584Smccanne 
17447584Smccanne static void
17547584Smccanne bpf_detachd(d)
17647584Smccanne 	struct bpf_d *d;
17747584Smccanne {
17847584Smccanne 	struct bpf_d **p;
17947584Smccanne 	struct bpf_if *bp;
18047584Smccanne 
18147584Smccanne 	bp = d->bd_bif;
18247584Smccanne 	/*
18347584Smccanne 	 * Check if this descriptor had requested promiscuous mode.
18447584Smccanne 	 * If so, turn it off.
18547584Smccanne 	 */
18647584Smccanne 	if (d->bd_promisc) {
18747584Smccanne 		d->bd_promisc = 0;
18847584Smccanne 		if (ifpromisc(bp->bif_ifp, 0))
18947584Smccanne 			/*
19047584Smccanne 			 * Something is really wrong if we were able to put
19147584Smccanne 			 * the driver into promiscuous mode, but can't
19247584Smccanne 			 * take it out.
19347584Smccanne 			 */
19448932Smccanne 			panic("bpf_detachd: ifpromisc failed");
19547584Smccanne 	}
19647584Smccanne 	/* Remove 'd' from the interface's descriptor list. */
19747584Smccanne 	p = &bp->bif_dlist;
19847584Smccanne 	while (*p != d) {
19947584Smccanne 		p = &(*p)->bd_next;
20047584Smccanne 		if (*p == 0)
20147584Smccanne 			panic("bpf_detachd: descriptor not in list");
20247584Smccanne 	}
20347584Smccanne 	*p = (*p)->bd_next;
20447584Smccanne 	if (bp->bif_dlist == 0)
20547584Smccanne 		/*
20647584Smccanne 		 * Let the driver know that there are no more listeners.
20747584Smccanne 		 */
20847584Smccanne 		*d->bd_bif->bif_driverp = 0;
20947584Smccanne 	d->bd_bif = 0;
21047584Smccanne }
21147584Smccanne 
21247584Smccanne 
21347584Smccanne /*
21447584Smccanne  * Mark a descriptor free by making it point to itself.
21547584Smccanne  * This is probably cheaper than marking with a constant since
21647584Smccanne  * the address should be in a register anyway.
21747584Smccanne  */
21847584Smccanne #define D_ISFREE(d) ((d) == (d)->bd_next)
21947584Smccanne #define D_MARKFREE(d) ((d)->bd_next = (d))
22047584Smccanne #define D_MARKUSED(d) ((d)->bd_next = 0)
22147584Smccanne 
22247584Smccanne /*
22347584Smccanne  *  bpfopen - open ethernet device
22447584Smccanne  *
22547584Smccanne  *  Errors:	ENXIO	- illegal minor device number
22647584Smccanne  *		EBUSY	- too many files open
22747584Smccanne  */
22847584Smccanne /* ARGSUSED */
22947584Smccanne int
23047584Smccanne bpfopen(dev, flag)
23147584Smccanne 	dev_t dev;
23247584Smccanne 	int flag;
23347584Smccanne {
23447584Smccanne 	int error, s;
23547584Smccanne 	register struct bpf_d *d;
23647584Smccanne 
23747584Smccanne 	if (minor(dev) >= NBPFILTER)
23848932Smccanne 		return (ENXIO);
23947584Smccanne 
24047584Smccanne 	/*
24147584Smccanne 	 * Each minor can be opened by only one process.  If the requested
24247584Smccanne 	 * minor is in use, return EBUSY.
24347584Smccanne 	 */
24447584Smccanne 	s = splimp();
24547584Smccanne 	d = &bpf_dtab[minor(dev)];
24647584Smccanne 	if (!D_ISFREE(d)) {
24747584Smccanne 		splx(s);
24848932Smccanne 		return (EBUSY);
24947584Smccanne 	} else
25047584Smccanne 		/* Mark "free" and do most initialization. */
25147584Smccanne 		bzero((char *)d, sizeof(*d));
25247584Smccanne 	splx(s);
25347584Smccanne 
25447584Smccanne 	error = bpf_initd(d);
25547584Smccanne 	if (error) {
25647584Smccanne 		D_MARKFREE(d);
25748932Smccanne 		return (error);
25847584Smccanne 	}
25948932Smccanne 	return (0);
26047584Smccanne }
26147584Smccanne 
26247584Smccanne /*
26347584Smccanne  * Close the descriptor by detaching it from its interface,
26447584Smccanne  * deallocating its buffers, and marking it free.
26547584Smccanne  */
26647584Smccanne /* ARGSUSED */
26747584Smccanne bpfclose(dev, flag)
26847584Smccanne 	dev_t dev;
26947584Smccanne 	int flag;
27047584Smccanne {
27147584Smccanne 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
27247584Smccanne 	int s;
27347584Smccanne 
27447584Smccanne 	s = splimp();
27547584Smccanne 	if (d->bd_bif)
27647584Smccanne 		bpf_detachd(d);
27747584Smccanne 	splx(s);
27847584Smccanne 
27948932Smccanne 	/* Free the buffer space. */
28048932Smccanne 	if (d->bd_hbuf)
28148932Smccanne 		free(d->bd_hbuf, M_DEVBUF);
28248932Smccanne 	if (d->bd_fbuf)
28348932Smccanne 		free(d->bd_fbuf, M_DEVBUF);
28448932Smccanne 	free(d->bd_sbuf, M_DEVBUF);
28548967Smccanne 	if (d->bd_filter)
28648932Smccanne 		free((caddr_t)d->bd_filter, M_DEVBUF);
28747584Smccanne 
28847584Smccanne 	D_MARKFREE(d);
28947584Smccanne }
29047584Smccanne 
29147584Smccanne /*
29248932Smccanne  * Rotate the packet buffers in descriptor d.  Move the store buffer
29348932Smccanne  * into the hold slot, and the free buffer into the store slot.
29448932Smccanne  * Zero the length of the new store buffer.
29548932Smccanne  */
29648932Smccanne #define ROTATE_BUFFERS(d) \
29748932Smccanne 	(d)->bd_hbuf = (d)->bd_sbuf; \
29848932Smccanne 	(d)->bd_hlen = (d)->bd_slen; \
29948932Smccanne 	(d)->bd_sbuf = (d)->bd_fbuf; \
30048932Smccanne 	(d)->bd_slen = 0; \
30148932Smccanne 	(d)->bd_fbuf = 0;
30248932Smccanne /*
30347584Smccanne  *  bpfread - read next chunk of packets from buffers
30447584Smccanne  */
30547584Smccanne int
30647584Smccanne bpfread(dev, uio)
30747584Smccanne 	dev_t dev;
30847584Smccanne 	register struct uio *uio;
30947584Smccanne {
31047584Smccanne 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
31147584Smccanne 	int error;
31247584Smccanne 	int s;
31347584Smccanne 
31447584Smccanne 	/*
31547584Smccanne 	 * Restrict application to use a buffer the same size as
31647584Smccanne 	 * as kernel buffers.
31747584Smccanne 	 */
31848932Smccanne 	if (uio->uio_resid != d->bd_bufsize)
31949202Smccanne 		return (EINVAL);
32047584Smccanne 
32147584Smccanne 	s = splimp();
32247584Smccanne 	/*
32347584Smccanne 	 * If the hold buffer is empty, then set a timer and sleep
32447584Smccanne 	 * until either the timeout has occurred or enough packets have
32547584Smccanne 	 * arrived to fill the store buffer.
32647584Smccanne 	 */
32747584Smccanne 	while (d->bd_hbuf == 0) {
32848932Smccanne 		if (d->bd_immediate && d->bd_slen != 0) {
32947584Smccanne 			/*
33047584Smccanne 			 * A packet(s) either arrived since the previous
33147584Smccanne 			 * read or arrived while we were asleep.
33247584Smccanne 			 * Rotate the buffers and return what's here.
33347584Smccanne 			 */
33448932Smccanne 			ROTATE_BUFFERS(d);
33547584Smccanne 			break;
33647584Smccanne 		}
33748932Smccanne 		error = tsleep((caddr_t)d, PRINET|PCATCH, "bpf", d->bd_rtout);
33848932Smccanne 		if (error == EINTR || error == ERESTART) {
33948932Smccanne 			splx(s);
34048932Smccanne 			return (error);
34147584Smccanne 		}
34248932Smccanne 		if (error == EWOULDBLOCK) {
34347584Smccanne 			/*
34447584Smccanne 			 * On a timeout, return what's in the buffer,
34548932Smccanne 			 * which may be nothing.  If there is something
34648932Smccanne 			 * in the store buffer, we can rotate the buffers.
34747584Smccanne 			 */
34847584Smccanne 			if (d->bd_hbuf)
34947584Smccanne 				/*
35047584Smccanne 				 * We filled up the buffer in between
35147584Smccanne 				 * getting the timeout and arriving
35247584Smccanne 				 * here, so we don't need to rotate.
35347584Smccanne 				 */
35447584Smccanne 				break;
35547584Smccanne 
35648932Smccanne 			if (d->bd_slen == 0) {
35747584Smccanne 				splx(s);
35848932Smccanne 				return (0);
35947584Smccanne 			}
36048932Smccanne 			ROTATE_BUFFERS(d);
36147584Smccanne 			break;
36247584Smccanne 		}
36347584Smccanne 	}
36447584Smccanne 	/*
36547584Smccanne 	 * At this point, we know we have something in the hold slot.
36647584Smccanne 	 */
36747584Smccanne 	splx(s);
36847584Smccanne 
36947584Smccanne 	/*
37047584Smccanne 	 * Move data from hold buffer into user space.
37147584Smccanne 	 * We know the entire buffer is transferred since
37248932Smccanne 	 * we checked above that the read buffer is bpf_bufsize bytes.
37347584Smccanne 	 */
37448932Smccanne 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
37547584Smccanne 
37647584Smccanne 	s = splimp();
37748932Smccanne 	d->bd_fbuf = d->bd_hbuf;
37848932Smccanne 	d->bd_hbuf = 0;
37947584Smccanne 	splx(s);
38047584Smccanne 
38148932Smccanne 	return (error);
38247584Smccanne }
38347584Smccanne 
38447584Smccanne 
38547584Smccanne /*
38648932Smccanne  * If there are processes sleeping on this descriptor, wake them up.
38747584Smccanne  */
38847584Smccanne static inline void
38947584Smccanne bpf_wakeup(d)
39047584Smccanne 	register struct bpf_d *d;
39147584Smccanne {
39248932Smccanne 	wakeup((caddr_t)d);
39348932Smccanne 	if (d->bd_selproc) {
39448932Smccanne 		selwakeup(d->bd_selproc, (int)d->bd_selcoll);
39548932Smccanne 		d->bd_selcoll = 0;
39648932Smccanne 		d->bd_selproc = 0;
39747584Smccanne 	}
39847584Smccanne }
39947584Smccanne 
40047584Smccanne int
40147584Smccanne bpfwrite(dev, uio)
40247584Smccanne 	dev_t dev;
40347584Smccanne 	struct uio *uio;
40447584Smccanne {
40547584Smccanne 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
40647584Smccanne 	struct ifnet *ifp;
40747584Smccanne 	struct mbuf *m;
40847584Smccanne 	int error, s;
40947584Smccanne 	static struct sockaddr dst;
41047584Smccanne 
41147584Smccanne 	if (d->bd_bif == 0)
41248932Smccanne 		return (ENXIO);
41347584Smccanne 
41447584Smccanne 	ifp = d->bd_bif->bif_ifp;
41547584Smccanne 
41647584Smccanne 	if (uio->uio_resid == 0)
41748932Smccanne 		return (0);
41847584Smccanne 	if (uio->uio_resid > ifp->if_mtu)
41948932Smccanne 		return (EMSGSIZE);
42047584Smccanne 
42149202Smccanne 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst);
42247584Smccanne 	if (error)
42348932Smccanne 		return (error);
42447584Smccanne 
42547584Smccanne 	s = splnet();
42647584Smccanne 	error = (*ifp->if_output)(ifp, m, &dst);
42747584Smccanne 	splx(s);
42847584Smccanne 	/*
42947584Smccanne 	 * The driver frees the mbuf.
43047584Smccanne 	 */
43148932Smccanne 	return (error);
43247584Smccanne }
43347584Smccanne 
43447584Smccanne /*
43548932Smccanne  * Reset a descriptor by flushing its packet bufferand clearing the receive
43648932Smccanne  * and drop counts.  Should be called at splimp.
43747584Smccanne  */
43847584Smccanne static void
43947584Smccanne reset_d(d)
44047584Smccanne 	struct bpf_d *d;
44147584Smccanne {
44247584Smccanne 	if (d->bd_hbuf) {
44347584Smccanne 		/* Free the hold buffer. */
44447584Smccanne 		d->bd_fbuf = d->bd_hbuf;
44547584Smccanne 		d->bd_hbuf = 0;
44647584Smccanne 	}
44748932Smccanne 	d->bd_slen = 0;
44847584Smccanne 	d->bd_rcount = 0;
44947584Smccanne 	d->bd_dcount = 0;
45047584Smccanne }
45147584Smccanne 
45247584Smccanne /*
45347584Smccanne  *  FIONREAD		Check for read packet available.
45447584Smccanne  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
45547584Smccanne  *  BIOCGFLEN		Get max filter len.
45647584Smccanne  *  BIOCGBLEN		Get buffer len [for read()].
45747584Smccanne  *  BIOCSETF		Set ethernet read filter.
45847584Smccanne  *  BIOCFLUSH		Flush read packet buffer.
45947584Smccanne  *  BIOCPROMISC		Put interface into promiscuous mode.
46049202Smccanne  *  BIOCGDLT		Get link layer type.
46147584Smccanne  *  BIOCGETIF		Get interface name.
46247584Smccanne  *  BIOCSETIF		Set interface.
46347584Smccanne  *  BIOCSRTIMEOUT	Set read timeout.
46447584Smccanne  *  BIOCGRTIMEOUT	Get read timeout.
46547584Smccanne  *  BIOCGSTATS		Get packet stats.
46647584Smccanne  *  BIOCIMMEDIATE	Set immediate mode.
46747584Smccanne  */
46847584Smccanne /* ARGSUSED */
46947584Smccanne int
47047584Smccanne bpfioctl(dev, cmd, addr, flag)
47147584Smccanne 	dev_t dev;
47247584Smccanne 	int cmd;
47347584Smccanne 	caddr_t addr;
47447584Smccanne 	int flag;
47547584Smccanne {
47647584Smccanne 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
47747584Smccanne 	int s, error = 0;
47847584Smccanne 
47947584Smccanne 	switch (cmd) {
48047584Smccanne 
48147584Smccanne 	default:
48247584Smccanne 		error = EINVAL;
48347584Smccanne 		break;
48447584Smccanne 
48547584Smccanne 	/*
48647584Smccanne 	 * Check for read packet available.
48747584Smccanne 	 */
48847584Smccanne 	case FIONREAD:
48947584Smccanne 		{
49047584Smccanne 			int n;
49147584Smccanne 
49247584Smccanne 			s = splimp();
49348932Smccanne 			n = d->bd_slen;
49447584Smccanne 			if (d->bd_hbuf)
49548932Smccanne 				n += d->bd_hlen;
49647584Smccanne 			splx(s);
49747584Smccanne 
49847584Smccanne 			*(int *)addr = n;
49947584Smccanne 			break;
50047584Smccanne 		}
50147584Smccanne 
50247584Smccanne 	case SIOCGIFADDR:
50347584Smccanne 		{
50447584Smccanne 			struct ifnet *ifp;
50547584Smccanne 
50647584Smccanne 			if (d->bd_bif == 0)
50747584Smccanne 				error = EINVAL;
50847584Smccanne 			else {
50947584Smccanne 				ifp = d->bd_bif->bif_ifp;
51047584Smccanne 				error =  (*ifp->if_ioctl)(ifp, cmd, addr);
51147584Smccanne 			}
51247584Smccanne 			break;
51347584Smccanne 		}
51447584Smccanne 
51547584Smccanne 	/*
51647584Smccanne 	 * Get max filter len.
51747584Smccanne 	 */
51847584Smccanne 	case BIOCGFLEN:
51948932Smccanne 		*(u_int *)addr = BPF_MAXINSNS;
52047584Smccanne 		break;
52147584Smccanne 	/*
52247584Smccanne 	 * Get buffer len [for read()].
52347584Smccanne 	 */
52447584Smccanne 	case BIOCGBLEN:
52548932Smccanne 		*(u_int *)addr = d->bd_bufsize;
52647584Smccanne 		break;
52747584Smccanne 
52847584Smccanne 	/*
52947584Smccanne 	 * Set ethernet read filter.
53047584Smccanne 	 */
53147584Smccanne         case BIOCSETF:
53247584Smccanne 		error = bpf_setf(d, (struct bpf_program *)addr);
53347584Smccanne 		break;
53447584Smccanne 
53547584Smccanne 	/*
53647584Smccanne 	 * Flush read packet buffer.
53747584Smccanne 	 */
53847584Smccanne 	case BIOCFLUSH:
53947584Smccanne 		s = splimp();
54047584Smccanne 		reset_d(d);
54147584Smccanne 		splx(s);
54247584Smccanne 		break;
54347584Smccanne 
54447584Smccanne 	/*
54547584Smccanne 	 * Put interface into promiscuous mode.
54647584Smccanne 	 */
54747584Smccanne 	case BIOCPROMISC:
54847584Smccanne 		if (d->bd_bif == 0) {
54947584Smccanne 			/*
55047584Smccanne 			 * No interface attached yet.
55147584Smccanne 			 */
55247584Smccanne 			error = EINVAL;
55347584Smccanne 			break;
55447584Smccanne 		}
55547584Smccanne 		s = splimp();
55647584Smccanne 		if (d->bd_promisc == 0) {
55747584Smccanne 			d->bd_promisc = 1;
55847584Smccanne 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
55947584Smccanne 		}
56047584Smccanne 		splx(s);
56147584Smccanne 		break;
56247584Smccanne 
56347584Smccanne 	/*
56447584Smccanne 	 * Get device parameters.
56547584Smccanne 	 */
56649202Smccanne 	case BIOCGDLT:
56747584Smccanne 		if (d->bd_bif == 0)
56847584Smccanne 			error = EINVAL;
56947584Smccanne 		else
57049202Smccanne 			*(u_int *)addr = d->bd_bif->bif_dlt;
57147584Smccanne 		break;
57247584Smccanne 
57347584Smccanne 	/*
57447584Smccanne 	 * Set interface name.
57547584Smccanne 	 */
57647584Smccanne 	case BIOCGETIF:
57747584Smccanne 		if (d->bd_bif == 0)
57847584Smccanne 			error = EINVAL;
57947584Smccanne 		else
58047584Smccanne 			bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
58147584Smccanne 		break;
58247584Smccanne 
58347584Smccanne 	/*
58447584Smccanne 	 * Set interface.
58547584Smccanne 	 */
58647584Smccanne 	case BIOCSETIF:
58747584Smccanne 		error = bpf_setif(d, (struct ifreq *)addr);
58847584Smccanne 		break;
58947584Smccanne 
59047584Smccanne 	/*
59147584Smccanne 	 * Set read timeout.
59247584Smccanne 	 */
59347584Smccanne  	case BIOCSRTIMEOUT:
59447584Smccanne 		{
59547584Smccanne 			struct timeval *tv = (struct timeval *)addr;
59647584Smccanne 			u_long msec;
59747584Smccanne 
59847584Smccanne 			/* Compute number of milliseconds. */
59947584Smccanne 			msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
60047584Smccanne 			/* Scale milliseconds to ticks.  Assume hard
60147584Smccanne 			   clock has millisecond or greater resolution
60247584Smccanne 			   (i.e. tick >= 1000).  For 10ms hardclock,
60347584Smccanne 			   tick/1000 = 10, so rtout<-msec/10. */
60447584Smccanne 			d->bd_rtout = msec / (tick / 1000);
60547584Smccanne 			break;
60647584Smccanne 		}
60747584Smccanne 
60847584Smccanne 	/*
60947584Smccanne 	 * Get read timeout.
61047584Smccanne 	 */
61147584Smccanne  	case BIOCGRTIMEOUT:
61247584Smccanne 		{
61347584Smccanne 			struct timeval *tv = (struct timeval *)addr;
61447584Smccanne 			u_long msec = d->bd_rtout;
61547584Smccanne 
61647584Smccanne 			msec *= tick / 1000;
61747584Smccanne 			tv->tv_sec = msec / 1000;
61847584Smccanne 			tv->tv_usec = msec % 1000;
61947584Smccanne 			break;
62047584Smccanne 		}
62147584Smccanne 
62247584Smccanne 	/*
62347584Smccanne 	 * Get packet stats.
62447584Smccanne 	 */
62547584Smccanne 	case BIOCGSTATS:
62647584Smccanne 		{
62747584Smccanne 			struct bpf_stat *bs = (struct bpf_stat *)addr;
62847584Smccanne 
62947584Smccanne 			bs->bs_recv = d->bd_rcount;
63047584Smccanne 			bs->bs_drop = d->bd_dcount;
63147584Smccanne 			break;
63247584Smccanne 		}
63347584Smccanne 
63447584Smccanne 	/*
63547584Smccanne 	 * Set immediate mode.
63647584Smccanne 	 */
63747584Smccanne 	case BIOCIMMEDIATE:
63847584Smccanne 		d->bd_immediate = *(u_int *)addr;
63947584Smccanne 		break;
64047584Smccanne 	}
64148932Smccanne 	return (error);
64247584Smccanne }
64347584Smccanne 
64447584Smccanne /*
64547584Smccanne  * Set d's packet filter program to 'fp'.  If 'd' already has a filter,
64648932Smccanne  * free it and replace it.  Returns EINVAL for bogus requests.
64747584Smccanne  */
64847584Smccanne int
64947584Smccanne bpf_setf(d, fp)
65047584Smccanne 	struct bpf_d *d;
65147584Smccanne 	struct bpf_program *fp;
65247584Smccanne {
65348932Smccanne 	struct bpf_insn *fcode, *old;
65447584Smccanne 	u_int flen, size;
65547584Smccanne 	int s;
65647584Smccanne 
65748932Smccanne 	old = d->bd_filter;
65847584Smccanne 	if (fp->bf_insns == 0) {
65947584Smccanne 		if (fp->bf_len != 0)
66048932Smccanne 			return (EINVAL);
66148932Smccanne 		s = splimp();
66248967Smccanne 		d->bd_filter = 0;
66347584Smccanne 		reset_d(d);
66447584Smccanne 		splx(s);
66548967Smccanne 		if (old != 0)
66648932Smccanne 			free((caddr_t)old, M_DEVBUF);
66748932Smccanne 		return (0);
66847584Smccanne 	}
66947584Smccanne 	flen = fp->bf_len;
67048932Smccanne 	if (flen > BPF_MAXINSNS)
67148932Smccanne 		return (EINVAL);
67248932Smccanne 
67347584Smccanne 	size = flen * sizeof(*fp->bf_insns);
67448932Smccanne 	fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
675*50417Smccanne 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
676*50417Smccanne 	    bpf_validate(fcode, (int)flen)) {
67748967Smccanne 		s = splimp();
67848932Smccanne 		d->bd_filter = fcode;
67947584Smccanne 		reset_d(d);
68047584Smccanne 		splx(s);
68148967Smccanne 		if (old != 0)
68248932Smccanne 			free((caddr_t)old, M_DEVBUF);
68347584Smccanne 
68448932Smccanne 		return (0);
68547584Smccanne 	}
68648932Smccanne 	free((caddr_t)fcode, M_DEVBUF);
68748932Smccanne 	return (EINVAL);
68847584Smccanne }
68947584Smccanne 
69047584Smccanne /*
69147584Smccanne  * Detach 'd' from its current interface (if attached at all) and attach to
69247584Smccanne  * the interface named 'name'.  Return ioctl error code or 0.
69347584Smccanne  */
69447584Smccanne static int
69547584Smccanne bpf_setif(d, ifr)
69647584Smccanne 	struct bpf_d *d;
69747584Smccanne 	struct ifreq *ifr;
69847584Smccanne {
69947584Smccanne 	struct bpf_if *bp;
70047584Smccanne 	char *cp;
70149202Smccanne 	int unit, s;
70247584Smccanne 
70347584Smccanne 	/*
70447584Smccanne 	 * Separate string into name part and unit number.  Put a null
70547584Smccanne 	 * byte at the end of the name part, and compute the number.
70647584Smccanne 	 * If the a unit number is unspecified, the default is 0,
70748932Smccanne 	 * as initialized above.  XXX This should be common code.
70847584Smccanne 	 */
70947584Smccanne 	unit = 0;
71047584Smccanne 	cp = ifr->ifr_name;
71147584Smccanne 	cp[sizeof(ifr->ifr_name) - 1] = '\0';
71247584Smccanne 	while (*cp++) {
71347584Smccanne 		if (*cp >= '0' && *cp <= '9') {
71447584Smccanne 			unit = *cp - '0';
71547584Smccanne 			*cp++ = '\0';
71647584Smccanne 			while (*cp)
71747584Smccanne 				unit = 10 * unit + *cp++ - '0';
71847584Smccanne 			break;
71947584Smccanne 		}
72047584Smccanne 	}
72147584Smccanne 	/*
72247584Smccanne 	 * Look through attached interfaces for the named one.
72347584Smccanne 	 */
72449202Smccanne 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
72547584Smccanne 		struct ifnet *ifp = bp->bif_ifp;
72647584Smccanne 
72747584Smccanne 		if (ifp == 0 || unit != ifp->if_unit
72847584Smccanne 		    || strcmp(ifp->if_name, ifr->ifr_name) != 0)
72947584Smccanne 			continue;
73047584Smccanne 		/*
73147584Smccanne 		 * We found the requested interface.  If we're
73247584Smccanne 		 * already attached to it, just flush the buffer.
73347584Smccanne 		 * If it's not up, return an error.
73447584Smccanne 		 */
73547584Smccanne 		if ((ifp->if_flags & IFF_UP) == 0)
73648932Smccanne 			return (ENETDOWN);
73747584Smccanne 		s = splimp();
73847584Smccanne 		if (bp != d->bd_bif) {
73947584Smccanne 			if (d->bd_bif)
74047584Smccanne 				/*
74148932Smccanne 				 * Detach if attached to something else.
74247584Smccanne 				 */
74347584Smccanne 				bpf_detachd(d);
74447584Smccanne 
74547584Smccanne 			bpf_attachd(d, bp);
74647584Smccanne 		}
74747584Smccanne 		reset_d(d);
74847584Smccanne 		splx(s);
74948932Smccanne 		return (0);
75047584Smccanne 	}
75147584Smccanne 	/* Not found. */
75248932Smccanne 	return (ENXIO);
75347584Smccanne }
75447584Smccanne 
75547584Smccanne /*
75647584Smccanne  * Lookup the name of the 'ifp' interface and return it in 'ifr->ifr_name'.
75747584Smccanne  * We augment the ifp's base name with its unit number.
75847584Smccanne  */
75947584Smccanne static void
76047584Smccanne bpf_ifname(ifp, ifr)
76147584Smccanne 	struct ifnet *ifp;
76247584Smccanne 	struct ifreq *ifr;
76347584Smccanne {
76447584Smccanne 	char *s = ifp->if_name;
76547584Smccanne 	char *d = ifr->ifr_name;
76647584Smccanne 
76747584Smccanne 	while (*d++ = *s++)
76847584Smccanne 		;
76948932Smccanne 	/* XXX Assume that unit number is less than 10. */
77047584Smccanne 	*d++ = ifp->if_unit + '0';
77147584Smccanne 	*d = '\0';
77247584Smccanne }
77347584Smccanne 
77447584Smccanne /*
77547584Smccanne  * Support for select() system call
77647584Smccanne  * Inspired by the code in tty.c for the same purpose.
77747584Smccanne  *
77847584Smccanne  * bpfselect - returns true iff the specific operation
77947584Smccanne  *	will not block indefinitely.  Otherwise, return
78047584Smccanne  *	false but make a note that a selwakeup() must be done.
78147584Smccanne  */
78247584Smccanne int
78348932Smccanne bpfselect(dev, rw, p)
78447584Smccanne 	register dev_t dev;
78547584Smccanne 	int rw;
78648932Smccanne 	struct proc *p;
78747584Smccanne {
78847584Smccanne 	register struct bpf_d *d;
78947584Smccanne 	register int s;
79047584Smccanne 
79147584Smccanne 	if (rw != FREAD)
79248932Smccanne 		return (0);
79347584Smccanne 	/*
79447584Smccanne 	 * An imitation of the FIONREAD ioctl code.
79547584Smccanne 	 */
79647584Smccanne 	d = &bpf_dtab[minor(dev)];
79747584Smccanne 
79847584Smccanne 	s = splimp();
79949723Smccanne 	if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0)) {
80047584Smccanne 		/*
80147584Smccanne 		 * There is data waiting.
80247584Smccanne 		 */
80347584Smccanne 		splx(s);
80448932Smccanne 		return (1);
80547584Smccanne 	}
80647584Smccanne 	/*
80747584Smccanne 	 * No data ready.  If there's already a select() waiting on this
80847584Smccanne 	 * minor device then this is a collision.  This shouldn't happen
80947584Smccanne 	 * because minors really should not be shared, but if a process
81047584Smccanne 	 * forks while one of these is open, it is possible that both
81147584Smccanne 	 * processes could select on the same descriptor.
81247584Smccanne 	 */
81348932Smccanne 	if (d->bd_selproc && d->bd_selproc->p_wchan == (caddr_t)&selwait)
81448932Smccanne 		d->bd_selcoll = 1;
81547584Smccanne 	else
81648932Smccanne 		d->bd_selproc = p;
81748932Smccanne 
81847584Smccanne 	splx(s);
81948932Smccanne 	return (0);
82047584Smccanne }
82147584Smccanne 
82247584Smccanne /*
82347584Smccanne  * bpf_tap - incoming linkage from device drivers
82447584Smccanne  */
82547584Smccanne void
82648932Smccanne bpf_tap(arg, pkt, pktlen)
82747584Smccanne 	caddr_t arg;
82848932Smccanne 	register u_char *pkt;
82948932Smccanne 	register u_int pktlen;
83047584Smccanne {
83147584Smccanne 	struct bpf_if *bp;
83247584Smccanne 	register struct bpf_d *d;
83347584Smccanne 	register u_int slen;
83447584Smccanne 	/*
83547584Smccanne 	 * Note that the ipl does not have to be raised at this point.
83647584Smccanne 	 * The only problem that could arise here is that if two different
83747584Smccanne 	 * interfaces shared any data.  This is not the case.
83847584Smccanne 	 */
83947584Smccanne 	bp = (struct bpf_if *)arg;
84047584Smccanne 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
84147584Smccanne 		++d->bd_rcount;
84249202Smccanne 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
84347584Smccanne 		if (slen != 0)
84449202Smccanne 			catchpacket(d, pkt, pktlen, slen, bcopy);
84547584Smccanne 	}
84647584Smccanne }
84747584Smccanne 
84847584Smccanne /*
84947584Smccanne  * Copy data from an mbuf chain into a buffer.  This code is derived
85047584Smccanne  * from m_copydata in sys/uipc_mbuf.c.
85147584Smccanne  */
85247584Smccanne static void
85349202Smccanne bpf_mcopy(src, dst, len)
85447584Smccanne 	u_char *src;
85547584Smccanne 	u_char *dst;
85647584Smccanne 	register int len;
85747584Smccanne {
85847584Smccanne 	register struct mbuf *m = (struct mbuf *)src;
85947584Smccanne 	register unsigned count;
86047584Smccanne 
86147584Smccanne 	while (len > 0) {
86247584Smccanne 		if (m == 0)
86349202Smccanne 			panic("bpf_mcopy");
86447584Smccanne 		count = MIN(m->m_len, len);
86549202Smccanne 		bcopy(mtod(m, caddr_t), (caddr_t)dst, count);
86649202Smccanne 		m = m->m_next;
86749202Smccanne 		dst += count;
86847584Smccanne 		len -= count;
86947584Smccanne 	}
87047584Smccanne }
87147584Smccanne 
87247584Smccanne /*
87347584Smccanne  * bpf_mtap - incoming linkage from device drivers, when packet
87447584Smccanne  *   is in an mbuf chain
87547584Smccanne  */
87647584Smccanne void
87749202Smccanne bpf_mtap(arg, m)
87847584Smccanne 	caddr_t arg;
87949202Smccanne 	struct mbuf *m;
88047584Smccanne {
88147584Smccanne 	struct bpf_if *bp = (struct bpf_if *)arg;
88247584Smccanne 	struct bpf_d *d;
88349202Smccanne 	u_int pktlen, slen;
88449202Smccanne 	struct mbuf *m0;
88547584Smccanne 
88648932Smccanne 	pktlen = 0;
88749723Smccanne 	for (m0 = m; m0 != m; m0 = m0->m_next)
88849202Smccanne 		pktlen += m0->m_len;
88949723Smccanne 
89047584Smccanne 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
89147584Smccanne 		++d->bd_rcount;
89249202Smccanne 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
89347584Smccanne 		if (slen != 0)
89449202Smccanne 			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
89547584Smccanne 	}
89647584Smccanne }
89747584Smccanne 
89847584Smccanne /*
89949202Smccanne  * Move the packet data from interface memory (pkt) into the
90047584Smccanne  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
90147584Smccanne  * otherwise 0.  'copy' is the routine called to do the actual data
90247584Smccanne  * transfer.  'bcopy' is passed in to copy contiguous chunks, while
90349202Smccanne  * 'bpf_mcopy' is passed in to copy mbuf chains.  In the latter
90448932Smccanne  * case, 'pkt' is really an mbuf.
90547584Smccanne  */
90647584Smccanne static void
90748932Smccanne catchpacket(d, pkt, pktlen, snaplen, cpfn)
90848932Smccanne 	register struct bpf_d *d;
90948932Smccanne 	register u_char *pkt;
91048932Smccanne 	register u_int pktlen, snaplen;
91148932Smccanne 	register void (*cpfn)();
91247584Smccanne {
91348932Smccanne 	register struct bpf_hdr *hp;
91448932Smccanne 	register int totlen, curlen;
91548932Smccanne 	register int hdrlen = d->bd_bif->bif_hdrlen;
91647584Smccanne 	/*
91747584Smccanne 	 * Figure out how many bytes to move.  If the packet is
91847584Smccanne 	 * greater or equal to the snapshot length, transfer that
91947584Smccanne 	 * much.  Otherwise, transfer the whole packet (unless
92048932Smccanne 	 * we hit the buffer size limit).
92147584Smccanne 	 */
92250082Smccanne 	totlen = hdrlen + MIN(snaplen, pktlen);
92350082Smccanne 	if (totlen > d->bd_bufsize)
92450082Smccanne 		totlen = d->bd_bufsize;
92547584Smccanne 
92647584Smccanne 	/*
92747584Smccanne 	 * Round up the end of the previous packet to the next longword.
92847584Smccanne 	 */
92948932Smccanne 	curlen = BPF_WORDALIGN(d->bd_slen);
93048932Smccanne 	if (curlen + totlen > d->bd_bufsize) {
93147584Smccanne 		/*
93247584Smccanne 		 * This packet will overflow the storage buffer.
93348932Smccanne 		 * Rotate the buffers if we can, then wakeup any
93448932Smccanne 		 * pending reads.
93547584Smccanne 		 */
93647584Smccanne 		if (d->bd_fbuf == 0) {
93747584Smccanne 			/*
93848932Smccanne 			 * We haven't completed the previous read yet,
93948932Smccanne 			 * so drop the packet.
94047584Smccanne 			 */
94147584Smccanne 			++d->bd_dcount;
94247584Smccanne 			return;
94347584Smccanne 		}
94448932Smccanne 		ROTATE_BUFFERS(d);
94547584Smccanne 		bpf_wakeup(d);
94648932Smccanne 		curlen = 0;
94747584Smccanne 	}
94848932Smccanne 	else if (d->bd_immediate)
94947584Smccanne 		/*
95047584Smccanne 		 * Immediate mode is set.  A packet arrived so any
95147584Smccanne 		 * reads should be woken up.
95247584Smccanne 		 */
95347584Smccanne 		bpf_wakeup(d);
95448932Smccanne 
95547584Smccanne 	/*
95647584Smccanne 	 * Append the bpf header.
95747584Smccanne 	 */
95848932Smccanne 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
95947584Smccanne #ifdef sun
96047584Smccanne 	uniqtime(&hp->bh_tstamp);
96147584Smccanne #else
96247584Smccanne #ifdef hp300
96347584Smccanne 	microtime(&hp->bh_tstamp);
96447584Smccanne #else
96547584Smccanne 	hp->bh_tstamp = time;
96647584Smccanne #endif
96747584Smccanne #endif
96848932Smccanne 	hp->bh_datalen = pktlen;
96947584Smccanne 	hp->bh_hdrlen = hdrlen;
97047584Smccanne 	/*
97148932Smccanne 	 * Copy the packet data into the store buffer and update its length.
97247584Smccanne 	 */
97348932Smccanne 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
97448932Smccanne 	d->bd_slen = curlen + totlen;
97547584Smccanne }
97647584Smccanne 
97747584Smccanne /*
97847584Smccanne  * Initialize all nonzero fields of a descriptor.
97947584Smccanne  */
98047584Smccanne static int
98147584Smccanne bpf_initd(d)
98247584Smccanne 	register struct bpf_d *d;
98347584Smccanne {
98448932Smccanne 	d->bd_bufsize = bpf_bufsize;
98548932Smccanne 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
98648932Smccanne 	if (d->bd_fbuf == 0)
98748932Smccanne 		return (ENOBUFS);
98847584Smccanne 
98948932Smccanne 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
99048932Smccanne 	if (d->bd_sbuf == 0) {
99148932Smccanne 		free(d->bd_fbuf, M_DEVBUF);
99248932Smccanne 		return (ENOBUFS);
99347584Smccanne 	}
99448932Smccanne 	d->bd_slen = 0;
99548932Smccanne 	d->bd_hlen = 0;
99648932Smccanne 	return (0);
99747584Smccanne }
99847584Smccanne 
99947584Smccanne /*
100049202Smccanne  * Register 'ifp' with bpf.  XXX
100147584Smccanne  * and 'driverp' is a pointer to the 'struct bpf_if *' in the driver's softc.
100247584Smccanne  */
100347584Smccanne void
100449202Smccanne bpfattach(driverp, ifp, dlt, hdrlen)
100547584Smccanne 	caddr_t *driverp;
100647584Smccanne 	struct ifnet *ifp;
100749202Smccanne 	u_int dlt, hdrlen;
100847584Smccanne {
100947584Smccanne 	struct bpf_if *bp;
101047584Smccanne 	int i;
101147584Smccanne 
101249202Smccanne 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
101349202Smccanne 	if (bp == 0)
101449202Smccanne 		panic("bpfattach");
101547584Smccanne 
101647584Smccanne 	bp->bif_dlist = 0;
101747584Smccanne 	bp->bif_driverp = (struct bpf_if **)driverp;
101847584Smccanne 	bp->bif_ifp = ifp;
101949202Smccanne 	bp->bif_dlt = dlt;
102047584Smccanne 
102149202Smccanne 	bp->bif_next = bpf_iflist;
102249202Smccanne 	bpf_iflist = bp;
102349202Smccanne 
102448932Smccanne 	*bp->bif_driverp = 0;
102548932Smccanne 
102647584Smccanne 	/*
102747584Smccanne 	 * Compute the length of the bpf header.  This is not necessarily
102847584Smccanne 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
102947584Smccanne 	 * that the network layer header begins on a longword boundary (for
103047584Smccanne 	 * performance reasons and to alleviate alignment restrictions).
103147584Smccanne 	 */
103249202Smccanne 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
103347584Smccanne 
103447584Smccanne 	/*
103547584Smccanne 	 * Mark all the descriptors free if this hasn't been done.
103647584Smccanne 	 */
103747584Smccanne 	if (!D_ISFREE(&bpf_dtab[0]))
103847584Smccanne 		for (i = 0; i < NBPFILTER; ++i)
103947584Smccanne 			D_MARKFREE(&bpf_dtab[i]);
104047584Smccanne 
104147584Smccanne 	printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
104247584Smccanne }
104347584Smccanne 
104448967Smccanne /* XXX This routine belongs in net/if.c. */
104548932Smccanne /*
104648932Smccanne  * Set/clear promiscuous mode on interface ifp based on the truth value`
104748932Smccanne  * of pswitch.  The calls are reference counted so that only the first
104848932Smccanne  * on request actually has an effect, as does the final off request.
104948932Smccanne  * Results are undefined if the off and on requests are not matched.
105048932Smccanne  */
105148932Smccanne int
105248932Smccanne ifpromisc(ifp, pswitch)
105348932Smccanne 	struct ifnet *ifp;
105448932Smccanne 	int pswitch;
105548932Smccanne {
105649726Smccanne 	struct ifreq ifr;
105748932Smccanne 	/*
105848932Smccanne 	 * If the device is not configured up, we cannot put it in
105948932Smccanne 	 * promiscuous mode.
106048932Smccanne 	 */
106148932Smccanne 	if ((ifp->if_flags & IFF_UP) == 0)
106248932Smccanne 		return (ENETDOWN);
106348932Smccanne 
106448932Smccanne 	if (pswitch) {
106548932Smccanne 		if (ifp->if_pcount++ != 0)
106648932Smccanne 			return (0);
106748932Smccanne 		ifp->if_flags |= IFF_PROMISC;
106848932Smccanne 	} else {
106948932Smccanne 		if (--ifp->if_pcount > 0)
107048932Smccanne 			return (0);
107148932Smccanne 		ifp->if_flags &= ~IFF_PROMISC;
107248932Smccanne 	}
107349726Smccanne 	ifr.ifr_flags = ifp->if_flags;
107449726Smccanne 	return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
107548932Smccanne }
107648932Smccanne 
107747584Smccanne #endif (NBPFILTER > 0)
1078