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.6 2003/07/26 20:19:33 rob 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 /* maj */ CDEV_MAJOR,
149 /* close */ bpfclose,
151 /* write */ bpfwrite,
152 /* ioctl */ bpfioctl,
155 /* strategy */ nostrategy,
162 bpf_movein(uio, linktype, mp, sockp, datlen)
164 int linktype, *datlen;
166 struct sockaddr *sockp;
174 * Build a sockaddr based on the data link layer type.
175 * We do this at this level because the ethernet header
176 * is copied directly into the data field of the sockaddr.
177 * In the case of SLIP, there is no header and the packet
178 * is forwarded as is.
179 * Also, we are careful to leave room at the front of the mbuf
180 * for the link level header.
185 sockp->sa_family = AF_INET;
190 sockp->sa_family = AF_UNSPEC;
191 /* XXX Would MAXLINKHDR be better? */
192 hlen = sizeof(struct ether_header);
196 #if defined(__FreeBSD__) || defined(__bsdi__)
197 sockp->sa_family = AF_IMPLINK;
200 sockp->sa_family = AF_UNSPEC;
201 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
208 sockp->sa_family = AF_UNSPEC;
213 case DLT_ATM_RFC1483:
215 * en atm driver requires 4-byte atm pseudo header.
216 * though it isn't standard, vpi:vci needs to be
219 sockp->sa_family = AF_UNSPEC;
220 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
224 sockp->sa_family = AF_UNSPEC;
225 hlen = 4; /* This should match PPP_HDRLEN */
232 len = uio->uio_resid;
233 *datlen = len - hlen;
234 if ((unsigned)len > MCLBYTES)
237 MGETHDR(m, M_WAIT, MT_DATA);
243 if ((m->m_flags & M_EXT) == 0) {
246 if (m->m_len != MCLBYTES) {
252 m->m_pkthdr.len = m->m_len = len;
253 m->m_pkthdr.rcvif = NULL;
256 * Make room for link header.
259 m->m_pkthdr.len -= hlen;
262 m->m_data += hlen; /* XXX */
266 error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio);
270 error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio);
279 * Attach file to the bpf interface, i.e. make d listen on bp.
280 * Must be called at splimp.
288 * Point d at bp, and add d to the interface's list of listeners.
289 * Finally, point the driver's bpf cookie at the interface so
290 * it will divert packets to bpf.
293 d->bd_next = bp->bif_dlist;
296 bp->bif_ifp->if_bpf = bp;
300 * Detach a file from its interface.
311 * Check if this descriptor had requested promiscuous mode.
312 * If so, turn it off.
316 if (ifpromisc(bp->bif_ifp, 0))
318 * Something is really wrong if we were able to put
319 * the driver into promiscuous mode, but can't
322 panic("bpf: ifpromisc failed");
324 /* Remove d from the interface's descriptor list. */
329 panic("bpf_detachd: descriptor not in list");
332 if (bp->bif_dlist == 0)
334 * Let the driver know that there are no more listeners.
336 d->bd_bif->bif_ifp->if_bpf = 0;
341 * Open ethernet device. Returns ENXIO for illegal minor device number,
342 * EBUSY if file is open by another process.
346 bpfopen(dev_t dev, int flags, int fmt, struct thread *td)
349 struct proc *p = td->td_proc;
353 if (p->p_ucred->cr_prison)
358 * Each minor can be opened by only one process. If the requested
359 * minor is in use, return EBUSY.
363 make_dev(&bpf_cdevsw, minor(dev), 0, 0, 0600, "bpf%d", lminor(dev));
364 MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
366 d->bd_bufsize = bpf_bufsize;
369 callout_init(&d->bd_callout);
374 * Close the descriptor by detaching it from its interface,
375 * deallocating its buffers, and marking it free.
379 bpfclose(dev, flags, fmt, td)
385 struct bpf_d *d = dev->si_drv1;
388 funsetown(d->bd_sigio);
390 if (d->bd_state == BPF_WAITING)
391 callout_stop(&d->bd_callout);
392 d->bd_state = BPF_IDLE;
404 * Support for SunOS, which does not have tsleep.
411 struct bpf_d *d = (struct bpf_d *)arg;
416 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
422 int rto = d->bd_rtout;
427 timeout(bpf_timeout, (caddr_t)d, rto);
429 st = sleep((caddr_t)d, PRINET|PCATCH);
431 if (d->bd_timedout == 0)
432 untimeout(bpf_timeout, (caddr_t)d);
436 return (st != 0) ? EINTR : 0;
439 #define BPF_SLEEP tsleep
443 * Rotate the packet buffers in descriptor d. Move the store buffer
444 * into the hold slot, and the free buffer into the store slot.
445 * Zero the length of the new store buffer.
447 #define ROTATE_BUFFERS(d) \
448 (d)->bd_hbuf = (d)->bd_sbuf; \
449 (d)->bd_hlen = (d)->bd_slen; \
450 (d)->bd_sbuf = (d)->bd_fbuf; \
454 * bpfread - read next chunk of packets from buffers
457 bpfread(dev, uio, ioflag)
462 struct bpf_d *d = dev->si_drv1;
468 * Restrict application to use a buffer the same size as
471 if (uio->uio_resid != d->bd_bufsize)
475 if (d->bd_state == BPF_WAITING)
476 callout_stop(&d->bd_callout);
477 timed_out = (d->bd_state == BPF_TIMED_OUT);
478 d->bd_state = BPF_IDLE;
480 * If the hold buffer is empty, then do a timed sleep, which
481 * ends when the timeout expires or when enough packets
482 * have arrived to fill the store buffer.
484 while (d->bd_hbuf == 0) {
485 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
487 * A packet(s) either arrived since the previous
488 * read or arrived while we were asleep.
489 * Rotate the buffers and return what's here.
496 * No data is available, check to see if the bpf device
497 * is still pointed at a real interface. If not, return
498 * ENXIO so that the userland process knows to rebind
499 * it before using it again.
501 if (d->bd_bif == NULL) {
506 if (ioflag & IO_NDELAY) {
508 return (EWOULDBLOCK);
510 error = BPF_SLEEP((caddr_t)d, PCATCH, "bpf", d->bd_rtout);
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.
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 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_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
690 struct bpf_d *d = dev->si_drv1;
694 if (d->bd_state == BPF_WAITING)
695 callout_stop(&d->bd_callout);
696 d->bd_state = BPF_IDLE;
706 * Check for read packet available.
729 ifp = d->bd_bif->bif_ifp;
730 error = (*ifp->if_ioctl)(ifp, cmd, addr);
736 * Get buffer len [for read()].
739 *(u_int *)addr = d->bd_bufsize;
752 u_int size = *(u_int *)addr;
754 if (size > bpf_maxbufsize)
755 *(u_int *)addr = size = bpf_maxbufsize;
756 else if (size < BPF_MINBUFSIZE)
757 *(u_int *)addr = size = BPF_MINBUFSIZE;
758 d->bd_bufsize = size;
764 * Set link layer read filter.
767 error = bpf_setf(d, (struct bpf_program *)addr);
771 * Flush read packet buffer.
780 * Put interface into promiscuous mode.
783 if (d->bd_bif == 0) {
785 * No interface attached yet.
791 if (d->bd_promisc == 0) {
792 error = ifpromisc(d->bd_bif->bif_ifp, 1);
800 * Get device parameters.
806 *(u_int *)addr = d->bd_bif->bif_dlt;
810 * Get interface name.
816 struct ifnet *const ifp = d->bd_bif->bif_ifp;
817 struct ifreq *const ifr = (struct ifreq *)addr;
819 snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
820 "%s%d", ifp->if_name, ifp->if_unit);
828 error = bpf_setif(d, (struct ifreq *)addr);
836 struct timeval *tv = (struct timeval *)addr;
839 * Subtract 1 tick from tvtohz() since this isn't
842 if ((error = itimerfix(tv)) == 0)
843 d->bd_rtout = tvtohz(tv) - 1;
852 struct timeval *tv = (struct timeval *)addr;
854 tv->tv_sec = d->bd_rtout / hz;
855 tv->tv_usec = (d->bd_rtout % hz) * tick;
864 struct bpf_stat *bs = (struct bpf_stat *)addr;
866 bs->bs_recv = d->bd_rcount;
867 bs->bs_drop = d->bd_dcount;
872 * Set immediate mode.
875 d->bd_immediate = *(u_int *)addr;
880 struct bpf_version *bv = (struct bpf_version *)addr;
882 bv->bv_major = BPF_MAJOR_VERSION;
883 bv->bv_minor = BPF_MINOR_VERSION;
888 * Get "header already complete" flag
891 *(u_int *)addr = d->bd_hdrcmplt;
895 * Set "header already complete" flag
898 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
902 * Get "see sent packets" flag
905 *(u_int *)addr = d->bd_seesent;
909 * Set "see sent packets" flag
912 d->bd_seesent = *(u_int *)addr;
915 case FIONBIO: /* Non-blocking I/O */
918 case FIOASYNC: /* Send signal on receive packets */
919 d->bd_async = *(int *)addr;
923 error = fsetown(*(int *)addr, &d->bd_sigio);
927 *(int *)addr = fgetown(d->bd_sigio);
930 /* This is deprecated, FIOSETOWN should be used instead. */
932 error = fsetown(-(*(int *)addr), &d->bd_sigio);
935 /* This is deprecated, FIOGETOWN should be used instead. */
937 *(int *)addr = -fgetown(d->bd_sigio);
940 case BIOCSRSIG: /* Set receive signal */
944 sig = *(u_int *)addr;
953 *(u_int *)addr = d->bd_sig;
960 * Set d's packet filter program to fp. If this file already has a filter,
961 * free it and replace it. Returns EINVAL for bogus requests.
966 struct bpf_program *fp;
968 struct bpf_insn *fcode, *old;
973 if (fp->bf_insns == 0) {
981 free((caddr_t)old, M_BPF);
985 if (flen > BPF_MAXINSNS)
988 size = flen * sizeof(*fp->bf_insns);
989 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
990 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
991 bpf_validate(fcode, (int)flen)) {
993 d->bd_filter = fcode;
997 free((caddr_t)old, M_BPF);
1001 free((caddr_t)fcode, M_BPF);
1006 * Detach a file from its current interface (if attached at all) and attach
1007 * to the interface indicated by the name stored in ifr.
1008 * Return an errno or 0.
1017 struct ifnet *theywant;
1019 theywant = ifunit(ifr->ifr_name);
1024 * Look through attached interfaces for the named one.
1026 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
1027 struct ifnet *ifp = bp->bif_ifp;
1029 if (ifp == 0 || ifp != theywant)
1032 * We found the requested interface.
1033 * If it's not up, return an error.
1034 * Allocate the packet buffers if we need to.
1035 * If we're already attached to requested interface,
1036 * just flush the buffer.
1038 if ((ifp->if_flags & IFF_UP) == 0)
1041 if (d->bd_sbuf == 0) {
1042 error = bpf_allocbufs(d);
1047 if (bp != d->bd_bif) {
1050 * Detach if attached to something else.
1065 * Support for select() and poll() system calls
1067 * Return true iff the specific operation will not block indefinitely.
1068 * Otherwise, return false but make a note that a selwakeup() must be done.
1071 bpfpoll(dev_t dev, int events, struct thread *td)
1078 if (d->bd_bif == NULL)
1081 revents = events & (POLLOUT | POLLWRNORM);
1083 if (events & (POLLIN | POLLRDNORM)) {
1085 * An imitation of the FIONREAD ioctl code.
1086 * XXX not quite. An exact imitation:
1087 * if (d->b_slen != 0 ||
1088 * (d->bd_hbuf != NULL && d->bd_hlen != 0)
1090 if (d->bd_hlen != 0 ||
1091 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1093 revents |= events & (POLLIN | POLLRDNORM);
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,
1100 d->bd_state = BPF_WAITING;
1109 * Incoming linkage from device drivers. Process the packet pkt, of length
1110 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1111 * by each process' filter, and if accepted, stashed into the corresponding
1115 bpf_tap(ifp, pkt, pktlen)
1124 * Note that the ipl does not have to be raised at this point.
1125 * The only problem that could arise here is that if two different
1126 * interfaces shared any data. This is not the case.
1129 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1131 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1133 catchpacket(d, pkt, pktlen, slen, bcopy);
1138 * Copy data from an mbuf chain into a buffer. This code is derived
1139 * from m_copydata in sys/uipc_mbuf.c.
1142 bpf_mcopy(src_arg, dst_arg, len)
1143 const void *src_arg;
1147 const struct mbuf *m;
1156 count = min(m->m_len, len);
1157 bcopy(mtod(m, void *), dst, count);
1165 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1172 struct bpf_if *bp = ifp->if_bpf;
1178 for (m0 = m; m0 != 0; m0 = m0->m_next)
1179 pktlen += m0->m_len;
1181 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1182 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1185 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1187 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1192 * Move the packet data from interface memory (pkt) into the
1193 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1194 * otherwise 0. "copy" is the routine called to do the actual data
1195 * transfer. bcopy is passed in to copy contiguous chunks, while
1196 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1197 * pkt is really an mbuf.
1200 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1203 u_int pktlen, snaplen;
1204 void (*cpfn) __P((const void *, void *, size_t));
1208 int hdrlen = d->bd_bif->bif_hdrlen;
1210 * Figure out how many bytes to move. If the packet is
1211 * greater or equal to the snapshot length, transfer that
1212 * much. Otherwise, transfer the whole packet (unless
1213 * we hit the buffer size limit).
1215 totlen = hdrlen + min(snaplen, pktlen);
1216 if (totlen > d->bd_bufsize)
1217 totlen = d->bd_bufsize;
1220 * Round up the end of the previous packet to the next longword.
1222 curlen = BPF_WORDALIGN(d->bd_slen);
1223 if (curlen + totlen > d->bd_bufsize) {
1225 * This packet will overflow the storage buffer.
1226 * Rotate the buffers if we can, then wakeup any
1229 if (d->bd_fbuf == 0) {
1231 * We haven't completed the previous read yet,
1232 * so drop the packet.
1241 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1243 * Immediate mode is set, or the read timeout has
1244 * already expired during a select call. A packet
1245 * arrived, so the reader should be woken up.
1250 * Append the bpf header.
1252 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1254 microtime(&hp->bh_tstamp);
1256 uniqtime(&hp->bh_tstamp);
1258 hp->bh_tstamp = time;
1260 hp->bh_datalen = pktlen;
1261 hp->bh_hdrlen = hdrlen;
1263 * Copy the packet data into the store buffer and update its length.
1265 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1266 d->bd_slen = curlen + totlen;
1270 * Initialize all nonzero fields of a descriptor.
1276 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1277 if (d->bd_fbuf == 0)
1280 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1281 if (d->bd_sbuf == 0) {
1282 free(d->bd_fbuf, M_BPF);
1291 * Free buffers currently in use by a descriptor.
1299 * We don't need to lock out interrupts since this descriptor has
1300 * been detached from its interface and it yet hasn't been marked
1303 if (d->bd_sbuf != 0) {
1304 free(d->bd_sbuf, M_BPF);
1305 if (d->bd_hbuf != 0)
1306 free(d->bd_hbuf, M_BPF);
1307 if (d->bd_fbuf != 0)
1308 free(d->bd_fbuf, M_BPF);
1311 free((caddr_t)d->bd_filter, M_BPF);
1315 * Attach an interface to bpf. ifp is a pointer to the structure
1316 * defining the interface to be attached, dlt is the link layer type,
1317 * and hdrlen is the fixed size of the link header (variable length
1318 * headers are not yet supporrted).
1321 bpfattach(ifp, dlt, hdrlen)
1326 bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_DONTWAIT | M_ZERO);
1333 bp->bif_next = bpf_iflist;
1336 bp->bif_ifp->if_bpf = 0;
1339 * Compute the length of the bpf header. This is not necessarily
1340 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1341 * that the network layer header begins on a longword boundary (for
1342 * performance reasons and to alleviate alignment restrictions).
1344 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1347 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1351 * Detach bpf from an interface. This involves detaching each descriptor
1352 * associated with the interface, and leaving bd_bif NULL. Notify each
1353 * descriptor as it's detached so that any sleepers wake up and get
1360 struct bpf_if *bp, *bp_prev;
1366 /* Locate BPF interface information */
1368 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1369 if (ifp == bp->bif_ifp)
1374 /* Interface wasn't attached */
1375 if (bp->bif_ifp == NULL) {
1377 printf("bpfdetach: %s%d was not attached\n", ifp->if_name,
1382 while ((d = bp->bif_dlist) != NULL) {
1388 bp_prev->bif_next = bp->bif_next;
1390 bpf_iflist = bp->bif_next;
1398 static void bpf_drvinit __P((void *unused));
1405 cdevsw_add(&bpf_cdevsw);
1408 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1412 * NOP stubs to allow bpf-using drivers to load and function.
1414 * A 'better' implementation would allow the core bpf functionality
1415 * to be loaded at runtime.
1419 bpf_tap(ifp, pkt, pktlen)
1434 bpfattach(ifp, dlt, hdrlen)
1447 bpf_filter(pc, p, wirelen, buflen)
1448 const struct bpf_insn *pc;
1453 return -1; /* "no filter" behaviour */