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.4 2003/07/19 21:14:43 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_t dev, int flags, int fmt, struct thread *td)
347 struct proc *p = td->td_proc;
351 if (p->p_ucred->cr_prison)
356 * Each minor can be opened by only one process. If the requested
357 * minor is in use, return EBUSY.
361 make_dev(&bpf_cdevsw, minor(dev), 0, 0, 0600, "bpf%d", lminor(dev));
362 MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
364 d->bd_bufsize = bpf_bufsize;
367 callout_init(&d->bd_callout);
372 * Close the descriptor by detaching it from its interface,
373 * deallocating its buffers, and marking it free.
377 bpfclose(dev, flags, fmt, td)
383 struct bpf_d *d = dev->si_drv1;
386 funsetown(d->bd_sigio);
388 if (d->bd_state == BPF_WAITING)
389 callout_stop(&d->bd_callout);
390 d->bd_state = BPF_IDLE;
402 * Support for SunOS, which does not have tsleep.
409 struct bpf_d *d = (struct bpf_d *)arg;
414 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
418 register struct bpf_d *d;
420 register int rto = d->bd_rtout;
425 timeout(bpf_timeout, (caddr_t)d, rto);
427 st = sleep((caddr_t)d, PRINET|PCATCH);
429 if (d->bd_timedout == 0)
430 untimeout(bpf_timeout, (caddr_t)d);
434 return (st != 0) ? EINTR : 0;
437 #define BPF_SLEEP tsleep
441 * Rotate the packet buffers in descriptor d. Move the store buffer
442 * into the hold slot, and the free buffer into the store slot.
443 * Zero the length of the new store buffer.
445 #define ROTATE_BUFFERS(d) \
446 (d)->bd_hbuf = (d)->bd_sbuf; \
447 (d)->bd_hlen = (d)->bd_slen; \
448 (d)->bd_sbuf = (d)->bd_fbuf; \
452 * bpfread - read next chunk of packets from buffers
455 bpfread(dev, uio, ioflag)
457 register struct uio *uio;
460 register struct bpf_d *d = dev->si_drv1;
466 * Restrict application to use a buffer the same size as
469 if (uio->uio_resid != d->bd_bufsize)
473 if (d->bd_state == BPF_WAITING)
474 callout_stop(&d->bd_callout);
475 timed_out = (d->bd_state == BPF_TIMED_OUT);
476 d->bd_state = BPF_IDLE;
478 * If the hold buffer is empty, then do a timed sleep, which
479 * ends when the timeout expires or when enough packets
480 * have arrived to fill the store buffer.
482 while (d->bd_hbuf == 0) {
483 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
485 * A packet(s) either arrived since the previous
486 * read or arrived while we were asleep.
487 * Rotate the buffers and return what's here.
494 * No data is available, check to see if the bpf device
495 * is still pointed at a real interface. If not, return
496 * ENXIO so that the userland process knows to rebind
497 * it before using it again.
499 if (d->bd_bif == NULL) {
504 if (ioflag & IO_NDELAY) {
506 return (EWOULDBLOCK);
508 error = BPF_SLEEP((caddr_t)d, PCATCH, "bpf", d->bd_rtout);
509 if (error == EINTR || error == ERESTART) {
513 if (error == EWOULDBLOCK) {
515 * On a timeout, return what's in the buffer,
516 * which may be nothing. If there is something
517 * in the store buffer, we can rotate the buffers.
521 * We filled up the buffer in between
522 * getting the timeout and arriving
523 * here, so we don't need to rotate.
527 if (d->bd_slen == 0) {
536 * At this point, we know we have something in the hold slot.
541 * Move data from hold buffer into user space.
542 * We know the entire buffer is transferred since
543 * we checked above that the read buffer is bpf_bufsize bytes.
545 error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
548 d->bd_fbuf = d->bd_hbuf;
558 * If there are processes sleeping on this descriptor, wake them up.
562 register struct bpf_d *d;
564 if (d->bd_state == BPF_WAITING) {
565 callout_stop(&d->bd_callout);
566 d->bd_state = BPF_IDLE;
569 if (d->bd_async && d->bd_sig && d->bd_sigio)
570 pgsigio(d->bd_sigio, d->bd_sig, 0);
573 selwakeup(&d->bd_sel);
575 d->bd_sel.si_pid = 0;
578 selwakeup(d->bd_selproc, (int)d->bd_selcoll);
589 struct bpf_d *d = (struct bpf_d *)arg;
593 if (d->bd_state == BPF_WAITING) {
594 d->bd_state = BPF_TIMED_OUT;
602 bpfwrite(dev, uio, ioflag)
607 register struct bpf_d *d = dev->si_drv1;
611 static struct sockaddr dst;
617 ifp = d->bd_bif->bif_ifp;
619 if (uio->uio_resid == 0)
622 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
626 if (datlen > ifp->if_mtu)
630 dst.sa_family = pseudo_AF_HDRCMPLT;
634 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
636 error = (*ifp->if_output)(ifp, m, &dst);
640 * The driver frees the mbuf.
646 * Reset a descriptor by flushing its packet buffer and clearing the
647 * receive and drop counts. Should be called at splimp.
654 /* Free the hold buffer. */
655 d->bd_fbuf = d->bd_hbuf;
665 * FIONREAD Check for read packet available.
666 * SIOCGIFADDR Get interface address - convenient hook to driver.
667 * BIOCGBLEN Get buffer len [for read()].
668 * BIOCSETF Set ethernet read filter.
669 * BIOCFLUSH Flush read packet buffer.
670 * BIOCPROMISC Put interface into promiscuous mode.
671 * BIOCGDLT Get link layer type.
672 * BIOCGETIF Get interface name.
673 * BIOCSETIF Set interface.
674 * BIOCSRTIMEOUT Set read timeout.
675 * BIOCGRTIMEOUT Get read timeout.
676 * BIOCGSTATS Get packet stats.
677 * BIOCIMMEDIATE Set immediate mode.
678 * BIOCVERSION Get filter language version.
679 * BIOCGHDRCMPLT Get "header already complete" flag
680 * BIOCSHDRCMPLT Set "header already complete" flag
681 * BIOCGSEESENT Get "see packets sent" flag
682 * BIOCSSEESENT Set "see packets sent" flag
686 bpfioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
688 struct bpf_d *d = dev->si_drv1;
692 if (d->bd_state == BPF_WAITING)
693 callout_stop(&d->bd_callout);
694 d->bd_state = BPF_IDLE;
704 * Check for read packet available.
727 ifp = d->bd_bif->bif_ifp;
728 error = (*ifp->if_ioctl)(ifp, cmd, addr);
734 * Get buffer len [for read()].
737 *(u_int *)addr = d->bd_bufsize;
750 register u_int size = *(u_int *)addr;
752 if (size > bpf_maxbufsize)
753 *(u_int *)addr = size = bpf_maxbufsize;
754 else if (size < BPF_MINBUFSIZE)
755 *(u_int *)addr = size = BPF_MINBUFSIZE;
756 d->bd_bufsize = size;
762 * Set link layer read filter.
765 error = bpf_setf(d, (struct bpf_program *)addr);
769 * Flush read packet buffer.
778 * Put interface into promiscuous mode.
781 if (d->bd_bif == 0) {
783 * No interface attached yet.
789 if (d->bd_promisc == 0) {
790 error = ifpromisc(d->bd_bif->bif_ifp, 1);
798 * Get device parameters.
804 *(u_int *)addr = d->bd_bif->bif_dlt;
808 * Get interface name.
814 struct ifnet *const ifp = d->bd_bif->bif_ifp;
815 struct ifreq *const ifr = (struct ifreq *)addr;
817 snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
818 "%s%d", ifp->if_name, ifp->if_unit);
826 error = bpf_setif(d, (struct ifreq *)addr);
834 struct timeval *tv = (struct timeval *)addr;
837 * Subtract 1 tick from tvtohz() since this isn't
840 if ((error = itimerfix(tv)) == 0)
841 d->bd_rtout = tvtohz(tv) - 1;
850 struct timeval *tv = (struct timeval *)addr;
852 tv->tv_sec = d->bd_rtout / hz;
853 tv->tv_usec = (d->bd_rtout % hz) * tick;
862 struct bpf_stat *bs = (struct bpf_stat *)addr;
864 bs->bs_recv = d->bd_rcount;
865 bs->bs_drop = d->bd_dcount;
870 * Set immediate mode.
873 d->bd_immediate = *(u_int *)addr;
878 struct bpf_version *bv = (struct bpf_version *)addr;
880 bv->bv_major = BPF_MAJOR_VERSION;
881 bv->bv_minor = BPF_MINOR_VERSION;
886 * Get "header already complete" flag
889 *(u_int *)addr = d->bd_hdrcmplt;
893 * Set "header already complete" flag
896 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
900 * Get "see sent packets" flag
903 *(u_int *)addr = d->bd_seesent;
907 * Set "see sent packets" flag
910 d->bd_seesent = *(u_int *)addr;
913 case FIONBIO: /* Non-blocking I/O */
916 case FIOASYNC: /* Send signal on receive packets */
917 d->bd_async = *(int *)addr;
921 error = fsetown(*(int *)addr, &d->bd_sigio);
925 *(int *)addr = fgetown(d->bd_sigio);
928 /* This is deprecated, FIOSETOWN should be used instead. */
930 error = fsetown(-(*(int *)addr), &d->bd_sigio);
933 /* This is deprecated, FIOGETOWN should be used instead. */
935 *(int *)addr = -fgetown(d->bd_sigio);
938 case BIOCSRSIG: /* Set receive signal */
942 sig = *(u_int *)addr;
951 *(u_int *)addr = d->bd_sig;
958 * Set d's packet filter program to fp. If this file already has a filter,
959 * free it and replace it. Returns EINVAL for bogus requests.
964 struct bpf_program *fp;
966 struct bpf_insn *fcode, *old;
971 if (fp->bf_insns == 0) {
979 free((caddr_t)old, M_BPF);
983 if (flen > BPF_MAXINSNS)
986 size = flen * sizeof(*fp->bf_insns);
987 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
988 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
989 bpf_validate(fcode, (int)flen)) {
991 d->bd_filter = fcode;
995 free((caddr_t)old, M_BPF);
999 free((caddr_t)fcode, M_BPF);
1004 * Detach a file from its current interface (if attached at all) and attach
1005 * to the interface indicated by the name stored in ifr.
1006 * Return an errno or 0.
1015 struct ifnet *theywant;
1017 theywant = ifunit(ifr->ifr_name);
1022 * Look through attached interfaces for the named one.
1024 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
1025 struct ifnet *ifp = bp->bif_ifp;
1027 if (ifp == 0 || ifp != theywant)
1030 * We found the requested interface.
1031 * If it's not up, return an error.
1032 * Allocate the packet buffers if we need to.
1033 * If we're already attached to requested interface,
1034 * just flush the buffer.
1036 if ((ifp->if_flags & IFF_UP) == 0)
1039 if (d->bd_sbuf == 0) {
1040 error = bpf_allocbufs(d);
1045 if (bp != d->bd_bif) {
1048 * Detach if attached to something else.
1063 * Support for select() and poll() system calls
1065 * Return true iff the specific operation will not block indefinitely.
1066 * Otherwise, return false but make a note that a selwakeup() must be done.
1069 bpfpoll(dev_t dev, int events, struct thread *td)
1071 register struct bpf_d *d;
1076 if (d->bd_bif == NULL)
1079 revents = events & (POLLOUT | POLLWRNORM);
1081 if (events & (POLLIN | POLLRDNORM)) {
1083 * An imitation of the FIONREAD ioctl code.
1084 * XXX not quite. An exact imitation:
1085 * if (d->b_slen != 0 ||
1086 * (d->bd_hbuf != NULL && d->bd_hlen != 0)
1088 if (d->bd_hlen != 0 ||
1089 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1091 revents |= events & (POLLIN | POLLRDNORM);
1093 selrecord(td, &d->bd_sel);
1094 /* Start the read timeout if necessary. */
1095 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1096 callout_reset(&d->bd_callout, d->bd_rtout,
1098 d->bd_state = BPF_WAITING;
1107 * Incoming linkage from device drivers. Process the packet pkt, of length
1108 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1109 * by each process' filter, and if accepted, stashed into the corresponding
1113 bpf_tap(ifp, pkt, pktlen)
1115 register u_char *pkt;
1116 register u_int pktlen;
1119 register struct bpf_d *d;
1120 register u_int slen;
1122 * Note that the ipl does not have to be raised at this point.
1123 * The only problem that could arise here is that if two different
1124 * interfaces shared any data. This is not the case.
1127 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1129 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1131 catchpacket(d, pkt, pktlen, slen, bcopy);
1136 * Copy data from an mbuf chain into a buffer. This code is derived
1137 * from m_copydata in sys/uipc_mbuf.c.
1140 bpf_mcopy(src_arg, dst_arg, len)
1141 const void *src_arg;
1143 register size_t len;
1145 register const struct mbuf *m;
1146 register u_int count;
1154 count = min(m->m_len, len);
1155 bcopy(mtod(m, void *), dst, count);
1163 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1170 struct bpf_if *bp = ifp->if_bpf;
1176 for (m0 = m; m0 != 0; m0 = m0->m_next)
1177 pktlen += m0->m_len;
1179 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1180 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1183 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1185 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1190 * Move the packet data from interface memory (pkt) into the
1191 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1192 * otherwise 0. "copy" is the routine called to do the actual data
1193 * transfer. bcopy is passed in to copy contiguous chunks, while
1194 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1195 * pkt is really an mbuf.
1198 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1199 register struct bpf_d *d;
1200 register u_char *pkt;
1201 register u_int pktlen, snaplen;
1202 register void (*cpfn) __P((const void *, void *, size_t));
1204 register struct bpf_hdr *hp;
1205 register int totlen, curlen;
1206 register int hdrlen = d->bd_bif->bif_hdrlen;
1208 * Figure out how many bytes to move. If the packet is
1209 * greater or equal to the snapshot length, transfer that
1210 * much. Otherwise, transfer the whole packet (unless
1211 * we hit the buffer size limit).
1213 totlen = hdrlen + min(snaplen, pktlen);
1214 if (totlen > d->bd_bufsize)
1215 totlen = d->bd_bufsize;
1218 * Round up the end of the previous packet to the next longword.
1220 curlen = BPF_WORDALIGN(d->bd_slen);
1221 if (curlen + totlen > d->bd_bufsize) {
1223 * This packet will overflow the storage buffer.
1224 * Rotate the buffers if we can, then wakeup any
1227 if (d->bd_fbuf == 0) {
1229 * We haven't completed the previous read yet,
1230 * so drop the packet.
1239 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1241 * Immediate mode is set, or the read timeout has
1242 * already expired during a select call. A packet
1243 * arrived, so the reader should be woken up.
1248 * Append the bpf header.
1250 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1252 microtime(&hp->bh_tstamp);
1254 uniqtime(&hp->bh_tstamp);
1256 hp->bh_tstamp = time;
1258 hp->bh_datalen = pktlen;
1259 hp->bh_hdrlen = hdrlen;
1261 * Copy the packet data into the store buffer and update its length.
1263 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1264 d->bd_slen = curlen + totlen;
1268 * Initialize all nonzero fields of a descriptor.
1272 register struct bpf_d *d;
1274 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1275 if (d->bd_fbuf == 0)
1278 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1279 if (d->bd_sbuf == 0) {
1280 free(d->bd_fbuf, M_BPF);
1289 * Free buffers currently in use by a descriptor.
1294 register struct bpf_d *d;
1297 * We don't need to lock out interrupts since this descriptor has
1298 * been detached from its interface and it yet hasn't been marked
1301 if (d->bd_sbuf != 0) {
1302 free(d->bd_sbuf, M_BPF);
1303 if (d->bd_hbuf != 0)
1304 free(d->bd_hbuf, M_BPF);
1305 if (d->bd_fbuf != 0)
1306 free(d->bd_fbuf, M_BPF);
1309 free((caddr_t)d->bd_filter, M_BPF);
1313 * Attach an interface to bpf. ifp is a pointer to the structure
1314 * defining the interface to be attached, dlt is the link layer type,
1315 * and hdrlen is the fixed size of the link header (variable length
1316 * headers are not yet supporrted).
1319 bpfattach(ifp, dlt, hdrlen)
1324 bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_DONTWAIT | M_ZERO);
1331 bp->bif_next = bpf_iflist;
1334 bp->bif_ifp->if_bpf = 0;
1337 * Compute the length of the bpf header. This is not necessarily
1338 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1339 * that the network layer header begins on a longword boundary (for
1340 * performance reasons and to alleviate alignment restrictions).
1342 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1345 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1349 * Detach bpf from an interface. This involves detaching each descriptor
1350 * associated with the interface, and leaving bd_bif NULL. Notify each
1351 * descriptor as it's detached so that any sleepers wake up and get
1358 struct bpf_if *bp, *bp_prev;
1364 /* Locate BPF interface information */
1366 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1367 if (ifp == bp->bif_ifp)
1372 /* Interface wasn't attached */
1373 if (bp->bif_ifp == NULL) {
1375 printf("bpfdetach: %s%d was not attached\n", ifp->if_name,
1380 while ((d = bp->bif_dlist) != NULL) {
1386 bp_prev->bif_next = bp->bif_next;
1388 bpf_iflist = bp->bif_next;
1396 static void bpf_drvinit __P((void *unused));
1403 cdevsw_add(&bpf_cdevsw);
1406 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1410 * NOP stubs to allow bpf-using drivers to load and function.
1412 * A 'better' implementation would allow the core bpf functionality
1413 * to be loaded at runtime.
1417 bpf_tap(ifp, pkt, pktlen)
1419 register u_char *pkt;
1420 register u_int pktlen;
1432 bpfattach(ifp, dlt, hdrlen)
1445 bpf_filter(pc, p, wirelen, buflen)
1446 register const struct bpf_insn *pc;
1449 register u_int buflen;
1451 return -1; /* "no filter" behaviour */