bpf.c revision 148418
1/*-
2 * Copyright (c) 1990, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from the Stanford/CMU enet packet filter,
6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8 * Berkeley Laboratory.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
35 *
36 * $FreeBSD: head/sys/net/bpf.c 148418 2005-07-26 17:21:56Z csjp $
37 */
38
39#include "opt_bpf.h"
40#include "opt_mac.h"
41#include "opt_netgraph.h"
42
43#include <sys/types.h>
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/conf.h>
47#include <sys/fcntl.h>
48#include <sys/mac.h>
49#include <sys/malloc.h>
50#include <sys/mbuf.h>
51#include <sys/time.h>
52#include <sys/proc.h>
53#include <sys/signalvar.h>
54#include <sys/filio.h>
55#include <sys/sockio.h>
56#include <sys/ttycom.h>
57#include <sys/uio.h>
58
59#include <sys/event.h>
60#include <sys/file.h>
61#include <sys/poll.h>
62#include <sys/proc.h>
63
64#include <sys/socket.h>
65
66#include <net/if.h>
67#include <net/bpf.h>
68#include <net/bpfdesc.h>
69
70#include <netinet/in.h>
71#include <netinet/if_ether.h>
72#include <sys/kernel.h>
73#include <sys/sysctl.h>
74
75static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
76
77#if defined(DEV_BPF) || defined(NETGRAPH_BPF)
78
79#define PRINET  26			/* interruptible */
80
81/*
82 * bpf_iflist is a list of BPF interface structures, each corresponding to a
83 * specific DLT.  The same network interface might have several BPF interface
84 * structures registered by different layers in the stack (i.e., 802.11
85 * frames, ethernet frames, etc).
86 */
87static LIST_HEAD(, bpf_if)	bpf_iflist;
88static struct mtx	bpf_mtx;		/* bpf global lock */
89static int		bpf_bpfd_cnt;
90
91static int	bpf_allocbufs(struct bpf_d *);
92static void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
93static void	bpf_detachd(struct bpf_d *d);
94static void	bpf_freed(struct bpf_d *);
95static void	bpf_mcopy(const void *, void *, size_t);
96static int	bpf_movein(struct uio *, int, int,
97		    struct mbuf **, struct sockaddr *);
98static int	bpf_setif(struct bpf_d *, struct ifreq *);
99static void	bpf_timed_out(void *);
100static __inline void
101		bpf_wakeup(struct bpf_d *);
102static void	catchpacket(struct bpf_d *, u_char *, u_int,
103		    u_int, void (*)(const void *, void *, size_t));
104static void	reset_d(struct bpf_d *);
105static int	 bpf_setf(struct bpf_d *, struct bpf_program *);
106static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
107static int	bpf_setdlt(struct bpf_d *, u_int);
108static void	filt_bpfdetach(struct knote *);
109static int	filt_bpfread(struct knote *, long);
110static void	bpf_drvinit(void *);
111static void	bpf_clone(void *, char *, int, struct cdev **);
112static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
113
114/*
115 * The default read buffer size is patchable.
116 */
117SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
118static int bpf_bufsize = 4096;
119SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
120    &bpf_bufsize, 0, "");
121static int bpf_maxbufsize = BPF_MAXBUFSIZE;
122SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
123    &bpf_maxbufsize, 0, "");
124static int bpf_maxinsns = BPF_MAXINSNS;
125SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
126    &bpf_maxinsns, 0, "Maximum bpf program instructions");
127SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
128    bpf_stats_sysctl, "bpf statistics portal");
129
130static	d_open_t	bpfopen;
131static	d_close_t	bpfclose;
132static	d_read_t	bpfread;
133static	d_write_t	bpfwrite;
134static	d_ioctl_t	bpfioctl;
135static	d_poll_t	bpfpoll;
136static	d_kqfilter_t	bpfkqfilter;
137
138static struct cdevsw bpf_cdevsw = {
139	.d_version =	D_VERSION,
140	.d_flags =	D_NEEDGIANT,
141	.d_open =	bpfopen,
142	.d_close =	bpfclose,
143	.d_read =	bpfread,
144	.d_write =	bpfwrite,
145	.d_ioctl =	bpfioctl,
146	.d_poll =	bpfpoll,
147	.d_name =	"bpf",
148	.d_kqfilter =	bpfkqfilter,
149};
150
151static struct filterops bpfread_filtops =
152	{ 1, NULL, filt_bpfdetach, filt_bpfread };
153
154static int
155bpf_movein(uio, linktype, mtu, mp, sockp)
156	struct uio *uio;
157	int linktype;
158	int mtu;
159	struct mbuf **mp;
160	struct sockaddr *sockp;
161{
162	struct mbuf *m;
163	int error;
164	int len;
165	int hlen;
166
167	/*
168	 * Build a sockaddr based on the data link layer type.
169	 * We do this at this level because the ethernet header
170	 * is copied directly into the data field of the sockaddr.
171	 * In the case of SLIP, there is no header and the packet
172	 * is forwarded as is.
173	 * Also, we are careful to leave room at the front of the mbuf
174	 * for the link level header.
175	 */
176	switch (linktype) {
177
178	case DLT_SLIP:
179		sockp->sa_family = AF_INET;
180		hlen = 0;
181		break;
182
183	case DLT_EN10MB:
184		sockp->sa_family = AF_UNSPEC;
185		/* XXX Would MAXLINKHDR be better? */
186		hlen = ETHER_HDR_LEN;
187		break;
188
189	case DLT_FDDI:
190		sockp->sa_family = AF_IMPLINK;
191		hlen = 0;
192		break;
193
194	case DLT_RAW:
195		sockp->sa_family = AF_UNSPEC;
196		hlen = 0;
197		break;
198
199	case DLT_NULL:
200		/*
201		 * null interface types require a 4 byte pseudo header which
202		 * corresponds to the address family of the packet.
203		 */
204		sockp->sa_family = AF_UNSPEC;
205		hlen = 4;
206		break;
207
208	case DLT_ATM_RFC1483:
209		/*
210		 * en atm driver requires 4-byte atm pseudo header.
211		 * though it isn't standard, vpi:vci needs to be
212		 * specified anyway.
213		 */
214		sockp->sa_family = AF_UNSPEC;
215		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
216		break;
217
218	case DLT_PPP:
219		sockp->sa_family = AF_UNSPEC;
220		hlen = 4;	/* This should match PPP_HDRLEN */
221		break;
222
223	default:
224		return (EIO);
225	}
226
227	len = uio->uio_resid;
228
229	if (len - hlen > mtu)
230		return (EMSGSIZE);
231
232	if ((unsigned)len > MCLBYTES)
233		return (EIO);
234
235	if (len > MHLEN) {
236		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
237	} else {
238		MGETHDR(m, M_TRYWAIT, MT_DATA);
239	}
240	if (m == NULL)
241		return (ENOBUFS);
242	m->m_pkthdr.len = m->m_len = len;
243	m->m_pkthdr.rcvif = NULL;
244	*mp = m;
245
246	/*
247	 * Make room for link header.
248	 */
249	if (hlen != 0) {
250		m->m_pkthdr.len -= hlen;
251		m->m_len -= hlen;
252#if BSD >= 199103
253		m->m_data += hlen; /* XXX */
254#else
255		m->m_off += hlen;
256#endif
257		error = uiomove(sockp->sa_data, hlen, uio);
258		if (error)
259			goto bad;
260	}
261	error = uiomove(mtod(m, void *), len - hlen, uio);
262	if (!error)
263		return (0);
264bad:
265	m_freem(m);
266	return (error);
267}
268
269/*
270 * Attach file to the bpf interface, i.e. make d listen on bp.
271 */
272static void
273bpf_attachd(d, bp)
274	struct bpf_d *d;
275	struct bpf_if *bp;
276{
277	/*
278	 * Point d at bp, and add d to the interface's list of listeners.
279	 * Finally, point the driver's bpf cookie at the interface so
280	 * it will divert packets to bpf.
281	 */
282	BPFIF_LOCK(bp);
283	d->bd_bif = bp;
284	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
285
286	bpf_bpfd_cnt++;
287	*bp->bif_driverp = bp;
288	BPFIF_UNLOCK(bp);
289}
290
291/*
292 * Detach a file from its interface.
293 */
294static void
295bpf_detachd(d)
296	struct bpf_d *d;
297{
298	int error;
299	struct bpf_if *bp;
300	struct ifnet *ifp;
301
302	bp = d->bd_bif;
303	BPFIF_LOCK(bp);
304	BPFD_LOCK(d);
305	ifp = d->bd_bif->bif_ifp;
306
307	/*
308	 * Remove d from the interface's descriptor list.
309	 */
310	LIST_REMOVE(d, bd_next);
311
312	bpf_bpfd_cnt--;
313	/*
314	 * Let the driver know that there are no more listeners.
315	 */
316	if (LIST_EMPTY(&bp->bif_dlist))
317		*bp->bif_driverp = NULL;
318
319	d->bd_bif = NULL;
320	BPFD_UNLOCK(d);
321	BPFIF_UNLOCK(bp);
322
323	/*
324	 * Check if this descriptor had requested promiscuous mode.
325	 * If so, turn it off.
326	 */
327	if (d->bd_promisc) {
328		d->bd_promisc = 0;
329		error = ifpromisc(ifp, 0);
330		if (error != 0 && error != ENXIO) {
331			/*
332			 * ENXIO can happen if a pccard is unplugged
333			 * Something is really wrong if we were able to put
334			 * the driver into promiscuous mode, but can't
335			 * take it out.
336			 */
337			if_printf(bp->bif_ifp,
338				"bpf_detach: ifpromisc failed (%d)\n", error);
339		}
340	}
341}
342
343/*
344 * Open ethernet device.  Returns ENXIO for illegal minor device number,
345 * EBUSY if file is open by another process.
346 */
347/* ARGSUSED */
348static	int
349bpfopen(dev, flags, fmt, td)
350	struct cdev *dev;
351	int flags;
352	int fmt;
353	struct thread *td;
354{
355	struct bpf_d *d;
356
357	mtx_lock(&bpf_mtx);
358	d = dev->si_drv1;
359	/*
360	 * Each minor can be opened by only one process.  If the requested
361	 * minor is in use, return EBUSY.
362	 */
363	if (d != NULL) {
364		mtx_unlock(&bpf_mtx);
365		return (EBUSY);
366	}
367	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
368	mtx_unlock(&bpf_mtx);
369
370	if ((dev->si_flags & SI_NAMED) == 0)
371		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
372		    "bpf%d", dev2unit(dev));
373	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
374	dev->si_drv1 = d;
375	d->bd_bufsize = bpf_bufsize;
376	d->bd_sig = SIGIO;
377	d->bd_seesent = 1;
378	d->bd_pid = td->td_proc->p_pid;
379	strlcpy(d->bd_pcomm, td->td_proc->p_comm, MAXCOMLEN);
380#ifdef MAC
381	mac_init_bpfdesc(d);
382	mac_create_bpfdesc(td->td_ucred, d);
383#endif
384	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
385	callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE);
386	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
387
388	return (0);
389}
390
391/*
392 * Close the descriptor by detaching it from its interface,
393 * deallocating its buffers, and marking it free.
394 */
395/* ARGSUSED */
396static	int
397bpfclose(dev, flags, fmt, td)
398	struct cdev *dev;
399	int flags;
400	int fmt;
401	struct thread *td;
402{
403	struct bpf_d *d = dev->si_drv1;
404
405	BPFD_LOCK(d);
406	if (d->bd_state == BPF_WAITING)
407		callout_stop(&d->bd_callout);
408	d->bd_state = BPF_IDLE;
409	BPFD_UNLOCK(d);
410	funsetown(&d->bd_sigio);
411	mtx_lock(&bpf_mtx);
412	if (d->bd_bif)
413		bpf_detachd(d);
414	mtx_unlock(&bpf_mtx);
415	selwakeuppri(&d->bd_sel, PRINET);
416#ifdef MAC
417	mac_destroy_bpfdesc(d);
418#endif /* MAC */
419	knlist_destroy(&d->bd_sel.si_note);
420	bpf_freed(d);
421	dev->si_drv1 = NULL;
422	free(d, M_BPF);
423
424	return (0);
425}
426
427
428/*
429 * Rotate the packet buffers in descriptor d.  Move the store buffer
430 * into the hold slot, and the free buffer into the store slot.
431 * Zero the length of the new store buffer.
432 */
433#define ROTATE_BUFFERS(d) \
434	(d)->bd_hbuf = (d)->bd_sbuf; \
435	(d)->bd_hlen = (d)->bd_slen; \
436	(d)->bd_sbuf = (d)->bd_fbuf; \
437	(d)->bd_slen = 0; \
438	(d)->bd_fbuf = NULL;
439/*
440 *  bpfread - read next chunk of packets from buffers
441 */
442static	int
443bpfread(dev, uio, ioflag)
444	struct cdev *dev;
445	struct uio *uio;
446	int ioflag;
447{
448	struct bpf_d *d = dev->si_drv1;
449	int timed_out;
450	int error;
451
452	/*
453	 * Restrict application to use a buffer the same size as
454	 * as kernel buffers.
455	 */
456	if (uio->uio_resid != d->bd_bufsize)
457		return (EINVAL);
458
459	BPFD_LOCK(d);
460	if (d->bd_state == BPF_WAITING)
461		callout_stop(&d->bd_callout);
462	timed_out = (d->bd_state == BPF_TIMED_OUT);
463	d->bd_state = BPF_IDLE;
464	/*
465	 * If the hold buffer is empty, then do a timed sleep, which
466	 * ends when the timeout expires or when enough packets
467	 * have arrived to fill the store buffer.
468	 */
469	while (d->bd_hbuf == NULL) {
470		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
471			/*
472			 * A packet(s) either arrived since the previous
473			 * read or arrived while we were asleep.
474			 * Rotate the buffers and return what's here.
475			 */
476			ROTATE_BUFFERS(d);
477			break;
478		}
479
480		/*
481		 * No data is available, check to see if the bpf device
482		 * is still pointed at a real interface.  If not, return
483		 * ENXIO so that the userland process knows to rebind
484		 * it before using it again.
485		 */
486		if (d->bd_bif == NULL) {
487			BPFD_UNLOCK(d);
488			return (ENXIO);
489		}
490
491		if (ioflag & O_NONBLOCK) {
492			BPFD_UNLOCK(d);
493			return (EWOULDBLOCK);
494		}
495		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
496		     "bpf", d->bd_rtout);
497		if (error == EINTR || error == ERESTART) {
498			BPFD_UNLOCK(d);
499			return (error);
500		}
501		if (error == EWOULDBLOCK) {
502			/*
503			 * On a timeout, return what's in the buffer,
504			 * which may be nothing.  If there is something
505			 * in the store buffer, we can rotate the buffers.
506			 */
507			if (d->bd_hbuf)
508				/*
509				 * We filled up the buffer in between
510				 * getting the timeout and arriving
511				 * here, so we don't need to rotate.
512				 */
513				break;
514
515			if (d->bd_slen == 0) {
516				BPFD_UNLOCK(d);
517				return (0);
518			}
519			ROTATE_BUFFERS(d);
520			break;
521		}
522	}
523	/*
524	 * At this point, we know we have something in the hold slot.
525	 */
526	BPFD_UNLOCK(d);
527
528	/*
529	 * Move data from hold buffer into user space.
530	 * We know the entire buffer is transferred since
531	 * we checked above that the read buffer is bpf_bufsize bytes.
532	 */
533	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
534
535	BPFD_LOCK(d);
536	d->bd_fbuf = d->bd_hbuf;
537	d->bd_hbuf = NULL;
538	d->bd_hlen = 0;
539	BPFD_UNLOCK(d);
540
541	return (error);
542}
543
544
545/*
546 * If there are processes sleeping on this descriptor, wake them up.
547 */
548static __inline void
549bpf_wakeup(d)
550	struct bpf_d *d;
551{
552
553	BPFD_LOCK_ASSERT(d);
554	if (d->bd_state == BPF_WAITING) {
555		callout_stop(&d->bd_callout);
556		d->bd_state = BPF_IDLE;
557	}
558	wakeup(d);
559	if (d->bd_async && d->bd_sig && d->bd_sigio)
560		pgsigio(&d->bd_sigio, d->bd_sig, 0);
561
562	selwakeuppri(&d->bd_sel, PRINET);
563	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
564}
565
566static void
567bpf_timed_out(arg)
568	void *arg;
569{
570	struct bpf_d *d = (struct bpf_d *)arg;
571
572	BPFD_LOCK(d);
573	if (d->bd_state == BPF_WAITING) {
574		d->bd_state = BPF_TIMED_OUT;
575		if (d->bd_slen != 0)
576			bpf_wakeup(d);
577	}
578	BPFD_UNLOCK(d);
579}
580
581static	int
582bpfwrite(dev, uio, ioflag)
583	struct cdev *dev;
584	struct uio *uio;
585	int ioflag;
586{
587	struct bpf_d *d = dev->si_drv1;
588	struct ifnet *ifp;
589	struct mbuf *m;
590	int error;
591	struct sockaddr dst;
592
593	if (d->bd_bif == NULL)
594		return (ENXIO);
595
596	ifp = d->bd_bif->bif_ifp;
597
598	if ((ifp->if_flags & IFF_UP) == 0)
599		return (ENETDOWN);
600
601	if (uio->uio_resid == 0)
602		return (0);
603
604	bzero(&dst, sizeof(dst));
605	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m, &dst);
606	if (error)
607		return (error);
608
609	if (d->bd_hdrcmplt)
610		dst.sa_family = pseudo_AF_HDRCMPLT;
611
612#ifdef MAC
613	BPFD_LOCK(d);
614	mac_create_mbuf_from_bpfdesc(d, m);
615	BPFD_UNLOCK(d);
616#endif
617	NET_LOCK_GIANT();
618	error = (*ifp->if_output)(ifp, m, &dst, NULL);
619	NET_UNLOCK_GIANT();
620	/*
621	 * The driver frees the mbuf.
622	 */
623	return (error);
624}
625
626/*
627 * Reset a descriptor by flushing its packet buffer and clearing the
628 * receive and drop counts.
629 */
630static void
631reset_d(d)
632	struct bpf_d *d;
633{
634
635	mtx_assert(&d->bd_mtx, MA_OWNED);
636	if (d->bd_hbuf) {
637		/* Free the hold buffer. */
638		d->bd_fbuf = d->bd_hbuf;
639		d->bd_hbuf = NULL;
640	}
641	d->bd_slen = 0;
642	d->bd_hlen = 0;
643	d->bd_rcount = 0;
644	d->bd_dcount = 0;
645	d->bd_fcount = 0;
646}
647
648/*
649 *  FIONREAD		Check for read packet available.
650 *  SIOCGIFADDR		Get interface address - convenient hook to driver.
651 *  BIOCGBLEN		Get buffer len [for read()].
652 *  BIOCSETF		Set ethernet read filter.
653 *  BIOCFLUSH		Flush read packet buffer.
654 *  BIOCPROMISC		Put interface into promiscuous mode.
655 *  BIOCGDLT		Get link layer type.
656 *  BIOCGETIF		Get interface name.
657 *  BIOCSETIF		Set interface.
658 *  BIOCSRTIMEOUT	Set read timeout.
659 *  BIOCGRTIMEOUT	Get read timeout.
660 *  BIOCGSTATS		Get packet stats.
661 *  BIOCIMMEDIATE	Set immediate mode.
662 *  BIOCVERSION		Get filter language version.
663 *  BIOCGHDRCMPLT	Get "header already complete" flag
664 *  BIOCSHDRCMPLT	Set "header already complete" flag
665 *  BIOCGSEESENT	Get "see packets sent" flag
666 *  BIOCSSEESENT	Set "see packets sent" flag
667 */
668/* ARGSUSED */
669static	int
670bpfioctl(dev, cmd, addr, flags, td)
671	struct cdev *dev;
672	u_long cmd;
673	caddr_t addr;
674	int flags;
675	struct thread *td;
676{
677	struct bpf_d *d = dev->si_drv1;
678	int error = 0;
679
680	BPFD_LOCK(d);
681	if (d->bd_state == BPF_WAITING)
682		callout_stop(&d->bd_callout);
683	d->bd_state = BPF_IDLE;
684	BPFD_UNLOCK(d);
685
686	switch (cmd) {
687
688	default:
689		error = EINVAL;
690		break;
691
692	/*
693	 * Check for read packet available.
694	 */
695	case FIONREAD:
696		{
697			int n;
698
699			BPFD_LOCK(d);
700			n = d->bd_slen;
701			if (d->bd_hbuf)
702				n += d->bd_hlen;
703			BPFD_UNLOCK(d);
704
705			*(int *)addr = n;
706			break;
707		}
708
709	case SIOCGIFADDR:
710		{
711			struct ifnet *ifp;
712
713			if (d->bd_bif == NULL)
714				error = EINVAL;
715			else {
716				ifp = d->bd_bif->bif_ifp;
717				error = (*ifp->if_ioctl)(ifp, cmd, addr);
718			}
719			break;
720		}
721
722	/*
723	 * Get buffer len [for read()].
724	 */
725	case BIOCGBLEN:
726		*(u_int *)addr = d->bd_bufsize;
727		break;
728
729	/*
730	 * Set buffer length.
731	 */
732	case BIOCSBLEN:
733		if (d->bd_bif != NULL)
734			error = EINVAL;
735		else {
736			u_int size = *(u_int *)addr;
737
738			if (size > bpf_maxbufsize)
739				*(u_int *)addr = size = bpf_maxbufsize;
740			else if (size < BPF_MINBUFSIZE)
741				*(u_int *)addr = size = BPF_MINBUFSIZE;
742			d->bd_bufsize = size;
743		}
744		break;
745
746	/*
747	 * Set link layer read filter.
748	 */
749	case BIOCSETF:
750		error = bpf_setf(d, (struct bpf_program *)addr);
751		break;
752
753	/*
754	 * Flush read packet buffer.
755	 */
756	case BIOCFLUSH:
757		BPFD_LOCK(d);
758		reset_d(d);
759		BPFD_UNLOCK(d);
760		break;
761
762	/*
763	 * Put interface into promiscuous mode.
764	 */
765	case BIOCPROMISC:
766		if (d->bd_bif == NULL) {
767			/*
768			 * No interface attached yet.
769			 */
770			error = EINVAL;
771			break;
772		}
773		if (d->bd_promisc == 0) {
774			mtx_lock(&Giant);
775			error = ifpromisc(d->bd_bif->bif_ifp, 1);
776			mtx_unlock(&Giant);
777			if (error == 0)
778				d->bd_promisc = 1;
779		}
780		break;
781
782	/*
783	 * Get current data link type.
784	 */
785	case BIOCGDLT:
786		if (d->bd_bif == NULL)
787			error = EINVAL;
788		else
789			*(u_int *)addr = d->bd_bif->bif_dlt;
790		break;
791
792	/*
793	 * Get a list of supported data link types.
794	 */
795	case BIOCGDLTLIST:
796		if (d->bd_bif == NULL)
797			error = EINVAL;
798		else
799			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
800		break;
801
802	/*
803	 * Set data link type.
804	 */
805	case BIOCSDLT:
806		if (d->bd_bif == NULL)
807			error = EINVAL;
808		else
809			error = bpf_setdlt(d, *(u_int *)addr);
810		break;
811
812	/*
813	 * Get interface name.
814	 */
815	case BIOCGETIF:
816		if (d->bd_bif == NULL)
817			error = EINVAL;
818		else {
819			struct ifnet *const ifp = d->bd_bif->bif_ifp;
820			struct ifreq *const ifr = (struct ifreq *)addr;
821
822			strlcpy(ifr->ifr_name, ifp->if_xname,
823			    sizeof(ifr->ifr_name));
824		}
825		break;
826
827	/*
828	 * Set interface.
829	 */
830	case BIOCSETIF:
831		error = bpf_setif(d, (struct ifreq *)addr);
832		break;
833
834	/*
835	 * Set read timeout.
836	 */
837	case BIOCSRTIMEOUT:
838		{
839			struct timeval *tv = (struct timeval *)addr;
840
841			/*
842			 * Subtract 1 tick from tvtohz() since this isn't
843			 * a one-shot timer.
844			 */
845			if ((error = itimerfix(tv)) == 0)
846				d->bd_rtout = tvtohz(tv) - 1;
847			break;
848		}
849
850	/*
851	 * Get read timeout.
852	 */
853	case BIOCGRTIMEOUT:
854		{
855			struct timeval *tv = (struct timeval *)addr;
856
857			tv->tv_sec = d->bd_rtout / hz;
858			tv->tv_usec = (d->bd_rtout % hz) * tick;
859			break;
860		}
861
862	/*
863	 * Get packet stats.
864	 */
865	case BIOCGSTATS:
866		{
867			struct bpf_stat *bs = (struct bpf_stat *)addr;
868
869			bs->bs_recv = d->bd_rcount;
870			bs->bs_drop = d->bd_dcount;
871			break;
872		}
873
874	/*
875	 * Set immediate mode.
876	 */
877	case BIOCIMMEDIATE:
878		d->bd_immediate = *(u_int *)addr;
879		break;
880
881	case BIOCVERSION:
882		{
883			struct bpf_version *bv = (struct bpf_version *)addr;
884
885			bv->bv_major = BPF_MAJOR_VERSION;
886			bv->bv_minor = BPF_MINOR_VERSION;
887			break;
888		}
889
890	/*
891	 * Get "header already complete" flag
892	 */
893	case BIOCGHDRCMPLT:
894		*(u_int *)addr = d->bd_hdrcmplt;
895		break;
896
897	/*
898	 * Set "header already complete" flag
899	 */
900	case BIOCSHDRCMPLT:
901		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
902		break;
903
904	/*
905	 * Get "see sent packets" flag
906	 */
907	case BIOCGSEESENT:
908		*(u_int *)addr = d->bd_seesent;
909		break;
910
911	/*
912	 * Set "see sent packets" flag
913	 */
914	case BIOCSSEESENT:
915		d->bd_seesent = *(u_int *)addr;
916		break;
917
918	case FIONBIO:		/* Non-blocking I/O */
919		break;
920
921	case FIOASYNC:		/* Send signal on receive packets */
922		d->bd_async = *(int *)addr;
923		break;
924
925	case FIOSETOWN:
926		error = fsetown(*(int *)addr, &d->bd_sigio);
927		break;
928
929	case FIOGETOWN:
930		*(int *)addr = fgetown(&d->bd_sigio);
931		break;
932
933	/* This is deprecated, FIOSETOWN should be used instead. */
934	case TIOCSPGRP:
935		error = fsetown(-(*(int *)addr), &d->bd_sigio);
936		break;
937
938	/* This is deprecated, FIOGETOWN should be used instead. */
939	case TIOCGPGRP:
940		*(int *)addr = -fgetown(&d->bd_sigio);
941		break;
942
943	case BIOCSRSIG:		/* Set receive signal */
944		{
945			u_int sig;
946
947			sig = *(u_int *)addr;
948
949			if (sig >= NSIG)
950				error = EINVAL;
951			else
952				d->bd_sig = sig;
953			break;
954		}
955	case BIOCGRSIG:
956		*(u_int *)addr = d->bd_sig;
957		break;
958	}
959	return (error);
960}
961
962/*
963 * Set d's packet filter program to fp.  If this file already has a filter,
964 * free it and replace it.  Returns EINVAL for bogus requests.
965 */
966static int
967bpf_setf(d, fp)
968	struct bpf_d *d;
969	struct bpf_program *fp;
970{
971	struct bpf_insn *fcode, *old;
972	u_int flen, size;
973
974	if (fp->bf_insns == NULL) {
975		if (fp->bf_len != 0)
976			return (EINVAL);
977		BPFD_LOCK(d);
978		old = d->bd_filter;
979		d->bd_filter = NULL;
980		reset_d(d);
981		BPFD_UNLOCK(d);
982		if (old != NULL)
983			free((caddr_t)old, M_BPF);
984		return (0);
985	}
986	flen = fp->bf_len;
987	if (flen > bpf_maxinsns)
988		return (EINVAL);
989
990	size = flen * sizeof(*fp->bf_insns);
991	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
992	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
993	    bpf_validate(fcode, (int)flen)) {
994		BPFD_LOCK(d);
995		old = d->bd_filter;
996		d->bd_filter = fcode;
997		reset_d(d);
998		BPFD_UNLOCK(d);
999		if (old != NULL)
1000			free((caddr_t)old, M_BPF);
1001
1002		return (0);
1003	}
1004	free((caddr_t)fcode, M_BPF);
1005	return (EINVAL);
1006}
1007
1008/*
1009 * Detach a file from its current interface (if attached at all) and attach
1010 * to the interface indicated by the name stored in ifr.
1011 * Return an errno or 0.
1012 */
1013static int
1014bpf_setif(d, ifr)
1015	struct bpf_d *d;
1016	struct ifreq *ifr;
1017{
1018	struct bpf_if *bp;
1019	int error;
1020	struct ifnet *theywant;
1021
1022	theywant = ifunit(ifr->ifr_name);
1023	if (theywant == NULL)
1024		return ENXIO;
1025
1026	/*
1027	 * Look through attached interfaces for the named one.
1028	 */
1029	mtx_lock(&bpf_mtx);
1030	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1031		struct ifnet *ifp = bp->bif_ifp;
1032
1033		if (ifp == NULL || ifp != theywant)
1034			continue;
1035		/* skip additional entry */
1036		if (bp->bif_driverp != &ifp->if_bpf)
1037			continue;
1038
1039		mtx_unlock(&bpf_mtx);
1040		/*
1041		 * We found the requested interface.
1042		 * Allocate the packet buffers if we need to.
1043		 * If we're already attached to requested interface,
1044		 * just flush the buffer.
1045		 */
1046		if (d->bd_sbuf == NULL) {
1047			error = bpf_allocbufs(d);
1048			if (error != 0)
1049				return (error);
1050		}
1051		if (bp != d->bd_bif) {
1052			if (d->bd_bif)
1053				/*
1054				 * Detach if attached to something else.
1055				 */
1056				bpf_detachd(d);
1057
1058			bpf_attachd(d, bp);
1059		}
1060		BPFD_LOCK(d);
1061		reset_d(d);
1062		BPFD_UNLOCK(d);
1063		return (0);
1064	}
1065	mtx_unlock(&bpf_mtx);
1066	/* Not found. */
1067	return (ENXIO);
1068}
1069
1070/*
1071 * Support for select() and poll() system calls
1072 *
1073 * Return true iff the specific operation will not block indefinitely.
1074 * Otherwise, return false but make a note that a selwakeup() must be done.
1075 */
1076static int
1077bpfpoll(dev, events, td)
1078	struct cdev *dev;
1079	int events;
1080	struct thread *td;
1081{
1082	struct bpf_d *d;
1083	int revents;
1084
1085	d = dev->si_drv1;
1086	if (d->bd_bif == NULL)
1087		return (ENXIO);
1088
1089	revents = events & (POLLOUT | POLLWRNORM);
1090	BPFD_LOCK(d);
1091	if (events & (POLLIN | POLLRDNORM)) {
1092		if (bpf_ready(d))
1093			revents |= events & (POLLIN | POLLRDNORM);
1094		else {
1095			selrecord(td, &d->bd_sel);
1096			/* Start the read timeout if necessary. */
1097			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1098				callout_reset(&d->bd_callout, d->bd_rtout,
1099				    bpf_timed_out, d);
1100				d->bd_state = BPF_WAITING;
1101			}
1102		}
1103	}
1104	BPFD_UNLOCK(d);
1105	return (revents);
1106}
1107
1108/*
1109 * Support for kevent() system call.  Register EVFILT_READ filters and
1110 * reject all others.
1111 */
1112int
1113bpfkqfilter(dev, kn)
1114	struct cdev *dev;
1115	struct knote *kn;
1116{
1117	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1118
1119	if (kn->kn_filter != EVFILT_READ)
1120		return (1);
1121
1122	kn->kn_fop = &bpfread_filtops;
1123	kn->kn_hook = d;
1124	knlist_add(&d->bd_sel.si_note, kn, 0);
1125
1126	return (0);
1127}
1128
1129static void
1130filt_bpfdetach(kn)
1131	struct knote *kn;
1132{
1133	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1134
1135	knlist_remove(&d->bd_sel.si_note, kn, 0);
1136}
1137
1138static int
1139filt_bpfread(kn, hint)
1140	struct knote *kn;
1141	long hint;
1142{
1143	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1144	int ready;
1145
1146	BPFD_LOCK_ASSERT(d);
1147	ready = bpf_ready(d);
1148	if (ready) {
1149		kn->kn_data = d->bd_slen;
1150		if (d->bd_hbuf)
1151			kn->kn_data += d->bd_hlen;
1152	}
1153	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1154		callout_reset(&d->bd_callout, d->bd_rtout,
1155		    bpf_timed_out, d);
1156		d->bd_state = BPF_WAITING;
1157	}
1158
1159	return (ready);
1160}
1161
1162/*
1163 * Incoming linkage from device drivers.  Process the packet pkt, of length
1164 * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1165 * by each process' filter, and if accepted, stashed into the corresponding
1166 * buffer.
1167 */
1168void
1169bpf_tap(bp, pkt, pktlen)
1170	struct bpf_if *bp;
1171	u_char *pkt;
1172	u_int pktlen;
1173{
1174	struct bpf_d *d;
1175	u_int slen;
1176
1177	/*
1178	 * Lockless read to avoid cost of locking the interface if there are
1179	 * no descriptors attached.
1180	 */
1181	if (LIST_EMPTY(&bp->bif_dlist))
1182		return;
1183
1184	BPFIF_LOCK(bp);
1185	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1186		BPFD_LOCK(d);
1187		++d->bd_rcount;
1188		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1189		if (slen != 0) {
1190			d->bd_fcount++;
1191#ifdef MAC
1192			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1193#endif
1194				catchpacket(d, pkt, pktlen, slen, bcopy);
1195		}
1196		BPFD_UNLOCK(d);
1197	}
1198	BPFIF_UNLOCK(bp);
1199}
1200
1201/*
1202 * Copy data from an mbuf chain into a buffer.  This code is derived
1203 * from m_copydata in sys/uipc_mbuf.c.
1204 */
1205static void
1206bpf_mcopy(src_arg, dst_arg, len)
1207	const void *src_arg;
1208	void *dst_arg;
1209	size_t len;
1210{
1211	const struct mbuf *m;
1212	u_int count;
1213	u_char *dst;
1214
1215	m = src_arg;
1216	dst = dst_arg;
1217	while (len > 0) {
1218		if (m == NULL)
1219			panic("bpf_mcopy");
1220		count = min(m->m_len, len);
1221		bcopy(mtod(m, void *), dst, count);
1222		m = m->m_next;
1223		dst += count;
1224		len -= count;
1225	}
1226}
1227
1228/*
1229 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1230 */
1231void
1232bpf_mtap(bp, m)
1233	struct bpf_if *bp;
1234	struct mbuf *m;
1235{
1236	struct bpf_d *d;
1237	u_int pktlen, slen;
1238
1239	/*
1240	 * Lockless read to avoid cost of locking the interface if there are
1241	 * no descriptors attached.
1242	 */
1243	if (LIST_EMPTY(&bp->bif_dlist))
1244		return;
1245
1246	pktlen = m_length(m, NULL);
1247	if (pktlen == m->m_len) {
1248		bpf_tap(bp, mtod(m, u_char *), pktlen);
1249		return;
1250	}
1251
1252	BPFIF_LOCK(bp);
1253	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1254		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1255			continue;
1256		BPFD_LOCK(d);
1257		++d->bd_rcount;
1258		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1259		if (slen != 0)
1260			d->bd_fcount++;
1261#ifdef MAC
1262			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1263#endif
1264				catchpacket(d, (u_char *)m, pktlen, slen,
1265				    bpf_mcopy);
1266		BPFD_UNLOCK(d);
1267	}
1268	BPFIF_UNLOCK(bp);
1269}
1270
1271/*
1272 * Incoming linkage from device drivers, when packet is in
1273 * an mbuf chain and to be prepended by a contiguous header.
1274 */
1275void
1276bpf_mtap2(bp, data, dlen, m)
1277	struct bpf_if *bp;
1278	void *data;
1279	u_int dlen;
1280	struct mbuf *m;
1281{
1282	struct mbuf mb;
1283	struct bpf_d *d;
1284	u_int pktlen, slen;
1285
1286	/*
1287	 * Lockless read to avoid cost of locking the interface if there are
1288	 * no descriptors attached.
1289	 */
1290	if (LIST_EMPTY(&bp->bif_dlist))
1291		return;
1292
1293	pktlen = m_length(m, NULL);
1294	/*
1295	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1296	 * Note that we cut corners here; we only setup what's
1297	 * absolutely needed--this mbuf should never go anywhere else.
1298	 */
1299	mb.m_next = m;
1300	mb.m_data = data;
1301	mb.m_len = dlen;
1302	pktlen += dlen;
1303
1304	BPFIF_LOCK(bp);
1305	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1306		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1307			continue;
1308		BPFD_LOCK(d);
1309		++d->bd_rcount;
1310		slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1311		if (slen != 0)
1312			d->bd_fcount++;
1313#ifdef MAC
1314			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1315#endif
1316				catchpacket(d, (u_char *)&mb, pktlen, slen,
1317				    bpf_mcopy);
1318		BPFD_UNLOCK(d);
1319	}
1320	BPFIF_UNLOCK(bp);
1321}
1322
1323/*
1324 * Move the packet data from interface memory (pkt) into the
1325 * store buffer.  "cpfn" is the routine called to do the actual data
1326 * transfer.  bcopy is passed in to copy contiguous chunks, while
1327 * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1328 * pkt is really an mbuf.
1329 */
1330static void
1331catchpacket(d, pkt, pktlen, snaplen, cpfn)
1332	struct bpf_d *d;
1333	u_char *pkt;
1334	u_int pktlen, snaplen;
1335	void (*cpfn)(const void *, void *, size_t);
1336{
1337	struct bpf_hdr *hp;
1338	int totlen, curlen;
1339	int hdrlen = d->bd_bif->bif_hdrlen;
1340	int do_wakeup = 0;
1341
1342	BPFD_LOCK_ASSERT(d);
1343	/*
1344	 * Figure out how many bytes to move.  If the packet is
1345	 * greater or equal to the snapshot length, transfer that
1346	 * much.  Otherwise, transfer the whole packet (unless
1347	 * we hit the buffer size limit).
1348	 */
1349	totlen = hdrlen + min(snaplen, pktlen);
1350	if (totlen > d->bd_bufsize)
1351		totlen = d->bd_bufsize;
1352
1353	/*
1354	 * Round up the end of the previous packet to the next longword.
1355	 */
1356	curlen = BPF_WORDALIGN(d->bd_slen);
1357	if (curlen + totlen > d->bd_bufsize) {
1358		/*
1359		 * This packet will overflow the storage buffer.
1360		 * Rotate the buffers if we can, then wakeup any
1361		 * pending reads.
1362		 */
1363		if (d->bd_fbuf == NULL) {
1364			/*
1365			 * We haven't completed the previous read yet,
1366			 * so drop the packet.
1367			 */
1368			++d->bd_dcount;
1369			return;
1370		}
1371		ROTATE_BUFFERS(d);
1372		do_wakeup = 1;
1373		curlen = 0;
1374	}
1375	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1376		/*
1377		 * Immediate mode is set, or the read timeout has
1378		 * already expired during a select call.  A packet
1379		 * arrived, so the reader should be woken up.
1380		 */
1381		do_wakeup = 1;
1382
1383	/*
1384	 * Append the bpf header.
1385	 */
1386	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1387	microtime(&hp->bh_tstamp);
1388	hp->bh_datalen = pktlen;
1389	hp->bh_hdrlen = hdrlen;
1390	/*
1391	 * Copy the packet data into the store buffer and update its length.
1392	 */
1393	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1394	d->bd_slen = curlen + totlen;
1395
1396	if (do_wakeup)
1397		bpf_wakeup(d);
1398}
1399
1400/*
1401 * Initialize all nonzero fields of a descriptor.
1402 */
1403static int
1404bpf_allocbufs(d)
1405	struct bpf_d *d;
1406{
1407	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1408	if (d->bd_fbuf == NULL)
1409		return (ENOBUFS);
1410
1411	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1412	if (d->bd_sbuf == NULL) {
1413		free(d->bd_fbuf, M_BPF);
1414		return (ENOBUFS);
1415	}
1416	d->bd_slen = 0;
1417	d->bd_hlen = 0;
1418	return (0);
1419}
1420
1421/*
1422 * Free buffers currently in use by a descriptor.
1423 * Called on close.
1424 */
1425static void
1426bpf_freed(d)
1427	struct bpf_d *d;
1428{
1429	/*
1430	 * We don't need to lock out interrupts since this descriptor has
1431	 * been detached from its interface and it yet hasn't been marked
1432	 * free.
1433	 */
1434	if (d->bd_sbuf != NULL) {
1435		free(d->bd_sbuf, M_BPF);
1436		if (d->bd_hbuf != NULL)
1437			free(d->bd_hbuf, M_BPF);
1438		if (d->bd_fbuf != NULL)
1439			free(d->bd_fbuf, M_BPF);
1440	}
1441	if (d->bd_filter)
1442		free((caddr_t)d->bd_filter, M_BPF);
1443	mtx_destroy(&d->bd_mtx);
1444}
1445
1446/*
1447 * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
1448 * fixed size of the link header (variable length headers not yet supported).
1449 */
1450void
1451bpfattach(ifp, dlt, hdrlen)
1452	struct ifnet *ifp;
1453	u_int dlt, hdrlen;
1454{
1455
1456	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1457}
1458
1459/*
1460 * Attach an interface to bpf.  ifp is a pointer to the structure
1461 * defining the interface to be attached, dlt is the link layer type,
1462 * and hdrlen is the fixed size of the link header (variable length
1463 * headers are not yet supporrted).
1464 */
1465void
1466bpfattach2(ifp, dlt, hdrlen, driverp)
1467	struct ifnet *ifp;
1468	u_int dlt, hdrlen;
1469	struct bpf_if **driverp;
1470{
1471	struct bpf_if *bp;
1472	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1473	if (bp == NULL)
1474		panic("bpfattach");
1475
1476	LIST_INIT(&bp->bif_dlist);
1477	bp->bif_driverp = driverp;
1478	bp->bif_ifp = ifp;
1479	bp->bif_dlt = dlt;
1480	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1481
1482	mtx_lock(&bpf_mtx);
1483	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1484	mtx_unlock(&bpf_mtx);
1485
1486	*bp->bif_driverp = NULL;
1487
1488	/*
1489	 * Compute the length of the bpf header.  This is not necessarily
1490	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1491	 * that the network layer header begins on a longword boundary (for
1492	 * performance reasons and to alleviate alignment restrictions).
1493	 */
1494	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1495
1496	if (bootverbose)
1497		if_printf(ifp, "bpf attached\n");
1498}
1499
1500/*
1501 * Detach bpf from an interface.  This involves detaching each descriptor
1502 * associated with the interface, and leaving bd_bif NULL.  Notify each
1503 * descriptor as it's detached so that any sleepers wake up and get
1504 * ENXIO.
1505 */
1506void
1507bpfdetach(ifp)
1508	struct ifnet *ifp;
1509{
1510	struct bpf_if	*bp;
1511	struct bpf_d	*d;
1512
1513	/* Locate BPF interface information */
1514	mtx_lock(&bpf_mtx);
1515	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1516		if (ifp == bp->bif_ifp)
1517			break;
1518	}
1519
1520	/* Interface wasn't attached */
1521	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1522		mtx_unlock(&bpf_mtx);
1523		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1524		return;
1525	}
1526
1527	LIST_REMOVE(bp, bif_next);
1528	mtx_unlock(&bpf_mtx);
1529
1530	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1531		bpf_detachd(d);
1532		BPFD_LOCK(d);
1533		bpf_wakeup(d);
1534		BPFD_UNLOCK(d);
1535	}
1536
1537	mtx_destroy(&bp->bif_mtx);
1538	free(bp, M_BPF);
1539}
1540
1541/*
1542 * Get a list of available data link type of the interface.
1543 */
1544static int
1545bpf_getdltlist(d, bfl)
1546	struct bpf_d *d;
1547	struct bpf_dltlist *bfl;
1548{
1549	int n, error;
1550	struct ifnet *ifp;
1551	struct bpf_if *bp;
1552
1553	ifp = d->bd_bif->bif_ifp;
1554	n = 0;
1555	error = 0;
1556	mtx_lock(&bpf_mtx);
1557	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1558		if (bp->bif_ifp != ifp)
1559			continue;
1560		if (bfl->bfl_list != NULL) {
1561			if (n >= bfl->bfl_len) {
1562				mtx_unlock(&bpf_mtx);
1563				return (ENOMEM);
1564			}
1565			error = copyout(&bp->bif_dlt,
1566			    bfl->bfl_list + n, sizeof(u_int));
1567		}
1568		n++;
1569	}
1570	mtx_unlock(&bpf_mtx);
1571	bfl->bfl_len = n;
1572	return (error);
1573}
1574
1575/*
1576 * Set the data link type of a BPF instance.
1577 */
1578static int
1579bpf_setdlt(d, dlt)
1580	struct bpf_d *d;
1581	u_int dlt;
1582{
1583	int error, opromisc;
1584	struct ifnet *ifp;
1585	struct bpf_if *bp;
1586
1587	if (d->bd_bif->bif_dlt == dlt)
1588		return (0);
1589	ifp = d->bd_bif->bif_ifp;
1590	mtx_lock(&bpf_mtx);
1591	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1592		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1593			break;
1594	}
1595	mtx_unlock(&bpf_mtx);
1596	if (bp != NULL) {
1597		opromisc = d->bd_promisc;
1598		bpf_detachd(d);
1599		bpf_attachd(d, bp);
1600		BPFD_LOCK(d);
1601		reset_d(d);
1602		BPFD_UNLOCK(d);
1603		if (opromisc) {
1604			error = ifpromisc(bp->bif_ifp, 1);
1605			if (error)
1606				if_printf(bp->bif_ifp,
1607					"bpf_setdlt: ifpromisc failed (%d)\n",
1608					error);
1609			else
1610				d->bd_promisc = 1;
1611		}
1612	}
1613	return (bp == NULL ? EINVAL : 0);
1614}
1615
1616static void
1617bpf_clone(arg, name, namelen, dev)
1618	void *arg;
1619	char *name;
1620	int namelen;
1621	struct cdev **dev;
1622{
1623	int u;
1624
1625	if (*dev != NULL)
1626		return;
1627	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1628		return;
1629	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1630	    "bpf%d", u);
1631	dev_ref(*dev);
1632	(*dev)->si_flags |= SI_CHEAPCLONE;
1633	return;
1634}
1635
1636static void
1637bpf_drvinit(unused)
1638	void *unused;
1639{
1640
1641	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1642	LIST_INIT(&bpf_iflist);
1643	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1644}
1645
1646static void
1647bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
1648{
1649
1650	bzero(d, sizeof(*d));
1651	BPFD_LOCK_ASSERT(bd);
1652	d->bd_immediate = bd->bd_immediate;
1653	d->bd_promisc = bd->bd_promisc;
1654	d->bd_hdrcmplt = bd->bd_hdrcmplt;
1655	d->bd_seesent = bd->bd_seesent;
1656	d->bd_async = bd->bd_async;
1657	d->bd_rcount = bd->bd_rcount;
1658	d->bd_dcount = bd->bd_dcount;
1659	d->bd_fcount = bd->bd_fcount;
1660	d->bd_sig = bd->bd_sig;
1661	d->bd_slen = bd->bd_slen;
1662	d->bd_hlen = bd->bd_hlen;
1663	d->bd_bufsize = bd->bd_bufsize;
1664	d->bd_pid = bd->bd_pid;
1665	strlcpy(d->bd_ifname,
1666	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
1667	strlcpy(d->bd_pcomm, bd->bd_pcomm, MAXCOMLEN);
1668}
1669
1670static int
1671bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
1672{
1673	struct xbpf_d *xbdbuf, *xbd;
1674	int index, error;
1675	struct bpf_if *bp;
1676	struct bpf_d *bd;
1677
1678	/*
1679	 * XXX This is not technically correct. It is possible for non
1680	 * privileged users to open bpf devices. It would make sense
1681	 * if the users who opened the devices were able to retrieve
1682	 * the statistics for them, too.
1683	 */
1684	error = suser(req->td);
1685	if (error)
1686		return (error);
1687	if (req->oldptr == NULL)
1688		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
1689	if (bpf_bpfd_cnt == 0)
1690		return (SYSCTL_OUT(req, 0, 0));
1691	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
1692	mtx_lock(&bpf_mtx);
1693	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
1694		mtx_unlock(&bpf_mtx);
1695		free(xbdbuf, M_BPF);
1696		return (ENOMEM);
1697	}
1698	index = 0;
1699	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1700		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
1701			xbd = &xbdbuf[index++];
1702			BPFD_LOCK(bd);
1703			bpfstats_fill_xbpf(xbd, bd);
1704			BPFD_UNLOCK(bd);
1705		}
1706	}
1707	mtx_unlock(&bpf_mtx);
1708	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
1709	free(xbdbuf, M_BPF);
1710	return (error);
1711}
1712
1713SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
1714
1715#else /* !DEV_BPF && !NETGRAPH_BPF */
1716/*
1717 * NOP stubs to allow bpf-using drivers to load and function.
1718 *
1719 * A 'better' implementation would allow the core bpf functionality
1720 * to be loaded at runtime.
1721 */
1722
1723void
1724bpf_tap(bp, pkt, pktlen)
1725	struct bpf_if *bp;
1726	u_char *pkt;
1727	u_int pktlen;
1728{
1729}
1730
1731void
1732bpf_mtap(bp, m)
1733	struct bpf_if *bp;
1734	struct mbuf *m;
1735{
1736}
1737
1738void
1739bpf_mtap2(bp, d, l, m)
1740	struct bpf_if *bp;
1741	void *d;
1742	u_int l;
1743	struct mbuf *m;
1744{
1745}
1746
1747void
1748bpfattach(ifp, dlt, hdrlen)
1749	struct ifnet *ifp;
1750	u_int dlt, hdrlen;
1751{
1752}
1753
1754void
1755bpfattach2(ifp, dlt, hdrlen, driverp)
1756	struct ifnet *ifp;
1757	u_int dlt, hdrlen;
1758	struct bpf_if **driverp;
1759{
1760}
1761
1762void
1763bpfdetach(ifp)
1764	struct ifnet *ifp;
1765{
1766}
1767
1768u_int
1769bpf_filter(pc, p, wirelen, buflen)
1770	const struct bpf_insn *pc;
1771	u_char *p;
1772	u_int wirelen;
1773	u_int buflen;
1774{
1775	return -1;	/* "no filter" behaviour */
1776}
1777
1778int
1779bpf_validate(f, len)
1780	const struct bpf_insn *f;
1781	int len;
1782{
1783	return 0;		/* false */
1784}
1785
1786#endif /* !DEV_BPF && !NETGRAPH_BPF */
1787