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.44 1998/10/08 00:32:08 alex Exp $
50 #define inline __inline
53 #include <sys/param.h>
54 #include <sys/systm.h>
56 #include <sys/malloc.h>
60 #include <sys/signalvar.h>
61 #include <sys/filio.h>
62 #include <sys/sockio.h>
63 #include <sys/ttycom.h>
64 #include <sys/filedesc.h>
66 #if defined(sparc) && BSD < 199103
67 #include <sys/stream.h>
71 #include <sys/socket.h>
72 #include <sys/vnode.h>
76 #include <net/bpfdesc.h>
78 #include <netinet/in.h>
79 #include <netinet/if_ether.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
83 #include "opt_devfs.h"
86 #include <sys/devfsext.h>
91 * Older BSDs don't have kernel malloc.
95 static caddr_t bpf_alloc();
96 #include <net/bpf_compat.h>
97 #define BPF_BUFSIZE (MCLBYTES-8)
98 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
100 #define BPF_BUFSIZE 4096
101 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
104 #define PRINET 26 /* interruptible */
107 * The default read buffer size is patchable.
109 static int bpf_bufsize = BPF_BUFSIZE;
110 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
111 &bpf_bufsize, 0, "");
114 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
115 * bpf_dtab holds the descriptors, indexed by minor device #
117 static struct bpf_if *bpf_iflist;
118 static struct bpf_d bpf_dtab[NBPFILTER];
119 static int bpf_dtab_init;
121 static int bpf_allocbufs __P((struct bpf_d *));
122 static void bpf_attachd __P((struct bpf_d *d, struct bpf_if *bp));
123 static void bpf_detachd __P((struct bpf_d *d));
124 static void bpf_freed __P((struct bpf_d *));
125 static void bpf_ifname __P((struct ifnet *, struct ifreq *));
126 static void bpf_mcopy __P((const void *, void *, size_t));
127 static int bpf_movein __P((struct uio *, int,
128 struct mbuf **, struct sockaddr *, int *));
129 static int bpf_setif __P((struct bpf_d *, struct ifreq *));
131 bpf_wakeup __P((struct bpf_d *));
132 static void catchpacket __P((struct bpf_d *, u_char *, u_int,
133 u_int, void (*)(const void *, void *, size_t)));
134 static void reset_d __P((struct bpf_d *));
135 static int bpf_setf __P((struct bpf_d *, struct bpf_program *));
137 static d_open_t bpfopen;
138 static d_close_t bpfclose;
139 static d_read_t bpfread;
140 static d_write_t bpfwrite;
141 static d_ioctl_t bpfioctl;
142 static d_poll_t bpfpoll;
144 #define CDEV_MAJOR 23
145 static struct cdevsw bpf_cdevsw =
146 { bpfopen, bpfclose, bpfread, bpfwrite, /*23*/
147 bpfioctl, nostop, nullreset, nodevtotty,/* bpf */
148 bpfpoll, nommap, NULL, "bpf", NULL, -1 };
152 bpf_movein(uio, linktype, mp, sockp, datlen)
153 register struct uio *uio;
154 int linktype, *datlen;
155 register struct mbuf **mp;
156 register struct sockaddr *sockp;
164 * Build a sockaddr based on the data link layer type.
165 * We do this at this level because the ethernet header
166 * is copied directly into the data field of the sockaddr.
167 * In the case of SLIP, there is no header and the packet
168 * is forwarded as is.
169 * Also, we are careful to leave room at the front of the mbuf
170 * for the link level header.
175 sockp->sa_family = AF_INET;
180 sockp->sa_family = AF_UNSPEC;
181 /* XXX Would MAXLINKHDR be better? */
182 hlen = sizeof(struct ether_header);
186 #if defined(__FreeBSD__) || defined(__bsdi__)
187 sockp->sa_family = AF_IMPLINK;
190 sockp->sa_family = AF_UNSPEC;
191 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
198 sockp->sa_family = AF_UNSPEC;
203 case DLT_ATM_RFC1483:
205 * en atm driver requires 4-byte atm pseudo header.
206 * though it isn't standard, vpi:vci needs to be
209 sockp->sa_family = AF_UNSPEC;
210 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
218 len = uio->uio_resid;
219 *datlen = len - hlen;
220 if ((unsigned)len > MCLBYTES)
223 MGETHDR(m, M_WAIT, MT_DATA);
229 if ((m->m_flags & M_EXT) == 0) {
232 if (m->m_len != MCLBYTES) {
238 m->m_pkthdr.len = m->m_len = len;
239 m->m_pkthdr.rcvif = NULL;
242 * Make room for link header.
245 m->m_pkthdr.len -= hlen;
248 m->m_data += hlen; /* XXX */
252 error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio);
256 error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio);
265 * Attach file to the bpf interface, i.e. make d listen on bp.
266 * Must be called at splimp.
274 * Point d at bp, and add d to the interface's list of listeners.
275 * Finally, point the driver's bpf cookie at the interface so
276 * it will divert packets to bpf.
279 d->bd_next = bp->bif_dlist;
282 bp->bif_ifp->if_bpf = bp;
286 * Detach a file from its interface.
297 * Check if this descriptor had requested promiscuous mode.
298 * If so, turn it off.
302 if (ifpromisc(bp->bif_ifp, 0))
304 * Something is really wrong if we were able to put
305 * the driver into promiscuous mode, but can't
308 panic("bpf: ifpromisc failed");
310 /* Remove d from the interface's descriptor list. */
315 panic("bpf_detachd: descriptor not in list");
318 if (bp->bif_dlist == 0)
320 * Let the driver know that there are no more listeners.
322 d->bd_bif->bif_ifp->if_bpf = 0;
328 * Mark a descriptor free by making it point to itself.
329 * This is probably cheaper than marking with a constant since
330 * the address should be in a register anyway.
332 #define D_ISFREE(d) ((d) == (d)->bd_next)
333 #define D_MARKFREE(d) ((d)->bd_next = (d))
334 #define D_MARKUSED(d) ((d)->bd_next = 0)
337 * Open ethernet device. Returns ENXIO for illegal minor device number,
338 * EBUSY if file is open by another process.
342 bpfopen(dev, flags, fmt, p)
348 register struct bpf_d *d;
350 if (minor(dev) >= NBPFILTER)
353 * Each minor can be opened by only one process. If the requested
354 * minor is in use, return EBUSY.
356 d = &bpf_dtab[minor(dev)];
360 /* Mark "free" and do most initialization. */
361 bzero((char *)d, sizeof(*d));
362 d->bd_bufsize = bpf_bufsize;
369 * Close the descriptor by detaching it from its interface,
370 * deallocating its buffers, and marking it free.
374 bpfclose(dev, flags, fmt, p)
380 register struct bpf_d *d = &bpf_dtab[minor(dev)];
383 funsetown(d->bd_sigio);
394 * Support for SunOS, which does not have tsleep.
401 struct bpf_d *d = (struct bpf_d *)arg;
406 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
410 register struct bpf_d *d;
412 register int rto = d->bd_rtout;
417 timeout(bpf_timeout, (caddr_t)d, rto);
419 st = sleep((caddr_t)d, PRINET|PCATCH);
421 if (d->bd_timedout == 0)
422 untimeout(bpf_timeout, (caddr_t)d);
426 return (st != 0) ? EINTR : 0;
429 #define BPF_SLEEP tsleep
433 * Rotate the packet buffers in descriptor d. Move the store buffer
434 * into the hold slot, and the free buffer into the store slot.
435 * Zero the length of the new store buffer.
437 #define ROTATE_BUFFERS(d) \
438 (d)->bd_hbuf = (d)->bd_sbuf; \
439 (d)->bd_hlen = (d)->bd_slen; \
440 (d)->bd_sbuf = (d)->bd_fbuf; \
444 * bpfread - read next chunk of packets from buffers
447 bpfread(dev, uio, ioflag)
449 register struct uio *uio;
452 register struct bpf_d *d = &bpf_dtab[minor(dev)];
457 * Restrict application to use a buffer the same size as
460 if (uio->uio_resid != d->bd_bufsize)
465 * If the hold buffer is empty, then do a timed sleep, which
466 * ends when the timeout expires or when enough packets
467 * have arrived to fill the store buffer.
469 while (d->bd_hbuf == 0) {
470 if (d->bd_immediate && d->bd_slen != 0) {
472 * A packet(s) either arrived since the previous
473 * read or arrived while we were asleep.
474 * Rotate the buffers and return what's here.
479 if (ioflag & IO_NDELAY)
482 error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf",
484 if (error == EINTR || error == ERESTART) {
488 if (error == EWOULDBLOCK) {
490 * On a timeout, return what's in the buffer,
491 * which may be nothing. If there is something
492 * in the store buffer, we can rotate the buffers.
496 * We filled up the buffer in between
497 * getting the timeout and arriving
498 * here, so we don't need to rotate.
502 if (d->bd_slen == 0) {
511 * At this point, we know we have something in the hold slot.
516 * Move data from hold buffer into user space.
517 * We know the entire buffer is transferred since
518 * we checked above that the read buffer is bpf_bufsize bytes.
520 error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
523 d->bd_fbuf = d->bd_hbuf;
533 * If there are processes sleeping on this descriptor, wake them up.
537 register struct bpf_d *d;
542 if (d->bd_async && d->bd_sig && d->bd_sigio)
543 pgsigio(d->bd_sigio, d->bd_sig, 0);
546 selwakeup(&d->bd_sel);
548 d->bd_sel.si_pid = 0;
551 selwakeup(d->bd_selproc, (int)d->bd_selcoll);
559 bpfwrite(dev, uio, ioflag)
564 register struct bpf_d *d = &bpf_dtab[minor(dev)];
568 static struct sockaddr dst;
574 ifp = d->bd_bif->bif_ifp;
576 if (uio->uio_resid == 0)
579 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
583 if (datlen > ifp->if_mtu)
588 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
590 error = (*ifp->if_output)(ifp, m, &dst);
594 * The driver frees the mbuf.
600 * Reset a descriptor by flushing its packet buffer and clearing the
601 * receive and drop counts. Should be called at splimp.
608 /* Free the hold buffer. */
609 d->bd_fbuf = d->bd_hbuf;
619 * FIONREAD Check for read packet available.
620 * SIOCGIFADDR Get interface address - convenient hook to driver.
621 * BIOCGBLEN Get buffer len [for read()].
622 * BIOCSETF Set ethernet read filter.
623 * BIOCFLUSH Flush read packet buffer.
624 * BIOCPROMISC Put interface into promiscuous mode.
625 * BIOCGDLT Get link layer type.
626 * BIOCGETIF Get interface name.
627 * BIOCSETIF Set interface.
628 * BIOCSRTIMEOUT Set read timeout.
629 * BIOCGRTIMEOUT Get read timeout.
630 * BIOCGSTATS Get packet stats.
631 * BIOCIMMEDIATE Set immediate mode.
632 * BIOCVERSION Get filter language version.
636 bpfioctl(dev, cmd, addr, flags, p)
643 register struct bpf_d *d = &bpf_dtab[minor(dev)];
653 * Check for read packet available.
676 ifp = d->bd_bif->bif_ifp;
677 error = (*ifp->if_ioctl)(ifp, cmd, addr);
683 * Get buffer len [for read()].
686 *(u_int *)addr = d->bd_bufsize;
699 register u_int size = *(u_int *)addr;
701 if (size > BPF_MAXBUFSIZE)
702 *(u_int *)addr = size = BPF_MAXBUFSIZE;
703 else if (size < BPF_MINBUFSIZE)
704 *(u_int *)addr = size = BPF_MINBUFSIZE;
705 d->bd_bufsize = size;
711 * Set link layer read filter.
714 error = bpf_setf(d, (struct bpf_program *)addr);
718 * Flush read packet buffer.
727 * Put interface into promiscuous mode.
730 if (d->bd_bif == 0) {
732 * No interface attached yet.
738 if (d->bd_promisc == 0) {
739 error = ifpromisc(d->bd_bif->bif_ifp, 1);
747 * Get device parameters.
753 *(u_int *)addr = d->bd_bif->bif_dlt;
757 * Set interface name.
763 bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
770 error = bpf_setif(d, (struct ifreq *)addr);
778 struct timeval *tv = (struct timeval *)addr;
781 * Subtract 1 tick from tvtohz() since this isn't
784 if ((error = itimerfix(tv)) == 0)
785 d->bd_rtout = tvtohz(tv) - 1;
794 struct timeval *tv = (struct timeval *)addr;
796 tv->tv_sec = d->bd_rtout / hz;
797 tv->tv_usec = (d->bd_rtout % hz) * tick;
806 struct bpf_stat *bs = (struct bpf_stat *)addr;
808 bs->bs_recv = d->bd_rcount;
809 bs->bs_drop = d->bd_dcount;
814 * Set immediate mode.
817 d->bd_immediate = *(u_int *)addr;
822 struct bpf_version *bv = (struct bpf_version *)addr;
824 bv->bv_major = BPF_MAJOR_VERSION;
825 bv->bv_minor = BPF_MINOR_VERSION;
829 case FIONBIO: /* Non-blocking I/O */
832 case FIOASYNC: /* Send signal on receive packets */
833 d->bd_async = *(int *)addr;
837 error = fsetown(*(int *)addr, &d->bd_sigio);
841 *(int *)addr = fgetown(d->bd_sigio);
844 /* This is deprecated, FIOSETOWN should be used instead. */
846 error = fsetown(-(*(int *)addr), &d->bd_sigio);
849 /* This is deprecated, FIOGETOWN should be used instead. */
851 *(int *)addr = -fgetown(d->bd_sigio);
854 case BIOCSRSIG: /* Set receive signal */
858 sig = *(u_int *)addr;
867 *(u_int *)addr = d->bd_sig;
874 * Set d's packet filter program to fp. If this file already has a filter,
875 * free it and replace it. Returns EINVAL for bogus requests.
880 struct bpf_program *fp;
882 struct bpf_insn *fcode, *old;
887 if (fp->bf_insns == 0) {
895 free((caddr_t)old, M_DEVBUF);
899 if (flen > BPF_MAXINSNS)
902 size = flen * sizeof(*fp->bf_insns);
903 fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
904 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
905 bpf_validate(fcode, (int)flen)) {
907 d->bd_filter = fcode;
911 free((caddr_t)old, M_DEVBUF);
915 free((caddr_t)fcode, M_DEVBUF);
920 * Detach a file from its current interface (if attached at all) and attach
921 * to the interface indicated by the name stored in ifr.
922 * Return an errno or 0.
931 struct ifnet *theywant;
933 theywant = ifunit(ifr->ifr_name);
938 * Look through attached interfaces for the named one.
940 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
941 struct ifnet *ifp = bp->bif_ifp;
943 if (ifp == 0 || ifp != theywant)
946 * We found the requested interface.
947 * If it's not up, return an error.
948 * Allocate the packet buffers if we need to.
949 * If we're already attached to requested interface,
950 * just flush the buffer.
952 if ((ifp->if_flags & IFF_UP) == 0)
955 if (d->bd_sbuf == 0) {
956 error = bpf_allocbufs(d);
961 if (bp != d->bd_bif) {
964 * Detach if attached to something else.
979 * Convert an interface name plus unit number of an ifp to a single
980 * name which is returned in the ifr.
987 char *s = ifp->if_name;
988 char *d = ifr->ifr_name;
992 d--; /* back to the null */
993 /* XXX Assume that unit number is less than 10. */
994 *d++ = ifp->if_unit + '0';
999 * Support for select() and poll() system calls
1001 * Return true iff the specific operation will not block indefinitely.
1002 * Otherwise, return false but make a note that a selwakeup() must be done.
1005 bpfpoll(dev, events, p)
1010 register struct bpf_d *d;
1015 * An imitation of the FIONREAD ioctl code.
1017 d = &bpf_dtab[minor(dev)];
1020 if (events & (POLLIN | POLLRDNORM))
1021 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1022 revents |= events & (POLLIN | POLLRDNORM);
1024 selrecord(p, &d->bd_sel);
1031 * Incoming linkage from device drivers. Process the packet pkt, of length
1032 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1033 * by each process' filter, and if accepted, stashed into the corresponding
1037 bpf_tap(ifp, pkt, pktlen)
1039 register u_char *pkt;
1040 register u_int pktlen;
1043 register struct bpf_d *d;
1044 register u_int slen;
1046 * Note that the ipl does not have to be raised at this point.
1047 * The only problem that could arise here is that if two different
1048 * interfaces shared any data. This is not the case.
1051 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1053 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1055 catchpacket(d, pkt, pktlen, slen, bcopy);
1060 * Copy data from an mbuf chain into a buffer. This code is derived
1061 * from m_copydata in sys/uipc_mbuf.c.
1064 bpf_mcopy(src_arg, dst_arg, len)
1065 const void *src_arg;
1067 register size_t len;
1069 register const struct mbuf *m;
1070 register u_int count;
1078 count = min(m->m_len, len);
1079 bcopy(mtod(m, void *), dst, count);
1087 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1094 struct bpf_if *bp = ifp->if_bpf;
1100 for (m0 = m; m0 != 0; m0 = m0->m_next)
1101 pktlen += m0->m_len;
1103 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1105 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1107 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1112 * Move the packet data from interface memory (pkt) into the
1113 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1114 * otherwise 0. "copy" is the routine called to do the actual data
1115 * transfer. bcopy is passed in to copy contiguous chunks, while
1116 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1117 * pkt is really an mbuf.
1120 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1121 register struct bpf_d *d;
1122 register u_char *pkt;
1123 register u_int pktlen, snaplen;
1124 register void (*cpfn) __P((const void *, void *, size_t));
1126 register struct bpf_hdr *hp;
1127 register int totlen, curlen;
1128 register int hdrlen = d->bd_bif->bif_hdrlen;
1130 * Figure out how many bytes to move. If the packet is
1131 * greater or equal to the snapshot length, transfer that
1132 * much. Otherwise, transfer the whole packet (unless
1133 * we hit the buffer size limit).
1135 totlen = hdrlen + min(snaplen, pktlen);
1136 if (totlen > d->bd_bufsize)
1137 totlen = d->bd_bufsize;
1140 * Round up the end of the previous packet to the next longword.
1142 curlen = BPF_WORDALIGN(d->bd_slen);
1143 if (curlen + totlen > d->bd_bufsize) {
1145 * This packet will overflow the storage buffer.
1146 * Rotate the buffers if we can, then wakeup any
1149 if (d->bd_fbuf == 0) {
1151 * We haven't completed the previous read yet,
1152 * so drop the packet.
1161 else if (d->bd_immediate)
1163 * Immediate mode is set. A packet arrived so any
1164 * reads should be woken up.
1169 * Append the bpf header.
1171 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1173 microtime(&hp->bh_tstamp);
1175 uniqtime(&hp->bh_tstamp);
1177 hp->bh_tstamp = time;
1179 hp->bh_datalen = pktlen;
1180 hp->bh_hdrlen = hdrlen;
1182 * Copy the packet data into the store buffer and update its length.
1184 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1185 d->bd_slen = curlen + totlen;
1189 * Initialize all nonzero fields of a descriptor.
1193 register struct bpf_d *d;
1195 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1196 if (d->bd_fbuf == 0)
1199 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1200 if (d->bd_sbuf == 0) {
1201 free(d->bd_fbuf, M_DEVBUF);
1210 * Free buffers currently in use by a descriptor.
1215 register struct bpf_d *d;
1218 * We don't need to lock out interrupts since this descriptor has
1219 * been detached from its interface and it yet hasn't been marked
1222 if (d->bd_sbuf != 0) {
1223 free(d->bd_sbuf, M_DEVBUF);
1224 if (d->bd_hbuf != 0)
1225 free(d->bd_hbuf, M_DEVBUF);
1226 if (d->bd_fbuf != 0)
1227 free(d->bd_fbuf, M_DEVBUF);
1230 free((caddr_t)d->bd_filter, M_DEVBUF);
1236 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1237 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1238 * size of the link header (variable length headers not yet supported).
1241 bpfattach(ifp, dlt, hdrlen)
1247 bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
1255 bp->bif_next = bpf_iflist;
1258 bp->bif_ifp->if_bpf = 0;
1261 * Compute the length of the bpf header. This is not necessarily
1262 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1263 * that the network layer header begins on a longword boundary (for
1264 * performance reasons and to alleviate alignment restrictions).
1266 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1269 * Mark all the descriptors free if this hasn't been done.
1271 if (!bpf_dtab_init) {
1272 for (i = 0; i < NBPFILTER; ++i)
1273 D_MARKFREE(&bpf_dtab[i]);
1278 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1282 static void *bpf_devfs_token[NBPFILTER];
1285 static int bpf_devsw_installed;
1287 static void bpf_drvinit __P((void *unused));
1297 if( ! bpf_devsw_installed ) {
1298 dev = makedev(CDEV_MAJOR, 0);
1299 cdevsw_add(&dev,&bpf_cdevsw, NULL);
1300 bpf_devsw_installed = 1;
1303 for ( i = 0 ; i < NBPFILTER ; i++ ) {
1304 bpf_devfs_token[i] =
1305 devfs_add_devswf(&bpf_cdevsw, i, DV_CHR, 0, 0,
1312 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)