2 * Copyright (c) 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)bpf.c 8.2 (Berkeley) 3/28/94
40 * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.12 2002/04/14 21:41:48 luigi Exp $
41 * $DragonFly: src/sys/net/bpf.c,v 1.29 2005/11/28 17:13:45 dillon Exp $
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/malloc.h>
53 #include <sys/signalvar.h>
54 #include <sys/filio.h>
55 #include <sys/sockio.h>
56 #include <sys/ttycom.h>
57 #include <sys/filedesc.h>
61 #include <sys/socket.h>
62 #include <sys/vnode.h>
64 #include <sys/thread2.h>
68 #include <net/bpfdesc.h>
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
72 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
75 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
80 * The default read buffer size is patchable.
82 static int bpf_bufsize = BPF_DEFAULTBUFSIZE;
83 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
85 static int bpf_maxbufsize = BPF_MAXBUFSIZE;
86 SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
87 &bpf_maxbufsize, 0, "");
90 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
92 static struct bpf_if *bpf_iflist;
94 static int bpf_allocbufs(struct bpf_d *);
95 static void bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
96 static void bpf_detachd(struct bpf_d *d);
97 static void bpf_freed(struct bpf_d *);
98 static void bpf_mcopy(const void *, void *, size_t);
99 static int bpf_movein(struct uio *, int, struct mbuf **,
100 struct sockaddr *, int *);
101 static int bpf_setif(struct bpf_d *, struct ifreq *);
102 static void bpf_timed_out(void *);
103 static void bpf_wakeup(struct bpf_d *);
104 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int,
105 void (*)(const void *, void *, size_t));
106 static void reset_d(struct bpf_d *);
107 static int bpf_setf(struct bpf_d *, struct bpf_program *);
108 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
109 static int bpf_setdlt(struct bpf_d *, u_int);
110 static void bpf_drvinit(void *unused);
112 static d_open_t bpfopen;
113 static d_close_t bpfclose;
114 static d_read_t bpfread;
115 static d_write_t bpfwrite;
116 static d_ioctl_t bpfioctl;
117 static d_poll_t bpfpoll;
119 #define CDEV_MAJOR 23
120 static struct cdevsw bpf_cdevsw = {
122 /* maj */ CDEV_MAJOR,
128 /* close */ bpfclose,
130 /* write */ bpfwrite,
131 /* ioctl */ bpfioctl,
134 /* strategy */ nostrategy,
141 bpf_movein(struct uio *uio, int linktype, struct mbuf **mp,
142 struct sockaddr *sockp, int *datlen)
150 * Build a sockaddr based on the data link layer type.
151 * We do this at this level because the ethernet header
152 * is copied directly into the data field of the sockaddr.
153 * In the case of SLIP, there is no header and the packet
154 * is forwarded as is.
155 * Also, we are careful to leave room at the front of the mbuf
156 * for the link level header.
161 sockp->sa_family = AF_INET;
166 sockp->sa_family = AF_UNSPEC;
167 /* XXX Would MAXLINKHDR be better? */
168 hlen = sizeof(struct ether_header);
172 sockp->sa_family = AF_IMPLINK;
178 sockp->sa_family = AF_UNSPEC;
182 case DLT_ATM_RFC1483:
184 * en atm driver requires 4-byte atm pseudo header.
185 * though it isn't standard, vpi:vci needs to be
188 sockp->sa_family = AF_UNSPEC;
189 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
193 sockp->sa_family = AF_UNSPEC;
194 hlen = 4; /* This should match PPP_HDRLEN */
201 len = uio->uio_resid;
202 *datlen = len - hlen;
203 if ((unsigned)len > MCLBYTES)
206 MGETHDR(m, MB_WAIT, MT_DATA);
211 if (!(m->m_flags & M_EXT)) {
216 m->m_pkthdr.len = m->m_len = len;
217 m->m_pkthdr.rcvif = NULL;
220 * Make room for link header.
223 m->m_pkthdr.len -= hlen;
225 m->m_data += hlen; /* XXX */
226 error = uiomove(sockp->sa_data, hlen, uio);
230 error = uiomove(mtod(m, caddr_t), len - hlen, uio);
239 * Attach file to the bpf interface, i.e. make d listen on bp.
240 * Must be called at splimp.
243 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
246 * Point d at bp, and add d to the interface's list of listeners.
247 * Finally, point the driver's bpf cookie at the interface so
248 * it will divert packets to bpf.
251 SLIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
252 *bp->bif_driverp = bp;
256 * Detach a file from its interface.
259 bpf_detachd(struct bpf_d *d)
268 /* Remove d from the interface's descriptor list. */
269 SLIST_REMOVE(&bp->bif_dlist, d, bpf_d, bd_next);
271 if (SLIST_EMPTY(&bp->bif_dlist)) {
273 * Let the driver know that there are no more listeners.
275 *bp->bif_driverp = NULL;
279 * Check if this descriptor had requested promiscuous mode.
280 * If so, turn it off.
284 error = ifpromisc(ifp, 0);
285 if (error != 0 && error != ENXIO) {
287 * ENXIO can happen if a pccard is unplugged,
288 * Something is really wrong if we were able to put
289 * the driver into promiscuous mode, but can't
292 if_printf(ifp, "bpf_detach: ifpromisc failed(%d)\n",
299 * Open ethernet device. Returns ENXIO for illegal minor device number,
300 * EBUSY if file is open by another process.
304 bpfopen(dev_t dev, int flags, int fmt, struct thread *td)
307 struct proc *p = td->td_proc;
311 if (p->p_ucred->cr_prison)
316 * Each minor can be opened by only one process. If the requested
317 * minor is in use, return EBUSY.
321 make_dev(&bpf_cdevsw, minor(dev), 0, 0, 0600, "bpf%d", lminor(dev));
322 MALLOC(d, struct bpf_d *, sizeof *d, M_BPF, M_WAITOK | M_ZERO);
324 d->bd_bufsize = bpf_bufsize;
327 callout_init(&d->bd_callout);
332 * Close the descriptor by detaching it from its interface,
333 * deallocating its buffers, and marking it free.
337 bpfclose(dev_t dev, int flags, int fmt, struct thread *td)
339 struct bpf_d *d = dev->si_drv1;
341 funsetown(d->bd_sigio);
343 if (d->bd_state == BPF_WAITING)
344 callout_stop(&d->bd_callout);
345 d->bd_state = BPF_IDLE;
346 if (d->bd_bif != NULL)
357 * Rotate the packet buffers in descriptor d. Move the store buffer
358 * into the hold slot, and the free buffer into the store slot.
359 * Zero the length of the new store buffer.
361 #define ROTATE_BUFFERS(d) \
362 (d)->bd_hbuf = (d)->bd_sbuf; \
363 (d)->bd_hlen = (d)->bd_slen; \
364 (d)->bd_sbuf = (d)->bd_fbuf; \
368 * bpfread - read next chunk of packets from buffers
371 bpfread(dev_t dev, struct uio *uio, int ioflag)
373 struct bpf_d *d = dev->si_drv1;
378 * Restrict application to use a buffer the same size as
381 if (uio->uio_resid != d->bd_bufsize)
385 if (d->bd_state == BPF_WAITING)
386 callout_stop(&d->bd_callout);
387 timed_out = (d->bd_state == BPF_TIMED_OUT);
388 d->bd_state = BPF_IDLE;
390 * If the hold buffer is empty, then do a timed sleep, which
391 * ends when the timeout expires or when enough packets
392 * have arrived to fill the store buffer.
394 while (d->bd_hbuf == NULL) {
395 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
397 * A packet(s) either arrived since the previous
398 * read or arrived while we were asleep.
399 * Rotate the buffers and return what's here.
406 * No data is available, check to see if the bpf device
407 * is still pointed at a real interface. If not, return
408 * ENXIO so that the userland process knows to rebind
409 * it before using it again.
411 if (d->bd_bif == NULL) {
416 if (ioflag & IO_NDELAY) {
420 error = tsleep(d, PCATCH, "bpf", d->bd_rtout);
421 if (error == EINTR || error == ERESTART) {
425 if (error == EWOULDBLOCK) {
427 * On a timeout, return what's in the buffer,
428 * which may be nothing. If there is something
429 * in the store buffer, we can rotate the buffers.
433 * We filled up the buffer in between
434 * getting the timeout and arriving
435 * here, so we don't need to rotate.
439 if (d->bd_slen == 0) {
448 * At this point, we know we have something in the hold slot.
453 * Move data from hold buffer into user space.
454 * We know the entire buffer is transferred since
455 * we checked above that the read buffer is bpf_bufsize bytes.
457 error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
460 d->bd_fbuf = d->bd_hbuf;
470 * If there are processes sleeping on this descriptor, wake them up.
473 bpf_wakeup(struct bpf_d *d)
475 if (d->bd_state == BPF_WAITING) {
476 callout_stop(&d->bd_callout);
477 d->bd_state = BPF_IDLE;
480 if (d->bd_async && d->bd_sig && d->bd_sigio)
481 pgsigio(d->bd_sigio, d->bd_sig, 0);
484 selwakeup(&d->bd_sel);
487 d->bd_sel.si_pid = 0;
491 bpf_timed_out(void *arg)
493 struct bpf_d *d = (struct bpf_d *)arg;
496 if (d->bd_state == BPF_WAITING) {
497 d->bd_state = BPF_TIMED_OUT;
505 bpfwrite(dev_t dev, struct uio *uio, int ioflag)
507 struct bpf_d *d = dev->si_drv1;
511 static struct sockaddr dst;
514 if (d->bd_bif == NULL)
517 ifp = d->bd_bif->bif_ifp;
519 if (uio->uio_resid == 0)
522 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
526 if (datlen > ifp->if_mtu) {
532 dst.sa_family = pseudo_AF_HDRCMPLT;
535 lwkt_serialize_enter(ifp->if_serializer);
536 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)NULL);
537 lwkt_serialize_exit(ifp->if_serializer);
540 * The driver frees the mbuf.
546 * Reset a descriptor by flushing its packet buffer and clearing the
547 * receive and drop counts. Should be called at splimp.
550 reset_d(struct bpf_d *d)
553 /* Free the hold buffer. */
554 d->bd_fbuf = d->bd_hbuf;
564 * FIONREAD Check for read packet available.
565 * SIOCGIFADDR Get interface address - convenient hook to driver.
566 * BIOCGBLEN Get buffer len [for read()].
567 * BIOCSETF Set ethernet read filter.
568 * BIOCFLUSH Flush read packet buffer.
569 * BIOCPROMISC Put interface into promiscuous mode.
570 * BIOCGDLT Get link layer type.
571 * BIOCGETIF Get interface name.
572 * BIOCSETIF Set interface.
573 * BIOCSRTIMEOUT Set read timeout.
574 * BIOCGRTIMEOUT Get read timeout.
575 * BIOCGSTATS Get packet stats.
576 * BIOCIMMEDIATE Set immediate mode.
577 * BIOCVERSION Get filter language version.
578 * BIOCGHDRCMPLT Get "header already complete" flag
579 * BIOCSHDRCMPLT Set "header already complete" flag
580 * BIOCGSEESENT Get "see packets sent" flag
581 * BIOCSSEESENT Set "see packets sent" flag
585 bpfioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
587 struct bpf_d *d = dev->si_drv1;
591 if (d->bd_state == BPF_WAITING)
592 callout_stop(&d->bd_callout);
593 d->bd_state = BPF_IDLE;
603 * Check for read packet available.
623 if (d->bd_bif == NULL)
626 ifp = d->bd_bif->bif_ifp;
627 lwkt_serialize_enter(ifp->if_serializer);
628 error = ifp->if_ioctl(ifp, cmd, addr,
629 td->td_proc->p_ucred);
630 lwkt_serialize_exit(ifp->if_serializer);
636 * Get buffer len [for read()].
639 *(u_int *)addr = d->bd_bufsize;
649 u_int size = *(u_int *)addr;
651 if (size > bpf_maxbufsize)
652 *(u_int *)addr = size = bpf_maxbufsize;
653 else if (size < BPF_MINBUFSIZE)
654 *(u_int *)addr = size = BPF_MINBUFSIZE;
655 d->bd_bufsize = size;
660 * Set link layer read filter.
663 error = bpf_setf(d, (struct bpf_program *)addr);
667 * Flush read packet buffer.
676 * Put interface into promiscuous mode.
679 if (d->bd_bif == NULL) {
681 * No interface attached yet.
687 if (d->bd_promisc == 0) {
688 error = ifpromisc(d->bd_bif->bif_ifp, 1);
696 * Get device parameters.
699 if (d->bd_bif == NULL)
702 *(u_int *)addr = d->bd_bif->bif_dlt;
706 * Get a list of supported data link types.
709 if (d->bd_bif == NULL)
712 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
716 * Set data link type.
719 if (d->bd_bif == NULL)
722 error = bpf_setdlt(d, *(u_int *)addr);
726 * Get interface name.
729 if (d->bd_bif == NULL) {
732 struct ifnet *const ifp = d->bd_bif->bif_ifp;
733 struct ifreq *const ifr = (struct ifreq *)addr;
735 strlcpy(ifr->ifr_name, ifp->if_xname,
736 sizeof ifr->ifr_name);
744 error = bpf_setif(d, (struct ifreq *)addr);
752 struct timeval *tv = (struct timeval *)addr;
755 * Subtract 1 tick from tvtohz() since this isn't
758 if ((error = itimerfix(tv)) == 0)
759 d->bd_rtout = tvtohz_low(tv);
768 struct timeval *tv = (struct timeval *)addr;
770 tv->tv_sec = d->bd_rtout / hz;
771 tv->tv_usec = (d->bd_rtout % hz) * tick;
780 struct bpf_stat *bs = (struct bpf_stat *)addr;
782 bs->bs_recv = d->bd_rcount;
783 bs->bs_drop = d->bd_dcount;
788 * Set immediate mode.
791 d->bd_immediate = *(u_int *)addr;
796 struct bpf_version *bv = (struct bpf_version *)addr;
798 bv->bv_major = BPF_MAJOR_VERSION;
799 bv->bv_minor = BPF_MINOR_VERSION;
804 * Get "header already complete" flag
807 *(u_int *)addr = d->bd_hdrcmplt;
811 * Set "header already complete" flag
814 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
818 * Get "see sent packets" flag
821 *(u_int *)addr = d->bd_seesent;
825 * Set "see sent packets" flag
828 d->bd_seesent = *(u_int *)addr;
831 case FIONBIO: /* Non-blocking I/O */
834 case FIOASYNC: /* Send signal on receive packets */
835 d->bd_async = *(int *)addr;
839 error = fsetown(*(int *)addr, &d->bd_sigio);
843 *(int *)addr = fgetown(d->bd_sigio);
846 /* This is deprecated, FIOSETOWN should be used instead. */
848 error = fsetown(-(*(int *)addr), &d->bd_sigio);
851 /* This is deprecated, FIOGETOWN should be used instead. */
853 *(int *)addr = -fgetown(d->bd_sigio);
856 case BIOCSRSIG: /* Set receive signal */
860 sig = *(u_int *)addr;
869 *(u_int *)addr = d->bd_sig;
876 * Set d's packet filter program to fp. If this file already has a filter,
877 * free it and replace it. Returns EINVAL for bogus requests.
880 bpf_setf(struct bpf_d *d, struct bpf_program *fp)
882 struct bpf_insn *fcode, *old;
886 if (fp->bf_insns == NULL) {
898 if (flen > BPF_MAXINSNS)
901 size = flen * sizeof *fp->bf_insns;
902 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
903 if (copyin(fp->bf_insns, fcode, size) == 0 &&
904 bpf_validate(fcode, (int)flen)) {
906 d->bd_filter = fcode;
919 * Detach a file from its current interface (if attached at all) and attach
920 * to the interface indicated by the name stored in ifr.
921 * Return an errno or 0.
924 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
928 struct ifnet *theywant;
930 theywant = ifunit(ifr->ifr_name);
931 if (theywant == NULL)
935 * Look through attached interfaces for the named one.
937 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
938 struct ifnet *ifp = bp->bif_ifp;
940 if (ifp == NULL || ifp != theywant)
942 /* skip additional entry */
943 if (bp->bif_driverp != &ifp->if_bpf)
946 * We found the requested interface.
947 * If it's not up, return an error.
948 * Allocate the packet buffers if we need to.
949 * If we're already attached to requested interface,
950 * just flush the buffer.
952 if (!(ifp->if_flags & IFF_UP))
955 if (d->bd_sbuf == NULL) {
956 error = bpf_allocbufs(d);
961 if (bp != d->bd_bif) {
962 if (d->bd_bif != NULL) {
964 * Detach if attached to something else.
981 * Support for select() and poll() system calls
983 * Return true iff the specific operation will not block indefinitely.
984 * Otherwise, return false but make a note that a selwakeup() must be done.
987 bpfpoll(dev_t dev, int events, struct thread *td)
993 if (d->bd_bif == NULL)
996 revents = events & (POLLOUT | POLLWRNORM);
998 if (events & (POLLIN | POLLRDNORM)) {
1000 * An imitation of the FIONREAD ioctl code.
1001 * XXX not quite. An exact imitation:
1002 * if (d->b_slen != 0 ||
1003 * (d->bd_hbuf != NULL && d->bd_hlen != 0)
1005 if (d->bd_hlen != 0 ||
1006 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1008 revents |= events & (POLLIN | POLLRDNORM);
1010 selrecord(td, &d->bd_sel);
1011 /* Start the read timeout if necessary. */
1012 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1013 callout_reset(&d->bd_callout, d->bd_rtout,
1015 d->bd_state = BPF_WAITING;
1024 * Process the packet pkt of length pktlen. The packet is parsed
1025 * by each listener's filter, and if accepted, stashed into the
1026 * corresponding buffer.
1029 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1035 * Note that the ipl does not have to be raised at this point.
1036 * The only problem that could arise here is that if two different
1037 * interfaces shared any data. This is not the case.
1039 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1041 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1043 catchpacket(d, pkt, pktlen, slen, ovbcopy);
1048 * Copy data from an mbuf chain into a buffer. This code is derived
1049 * from m_copydata in sys/uipc_mbuf.c.
1052 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1054 const struct mbuf *m;
1063 count = min(m->m_len, len);
1064 bcopy(mtod(m, void *), dst, count);
1072 * Process the packet in the mbuf chain m. The packet is parsed by each
1073 * listener's filter, and if accepted, stashed into the corresponding
1077 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1083 /* Don't compute pktlen, if no descriptor is attached. */
1084 if (SLIST_EMPTY(&bp->bif_dlist))
1088 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1089 pktlen += m0->m_len;
1091 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1092 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1095 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1097 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1102 bpf_mtap_family(struct bpf_if *bp, struct mbuf *m, sa_family_t family)
1106 KKASSERT(family != AF_UNSPEC);
1108 family4 = (u_int)family;
1109 bpf_ptap(bp, m, &family4, sizeof(family4));
1113 * Process the packet in the mbuf chain m with the header in m prepended.
1114 * The packet is parsed by each listener's filter, and if accepted,
1115 * stashed into the corresponding buffer.
1118 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen)
1123 * Craft on-stack mbuf suitable for passing to bpf_mtap.
1124 * Note that we cut corners here; we only setup what's
1125 * absolutely needed--this mbuf should never go anywhere else.
1128 mb.m_data = __DECONST(void *, data); /* LINTED */
1135 * Move the packet data from interface memory (pkt) into the
1136 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1137 * otherwise 0. "copy" is the routine called to do the actual data
1138 * transfer. bcopy is passed in to copy contiguous chunks, while
1139 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1140 * pkt is really an mbuf.
1143 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1144 void (*cpfn)(const void *, void *, size_t))
1148 int hdrlen = d->bd_bif->bif_hdrlen;
1150 * Figure out how many bytes to move. If the packet is
1151 * greater or equal to the snapshot length, transfer that
1152 * much. Otherwise, transfer the whole packet (unless
1153 * we hit the buffer size limit).
1155 totlen = hdrlen + min(snaplen, pktlen);
1156 if (totlen > d->bd_bufsize)
1157 totlen = d->bd_bufsize;
1160 * Round up the end of the previous packet to the next longword.
1162 curlen = BPF_WORDALIGN(d->bd_slen);
1163 if (curlen + totlen > d->bd_bufsize) {
1165 * This packet will overflow the storage buffer.
1166 * Rotate the buffers if we can, then wakeup any
1169 if (d->bd_fbuf == NULL) {
1171 * We haven't completed the previous read yet,
1172 * so drop the packet.
1181 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1183 * Immediate mode is set, or the read timeout has
1184 * already expired during a select call. A packet
1185 * arrived, so the reader should be woken up.
1190 * Append the bpf header.
1192 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1193 microtime(&hp->bh_tstamp);
1194 hp->bh_datalen = pktlen;
1195 hp->bh_hdrlen = hdrlen;
1197 * Copy the packet data into the store buffer and update its length.
1199 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1200 d->bd_slen = curlen + totlen;
1204 * Initialize all nonzero fields of a descriptor.
1207 bpf_allocbufs(struct bpf_d *d)
1209 d->bd_fbuf = malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1210 if (d->bd_fbuf == NULL)
1213 d->bd_sbuf = malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1214 if (d->bd_sbuf == NULL) {
1215 free(d->bd_fbuf, M_BPF);
1224 * Free buffers currently in use by a descriptor.
1228 bpf_freed(struct bpf_d *d)
1231 * We don't need to lock out interrupts since this descriptor has
1232 * been detached from its interface and it yet hasn't been marked
1235 if (d->bd_sbuf != NULL) {
1236 free(d->bd_sbuf, M_BPF);
1237 if (d->bd_hbuf != NULL)
1238 free(d->bd_hbuf, M_BPF);
1239 if (d->bd_fbuf != NULL)
1240 free(d->bd_fbuf, M_BPF);
1243 free(d->bd_filter, M_BPF);
1247 * Attach an interface to bpf. ifp is a pointer to the structure
1248 * defining the interface to be attached, dlt is the link layer type,
1249 * and hdrlen is the fixed size of the link header (variable length
1250 * headers are not yet supported).
1253 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1255 bpfattach_dlt(ifp, dlt, hdrlen, &ifp->if_bpf);
1259 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1263 bp = malloc(sizeof *bp, M_BPF, M_WAITOK | M_ZERO);
1265 SLIST_INIT(&bp->bif_dlist);
1268 bp->bif_driverp = driverp;
1269 *bp->bif_driverp = NULL;
1271 bp->bif_next = bpf_iflist;
1275 * Compute the length of the bpf header. This is not necessarily
1276 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1277 * that the network layer header begins on a longword boundary (for
1278 * performance reasons and to alleviate alignment restrictions).
1280 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1283 if_printf(ifp, "bpf attached\n");
1287 * Detach bpf from an interface. This involves detaching each descriptor
1288 * associated with the interface, and leaving bd_bif NULL. Notify each
1289 * descriptor as it's detached so that any sleepers wake up and get
1293 bpfdetach(struct ifnet *ifp)
1295 struct bpf_if *bp, *bp_prev;
1300 /* Locate BPF interface information */
1302 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1303 if (ifp == bp->bif_ifp)
1308 /* Interface wasn't attached */
1309 if (bp->bif_ifp == NULL) {
1311 printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1315 while ((d = SLIST_FIRST(&bp->bif_dlist)) != NULL) {
1320 if (bp_prev != NULL)
1321 bp_prev->bif_next = bp->bif_next;
1323 bpf_iflist = bp->bif_next;
1331 * Get a list of available data link type of the interface.
1334 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1340 ifp = d->bd_bif->bif_ifp;
1343 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
1344 if (bp->bif_ifp != ifp)
1346 if (bfl->bfl_list != NULL) {
1347 if (n >= bfl->bfl_len) {
1350 error = copyout(&bp->bif_dlt,
1351 bfl->bfl_list + n, sizeof(u_int));
1360 * Set the data link type of a BPF instance.
1363 bpf_setdlt(struct bpf_d *d, u_int dlt)
1365 int error, opromisc;
1369 if (d->bd_bif->bif_dlt == dlt)
1371 ifp = d->bd_bif->bif_ifp;
1372 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
1373 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1377 opromisc = d->bd_promisc;
1383 error = ifpromisc(bp->bif_ifp, 1);
1385 if_printf(bp->bif_ifp,
1386 "bpf_setdlt: ifpromisc failed (%d)\n",
1393 return(bp == NULL ? EINVAL : 0);
1397 bpf_drvinit(void *unused)
1399 cdevsw_add(&bpf_cdevsw, 0, 0);
1402 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1406 * NOP stubs to allow bpf-using drivers to load and function.
1408 * A 'better' implementation would allow the core bpf functionality
1409 * to be loaded at runtime.
1413 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1418 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1423 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen)
1428 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1433 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1438 bpfdetach(struct ifnet *ifp)
1443 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1445 return -1; /* "no filter" behaviour */