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.50 2008/09/23 11:28:49 sephe Exp $
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/device.h>
50 #include <sys/malloc.h>
54 #include <sys/signalvar.h>
55 #include <sys/filio.h>
56 #include <sys/sockio.h>
57 #include <sys/ttycom.h>
58 #include <sys/filedesc.h>
62 #include <sys/socket.h>
63 #include <sys/vnode.h>
65 #include <sys/thread2.h>
69 #include <net/bpfdesc.h>
70 #include <net/netmsg2.h>
72 #include <netinet/in.h>
73 #include <netinet/if_ether.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
77 #include <vfs/devfs/devfs.h>
79 struct netmsg_bpf_output {
80 struct netmsg nm_netmsg;
83 struct sockaddr *nm_dst;
86 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
87 DEVFS_DECLARE_CLONE_BITMAP(bpf);
88 #define BPF_PREALLOCATED_UNITS 4
93 * The default read buffer size is patchable.
95 static int bpf_bufsize = BPF_DEFAULTBUFSIZE;
96 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
98 int bpf_maxbufsize = BPF_MAXBUFSIZE;
99 SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
100 &bpf_maxbufsize, 0, "");
103 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
105 static struct bpf_if *bpf_iflist;
107 static int bpf_allocbufs(struct bpf_d *);
108 static void bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
109 static void bpf_detachd(struct bpf_d *d);
110 static void bpf_resetd(struct bpf_d *);
111 static void bpf_freed(struct bpf_d *);
112 static void bpf_mcopy(const void *, void *, size_t);
113 static int bpf_movein(struct uio *, int, struct mbuf **,
114 struct sockaddr *, int *, struct bpf_insn *);
115 static int bpf_setif(struct bpf_d *, struct ifreq *);
116 static void bpf_timed_out(void *);
117 static void bpf_wakeup(struct bpf_d *);
118 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int,
119 void (*)(const void *, void *, size_t),
120 const struct timeval *);
121 static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
122 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
123 static int bpf_setdlt(struct bpf_d *, u_int);
124 static void bpf_drvinit(void *unused);
126 static d_open_t bpfopen;
127 static d_clone_t bpfclone;
128 static d_close_t bpfclose;
129 static d_read_t bpfread;
130 static d_write_t bpfwrite;
131 static d_ioctl_t bpfioctl;
132 static d_poll_t bpfpoll;
134 #define CDEV_MAJOR 23
135 static struct dev_ops bpf_ops = {
136 { "bpf", CDEV_MAJOR, 0 },
147 bpf_movein(struct uio *uio, int linktype, struct mbuf **mp,
148 struct sockaddr *sockp, int *datlen, struct bpf_insn *wfilter)
160 * Build a sockaddr based on the data link layer type.
161 * We do this at this level because the ethernet header
162 * is copied directly into the data field of the sockaddr.
163 * In the case of SLIP, there is no header and the packet
164 * is forwarded as is.
165 * Also, we are careful to leave room at the front of the mbuf
166 * for the link level header.
170 sockp->sa_family = AF_INET;
175 sockp->sa_family = AF_UNSPEC;
176 /* XXX Would MAXLINKHDR be better? */
177 hlen = sizeof(struct ether_header);
182 sockp->sa_family = AF_UNSPEC;
186 case DLT_ATM_RFC1483:
188 * en atm driver requires 4-byte atm pseudo header.
189 * though it isn't standard, vpi:vci needs to be
192 sockp->sa_family = AF_UNSPEC;
193 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
197 sockp->sa_family = AF_UNSPEC;
198 hlen = 4; /* This should match PPP_HDRLEN */
205 len = uio->uio_resid;
206 *datlen = len - hlen;
207 if ((unsigned)len > MCLBYTES)
210 m = m_getl(len, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
213 m->m_pkthdr.len = m->m_len = len;
214 m->m_pkthdr.rcvif = NULL;
217 if (m->m_len < hlen) {
222 error = uiomove(mtod(m, u_char *), len, uio);
226 slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
233 * Make room for link header, and copy it to sockaddr.
236 bcopy(m->m_data, sockp->sa_data, hlen);
237 m->m_pkthdr.len -= hlen;
239 m->m_data += hlen; /* XXX */
248 * Attach file to the bpf interface, i.e. make d listen on bp.
249 * Must be called at splimp.
252 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
255 * Point d at bp, and add d to the interface's list of listeners.
256 * Finally, point the driver's bpf cookie at the interface so
257 * it will divert packets to bpf.
260 SLIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
261 *bp->bif_driverp = bp;
265 * Detach a file from its interface.
268 bpf_detachd(struct bpf_d *d)
277 /* Remove d from the interface's descriptor list. */
278 SLIST_REMOVE(&bp->bif_dlist, d, bpf_d, bd_next);
280 if (SLIST_EMPTY(&bp->bif_dlist)) {
282 * Let the driver know that there are no more listeners.
284 *bp->bif_driverp = NULL;
288 * Check if this descriptor had requested promiscuous mode.
289 * If so, turn it off.
293 error = ifpromisc(ifp, 0);
294 if (error != 0 && error != ENXIO) {
296 * ENXIO can happen if a pccard is unplugged,
297 * Something is really wrong if we were able to put
298 * the driver into promiscuous mode, but can't
301 if_printf(ifp, "bpf_detach: ifpromisc failed(%d)\n",
308 * Open ethernet device. Returns ENXIO for illegal minor device number,
309 * EBUSY if file is open by another process.
313 bpfopen(struct dev_open_args *ap)
315 cdev_t dev = ap->a_head.a_dev;
318 if (ap->a_cred->cr_prison)
323 * Each minor can be opened by only one process. If the requested
324 * minor is in use, return EBUSY.
329 MALLOC(d, struct bpf_d *, sizeof *d, M_BPF, M_WAITOK | M_ZERO);
331 d->bd_bufsize = bpf_bufsize;
334 callout_init(&d->bd_callout);
339 bpfclone(struct dev_clone_args *ap)
343 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(bpf), 0);
344 ap->a_dev = make_only_dev(&bpf_ops, unit, 0, 0, 0600, "bpf%d", unit);
350 * Close the descriptor by detaching it from its interface,
351 * deallocating its buffers, and marking it free.
355 bpfclose(struct dev_close_args *ap)
357 cdev_t dev = ap->a_head.a_dev;
358 struct bpf_d *d = dev->si_drv1;
360 funsetown(d->bd_sigio);
362 if (d->bd_state == BPF_WAITING)
363 callout_stop(&d->bd_callout);
364 d->bd_state = BPF_IDLE;
365 if (d->bd_bif != NULL)
370 if (dev->si_uminor >= BPF_PREALLOCATED_UNITS) {
371 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(bpf), dev->si_uminor);
379 * Rotate the packet buffers in descriptor d. Move the store buffer
380 * into the hold slot, and the free buffer into the store slot.
381 * Zero the length of the new store buffer.
383 #define ROTATE_BUFFERS(d) \
384 (d)->bd_hbuf = (d)->bd_sbuf; \
385 (d)->bd_hlen = (d)->bd_slen; \
386 (d)->bd_sbuf = (d)->bd_fbuf; \
390 * bpfread - read next chunk of packets from buffers
393 bpfread(struct dev_read_args *ap)
395 cdev_t dev = ap->a_head.a_dev;
396 struct bpf_d *d = dev->si_drv1;
401 * Restrict application to use a buffer the same size as
404 if (ap->a_uio->uio_resid != d->bd_bufsize)
408 if (d->bd_state == BPF_WAITING)
409 callout_stop(&d->bd_callout);
410 timed_out = (d->bd_state == BPF_TIMED_OUT);
411 d->bd_state = BPF_IDLE;
413 * If the hold buffer is empty, then do a timed sleep, which
414 * ends when the timeout expires or when enough packets
415 * have arrived to fill the store buffer.
417 while (d->bd_hbuf == NULL) {
418 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
420 * A packet(s) either arrived since the previous
421 * read or arrived while we were asleep.
422 * Rotate the buffers and return what's here.
429 * No data is available, check to see if the bpf device
430 * is still pointed at a real interface. If not, return
431 * ENXIO so that the userland process knows to rebind
432 * it before using it again.
434 if (d->bd_bif == NULL) {
439 if (ap->a_ioflag & IO_NDELAY) {
443 error = tsleep(d, PCATCH, "bpf", d->bd_rtout);
444 if (error == EINTR || error == ERESTART) {
448 if (error == EWOULDBLOCK) {
450 * On a timeout, return what's in the buffer,
451 * which may be nothing. If there is something
452 * in the store buffer, we can rotate the buffers.
456 * We filled up the buffer in between
457 * getting the timeout and arriving
458 * here, so we don't need to rotate.
462 if (d->bd_slen == 0) {
471 * At this point, we know we have something in the hold slot.
476 * Move data from hold buffer into user space.
477 * We know the entire buffer is transferred since
478 * we checked above that the read buffer is bpf_bufsize bytes.
480 error = uiomove(d->bd_hbuf, d->bd_hlen, ap->a_uio);
483 d->bd_fbuf = d->bd_hbuf;
493 * If there are processes sleeping on this descriptor, wake them up.
496 bpf_wakeup(struct bpf_d *d)
498 if (d->bd_state == BPF_WAITING) {
499 callout_stop(&d->bd_callout);
500 d->bd_state = BPF_IDLE;
503 if (d->bd_async && d->bd_sig && d->bd_sigio)
504 pgsigio(d->bd_sigio, d->bd_sig, 0);
507 selwakeup(&d->bd_sel);
510 d->bd_sel.si_pid = 0;
514 bpf_timed_out(void *arg)
516 struct bpf_d *d = (struct bpf_d *)arg;
519 if (d->bd_state == BPF_WAITING) {
520 d->bd_state = BPF_TIMED_OUT;
528 bpf_output_dispatch(struct netmsg *nmsg)
530 struct netmsg_bpf_output *bmsg = (struct netmsg_bpf_output *)nmsg;
531 struct ifnet *ifp = bmsg->nm_ifp;
535 * The driver frees the mbuf.
537 error = ifp->if_output(ifp, bmsg->nm_mbuf, bmsg->nm_dst, NULL);
538 lwkt_replymsg(&nmsg->nm_lmsg, error);
542 bpfwrite(struct dev_write_args *ap)
544 cdev_t dev = ap->a_head.a_dev;
545 struct bpf_d *d = dev->si_drv1;
551 struct netmsg_bpf_output bmsg;
553 if (d->bd_bif == NULL)
556 ifp = d->bd_bif->bif_ifp;
558 if (ap->a_uio->uio_resid == 0)
561 error = bpf_movein(ap->a_uio, (int)d->bd_bif->bif_dlt, &m,
562 &dst, &datlen, d->bd_wfilter);
566 if (datlen > ifp->if_mtu) {
572 dst.sa_family = pseudo_AF_HDRCMPLT;
574 netmsg_init(&bmsg.nm_netmsg, &curthread->td_msgport, MSGF_MPSAFE,
575 bpf_output_dispatch);
580 return lwkt_domsg(cpu_portfn(0), &bmsg.nm_netmsg.nm_lmsg, 0);
584 * Reset a descriptor by flushing its packet buffer and clearing the
585 * receive and drop counts. Should be called at splimp.
588 bpf_resetd(struct bpf_d *d)
591 /* Free the hold buffer. */
592 d->bd_fbuf = d->bd_hbuf;
602 * FIONREAD Check for read packet available.
603 * SIOCGIFADDR Get interface address - convenient hook to driver.
604 * BIOCGBLEN Get buffer len [for read()].
605 * BIOCSETF Set ethernet read filter.
606 * BIOCSETWF Set ethernet write filter.
607 * BIOCFLUSH Flush read packet buffer.
608 * BIOCPROMISC Put interface into promiscuous mode.
609 * BIOCGDLT Get link layer type.
610 * BIOCGETIF Get interface name.
611 * BIOCSETIF Set interface.
612 * BIOCSRTIMEOUT Set read timeout.
613 * BIOCGRTIMEOUT Get read timeout.
614 * BIOCGSTATS Get packet stats.
615 * BIOCIMMEDIATE Set immediate mode.
616 * BIOCVERSION Get filter language version.
617 * BIOCGHDRCMPLT Get "header already complete" flag
618 * BIOCSHDRCMPLT Set "header already complete" flag
619 * BIOCGSEESENT Get "see packets sent" flag
620 * BIOCSSEESENT Set "see packets sent" flag
621 * BIOCLOCK Set "locked" flag
625 bpfioctl(struct dev_ioctl_args *ap)
627 cdev_t dev = ap->a_head.a_dev;
628 struct bpf_d *d = dev->si_drv1;
632 if (d->bd_state == BPF_WAITING)
633 callout_stop(&d->bd_callout);
634 d->bd_state = BPF_IDLE;
637 if (d->bd_locked == 1) {
665 * Check for read packet available.
677 *(int *)ap->a_data = n;
685 if (d->bd_bif == NULL) {
688 ifp = d->bd_bif->bif_ifp;
689 ifnet_serialize_all(ifp);
690 error = ifp->if_ioctl(ifp, ap->a_cmd,
691 ap->a_data, ap->a_cred);
692 ifnet_deserialize_all(ifp);
698 * Get buffer len [for read()].
701 *(u_int *)ap->a_data = d->bd_bufsize;
708 if (d->bd_bif != NULL) {
711 u_int size = *(u_int *)ap->a_data;
713 if (size > bpf_maxbufsize)
714 *(u_int *)ap->a_data = size = bpf_maxbufsize;
715 else if (size < BPF_MINBUFSIZE)
716 *(u_int *)ap->a_data = size = BPF_MINBUFSIZE;
717 d->bd_bufsize = size;
722 * Set link layer read filter.
726 error = bpf_setf(d, (struct bpf_program *)ap->a_data,
731 * Flush read packet buffer.
740 * Put interface into promiscuous mode.
743 if (d->bd_bif == NULL) {
745 * No interface attached yet.
751 if (d->bd_promisc == 0) {
752 error = ifpromisc(d->bd_bif->bif_ifp, 1);
760 * Get device parameters.
763 if (d->bd_bif == NULL)
766 *(u_int *)ap->a_data = d->bd_bif->bif_dlt;
770 * Get a list of supported data link types.
773 if (d->bd_bif == NULL) {
776 error = bpf_getdltlist(d,
777 (struct bpf_dltlist *)ap->a_data);
782 * Set data link type.
785 if (d->bd_bif == NULL)
788 error = bpf_setdlt(d, *(u_int *)ap->a_data);
792 * Get interface name.
795 if (d->bd_bif == NULL) {
798 struct ifnet *const ifp = d->bd_bif->bif_ifp;
799 struct ifreq *const ifr = (struct ifreq *)ap->a_data;
801 strlcpy(ifr->ifr_name, ifp->if_xname,
802 sizeof ifr->ifr_name);
810 error = bpf_setif(d, (struct ifreq *)ap->a_data);
818 struct timeval *tv = (struct timeval *)ap->a_data;
821 * Subtract 1 tick from tvtohz() since this isn't
824 if ((error = itimerfix(tv)) == 0)
825 d->bd_rtout = tvtohz_low(tv);
834 struct timeval *tv = (struct timeval *)ap->a_data;
836 tv->tv_sec = d->bd_rtout / hz;
837 tv->tv_usec = (d->bd_rtout % hz) * tick;
846 struct bpf_stat *bs = (struct bpf_stat *)ap->a_data;
848 bs->bs_recv = d->bd_rcount;
849 bs->bs_drop = d->bd_dcount;
854 * Set immediate mode.
857 d->bd_immediate = *(u_int *)ap->a_data;
862 struct bpf_version *bv = (struct bpf_version *)ap->a_data;
864 bv->bv_major = BPF_MAJOR_VERSION;
865 bv->bv_minor = BPF_MINOR_VERSION;
870 * Get "header already complete" flag
873 *(u_int *)ap->a_data = d->bd_hdrcmplt;
877 * Set "header already complete" flag
880 d->bd_hdrcmplt = *(u_int *)ap->a_data ? 1 : 0;
884 * Get "see sent packets" flag
887 *(u_int *)ap->a_data = d->bd_seesent;
891 * Set "see sent packets" flag
894 d->bd_seesent = *(u_int *)ap->a_data;
897 case FIOASYNC: /* Send signal on receive packets */
898 d->bd_async = *(int *)ap->a_data;
902 error = fsetown(*(int *)ap->a_data, &d->bd_sigio);
906 *(int *)ap->a_data = fgetown(d->bd_sigio);
909 /* This is deprecated, FIOSETOWN should be used instead. */
911 error = fsetown(-(*(int *)ap->a_data), &d->bd_sigio);
914 /* This is deprecated, FIOGETOWN should be used instead. */
916 *(int *)ap->a_data = -fgetown(d->bd_sigio);
919 case BIOCSRSIG: /* Set receive signal */
923 sig = *(u_int *)ap->a_data;
932 *(u_int *)ap->a_data = d->bd_sig;
942 * Set d's packet filter program to fp. If this file already has a filter,
943 * free it and replace it. Returns EINVAL for bogus requests.
946 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
948 struct bpf_insn *fcode, *old;
949 u_int wfilter, flen, size;
951 if (cmd == BIOCSETWF) {
958 if (fp->bf_insns == NULL) {
963 d->bd_wfilter = NULL;
965 d->bd_rfilter = NULL;
973 if (flen > BPF_MAXINSNS)
976 size = flen * sizeof *fp->bf_insns;
977 fcode = (struct bpf_insn *)kmalloc(size, M_BPF, M_WAITOK);
978 if (copyin(fp->bf_insns, fcode, size) == 0 &&
979 bpf_validate(fcode, (int)flen)) {
982 d->bd_wfilter = fcode;
984 d->bd_rfilter = fcode;
997 * Detach a file from its current interface (if attached at all) and attach
998 * to the interface indicated by the name stored in ifr.
999 * Return an errno or 0.
1002 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1006 struct ifnet *theywant;
1008 theywant = ifunit(ifr->ifr_name);
1009 if (theywant == NULL)
1013 * Look through attached interfaces for the named one.
1015 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1016 struct ifnet *ifp = bp->bif_ifp;
1018 if (ifp == NULL || ifp != theywant)
1020 /* skip additional entry */
1021 if (bp->bif_driverp != &ifp->if_bpf)
1024 * We found the requested interface.
1025 * If it's not up, return an error.
1026 * Allocate the packet buffers if we need to.
1027 * If we're already attached to requested interface,
1028 * just flush the buffer.
1030 if (!(ifp->if_flags & IFF_UP))
1033 if (d->bd_sbuf == NULL) {
1034 error = bpf_allocbufs(d);
1039 if (bp != d->bd_bif) {
1040 if (d->bd_bif != NULL) {
1042 * Detach if attached to something else.
1059 * Support for select() and poll() system calls
1061 * Return true iff the specific operation will not block indefinitely.
1062 * Otherwise, return false but make a note that a selwakeup() must be done.
1065 bpfpoll(struct dev_poll_args *ap)
1067 cdev_t dev = ap->a_head.a_dev;
1072 if (d->bd_bif == NULL)
1075 revents = ap->a_events & (POLLOUT | POLLWRNORM);
1077 if (ap->a_events & (POLLIN | POLLRDNORM)) {
1079 * An imitation of the FIONREAD ioctl code.
1080 * XXX not quite. An exact imitation:
1081 * if (d->b_slen != 0 ||
1082 * (d->bd_hbuf != NULL && d->bd_hlen != 0)
1084 if (d->bd_hlen != 0 ||
1085 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1087 revents |= ap->a_events & (POLLIN | POLLRDNORM);
1089 selrecord(curthread, &d->bd_sel);
1090 /* Start the read timeout if necessary. */
1091 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1092 callout_reset(&d->bd_callout, d->bd_rtout,
1094 d->bd_state = BPF_WAITING;
1099 ap->a_events = revents;
1104 * Process the packet pkt of length pktlen. The packet is parsed
1105 * by each listener's filter, and if accepted, stashed into the
1106 * corresponding buffer.
1109 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1125 * Note that the ipl does not have to be raised at this point.
1126 * The only problem that could arise here is that if two different
1127 * interfaces shared any data. This is not the case.
1129 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1131 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1137 catchpacket(d, pkt, pktlen, slen, ovbcopy, &tv);
1145 * Copy data from an mbuf chain into a buffer. This code is derived
1146 * from m_copydata in sys/uipc_mbuf.c.
1149 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1151 const struct mbuf *m;
1160 count = min(m->m_len, len);
1161 bcopy(mtod(m, void *), dst, count);
1169 * Process the packet in the mbuf chain m. The packet is parsed by each
1170 * listener's filter, and if accepted, stashed into the corresponding
1174 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1189 /* Don't compute pktlen, if no descriptor is attached. */
1190 if (SLIST_EMPTY(&bp->bif_dlist)) {
1195 pktlen = m_lengthm(m, NULL);
1197 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1198 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1201 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1207 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy,
1216 bpf_mtap_family(struct bpf_if *bp, struct mbuf *m, sa_family_t family)
1220 KKASSERT(family != AF_UNSPEC);
1222 family4 = (u_int)family;
1223 bpf_ptap(bp, m, &family4, sizeof(family4));
1227 * Process the packet in the mbuf chain m with the header in m prepended.
1228 * The packet is parsed by each listener's filter, and if accepted,
1229 * stashed into the corresponding buffer.
1232 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen)
1237 * Craft on-stack mbuf suitable for passing to bpf_mtap.
1238 * Note that we cut corners here; we only setup what's
1239 * absolutely needed--this mbuf should never go anywhere else.
1242 mb.m_data = __DECONST(void *, data); /* LINTED */
1244 mb.m_pkthdr.rcvif = m->m_pkthdr.rcvif;
1250 * Move the packet data from interface memory (pkt) into the
1251 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1252 * otherwise 0. "copy" is the routine called to do the actual data
1253 * transfer. bcopy is passed in to copy contiguous chunks, while
1254 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1255 * pkt is really an mbuf.
1258 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1259 void (*cpfn)(const void *, void *, size_t),
1260 const struct timeval *tv)
1264 int hdrlen = d->bd_bif->bif_hdrlen;
1266 * Figure out how many bytes to move. If the packet is
1267 * greater or equal to the snapshot length, transfer that
1268 * much. Otherwise, transfer the whole packet (unless
1269 * we hit the buffer size limit).
1271 totlen = hdrlen + min(snaplen, pktlen);
1272 if (totlen > d->bd_bufsize)
1273 totlen = d->bd_bufsize;
1276 * Round up the end of the previous packet to the next longword.
1278 curlen = BPF_WORDALIGN(d->bd_slen);
1279 if (curlen + totlen > d->bd_bufsize) {
1281 * This packet will overflow the storage buffer.
1282 * Rotate the buffers if we can, then wakeup any
1285 if (d->bd_fbuf == NULL) {
1287 * We haven't completed the previous read yet,
1288 * so drop the packet.
1296 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) {
1298 * Immediate mode is set, or the read timeout has
1299 * already expired during a select call. A packet
1300 * arrived, so the reader should be woken up.
1306 * Append the bpf header.
1308 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1309 hp->bh_tstamp = *tv;
1310 hp->bh_datalen = pktlen;
1311 hp->bh_hdrlen = hdrlen;
1313 * Copy the packet data into the store buffer and update its length.
1315 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1316 d->bd_slen = curlen + totlen;
1320 * Initialize all nonzero fields of a descriptor.
1323 bpf_allocbufs(struct bpf_d *d)
1325 d->bd_fbuf = kmalloc(d->bd_bufsize, M_BPF, M_WAITOK);
1326 d->bd_sbuf = kmalloc(d->bd_bufsize, M_BPF, M_WAITOK);
1333 * Free buffers and packet filter program currently in use by a descriptor.
1337 bpf_freed(struct bpf_d *d)
1340 * We don't need to lock out interrupts since this descriptor has
1341 * been detached from its interface and it yet hasn't been marked
1344 if (d->bd_sbuf != NULL) {
1345 kfree(d->bd_sbuf, M_BPF);
1346 if (d->bd_hbuf != NULL)
1347 kfree(d->bd_hbuf, M_BPF);
1348 if (d->bd_fbuf != NULL)
1349 kfree(d->bd_fbuf, M_BPF);
1352 kfree(d->bd_rfilter, M_BPF);
1354 kfree(d->bd_wfilter, M_BPF);
1358 * Attach an interface to bpf. ifp is a pointer to the structure
1359 * defining the interface to be attached, dlt is the link layer type,
1360 * and hdrlen is the fixed size of the link header (variable length
1361 * headers are not yet supported).
1364 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1366 bpfattach_dlt(ifp, dlt, hdrlen, &ifp->if_bpf);
1370 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1374 bp = kmalloc(sizeof *bp, M_BPF, M_WAITOK | M_ZERO);
1376 SLIST_INIT(&bp->bif_dlist);
1379 bp->bif_driverp = driverp;
1380 *bp->bif_driverp = NULL;
1382 bp->bif_next = bpf_iflist;
1386 * Compute the length of the bpf header. This is not necessarily
1387 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1388 * that the network layer header begins on a longword boundary (for
1389 * performance reasons and to alleviate alignment restrictions).
1391 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1394 if_printf(ifp, "bpf attached\n");
1398 * Detach bpf from an interface. This involves detaching each descriptor
1399 * associated with the interface, and leaving bd_bif NULL. Notify each
1400 * descriptor as it's detached so that any sleepers wake up and get
1404 bpfdetach(struct ifnet *ifp)
1406 struct bpf_if *bp, *bp_prev;
1411 /* Locate BPF interface information */
1413 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1414 if (ifp == bp->bif_ifp)
1419 /* Interface wasn't attached */
1420 if (bp->bif_ifp == NULL) {
1422 kprintf("bpfdetach: %s was not attached\n", ifp->if_xname);
1426 while ((d = SLIST_FIRST(&bp->bif_dlist)) != NULL) {
1431 if (bp_prev != NULL)
1432 bp_prev->bif_next = bp->bif_next;
1434 bpf_iflist = bp->bif_next;
1442 * Get a list of available data link type of the interface.
1445 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1451 ifp = d->bd_bif->bif_ifp;
1454 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1455 if (bp->bif_ifp != ifp)
1457 if (bfl->bfl_list != NULL) {
1458 if (n >= bfl->bfl_len) {
1461 error = copyout(&bp->bif_dlt,
1462 bfl->bfl_list + n, sizeof(u_int));
1471 * Set the data link type of a BPF instance.
1474 bpf_setdlt(struct bpf_d *d, u_int dlt)
1476 int error, opromisc;
1480 if (d->bd_bif->bif_dlt == dlt)
1482 ifp = d->bd_bif->bif_ifp;
1483 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1484 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1488 opromisc = d->bd_promisc;
1494 error = ifpromisc(bp->bif_ifp, 1);
1496 if_printf(bp->bif_ifp,
1497 "bpf_setdlt: ifpromisc failed (%d)\n",
1505 return(bp == NULL ? EINVAL : 0);
1509 bpf_drvinit(void *unused)
1513 make_autoclone_dev(&bpf_ops, &DEVFS_CLONE_BITMAP(bpf),
1514 bpfclone, 0, 0, 0600, "bpf");
1515 for (i = 0; i < BPF_PREALLOCATED_UNITS; i++) {
1516 make_dev(&bpf_ops, i, 0, 0, 0600, "bpf%d", i);
1517 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(bpf), i);
1522 bpf_drvuninit(void *unused)
1524 devfs_clone_handler_del("bpf");
1525 dev_ops_remove_all(&bpf_ops);
1526 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(bpf));
1529 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1530 SYSUNINIT(bpfdev, SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvuninit, NULL);
1534 * NOP stubs to allow bpf-using drivers to load and function.
1536 * A 'better' implementation would allow the core bpf functionality
1537 * to be loaded at runtime.
1541 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1546 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1551 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen)
1556 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1561 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1566 bpfdetach(struct ifnet *ifp)
1571 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1573 return -1; /* "no filter" behaviour */