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.21 2005/01/06 09:14:13 hsu 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>
66 #include <net/bpfdesc.h>
68 #include <netinet/in.h>
69 #include <netinet/if_ether.h>
70 #include <sys/kernel.h>
71 #include <sys/sysctl.h>
73 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
78 * The default read buffer size is patchable.
80 static int bpf_bufsize = BPF_DEFAULTBUFSIZE;
81 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
83 static int bpf_maxbufsize = BPF_MAXBUFSIZE;
84 SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
85 &bpf_maxbufsize, 0, "");
88 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
90 static struct bpf_if *bpf_iflist;
92 static int bpf_allocbufs(struct bpf_d *);
93 static void bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
94 static void bpf_detachd(struct bpf_d *d);
95 static void bpf_freed(struct bpf_d *);
96 static void bpf_mcopy(const void *, void *, size_t);
97 static int bpf_movein(struct uio *, int, struct mbuf **,
98 struct sockaddr *, int *);
99 static int bpf_setif(struct bpf_d *, struct ifreq *);
100 static void bpf_timed_out(void *);
101 static void bpf_wakeup(struct bpf_d *);
102 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int,
103 void (*)(const void *, void *, size_t));
104 static void reset_d(struct bpf_d *);
105 static int bpf_setf(struct bpf_d *, struct bpf_program *);
106 static void bpf_drvinit(void *unused);
108 static d_open_t bpfopen;
109 static d_close_t bpfclose;
110 static d_read_t bpfread;
111 static d_write_t bpfwrite;
112 static d_ioctl_t bpfioctl;
113 static d_poll_t bpfpoll;
115 #define CDEV_MAJOR 23
116 static struct cdevsw bpf_cdevsw = {
118 /* maj */ CDEV_MAJOR,
124 /* close */ bpfclose,
126 /* write */ bpfwrite,
127 /* ioctl */ bpfioctl,
130 /* strategy */ nostrategy,
137 bpf_movein(struct uio *uio, int linktype, struct mbuf **mp,
138 struct sockaddr *sockp, int *datlen)
146 * Build a sockaddr based on the data link layer type.
147 * We do this at this level because the ethernet header
148 * is copied directly into the data field of the sockaddr.
149 * In the case of SLIP, there is no header and the packet
150 * is forwarded as is.
151 * Also, we are careful to leave room at the front of the mbuf
152 * for the link level header.
157 sockp->sa_family = AF_INET;
162 sockp->sa_family = AF_UNSPEC;
163 /* XXX Would MAXLINKHDR be better? */
164 hlen = sizeof(struct ether_header);
168 sockp->sa_family = AF_IMPLINK;
174 sockp->sa_family = AF_UNSPEC;
178 case DLT_ATM_RFC1483:
180 * en atm driver requires 4-byte atm pseudo header.
181 * though it isn't standard, vpi:vci needs to be
184 sockp->sa_family = AF_UNSPEC;
185 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
189 sockp->sa_family = AF_UNSPEC;
190 hlen = 4; /* This should match PPP_HDRLEN */
197 len = uio->uio_resid;
198 *datlen = len - hlen;
199 if ((unsigned)len > MCLBYTES)
202 MGETHDR(m, MB_WAIT, MT_DATA);
207 if (!(m->m_flags & M_EXT)) {
212 m->m_pkthdr.len = m->m_len = len;
213 m->m_pkthdr.rcvif = NULL;
216 * Make room for link header.
219 m->m_pkthdr.len -= hlen;
221 m->m_data += hlen; /* XXX */
222 error = uiomove(sockp->sa_data, hlen, uio);
226 error = uiomove(mtod(m, caddr_t), len - hlen, uio);
235 * Attach file to the bpf interface, i.e. make d listen on bp.
236 * Must be called at splimp.
239 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
242 * Point d at bp, and add d to the interface's list of listeners.
243 * Finally, point the driver's bpf cookie at the interface so
244 * it will divert packets to bpf.
247 SLIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
248 bp->bif_ifp->if_bpf = bp;
252 * Detach a file from its interface.
255 bpf_detachd(struct bpf_d *d)
261 * Check if this descriptor had requested promiscuous mode.
262 * If so, turn it off.
266 if (ifpromisc(bp->bif_ifp, 0)) {
268 * Something is really wrong if we were able to put
269 * the driver into promiscuous mode, but can't
272 panic("bpf: ifpromisc failed");
275 /* Remove d from the interface's descriptor list. */
276 SLIST_REMOVE(&bp->bif_dlist, d, bpf_d, bd_next);
278 if (SLIST_EMPTY(&bp->bif_dlist)) {
280 * Let the driver know that there are no more listeners.
282 d->bd_bif->bif_ifp->if_bpf = NULL;
288 * Open ethernet device. Returns ENXIO for illegal minor device number,
289 * EBUSY if file is open by another process.
293 bpfopen(dev_t dev, int flags, int fmt, struct thread *td)
296 struct proc *p = td->td_proc;
300 if (p->p_ucred->cr_prison)
305 * Each minor can be opened by only one process. If the requested
306 * minor is in use, return EBUSY.
310 make_dev(&bpf_cdevsw, minor(dev), 0, 0, 0600, "bpf%d", lminor(dev));
311 MALLOC(d, struct bpf_d *, sizeof *d, M_BPF, M_WAITOK | M_ZERO);
313 d->bd_bufsize = bpf_bufsize;
316 callout_init(&d->bd_callout);
321 * Close the descriptor by detaching it from its interface,
322 * deallocating its buffers, and marking it free.
326 bpfclose(dev_t dev, int flags, int fmt, struct thread *td)
328 struct bpf_d *d = dev->si_drv1;
331 funsetown(d->bd_sigio);
333 if (d->bd_state == BPF_WAITING)
334 callout_stop(&d->bd_callout);
335 d->bd_state = BPF_IDLE;
336 if (d->bd_bif != NULL)
347 * Rotate the packet buffers in descriptor d. Move the store buffer
348 * into the hold slot, and the free buffer into the store slot.
349 * Zero the length of the new store buffer.
351 #define ROTATE_BUFFERS(d) \
352 (d)->bd_hbuf = (d)->bd_sbuf; \
353 (d)->bd_hlen = (d)->bd_slen; \
354 (d)->bd_sbuf = (d)->bd_fbuf; \
358 * bpfread - read next chunk of packets from buffers
361 bpfread(dev_t dev, struct uio *uio, int ioflag)
363 struct bpf_d *d = dev->si_drv1;
369 * Restrict application to use a buffer the same size as
372 if (uio->uio_resid != d->bd_bufsize)
376 if (d->bd_state == BPF_WAITING)
377 callout_stop(&d->bd_callout);
378 timed_out = (d->bd_state == BPF_TIMED_OUT);
379 d->bd_state = BPF_IDLE;
381 * If the hold buffer is empty, then do a timed sleep, which
382 * ends when the timeout expires or when enough packets
383 * have arrived to fill the store buffer.
385 while (d->bd_hbuf == NULL) {
386 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
388 * A packet(s) either arrived since the previous
389 * read or arrived while we were asleep.
390 * Rotate the buffers and return what's here.
397 * No data is available, check to see if the bpf device
398 * is still pointed at a real interface. If not, return
399 * ENXIO so that the userland process knows to rebind
400 * it before using it again.
402 if (d->bd_bif == NULL) {
407 if (ioflag & IO_NDELAY) {
411 error = tsleep(d, PCATCH, "bpf", d->bd_rtout);
412 if (error == EINTR || error == ERESTART) {
416 if (error == EWOULDBLOCK) {
418 * On a timeout, return what's in the buffer,
419 * which may be nothing. If there is something
420 * in the store buffer, we can rotate the buffers.
424 * We filled up the buffer in between
425 * getting the timeout and arriving
426 * here, so we don't need to rotate.
430 if (d->bd_slen == 0) {
439 * At this point, we know we have something in the hold slot.
444 * Move data from hold buffer into user space.
445 * We know the entire buffer is transferred since
446 * we checked above that the read buffer is bpf_bufsize bytes.
448 error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
451 d->bd_fbuf = d->bd_hbuf;
461 * If there are processes sleeping on this descriptor, wake them up.
464 bpf_wakeup(struct bpf_d *d)
466 if (d->bd_state == BPF_WAITING) {
467 callout_stop(&d->bd_callout);
468 d->bd_state = BPF_IDLE;
471 if (d->bd_async && d->bd_sig && d->bd_sigio)
472 pgsigio(d->bd_sigio, d->bd_sig, 0);
474 selwakeup(&d->bd_sel);
476 d->bd_sel.si_pid = 0;
480 bpf_timed_out(void *arg)
482 struct bpf_d *d = (struct bpf_d *)arg;
486 if (d->bd_state == BPF_WAITING) {
487 d->bd_state = BPF_TIMED_OUT;
495 bpfwrite(dev_t dev, struct uio *uio, int ioflag)
497 struct bpf_d *d = dev->si_drv1;
501 static struct sockaddr dst;
504 if (d->bd_bif == NULL)
507 ifp = d->bd_bif->bif_ifp;
509 if (uio->uio_resid == 0)
512 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
516 if (datlen > ifp->if_mtu)
520 dst.sa_family = pseudo_AF_HDRCMPLT;
523 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)NULL);
526 * The driver frees the mbuf.
532 * Reset a descriptor by flushing its packet buffer and clearing the
533 * receive and drop counts. Should be called at splimp.
536 reset_d(struct bpf_d *d)
539 /* Free the hold buffer. */
540 d->bd_fbuf = d->bd_hbuf;
550 * FIONREAD Check for read packet available.
551 * SIOCGIFADDR Get interface address - convenient hook to driver.
552 * BIOCGBLEN Get buffer len [for read()].
553 * BIOCSETF Set ethernet read filter.
554 * BIOCFLUSH Flush read packet buffer.
555 * BIOCPROMISC Put interface into promiscuous mode.
556 * BIOCGDLT Get link layer type.
557 * BIOCGETIF Get interface name.
558 * BIOCSETIF Set interface.
559 * BIOCSRTIMEOUT Set read timeout.
560 * BIOCGRTIMEOUT Get read timeout.
561 * BIOCGSTATS Get packet stats.
562 * BIOCIMMEDIATE Set immediate mode.
563 * BIOCVERSION Get filter language version.
564 * BIOCGHDRCMPLT Get "header already complete" flag
565 * BIOCSHDRCMPLT Set "header already complete" flag
566 * BIOCGSEESENT Get "see packets sent" flag
567 * BIOCSSEESENT Set "see packets sent" flag
571 bpfioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
573 struct bpf_d *d = dev->si_drv1;
577 if (d->bd_state == BPF_WAITING)
578 callout_stop(&d->bd_callout);
579 d->bd_state = BPF_IDLE;
589 * Check for read packet available.
609 if (d->bd_bif == NULL)
612 ifp = d->bd_bif->bif_ifp;
613 error = (*ifp->if_ioctl)(ifp, cmd, addr,
614 td->td_proc->p_ucred);
620 * Get buffer len [for read()].
623 *(u_int *)addr = d->bd_bufsize;
633 u_int size = *(u_int *)addr;
635 if (size > bpf_maxbufsize)
636 *(u_int *)addr = size = bpf_maxbufsize;
637 else if (size < BPF_MINBUFSIZE)
638 *(u_int *)addr = size = BPF_MINBUFSIZE;
639 d->bd_bufsize = size;
644 * Set link layer read filter.
647 error = bpf_setf(d, (struct bpf_program *)addr);
651 * Flush read packet buffer.
660 * Put interface into promiscuous mode.
663 if (d->bd_bif == NULL) {
665 * No interface attached yet.
671 if (d->bd_promisc == 0) {
672 error = ifpromisc(d->bd_bif->bif_ifp, 1);
680 * Get device parameters.
683 if (d->bd_bif == NULL)
686 *(u_int *)addr = d->bd_bif->bif_dlt;
690 * Get interface name.
693 if (d->bd_bif == NULL) {
696 struct ifnet *const ifp = d->bd_bif->bif_ifp;
697 struct ifreq *const ifr = (struct ifreq *)addr;
699 strlcpy(ifr->ifr_name, ifp->if_xname,
700 sizeof ifr->ifr_name);
708 error = bpf_setif(d, (struct ifreq *)addr);
716 struct timeval *tv = (struct timeval *)addr;
719 * Subtract 1 tick from tvtohz() since this isn't
722 if ((error = itimerfix(tv)) == 0)
723 d->bd_rtout = tvtohz_low(tv);
732 struct timeval *tv = (struct timeval *)addr;
734 tv->tv_sec = d->bd_rtout / hz;
735 tv->tv_usec = (d->bd_rtout % hz) * tick;
744 struct bpf_stat *bs = (struct bpf_stat *)addr;
746 bs->bs_recv = d->bd_rcount;
747 bs->bs_drop = d->bd_dcount;
752 * Set immediate mode.
755 d->bd_immediate = *(u_int *)addr;
760 struct bpf_version *bv = (struct bpf_version *)addr;
762 bv->bv_major = BPF_MAJOR_VERSION;
763 bv->bv_minor = BPF_MINOR_VERSION;
768 * Get "header already complete" flag
771 *(u_int *)addr = d->bd_hdrcmplt;
775 * Set "header already complete" flag
778 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
782 * Get "see sent packets" flag
785 *(u_int *)addr = d->bd_seesent;
789 * Set "see sent packets" flag
792 d->bd_seesent = *(u_int *)addr;
795 case FIONBIO: /* Non-blocking I/O */
798 case FIOASYNC: /* Send signal on receive packets */
799 d->bd_async = *(int *)addr;
803 error = fsetown(*(int *)addr, &d->bd_sigio);
807 *(int *)addr = fgetown(d->bd_sigio);
810 /* This is deprecated, FIOSETOWN should be used instead. */
812 error = fsetown(-(*(int *)addr), &d->bd_sigio);
815 /* This is deprecated, FIOGETOWN should be used instead. */
817 *(int *)addr = -fgetown(d->bd_sigio);
820 case BIOCSRSIG: /* Set receive signal */
824 sig = *(u_int *)addr;
833 *(u_int *)addr = d->bd_sig;
840 * Set d's packet filter program to fp. If this file already has a filter,
841 * free it and replace it. Returns EINVAL for bogus requests.
844 bpf_setf(struct bpf_d *d, struct bpf_program *fp)
846 struct bpf_insn *fcode, *old;
851 if (fp->bf_insns == NULL) {
863 if (flen > BPF_MAXINSNS)
866 size = flen * sizeof *fp->bf_insns;
867 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
868 if (copyin(fp->bf_insns, fcode, size) == 0 &&
869 bpf_validate(fcode, (int)flen)) {
871 d->bd_filter = fcode;
884 * Detach a file from its current interface (if attached at all) and attach
885 * to the interface indicated by the name stored in ifr.
886 * Return an errno or 0.
889 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
893 struct ifnet *theywant;
895 theywant = ifunit(ifr->ifr_name);
896 if (theywant == NULL)
900 * Look through attached interfaces for the named one.
902 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
903 struct ifnet *ifp = bp->bif_ifp;
905 if (ifp == NULL || ifp != theywant)
908 * We found the requested interface.
909 * If it's not up, return an error.
910 * Allocate the packet buffers if we need to.
911 * If we're already attached to requested interface,
912 * just flush the buffer.
914 if (!(ifp->if_flags & IFF_UP))
917 if (d->bd_sbuf == NULL) {
918 error = bpf_allocbufs(d);
923 if (bp != d->bd_bif) {
924 if (d->bd_bif != NULL) {
926 * Detach if attached to something else.
943 * Support for select() and poll() system calls
945 * Return true iff the specific operation will not block indefinitely.
946 * Otherwise, return false but make a note that a selwakeup() must be done.
949 bpfpoll(dev_t dev, int events, struct thread *td)
956 if (d->bd_bif == NULL)
959 revents = events & (POLLOUT | POLLWRNORM);
961 if (events & (POLLIN | POLLRDNORM)) {
963 * An imitation of the FIONREAD ioctl code.
964 * XXX not quite. An exact imitation:
965 * if (d->b_slen != 0 ||
966 * (d->bd_hbuf != NULL && d->bd_hlen != 0)
968 if (d->bd_hlen != 0 ||
969 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
971 revents |= events & (POLLIN | POLLRDNORM);
973 selrecord(td, &d->bd_sel);
974 /* Start the read timeout if necessary. */
975 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
976 callout_reset(&d->bd_callout, d->bd_rtout,
978 d->bd_state = BPF_WAITING;
987 * Incoming linkage from device drivers. Process the packet pkt, of length
988 * pktlen, which is stored in a contiguous buffer. The packet is parsed
989 * by each process' filter, and if accepted, stashed into the corresponding
993 bpf_tap(struct ifnet *ifp, u_char *pkt, u_int pktlen)
995 struct bpf_if *bp = ifp->if_bpf;
1000 * Note that the ipl does not have to be raised at this point.
1001 * The only problem that could arise here is that if two different
1002 * interfaces shared any data. This is not the case.
1004 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1006 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1008 catchpacket(d, pkt, pktlen, slen, ovbcopy);
1013 * Copy data from an mbuf chain into a buffer. This code is derived
1014 * from m_copydata in sys/uipc_mbuf.c.
1017 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1019 const struct mbuf *m;
1028 count = min(m->m_len, len);
1029 bcopy(mtod(m, void *), dst, count);
1037 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1040 bpf_mtap(struct ifnet *ifp, struct mbuf *m)
1042 struct bpf_if *bp = ifp->if_bpf;
1048 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1049 pktlen += m0->m_len;
1051 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1052 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1055 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1057 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1062 * Move the packet data from interface memory (pkt) into the
1063 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1064 * otherwise 0. "copy" is the routine called to do the actual data
1065 * transfer. bcopy is passed in to copy contiguous chunks, while
1066 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1067 * pkt is really an mbuf.
1070 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1071 void (*cpfn)(const void *, void *, size_t))
1075 int hdrlen = d->bd_bif->bif_hdrlen;
1077 * Figure out how many bytes to move. If the packet is
1078 * greater or equal to the snapshot length, transfer that
1079 * much. Otherwise, transfer the whole packet (unless
1080 * we hit the buffer size limit).
1082 totlen = hdrlen + min(snaplen, pktlen);
1083 if (totlen > d->bd_bufsize)
1084 totlen = d->bd_bufsize;
1087 * Round up the end of the previous packet to the next longword.
1089 curlen = BPF_WORDALIGN(d->bd_slen);
1090 if (curlen + totlen > d->bd_bufsize) {
1092 * This packet will overflow the storage buffer.
1093 * Rotate the buffers if we can, then wakeup any
1096 if (d->bd_fbuf == NULL) {
1098 * We haven't completed the previous read yet,
1099 * so drop the packet.
1108 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1110 * Immediate mode is set, or the read timeout has
1111 * already expired during a select call. A packet
1112 * arrived, so the reader should be woken up.
1117 * Append the bpf header.
1119 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1120 microtime(&hp->bh_tstamp);
1121 hp->bh_datalen = pktlen;
1122 hp->bh_hdrlen = hdrlen;
1124 * Copy the packet data into the store buffer and update its length.
1126 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1127 d->bd_slen = curlen + totlen;
1131 * Initialize all nonzero fields of a descriptor.
1134 bpf_allocbufs(struct bpf_d *d)
1136 d->bd_fbuf = malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1137 if (d->bd_fbuf == NULL)
1140 d->bd_sbuf = malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1141 if (d->bd_sbuf == NULL) {
1142 free(d->bd_fbuf, M_BPF);
1151 * Free buffers currently in use by a descriptor.
1155 bpf_freed(struct bpf_d *d)
1158 * We don't need to lock out interrupts since this descriptor has
1159 * been detached from its interface and it yet hasn't been marked
1162 if (d->bd_sbuf != NULL) {
1163 free(d->bd_sbuf, M_BPF);
1164 if (d->bd_hbuf != NULL)
1165 free(d->bd_hbuf, M_BPF);
1166 if (d->bd_fbuf != NULL)
1167 free(d->bd_fbuf, M_BPF);
1170 free(d->bd_filter, M_BPF);
1174 * Attach an interface to bpf. ifp is a pointer to the structure
1175 * defining the interface to be attached, dlt is the link layer type,
1176 * and hdrlen is the fixed size of the link header (variable length
1177 * headers are not yet supporrted).
1180 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1184 bp = malloc(sizeof *bp, M_BPF, M_WAITOK | M_ZERO);
1189 bp->bif_next = bpf_iflist;
1192 bp->bif_ifp->if_bpf = NULL;
1195 * Compute the length of the bpf header. This is not necessarily
1196 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1197 * that the network layer header begins on a longword boundary (for
1198 * performance reasons and to alleviate alignment restrictions).
1200 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1203 printf("bpf: %s attached\n", ifp->if_xname);
1207 * Detach bpf from an interface. This involves detaching each descriptor
1208 * associated with the interface, and leaving bd_bif NULL. Notify each
1209 * descriptor as it's detached so that any sleepers wake up and get
1213 bpfdetach(struct ifnet *ifp)
1215 struct bpf_if *bp, *bp_prev;
1221 /* Locate BPF interface information */
1223 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1224 if (ifp == bp->bif_ifp)
1229 /* Interface wasn't attached */
1230 if (bp->bif_ifp == NULL) {
1232 printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1236 while ((d = SLIST_FIRST(&bp->bif_dlist)) != NULL) {
1241 if (bp_prev != NULL)
1242 bp_prev->bif_next = bp->bif_next;
1244 bpf_iflist = bp->bif_next;
1252 bpf_drvinit(void *unused)
1254 cdevsw_add(&bpf_cdevsw, 0, 0);
1257 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1261 * NOP stubs to allow bpf-using drivers to load and function.
1263 * A 'better' implementation would allow the core bpf functionality
1264 * to be loaded at runtime.
1268 bpf_tap(struct ifnet *ifp, u_char *pkt, u_int pktlen)
1273 bpf_mtap(struct ifnet *ifp, struct mbuf *m)
1278 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1283 bpfdetach(struct ifnet *ifp)
1288 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1290 return -1; /* "no filter" behaviour */