bpf.c revision 171744
129088Smarkm/*-
229088Smarkm * Copyright (c) 1990, 1991, 1993
329088Smarkm *	The Regents of the University of California.  All rights reserved.
429088Smarkm *
529088Smarkm * This code is derived from the Stanford/CMU enet packet filter,
629088Smarkm * (net/enet.c) distributed as part of 4.3BSD, and code contributed
729088Smarkm * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
829088Smarkm * Berkeley Laboratory.
929088Smarkm *
1029088Smarkm * Redistribution and use in source and binary forms, with or without
1129088Smarkm * modification, are permitted provided that the following conditions
1229088Smarkm * are met:
1329088Smarkm * 1. Redistributions of source code must retain the above copyright
1429088Smarkm *    notice, this list of conditions and the following disclaimer.
1529088Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1629088Smarkm *    notice, this list of conditions and the following disclaimer in the
1729088Smarkm *    documentation and/or other materials provided with the distribution.
1829088Smarkm * 4. Neither the name of the University nor the names of its contributors
1929088Smarkm *    may be used to endorse or promote products derived from this software
2029088Smarkm *    without specific prior written permission.
2129088Smarkm *
2229088Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2329088Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2429088Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2529088Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2629088Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2729088Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2829088Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2929088Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3029088Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3129088Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3229088Smarkm * SUCH DAMAGE.
3329088Smarkm *
34114630Sobrien *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
3529088Smarkm *
3629181Smarkm * $FreeBSD: head/sys/net/bpf.c 171744 2007-08-06 14:26:03Z rwatson $
37114630Sobrien */
3831622Scharnier
39114630Sobrien#include "opt_bpf.h"
40114630Sobrien#include "opt_mac.h"
4129088Smarkm#include "opt_netgraph.h"
4229181Smarkm
4329181Smarkm#include <sys/types.h>
4441856Speter#include <sys/param.h>
4529181Smarkm#include <sys/systm.h>
4681965Smarkm#include <sys/conf.h>
4729088Smarkm#include <sys/fcntl.h>
4829088Smarkm#include <sys/malloc.h>
4929088Smarkm#include <sys/mbuf.h>
5087139Smarkm#include <sys/time.h>
5129181Smarkm#include <sys/priv.h>
5229181Smarkm#include <sys/proc.h>
5387139Smarkm#include <sys/signalvar.h>
5429181Smarkm#include <sys/filio.h>
5529181Smarkm#include <sys/sockio.h>
5629181Smarkm#include <sys/ttycom.h>
5729088Smarkm#include <sys/uio.h>
5829088Smarkm
5929088Smarkm#include <sys/event.h>
6029088Smarkm#include <sys/file.h>
6129088Smarkm#include <sys/poll.h>
6229088Smarkm#include <sys/proc.h>
6329088Smarkm
6429088Smarkm#include <sys/socket.h>
6529088Smarkm
6629088Smarkm#include <net/if.h>
6729088Smarkm#include <net/bpf.h>
6829088Smarkm#ifdef BPF_JITTER
6929088Smarkm#include <net/bpf_jitter.h>
7029088Smarkm#endif
7129088Smarkm#include <net/bpfdesc.h>
7229088Smarkm
7329088Smarkm#include <netinet/in.h>
7479981Sru#include <netinet/if_ether.h>
7580224Skris#include <sys/kernel.h>
7629088Smarkm#include <sys/sysctl.h>
7729088Smarkm
7829088Smarkm#include <net80211/ieee80211_freebsd.h>
7929088Smarkm
8031622Scharnier#include <security/mac/mac_framework.h>
8129088Smarkm
8229088Smarkmstatic MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
8331622Scharnier
8429088Smarkm#if defined(DEV_BPF) || defined(NETGRAPH_BPF)
8529088Smarkm
8679981Sru#define PRINET  26			/* interruptible */
8729088Smarkm
8829088Smarkm#define	M_SKIP_BPF	M_SKIP_FIREWALL
8929088Smarkm
9029088Smarkm/*
9129088Smarkm * bpf_iflist is a list of BPF interface structures, each corresponding to a
9229088Smarkm * specific DLT.  The same network interface might have several BPF interface
9329088Smarkm * structures registered by different layers in the stack (i.e., 802.11
9429088Smarkm * frames, ethernet frames, etc).
9529088Smarkm */
9629088Smarkmstatic LIST_HEAD(, bpf_if)	bpf_iflist;
9729088Smarkmstatic struct mtx	bpf_mtx;		/* bpf global lock */
9887139Smarkmstatic int		bpf_bpfd_cnt;
9987139Smarkm
10029088Smarkmstatic void	bpf_allocbufs(struct bpf_d *);
10187139Smarkmstatic void	bpf_attachd(struct bpf_d *, struct bpf_if *);
10229088Smarkmstatic void	bpf_detachd(struct bpf_d *);
10329088Smarkmstatic void	bpf_freed(struct bpf_d *);
10429088Smarkmstatic void	bpf_mcopy(const void *, void *, size_t);
10529088Smarkmstatic int	bpf_movein(struct uio *, int, int, struct mbuf **,
10629088Smarkm		    struct sockaddr *, int *, struct bpf_insn *);
10729088Smarkmstatic int	bpf_setif(struct bpf_d *, struct ifreq *);
10829181Smarkmstatic void	bpf_timed_out(void *);
10929088Smarkmstatic __inline void
11029088Smarkm		bpf_wakeup(struct bpf_d *);
11129088Smarkmstatic void	catchpacket(struct bpf_d *, u_char *, u_int,
11229088Smarkm		    u_int, void (*)(const void *, void *, size_t),
11329088Smarkm		    struct timeval *);
11429088Smarkmstatic void	reset_d(struct bpf_d *);
11529088Smarkmstatic int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
11629088Smarkmstatic int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
11729088Smarkmstatic int	bpf_setdlt(struct bpf_d *, u_int);
11829088Smarkmstatic void	filt_bpfdetach(struct knote *);
11929088Smarkmstatic int	filt_bpfread(struct knote *, long);
12029088Smarkmstatic void	bpf_drvinit(void *);
12129088Smarkmstatic void	bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
12287139Smarkmstatic int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
12387139Smarkm
12429088SmarkmSYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
12529088Smarkmstatic int bpf_bufsize = 4096;
12629088SmarkmSYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
12729088Smarkm    &bpf_bufsize, 0, "Default bpf buffer size");
12879981Srustatic int bpf_maxbufsize = BPF_MAXBUFSIZE;
12979981SruSYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
13029088Smarkm    &bpf_maxbufsize, 0, "Maximum bpf buffer size");
13129088Smarkmstatic int bpf_maxinsns = BPF_MAXINSNS;
13229088SmarkmSYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
13329088Smarkm    &bpf_maxinsns, 0, "Maximum bpf program instructions");
13429088SmarkmSYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
13529088Smarkm    bpf_stats_sysctl, "bpf statistics portal");
13629088Smarkm
13729088Smarkmstatic	d_open_t	bpfopen;
13829088Smarkmstatic	d_close_t	bpfclose;
13929088Smarkmstatic	d_read_t	bpfread;
14029088Smarkmstatic	d_write_t	bpfwrite;
14129088Smarkmstatic	d_ioctl_t	bpfioctl;
14229088Smarkmstatic	d_poll_t	bpfpoll;
14329088Smarkmstatic	d_kqfilter_t	bpfkqfilter;
14429088Smarkm
14529088Smarkmstatic struct cdevsw bpf_cdevsw = {
14629088Smarkm	.d_version =	D_VERSION,
14729088Smarkm	.d_open =	bpfopen,
14829088Smarkm	.d_close =	bpfclose,
14929088Smarkm	.d_read =	bpfread,
15029088Smarkm	.d_write =	bpfwrite,
15129088Smarkm	.d_ioctl =	bpfioctl,
15229088Smarkm	.d_poll =	bpfpoll,
15387139Smarkm	.d_name =	"bpf",
15487139Smarkm	.d_kqfilter =	bpfkqfilter,
15529088Smarkm};
15629088Smarkm
15729088Smarkmstatic struct filterops bpfread_filtops =
15829088Smarkm	{ 1, NULL, filt_bpfdetach, filt_bpfread };
15929088Smarkm
16029088Smarkmstatic int
16129088Smarkmbpf_movein(struct uio *uio, int linktype, int mtu, struct mbuf **mp,
16229088Smarkm    struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
16329088Smarkm{
16429088Smarkm	const struct ieee80211_bpf_params *p;
16529088Smarkm	struct mbuf *m;
16629088Smarkm	int error;
16787139Smarkm	int len;
16829088Smarkm	int hlen;
16929088Smarkm	int slen;
17029088Smarkm
17129088Smarkm	/*
17229088Smarkm	 * Build a sockaddr based on the data link layer type.
17329088Smarkm	 * We do this at this level because the ethernet header
17429088Smarkm	 * is copied directly into the data field of the sockaddr.
17529088Smarkm	 * In the case of SLIP, there is no header and the packet
17629088Smarkm	 * is forwarded as is.
17729088Smarkm	 * Also, we are careful to leave room at the front of the mbuf
17829088Smarkm	 * for the link level header.
17929088Smarkm	 */
18029088Smarkm	switch (linktype) {
18129088Smarkm
18229088Smarkm	case DLT_SLIP:
18329088Smarkm		sockp->sa_family = AF_INET;
18429088Smarkm		hlen = 0;
18529088Smarkm		break;
18629088Smarkm
18729088Smarkm	case DLT_EN10MB:
18829088Smarkm		sockp->sa_family = AF_UNSPEC;
18929088Smarkm		/* XXX Would MAXLINKHDR be better? */
19029088Smarkm		hlen = ETHER_HDR_LEN;
19129088Smarkm		break;
19229088Smarkm
19329088Smarkm	case DLT_FDDI:
19429088Smarkm		sockp->sa_family = AF_IMPLINK;
19529088Smarkm		hlen = 0;
19629088Smarkm		break;
19729088Smarkm
19887139Smarkm	case DLT_RAW:
19987139Smarkm		sockp->sa_family = AF_UNSPEC;
20029088Smarkm		hlen = 0;
20187139Smarkm		break;
20229088Smarkm
20329088Smarkm	case DLT_NULL:
20429088Smarkm		/*
20529088Smarkm		 * null interface types require a 4 byte pseudo header which
20629088Smarkm		 * corresponds to the address family of the packet.
20729088Smarkm		 */
20829088Smarkm		sockp->sa_family = AF_UNSPEC;
20929088Smarkm		hlen = 4;
21029088Smarkm		break;
21129088Smarkm
21229088Smarkm	case DLT_ATM_RFC1483:
21329088Smarkm		/*
21429088Smarkm		 * en atm driver requires 4-byte atm pseudo header.
21529088Smarkm		 * though it isn't standard, vpi:vci needs to be
21629088Smarkm		 * specified anyway.
21729088Smarkm		 */
21829088Smarkm		sockp->sa_family = AF_UNSPEC;
21929088Smarkm		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
22029088Smarkm		break;
22129088Smarkm
22229088Smarkm	case DLT_PPP:
22329088Smarkm		sockp->sa_family = AF_UNSPEC;
22429088Smarkm		hlen = 4;	/* This should match PPP_HDRLEN */
22529088Smarkm		break;
22629088Smarkm
22729088Smarkm	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
22829088Smarkm		sockp->sa_family = AF_IEEE80211;
22929088Smarkm		hlen = 0;
23029088Smarkm		break;
23129088Smarkm
23229088Smarkm	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
23329088Smarkm		sockp->sa_family = AF_IEEE80211;
23429088Smarkm		sockp->sa_len = 12;	/* XXX != 0 */
23529088Smarkm		hlen = sizeof(struct ieee80211_bpf_params);
23629088Smarkm		break;
23729088Smarkm
23829088Smarkm	default:
23929088Smarkm		return (EIO);
24029088Smarkm	}
24129088Smarkm
24229088Smarkm	len = uio->uio_resid;
24329088Smarkm
24429088Smarkm	if (len - hlen > mtu)
24529088Smarkm		return (EMSGSIZE);
24629088Smarkm
24729088Smarkm	if ((unsigned)len > MCLBYTES)
24829088Smarkm		return (EIO);
24929088Smarkm
25029088Smarkm	if (len > MHLEN) {
25187139Smarkm		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
25287139Smarkm	} else {
25329088Smarkm		MGETHDR(m, M_TRYWAIT, MT_DATA);
25429088Smarkm	}
25529088Smarkm	if (m == NULL)
25629088Smarkm		return (ENOBUFS);
25780224Skris	m->m_pkthdr.len = m->m_len = len;
25880224Skris	m->m_pkthdr.rcvif = NULL;
25980224Skris	*mp = m;
26079981Sru
26179981Sru	if (m->m_len < hlen) {
26279981Sru		error = EPERM;
26380224Skris		goto bad;
26429088Smarkm	}
26529088Smarkm
26629088Smarkm	error = uiomove(mtod(m, u_char *), len, uio);
26729088Smarkm	if (error)
26829088Smarkm		goto bad;
26929088Smarkm
27029088Smarkm	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
27129088Smarkm	if (slen == 0) {
27229088Smarkm		error = EPERM;
27329088Smarkm		goto bad;
27429088Smarkm	}
27529088Smarkm
27629088Smarkm	/*
27729088Smarkm	 * Make room for link header, and copy it to sockaddr
27829088Smarkm	 */
27929088Smarkm	if (hlen != 0) {
28029088Smarkm		if (sockp->sa_family == AF_IEEE80211) {
28129088Smarkm			/*
28229088Smarkm			 * Collect true length from the parameter header
28329088Smarkm			 * NB: sockp is known to be zero'd so if we do a
28429088Smarkm			 *     short copy unspecified parameters will be
28529088Smarkm			 *     zero.
28629088Smarkm			 * NB: packet may not be aligned after stripping
28729088Smarkm			 *     bpf params
28829088Smarkm			 * XXX check ibp_vers
28929088Smarkm			 */
29029088Smarkm			p = mtod(m, const struct ieee80211_bpf_params *);
29129088Smarkm			hlen = p->ibp_len;
29229088Smarkm			if (hlen > sizeof(sockp->sa_data)) {
29329088Smarkm				error = EINVAL;
29429088Smarkm				goto bad;
29529088Smarkm			}
29680224Skris		}
29780224Skris		bcopy(m->m_data, sockp->sa_data, hlen);
29880224Skris	}
29980224Skris	*hdrlen = hlen;
30080224Skris
30180224Skris	return (0);
30280224Skrisbad:
30329088Smarkm	m_freem(m);
30480224Skris	return (error);
30580224Skris}
30629088Smarkm
30780224Skris/*
30880224Skris * Attach file to the bpf interface, i.e. make d listen on bp.
30980224Skris */
31080224Skrisstatic void
31180224Skrisbpf_attachd(struct bpf_d *d, struct bpf_if *bp)
31229088Smarkm{
31380224Skris	/*
31429088Smarkm	 * Point d at bp, and add d to the interface's list of listeners.
31580224Skris	 * Finally, point the driver's bpf cookie at the interface so
31629088Smarkm	 * it will divert packets to bpf.
31729088Smarkm	 */
31829088Smarkm	BPFIF_LOCK(bp);
31929088Smarkm	d->bd_bif = bp;
32029088Smarkm	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
32129088Smarkm
32229088Smarkm	bpf_bpfd_cnt++;
32329088Smarkm	BPFIF_UNLOCK(bp);
32429088Smarkm}
32529088Smarkm
32687139Smarkm/*
32787139Smarkm * Detach a file from its interface.
32829088Smarkm */
32929088Smarkmstatic void
33029088Smarkmbpf_detachd(struct bpf_d *d)
33169384Sasmodai{
33229088Smarkm	int error;
33329088Smarkm	struct bpf_if *bp;
33429088Smarkm	struct ifnet *ifp;
33529088Smarkm
33629088Smarkm	bp = d->bd_bif;
33729088Smarkm	BPFIF_LOCK(bp);
33829088Smarkm	BPFD_LOCK(d);
33929088Smarkm	ifp = d->bd_bif->bif_ifp;
34029088Smarkm
34129088Smarkm	/*
34229088Smarkm	 * Remove d from the interface's descriptor list.
34329088Smarkm	 */
34429088Smarkm	LIST_REMOVE(d, bd_next);
34529088Smarkm
34629088Smarkm	bpf_bpfd_cnt--;
34787139Smarkm	d->bd_bif = NULL;
34887139Smarkm	BPFD_UNLOCK(d);
34929088Smarkm	BPFIF_UNLOCK(bp);
35087139Smarkm
35129088Smarkm	/*
35269384Sasmodai	 * Check if this descriptor had requested promiscuous mode.
35329088Smarkm	 * If so, turn it off.
35429088Smarkm	 */
35529088Smarkm	if (d->bd_promisc) {
35629088Smarkm		d->bd_promisc = 0;
35729088Smarkm		error = ifpromisc(ifp, 0);
35887139Smarkm		if (error != 0 && error != ENXIO) {
35987139Smarkm			/*
36029088Smarkm			 * ENXIO can happen if a pccard is unplugged
36187139Smarkm			 * Something is really wrong if we were able to put
36229088Smarkm			 * the driver into promiscuous mode, but can't
36329088Smarkm			 * take it out.
36487139Smarkm			 */
36529088Smarkm			if_printf(bp->bif_ifp,
36629088Smarkm				"bpf_detach: ifpromisc failed (%d)\n", error);
36729088Smarkm		}
36829088Smarkm	}
36929088Smarkm}
37029088Smarkm
37129088Smarkm/*
37229088Smarkm * Open ethernet device.  Returns ENXIO for illegal minor device number,
37329088Smarkm * EBUSY if file is open by another process.
37429088Smarkm */
37529088Smarkm/* ARGSUSED */
37629088Smarkmstatic	int
37729088Smarkmbpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
37829088Smarkm{
37929088Smarkm	struct bpf_d *d;
38029088Smarkm
38129088Smarkm	mtx_lock(&bpf_mtx);
38229088Smarkm	d = dev->si_drv1;
38329088Smarkm	/*
38429088Smarkm	 * Each minor can be opened by only one process.  If the requested
38529088Smarkm	 * minor is in use, return EBUSY.
38629088Smarkm	 */
38729088Smarkm	if (d != NULL) {
38829088Smarkm		mtx_unlock(&bpf_mtx);
38929088Smarkm		return (EBUSY);
39029088Smarkm	}
39129088Smarkm	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
39229088Smarkm	mtx_unlock(&bpf_mtx);
39329088Smarkm
39429088Smarkm	if ((dev->si_flags & SI_NAMED) == 0)
39529088Smarkm		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
39629088Smarkm		    "bpf%d", dev2unit(dev));
39729088Smarkm	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
39887139Smarkm	dev->si_drv1 = d;
39987139Smarkm	d->bd_bufsize = bpf_bufsize;
40029088Smarkm	d->bd_sig = SIGIO;
40129088Smarkm	d->bd_direction = BPF_D_INOUT;
40229088Smarkm	d->bd_pid = td->td_proc->p_pid;
40329088Smarkm#ifdef MAC
40429088Smarkm	mac_init_bpfdesc(d);
40529088Smarkm	mac_create_bpfdesc(td->td_ucred, d);
40687139Smarkm#endif
40787139Smarkm	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
40829088Smarkm	callout_init(&d->bd_callout, CALLOUT_MPSAFE);
40929088Smarkm	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
41029088Smarkm
41129088Smarkm	return (0);
41229181Smarkm}
41329181Smarkm
41429181Smarkm/*
41587139Smarkm * Close the descriptor by detaching it from its interface,
41629181Smarkm * deallocating its buffers, and marking it free.
41729088Smarkm */
41887139Smarkm/* ARGSUSED */
41987139Smarkmstatic	int
42029088Smarkmbpfclose(struct cdev *dev, int flags, int fmt, struct thread *td)
42129088Smarkm{
42229088Smarkm	struct bpf_d *d = dev->si_drv1;
42329088Smarkm
42441856Speter	BPFD_LOCK(d);
42541856Speter	if (d->bd_state == BPF_WAITING)
42629088Smarkm		callout_stop(&d->bd_callout);
42741856Speter	d->bd_state = BPF_IDLE;
42841856Speter	BPFD_UNLOCK(d);
42941856Speter	funsetown(&d->bd_sigio);
43041856Speter	mtx_lock(&bpf_mtx);
43129088Smarkm	if (d->bd_bif)
43229088Smarkm		bpf_detachd(d);
43329088Smarkm	mtx_unlock(&bpf_mtx);
43441856Speter	selwakeuppri(&d->bd_sel, PRINET);
43541856Speter#ifdef MAC
43641856Speter	mac_destroy_bpfdesc(d);
43741856Speter#endif /* MAC */
43841856Speter	knlist_destroy(&d->bd_sel.si_note);
43929088Smarkm	bpf_freed(d);
44029088Smarkm	dev->si_drv1 = NULL;
44129088Smarkm	free(d, M_BPF);
44229088Smarkm
44329088Smarkm	return (0);
44429088Smarkm}
44529088Smarkm
44629088Smarkm
44729088Smarkm/*
44829088Smarkm * Rotate the packet buffers in descriptor d.  Move the store buffer
44929088Smarkm * into the hold slot, and the free buffer into the store slot.
45029088Smarkm * Zero the length of the new store buffer.
45129088Smarkm */
45229088Smarkm#define ROTATE_BUFFERS(d) \
45329088Smarkm	(d)->bd_hbuf = (d)->bd_sbuf; \
45429088Smarkm	(d)->bd_hlen = (d)->bd_slen; \
45529088Smarkm	(d)->bd_sbuf = (d)->bd_fbuf; \
45629088Smarkm	(d)->bd_slen = 0; \
45729088Smarkm	(d)->bd_fbuf = NULL;
45829088Smarkm/*
45929088Smarkm *  bpfread - read next chunk of packets from buffers
46029088Smarkm */
46129088Smarkmstatic	int
46229181Smarkmbpfread(struct cdev *dev, struct uio *uio, int ioflag)
46329181Smarkm{
46429181Smarkm	struct bpf_d *d = dev->si_drv1;
46529088Smarkm	int timed_out;
46629088Smarkm	int error;
46729088Smarkm
46829088Smarkm	/*
46929088Smarkm	 * Restrict application to use a buffer the same size as
47041856Speter	 * as kernel buffers.
47141856Speter	 */
47241856Speter	if (uio->uio_resid != d->bd_bufsize)
47341856Speter		return (EINVAL);
47441856Speter
47541856Speter	BPFD_LOCK(d);
47641856Speter	if (d->bd_state == BPF_WAITING)
47741856Speter		callout_stop(&d->bd_callout);
47841856Speter	timed_out = (d->bd_state == BPF_TIMED_OUT);
47941856Speter	d->bd_state = BPF_IDLE;
48041856Speter	/*
48141856Speter	 * If the hold buffer is empty, then do a timed sleep, which
48241856Speter	 * ends when the timeout expires or when enough packets
48341856Speter	 * have arrived to fill the store buffer.
48441856Speter	 */
48541856Speter	while (d->bd_hbuf == NULL) {
48641856Speter		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
48741856Speter			/*
48829088Smarkm			 * A packet(s) either arrived since the previous
48929088Smarkm			 * read or arrived while we were asleep.
49029088Smarkm			 * Rotate the buffers and return what's here.
49129088Smarkm			 */
49229088Smarkm			ROTATE_BUFFERS(d);
49329088Smarkm			break;
49429088Smarkm		}
49529088Smarkm
49629088Smarkm		/*
49729088Smarkm		 * No data is available, check to see if the bpf device
49829088Smarkm		 * is still pointed at a real interface.  If not, return
49929088Smarkm		 * ENXIO so that the userland process knows to rebind
50087139Smarkm		 * it before using it again.
50187139Smarkm		 */
50229088Smarkm		if (d->bd_bif == NULL) {
50329088Smarkm			BPFD_UNLOCK(d);
50479981Sru			return (ENXIO);
50529088Smarkm		}
50679981Sru
50729088Smarkm		if (ioflag & O_NONBLOCK) {
50879981Sru			BPFD_UNLOCK(d);
50929088Smarkm			return (EWOULDBLOCK);
51029088Smarkm		}
51129088Smarkm		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
51287139Smarkm		     "bpf", d->bd_rtout);
51387139Smarkm		if (error == EINTR || error == ERESTART) {
51429088Smarkm			BPFD_UNLOCK(d);
51587139Smarkm			return (error);
51629088Smarkm		}
51729088Smarkm		if (error == EWOULDBLOCK) {
51829088Smarkm			/*
51929088Smarkm			 * On a timeout, return what's in the buffer,
52029088Smarkm			 * which may be nothing.  If there is something
52179981Sru			 * in the store buffer, we can rotate the buffers.
52229088Smarkm			 */
52329088Smarkm			if (d->bd_hbuf)
52487139Smarkm				/*
52529088Smarkm				 * We filled up the buffer in between
52629088Smarkm				 * getting the timeout and arriving
52729088Smarkm				 * here, so we don't need to rotate.
52829088Smarkm				 */
52929088Smarkm				break;
53079981Sru
53129088Smarkm			if (d->bd_slen == 0) {
53279981Sru				BPFD_UNLOCK(d);
53329088Smarkm				return (0);
53479981Sru			}
53529088Smarkm			ROTATE_BUFFERS(d);
53679981Sru			break;
53729088Smarkm		}
53879981Sru	}
53929088Smarkm	/*
54079981Sru	 * At this point, we know we have something in the hold slot.
54129088Smarkm	 */
54279981Sru	BPFD_UNLOCK(d);
54379981Sru
54429088Smarkm	/*
54529088Smarkm	 * Move data from hold buffer into user space.
54629088Smarkm	 * We know the entire buffer is transferred since
54729088Smarkm	 * we checked above that the read buffer is bpf_bufsize bytes.
54829088Smarkm	 */
54979981Sru	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
55029088Smarkm
55129088Smarkm	BPFD_LOCK(d);
55229088Smarkm	d->bd_fbuf = d->bd_hbuf;
55329088Smarkm	d->bd_hbuf = NULL;
55479981Sru	d->bd_hlen = 0;
55529088Smarkm	BPFD_UNLOCK(d);
55629088Smarkm
55779981Sru	return (error);
55829088Smarkm}
55929088Smarkm
56079981Sru
56129088Smarkm/*
56229088Smarkm * If there are processes sleeping on this descriptor, wake them up.
56379981Sru */
56429088Smarkmstatic __inline void
56529088Smarkmbpf_wakeup(struct bpf_d *d)
56629088Smarkm{
56729088Smarkm
56829088Smarkm	BPFD_LOCK_ASSERT(d);
56979981Sru	if (d->bd_state == BPF_WAITING) {
57029088Smarkm		callout_stop(&d->bd_callout);
57179981Sru		d->bd_state = BPF_IDLE;
57229088Smarkm	}
57329088Smarkm	wakeup(d);
57429088Smarkm	if (d->bd_async && d->bd_sig && d->bd_sigio)
57529088Smarkm		pgsigio(&d->bd_sigio, d->bd_sig, 0);
57679981Sru
57729088Smarkm	selwakeuppri(&d->bd_sel, PRINET);
57829088Smarkm	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
57929088Smarkm}
58079981Sru
58129088Smarkmstatic void
58279981Srubpf_timed_out(void *arg)
58329088Smarkm{
58479981Sru	struct bpf_d *d = (struct bpf_d *)arg;
58529088Smarkm
58629088Smarkm	BPFD_LOCK(d);
58729088Smarkm	if (d->bd_state == BPF_WAITING) {
58829088Smarkm		d->bd_state = BPF_TIMED_OUT;
58929088Smarkm		if (d->bd_slen != 0)
59029088Smarkm			bpf_wakeup(d);
59179981Sru	}
59229088Smarkm	BPFD_UNLOCK(d);
59379981Sru}
59429088Smarkm
59529088Smarkmstatic int
59629088Smarkmbpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
59729088Smarkm{
59879981Sru	struct bpf_d *d = dev->si_drv1;
59929088Smarkm	struct ifnet *ifp;
60079981Sru	struct mbuf *m, *mc;
60129088Smarkm	struct sockaddr dst;
60279981Sru	int error, hlen;
60329088Smarkm
60479981Sru	if (d->bd_bif == NULL)
60529088Smarkm		return (ENXIO);
60679981Sru
60729088Smarkm	ifp = d->bd_bif->bif_ifp;
60829088Smarkm
60979981Sru	if ((ifp->if_flags & IFF_UP) == 0)
61029088Smarkm		return (ENETDOWN);
61129088Smarkm
61229088Smarkm	if (uio->uio_resid == 0)
61329088Smarkm		return (0);
61479981Sru
61529088Smarkm	bzero(&dst, sizeof(dst));
61679981Sru	m = NULL;
61729088Smarkm	hlen = 0;
61829088Smarkm	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu,
61929088Smarkm	    &m, &dst, &hlen, d->bd_wfilter);
62079981Sru	if (error)
62129088Smarkm		return (error);
62229088Smarkm
62379981Sru	if (d->bd_hdrcmplt)
62429088Smarkm		dst.sa_family = pseudo_AF_HDRCMPLT;
62529088Smarkm
62629088Smarkm	if (d->bd_feedback) {
62779981Sru		mc = m_dup(m, M_DONTWAIT);
62829088Smarkm		if (mc != NULL)
62929088Smarkm			mc->m_pkthdr.rcvif = ifp;
63079981Sru		/* XXX Do not return the same packet twice. */
63129088Smarkm		if (d->bd_direction == BPF_D_INOUT)
63229088Smarkm			m->m_flags |= M_SKIP_BPF;
63329088Smarkm	} else
63479981Sru		mc = NULL;
63529088Smarkm
63629088Smarkm	m->m_pkthdr.len -= hlen;
63729088Smarkm	m->m_len -= hlen;
63829088Smarkm	m->m_data += hlen;	/* XXX */
63979981Sru
64029088Smarkm#ifdef MAC
64179981Sru	BPFD_LOCK(d);
64229088Smarkm	mac_create_mbuf_from_bpfdesc(d, m);
64329088Smarkm	if (mc != NULL)
64429088Smarkm		mac_create_mbuf_from_bpfdesc(d, mc);
64529088Smarkm	BPFD_UNLOCK(d);
64679981Sru#endif
64729088Smarkm
64829088Smarkm	error = (*ifp->if_output)(ifp, m, &dst, NULL);
64979981Sru
65029088Smarkm	if (mc != NULL) {
65129088Smarkm		if (error == 0)
65279981Sru			(*ifp->if_input)(ifp, mc);
65329088Smarkm		else
65429088Smarkm			m_freem(mc);
65579981Sru	}
65629088Smarkm
65729088Smarkm	return (error);
65879981Sru}
65929088Smarkm
66029088Smarkm/*
66129088Smarkm * Reset a descriptor by flushing its packet buffer and clearing the
66229088Smarkm * receive and drop counts.
66379981Sru */
66429088Smarkmstatic void
66579981Srureset_d(struct bpf_d *d)
66629088Smarkm{
66729088Smarkm
66829088Smarkm	mtx_assert(&d->bd_mtx, MA_OWNED);
66979981Sru	if (d->bd_hbuf) {
67029088Smarkm		/* Free the hold buffer. */
67179981Sru		d->bd_fbuf = d->bd_hbuf;
67229088Smarkm		d->bd_hbuf = NULL;
67329088Smarkm	}
67429088Smarkm	d->bd_slen = 0;
67529088Smarkm	d->bd_hlen = 0;
67629088Smarkm	d->bd_rcount = 0;
67729088Smarkm	d->bd_dcount = 0;
67879981Sru	d->bd_fcount = 0;
67929088Smarkm}
68029088Smarkm
68179981Sru/*
68229088Smarkm *  FIONREAD		Check for read packet available.
68379981Sru *  SIOCGIFADDR		Get interface address - convenient hook to driver.
68429088Smarkm *  BIOCGBLEN		Get buffer len [for read()].
68529088Smarkm *  BIOCSETF		Set ethernet read filter.
68679981Sru *  BIOCSETWF		Set ethernet write filter.
68729088Smarkm *  BIOCFLUSH		Flush read packet buffer.
68879981Sru *  BIOCPROMISC		Put interface into promiscuous mode.
68929088Smarkm *  BIOCGDLT		Get link layer type.
69079981Sru *  BIOCGETIF		Get interface name.
69129088Smarkm *  BIOCSETIF		Set interface.
69279981Sru *  BIOCSRTIMEOUT	Set read timeout.
69329088Smarkm *  BIOCGRTIMEOUT	Get read timeout.
69479981Sru *  BIOCGSTATS		Get packet stats.
69529088Smarkm *  BIOCIMMEDIATE	Set immediate mode.
69629088Smarkm *  BIOCVERSION		Get filter language version.
69729088Smarkm *  BIOCGHDRCMPLT	Get "header already complete" flag
69829088Smarkm *  BIOCSHDRCMPLT	Set "header already complete" flag
69929088Smarkm *  BIOCGDIRECTION	Get packet direction flag
70079981Sru *  BIOCSDIRECTION	Set packet direction flag
70129088Smarkm *  BIOCLOCK		Set "locked" flag
70279981Sru *  BIOCFEEDBACK	Set packet feedback mode.
70329088Smarkm */
70429088Smarkm/* ARGSUSED */
70529088Smarkmstatic	int
70629088Smarkmbpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
70729088Smarkm    struct thread *td)
70879981Sru{
70929088Smarkm	struct bpf_d *d = dev->si_drv1;
71029088Smarkm	int error = 0;
71129088Smarkm
71229088Smarkm	/*
71379981Sru	 * Refresh PID associated with this descriptor.
71429088Smarkm	 */
71579981Sru	BPFD_LOCK(d);
71629088Smarkm	d->bd_pid = td->td_proc->p_pid;
71729088Smarkm	if (d->bd_state == BPF_WAITING)
71829088Smarkm		callout_stop(&d->bd_callout);
71929088Smarkm	d->bd_state = BPF_IDLE;
72029088Smarkm	BPFD_UNLOCK(d);
72129088Smarkm
72229088Smarkm	if (d->bd_locked == 1) {
72329088Smarkm		switch (cmd) {
72429088Smarkm		case BIOCGBLEN:
72529088Smarkm		case BIOCFLUSH:
72679981Sru		case BIOCGDLT:
72729088Smarkm		case BIOCGDLTLIST:
72829088Smarkm		case BIOCGETIF:
72979981Sru		case BIOCGRTIMEOUT:
73029088Smarkm		case BIOCGSTATS:
73129088Smarkm		case BIOCVERSION:
73279981Sru		case BIOCGRSIG:
73329088Smarkm		case BIOCGHDRCMPLT:
73429088Smarkm		case BIOCFEEDBACK:
73529088Smarkm		case FIONREAD:
73679981Sru		case BIOCLOCK:
73729088Smarkm		case BIOCSRTIMEOUT:
73879981Sru		case BIOCIMMEDIATE:
73929088Smarkm		case TIOCGPGRP:
74029088Smarkm			break;
74129088Smarkm		default:
74229088Smarkm			return (EPERM);
74329088Smarkm		}
74487139Smarkm	}
74587139Smarkm	switch (cmd) {
74629088Smarkm
74779981Sru	default:
74829088Smarkm		error = EINVAL;
74929088Smarkm		break;
75029088Smarkm
75129088Smarkm	/*
75279981Sru	 * Check for read packet available.
75329088Smarkm	 */
75479981Sru	case FIONREAD:
75529088Smarkm		{
75679981Sru			int n;
75729088Smarkm
75829088Smarkm			BPFD_LOCK(d);
75929088Smarkm			n = d->bd_slen;
76079981Sru			if (d->bd_hbuf)
76129088Smarkm				n += d->bd_hlen;
76229088Smarkm			BPFD_UNLOCK(d);
76329088Smarkm
76429088Smarkm			*(int *)addr = n;
76529088Smarkm			break;
76629088Smarkm		}
76729088Smarkm
76829088Smarkm	case SIOCGIFADDR:
76929088Smarkm		{
77029088Smarkm			struct ifnet *ifp;
77179981Sru
77229088Smarkm			if (d->bd_bif == NULL)
77379981Sru				error = EINVAL;
77429088Smarkm			else {
77579981Sru				ifp = d->bd_bif->bif_ifp;
77629088Smarkm				error = (*ifp->if_ioctl)(ifp, cmd, addr);
77729088Smarkm			}
77829088Smarkm			break;
77979981Sru		}
78029088Smarkm
78129088Smarkm	/*
78229088Smarkm	 * Get buffer len [for read()].
78329088Smarkm	 */
78429088Smarkm	case BIOCGBLEN:
78529088Smarkm		*(u_int *)addr = d->bd_bufsize;
78629088Smarkm		break;
78729088Smarkm
78829088Smarkm	/*
78929088Smarkm	 * Set buffer length.
79029088Smarkm	 */
79129088Smarkm	case BIOCSBLEN:
79229088Smarkm		if (d->bd_bif != NULL)
79329088Smarkm			error = EINVAL;
79429088Smarkm		else {
79579981Sru			u_int size = *(u_int *)addr;
79629088Smarkm
79729088Smarkm			if (size > bpf_maxbufsize)
79829088Smarkm				*(u_int *)addr = size = bpf_maxbufsize;
79929088Smarkm			else if (size < BPF_MINBUFSIZE)
80079981Sru				*(u_int *)addr = size = BPF_MINBUFSIZE;
80129088Smarkm			d->bd_bufsize = size;
80229088Smarkm		}
80329088Smarkm		break;
80429088Smarkm
80579981Sru	/*
80629088Smarkm	 * Set link layer read filter.
80729088Smarkm	 */
80829088Smarkm	case BIOCSETF:
80929088Smarkm	case BIOCSETWF:
81029088Smarkm		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
81129088Smarkm		break;
81229088Smarkm
81329088Smarkm	/*
81429088Smarkm	 * Flush read packet buffer.
81579981Sru	 */
81629088Smarkm	case BIOCFLUSH:
81729088Smarkm		BPFD_LOCK(d);
81879981Sru		reset_d(d);
81929088Smarkm		BPFD_UNLOCK(d);
82029088Smarkm		break;
82179981Sru
82229088Smarkm	/*
82329088Smarkm	 * Put interface into promiscuous mode.
82479981Sru	 */
82529088Smarkm	case BIOCPROMISC:
82629088Smarkm		if (d->bd_bif == NULL) {
82729088Smarkm			/*
82829088Smarkm			 * No interface attached yet.
82929088Smarkm			 */
83079981Sru			error = EINVAL;
83129088Smarkm			break;
83229088Smarkm		}
83379981Sru		if (d->bd_promisc == 0) {
83429088Smarkm			error = ifpromisc(d->bd_bif->bif_ifp, 1);
83529088Smarkm			if (error == 0)
83629088Smarkm				d->bd_promisc = 1;
83779981Sru		}
83829088Smarkm		break;
83929088Smarkm
84079981Sru	/*
84129088Smarkm	 * Get current data link type.
84229088Smarkm	 */
84379981Sru	case BIOCGDLT:
84429088Smarkm		if (d->bd_bif == NULL)
84529088Smarkm			error = EINVAL;
84687139Smarkm		else
84729088Smarkm			*(u_int *)addr = d->bd_bif->bif_dlt;
84829088Smarkm		break;
84929088Smarkm
850228589Sdim	/*
85129088Smarkm	 * Get a list of supported data link types.
85229088Smarkm	 */
85329088Smarkm	case BIOCGDLTLIST:
85429088Smarkm		if (d->bd_bif == NULL)
855228589Sdim			error = EINVAL;
85629088Smarkm		else
85729088Smarkm			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
85829088Smarkm		break;
85929088Smarkm
860228589Sdim	/*
86129088Smarkm	 * Set data link type.
86229088Smarkm	 */
86329088Smarkm	case BIOCSDLT:
86429088Smarkm		if (d->bd_bif == NULL)
865228589Sdim			error = EINVAL;
86629088Smarkm		else
86729088Smarkm			error = bpf_setdlt(d, *(u_int *)addr);
86829088Smarkm		break;
86929088Smarkm
87029088Smarkm	/*
87129088Smarkm	 * Get interface name.
87279981Sru	 */
87329088Smarkm	case BIOCGETIF:
87429088Smarkm		if (d->bd_bif == NULL)
87579981Sru			error = EINVAL;
87629088Smarkm		else {
87779981Sru			struct ifnet *const ifp = d->bd_bif->bif_ifp;
87829088Smarkm			struct ifreq *const ifr = (struct ifreq *)addr;
87929088Smarkm
88029088Smarkm			strlcpy(ifr->ifr_name, ifp->if_xname,
88129088Smarkm			    sizeof(ifr->ifr_name));
88229088Smarkm		}
88329088Smarkm		break;
88429088Smarkm
88579981Sru	/*
88629088Smarkm	 * Set interface.
88729088Smarkm	 */
88829088Smarkm	case BIOCSETIF:
88929088Smarkm		error = bpf_setif(d, (struct ifreq *)addr);
89029088Smarkm		break;
89187139Smarkm
89229088Smarkm	/*
89379981Sru	 * Set read timeout.
89429088Smarkm	 */
89529088Smarkm	case BIOCSRTIMEOUT:
89679981Sru		{
89729088Smarkm			struct timeval *tv = (struct timeval *)addr;
89829088Smarkm
89929088Smarkm			/*
90029088Smarkm			 * Subtract 1 tick from tvtohz() since this isn't
90129088Smarkm			 * a one-shot timer.
90279981Sru			 */
90329088Smarkm			if ((error = itimerfix(tv)) == 0)
90429088Smarkm				d->bd_rtout = tvtohz(tv) - 1;
90579981Sru			break;
90629088Smarkm		}
90779981Sru
90829088Smarkm	/*
90979981Sru	 * Get read timeout.
91029088Smarkm	 */
91129088Smarkm	case BIOCGRTIMEOUT:
91279981Sru		{
91329088Smarkm			struct timeval *tv = (struct timeval *)addr;
91429088Smarkm
91529088Smarkm			tv->tv_sec = d->bd_rtout / hz;
91629088Smarkm			tv->tv_usec = (d->bd_rtout % hz) * tick;
91729088Smarkm			break;
91831622Scharnier		}
91931622Scharnier
92031622Scharnier	/*
92179981Sru	 * Get packet stats.
92231622Scharnier	 */
92329088Smarkm	case BIOCGSTATS:
92429088Smarkm		{
92529088Smarkm			struct bpf_stat *bs = (struct bpf_stat *)addr;
92629088Smarkm
92779981Sru			bs->bs_recv = d->bd_rcount;
92829088Smarkm			bs->bs_drop = d->bd_dcount;
92929088Smarkm			break;
93079981Sru		}
93129088Smarkm
93279981Sru	/*
93329088Smarkm	 * Set immediate mode.
93479981Sru	 */
93529088Smarkm	case BIOCIMMEDIATE:
93629088Smarkm		d->bd_immediate = *(u_int *)addr;
93779981Sru		break;
93829088Smarkm
93929088Smarkm	case BIOCVERSION:
94029088Smarkm		{
94129088Smarkm			struct bpf_version *bv = (struct bpf_version *)addr;
94229088Smarkm
94329088Smarkm			bv->bv_major = BPF_MAJOR_VERSION;
94429088Smarkm			bv->bv_minor = BPF_MINOR_VERSION;
94529088Smarkm			break;
94629088Smarkm		}
94779981Sru
94829088Smarkm	/*
94929088Smarkm	 * Get "header already complete" flag
95029088Smarkm	 */
95129088Smarkm	case BIOCGHDRCMPLT:
95279981Sru		*(u_int *)addr = d->bd_hdrcmplt;
95329088Smarkm		break;
95429088Smarkm
95529088Smarkm	/*
95629088Smarkm	 * Set "header already complete" flag
95729088Smarkm	 */
95829088Smarkm	case BIOCSHDRCMPLT:
95929088Smarkm		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
96029088Smarkm		break;
96179981Sru
96229088Smarkm	/*
96379981Sru	 * Get packet direction flag
96429088Smarkm	 */
96529088Smarkm	case BIOCGDIRECTION:
96629088Smarkm		*(u_int *)addr = d->bd_direction;
96729088Smarkm		break;
96879981Sru
96929088Smarkm	/*
97029088Smarkm	 * Set packet direction flag
97129088Smarkm	 */
97279981Sru	case BIOCSDIRECTION:
97329088Smarkm		{
97429088Smarkm			u_int	direction;
97529088Smarkm
97679981Sru			direction = *(u_int *)addr;
97729088Smarkm			switch (direction) {
97829088Smarkm			case BPF_D_IN:
97929088Smarkm			case BPF_D_INOUT:
98079981Sru			case BPF_D_OUT:
98129088Smarkm				d->bd_direction = direction;
98229088Smarkm				break;
98329088Smarkm			default:
98429088Smarkm				error = EINVAL;
98579981Sru			}
98629088Smarkm		}
98729088Smarkm		break;
98879981Sru
98929088Smarkm	case BIOCFEEDBACK:
99029088Smarkm		d->bd_feedback = *(u_int *)addr;
99129088Smarkm		break;
99279981Sru
99329088Smarkm	case BIOCLOCK:
99479981Sru		d->bd_locked = 1;
99529088Smarkm		break;
99631622Scharnier
99731622Scharnier	case FIONBIO:		/* Non-blocking I/O */
99831622Scharnier		break;
99979981Sru
100031622Scharnier	case FIOASYNC:		/* Send signal on receive packets */
100129088Smarkm		d->bd_async = *(int *)addr;
100229088Smarkm		break;
100329088Smarkm
100429088Smarkm	case FIOSETOWN:
100579981Sru		error = fsetown(*(int *)addr, &d->bd_sigio);
100629088Smarkm		break;
100729088Smarkm
100879981Sru	case FIOGETOWN:
100929088Smarkm		*(int *)addr = fgetown(&d->bd_sigio);
101079981Sru		break;
101129088Smarkm
101229088Smarkm	/* This is deprecated, FIOSETOWN should be used instead. */
101329088Smarkm	case TIOCSPGRP:
101429088Smarkm		error = fsetown(-(*(int *)addr), &d->bd_sigio);
101529088Smarkm		break;
101679981Sru
101729088Smarkm	/* This is deprecated, FIOGETOWN should be used instead. */
101829088Smarkm	case TIOCGPGRP:
101929088Smarkm		*(int *)addr = -fgetown(&d->bd_sigio);
102079981Sru		break;
102129088Smarkm
102229088Smarkm	case BIOCSRSIG:		/* Set receive signal */
102329088Smarkm		{
102479981Sru			u_int sig;
102529088Smarkm
102629088Smarkm			sig = *(u_int *)addr;
102779981Sru
102829088Smarkm			if (sig >= NSIG)
102929088Smarkm				error = EINVAL;
103029088Smarkm			else
103129088Smarkm				d->bd_sig = sig;
103229088Smarkm			break;
103329088Smarkm		}
103429088Smarkm	case BIOCGRSIG:
103529088Smarkm		*(u_int *)addr = d->bd_sig;
103679981Sru		break;
103729088Smarkm	}
103879981Sru	return (error);
103929088Smarkm}
104079981Sru
104129088Smarkm/*
104229088Smarkm * Set d's packet filter program to fp.  If this file already has a filter,
104329088Smarkm * free it and replace it.  Returns EINVAL for bogus requests.
104479981Sru */
104529088Smarkmstatic int
104629088Smarkmbpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
104729088Smarkm{
104829088Smarkm	struct bpf_insn *fcode, *old;
104929088Smarkm	u_int wfilter, flen, size;
105087139Smarkm#ifdef BPF_JITTER
105187139Smarkm	bpf_jit_filter *ofunc;
105229088Smarkm#endif
105387139Smarkm
105429088Smarkm	if (cmd == BIOCSETWF) {
105529088Smarkm		old = d->bd_wfilter;
105629088Smarkm		wfilter = 1;
105729088Smarkm#ifdef BPF_JITTER
105829088Smarkm		ofunc = NULL;
105929088Smarkm#endif
106029088Smarkm	} else {
106129088Smarkm		wfilter = 0;
106229088Smarkm		old = d->bd_rfilter;
106379981Sru#ifdef BPF_JITTER
106429088Smarkm		ofunc = d->bd_bfilter;
106579981Sru#endif
106629088Smarkm	}
106729088Smarkm	if (fp->bf_insns == NULL) {
106829088Smarkm		if (fp->bf_len != 0)
106929088Smarkm			return (EINVAL);
107029088Smarkm		BPFD_LOCK(d);
107129088Smarkm		if (wfilter)
107279981Sru			d->bd_wfilter = NULL;
107329088Smarkm		else {
107429088Smarkm			d->bd_rfilter = NULL;
107529088Smarkm#ifdef BPF_JITTER
107629088Smarkm			d->bd_bfilter = NULL;
107729088Smarkm#endif
107879981Sru		}
107929088Smarkm		reset_d(d);
108029088Smarkm		BPFD_UNLOCK(d);
108129088Smarkm		if (old != NULL)
1082			free((caddr_t)old, M_BPF);
1083#ifdef BPF_JITTER
1084		if (ofunc != NULL)
1085			bpf_destroy_jit_filter(ofunc);
1086#endif
1087		return (0);
1088	}
1089	flen = fp->bf_len;
1090	if (flen > bpf_maxinsns)
1091		return (EINVAL);
1092
1093	size = flen * sizeof(*fp->bf_insns);
1094	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1095	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1096	    bpf_validate(fcode, (int)flen)) {
1097		BPFD_LOCK(d);
1098		if (wfilter)
1099			d->bd_wfilter = fcode;
1100		else {
1101			d->bd_rfilter = fcode;
1102#ifdef BPF_JITTER
1103			d->bd_bfilter = bpf_jitter(fcode, flen);
1104#endif
1105		}
1106		reset_d(d);
1107		BPFD_UNLOCK(d);
1108		if (old != NULL)
1109			free((caddr_t)old, M_BPF);
1110#ifdef BPF_JITTER
1111		if (ofunc != NULL)
1112			bpf_destroy_jit_filter(ofunc);
1113#endif
1114
1115		return (0);
1116	}
1117	free((caddr_t)fcode, M_BPF);
1118	return (EINVAL);
1119}
1120
1121/*
1122 * Detach a file from its current interface (if attached at all) and attach
1123 * to the interface indicated by the name stored in ifr.
1124 * Return an errno or 0.
1125 */
1126static int
1127bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1128{
1129	struct bpf_if *bp;
1130	struct ifnet *theywant;
1131
1132	theywant = ifunit(ifr->ifr_name);
1133	if (theywant == NULL || theywant->if_bpf == NULL)
1134		return (ENXIO);
1135
1136	bp = theywant->if_bpf;
1137	/*
1138	 * Allocate the packet buffers if we need to.
1139	 * If we're already attached to requested interface,
1140	 * just flush the buffer.
1141	 */
1142	if (d->bd_sbuf == NULL)
1143		bpf_allocbufs(d);
1144	if (bp != d->bd_bif) {
1145		if (d->bd_bif)
1146			/*
1147			 * Detach if attached to something else.
1148			 */
1149			bpf_detachd(d);
1150
1151		bpf_attachd(d, bp);
1152	}
1153	BPFD_LOCK(d);
1154	reset_d(d);
1155	BPFD_UNLOCK(d);
1156	return (0);
1157}
1158
1159/*
1160 * Support for select() and poll() system calls
1161 *
1162 * Return true iff the specific operation will not block indefinitely.
1163 * Otherwise, return false but make a note that a selwakeup() must be done.
1164 */
1165static int
1166bpfpoll(struct cdev *dev, int events, struct thread *td)
1167{
1168	struct bpf_d *d;
1169	int revents;
1170
1171	d = dev->si_drv1;
1172	if (d->bd_bif == NULL)
1173		return (ENXIO);
1174
1175	/*
1176	 * Refresh PID associated with this descriptor.
1177	 */
1178	revents = events & (POLLOUT | POLLWRNORM);
1179	BPFD_LOCK(d);
1180	d->bd_pid = td->td_proc->p_pid;
1181	if (events & (POLLIN | POLLRDNORM)) {
1182		if (bpf_ready(d))
1183			revents |= events & (POLLIN | POLLRDNORM);
1184		else {
1185			selrecord(td, &d->bd_sel);
1186			/* Start the read timeout if necessary. */
1187			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1188				callout_reset(&d->bd_callout, d->bd_rtout,
1189				    bpf_timed_out, d);
1190				d->bd_state = BPF_WAITING;
1191			}
1192		}
1193	}
1194	BPFD_UNLOCK(d);
1195	return (revents);
1196}
1197
1198/*
1199 * Support for kevent() system call.  Register EVFILT_READ filters and
1200 * reject all others.
1201 */
1202int
1203bpfkqfilter(struct cdev *dev, struct knote *kn)
1204{
1205	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1206
1207	if (kn->kn_filter != EVFILT_READ)
1208		return (1);
1209
1210	/*
1211	 * Refresh PID associated with this descriptor.
1212	 */
1213	BPFD_LOCK(d);
1214	d->bd_pid = curthread->td_proc->p_pid;
1215	kn->kn_fop = &bpfread_filtops;
1216	kn->kn_hook = d;
1217	knlist_add(&d->bd_sel.si_note, kn, 1);
1218	BPFD_UNLOCK(d);
1219
1220	return (0);
1221}
1222
1223static void
1224filt_bpfdetach(struct knote *kn)
1225{
1226	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1227
1228	knlist_remove(&d->bd_sel.si_note, kn, 0);
1229}
1230
1231static int
1232filt_bpfread(struct knote *kn, long hint)
1233{
1234	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1235	int ready;
1236
1237	BPFD_LOCK_ASSERT(d);
1238	ready = bpf_ready(d);
1239	if (ready) {
1240		kn->kn_data = d->bd_slen;
1241		if (d->bd_hbuf)
1242			kn->kn_data += d->bd_hlen;
1243	}
1244	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1245		callout_reset(&d->bd_callout, d->bd_rtout,
1246		    bpf_timed_out, d);
1247		d->bd_state = BPF_WAITING;
1248	}
1249
1250	return (ready);
1251}
1252
1253/*
1254 * Incoming linkage from device drivers.  Process the packet pkt, of length
1255 * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1256 * by each process' filter, and if accepted, stashed into the corresponding
1257 * buffer.
1258 */
1259void
1260bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1261{
1262	struct bpf_d *d;
1263	u_int slen;
1264	int gottime;
1265	struct timeval tv;
1266
1267	gottime = 0;
1268	BPFIF_LOCK(bp);
1269	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1270		BPFD_LOCK(d);
1271		++d->bd_rcount;
1272#ifdef BPF_JITTER
1273		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1274			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1275		else
1276#endif
1277		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1278		if (slen != 0) {
1279			d->bd_fcount++;
1280			if (!gottime) {
1281				microtime(&tv);
1282				gottime = 1;
1283			}
1284#ifdef MAC
1285			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1286#endif
1287				catchpacket(d, pkt, pktlen, slen, bcopy, &tv);
1288		}
1289		BPFD_UNLOCK(d);
1290	}
1291	BPFIF_UNLOCK(bp);
1292}
1293
1294/*
1295 * Copy data from an mbuf chain into a buffer.  This code is derived
1296 * from m_copydata in sys/uipc_mbuf.c.
1297 */
1298static void
1299bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1300{
1301	const struct mbuf *m;
1302	u_int count;
1303	u_char *dst;
1304
1305	m = src_arg;
1306	dst = dst_arg;
1307	while (len > 0) {
1308		if (m == NULL)
1309			panic("bpf_mcopy");
1310		count = min(m->m_len, len);
1311		bcopy(mtod(m, void *), dst, count);
1312		m = m->m_next;
1313		dst += count;
1314		len -= count;
1315	}
1316}
1317
1318#define	BPF_CHECK_DIRECTION(d, m) \
1319	if (((d)->bd_direction == BPF_D_IN && (m)->m_pkthdr.rcvif == NULL) || \
1320	    ((d)->bd_direction == BPF_D_OUT && (m)->m_pkthdr.rcvif != NULL))
1321
1322/*
1323 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1324 */
1325void
1326bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1327{
1328	struct bpf_d *d;
1329	u_int pktlen, slen;
1330	int gottime;
1331	struct timeval tv;
1332
1333	if (m->m_flags & M_SKIP_BPF) {
1334		m->m_flags &= ~M_SKIP_BPF;
1335		return;
1336	}
1337
1338	gottime = 0;
1339
1340	pktlen = m_length(m, NULL);
1341
1342	BPFIF_LOCK(bp);
1343	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1344		BPF_CHECK_DIRECTION(d, m)
1345			continue;
1346		BPFD_LOCK(d);
1347		++d->bd_rcount;
1348#ifdef BPF_JITTER
1349		/* XXX We cannot handle multiple mbufs. */
1350		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1351		    m->m_next == NULL)
1352			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1353			    pktlen, pktlen);
1354		else
1355#endif
1356		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1357		if (slen != 0) {
1358			d->bd_fcount++;
1359			if (!gottime) {
1360				microtime(&tv);
1361				gottime = 1;
1362			}
1363#ifdef MAC
1364			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1365#endif
1366				catchpacket(d, (u_char *)m, pktlen, slen,
1367				    bpf_mcopy, &tv);
1368		}
1369		BPFD_UNLOCK(d);
1370	}
1371	BPFIF_UNLOCK(bp);
1372}
1373
1374/*
1375 * Incoming linkage from device drivers, when packet is in
1376 * an mbuf chain and to be prepended by a contiguous header.
1377 */
1378void
1379bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1380{
1381	struct mbuf mb;
1382	struct bpf_d *d;
1383	u_int pktlen, slen;
1384	int gottime;
1385	struct timeval tv;
1386
1387	if (m->m_flags & M_SKIP_BPF) {
1388		m->m_flags &= ~M_SKIP_BPF;
1389		return;
1390	}
1391
1392	gottime = 0;
1393
1394	pktlen = m_length(m, NULL);
1395	/*
1396	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1397	 * Note that we cut corners here; we only setup what's
1398	 * absolutely needed--this mbuf should never go anywhere else.
1399	 */
1400	mb.m_next = m;
1401	mb.m_data = data;
1402	mb.m_len = dlen;
1403	pktlen += dlen;
1404
1405	BPFIF_LOCK(bp);
1406	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1407		BPF_CHECK_DIRECTION(d, m)
1408			continue;
1409		BPFD_LOCK(d);
1410		++d->bd_rcount;
1411		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
1412		if (slen != 0) {
1413			d->bd_fcount++;
1414			if (!gottime) {
1415				microtime(&tv);
1416				gottime = 1;
1417			}
1418#ifdef MAC
1419			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1420#endif
1421				catchpacket(d, (u_char *)&mb, pktlen, slen,
1422				    bpf_mcopy, &tv);
1423		}
1424		BPFD_UNLOCK(d);
1425	}
1426	BPFIF_UNLOCK(bp);
1427}
1428
1429#undef	BPF_CHECK_DIRECTION
1430
1431/*
1432 * Move the packet data from interface memory (pkt) into the
1433 * store buffer.  "cpfn" is the routine called to do the actual data
1434 * transfer.  bcopy is passed in to copy contiguous chunks, while
1435 * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1436 * pkt is really an mbuf.
1437 */
1438static void
1439catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1440    void (*cpfn)(const void *, void *, size_t), struct timeval *tv)
1441{
1442	struct bpf_hdr *hp;
1443	int totlen, curlen;
1444	int hdrlen = d->bd_bif->bif_hdrlen;
1445	int do_wakeup = 0;
1446
1447	BPFD_LOCK_ASSERT(d);
1448	/*
1449	 * Figure out how many bytes to move.  If the packet is
1450	 * greater or equal to the snapshot length, transfer that
1451	 * much.  Otherwise, transfer the whole packet (unless
1452	 * we hit the buffer size limit).
1453	 */
1454	totlen = hdrlen + min(snaplen, pktlen);
1455	if (totlen > d->bd_bufsize)
1456		totlen = d->bd_bufsize;
1457
1458	/*
1459	 * Round up the end of the previous packet to the next longword.
1460	 */
1461	curlen = BPF_WORDALIGN(d->bd_slen);
1462	if (curlen + totlen > d->bd_bufsize) {
1463		/*
1464		 * This packet will overflow the storage buffer.
1465		 * Rotate the buffers if we can, then wakeup any
1466		 * pending reads.
1467		 */
1468		if (d->bd_fbuf == NULL) {
1469			/*
1470			 * We haven't completed the previous read yet,
1471			 * so drop the packet.
1472			 */
1473			++d->bd_dcount;
1474			return;
1475		}
1476		ROTATE_BUFFERS(d);
1477		do_wakeup = 1;
1478		curlen = 0;
1479	}
1480	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1481		/*
1482		 * Immediate mode is set, or the read timeout has
1483		 * already expired during a select call.  A packet
1484		 * arrived, so the reader should be woken up.
1485		 */
1486		do_wakeup = 1;
1487
1488	/*
1489	 * Append the bpf header.
1490	 */
1491	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1492	hp->bh_tstamp = *tv;
1493	hp->bh_datalen = pktlen;
1494	hp->bh_hdrlen = hdrlen;
1495	/*
1496	 * Copy the packet data into the store buffer and update its length.
1497	 */
1498	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1499	d->bd_slen = curlen + totlen;
1500
1501	if (do_wakeup)
1502		bpf_wakeup(d);
1503}
1504
1505/*
1506 * Initialize all nonzero fields of a descriptor.
1507 */
1508static void
1509bpf_allocbufs(struct bpf_d *d)
1510{
1511
1512	KASSERT(d->bd_fbuf == NULL, ("bpf_allocbufs: bd_fbuf != NULL"));
1513	KASSERT(d->bd_sbuf == NULL, ("bpf_allocbufs: bd_sbuf != NULL"));
1514	KASSERT(d->bd_hbuf == NULL, ("bpf_allocbufs: bd_hbuf != NULL"));
1515
1516	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1517	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1518	d->bd_slen = 0;
1519	d->bd_hlen = 0;
1520}
1521
1522/*
1523 * Free buffers currently in use by a descriptor.
1524 * Called on close.
1525 */
1526static void
1527bpf_freed(struct bpf_d *d)
1528{
1529	/*
1530	 * We don't need to lock out interrupts since this descriptor has
1531	 * been detached from its interface and it yet hasn't been marked
1532	 * free.
1533	 */
1534	if (d->bd_sbuf != NULL) {
1535		free(d->bd_sbuf, M_BPF);
1536		if (d->bd_hbuf != NULL)
1537			free(d->bd_hbuf, M_BPF);
1538		if (d->bd_fbuf != NULL)
1539			free(d->bd_fbuf, M_BPF);
1540	}
1541	if (d->bd_rfilter) {
1542		free((caddr_t)d->bd_rfilter, M_BPF);
1543#ifdef BPF_JITTER
1544		bpf_destroy_jit_filter(d->bd_bfilter);
1545#endif
1546	}
1547	if (d->bd_wfilter)
1548		free((caddr_t)d->bd_wfilter, M_BPF);
1549	mtx_destroy(&d->bd_mtx);
1550}
1551
1552/*
1553 * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
1554 * fixed size of the link header (variable length headers not yet supported).
1555 */
1556void
1557bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1558{
1559
1560	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1561}
1562
1563/*
1564 * Attach an interface to bpf.  ifp is a pointer to the structure
1565 * defining the interface to be attached, dlt is the link layer type,
1566 * and hdrlen is the fixed size of the link header (variable length
1567 * headers are not yet supporrted).
1568 */
1569void
1570bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1571{
1572	struct bpf_if *bp;
1573
1574	bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1575	if (bp == NULL)
1576		panic("bpfattach");
1577
1578	LIST_INIT(&bp->bif_dlist);
1579	bp->bif_ifp = ifp;
1580	bp->bif_dlt = dlt;
1581	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1582	KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
1583	*driverp = bp;
1584
1585	mtx_lock(&bpf_mtx);
1586	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1587	mtx_unlock(&bpf_mtx);
1588
1589	/*
1590	 * Compute the length of the bpf header.  This is not necessarily
1591	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1592	 * that the network layer header begins on a longword boundary (for
1593	 * performance reasons and to alleviate alignment restrictions).
1594	 */
1595	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1596
1597	if (bootverbose)
1598		if_printf(ifp, "bpf attached\n");
1599}
1600
1601/*
1602 * Detach bpf from an interface.  This involves detaching each descriptor
1603 * associated with the interface, and leaving bd_bif NULL.  Notify each
1604 * descriptor as it's detached so that any sleepers wake up and get
1605 * ENXIO.
1606 */
1607void
1608bpfdetach(struct ifnet *ifp)
1609{
1610	struct bpf_if	*bp;
1611	struct bpf_d	*d;
1612
1613	/* Locate BPF interface information */
1614	mtx_lock(&bpf_mtx);
1615	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1616		if (ifp == bp->bif_ifp)
1617			break;
1618	}
1619
1620	/* Interface wasn't attached */
1621	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1622		mtx_unlock(&bpf_mtx);
1623		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1624		return;
1625	}
1626
1627	LIST_REMOVE(bp, bif_next);
1628	mtx_unlock(&bpf_mtx);
1629
1630	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1631		bpf_detachd(d);
1632		BPFD_LOCK(d);
1633		bpf_wakeup(d);
1634		BPFD_UNLOCK(d);
1635	}
1636
1637	mtx_destroy(&bp->bif_mtx);
1638	free(bp, M_BPF);
1639}
1640
1641/*
1642 * Get a list of available data link type of the interface.
1643 */
1644static int
1645bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1646{
1647	int n, error;
1648	struct ifnet *ifp;
1649	struct bpf_if *bp;
1650
1651	ifp = d->bd_bif->bif_ifp;
1652	n = 0;
1653	error = 0;
1654	mtx_lock(&bpf_mtx);
1655	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1656		if (bp->bif_ifp != ifp)
1657			continue;
1658		if (bfl->bfl_list != NULL) {
1659			if (n >= bfl->bfl_len) {
1660				mtx_unlock(&bpf_mtx);
1661				return (ENOMEM);
1662			}
1663			error = copyout(&bp->bif_dlt,
1664			    bfl->bfl_list + n, sizeof(u_int));
1665		}
1666		n++;
1667	}
1668	mtx_unlock(&bpf_mtx);
1669	bfl->bfl_len = n;
1670	return (error);
1671}
1672
1673/*
1674 * Set the data link type of a BPF instance.
1675 */
1676static int
1677bpf_setdlt(struct bpf_d *d, u_int dlt)
1678{
1679	int error, opromisc;
1680	struct ifnet *ifp;
1681	struct bpf_if *bp;
1682
1683	if (d->bd_bif->bif_dlt == dlt)
1684		return (0);
1685	ifp = d->bd_bif->bif_ifp;
1686	mtx_lock(&bpf_mtx);
1687	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1688		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1689			break;
1690	}
1691	mtx_unlock(&bpf_mtx);
1692	if (bp != NULL) {
1693		opromisc = d->bd_promisc;
1694		bpf_detachd(d);
1695		bpf_attachd(d, bp);
1696		BPFD_LOCK(d);
1697		reset_d(d);
1698		BPFD_UNLOCK(d);
1699		if (opromisc) {
1700			error = ifpromisc(bp->bif_ifp, 1);
1701			if (error)
1702				if_printf(bp->bif_ifp,
1703					"bpf_setdlt: ifpromisc failed (%d)\n",
1704					error);
1705			else
1706				d->bd_promisc = 1;
1707		}
1708	}
1709	return (bp == NULL ? EINVAL : 0);
1710}
1711
1712static void
1713bpf_clone(void *arg, struct ucred *cred, char *name, int namelen,
1714    struct cdev **dev)
1715{
1716	int u;
1717
1718	if (*dev != NULL)
1719		return;
1720	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1721		return;
1722	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1723	    "bpf%d", u);
1724	dev_ref(*dev);
1725	(*dev)->si_flags |= SI_CHEAPCLONE;
1726	return;
1727}
1728
1729static void
1730bpf_drvinit(void *unused)
1731{
1732
1733	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1734	LIST_INIT(&bpf_iflist);
1735	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1736}
1737
1738static void
1739bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
1740{
1741
1742	bzero(d, sizeof(*d));
1743	BPFD_LOCK_ASSERT(bd);
1744	d->bd_immediate = bd->bd_immediate;
1745	d->bd_promisc = bd->bd_promisc;
1746	d->bd_hdrcmplt = bd->bd_hdrcmplt;
1747	d->bd_direction = bd->bd_direction;
1748	d->bd_feedback = bd->bd_feedback;
1749	d->bd_async = bd->bd_async;
1750	d->bd_rcount = bd->bd_rcount;
1751	d->bd_dcount = bd->bd_dcount;
1752	d->bd_fcount = bd->bd_fcount;
1753	d->bd_sig = bd->bd_sig;
1754	d->bd_slen = bd->bd_slen;
1755	d->bd_hlen = bd->bd_hlen;
1756	d->bd_bufsize = bd->bd_bufsize;
1757	d->bd_pid = bd->bd_pid;
1758	strlcpy(d->bd_ifname,
1759	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
1760	d->bd_locked = bd->bd_locked;
1761}
1762
1763static int
1764bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
1765{
1766	struct xbpf_d *xbdbuf, *xbd;
1767	int index, error;
1768	struct bpf_if *bp;
1769	struct bpf_d *bd;
1770
1771	/*
1772	 * XXX This is not technically correct. It is possible for non
1773	 * privileged users to open bpf devices. It would make sense
1774	 * if the users who opened the devices were able to retrieve
1775	 * the statistics for them, too.
1776	 */
1777	error = priv_check(req->td, PRIV_NET_BPF);
1778	if (error)
1779		return (error);
1780	if (req->oldptr == NULL)
1781		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
1782	if (bpf_bpfd_cnt == 0)
1783		return (SYSCTL_OUT(req, 0, 0));
1784	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
1785	mtx_lock(&bpf_mtx);
1786	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
1787		mtx_unlock(&bpf_mtx);
1788		free(xbdbuf, M_BPF);
1789		return (ENOMEM);
1790	}
1791	index = 0;
1792	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1793		BPFIF_LOCK(bp);
1794		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
1795			xbd = &xbdbuf[index++];
1796			BPFD_LOCK(bd);
1797			bpfstats_fill_xbpf(xbd, bd);
1798			BPFD_UNLOCK(bd);
1799		}
1800		BPFIF_UNLOCK(bp);
1801	}
1802	mtx_unlock(&bpf_mtx);
1803	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
1804	free(xbdbuf, M_BPF);
1805	return (error);
1806}
1807
1808SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
1809
1810#else /* !DEV_BPF && !NETGRAPH_BPF */
1811/*
1812 * NOP stubs to allow bpf-using drivers to load and function.
1813 *
1814 * A 'better' implementation would allow the core bpf functionality
1815 * to be loaded at runtime.
1816 */
1817static struct bpf_if bp_null;
1818
1819void
1820bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1821{
1822}
1823
1824void
1825bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1826{
1827}
1828
1829void
1830bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
1831{
1832}
1833
1834void
1835bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1836{
1837
1838	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1839}
1840
1841void
1842bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1843{
1844
1845	*driverp = &bp_null;
1846}
1847
1848void
1849bpfdetach(struct ifnet *ifp)
1850{
1851}
1852
1853u_int
1854bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1855{
1856	return -1;	/* "no filter" behaviour */
1857}
1858
1859int
1860bpf_validate(const struct bpf_insn *f, int len)
1861{
1862	return 0;		/* false */
1863}
1864
1865#endif /* !DEV_BPF && !NETGRAPH_BPF */
1866