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"
77 #define USBMON_DEV_PREFIX "usbmon"
78 #define USBMON_DEV_PREFIX_LEN (sizeof USBMON_DEV_PREFIX - 1)
79 #define USB_LINE_LEN 4096
81 #if __BYTE_ORDER == __LITTLE_ENDIAN
86 #define htols(s) bswap_16(s)
87 #define htoll(l) bswap_32(l)
88 #define htol64(ll) bswap_64(ll)
91 struct mon_bin_stats
{
99 size_t data_len
; /* Length of data (can be zero) */
102 struct mon_bin_mfetch
{
103 int32_t *offvec
; /* Vector of events fetched */
104 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
105 int32_t nflush
; /* Number of events to flush */
108 #define MON_IOC_MAGIC 0x92
110 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
111 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
112 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
113 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
114 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
115 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
116 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
117 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
119 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
120 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
121 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
122 #define MON_BIN_ERROR 0x8
125 * Private data for capturing on Linux USB.
127 struct pcap_usb_linux
{
128 u_char
*mmapbuf
; /* memory-mapped region pointer */
129 size_t mmapbuflen
; /* size of region */
134 /* forward declaration */
135 static int usb_activate(pcap_t
*);
136 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
137 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
138 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
139 static int usb_inject_linux(pcap_t
*, const void *, int);
140 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
141 static void usb_cleanup_linux_mmap(pcap_t
*);
143 /* facility to add an USB device to the device list*/
145 usb_dev_add(pcap_if_list_t
*devlistp
, int n
, char *err_str
)
149 snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
151 * XXX - is there any notion of "up" and "running"?
155 * As this refers to all buses, there's no notion of
156 * "connected" vs. "disconnected", as that's a property
157 * that would apply to a particular USB interface.
159 if (add_dev(devlistp
, dev_name
,
160 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
,
161 "Raw USB traffic, all USB buses", err_str
) == NULL
)
165 * XXX - is there a way to determine whether anything's
166 * plugged into this bus interface or not, and set
167 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
168 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
170 snprintf(dev_descr
, 30, "Raw USB traffic, bus number %d", n
);
171 if (add_dev(devlistp
, dev_name
, 0, dev_descr
, err_str
) == NULL
)
179 usb_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
188 * We require 2.6.27 or later kernels, so we have binary-mode support.
189 * The devices are of the form /dev/usbmon{N}.
190 * Open /dev and scan it.
192 dir
= opendir("/dev");
194 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
198 * Is this a usbmon device?
200 if (strncmp(name
, USBMON_DEV_PREFIX
,
201 USBMON_DEV_PREFIX_LEN
) != 0)
205 * What's the device number?
207 if (sscanf(&name
[USBMON_DEV_PREFIX_LEN
], "%d", &n
) == 0)
208 continue; /* failed */
210 ret
= usb_dev_add(devlistp
, n
, err_str
);
219 * Matches what's in mon_bin.c in the Linux kernel.
221 #define MIN_RING_SIZE (8*1024)
222 #define MAX_RING_SIZE (1200*1024)
225 usb_set_ring_size(pcap_t
* handle
, int header_size
)
228 * A packet from binary usbmon has:
230 * 1) a fixed-length header, of size header_size;
231 * 2) descriptors, for isochronous transfers;
234 * The kernel buffer has a size, defaulting to 300KB, with a
235 * minimum of 8KB and a maximum of 1200KB. The size is set with
236 * the MON_IOCT_RING_SIZE ioctl; the size passed in is rounded up
239 * No more than {buffer size}/5 bytes worth of payload is saved.
240 * Therefore, if we subtract the fixed-length size from the
241 * snapshot length, we have the biggest payload we want (we
242 * don't worry about the descriptors - if we have descriptors,
243 * we'll just discard the last bit of the payload to get it
244 * to fit). We multiply that result by 5 and set the buffer
245 * size to that value.
249 if (handle
->snapshot
< header_size
)
250 handle
->snapshot
= header_size
;
251 /* The maximum snapshot size is small enough that this won't overflow */
252 ring_size
= (handle
->snapshot
- header_size
) * 5;
255 * Will this get an error?
256 * (There's no wqy to query the minimum or maximum, so we just
257 * copy the value from the kernel source. We don't round it
258 * up to a multiple of the page size.)
260 if (ring_size
> MAX_RING_SIZE
) {
262 * Yes. Lower the ring size to the maximum, and set the
263 * snapshot length to the value that would give us a
266 ring_size
= MAX_RING_SIZE
;
267 handle
->snapshot
= header_size
+ (MAX_RING_SIZE
/5);
268 } else if (ring_size
< MIN_RING_SIZE
) {
270 * Yes. Raise the ring size to the minimum, but leave
271 * the snapshot length unchanged, so we show the
272 * callback no more data than specified by the
275 ring_size
= MIN_RING_SIZE
;
278 if (ioctl(handle
->fd
, MON_IOCT_RING_SIZE
, ring_size
) == -1) {
279 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
280 errno
, "Can't set ring size from fd %d", handle
->fd
);
287 int usb_mmap(pcap_t
* handle
)
289 struct pcap_usb_linux
*handlep
= handle
->priv
;
293 * Attempt to set the ring size as appropriate for the snapshot
294 * length, reducing the snapshot length if that'd make the ring
295 * bigger than the kernel supports.
297 len
= usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header_mmapped
));
299 /* Failed. Fall back on non-memory-mapped access. */
303 handlep
->mmapbuflen
= len
;
304 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
305 MAP_SHARED
, handle
->fd
, 0);
306 if (handlep
->mmapbuf
== MAP_FAILED
) {
308 * Failed. We don't treat that as a fatal error, we
309 * just try to fall back on non-memory-mapped access.
316 #ifdef HAVE_LINUX_USBDEVICE_FS_H
318 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
320 #define USB_DIR_IN 0x80
321 #define USB_TYPE_STANDARD 0x00
322 #define USB_RECIP_DEVICE 0x00
324 #define USB_REQ_GET_DESCRIPTOR 6
326 #define USB_DT_DEVICE 1
327 #define USB_DT_CONFIG 2
329 #define USB_DEVICE_DESCRIPTOR_SIZE 18
330 #define USB_CONFIG_DESCRIPTOR_SIZE 9
332 /* probe the descriptors of the devices attached to the bus */
333 /* the descriptors will end up in the captured packet stream */
334 /* and be decoded by external apps like wireshark */
335 /* without these identifying probes packet data can't be fully decoded */
337 probe_devices(int bus
)
339 struct usbdevfs_ctrltransfer ctrl
;
342 char busdevpath
[sizeof("/dev/bus/usb/000/") + NAME_MAX
];
344 uint8_t descriptor
[USB_DEVICE_DESCRIPTOR_SIZE
];
345 uint8_t configdesc
[USB_CONFIG_DESCRIPTOR_SIZE
];
347 /* scan usb bus directories for device nodes */
348 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d", bus
);
349 dir
= opendir(busdevpath
);
353 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
355 char* name
= data
->d_name
;
360 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
362 fd
= open(busdevpath
, O_RDWR
);
367 * Sigh. Different kernels have different member names
368 * for this structure.
370 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
371 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
372 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
373 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
375 ctrl
.wLength
= sizeof(descriptor
);
377 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
378 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
379 ctrl
.value
= USB_DT_DEVICE
<< 8;
381 ctrl
.length
= sizeof(descriptor
);
383 ctrl
.data
= descriptor
;
384 ctrl
.timeout
= CTRL_TIMEOUT
;
386 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
388 /* Request CONFIGURATION descriptor alone to know wTotalLength */
389 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
390 ctrl
.wValue
= USB_DT_CONFIG
<< 8;
391 ctrl
.wLength
= sizeof(configdesc
);
393 ctrl
.value
= USB_DT_CONFIG
<< 8;
394 ctrl
.length
= sizeof(configdesc
);
396 ctrl
.data
= configdesc
;
397 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
399 uint16_t wtotallength
;
400 wtotallength
= EXTRACT_LE_U_2(&configdesc
[2]);
401 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
402 ctrl
.wLength
= wtotallength
;
404 ctrl
.length
= wtotallength
;
406 ctrl
.data
= malloc(wtotallength
);
408 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
416 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
419 usb_create(const char *device
, char *ebuf
, int *is_ours
)
426 /* Does this look like a USB monitoring device? */
427 cp
= strrchr(device
, '/');
430 /* Does it begin with USB_IFACE? */
431 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
432 /* Nope, doesn't begin with USB_IFACE */
436 /* Yes - is USB_IFACE followed by a number? */
437 cp
+= sizeof USB_IFACE
- 1;
438 devnum
= strtol(cp
, &cpend
, 10);
439 if (cpend
== cp
|| *cpend
!= '\0') {
440 /* Not followed by a number. */
445 /* Followed by a non-valid number. */
450 /* OK, it's probably ours. */
453 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_usb_linux
);
457 p
->activate_op
= usb_activate
;
462 usb_activate(pcap_t
* handle
)
464 struct pcap_usb_linux
*handlep
= handle
->priv
;
465 char full_path
[USB_LINE_LEN
];
468 * Turn a negative snapshot value (invalid), a snapshot value of
469 * 0 (unspecified), or a value bigger than the normal maximum
470 * value, into the maximum allowed value.
472 * If some application really *needs* a bigger snapshot
473 * length, we should just increase MAXIMUM_SNAPLEN.
475 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
476 handle
->snapshot
= MAXIMUM_SNAPLEN
;
478 /* Initialize some components of the pcap structure. */
479 handle
->bufsize
= handle
->snapshot
;
481 handle
->linktype
= DLT_USB_LINUX
;
483 handle
->inject_op
= usb_inject_linux
;
484 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
485 handle
->setdirection_op
= usb_setdirection_linux
;
486 handle
->set_datalink_op
= NULL
; /* can't change data link type */
487 handle
->getnonblock_op
= pcap_getnonblock_fd
;
488 handle
->setnonblock_op
= pcap_setnonblock_fd
;
490 /*get usb bus index from device name */
491 if (sscanf(handle
->opt
.device
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
493 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
494 "Can't get USB bus index from %s", handle
->opt
.device
);
499 * We require 2.6.27 or later kernels, so we have binary-mode support.
500 * Try to open the binary interface.
502 snprintf(full_path
, USB_LINE_LEN
, "/dev/"USBMON_DEV_PREFIX
"%d",
504 handle
->fd
= open(full_path
, O_RDONLY
, 0);
508 * The attempt failed; why?
514 * The device doesn't exist.
515 * That could either mean that there's
516 * no support for monitoring USB buses
517 * (which probably means "the usbmon
518 * module isn't loaded") or that there
519 * is but that *particular* device
520 * doesn't exist (no "scan all buses"
521 * device if the bus index is 0, no
522 * such bus if the bus index isn't 0).
524 * For now, don't provide an error message;
525 * if we can determine what the particular
526 * problem is, we should report that.
528 handle
->errbuf
[0] = '\0';
529 return PCAP_ERROR_NO_SUCH_DEVICE
;
533 * We didn't have permission to open it.
535 DIAG_OFF_FORMAT_TRUNCATION
536 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
537 "Attempt to open %s failed with EACCES - root privileges may be required",
539 DIAG_ON_FORMAT_TRUNCATION
540 return PCAP_ERROR_PERM_DENIED
;
544 * Something went wrong.
546 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
547 PCAP_ERRBUF_SIZE
, errno
,
548 "Can't open USB bus file %s", full_path
);
553 if (handle
->opt
.rfmon
)
556 * Monitor mode doesn't apply to USB devices.
559 return PCAP_ERROR_RFMON_NOTSUP
;
562 /* try to use fast mmap access */
563 if (usb_mmap(handle
))
566 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
567 handle
->stats_op
= usb_stats_linux_bin
;
568 handle
->read_op
= usb_read_linux_mmap
;
569 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
570 #ifdef HAVE_LINUX_USBDEVICE_FS_H
571 probe_devices(handlep
->bus_index
);
575 * "handle->fd" is a real file, so
576 * "select()" and "poll()" work on it.
578 handle
->selectable_fd
= handle
->fd
;
583 * We failed; try plain binary interface access.
585 * Attempt to set the ring size as appropriate for
586 * the snapshot length, reducing the snapshot length
587 * if that'd make the ring bigger than the kernel
590 if (usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header
)) == -1) {
595 handle
->stats_op
= usb_stats_linux_bin
;
596 handle
->read_op
= usb_read_linux_bin
;
597 #ifdef HAVE_LINUX_USBDEVICE_FS_H
598 probe_devices(handlep
->bus_index
);
602 * "handle->fd" is a real file, so "select()" and "poll()"
605 handle
->selectable_fd
= handle
->fd
;
607 /* for plain binary access and text access we need to allocate the read
609 handle
->buffer
= malloc(handle
->bufsize
);
610 if (!handle
->buffer
) {
611 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
620 usb_inject_linux(pcap_t
*handle
, const void *buf _U_
, int size _U_
)
622 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
623 "Packet injection is not supported on USB devices");
628 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
631 * It's guaranteed, at this point, that d is a valid
639 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
641 struct pcap_usb_linux
*handlep
= handle
->priv
;
643 struct mon_bin_stats st
;
644 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
647 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
648 errno
, "Can't read stats from fd %d", handle
->fd
);
652 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
653 stats
->ps_drop
= st
.dropped
;
654 stats
->ps_ifdrop
= 0;
659 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
660 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
663 usb_read_linux_bin(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
665 struct pcap_usb_linux
*handlep
= handle
->priv
;
666 struct mon_bin_get info
;
668 struct pcap_pkthdr pkth
;
669 u_int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
671 /* the usb header is going to be part of 'packet' data*/
672 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
673 info
.data
= (u_char
*)handle
->buffer
+ sizeof(pcap_usb_header
);
674 info
.data_len
= clen
;
676 /* ignore interrupt system call errors */
678 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
679 if (handle
->break_loop
)
681 handle
->break_loop
= 0;
684 } while ((ret
== -1) && (errno
== EINTR
));
688 return 0; /* no data there */
690 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
691 errno
, "Can't read from fd %d", handle
->fd
);
696 * info.hdr->data_len is the number of bytes of isochronous
697 * descriptors (if any) plus the number of bytes of data
698 * provided. There are no isochronous descriptors here,
699 * because we're using the old 48-byte header.
701 * If info.hdr->data_flag is non-zero, there's no URB data;
702 * info.hdr->urb_len is the size of the buffer into which
703 * data is to be placed; it does not represent the amount
704 * of data transferred. If info.hdr->data_flag is zero,
705 * there is URB data, and info.hdr->urb_len is the number
706 * of bytes transmitted or received; it doesn't include
707 * isochronous descriptors.
709 * The kernel may give us more data than the snaplen; if it did,
710 * reduce the data length so that the total number of bytes we
711 * tell our client we have is not greater than the snaplen.
713 if (info
.hdr
->data_len
< clen
)
714 clen
= info
.hdr
->data_len
;
715 info
.hdr
->data_len
= clen
;
716 pkth
.caplen
= sizeof(pcap_usb_header
) + clen
;
717 if (info
.hdr
->data_flag
) {
719 * No data; just base the on-the-wire length on
720 * info.hdr->data_len (so that it's >= the captured
723 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->data_len
;
726 * We got data; base the on-the-wire length on
727 * info.hdr->urb_len, so that it includes data
728 * discarded by the USB monitor device due to
729 * its buffer being too small.
731 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->urb_len
;
733 pkth
.ts
.tv_sec
= (time_t)info
.hdr
->ts_sec
;
734 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
736 if (handle
->fcode
.bf_insns
== NULL
||
737 pcap_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
738 pkth
.len
, pkth
.caplen
)) {
739 handlep
->packets_read
++;
740 callback(user
, &pkth
, handle
->buffer
);
744 return 0; /* didn't pass filter */
748 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
749 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
753 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
755 struct pcap_usb_linux
*handlep
= handle
->priv
;
756 struct mon_bin_mfetch fetch
;
757 int32_t vec
[VEC_SIZE
];
758 struct pcap_pkthdr pkth
;
759 pcap_usb_header_mmapped
* hdr
;
762 u_int clen
, max_clen
;
764 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header_mmapped
);
770 if (PACKET_COUNT_IS_UNLIMITED(max_packets
)) {
772 * There's no limit on the number of packets
773 * to process, so try to fetch VEC_SIZE packets.
778 * Try to fetch as many packets as we have left
779 * to process, or VEC_SIZE packets, whichever
782 * At this point, max_packets > 0 (otherwise,
783 * PACKET_COUNT_IS_UNLIMITED(max_packets)
784 * would be true) and max_packets > packets
785 * (packet starts out as 0, and the test
786 * at the bottom of the loop exits if
787 * max_packets <= packets), so limit is
788 * guaranteed to be > 0.
790 limit
= max_packets
- packets
;
791 if (limit
> VEC_SIZE
)
796 * Try to fetch as many events as possible, up to
797 * the limit, and flush the events we've processed
798 * earlier (nflush) - MON_IOCX_MFETCH does both
799 * (presumably to reduce the number of system
800 * calls in loops like this).
803 fetch
.nfetch
= limit
;
804 fetch
.nflush
= nflush
;
805 /* ignore interrupt system call errors */
807 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
808 if (handle
->break_loop
)
810 handle
->break_loop
= 0;
813 } while ((ret
== -1) && (errno
== EINTR
));
817 return 0; /* no data there */
819 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
820 PCAP_ERRBUF_SIZE
, errno
, "Can't mfetch fd %d",
825 /* keep track of processed events, we will flush them later */
826 nflush
= fetch
.nfetch
;
827 for (i
=0; i
<fetch
.nfetch
; ++i
) {
829 * XXX - we can't check break_loop here, as
830 * we read the indices of packets into a
831 * local variable, so if we're later called
832 * to fetch more packets, those packets will
833 * not be seen - and won't be flushed, either.
835 * Instead, we would have to keep the array
836 * of indices in our private data, along
837 * with the count of packets to flush - or
838 * would have to flush the already-processed
839 * packets if we break out of the loop here.
843 hdr
= (pcap_usb_header_mmapped
*) &handlep
->mmapbuf
[vec
[i
]];
844 if (hdr
->event_type
== '@')
848 * hdr->data_len is the number of bytes of
849 * isochronous descriptors (if any) plus the
850 * number of bytes of data provided.
852 * If hdr->data_flag is non-zero, there's no
853 * URB data; hdr->urb_len is the size of the
854 * buffer into which data is to be placed; it does
855 * not represent the amount of data transferred.
856 * If hdr->data_flag is zero, there is URB data,
857 * and hdr->urb_len is the number of bytes
858 * transmitted or received; it doesn't include
859 * isochronous descriptors.
861 * The kernel may give us more data than the
862 * snaplen; if it did, reduce the data length
863 * so that the total number of bytes we
864 * tell our client we have is not greater than
868 if (hdr
->data_len
< clen
)
869 clen
= hdr
->data_len
;
870 pkth
.caplen
= sizeof(pcap_usb_header_mmapped
) + clen
;
871 if (hdr
->data_flag
) {
873 * No data; just base the on-the-wire length
874 * on hdr->data_len (so that it's >= the
877 pkth
.len
= sizeof(pcap_usb_header_mmapped
) +
881 * We got data; base the on-the-wire length
882 * on hdr->urb_len, so that it includes
883 * data discarded by the USB monitor device
884 * due to its buffer being too small.
886 pkth
.len
= sizeof(pcap_usb_header_mmapped
) +
887 (hdr
->ndesc
* sizeof (usb_isodesc
)) + hdr
->urb_len
;
889 pkth
.ts
.tv_sec
= (time_t)hdr
->ts_sec
;
890 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
892 if (handle
->fcode
.bf_insns
== NULL
||
893 pcap_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
894 pkth
.len
, pkth
.caplen
)) {
895 handlep
->packets_read
++;
896 callback(user
, &pkth
, (u_char
*) hdr
);
902 * If max_packets specifiesg "unlimited", we stop after
905 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) ||
906 (packets
>= max_packets
))
910 /* flush pending events*/
911 if (ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
) == -1) {
912 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
913 errno
, "Can't mflush fd %d", handle
->fd
);
920 usb_cleanup_linux_mmap(pcap_t
* handle
)
922 struct pcap_usb_linux
*handlep
= handle
->priv
;
924 /* if we have a memory-mapped buffer, unmap it */
925 if (handlep
->mmapbuf
!= NULL
) {
926 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
927 handlep
->mmapbuf
= NULL
;
929 pcap_cleanup_live_common(handle
);