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 $
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/device.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>
59 #include <sys/event.h>
61 #include <sys/socket.h>
62 #include <sys/vnode.h>
64 #include <sys/thread2.h>
68 #include <net/bpfdesc.h>
69 #include <net/netmsg2.h>
70 #include <net/netisr2.h>
72 #include <netinet/in.h>
73 #include <netinet/if_ether.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
77 #include <sys/devfs.h>
79 struct netmsg_bpf_output {
80 struct netmsg_base base;
83 struct sockaddr *nm_dst;
86 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
87 DEVFS_DECLARE_CLONE_BITMAP(bpf);
90 #define BPF_PREALLOCATED_UNITS 4
92 #define BPF_PREALLOCATED_UNITS NBPF
98 * The default read buffer size is patchable.
100 static int bpf_bufsize = BPF_DEFAULTBUFSIZE;
101 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
102 &bpf_bufsize, 0, "Current size of bpf buffer");
103 int bpf_maxbufsize = BPF_MAXBUFSIZE;
104 SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
105 &bpf_maxbufsize, 0, "Maximum size of bpf buffer");
108 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
110 static struct bpf_if *bpf_iflist;
112 static struct lwkt_token bpf_token = LWKT_TOKEN_INITIALIZER(bpf_token);
114 static int bpf_allocbufs(struct bpf_d *);
115 static void bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
116 static void bpf_detachd(struct bpf_d *d);
117 static void bpf_resetd(struct bpf_d *);
118 static void bpf_freed(struct bpf_d *);
119 static void bpf_mcopy(const void *, void *, size_t);
120 static int bpf_movein(struct uio *, int, struct mbuf **,
121 struct sockaddr *, int *, struct bpf_insn *);
122 static int bpf_setif(struct bpf_d *, struct ifreq *);
123 static void bpf_timed_out(void *);
124 static void bpf_wakeup(struct bpf_d *);
125 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int,
126 void (*)(const void *, void *, size_t),
127 const struct timeval *);
128 static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
129 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
130 static int bpf_setdlt(struct bpf_d *, u_int);
131 static void bpf_drvinit(void *unused);
132 static void bpf_filter_detach(struct knote *kn);
133 static int bpf_filter_read(struct knote *kn, long hint);
135 static d_open_t bpfopen;
136 static d_clone_t bpfclone;
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_kqfilter_t bpfkqfilter;
143 #define CDEV_MAJOR 23
144 static struct dev_ops bpf_ops = {
145 { "bpf", 0, D_MPSAFE },
151 .d_kqfilter = bpfkqfilter
156 bpf_movein(struct uio *uio, int linktype, struct mbuf **mp,
157 struct sockaddr *sockp, int *datlen, struct bpf_insn *wfilter)
169 * Build a sockaddr based on the data link layer type.
170 * We do this at this level because the ethernet header
171 * is copied directly into the data field of the sockaddr.
172 * In the case of SLIP, there is no header and the packet
173 * is forwarded as is.
174 * Also, we are careful to leave room at the front of the mbuf
175 * for the link level header.
179 sockp->sa_family = AF_INET;
184 sockp->sa_family = AF_UNSPEC;
185 /* XXX Would MAXLINKHDR be better? */
186 hlen = sizeof(struct ether_header);
191 sockp->sa_family = AF_UNSPEC;
195 case DLT_ATM_RFC1483:
197 * en atm driver requires 4-byte atm pseudo header.
198 * though it isn't standard, vpi:vci needs to be
201 sockp->sa_family = AF_UNSPEC;
202 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
206 sockp->sa_family = AF_UNSPEC;
207 hlen = 4; /* This should match PPP_HDRLEN */
214 len = uio->uio_resid;
215 *datlen = len - hlen;
216 if ((unsigned)len > MCLBYTES)
219 m = m_getl(len, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
222 m->m_pkthdr.len = m->m_len = len;
223 m->m_pkthdr.rcvif = NULL;
226 if (m->m_len < hlen) {
231 error = uiomove(mtod(m, u_char *), len, uio);
235 slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
242 * Make room for link header, and copy it to sockaddr.
245 bcopy(m->m_data, sockp->sa_data, hlen);
246 m->m_pkthdr.len -= hlen;
248 m->m_data += hlen; /* XXX */
257 * Attach file to the bpf interface, i.e. make d listen on bp.
258 * Must be called at splimp.
261 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
264 * Point d at bp, and add d to the interface's list of listeners.
265 * Finally, point the driver's bpf cookie at the interface so
266 * it will divert packets to bpf.
268 lwkt_gettoken(&bpf_token);
270 SLIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
271 *bp->bif_driverp = bp;
273 EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1);
274 lwkt_reltoken(&bpf_token);
278 * Detach a file from its interface.
281 bpf_detachd(struct bpf_d *d)
287 lwkt_gettoken(&bpf_token);
291 /* Remove d from the interface's descriptor list. */
292 SLIST_REMOVE(&bp->bif_dlist, d, bpf_d, bd_next);
294 if (SLIST_EMPTY(&bp->bif_dlist)) {
296 * Let the driver know that there are no more listeners.
298 *bp->bif_driverp = NULL;
302 EVENTHANDLER_INVOKE(bpf_track, ifp, bp->bif_dlt, 0);
305 * Check if this descriptor had requested promiscuous mode.
306 * If so, turn it off.
310 error = ifpromisc(ifp, 0);
311 if (error != 0 && error != ENXIO) {
313 * ENXIO can happen if a pccard is unplugged,
314 * Something is really wrong if we were able to put
315 * the driver into promiscuous mode, but can't
318 if_printf(ifp, "bpf_detach: ifpromisc failed(%d)\n",
322 lwkt_reltoken(&bpf_token);
326 * Open ethernet device. Returns ENXIO for illegal minor device number,
327 * EBUSY if file is open by another process.
331 bpfopen(struct dev_open_args *ap)
333 cdev_t dev = ap->a_head.a_dev;
336 lwkt_gettoken(&bpf_token);
337 if (ap->a_cred->cr_prison) {
338 lwkt_reltoken(&bpf_token);
344 * Each minor can be opened by only one process. If the requested
345 * minor is in use, return EBUSY.
348 lwkt_reltoken(&bpf_token);
352 d = kmalloc(sizeof *d, M_BPF, M_WAITOK | M_ZERO);
354 d->bd_bufsize = bpf_bufsize;
357 callout_init(&d->bd_callout);
358 lwkt_reltoken(&bpf_token);
364 bpfclone(struct dev_clone_args *ap)
368 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(bpf), 0);
369 ap->a_dev = make_only_dev(&bpf_ops, unit, 0, 0, 0600, "bpf%d", unit);
375 * Close the descriptor by detaching it from its interface,
376 * deallocating its buffers, and marking it free.
380 bpfclose(struct dev_close_args *ap)
382 cdev_t dev = ap->a_head.a_dev;
383 struct bpf_d *d = dev->si_drv1;
385 lwkt_gettoken(&bpf_token);
386 funsetown(&d->bd_sigio);
387 if (d->bd_state == BPF_WAITING)
388 callout_stop(&d->bd_callout);
389 d->bd_state = BPF_IDLE;
390 if (d->bd_bif != NULL)
394 if (dev->si_uminor >= BPF_PREALLOCATED_UNITS) {
395 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(bpf), dev->si_uminor);
399 lwkt_reltoken(&bpf_token);
405 * Rotate the packet buffers in descriptor d. Move the store buffer
406 * into the hold slot, and the free buffer into the store slot.
407 * Zero the length of the new store buffer.
409 #define ROTATE_BUFFERS(d) \
410 (d)->bd_hbuf = (d)->bd_sbuf; \
411 (d)->bd_hlen = (d)->bd_slen; \
412 (d)->bd_sbuf = (d)->bd_fbuf; \
416 * bpfread - read next chunk of packets from buffers
419 bpfread(struct dev_read_args *ap)
421 cdev_t dev = ap->a_head.a_dev;
422 struct bpf_d *d = dev->si_drv1;
426 lwkt_gettoken(&bpf_token);
428 * Restrict application to use a buffer the same size as
431 if (ap->a_uio->uio_resid != d->bd_bufsize) {
432 lwkt_reltoken(&bpf_token);
436 if (d->bd_state == BPF_WAITING)
437 callout_stop(&d->bd_callout);
438 timed_out = (d->bd_state == BPF_TIMED_OUT);
439 d->bd_state = BPF_IDLE;
441 * If the hold buffer is empty, then do a timed sleep, which
442 * ends when the timeout expires or when enough packets
443 * have arrived to fill the store buffer.
445 while (d->bd_hbuf == NULL) {
446 if ((d->bd_immediate || (ap->a_ioflag & IO_NDELAY) || timed_out)
447 && d->bd_slen != 0) {
449 * A packet(s) either arrived since the previous,
450 * We're in immediate mode, or are reading
451 * in non-blocking mode, and a packet(s)
452 * either arrived since the previous
453 * read or arrived while we were asleep.
454 * Rotate the buffers and return what's here.
461 * No data is available, check to see if the bpf device
462 * is still pointed at a real interface. If not, return
463 * ENXIO so that the userland process knows to rebind
464 * it before using it again.
466 if (d->bd_bif == NULL) {
467 lwkt_reltoken(&bpf_token);
471 if (ap->a_ioflag & IO_NDELAY) {
472 lwkt_reltoken(&bpf_token);
475 error = tsleep(d, PCATCH, "bpf", d->bd_rtout);
476 if (error == EINTR || error == ERESTART) {
477 lwkt_reltoken(&bpf_token);
480 if (error == EWOULDBLOCK) {
482 * On a timeout, return what's in the buffer,
483 * which may be nothing. If there is something
484 * in the store buffer, we can rotate the buffers.
488 * We filled up the buffer in between
489 * getting the timeout and arriving
490 * here, so we don't need to rotate.
494 if (d->bd_slen == 0) {
495 lwkt_reltoken(&bpf_token);
503 * At this point, we know we have something in the hold slot.
507 * Move data from hold buffer into user space.
508 * We know the entire buffer is transferred since
509 * we checked above that the read buffer is bpf_bufsize bytes.
511 error = uiomove(d->bd_hbuf, d->bd_hlen, ap->a_uio);
513 d->bd_fbuf = d->bd_hbuf;
516 lwkt_reltoken(&bpf_token);
523 * If there are processes sleeping on this descriptor, wake them up.
526 bpf_wakeup(struct bpf_d *d)
528 if (d->bd_state == BPF_WAITING) {
529 callout_stop(&d->bd_callout);
530 d->bd_state = BPF_IDLE;
533 if (d->bd_async && d->bd_sig && d->bd_sigio)
534 pgsigio(d->bd_sigio, d->bd_sig, 0);
536 KNOTE(&d->bd_kq.ki_note, 0);
540 bpf_timed_out(void *arg)
542 struct bpf_d *d = (struct bpf_d *)arg;
544 if (d->bd_state == BPF_WAITING) {
545 d->bd_state = BPF_TIMED_OUT;
552 bpf_output_dispatch(netmsg_t msg)
554 struct netmsg_bpf_output *bmsg = (struct netmsg_bpf_output *)msg;
555 struct ifnet *ifp = bmsg->nm_ifp;
559 * The driver frees the mbuf.
561 error = ifp->if_output(ifp, bmsg->nm_mbuf, bmsg->nm_dst, NULL);
562 lwkt_replymsg(&msg->lmsg, error);
566 bpfwrite(struct dev_write_args *ap)
568 cdev_t dev = ap->a_head.a_dev;
569 struct bpf_d *d = dev->si_drv1;
575 struct netmsg_bpf_output bmsg;
577 lwkt_gettoken(&bpf_token);
578 if (d->bd_bif == NULL) {
579 lwkt_reltoken(&bpf_token);
583 ifp = d->bd_bif->bif_ifp;
585 if (ap->a_uio->uio_resid == 0) {
586 lwkt_reltoken(&bpf_token);
590 error = bpf_movein(ap->a_uio, (int)d->bd_bif->bif_dlt, &m,
591 &dst, &datlen, d->bd_wfilter);
593 lwkt_reltoken(&bpf_token);
597 if (datlen > ifp->if_mtu) {
599 lwkt_reltoken(&bpf_token);
604 dst.sa_family = pseudo_AF_HDRCMPLT;
606 netmsg_init(&bmsg.base, NULL, &curthread->td_msgport,
607 0, bpf_output_dispatch);
612 ret = lwkt_domsg(netisr_cpuport(0), &bmsg.base.lmsg, 0);
613 lwkt_reltoken(&bpf_token);
619 * Reset a descriptor by flushing its packet buffer and clearing the
620 * receive and drop counts. Should be called at splimp.
623 bpf_resetd(struct bpf_d *d)
626 /* Free the hold buffer. */
627 d->bd_fbuf = d->bd_hbuf;
637 * FIONREAD Check for read packet available.
638 * SIOCGIFADDR Get interface address - convenient hook to driver.
639 * BIOCGBLEN Get buffer len [for read()].
640 * BIOCSETF Set ethernet read filter.
641 * BIOCSETWF Set ethernet write filter.
642 * BIOCFLUSH Flush read packet buffer.
643 * BIOCPROMISC Put interface into promiscuous mode.
644 * BIOCGDLT Get link layer type.
645 * BIOCGETIF Get interface name.
646 * BIOCSETIF Set interface.
647 * BIOCSRTIMEOUT Set read timeout.
648 * BIOCGRTIMEOUT Get read timeout.
649 * BIOCGSTATS Get packet stats.
650 * BIOCIMMEDIATE Set immediate mode.
651 * BIOCVERSION Get filter language version.
652 * BIOCGHDRCMPLT Get "header already complete" flag
653 * BIOCSHDRCMPLT Set "header already complete" flag
654 * BIOCGSEESENT Get "see packets sent" flag
655 * BIOCSSEESENT Set "see packets sent" flag
656 * BIOCLOCK Set "locked" flag
660 bpfioctl(struct dev_ioctl_args *ap)
662 cdev_t dev = ap->a_head.a_dev;
663 struct bpf_d *d = dev->si_drv1;
666 lwkt_gettoken(&bpf_token);
667 if (d->bd_state == BPF_WAITING)
668 callout_stop(&d->bd_callout);
669 d->bd_state = BPF_IDLE;
671 if (d->bd_locked == 1) {
690 lwkt_reltoken(&bpf_token);
700 * Check for read packet available.
710 *(int *)ap->a_data = n;
718 if (d->bd_bif == NULL) {
721 ifp = d->bd_bif->bif_ifp;
722 ifnet_serialize_all(ifp);
723 error = ifp->if_ioctl(ifp, ap->a_cmd,
724 ap->a_data, ap->a_cred);
725 ifnet_deserialize_all(ifp);
731 * Get buffer len [for read()].
734 *(u_int *)ap->a_data = d->bd_bufsize;
741 if (d->bd_bif != NULL) {
744 u_int size = *(u_int *)ap->a_data;
746 if (size > bpf_maxbufsize)
747 *(u_int *)ap->a_data = size = bpf_maxbufsize;
748 else if (size < BPF_MINBUFSIZE)
749 *(u_int *)ap->a_data = size = BPF_MINBUFSIZE;
750 d->bd_bufsize = size;
755 * Set link layer read filter.
759 error = bpf_setf(d, (struct bpf_program *)ap->a_data,
764 * Flush read packet buffer.
771 * Put interface into promiscuous mode.
774 if (d->bd_bif == NULL) {
776 * No interface attached yet.
781 if (d->bd_promisc == 0) {
782 error = ifpromisc(d->bd_bif->bif_ifp, 1);
789 * Get device parameters.
792 if (d->bd_bif == NULL)
795 *(u_int *)ap->a_data = d->bd_bif->bif_dlt;
799 * Get a list of supported data link types.
802 if (d->bd_bif == NULL) {
805 error = bpf_getdltlist(d,
806 (struct bpf_dltlist *)ap->a_data);
811 * Set data link type.
814 if (d->bd_bif == NULL)
817 error = bpf_setdlt(d, *(u_int *)ap->a_data);
821 * Get interface name.
824 if (d->bd_bif == NULL) {
827 struct ifnet *const ifp = d->bd_bif->bif_ifp;
828 struct ifreq *const ifr = (struct ifreq *)ap->a_data;
830 strlcpy(ifr->ifr_name, ifp->if_xname,
831 sizeof ifr->ifr_name);
839 error = bpf_setif(d, (struct ifreq *)ap->a_data);
847 struct timeval *tv = (struct timeval *)ap->a_data;
850 * Subtract 1 tick from tvtohz() since this isn't
853 if ((error = itimerfix(tv)) == 0)
854 d->bd_rtout = tvtohz_low(tv);
863 struct timeval *tv = (struct timeval *)ap->a_data;
865 tv->tv_sec = d->bd_rtout / hz;
866 tv->tv_usec = (d->bd_rtout % hz) * ustick;
875 struct bpf_stat *bs = (struct bpf_stat *)ap->a_data;
877 bs->bs_recv = d->bd_rcount;
878 bs->bs_drop = d->bd_dcount;
883 * Set immediate mode.
886 d->bd_immediate = *(u_int *)ap->a_data;
891 struct bpf_version *bv = (struct bpf_version *)ap->a_data;
893 bv->bv_major = BPF_MAJOR_VERSION;
894 bv->bv_minor = BPF_MINOR_VERSION;
899 * Get "header already complete" flag
902 *(u_int *)ap->a_data = d->bd_hdrcmplt;
906 * Set "header already complete" flag
909 d->bd_hdrcmplt = *(u_int *)ap->a_data ? 1 : 0;
913 * Get "see sent packets" flag
916 *(u_int *)ap->a_data = d->bd_seesent;
920 * Set "see sent packets" flag
923 d->bd_seesent = *(u_int *)ap->a_data;
926 case FIOASYNC: /* Send signal on receive packets */
927 d->bd_async = *(int *)ap->a_data;
931 error = fsetown(*(int *)ap->a_data, &d->bd_sigio);
935 *(int *)ap->a_data = fgetown(&d->bd_sigio);
938 /* This is deprecated, FIOSETOWN should be used instead. */
940 error = fsetown(-(*(int *)ap->a_data), &d->bd_sigio);
943 /* This is deprecated, FIOGETOWN should be used instead. */
945 *(int *)ap->a_data = -fgetown(&d->bd_sigio);
948 case BIOCSRSIG: /* Set receive signal */
952 sig = *(u_int *)ap->a_data;
961 *(u_int *)ap->a_data = d->bd_sig;
967 lwkt_reltoken(&bpf_token);
973 * Set d's packet filter program to fp. If this file already has a filter,
974 * free it and replace it. Returns EINVAL for bogus requests.
977 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
979 struct bpf_insn *fcode, *old;
980 u_int wfilter, flen, size;
982 if (cmd == BIOCSETWF) {
989 if (fp->bf_insns == NULL) {
993 d->bd_wfilter = NULL;
995 d->bd_rfilter = NULL;
1002 if (flen > BPF_MAXINSNS)
1005 size = flen * sizeof *fp->bf_insns;
1006 fcode = (struct bpf_insn *)kmalloc(size, M_BPF, M_WAITOK);
1007 if (copyin(fp->bf_insns, fcode, size) == 0 &&
1008 bpf_validate(fcode, (int)flen)) {
1010 d->bd_wfilter = fcode;
1012 d->bd_rfilter = fcode;
1019 kfree(fcode, M_BPF);
1024 * Detach a file from its current interface (if attached at all) and attach
1025 * to the interface indicated by the name stored in ifr.
1026 * Return an errno or 0.
1029 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1033 struct ifnet *theywant;
1035 theywant = ifunit(ifr->ifr_name);
1036 if (theywant == NULL)
1040 * Look through attached interfaces for the named one.
1042 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1043 struct ifnet *ifp = bp->bif_ifp;
1045 if (ifp == NULL || ifp != theywant)
1047 /* skip additional entry */
1048 if (bp->bif_driverp != &ifp->if_bpf)
1051 * We found the requested interface.
1052 * Allocate the packet buffers if we need to.
1053 * If we're already attached to requested interface,
1054 * just flush the buffer.
1056 if (d->bd_sbuf == NULL) {
1057 error = bpf_allocbufs(d);
1061 if (bp != d->bd_bif) {
1062 if (d->bd_bif != NULL) {
1064 * Detach if attached to something else.
1079 static struct filterops bpf_read_filtops =
1080 { FILTEROP_ISFD, NULL, bpf_filter_detach, bpf_filter_read };
1083 bpfkqfilter(struct dev_kqfilter_args *ap)
1085 cdev_t dev = ap->a_head.a_dev;
1086 struct knote *kn = ap->a_kn;
1087 struct klist *klist;
1090 lwkt_gettoken(&bpf_token);
1092 if (d->bd_bif == NULL) {
1094 lwkt_reltoken(&bpf_token);
1099 switch (kn->kn_filter) {
1101 kn->kn_fop = &bpf_read_filtops;
1102 kn->kn_hook = (caddr_t)d;
1105 ap->a_result = EOPNOTSUPP;
1106 lwkt_reltoken(&bpf_token);
1110 klist = &d->bd_kq.ki_note;
1111 knote_insert(klist, kn);
1112 lwkt_reltoken(&bpf_token);
1118 bpf_filter_detach(struct knote *kn)
1120 struct klist *klist;
1123 d = (struct bpf_d *)kn->kn_hook;
1124 klist = &d->bd_kq.ki_note;
1125 knote_remove(klist, kn);
1129 bpf_filter_read(struct knote *kn, long hint)
1134 d = (struct bpf_d *)kn->kn_hook;
1135 if (d->bd_hlen != 0 ||
1136 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1140 /* Start the read timeout if necessary. */
1141 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1142 callout_reset(&d->bd_callout, d->bd_rtout,
1144 d->bd_state = BPF_WAITING;
1153 * Process the packet pkt of length pktlen. The packet is parsed
1154 * by each listener's filter, and if accepted, stashed into the
1155 * corresponding buffer.
1158 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1165 lwkt_gettoken(&bpf_token);
1168 lwkt_reltoken(&bpf_token);
1173 * Note that the ipl does not have to be raised at this point.
1174 * The only problem that could arise here is that if two different
1175 * interfaces shared any data. This is not the case.
1177 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1179 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1185 catchpacket(d, pkt, pktlen, slen, ovbcopy, &tv);
1188 lwkt_reltoken(&bpf_token);
1192 * Copy data from an mbuf chain into a buffer. This code is derived
1193 * from m_copydata in sys/uipc_mbuf.c.
1196 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1198 const struct mbuf *m;
1207 count = min(m->m_len, len);
1208 bcopy(mtod(m, void *), dst, count);
1216 * Process the packet in the mbuf chain m. The packet is parsed by each
1217 * listener's filter, and if accepted, stashed into the corresponding
1221 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1228 lwkt_gettoken(&bpf_token);
1231 lwkt_reltoken(&bpf_token);
1235 /* Don't compute pktlen, if no descriptor is attached. */
1236 if (SLIST_EMPTY(&bp->bif_dlist)) {
1237 lwkt_reltoken(&bpf_token);
1241 pktlen = m_lengthm(m, NULL);
1243 SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1244 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1247 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1253 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy,
1257 lwkt_reltoken(&bpf_token);
1261 * Incoming linkage from device drivers, where we have a mbuf chain
1262 * but need to prepend some arbitrary header from a linear buffer.
1264 * Con up a minimal dummy header to pacify bpf. Allocate (only) a
1265 * struct m_hdr on the stack. This is safe as bpf only reads from the
1266 * fields in this header that we initialize, and will not try to free
1267 * it or keep a pointer to it.
1270 bpf_mtap_hdr(struct bpf_if *arg, caddr_t data, u_int dlen, struct mbuf *m, u_int direction)
1279 return bpf_mtap(arg, (struct mbuf *) &mh);
1283 bpf_mtap_family(struct bpf_if *bp, struct mbuf *m, sa_family_t family)
1287 KKASSERT(family != AF_UNSPEC);
1289 family4 = (u_int)family;
1290 bpf_ptap(bp, m, &family4, sizeof(family4));
1294 * Process the packet in the mbuf chain m with the header in m prepended.
1295 * The packet is parsed by each listener's filter, and if accepted,
1296 * stashed into the corresponding buffer.
1299 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen)
1304 * Craft on-stack mbuf suitable for passing to bpf_mtap.
1305 * Note that we cut corners here; we only setup what's
1306 * absolutely needed--this mbuf should never go anywhere else.
1309 mb.m_data = __DECONST(void *, data); /* LINTED */
1311 mb.m_pkthdr.rcvif = m->m_pkthdr.rcvif;
1317 * Move the packet data from interface memory (pkt) into the
1318 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1319 * otherwise 0. "copy" is the routine called to do the actual data
1320 * transfer. bcopy is passed in to copy contiguous chunks, while
1321 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1322 * pkt is really an mbuf.
1325 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1326 void (*cpfn)(const void *, void *, size_t),
1327 const struct timeval *tv)
1331 int hdrlen = d->bd_bif->bif_hdrlen;
1334 * Figure out how many bytes to move. If the packet is
1335 * greater or equal to the snapshot length, transfer that
1336 * much. Otherwise, transfer the whole packet (unless
1337 * we hit the buffer size limit).
1339 totlen = hdrlen + min(snaplen, pktlen);
1340 if (totlen > d->bd_bufsize)
1341 totlen = d->bd_bufsize;
1344 * Round up the end of the previous packet to the next longword.
1346 curlen = BPF_WORDALIGN(d->bd_slen);
1347 if (curlen + totlen > d->bd_bufsize) {
1349 * This packet will overflow the storage buffer.
1350 * Rotate the buffers if we can, then wakeup any
1353 if (d->bd_fbuf == NULL) {
1355 * We haven't completed the previous read yet,
1356 * so drop the packet.
1364 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) {
1366 * Immediate mode is set, or the read timeout has
1367 * already expired during a select call. A packet
1368 * arrived, so the reader should be woken up.
1374 * Append the bpf header.
1376 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1377 hp->bh_tstamp = *tv;
1378 hp->bh_datalen = pktlen;
1379 hp->bh_hdrlen = hdrlen;
1381 * Copy the packet data into the store buffer and update its length.
1383 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1384 d->bd_slen = curlen + totlen;
1391 * Initialize all nonzero fields of a descriptor.
1394 bpf_allocbufs(struct bpf_d *d)
1396 d->bd_fbuf = kmalloc(d->bd_bufsize, M_BPF, M_WAITOK);
1397 d->bd_sbuf = kmalloc(d->bd_bufsize, M_BPF, M_WAITOK);
1404 * Free buffers and packet filter program currently in use by a descriptor.
1408 bpf_freed(struct bpf_d *d)
1411 * We don't need to lock out interrupts since this descriptor has
1412 * been detached from its interface and it yet hasn't been marked
1415 if (d->bd_sbuf != NULL) {
1416 kfree(d->bd_sbuf, M_BPF);
1417 if (d->bd_hbuf != NULL)
1418 kfree(d->bd_hbuf, M_BPF);
1419 if (d->bd_fbuf != NULL)
1420 kfree(d->bd_fbuf, M_BPF);
1423 kfree(d->bd_rfilter, M_BPF);
1425 kfree(d->bd_wfilter, M_BPF);
1429 * Attach an interface to bpf. ifp is a pointer to the structure
1430 * defining the interface to be attached, dlt is the link layer type,
1431 * and hdrlen is the fixed size of the link header (variable length
1432 * headers are not yet supported).
1435 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1437 bpfattach_dlt(ifp, dlt, hdrlen, &ifp->if_bpf);
1441 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1445 bp = kmalloc(sizeof *bp, M_BPF, M_WAITOK | M_ZERO);
1447 lwkt_gettoken(&bpf_token);
1449 SLIST_INIT(&bp->bif_dlist);
1452 bp->bif_driverp = driverp;
1453 *bp->bif_driverp = NULL;
1455 bp->bif_next = bpf_iflist;
1459 * Compute the length of the bpf header. This is not necessarily
1460 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1461 * that the network layer header begins on a longword boundary (for
1462 * performance reasons and to alleviate alignment restrictions).
1464 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1466 lwkt_reltoken(&bpf_token);
1469 if_printf(ifp, "bpf attached\n");
1473 * Detach bpf from an interface. This involves detaching each descriptor
1474 * associated with the interface, and leaving bd_bif NULL. Notify each
1475 * descriptor as it's detached so that any sleepers wake up and get
1479 bpfdetach(struct ifnet *ifp)
1481 struct bpf_if *bp, *bp_prev;
1484 lwkt_gettoken(&bpf_token);
1486 /* Locate BPF interface information */
1488 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1489 if (ifp == bp->bif_ifp)
1494 /* Interface wasn't attached */
1495 if (bp->bif_ifp == NULL) {
1496 lwkt_reltoken(&bpf_token);
1497 kprintf("bpfdetach: %s was not attached\n", ifp->if_xname);
1501 while ((d = SLIST_FIRST(&bp->bif_dlist)) != NULL) {
1506 if (bp_prev != NULL)
1507 bp_prev->bif_next = bp->bif_next;
1509 bpf_iflist = bp->bif_next;
1513 lwkt_reltoken(&bpf_token);
1517 * Get a list of available data link type of the interface.
1520 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1526 ifp = d->bd_bif->bif_ifp;
1529 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1530 if (bp->bif_ifp != ifp)
1532 if (bfl->bfl_list != NULL) {
1533 if (n >= bfl->bfl_len) {
1536 error = copyout(&bp->bif_dlt,
1537 bfl->bfl_list + n, sizeof(u_int));
1546 * Set the data link type of a BPF instance.
1549 bpf_setdlt(struct bpf_d *d, u_int dlt)
1551 int error, opromisc;
1555 if (d->bd_bif->bif_dlt == dlt)
1557 ifp = d->bd_bif->bif_ifp;
1558 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1559 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1563 opromisc = d->bd_promisc;
1568 error = ifpromisc(bp->bif_ifp, 1);
1570 if_printf(bp->bif_ifp,
1571 "bpf_setdlt: ifpromisc failed (%d)\n",
1578 return(bp == NULL ? EINVAL : 0);
1584 lwkt_gettoken(&bpf_token);
1590 lwkt_reltoken(&bpf_token);
1594 bpf_drvinit(void *unused)
1598 make_autoclone_dev(&bpf_ops, &DEVFS_CLONE_BITMAP(bpf),
1599 bpfclone, 0, 0, 0600, "bpf");
1600 for (i = 0; i < BPF_PREALLOCATED_UNITS; i++) {
1601 make_dev(&bpf_ops, i, 0, 0, 0600, "bpf%d", i);
1602 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(bpf), i);
1607 bpf_drvuninit(void *unused)
1609 devfs_clone_handler_del("bpf");
1610 dev_ops_remove_all(&bpf_ops);
1611 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(bpf));
1614 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1615 SYSUNINIT(bpfdev, SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvuninit, NULL);
1619 * NOP stubs to allow bpf-using drivers to load and function.
1621 * A 'better' implementation would allow the core bpf functionality
1622 * to be loaded at runtime.
1626 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1631 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1636 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen)
1641 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1646 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1651 bpfdetach(struct ifnet *ifp)
1656 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1658 return -1; /* "no filter" behaviour */