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.2 2003/06/17 04:28:47 dillon Exp $
49 #define inline __inline
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <sys/malloc.h>
59 #include <sys/signalvar.h>
60 #include <sys/filio.h>
61 #include <sys/sockio.h>
62 #include <sys/ttycom.h>
63 #include <sys/filedesc.h>
65 #if defined(sparc) && BSD < 199103
66 #include <sys/stream.h>
70 #include <sys/socket.h>
71 #include <sys/vnode.h>
75 #include <net/bpfdesc.h>
77 #include <netinet/in.h>
78 #include <netinet/if_ether.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
82 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
87 * Older BSDs don't have kernel malloc.
91 static caddr_t bpf_alloc();
92 #include <net/bpf_compat.h>
93 #define BPF_BUFSIZE (MCLBYTES-8)
94 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
96 #define BPF_BUFSIZE 4096
97 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
100 #define PRINET 26 /* interruptible */
103 * The default read buffer size is patchable.
105 static int bpf_bufsize = BPF_BUFSIZE;
106 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
107 &bpf_bufsize, 0, "");
108 static int bpf_maxbufsize = BPF_MAXBUFSIZE;
109 SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
110 &bpf_maxbufsize, 0, "");
113 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
115 static struct bpf_if *bpf_iflist;
117 static int bpf_allocbufs __P((struct bpf_d *));
118 static void bpf_attachd __P((struct bpf_d *d, struct bpf_if *bp));
119 static void bpf_detachd __P((struct bpf_d *d));
120 static void bpf_freed __P((struct bpf_d *));
121 static void bpf_mcopy __P((const void *, void *, size_t));
122 static int bpf_movein __P((struct uio *, int,
123 struct mbuf **, struct sockaddr *, int *));
124 static int bpf_setif __P((struct bpf_d *, struct ifreq *));
125 static void bpf_timed_out __P((void *));
127 bpf_wakeup __P((struct bpf_d *));
128 static void catchpacket __P((struct bpf_d *, u_char *, u_int,
129 u_int, void (*)(const void *, void *, size_t)));
130 static void reset_d __P((struct bpf_d *));
131 static int bpf_setf __P((struct bpf_d *, struct bpf_program *));
133 static d_open_t bpfopen;
134 static d_close_t bpfclose;
135 static d_read_t bpfread;
136 static d_write_t bpfwrite;
137 static d_ioctl_t bpfioctl;
138 static d_poll_t bpfpoll;
140 #define CDEV_MAJOR 23
141 static struct cdevsw bpf_cdevsw = {
143 /* close */ bpfclose,
145 /* write */ bpfwrite,
146 /* ioctl */ bpfioctl,
149 /* strategy */ nostrategy,
151 /* maj */ CDEV_MAJOR,
160 bpf_movein(uio, linktype, mp, sockp, datlen)
161 register struct uio *uio;
162 int linktype, *datlen;
163 register struct mbuf **mp;
164 register struct sockaddr *sockp;
172 * Build a sockaddr based on the data link layer type.
173 * We do this at this level because the ethernet header
174 * is copied directly into the data field of the sockaddr.
175 * In the case of SLIP, there is no header and the packet
176 * is forwarded as is.
177 * Also, we are careful to leave room at the front of the mbuf
178 * for the link level header.
183 sockp->sa_family = AF_INET;
188 sockp->sa_family = AF_UNSPEC;
189 /* XXX Would MAXLINKHDR be better? */
190 hlen = sizeof(struct ether_header);
194 #if defined(__FreeBSD__) || defined(__bsdi__)
195 sockp->sa_family = AF_IMPLINK;
198 sockp->sa_family = AF_UNSPEC;
199 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
206 sockp->sa_family = AF_UNSPEC;
211 case DLT_ATM_RFC1483:
213 * en atm driver requires 4-byte atm pseudo header.
214 * though it isn't standard, vpi:vci needs to be
217 sockp->sa_family = AF_UNSPEC;
218 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
222 sockp->sa_family = AF_UNSPEC;
223 hlen = 4; /* This should match PPP_HDRLEN */
230 len = uio->uio_resid;
231 *datlen = len - hlen;
232 if ((unsigned)len > MCLBYTES)
235 MGETHDR(m, M_WAIT, MT_DATA);
241 if ((m->m_flags & M_EXT) == 0) {
244 if (m->m_len != MCLBYTES) {
250 m->m_pkthdr.len = m->m_len = len;
251 m->m_pkthdr.rcvif = NULL;
254 * Make room for link header.
257 m->m_pkthdr.len -= hlen;
260 m->m_data += hlen; /* XXX */
264 error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio);
268 error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio);
277 * Attach file to the bpf interface, i.e. make d listen on bp.
278 * Must be called at splimp.
286 * Point d at bp, and add d to the interface's list of listeners.
287 * Finally, point the driver's bpf cookie at the interface so
288 * it will divert packets to bpf.
291 d->bd_next = bp->bif_dlist;
294 bp->bif_ifp->if_bpf = bp;
298 * Detach a file from its interface.
309 * Check if this descriptor had requested promiscuous mode.
310 * If so, turn it off.
314 if (ifpromisc(bp->bif_ifp, 0))
316 * Something is really wrong if we were able to put
317 * the driver into promiscuous mode, but can't
320 panic("bpf: ifpromisc failed");
322 /* Remove d from the interface's descriptor list. */
327 panic("bpf_detachd: descriptor not in list");
330 if (bp->bif_dlist == 0)
332 * Let the driver know that there are no more listeners.
334 d->bd_bif->bif_ifp->if_bpf = 0;
339 * Open ethernet device. Returns ENXIO for illegal minor device number,
340 * EBUSY if file is open by another process.
344 bpfopen(dev, flags, fmt, p)
350 register struct bpf_d *d;
357 * Each minor can be opened by only one process. If the requested
358 * minor is in use, return EBUSY.
362 make_dev(&bpf_cdevsw, minor(dev), 0, 0, 0600, "bpf%d", lminor(dev));
363 MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
365 d->bd_bufsize = bpf_bufsize;
368 callout_init(&d->bd_callout);
373 * Close the descriptor by detaching it from its interface,
374 * deallocating its buffers, and marking it free.
378 bpfclose(dev, flags, fmt, p)
384 register struct bpf_d *d = dev->si_drv1;
387 funsetown(d->bd_sigio);
389 if (d->bd_state == BPF_WAITING)
390 callout_stop(&d->bd_callout);
391 d->bd_state = BPF_IDLE;
403 * Support for SunOS, which does not have tsleep.
410 struct bpf_d *d = (struct bpf_d *)arg;
415 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
419 register struct bpf_d *d;
421 register int rto = d->bd_rtout;
426 timeout(bpf_timeout, (caddr_t)d, rto);
428 st = sleep((caddr_t)d, PRINET|PCATCH);
430 if (d->bd_timedout == 0)
431 untimeout(bpf_timeout, (caddr_t)d);
435 return (st != 0) ? EINTR : 0;
438 #define BPF_SLEEP tsleep
442 * Rotate the packet buffers in descriptor d. Move the store buffer
443 * into the hold slot, and the free buffer into the store slot.
444 * Zero the length of the new store buffer.
446 #define ROTATE_BUFFERS(d) \
447 (d)->bd_hbuf = (d)->bd_sbuf; \
448 (d)->bd_hlen = (d)->bd_slen; \
449 (d)->bd_sbuf = (d)->bd_fbuf; \
453 * bpfread - read next chunk of packets from buffers
456 bpfread(dev, uio, ioflag)
458 register struct uio *uio;
461 register struct bpf_d *d = dev->si_drv1;
467 * Restrict application to use a buffer the same size as
470 if (uio->uio_resid != d->bd_bufsize)
474 if (d->bd_state == BPF_WAITING)
475 callout_stop(&d->bd_callout);
476 timed_out = (d->bd_state == BPF_TIMED_OUT);
477 d->bd_state = BPF_IDLE;
479 * If the hold buffer is empty, then do a timed sleep, which
480 * ends when the timeout expires or when enough packets
481 * have arrived to fill the store buffer.
483 while (d->bd_hbuf == 0) {
484 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
486 * A packet(s) either arrived since the previous
487 * read or arrived while we were asleep.
488 * Rotate the buffers and return what's here.
495 * No data is available, check to see if the bpf device
496 * is still pointed at a real interface. If not, return
497 * ENXIO so that the userland process knows to rebind
498 * it before using it again.
500 if (d->bd_bif == NULL) {
505 if (ioflag & IO_NDELAY) {
507 return (EWOULDBLOCK);
509 error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf",
511 if (error == EINTR || error == ERESTART) {
515 if (error == EWOULDBLOCK) {
517 * On a timeout, return what's in the buffer,
518 * which may be nothing. If there is something
519 * in the store buffer, we can rotate the buffers.
523 * We filled up the buffer in between
524 * getting the timeout and arriving
525 * here, so we don't need to rotate.
529 if (d->bd_slen == 0) {
538 * At this point, we know we have something in the hold slot.
543 * Move data from hold buffer into user space.
544 * We know the entire buffer is transferred since
545 * we checked above that the read buffer is bpf_bufsize bytes.
547 error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
550 d->bd_fbuf = d->bd_hbuf;
560 * If there are processes sleeping on this descriptor, wake them up.
564 register struct bpf_d *d;
566 if (d->bd_state == BPF_WAITING) {
567 callout_stop(&d->bd_callout);
568 d->bd_state = BPF_IDLE;
571 if (d->bd_async && d->bd_sig && d->bd_sigio)
572 pgsigio(d->bd_sigio, d->bd_sig, 0);
575 selwakeup(&d->bd_sel);
577 d->bd_sel.si_pid = 0;
580 selwakeup(d->bd_selproc, (int)d->bd_selcoll);
591 struct bpf_d *d = (struct bpf_d *)arg;
595 if (d->bd_state == BPF_WAITING) {
596 d->bd_state = BPF_TIMED_OUT;
604 bpfwrite(dev, uio, ioflag)
609 register struct bpf_d *d = dev->si_drv1;
613 static struct sockaddr dst;
619 ifp = d->bd_bif->bif_ifp;
621 if (uio->uio_resid == 0)
624 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
628 if (datlen > ifp->if_mtu)
632 dst.sa_family = pseudo_AF_HDRCMPLT;
636 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
638 error = (*ifp->if_output)(ifp, m, &dst);
642 * The driver frees the mbuf.
648 * Reset a descriptor by flushing its packet buffer and clearing the
649 * receive and drop counts. Should be called at splimp.
656 /* Free the hold buffer. */
657 d->bd_fbuf = d->bd_hbuf;
667 * FIONREAD Check for read packet available.
668 * SIOCGIFADDR Get interface address - convenient hook to driver.
669 * BIOCGBLEN Get buffer len [for read()].
670 * BIOCSETF Set ethernet read filter.
671 * BIOCFLUSH Flush read packet buffer.
672 * BIOCPROMISC Put interface into promiscuous mode.
673 * BIOCGDLT Get link layer type.
674 * BIOCGETIF Get interface name.
675 * BIOCSETIF Set interface.
676 * BIOCSRTIMEOUT Set read timeout.
677 * BIOCGRTIMEOUT Get read timeout.
678 * BIOCGSTATS Get packet stats.
679 * BIOCIMMEDIATE Set immediate mode.
680 * BIOCVERSION Get filter language version.
681 * BIOCGHDRCMPLT Get "header already complete" flag
682 * BIOCSHDRCMPLT Set "header already complete" flag
683 * BIOCGSEESENT Get "see packets sent" flag
684 * BIOCSSEESENT Set "see packets sent" flag
688 bpfioctl(dev, cmd, addr, flags, p)
695 register struct bpf_d *d = dev->si_drv1;
699 if (d->bd_state == BPF_WAITING)
700 callout_stop(&d->bd_callout);
701 d->bd_state = BPF_IDLE;
711 * Check for read packet available.
734 ifp = d->bd_bif->bif_ifp;
735 error = (*ifp->if_ioctl)(ifp, cmd, addr);
741 * Get buffer len [for read()].
744 *(u_int *)addr = d->bd_bufsize;
757 register u_int size = *(u_int *)addr;
759 if (size > bpf_maxbufsize)
760 *(u_int *)addr = size = bpf_maxbufsize;
761 else if (size < BPF_MINBUFSIZE)
762 *(u_int *)addr = size = BPF_MINBUFSIZE;
763 d->bd_bufsize = size;
769 * Set link layer read filter.
772 error = bpf_setf(d, (struct bpf_program *)addr);
776 * Flush read packet buffer.
785 * Put interface into promiscuous mode.
788 if (d->bd_bif == 0) {
790 * No interface attached yet.
796 if (d->bd_promisc == 0) {
797 error = ifpromisc(d->bd_bif->bif_ifp, 1);
805 * Get device parameters.
811 *(u_int *)addr = d->bd_bif->bif_dlt;
815 * Get interface name.
821 struct ifnet *const ifp = d->bd_bif->bif_ifp;
822 struct ifreq *const ifr = (struct ifreq *)addr;
824 snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
825 "%s%d", ifp->if_name, ifp->if_unit);
833 error = bpf_setif(d, (struct ifreq *)addr);
841 struct timeval *tv = (struct timeval *)addr;
844 * Subtract 1 tick from tvtohz() since this isn't
847 if ((error = itimerfix(tv)) == 0)
848 d->bd_rtout = tvtohz(tv) - 1;
857 struct timeval *tv = (struct timeval *)addr;
859 tv->tv_sec = d->bd_rtout / hz;
860 tv->tv_usec = (d->bd_rtout % hz) * tick;
869 struct bpf_stat *bs = (struct bpf_stat *)addr;
871 bs->bs_recv = d->bd_rcount;
872 bs->bs_drop = d->bd_dcount;
877 * Set immediate mode.
880 d->bd_immediate = *(u_int *)addr;
885 struct bpf_version *bv = (struct bpf_version *)addr;
887 bv->bv_major = BPF_MAJOR_VERSION;
888 bv->bv_minor = BPF_MINOR_VERSION;
893 * Get "header already complete" flag
896 *(u_int *)addr = d->bd_hdrcmplt;
900 * Set "header already complete" flag
903 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
907 * Get "see sent packets" flag
910 *(u_int *)addr = d->bd_seesent;
914 * Set "see sent packets" flag
917 d->bd_seesent = *(u_int *)addr;
920 case FIONBIO: /* Non-blocking I/O */
923 case FIOASYNC: /* Send signal on receive packets */
924 d->bd_async = *(int *)addr;
928 error = fsetown(*(int *)addr, &d->bd_sigio);
932 *(int *)addr = fgetown(d->bd_sigio);
935 /* This is deprecated, FIOSETOWN should be used instead. */
937 error = fsetown(-(*(int *)addr), &d->bd_sigio);
940 /* This is deprecated, FIOGETOWN should be used instead. */
942 *(int *)addr = -fgetown(d->bd_sigio);
945 case BIOCSRSIG: /* Set receive signal */
949 sig = *(u_int *)addr;
958 *(u_int *)addr = d->bd_sig;
965 * Set d's packet filter program to fp. If this file already has a filter,
966 * free it and replace it. Returns EINVAL for bogus requests.
971 struct bpf_program *fp;
973 struct bpf_insn *fcode, *old;
978 if (fp->bf_insns == 0) {
986 free((caddr_t)old, M_BPF);
990 if (flen > BPF_MAXINSNS)
993 size = flen * sizeof(*fp->bf_insns);
994 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
995 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
996 bpf_validate(fcode, (int)flen)) {
998 d->bd_filter = fcode;
1002 free((caddr_t)old, M_BPF);
1006 free((caddr_t)fcode, M_BPF);
1011 * Detach a file from its current interface (if attached at all) and attach
1012 * to the interface indicated by the name stored in ifr.
1013 * Return an errno or 0.
1022 struct ifnet *theywant;
1024 theywant = ifunit(ifr->ifr_name);
1029 * Look through attached interfaces for the named one.
1031 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
1032 struct ifnet *ifp = bp->bif_ifp;
1034 if (ifp == 0 || ifp != theywant)
1037 * We found the requested interface.
1038 * If it's not up, return an error.
1039 * Allocate the packet buffers if we need to.
1040 * If we're already attached to requested interface,
1041 * just flush the buffer.
1043 if ((ifp->if_flags & IFF_UP) == 0)
1046 if (d->bd_sbuf == 0) {
1047 error = bpf_allocbufs(d);
1052 if (bp != d->bd_bif) {
1055 * Detach if attached to something else.
1070 * Support for select() and poll() system calls
1072 * Return true iff the specific operation will not block indefinitely.
1073 * Otherwise, return false but make a note that a selwakeup() must be done.
1076 bpfpoll(dev, events, p)
1081 register struct bpf_d *d;
1086 if (d->bd_bif == NULL)
1089 revents = events & (POLLOUT | POLLWRNORM);
1091 if (events & (POLLIN | POLLRDNORM)) {
1093 * An imitation of the FIONREAD ioctl code.
1094 * XXX not quite. An exact imitation:
1095 * if (d->b_slen != 0 ||
1096 * (d->bd_hbuf != NULL && d->bd_hlen != 0)
1098 if (d->bd_hlen != 0 ||
1099 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1101 revents |= events & (POLLIN | POLLRDNORM);
1103 selrecord(p, &d->bd_sel);
1104 /* Start the read timeout if necessary. */
1105 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1106 callout_reset(&d->bd_callout, d->bd_rtout,
1108 d->bd_state = BPF_WAITING;
1117 * Incoming linkage from device drivers. Process the packet pkt, of length
1118 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1119 * by each process' filter, and if accepted, stashed into the corresponding
1123 bpf_tap(ifp, pkt, pktlen)
1125 register u_char *pkt;
1126 register u_int pktlen;
1129 register struct bpf_d *d;
1130 register u_int slen;
1132 * Note that the ipl does not have to be raised at this point.
1133 * The only problem that could arise here is that if two different
1134 * interfaces shared any data. This is not the case.
1137 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1139 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1141 catchpacket(d, pkt, pktlen, slen, bcopy);
1146 * Copy data from an mbuf chain into a buffer. This code is derived
1147 * from m_copydata in sys/uipc_mbuf.c.
1150 bpf_mcopy(src_arg, dst_arg, len)
1151 const void *src_arg;
1153 register size_t len;
1155 register const struct mbuf *m;
1156 register u_int count;
1164 count = min(m->m_len, len);
1165 bcopy(mtod(m, void *), dst, count);
1173 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1180 struct bpf_if *bp = ifp->if_bpf;
1186 for (m0 = m; m0 != 0; m0 = m0->m_next)
1187 pktlen += m0->m_len;
1189 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1190 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1193 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1195 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1200 * Move the packet data from interface memory (pkt) into the
1201 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1202 * otherwise 0. "copy" is the routine called to do the actual data
1203 * transfer. bcopy is passed in to copy contiguous chunks, while
1204 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1205 * pkt is really an mbuf.
1208 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1209 register struct bpf_d *d;
1210 register u_char *pkt;
1211 register u_int pktlen, snaplen;
1212 register void (*cpfn) __P((const void *, void *, size_t));
1214 register struct bpf_hdr *hp;
1215 register int totlen, curlen;
1216 register int hdrlen = d->bd_bif->bif_hdrlen;
1218 * Figure out how many bytes to move. If the packet is
1219 * greater or equal to the snapshot length, transfer that
1220 * much. Otherwise, transfer the whole packet (unless
1221 * we hit the buffer size limit).
1223 totlen = hdrlen + min(snaplen, pktlen);
1224 if (totlen > d->bd_bufsize)
1225 totlen = d->bd_bufsize;
1228 * Round up the end of the previous packet to the next longword.
1230 curlen = BPF_WORDALIGN(d->bd_slen);
1231 if (curlen + totlen > d->bd_bufsize) {
1233 * This packet will overflow the storage buffer.
1234 * Rotate the buffers if we can, then wakeup any
1237 if (d->bd_fbuf == 0) {
1239 * We haven't completed the previous read yet,
1240 * so drop the packet.
1249 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1251 * Immediate mode is set, or the read timeout has
1252 * already expired during a select call. A packet
1253 * arrived, so the reader should be woken up.
1258 * Append the bpf header.
1260 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1262 microtime(&hp->bh_tstamp);
1264 uniqtime(&hp->bh_tstamp);
1266 hp->bh_tstamp = time;
1268 hp->bh_datalen = pktlen;
1269 hp->bh_hdrlen = hdrlen;
1271 * Copy the packet data into the store buffer and update its length.
1273 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1274 d->bd_slen = curlen + totlen;
1278 * Initialize all nonzero fields of a descriptor.
1282 register struct bpf_d *d;
1284 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1285 if (d->bd_fbuf == 0)
1288 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1289 if (d->bd_sbuf == 0) {
1290 free(d->bd_fbuf, M_BPF);
1299 * Free buffers currently in use by a descriptor.
1304 register struct bpf_d *d;
1307 * We don't need to lock out interrupts since this descriptor has
1308 * been detached from its interface and it yet hasn't been marked
1311 if (d->bd_sbuf != 0) {
1312 free(d->bd_sbuf, M_BPF);
1313 if (d->bd_hbuf != 0)
1314 free(d->bd_hbuf, M_BPF);
1315 if (d->bd_fbuf != 0)
1316 free(d->bd_fbuf, M_BPF);
1319 free((caddr_t)d->bd_filter, M_BPF);
1323 * Attach an interface to bpf. ifp is a pointer to the structure
1324 * defining the interface to be attached, dlt is the link layer type,
1325 * and hdrlen is the fixed size of the link header (variable length
1326 * headers are not yet supporrted).
1329 bpfattach(ifp, dlt, hdrlen)
1334 bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_DONTWAIT | M_ZERO);
1341 bp->bif_next = bpf_iflist;
1344 bp->bif_ifp->if_bpf = 0;
1347 * Compute the length of the bpf header. This is not necessarily
1348 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1349 * that the network layer header begins on a longword boundary (for
1350 * performance reasons and to alleviate alignment restrictions).
1352 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1355 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1359 * Detach bpf from an interface. This involves detaching each descriptor
1360 * associated with the interface, and leaving bd_bif NULL. Notify each
1361 * descriptor as it's detached so that any sleepers wake up and get
1368 struct bpf_if *bp, *bp_prev;
1374 /* Locate BPF interface information */
1376 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1377 if (ifp == bp->bif_ifp)
1382 /* Interface wasn't attached */
1383 if (bp->bif_ifp == NULL) {
1385 printf("bpfdetach: %s%d was not attached\n", ifp->if_name,
1390 while ((d = bp->bif_dlist) != NULL) {
1396 bp_prev->bif_next = bp->bif_next;
1398 bpf_iflist = bp->bif_next;
1406 static void bpf_drvinit __P((void *unused));
1413 cdevsw_add(&bpf_cdevsw);
1416 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1420 * NOP stubs to allow bpf-using drivers to load and function.
1422 * A 'better' implementation would allow the core bpf functionality
1423 * to be loaded at runtime.
1427 bpf_tap(ifp, pkt, pktlen)
1429 register u_char *pkt;
1430 register u_int pktlen;
1442 bpfattach(ifp, dlt, hdrlen)
1455 bpf_filter(pc, p, wirelen, buflen)
1456 register const struct bpf_insn *pc;
1459 register u_int buflen;
1461 return -1; /* "no filter" behaviour */