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>
40 #include "pcap-usb-linux.h"
41 #include "pcap-usb-linux-common.h"
53 #include <netinet/in.h>
54 #include <sys/ioctl.h>
56 #include <sys/utsname.h>
57 #ifdef HAVE_LINUX_USBDEVICE_FS_H
59 * We might need <linux/compiler.h> to define __user for
60 * <linux/usbdevice_fs.h>.
62 #ifdef HAVE_LINUX_COMPILER_H
63 #include <linux/compiler.h>
64 #endif /* HAVE_LINUX_COMPILER_H */
65 #include <linux/usbdevice_fs.h>
66 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
68 #include "diag-control.h"
70 #define USB_IFACE "usbmon"
72 #define USBMON_DEV_PREFIX "usbmon"
73 #define USBMON_DEV_PREFIX_LEN (sizeof USBMON_DEV_PREFIX - 1)
74 #define USB_LINE_LEN 4096
76 #if __BYTE_ORDER == __LITTLE_ENDIAN
81 #define htols(s) bswap_16(s)
82 #define htoll(l) bswap_32(l)
83 #define htol64(ll) bswap_64(ll)
86 struct mon_bin_stats
{
94 size_t data_len
; /* Length of data (can be zero) */
97 struct mon_bin_mfetch
{
98 int32_t *offvec
; /* Vector of events fetched */
99 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
100 int32_t nflush
; /* Number of events to flush */
103 #define MON_IOC_MAGIC 0x92
105 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
106 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
107 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
108 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
109 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
110 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
111 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
112 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
114 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
115 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
116 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
117 #define MON_BIN_ERROR 0x8
120 * Private data for capturing on Linux USB.
122 struct pcap_usb_linux
{
123 u_char
*mmapbuf
; /* memory-mapped region pointer */
124 size_t mmapbuflen
; /* size of region */
129 /* forward declaration */
130 static int usb_activate(pcap_t
*);
131 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
132 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
133 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
134 static int usb_inject_linux(pcap_t
*, const void *, int);
135 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
136 static void usb_cleanup_linux_mmap(pcap_t
*);
138 /* facility to add an USB device to the device list*/
140 usb_dev_add(pcap_if_list_t
*devlistp
, int n
, char *err_str
)
144 snprintf(dev_name
, sizeof(dev_name
), USB_IFACE
"%d", n
);
146 * XXX - is there any notion of "up" and "running"?
150 * As this refers to all buses, there's no notion of
151 * "connected" vs. "disconnected", as that's a property
152 * that would apply to a particular USB interface.
154 if (pcapint_add_dev(devlistp
, dev_name
,
155 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
,
156 "Raw USB traffic, all USB buses", err_str
) == NULL
)
160 * XXX - is there a way to determine whether anything's
161 * plugged into this bus interface or not, and set
162 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
163 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
165 snprintf(dev_descr
, sizeof(dev_descr
), "Raw USB traffic, bus number %d", n
);
166 if (pcapint_add_dev(devlistp
, dev_name
, 0, dev_descr
, err_str
) == NULL
)
174 usb_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
183 * We require 2.6.27 or later kernels, so we have binary-mode support.
184 * The devices are of the form /dev/usbmon{N}.
185 * Open /dev and scan it.
187 dir
= opendir("/dev");
189 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
193 * Is this a usbmon device?
195 if (strncmp(name
, USBMON_DEV_PREFIX
,
196 USBMON_DEV_PREFIX_LEN
) != 0)
200 * What's the device number?
202 if (sscanf(&name
[USBMON_DEV_PREFIX_LEN
], "%d", &n
) == 0)
203 continue; /* failed */
205 ret
= usb_dev_add(devlistp
, n
, err_str
);
214 * Matches what's in mon_bin.c in the Linux kernel.
216 #define MIN_RING_SIZE (8*1024)
217 #define MAX_RING_SIZE (1200*1024)
220 usb_set_ring_size(pcap_t
* handle
, int header_size
)
223 * A packet from binary usbmon has:
225 * 1) a fixed-length header, of size header_size;
226 * 2) descriptors, for isochronous transfers;
229 * The kernel buffer has a size, defaulting to 300KB, with a
230 * minimum of 8KB and a maximum of 1200KB. The size is set with
231 * the MON_IOCT_RING_SIZE ioctl; the size passed in is rounded up
234 * No more than {buffer size}/5 bytes worth of payload is saved.
235 * Therefore, if we subtract the fixed-length size from the
236 * snapshot length, we have the biggest payload we want (we
237 * don't worry about the descriptors - if we have descriptors,
238 * we'll just discard the last bit of the payload to get it
239 * to fit). We multiply that result by 5 and set the buffer
240 * size to that value.
244 if (handle
->snapshot
< header_size
)
245 handle
->snapshot
= header_size
;
246 /* The maximum snapshot size is small enough that this won't overflow */
247 ring_size
= (handle
->snapshot
- header_size
) * 5;
250 * Will this get an error?
251 * (There's no way to query the minimum or maximum, so we just
252 * copy the value from the kernel source. We don't round it
253 * up to a multiple of the page size.)
255 if (ring_size
> MAX_RING_SIZE
) {
257 * Yes. Lower the ring size to the maximum, and set the
258 * snapshot length to the value that would give us a
261 ring_size
= MAX_RING_SIZE
;
262 handle
->snapshot
= header_size
+ (MAX_RING_SIZE
/5);
263 } else if (ring_size
< MIN_RING_SIZE
) {
265 * Yes. Raise the ring size to the minimum, but leave
266 * the snapshot length unchanged, so we show the
267 * callback no more data than specified by the
270 ring_size
= MIN_RING_SIZE
;
273 if (ioctl(handle
->fd
, MON_IOCT_RING_SIZE
, ring_size
) == -1) {
274 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
275 errno
, "Can't set ring size from fd %d", handle
->fd
);
282 int usb_mmap(pcap_t
* handle
)
284 struct pcap_usb_linux
*handlep
= handle
->priv
;
288 * Attempt to set the ring size as appropriate for the snapshot
289 * length, reducing the snapshot length if that'd make the ring
290 * bigger than the kernel supports.
292 len
= usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header_mmapped
));
294 /* Failed. Fall back on non-memory-mapped access. */
298 handlep
->mmapbuflen
= len
;
299 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
300 MAP_SHARED
, handle
->fd
, 0);
301 if (handlep
->mmapbuf
== MAP_FAILED
) {
303 * Failed. We don't treat that as a fatal error, we
304 * just try to fall back on non-memory-mapped access.
311 #ifdef HAVE_LINUX_USBDEVICE_FS_H
313 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
315 #define USB_DIR_IN 0x80
316 #define USB_TYPE_STANDARD 0x00
317 #define USB_RECIP_DEVICE 0x00
319 #define USB_REQ_GET_DESCRIPTOR 6
321 #define USB_DT_DEVICE 1
322 #define USB_DT_CONFIG 2
324 #define USB_DEVICE_DESCRIPTOR_SIZE 18
325 #define USB_CONFIG_DESCRIPTOR_SIZE 9
327 /* probe the descriptors of the devices attached to the bus */
328 /* the descriptors will end up in the captured packet stream */
329 /* and be decoded by external apps like wireshark */
330 /* without these identifying probes packet data can't be fully decoded */
332 probe_devices(int bus
)
334 struct usbdevfs_ctrltransfer ctrl
;
337 char busdevpath
[sizeof("/dev/bus/usb/000/") + NAME_MAX
];
339 uint8_t descriptor
[USB_DEVICE_DESCRIPTOR_SIZE
];
340 uint8_t configdesc
[USB_CONFIG_DESCRIPTOR_SIZE
];
342 /* scan usb bus directories for device nodes */
343 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d", bus
);
344 dir
= opendir(busdevpath
);
348 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
350 char* name
= data
->d_name
;
355 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
357 fd
= open(busdevpath
, O_RDWR
);
362 * Sigh. Different kernels have different member names
363 * for this structure.
365 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
366 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
367 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
368 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
370 ctrl
.wLength
= sizeof(descriptor
);
372 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
373 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
374 ctrl
.value
= USB_DT_DEVICE
<< 8;
376 ctrl
.length
= sizeof(descriptor
);
378 ctrl
.data
= descriptor
;
379 ctrl
.timeout
= CTRL_TIMEOUT
;
381 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
383 /* Request CONFIGURATION descriptor alone to know wTotalLength */
384 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
385 ctrl
.wValue
= USB_DT_CONFIG
<< 8;
386 ctrl
.wLength
= sizeof(configdesc
);
388 ctrl
.value
= USB_DT_CONFIG
<< 8;
389 ctrl
.length
= sizeof(configdesc
);
391 ctrl
.data
= configdesc
;
392 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
394 uint16_t wtotallength
;
395 wtotallength
= EXTRACT_LE_U_2(&configdesc
[2]);
396 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
397 ctrl
.wLength
= wtotallength
;
399 ctrl
.length
= wtotallength
;
401 ctrl
.data
= malloc(wtotallength
);
403 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
411 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
414 usb_create(const char *device
, char *ebuf
, int *is_ours
)
421 /* Does this look like a USB monitoring device? */
423 /* Does it begin with USB_IFACE? */
424 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
425 /* Nope, doesn't begin with USB_IFACE */
429 /* Yes - is USB_IFACE followed by a number? */
430 cp
+= sizeof USB_IFACE
- 1;
431 devnum
= strtol(cp
, &cpend
, 10);
432 if (cpend
== cp
|| *cpend
!= '\0') {
433 /* Not followed by a number. */
438 /* Followed by a non-valid number. */
443 /* OK, it's probably ours. */
446 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_usb_linux
);
450 p
->activate_op
= usb_activate
;
455 usb_activate(pcap_t
* handle
)
457 struct pcap_usb_linux
*handlep
= handle
->priv
;
458 char full_path
[USB_LINE_LEN
];
461 * Turn a negative snapshot value (invalid), a snapshot value of
462 * 0 (unspecified), or a value bigger than the normal maximum
463 * value, into the maximum allowed value.
465 * If some application really *needs* a bigger snapshot
466 * length, we should just increase MAXIMUM_SNAPLEN.
468 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
469 handle
->snapshot
= MAXIMUM_SNAPLEN
;
471 /* Initialize some components of the pcap structure. */
472 handle
->bufsize
= handle
->snapshot
;
474 handle
->linktype
= DLT_USB_LINUX
;
476 handle
->inject_op
= usb_inject_linux
;
477 handle
->setfilter_op
= pcapint_install_bpf_program
; /* no kernel filtering */
478 handle
->setdirection_op
= usb_setdirection_linux
;
479 handle
->set_datalink_op
= NULL
; /* can't change data link type */
480 handle
->getnonblock_op
= pcapint_getnonblock_fd
;
481 handle
->setnonblock_op
= pcapint_setnonblock_fd
;
483 /*get usb bus index from device name */
484 if (sscanf(handle
->opt
.device
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
486 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
487 "Can't get USB bus index from %s", handle
->opt
.device
);
492 * We require 2.6.27 or later kernels, so we have binary-mode support.
493 * Try to open the binary interface.
495 snprintf(full_path
, USB_LINE_LEN
, "/dev/"USBMON_DEV_PREFIX
"%d",
497 handle
->fd
= open(full_path
, O_RDONLY
, 0);
501 * The attempt failed; why?
507 * The device doesn't exist.
508 * That could either mean that there's
509 * no support for monitoring USB buses
510 * (which probably means "the usbmon
511 * module isn't loaded") or that there
512 * is but that *particular* device
513 * doesn't exist (no "scan all buses"
514 * device if the bus index is 0, no
515 * such bus if the bus index isn't 0).
517 * For now, don't provide an error message;
518 * if we can determine what the particular
519 * problem is, we should report that.
521 handle
->errbuf
[0] = '\0';
522 return PCAP_ERROR_NO_SUCH_DEVICE
;
526 * We didn't have permission to open it.
528 DIAG_OFF_FORMAT_TRUNCATION
529 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
530 "Attempt to open %s failed with EACCES - root privileges may be required",
532 DIAG_ON_FORMAT_TRUNCATION
533 return PCAP_ERROR_PERM_DENIED
;
537 * Something went wrong.
539 pcapint_fmt_errmsg_for_errno(handle
->errbuf
,
540 PCAP_ERRBUF_SIZE
, errno
,
541 "Can't open USB bus file %s", full_path
);
546 if (handle
->opt
.rfmon
)
549 * Monitor mode doesn't apply to USB devices.
552 return PCAP_ERROR_RFMON_NOTSUP
;
555 /* try to use fast mmap access */
556 if (usb_mmap(handle
))
559 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
560 handle
->stats_op
= usb_stats_linux_bin
;
561 handle
->read_op
= usb_read_linux_mmap
;
562 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
563 #ifdef HAVE_LINUX_USBDEVICE_FS_H
564 probe_devices(handlep
->bus_index
);
568 * "handle->fd" is a real file, so
569 * "select()" and "poll()" work on it.
571 handle
->selectable_fd
= handle
->fd
;
576 * We failed; try plain binary interface access.
578 * Attempt to set the ring size as appropriate for
579 * the snapshot length, reducing the snapshot length
580 * if that'd make the ring bigger than the kernel
583 if (usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header
)) == -1) {
588 handle
->stats_op
= usb_stats_linux_bin
;
589 handle
->read_op
= usb_read_linux_bin
;
590 #ifdef HAVE_LINUX_USBDEVICE_FS_H
591 probe_devices(handlep
->bus_index
);
595 * "handle->fd" is a real file, so "select()" and "poll()"
598 handle
->selectable_fd
= handle
->fd
;
600 /* for plain binary access and text access we need to allocate the read
602 handle
->buffer
= malloc(handle
->bufsize
);
603 if (!handle
->buffer
) {
604 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
613 usb_inject_linux(pcap_t
*handle
, const void *buf _U_
, int size _U_
)
615 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
616 "Packet injection is not supported on USB devices");
621 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
624 * It's guaranteed, at this point, that d is a valid
632 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
634 struct pcap_usb_linux
*handlep
= handle
->priv
;
636 struct mon_bin_stats st
;
637 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
640 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
641 errno
, "Can't read stats from fd %d", handle
->fd
);
645 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
646 stats
->ps_drop
= st
.dropped
;
647 stats
->ps_ifdrop
= 0;
652 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
653 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
656 usb_read_linux_bin(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
658 struct pcap_usb_linux
*handlep
= handle
->priv
;
659 struct mon_bin_get info
;
661 struct pcap_pkthdr pkth
;
662 u_int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
664 /* the usb header is going to be part of 'packet' data*/
665 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
666 info
.data
= handle
->buffer
+ sizeof(pcap_usb_header
);
667 info
.data_len
= clen
;
669 /* ignore interrupt system call errors */
671 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
672 if (handle
->break_loop
)
674 handle
->break_loop
= 0;
677 } while ((ret
== -1) && (errno
== EINTR
));
681 return 0; /* no data there */
683 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
684 errno
, "Can't read from fd %d", handle
->fd
);
689 * info.hdr->data_len is the number of bytes of isochronous
690 * descriptors (if any) plus the number of bytes of data
691 * provided. There are no isochronous descriptors here,
692 * because we're using the old 48-byte header.
694 * If info.hdr->data_flag is non-zero, there's no URB data;
695 * info.hdr->urb_len is the size of the buffer into which
696 * data is to be placed; it does not represent the amount
697 * of data transferred. If info.hdr->data_flag is zero,
698 * there is URB data, and info.hdr->urb_len is the number
699 * of bytes transmitted or received; it doesn't include
700 * isochronous descriptors.
702 * The kernel may give us more data than the snaplen; if it did,
703 * reduce the data length so that the total number of bytes we
704 * tell our client we have is not greater than the snaplen.
706 if (info
.hdr
->data_len
< clen
)
707 clen
= info
.hdr
->data_len
;
708 info
.hdr
->data_len
= clen
;
709 pkth
.caplen
= sizeof(pcap_usb_header
) + clen
;
710 if (info
.hdr
->data_flag
) {
712 * No data; just base the original length on
713 * info.hdr->data_len (so that it's >= the captured
716 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->data_len
;
719 * We got data; base the original length on
720 * info.hdr->urb_len, so that it includes data
721 * discarded by the USB monitor device due to
722 * its buffer being too small.
724 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->urb_len
;
726 pkth
.ts
.tv_sec
= (time_t)info
.hdr
->ts_sec
;
727 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
729 if (handle
->fcode
.bf_insns
== NULL
||
730 pcapint_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
731 pkth
.len
, pkth
.caplen
)) {
732 handlep
->packets_read
++;
733 callback(user
, &pkth
, handle
->buffer
);
737 return 0; /* didn't pass filter */
741 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
742 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
746 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
748 struct pcap_usb_linux
*handlep
= handle
->priv
;
749 struct mon_bin_mfetch fetch
;
750 int32_t vec
[VEC_SIZE
];
751 struct pcap_pkthdr pkth
;
753 pcap_usb_header_mmapped
* hdr
;
756 u_int clen
, max_clen
;
758 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header_mmapped
);
764 if (PACKET_COUNT_IS_UNLIMITED(max_packets
)) {
766 * There's no limit on the number of packets
767 * to process, so try to fetch VEC_SIZE packets.
772 * Try to fetch as many packets as we have left
773 * to process, or VEC_SIZE packets, whichever
776 * At this point, max_packets > 0 (otherwise,
777 * PACKET_COUNT_IS_UNLIMITED(max_packets)
778 * would be true) and max_packets > packets
779 * (packet starts out as 0, and the test
780 * at the bottom of the loop exits if
781 * max_packets <= packets), so limit is
782 * guaranteed to be > 0.
784 limit
= max_packets
- packets
;
785 if (limit
> VEC_SIZE
)
790 * Try to fetch as many events as possible, up to
791 * the limit, and flush the events we've processed
792 * earlier (nflush) - MON_IOCX_MFETCH does both
793 * (presumably to reduce the number of system
794 * calls in loops like this).
797 fetch
.nfetch
= limit
;
798 fetch
.nflush
= nflush
;
799 /* ignore interrupt system call errors */
801 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
802 if (handle
->break_loop
)
804 handle
->break_loop
= 0;
807 } while ((ret
== -1) && (errno
== EINTR
));
811 return 0; /* no data there */
813 pcapint_fmt_errmsg_for_errno(handle
->errbuf
,
814 PCAP_ERRBUF_SIZE
, errno
, "Can't mfetch fd %d",
819 /* keep track of processed events, we will flush them later */
820 nflush
= fetch
.nfetch
;
821 for (i
=0; i
<fetch
.nfetch
; ++i
) {
823 * XXX - we can't check break_loop here, as
824 * we read the indices of packets into a
825 * local variable, so if we're later called
826 * to fetch more packets, those packets will
827 * not be seen - and won't be flushed, either.
829 * Instead, we would have to keep the array
830 * of indices in our private data, along
831 * with the count of packets to flush - or
832 * would have to flush the already-processed
833 * packets if we break out of the loop here.
836 /* Get a pointer to this packet's buffer */
837 bp
= &handlep
->mmapbuf
[vec
[i
]];
839 /* That begins with a metadata header */
840 hdr
= (pcap_usb_header_mmapped
*) bp
;
843 if (hdr
->event_type
== '@')
847 * hdr->data_len is the number of bytes of
848 * isochronous descriptors (if any) plus the
849 * number of bytes of data provided.
851 * If hdr->data_flag is non-zero, there's no
852 * URB data; hdr->urb_len is the size of the
853 * buffer into which data is to be placed; it does
854 * not represent the amount of data transferred.
855 * If hdr->data_flag is zero, there is URB data,
856 * and hdr->urb_len is the number of bytes
857 * transmitted or received; it doesn't include
858 * isochronous descriptors.
860 * The kernel may give us more data than the
861 * snaplen; if it did, reduce the data length
862 * so that the total number of bytes we
863 * tell our client we have is not greater than
867 if (hdr
->data_len
< clen
)
868 clen
= hdr
->data_len
;
869 pkth
.caplen
= sizeof(pcap_usb_header_mmapped
) + clen
;
870 if (hdr
->data_flag
) {
872 * No data; just base the original length
873 * on hdr->data_len (so that it's >= the
874 * captured length). Clamp the result
875 * at UINT_MAX, so it fits in an unsigned
878 pkth
.len
= u_int_sum(sizeof(pcap_usb_header_mmapped
),
884 if (is_isochronous_transfer_completion(hdr
)) {
886 * For isochronous transfer completion
887 * events, hdr->urb_len doesn't take
888 * into account the way the data is
889 * put into the buffer, as it doesn't
890 * count any padding between the
891 * chunks of isochronous data, so
892 * we have to calculate the amount
893 * of data from the isochronous
896 pkth
.len
= incoming_isochronous_transfer_completed_len(&pkth
, bp
);
899 * For everything else, the original
900 * data length is just the length of
901 * the memory-mapped Linux USB header
902 * plus hdr->urb_len; we use
903 * hdr->urb_len so that it includes
904 * data discarded by the USB monitor
905 * device due to its buffer being
906 * too small. Clamp the result at
907 * UINT_MAX, so it fits in an
910 pkth
.len
= u_int_sum(sizeof(pcap_usb_header_mmapped
),
914 pkth
.ts
.tv_sec
= (time_t)hdr
->ts_sec
;
915 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
917 if (handle
->fcode
.bf_insns
== NULL
||
918 pcapint_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
919 pkth
.len
, pkth
.caplen
)) {
920 handlep
->packets_read
++;
921 callback(user
, &pkth
, (u_char
*) hdr
);
927 * If max_packets specifies "unlimited", we stop after
930 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) ||
931 (packets
>= max_packets
))
935 /* flush pending events*/
936 if (ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
) == -1) {
937 pcapint_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
938 errno
, "Can't mflush fd %d", handle
->fd
);
945 usb_cleanup_linux_mmap(pcap_t
* handle
)
947 struct pcap_usb_linux
*handlep
= handle
->priv
;
949 /* if we have a memory-mapped buffer, unmap it */
950 if (handlep
->mmapbuf
!= NULL
) {
951 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
952 handlep
->mmapbuf
= NULL
;
954 pcapint_cleanup_live_common(handle
);