2 * Copyright (c) 2006 Paolo Abeni (Italy)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * USB sniffing API implementation for Linux platform
31 * By Paolo Abeni <paolo.abeni@email.it>
32 * Modifications: Kris Katterjohn <katterjohn@gmail.com>
41 #include "pcap-usb-linux.h"
46 #ifdef NEED_STRERROR_H
58 #include <netinet/in.h>
59 #include <sys/ioctl.h>
61 #include <sys/utsname.h>
62 #ifdef HAVE_LINUX_USBDEVICE_FS_H
64 * We might need <linux/compiler.h> to define __user for
65 * <linux/usbdevice_fs.h>.
67 #ifdef HAVE_LINUX_COMPILER_H
68 #include <linux/compiler.h>
69 #endif /* HAVE_LINUX_COMPILER_H */
70 #include <linux/usbdevice_fs.h>
71 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
73 #include "diag-control.h"
75 #define USB_IFACE "usbmon"
76 #define USB_LINE_LEN 4096
78 #if __BYTE_ORDER == __LITTLE_ENDIAN
83 #define htols(s) bswap_16(s)
84 #define htoll(l) bswap_32(l)
85 #define htol64(ll) bswap_64(ll)
88 struct mon_bin_stats
{
96 size_t data_len
; /* Length of data (can be zero) */
99 struct mon_bin_mfetch
{
100 int32_t *offvec
; /* Vector of events fetched */
101 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
102 int32_t nflush
; /* Number of events to flush */
105 #define MON_IOC_MAGIC 0x92
107 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
108 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
109 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
110 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
111 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
112 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
113 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
114 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
116 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
117 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
118 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
119 #define MON_BIN_ERROR 0x8
122 * Private data for capturing on Linux USB.
124 struct pcap_usb_linux
{
125 u_char
*mmapbuf
; /* memory-mapped region pointer */
126 size_t mmapbuflen
; /* size of region */
131 /* forward declaration */
132 static int usb_activate(pcap_t
*);
133 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
134 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
135 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
136 static int usb_inject_linux(pcap_t
*, const void *, int);
137 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
138 static void usb_cleanup_linux_mmap(pcap_t
*);
140 /* facility to add an USB device to the device list*/
142 usb_dev_add(pcap_if_list_t
*devlistp
, int n
, char *err_str
)
146 snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
148 * XXX - is there any notion of "up" and "running"?
152 * As this refers to all buses, there's no notion of
153 * "connected" vs. "disconnected", as that's a property
154 * that would apply to a particular USB interface.
156 if (add_dev(devlistp
, dev_name
,
157 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
,
158 "Raw USB traffic, all USB buses", err_str
) == NULL
)
162 * XXX - is there a way to determine whether anything's
163 * plugged into this bus interface or not, and set
164 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
165 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
167 snprintf(dev_descr
, 30, "Raw USB traffic, bus number %d", n
);
168 if (add_dev(devlistp
, dev_name
, 0, dev_descr
, err_str
) == NULL
)
176 usb_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
178 char usb_mon_dir
[PATH_MAX
];
179 char *usb_mon_prefix
;
180 size_t usb_mon_prefix_len
;
188 * We require 2.6.27 or later kernels, so we have binary-mode support.
189 * What do the device names look like?
190 * Split LINUX_USB_MON_DEV into a directory that we'll
191 * scan and a file name prefix that we'll check for.
193 pcap_strlcpy(usb_mon_dir
, LINUX_USB_MON_DEV
, sizeof usb_mon_dir
);
194 usb_mon_prefix
= strrchr(usb_mon_dir
, '/');
195 if (usb_mon_prefix
== NULL
) {
197 * This "shouldn't happen". Just give up if it
202 *usb_mon_prefix
++ = '\0';
203 usb_mon_prefix_len
= strlen(usb_mon_prefix
);
206 * Open the directory and scan it.
208 dir
= opendir(usb_mon_dir
);
210 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
214 * Is this a usbmon device?
216 if (strncmp(name
, usb_mon_prefix
, usb_mon_prefix_len
) != 0)
220 * What's the device number?
222 if (sscanf(&name
[usb_mon_prefix_len
], "%d", &n
) == 0)
223 continue; /* failed */
225 ret
= usb_dev_add(devlistp
, n
, err_str
);
234 * Matches what's in mon_bin.c in the Linux kernel.
236 #define MIN_RING_SIZE (8*1024)
237 #define MAX_RING_SIZE (1200*1024)
240 usb_set_ring_size(pcap_t
* handle
, int header_size
)
243 * A packet from binary usbmon has:
245 * 1) a fixed-length header, of size header_size;
246 * 2) descriptors, for isochronous transfers;
249 * The kernel buffer has a size, defaulting to 300KB, with a
250 * minimum of 8KB and a maximum of 1200KB. The size is set with
251 * the MON_IOCT_RING_SIZE ioctl; the size passed in is rounded up
254 * No more than {buffer size}/5 bytes worth of payload is saved.
255 * Therefore, if we subtract the fixed-length size from the
256 * snapshot length, we have the biggest payload we want (we
257 * don't worry about the descriptors - if we have descriptors,
258 * we'll just discard the last bit of the payload to get it
259 * to fit). We multiply that result by 5 and set the buffer
260 * size to that value.
264 if (handle
->snapshot
< header_size
)
265 handle
->snapshot
= header_size
;
266 /* The maximum snapshot size is small enough that this won't overflow */
267 ring_size
= (handle
->snapshot
- header_size
) * 5;
270 * Will this get an error?
271 * (There's no wqy to query the minimum or maximum, so we just
272 * copy the value from the kernel source. We don't round it
273 * up to a multiple of the page size.)
275 if (ring_size
> MAX_RING_SIZE
) {
277 * Yes. Lower the ring size to the maximum, and set the
278 * snapshot length to the value that would give us a
281 ring_size
= MAX_RING_SIZE
;
282 handle
->snapshot
= header_size
+ (MAX_RING_SIZE
/5);
283 } else if (ring_size
< MIN_RING_SIZE
) {
285 * Yes. Raise the ring size to the minimum, but leave
286 * the snapshot length unchanged, so we show the
287 * callback no more data than specified by the
290 ring_size
= MIN_RING_SIZE
;
293 if (ioctl(handle
->fd
, MON_IOCT_RING_SIZE
, ring_size
) == -1) {
294 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
295 errno
, "Can't set ring size from fd %d", handle
->fd
);
302 int usb_mmap(pcap_t
* handle
)
304 struct pcap_usb_linux
*handlep
= handle
->priv
;
308 * Attempt to set the ring size as appropriate for the snapshot
309 * length, reducing the snapshot length if that'd make the ring
310 * bigger than the kernel supports.
312 len
= usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header_mmapped
));
314 /* Failed. Fall back on non-memory-mapped access. */
318 handlep
->mmapbuflen
= len
;
319 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
320 MAP_SHARED
, handle
->fd
, 0);
321 if (handlep
->mmapbuf
== MAP_FAILED
) {
323 * Failed. We don't treat that as a fatal error, we
324 * just try to fall back on non-memory-mapped access.
331 #ifdef HAVE_LINUX_USBDEVICE_FS_H
333 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
335 #define USB_DIR_IN 0x80
336 #define USB_TYPE_STANDARD 0x00
337 #define USB_RECIP_DEVICE 0x00
339 #define USB_REQ_GET_DESCRIPTOR 6
341 #define USB_DT_DEVICE 1
342 #define USB_DT_CONFIG 2
344 #define USB_DEVICE_DESCRIPTOR_SIZE 18
345 #define USB_CONFIG_DESCRIPTOR_SIZE 9
347 /* probe the descriptors of the devices attached to the bus */
348 /* the descriptors will end up in the captured packet stream */
349 /* and be decoded by external apps like wireshark */
350 /* without these identifying probes packet data can't be fully decoded */
352 probe_devices(int bus
)
354 struct usbdevfs_ctrltransfer ctrl
;
357 char busdevpath
[sizeof("/dev/bus/usb/000/") + NAME_MAX
];
359 uint8_t descriptor
[USB_DEVICE_DESCRIPTOR_SIZE
];
360 uint8_t configdesc
[USB_CONFIG_DESCRIPTOR_SIZE
];
362 /* scan usb bus directories for device nodes */
363 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d", bus
);
364 dir
= opendir(busdevpath
);
368 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
370 char* name
= data
->d_name
;
375 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
377 fd
= open(busdevpath
, O_RDWR
);
382 * Sigh. Different kernels have different member names
383 * for this structure.
385 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
386 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
387 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
388 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
390 ctrl
.wLength
= sizeof(descriptor
);
392 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
393 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
394 ctrl
.value
= USB_DT_DEVICE
<< 8;
396 ctrl
.length
= sizeof(descriptor
);
398 ctrl
.data
= descriptor
;
399 ctrl
.timeout
= CTRL_TIMEOUT
;
401 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
403 /* Request CONFIGURATION descriptor alone to know wTotalLength */
404 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
405 ctrl
.wValue
= USB_DT_CONFIG
<< 8;
406 ctrl
.wLength
= sizeof(configdesc
);
408 ctrl
.value
= USB_DT_CONFIG
<< 8;
409 ctrl
.length
= sizeof(configdesc
);
411 ctrl
.data
= configdesc
;
412 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
414 uint16_t wtotallength
;
415 wtotallength
= EXTRACT_LE_U_2(&configdesc
[2]);
416 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
417 ctrl
.wLength
= wtotallength
;
419 ctrl
.length
= wtotallength
;
421 ctrl
.data
= malloc(wtotallength
);
423 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
431 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
434 usb_create(const char *device
, char *ebuf
, int *is_ours
)
441 /* Does this look like a USB monitoring device? */
442 cp
= strrchr(device
, '/');
445 /* Does it begin with USB_IFACE? */
446 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
447 /* Nope, doesn't begin with USB_IFACE */
451 /* Yes - is USB_IFACE followed by a number? */
452 cp
+= sizeof USB_IFACE
- 1;
453 devnum
= strtol(cp
, &cpend
, 10);
454 if (cpend
== cp
|| *cpend
!= '\0') {
455 /* Not followed by a number. */
460 /* Followed by a non-valid number. */
465 /* OK, it's probably ours. */
468 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_usb_linux
);
472 p
->activate_op
= usb_activate
;
477 usb_activate(pcap_t
* handle
)
479 struct pcap_usb_linux
*handlep
= handle
->priv
;
480 char full_path
[USB_LINE_LEN
];
483 * Turn a negative snapshot value (invalid), a snapshot value of
484 * 0 (unspecified), or a value bigger than the normal maximum
485 * value, into the maximum allowed value.
487 * If some application really *needs* a bigger snapshot
488 * length, we should just increase MAXIMUM_SNAPLEN.
490 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
491 handle
->snapshot
= MAXIMUM_SNAPLEN
;
493 /* Initialize some components of the pcap structure. */
494 handle
->bufsize
= handle
->snapshot
;
496 handle
->linktype
= DLT_USB_LINUX
;
498 handle
->inject_op
= usb_inject_linux
;
499 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
500 handle
->setdirection_op
= usb_setdirection_linux
;
501 handle
->set_datalink_op
= NULL
; /* can't change data link type */
502 handle
->getnonblock_op
= pcap_getnonblock_fd
;
503 handle
->setnonblock_op
= pcap_setnonblock_fd
;
505 /*get usb bus index from device name */
506 if (sscanf(handle
->opt
.device
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
508 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
509 "Can't get USB bus index from %s", handle
->opt
.device
);
514 * We require 2.6.27 or later kernels, so we have binary-mode support.
515 * Try to open the binary interface.
517 snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handlep
->bus_index
);
518 handle
->fd
= open(full_path
, O_RDONLY
, 0);
522 * The attempt failed; why?
528 * The device doesn't exist.
529 * That could either mean that there's
530 * no support for monitoring USB buses
531 * (which probably means "the usbmon
532 * module isn't loaded") or that there
533 * is but that *particular* device
534 * doesn't exist (no "scan all buses"
535 * device if the bus index is 0, no
536 * such bus if the bus index isn't 0).
538 return PCAP_ERROR_NO_SUCH_DEVICE
;
542 * We didn't have permission to open it.
544 DIAG_OFF_FORMAT_TRUNCATION
545 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
546 "Attempt to open %s failed with EACCES - root privileges may be required",
548 DIAG_ON_FORMAT_TRUNCATION
549 return PCAP_ERROR_PERM_DENIED
;
553 * Something went wrong.
555 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
556 PCAP_ERRBUF_SIZE
, errno
,
557 "Can't open USB bus file %s", full_path
);
562 if (handle
->opt
.rfmon
)
565 * Monitor mode doesn't apply to USB devices.
568 return PCAP_ERROR_RFMON_NOTSUP
;
571 /* try to use fast mmap access */
572 if (usb_mmap(handle
))
575 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
576 handle
->stats_op
= usb_stats_linux_bin
;
577 handle
->read_op
= usb_read_linux_mmap
;
578 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
579 #ifdef HAVE_LINUX_USBDEVICE_FS_H
580 probe_devices(handlep
->bus_index
);
584 * "handle->fd" is a real file, so
585 * "select()" and "poll()" work on it.
587 handle
->selectable_fd
= handle
->fd
;
592 * We failed; try plain binary interface access.
594 * Attempt to set the ring size as appropriate for
595 * the snapshot length, reducing the snapshot length
596 * if that'd make the ring bigger than the kernel
599 if (usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header
)) == -1) {
604 handle
->stats_op
= usb_stats_linux_bin
;
605 handle
->read_op
= usb_read_linux_bin
;
606 #ifdef HAVE_LINUX_USBDEVICE_FS_H
607 probe_devices(handlep
->bus_index
);
611 * "handle->fd" is a real file, so "select()" and "poll()"
614 handle
->selectable_fd
= handle
->fd
;
616 /* for plain binary access and text access we need to allocate the read
618 handle
->buffer
= malloc(handle
->bufsize
);
619 if (!handle
->buffer
) {
620 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
629 usb_inject_linux(pcap_t
*handle
, const void *buf _U_
, int size _U_
)
631 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
632 "Packet injection is not supported on USB devices");
637 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
640 * It's guaranteed, at this point, that d is a valid
648 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
650 struct pcap_usb_linux
*handlep
= handle
->priv
;
652 struct mon_bin_stats st
;
653 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
656 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
657 errno
, "Can't read stats from fd %d", handle
->fd
);
661 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
662 stats
->ps_drop
= st
.dropped
;
663 stats
->ps_ifdrop
= 0;
668 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
669 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
672 usb_read_linux_bin(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
674 struct pcap_usb_linux
*handlep
= handle
->priv
;
675 struct mon_bin_get info
;
677 struct pcap_pkthdr pkth
;
678 u_int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
680 /* the usb header is going to be part of 'packet' data*/
681 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
682 info
.data
= (u_char
*)handle
->buffer
+ sizeof(pcap_usb_header
);
683 info
.data_len
= clen
;
685 /* ignore interrupt system call errors */
687 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
688 if (handle
->break_loop
)
690 handle
->break_loop
= 0;
693 } while ((ret
== -1) && (errno
== EINTR
));
697 return 0; /* no data there */
699 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
700 errno
, "Can't read from fd %d", handle
->fd
);
705 * info.hdr->data_len is the number of bytes of isochronous
706 * descriptors (if any) plus the number of bytes of data
707 * provided. There are no isochronous descriptors here,
708 * because we're using the old 48-byte header.
710 * If info.hdr->data_flag is non-zero, there's no URB data;
711 * info.hdr->urb_len is the size of the buffer into which
712 * data is to be placed; it does not represent the amount
713 * of data transferred. If info.hdr->data_flag is zero,
714 * there is URB data, and info.hdr->urb_len is the number
715 * of bytes transmitted or received; it doesn't include
716 * isochronous descriptors.
718 * The kernel may give us more data than the snaplen; if it did,
719 * reduce the data length so that the total number of bytes we
720 * tell our client we have is not greater than the snaplen.
722 if (info
.hdr
->data_len
< clen
)
723 clen
= info
.hdr
->data_len
;
724 info
.hdr
->data_len
= clen
;
725 pkth
.caplen
= sizeof(pcap_usb_header
) + clen
;
726 if (info
.hdr
->data_flag
) {
728 * No data; just base the on-the-wire length on
729 * info.hdr->data_len (so that it's >= the captured
732 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->data_len
;
735 * We got data; base the on-the-wire length on
736 * info.hdr->urb_len, so that it includes data
737 * discarded by the USB monitor device due to
738 * its buffer being too small.
740 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->urb_len
;
742 pkth
.ts
.tv_sec
= (time_t)info
.hdr
->ts_sec
;
743 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
745 if (handle
->fcode
.bf_insns
== NULL
||
746 pcap_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
747 pkth
.len
, pkth
.caplen
)) {
748 handlep
->packets_read
++;
749 callback(user
, &pkth
, handle
->buffer
);
753 return 0; /* didn't pass filter */
757 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
758 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
762 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
764 struct pcap_usb_linux
*handlep
= handle
->priv
;
765 struct mon_bin_mfetch fetch
;
766 int32_t vec
[VEC_SIZE
];
767 struct pcap_pkthdr pkth
;
768 pcap_usb_header_mmapped
* hdr
;
771 u_int clen
, max_clen
;
773 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header_mmapped
);
779 if (PACKET_COUNT_IS_UNLIMITED(max_packets
)) {
781 * There's no limit on the number of packets
782 * to process, so try to fetch VEC_SIZE packets.
787 * Try to fetch as many packets as we have left
788 * to process, or VEC_SIZE packets, whichever
791 * At this point, max_packets > 0 (otherwise,
792 * PACKET_COUNT_IS_UNLIMITED(max_packets)
793 * would be true) and max_packets > packets
794 * (packet starts out as 0, and the test
795 * at the bottom of the loop exits if
796 * max_packets <= packets), so limit is
797 * guaranteed to be > 0.
799 limit
= max_packets
- packets
;
800 if (limit
> VEC_SIZE
)
805 * Try to fetch as many events as possible, up to
806 * the limit, and flush the events we've processed
807 * earlier (nflush) - MON_IOCX_MFETCH does both
808 * (presumably to reduce the number of system
809 * calls in loops like this).
812 fetch
.nfetch
= limit
;
813 fetch
.nflush
= nflush
;
814 /* ignore interrupt system call errors */
816 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
817 if (handle
->break_loop
)
819 handle
->break_loop
= 0;
822 } while ((ret
== -1) && (errno
== EINTR
));
826 return 0; /* no data there */
828 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
829 PCAP_ERRBUF_SIZE
, errno
, "Can't mfetch fd %d",
834 /* keep track of processed events, we will flush them later */
835 nflush
= fetch
.nfetch
;
836 for (i
=0; i
<fetch
.nfetch
; ++i
) {
838 * XXX - we can't check break_loop here, as
839 * we read the indices of packets into a
840 * local variable, so if we're later called
841 * to fetch more packets, those packets will
842 * not be seen - and won't be flushed, either.
844 * Instead, we would have to keep the array
845 * of indices in our private data, along
846 * with the count of packets to flush - or
847 * would have to flush the already-processed
848 * packets if we break out of the loop here.
852 hdr
= (pcap_usb_header_mmapped
*) &handlep
->mmapbuf
[vec
[i
]];
853 if (hdr
->event_type
== '@')
857 * hdr->data_len is the number of bytes of
858 * isochronous descriptors (if any) plus the
859 * number of bytes of data provided.
861 * If hdr->data_flag is non-zero, there's no
862 * URB data; hdr->urb_len is the size of the
863 * buffer into which data is to be placed; it does
864 * not represent the amount of data transferred.
865 * If hdr->data_flag is zero, there is URB data,
866 * and hdr->urb_len is the number of bytes
867 * transmitted or received; it doesn't include
868 * isochronous descriptors.
870 * The kernel may give us more data than the
871 * snaplen; if it did, reduce the data length
872 * so that the total number of bytes we
873 * tell our client we have is not greater than
877 if (hdr
->data_len
< clen
)
878 clen
= hdr
->data_len
;
879 pkth
.caplen
= sizeof(pcap_usb_header_mmapped
) + clen
;
880 if (hdr
->data_flag
) {
882 * No data; just base the on-the-wire length
883 * on hdr->data_len (so that it's >= the
886 pkth
.len
= sizeof(pcap_usb_header_mmapped
) +
890 * We got data; base the on-the-wire length
891 * on hdr->urb_len, so that it includes
892 * data discarded by the USB monitor device
893 * due to its buffer being too small.
895 pkth
.len
= sizeof(pcap_usb_header_mmapped
) +
896 (hdr
->ndesc
* sizeof (usb_isodesc
)) + hdr
->urb_len
;
898 pkth
.ts
.tv_sec
= (time_t)hdr
->ts_sec
;
899 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
901 if (handle
->fcode
.bf_insns
== NULL
||
902 pcap_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
903 pkth
.len
, pkth
.caplen
)) {
904 handlep
->packets_read
++;
905 callback(user
, &pkth
, (u_char
*) hdr
);
911 * If max_packets specifiesg "unlimited", we stop after
914 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) ||
915 (packets
>= max_packets
))
919 /* flush pending events*/
920 if (ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
) == -1) {
921 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
922 errno
, "Can't mflush fd %d", handle
->fd
);
929 usb_cleanup_linux_mmap(pcap_t
* handle
)
931 struct pcap_usb_linux
*handlep
= handle
->priv
;
933 /* if we have a memory-mapped buffer, unmap it */
934 if (handlep
->mmapbuf
!= NULL
) {
935 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
936 handlep
->mmapbuf
= NULL
;
938 pcap_cleanup_live_common(handle
);