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 * $Id: bpf.c,v 1.47 1999/01/27 22:42:13 dillon Exp $
48 #define inline __inline
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/malloc.h>
58 #include <sys/signalvar.h>
59 #include <sys/filio.h>
60 #include <sys/sockio.h>
61 #include <sys/ttycom.h>
62 #include <sys/filedesc.h>
64 #if defined(sparc) && BSD < 199103
65 #include <sys/stream.h>
69 #include <sys/socket.h>
70 #include <sys/vnode.h>
74 #include <net/bpfdesc.h>
76 #include <netinet/in.h>
77 #include <netinet/if_ether.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
81 #include "opt_devfs.h"
84 #include <sys/devfsext.h>
90 * Older BSDs don't have kernel malloc.
94 static caddr_t bpf_alloc();
95 #include <net/bpf_compat.h>
96 #define BPF_BUFSIZE (MCLBYTES-8)
97 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
99 #define BPF_BUFSIZE 4096
100 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
103 #define PRINET 26 /* interruptible */
106 * The default read buffer size is patchable.
108 static int bpf_bufsize = BPF_BUFSIZE;
109 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
110 &bpf_bufsize, 0, "");
113 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
114 * bpf_dtab holds the descriptors, indexed by minor device #
116 static struct bpf_if *bpf_iflist;
117 static struct bpf_d bpf_dtab[NBPFILTER];
118 static int bpf_dtab_init;
120 static int bpf_allocbufs __P((struct bpf_d *));
121 static void bpf_attachd __P((struct bpf_d *d, struct bpf_if *bp));
122 static void bpf_detachd __P((struct bpf_d *d));
123 static void bpf_freed __P((struct bpf_d *));
124 static void bpf_ifname __P((struct ifnet *, struct ifreq *));
125 static void bpf_mcopy __P((const void *, void *, size_t));
126 static int bpf_movein __P((struct uio *, int,
127 struct mbuf **, struct sockaddr *, int *));
128 static int bpf_setif __P((struct bpf_d *, struct ifreq *));
130 bpf_wakeup __P((struct bpf_d *));
131 static void catchpacket __P((struct bpf_d *, u_char *, u_int,
132 u_int, void (*)(const void *, void *, size_t)));
133 static void reset_d __P((struct bpf_d *));
134 static int bpf_setf __P((struct bpf_d *, struct bpf_program *));
136 static d_open_t bpfopen;
137 static d_close_t bpfclose;
138 static d_read_t bpfread;
139 static d_write_t bpfwrite;
140 static d_ioctl_t bpfioctl;
141 static d_poll_t bpfpoll;
143 #define CDEV_MAJOR 23
144 static struct cdevsw bpf_cdevsw =
145 { bpfopen, bpfclose, bpfread, bpfwrite, /*23*/
146 bpfioctl, nostop, nullreset, nodevtotty,/* bpf */
147 bpfpoll, nommap, NULL, "bpf", NULL, -1 };
151 bpf_movein(uio, linktype, mp, sockp, datlen)
152 register struct uio *uio;
153 int linktype, *datlen;
154 register struct mbuf **mp;
155 register struct sockaddr *sockp;
163 * Build a sockaddr based on the data link layer type.
164 * We do this at this level because the ethernet header
165 * is copied directly into the data field of the sockaddr.
166 * In the case of SLIP, there is no header and the packet
167 * is forwarded as is.
168 * Also, we are careful to leave room at the front of the mbuf
169 * for the link level header.
174 sockp->sa_family = AF_INET;
179 sockp->sa_family = AF_UNSPEC;
180 /* XXX Would MAXLINKHDR be better? */
181 hlen = sizeof(struct ether_header);
185 #if defined(__FreeBSD__) || defined(__bsdi__)
186 sockp->sa_family = AF_IMPLINK;
189 sockp->sa_family = AF_UNSPEC;
190 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
197 sockp->sa_family = AF_UNSPEC;
202 case DLT_ATM_RFC1483:
204 * en atm driver requires 4-byte atm pseudo header.
205 * though it isn't standard, vpi:vci needs to be
208 sockp->sa_family = AF_UNSPEC;
209 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
217 len = uio->uio_resid;
218 *datlen = len - hlen;
219 if ((unsigned)len > MCLBYTES)
222 MGETHDR(m, M_WAIT, MT_DATA);
228 if ((m->m_flags & M_EXT) == 0) {
231 if (m->m_len != MCLBYTES) {
237 m->m_pkthdr.len = m->m_len = len;
238 m->m_pkthdr.rcvif = NULL;
241 * Make room for link header.
244 m->m_pkthdr.len -= hlen;
247 m->m_data += hlen; /* XXX */
251 error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio);
255 error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio);
264 * Attach file to the bpf interface, i.e. make d listen on bp.
265 * Must be called at splimp.
273 * Point d at bp, and add d to the interface's list of listeners.
274 * Finally, point the driver's bpf cookie at the interface so
275 * it will divert packets to bpf.
278 d->bd_next = bp->bif_dlist;
281 bp->bif_ifp->if_bpf = bp;
285 * Detach a file from its interface.
296 * Check if this descriptor had requested promiscuous mode.
297 * If so, turn it off.
301 if (ifpromisc(bp->bif_ifp, 0))
303 * Something is really wrong if we were able to put
304 * the driver into promiscuous mode, but can't
307 panic("bpf: ifpromisc failed");
309 /* Remove d from the interface's descriptor list. */
314 panic("bpf_detachd: descriptor not in list");
317 if (bp->bif_dlist == 0)
319 * Let the driver know that there are no more listeners.
321 d->bd_bif->bif_ifp->if_bpf = 0;
327 * Mark a descriptor free by making it point to itself.
328 * This is probably cheaper than marking with a constant since
329 * the address should be in a register anyway.
331 #define D_ISFREE(d) ((d) == (d)->bd_next)
332 #define D_MARKFREE(d) ((d)->bd_next = (d))
333 #define D_MARKUSED(d) ((d)->bd_next = 0)
336 * Open ethernet device. Returns ENXIO for illegal minor device number,
337 * EBUSY if file is open by another process.
341 bpfopen(dev, flags, fmt, p)
347 register struct bpf_d *d;
349 if (minor(dev) >= NBPFILTER)
352 * Each minor can be opened by only one process. If the requested
353 * minor is in use, return EBUSY.
355 d = &bpf_dtab[minor(dev)];
359 /* Mark "free" and do most initialization. */
360 bzero((char *)d, sizeof(*d));
361 d->bd_bufsize = bpf_bufsize;
368 * Close the descriptor by detaching it from its interface,
369 * deallocating its buffers, and marking it free.
373 bpfclose(dev, flags, fmt, p)
379 register struct bpf_d *d = &bpf_dtab[minor(dev)];
382 funsetown(d->bd_sigio);
393 * Support for SunOS, which does not have tsleep.
400 struct bpf_d *d = (struct bpf_d *)arg;
405 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
409 register struct bpf_d *d;
411 register int rto = d->bd_rtout;
416 timeout(bpf_timeout, (caddr_t)d, rto);
418 st = sleep((caddr_t)d, PRINET|PCATCH);
420 if (d->bd_timedout == 0)
421 untimeout(bpf_timeout, (caddr_t)d);
425 return (st != 0) ? EINTR : 0;
428 #define BPF_SLEEP tsleep
432 * Rotate the packet buffers in descriptor d. Move the store buffer
433 * into the hold slot, and the free buffer into the store slot.
434 * Zero the length of the new store buffer.
436 #define ROTATE_BUFFERS(d) \
437 (d)->bd_hbuf = (d)->bd_sbuf; \
438 (d)->bd_hlen = (d)->bd_slen; \
439 (d)->bd_sbuf = (d)->bd_fbuf; \
443 * bpfread - read next chunk of packets from buffers
446 bpfread(dev, uio, ioflag)
448 register struct uio *uio;
451 register struct bpf_d *d = &bpf_dtab[minor(dev)];
456 * Restrict application to use a buffer the same size as
459 if (uio->uio_resid != d->bd_bufsize)
464 * If the hold buffer is empty, then do a timed sleep, which
465 * ends when the timeout expires or when enough packets
466 * have arrived to fill the store buffer.
468 while (d->bd_hbuf == 0) {
469 if (d->bd_immediate && d->bd_slen != 0) {
471 * A packet(s) either arrived since the previous
472 * read or arrived while we were asleep.
473 * Rotate the buffers and return what's here.
478 if (ioflag & IO_NDELAY)
481 error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf",
483 if (error == EINTR || error == ERESTART) {
487 if (error == EWOULDBLOCK) {
489 * On a timeout, return what's in the buffer,
490 * which may be nothing. If there is something
491 * in the store buffer, we can rotate the buffers.
495 * We filled up the buffer in between
496 * getting the timeout and arriving
497 * here, so we don't need to rotate.
501 if (d->bd_slen == 0) {
510 * At this point, we know we have something in the hold slot.
515 * Move data from hold buffer into user space.
516 * We know the entire buffer is transferred since
517 * we checked above that the read buffer is bpf_bufsize bytes.
519 error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
522 d->bd_fbuf = d->bd_hbuf;
532 * If there are processes sleeping on this descriptor, wake them up.
536 register struct bpf_d *d;
539 if (d->bd_async && d->bd_sig && d->bd_sigio)
540 pgsigio(d->bd_sigio, d->bd_sig, 0);
543 selwakeup(&d->bd_sel);
545 d->bd_sel.si_pid = 0;
548 selwakeup(d->bd_selproc, (int)d->bd_selcoll);
556 bpfwrite(dev, uio, ioflag)
561 register struct bpf_d *d = &bpf_dtab[minor(dev)];
565 static struct sockaddr dst;
571 ifp = d->bd_bif->bif_ifp;
573 if (uio->uio_resid == 0)
576 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
580 if (datlen > ifp->if_mtu)
585 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
587 error = (*ifp->if_output)(ifp, m, &dst);
591 * The driver frees the mbuf.
597 * Reset a descriptor by flushing its packet buffer and clearing the
598 * receive and drop counts. Should be called at splimp.
605 /* Free the hold buffer. */
606 d->bd_fbuf = d->bd_hbuf;
616 * FIONREAD Check for read packet available.
617 * SIOCGIFADDR Get interface address - convenient hook to driver.
618 * BIOCGBLEN Get buffer len [for read()].
619 * BIOCSETF Set ethernet read filter.
620 * BIOCFLUSH Flush read packet buffer.
621 * BIOCPROMISC Put interface into promiscuous mode.
622 * BIOCGDLT Get link layer type.
623 * BIOCGETIF Get interface name.
624 * BIOCSETIF Set interface.
625 * BIOCSRTIMEOUT Set read timeout.
626 * BIOCGRTIMEOUT Get read timeout.
627 * BIOCGSTATS Get packet stats.
628 * BIOCIMMEDIATE Set immediate mode.
629 * BIOCVERSION Get filter language version.
633 bpfioctl(dev, cmd, addr, flags, p)
640 register struct bpf_d *d = &bpf_dtab[minor(dev)];
650 * Check for read packet available.
673 ifp = d->bd_bif->bif_ifp;
674 error = (*ifp->if_ioctl)(ifp, cmd, addr);
680 * Get buffer len [for read()].
683 *(u_int *)addr = d->bd_bufsize;
696 register u_int size = *(u_int *)addr;
698 if (size > BPF_MAXBUFSIZE)
699 *(u_int *)addr = size = BPF_MAXBUFSIZE;
700 else if (size < BPF_MINBUFSIZE)
701 *(u_int *)addr = size = BPF_MINBUFSIZE;
702 d->bd_bufsize = size;
708 * Set link layer read filter.
711 error = bpf_setf(d, (struct bpf_program *)addr);
715 * Flush read packet buffer.
724 * Put interface into promiscuous mode.
727 if (d->bd_bif == 0) {
729 * No interface attached yet.
735 if (d->bd_promisc == 0) {
736 error = ifpromisc(d->bd_bif->bif_ifp, 1);
744 * Get device parameters.
750 *(u_int *)addr = d->bd_bif->bif_dlt;
754 * Set interface name.
760 bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
767 error = bpf_setif(d, (struct ifreq *)addr);
775 struct timeval *tv = (struct timeval *)addr;
778 * Subtract 1 tick from tvtohz() since this isn't
781 if ((error = itimerfix(tv)) == 0)
782 d->bd_rtout = tvtohz(tv) - 1;
791 struct timeval *tv = (struct timeval *)addr;
793 tv->tv_sec = d->bd_rtout / hz;
794 tv->tv_usec = (d->bd_rtout % hz) * tick;
803 struct bpf_stat *bs = (struct bpf_stat *)addr;
805 bs->bs_recv = d->bd_rcount;
806 bs->bs_drop = d->bd_dcount;
811 * Set immediate mode.
814 d->bd_immediate = *(u_int *)addr;
819 struct bpf_version *bv = (struct bpf_version *)addr;
821 bv->bv_major = BPF_MAJOR_VERSION;
822 bv->bv_minor = BPF_MINOR_VERSION;
826 case FIONBIO: /* Non-blocking I/O */
829 case FIOASYNC: /* Send signal on receive packets */
830 d->bd_async = *(int *)addr;
834 error = fsetown(*(int *)addr, &d->bd_sigio);
838 *(int *)addr = fgetown(d->bd_sigio);
841 /* This is deprecated, FIOSETOWN should be used instead. */
843 error = fsetown(-(*(int *)addr), &d->bd_sigio);
846 /* This is deprecated, FIOGETOWN should be used instead. */
848 *(int *)addr = -fgetown(d->bd_sigio);
851 case BIOCSRSIG: /* Set receive signal */
855 sig = *(u_int *)addr;
864 *(u_int *)addr = d->bd_sig;
871 * Set d's packet filter program to fp. If this file already has a filter,
872 * free it and replace it. Returns EINVAL for bogus requests.
877 struct bpf_program *fp;
879 struct bpf_insn *fcode, *old;
884 if (fp->bf_insns == 0) {
892 free((caddr_t)old, M_DEVBUF);
896 if (flen > BPF_MAXINSNS)
899 size = flen * sizeof(*fp->bf_insns);
900 fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
901 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
902 bpf_validate(fcode, (int)flen)) {
904 d->bd_filter = fcode;
908 free((caddr_t)old, M_DEVBUF);
912 free((caddr_t)fcode, M_DEVBUF);
917 * Detach a file from its current interface (if attached at all) and attach
918 * to the interface indicated by the name stored in ifr.
919 * Return an errno or 0.
928 struct ifnet *theywant;
930 theywant = ifunit(ifr->ifr_name);
935 * Look through attached interfaces for the named one.
937 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
938 struct ifnet *ifp = bp->bif_ifp;
940 if (ifp == 0 || ifp != theywant)
943 * We found the requested interface.
944 * If it's not up, return an error.
945 * Allocate the packet buffers if we need to.
946 * If we're already attached to requested interface,
947 * just flush the buffer.
949 if ((ifp->if_flags & IFF_UP) == 0)
952 if (d->bd_sbuf == 0) {
953 error = bpf_allocbufs(d);
958 if (bp != d->bd_bif) {
961 * Detach if attached to something else.
976 * Convert an interface name plus unit number of an ifp to a single
977 * name which is returned in the ifr.
984 char *s = ifp->if_name;
985 char *d = ifr->ifr_name;
987 while ((*d++ = *s++) != 0)
989 d--; /* back to the null */
990 /* XXX Assume that unit number is less than 10. */
991 *d++ = ifp->if_unit + '0';
996 * Support for select() and poll() system calls
998 * Return true iff the specific operation will not block indefinitely.
999 * Otherwise, return false but make a note that a selwakeup() must be done.
1002 bpfpoll(dev, events, p)
1007 register struct bpf_d *d;
1012 * An imitation of the FIONREAD ioctl code.
1014 d = &bpf_dtab[minor(dev)];
1017 if (events & (POLLIN | POLLRDNORM))
1018 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1019 revents |= events & (POLLIN | POLLRDNORM);
1021 selrecord(p, &d->bd_sel);
1028 * Incoming linkage from device drivers. Process the packet pkt, of length
1029 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1030 * by each process' filter, and if accepted, stashed into the corresponding
1034 bpf_tap(ifp, pkt, pktlen)
1036 register u_char *pkt;
1037 register u_int pktlen;
1040 register struct bpf_d *d;
1041 register u_int slen;
1043 * Note that the ipl does not have to be raised at this point.
1044 * The only problem that could arise here is that if two different
1045 * interfaces shared any data. This is not the case.
1048 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1050 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1052 catchpacket(d, pkt, pktlen, slen, bcopy);
1057 * Copy data from an mbuf chain into a buffer. This code is derived
1058 * from m_copydata in sys/uipc_mbuf.c.
1061 bpf_mcopy(src_arg, dst_arg, len)
1062 const void *src_arg;
1064 register size_t len;
1066 register const struct mbuf *m;
1067 register u_int count;
1075 count = min(m->m_len, len);
1076 bcopy(mtod(m, void *), dst, count);
1084 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1091 struct bpf_if *bp = ifp->if_bpf;
1097 for (m0 = m; m0 != 0; m0 = m0->m_next)
1098 pktlen += m0->m_len;
1100 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1102 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1104 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1109 * Move the packet data from interface memory (pkt) into the
1110 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1111 * otherwise 0. "copy" is the routine called to do the actual data
1112 * transfer. bcopy is passed in to copy contiguous chunks, while
1113 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1114 * pkt is really an mbuf.
1117 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1118 register struct bpf_d *d;
1119 register u_char *pkt;
1120 register u_int pktlen, snaplen;
1121 register void (*cpfn) __P((const void *, void *, size_t));
1123 register struct bpf_hdr *hp;
1124 register int totlen, curlen;
1125 register int hdrlen = d->bd_bif->bif_hdrlen;
1127 * Figure out how many bytes to move. If the packet is
1128 * greater or equal to the snapshot length, transfer that
1129 * much. Otherwise, transfer the whole packet (unless
1130 * we hit the buffer size limit).
1132 totlen = hdrlen + min(snaplen, pktlen);
1133 if (totlen > d->bd_bufsize)
1134 totlen = d->bd_bufsize;
1137 * Round up the end of the previous packet to the next longword.
1139 curlen = BPF_WORDALIGN(d->bd_slen);
1140 if (curlen + totlen > d->bd_bufsize) {
1142 * This packet will overflow the storage buffer.
1143 * Rotate the buffers if we can, then wakeup any
1146 if (d->bd_fbuf == 0) {
1148 * We haven't completed the previous read yet,
1149 * so drop the packet.
1158 else if (d->bd_immediate)
1160 * Immediate mode is set. A packet arrived so any
1161 * reads should be woken up.
1166 * Append the bpf header.
1168 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1170 microtime(&hp->bh_tstamp);
1172 uniqtime(&hp->bh_tstamp);
1174 hp->bh_tstamp = time;
1176 hp->bh_datalen = pktlen;
1177 hp->bh_hdrlen = hdrlen;
1179 * Copy the packet data into the store buffer and update its length.
1181 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1182 d->bd_slen = curlen + totlen;
1186 * Initialize all nonzero fields of a descriptor.
1190 register struct bpf_d *d;
1192 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1193 if (d->bd_fbuf == 0)
1196 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1197 if (d->bd_sbuf == 0) {
1198 free(d->bd_fbuf, M_DEVBUF);
1207 * Free buffers currently in use by a descriptor.
1212 register struct bpf_d *d;
1215 * We don't need to lock out interrupts since this descriptor has
1216 * been detached from its interface and it yet hasn't been marked
1219 if (d->bd_sbuf != 0) {
1220 free(d->bd_sbuf, M_DEVBUF);
1221 if (d->bd_hbuf != 0)
1222 free(d->bd_hbuf, M_DEVBUF);
1223 if (d->bd_fbuf != 0)
1224 free(d->bd_fbuf, M_DEVBUF);
1227 free((caddr_t)d->bd_filter, M_DEVBUF);
1233 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1234 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1235 * size of the link header (variable length headers not yet supported).
1238 bpfattach(ifp, dlt, hdrlen)
1244 bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
1252 bp->bif_next = bpf_iflist;
1255 bp->bif_ifp->if_bpf = 0;
1258 * Compute the length of the bpf header. This is not necessarily
1259 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1260 * that the network layer header begins on a longword boundary (for
1261 * performance reasons and to alleviate alignment restrictions).
1263 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1266 * Mark all the descriptors free if this hasn't been done.
1268 if (!bpf_dtab_init) {
1269 for (i = 0; i < NBPFILTER; ++i)
1270 D_MARKFREE(&bpf_dtab[i]);
1275 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1279 static void *bpf_devfs_token[NBPFILTER];
1282 static int bpf_devsw_installed;
1284 static void bpf_drvinit __P((void *unused));
1294 if( ! bpf_devsw_installed ) {
1295 dev = makedev(CDEV_MAJOR, 0);
1296 cdevsw_add(&dev,&bpf_cdevsw, NULL);
1297 bpf_devsw_installed = 1;
1300 for ( i = 0 ; i < NBPFILTER ; i++ ) {
1301 bpf_devfs_token[i] =
1302 devfs_add_devswf(&bpf_cdevsw, i, DV_CHR, 0, 0,
1309 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1311 #else /* !BPFILTER */
1313 * NOP stubs to allow bpf-using drivers to load and function.
1315 * A 'better' implementation would allow the core bpf functionality
1316 * to be loaded at runtime.
1320 bpf_tap(ifp, pkt, pktlen)
1322 register u_char *pkt;
1323 register u_int pktlen;
1335 bpfattach(ifp, dlt, hdrlen)
1342 bpf_filter(pc, p, wirelen, buflen)
1343 register struct bpf_insn *pc;
1346 register u_int buflen;
1348 return -1; /* "no filter" behaviour */