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"
42 #include "pcap-usb-linux-common.h"
47 #ifdef NEED_STRERROR_H
59 #include <netinet/in.h>
60 #include <sys/ioctl.h>
62 #include <sys/utsname.h>
63 #ifdef HAVE_LINUX_USBDEVICE_FS_H
65 * We might need <linux/compiler.h> to define __user for
66 * <linux/usbdevice_fs.h>.
68 #ifdef HAVE_LINUX_COMPILER_H
69 #include <linux/compiler.h>
70 #endif /* HAVE_LINUX_COMPILER_H */
71 #include <linux/usbdevice_fs.h>
72 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
74 #include "diag-control.h"
76 #define USB_IFACE "usbmon"
78 #define USBMON_DEV_PREFIX "usbmon"
79 #define USBMON_DEV_PREFIX_LEN (sizeof USBMON_DEV_PREFIX - 1)
80 #define USB_LINE_LEN 4096
82 #if __BYTE_ORDER == __LITTLE_ENDIAN
87 #define htols(s) bswap_16(s)
88 #define htoll(l) bswap_32(l)
89 #define htol64(ll) bswap_64(ll)
92 struct mon_bin_stats
{
100 size_t data_len
; /* Length of data (can be zero) */
103 struct mon_bin_mfetch
{
104 int32_t *offvec
; /* Vector of events fetched */
105 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
106 int32_t nflush
; /* Number of events to flush */
109 #define MON_IOC_MAGIC 0x92
111 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
112 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
113 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
114 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
115 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
116 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
117 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
118 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
120 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
121 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
122 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
123 #define MON_BIN_ERROR 0x8
126 * Private data for capturing on Linux USB.
128 struct pcap_usb_linux
{
129 u_char
*mmapbuf
; /* memory-mapped region pointer */
130 size_t mmapbuflen
; /* size of region */
135 /* forward declaration */
136 static int usb_activate(pcap_t
*);
137 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
138 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
139 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
140 static int usb_inject_linux(pcap_t
*, const void *, int);
141 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
142 static void usb_cleanup_linux_mmap(pcap_t
*);
144 /* facility to add an USB device to the device list*/
146 usb_dev_add(pcap_if_list_t
*devlistp
, int n
, char *err_str
)
150 snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
152 * XXX - is there any notion of "up" and "running"?
156 * As this refers to all buses, there's no notion of
157 * "connected" vs. "disconnected", as that's a property
158 * that would apply to a particular USB interface.
160 if (add_dev(devlistp
, dev_name
,
161 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
,
162 "Raw USB traffic, all USB buses", err_str
) == NULL
)
166 * XXX - is there a way to determine whether anything's
167 * plugged into this bus interface or not, and set
168 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
169 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
171 snprintf(dev_descr
, 30, "Raw USB traffic, bus number %d", n
);
172 if (add_dev(devlistp
, dev_name
, 0, dev_descr
, err_str
) == NULL
)
180 usb_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
189 * We require 2.6.27 or later kernels, so we have binary-mode support.
190 * The devices are of the form /dev/usbmon{N}.
191 * Open /dev and scan it.
193 dir
= opendir("/dev");
195 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
199 * Is this a usbmon device?
201 if (strncmp(name
, USBMON_DEV_PREFIX
,
202 USBMON_DEV_PREFIX_LEN
) != 0)
206 * What's the device number?
208 if (sscanf(&name
[USBMON_DEV_PREFIX_LEN
], "%d", &n
) == 0)
209 continue; /* failed */
211 ret
= usb_dev_add(devlistp
, n
, err_str
);
220 * Matches what's in mon_bin.c in the Linux kernel.
222 #define MIN_RING_SIZE (8*1024)
223 #define MAX_RING_SIZE (1200*1024)
226 usb_set_ring_size(pcap_t
* handle
, int header_size
)
229 * A packet from binary usbmon has:
231 * 1) a fixed-length header, of size header_size;
232 * 2) descriptors, for isochronous transfers;
235 * The kernel buffer has a size, defaulting to 300KB, with a
236 * minimum of 8KB and a maximum of 1200KB. The size is set with
237 * the MON_IOCT_RING_SIZE ioctl; the size passed in is rounded up
240 * No more than {buffer size}/5 bytes worth of payload is saved.
241 * Therefore, if we subtract the fixed-length size from the
242 * snapshot length, we have the biggest payload we want (we
243 * don't worry about the descriptors - if we have descriptors,
244 * we'll just discard the last bit of the payload to get it
245 * to fit). We multiply that result by 5 and set the buffer
246 * size to that value.
250 if (handle
->snapshot
< header_size
)
251 handle
->snapshot
= header_size
;
252 /* The maximum snapshot size is small enough that this won't overflow */
253 ring_size
= (handle
->snapshot
- header_size
) * 5;
256 * Will this get an error?
257 * (There's no wqy to query the minimum or maximum, so we just
258 * copy the value from the kernel source. We don't round it
259 * up to a multiple of the page size.)
261 if (ring_size
> MAX_RING_SIZE
) {
263 * Yes. Lower the ring size to the maximum, and set the
264 * snapshot length to the value that would give us a
267 ring_size
= MAX_RING_SIZE
;
268 handle
->snapshot
= header_size
+ (MAX_RING_SIZE
/5);
269 } else if (ring_size
< MIN_RING_SIZE
) {
271 * Yes. Raise the ring size to the minimum, but leave
272 * the snapshot length unchanged, so we show the
273 * callback no more data than specified by the
276 ring_size
= MIN_RING_SIZE
;
279 if (ioctl(handle
->fd
, MON_IOCT_RING_SIZE
, ring_size
) == -1) {
280 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
281 errno
, "Can't set ring size from fd %d", handle
->fd
);
288 int usb_mmap(pcap_t
* handle
)
290 struct pcap_usb_linux
*handlep
= handle
->priv
;
294 * Attempt to set the ring size as appropriate for the snapshot
295 * length, reducing the snapshot length if that'd make the ring
296 * bigger than the kernel supports.
298 len
= usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header_mmapped
));
300 /* Failed. Fall back on non-memory-mapped access. */
304 handlep
->mmapbuflen
= len
;
305 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
306 MAP_SHARED
, handle
->fd
, 0);
307 if (handlep
->mmapbuf
== MAP_FAILED
) {
309 * Failed. We don't treat that as a fatal error, we
310 * just try to fall back on non-memory-mapped access.
317 #ifdef HAVE_LINUX_USBDEVICE_FS_H
319 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
321 #define USB_DIR_IN 0x80
322 #define USB_TYPE_STANDARD 0x00
323 #define USB_RECIP_DEVICE 0x00
325 #define USB_REQ_GET_DESCRIPTOR 6
327 #define USB_DT_DEVICE 1
328 #define USB_DT_CONFIG 2
330 #define USB_DEVICE_DESCRIPTOR_SIZE 18
331 #define USB_CONFIG_DESCRIPTOR_SIZE 9
333 /* probe the descriptors of the devices attached to the bus */
334 /* the descriptors will end up in the captured packet stream */
335 /* and be decoded by external apps like wireshark */
336 /* without these identifying probes packet data can't be fully decoded */
338 probe_devices(int bus
)
340 struct usbdevfs_ctrltransfer ctrl
;
343 char busdevpath
[sizeof("/dev/bus/usb/000/") + NAME_MAX
];
345 uint8_t descriptor
[USB_DEVICE_DESCRIPTOR_SIZE
];
346 uint8_t configdesc
[USB_CONFIG_DESCRIPTOR_SIZE
];
348 /* scan usb bus directories for device nodes */
349 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d", bus
);
350 dir
= opendir(busdevpath
);
354 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
356 char* name
= data
->d_name
;
361 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
363 fd
= open(busdevpath
, O_RDWR
);
368 * Sigh. Different kernels have different member names
369 * for this structure.
371 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
372 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
373 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
374 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
376 ctrl
.wLength
= sizeof(descriptor
);
378 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
379 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
380 ctrl
.value
= USB_DT_DEVICE
<< 8;
382 ctrl
.length
= sizeof(descriptor
);
384 ctrl
.data
= descriptor
;
385 ctrl
.timeout
= CTRL_TIMEOUT
;
387 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
389 /* Request CONFIGURATION descriptor alone to know wTotalLength */
390 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
391 ctrl
.wValue
= USB_DT_CONFIG
<< 8;
392 ctrl
.wLength
= sizeof(configdesc
);
394 ctrl
.value
= USB_DT_CONFIG
<< 8;
395 ctrl
.length
= sizeof(configdesc
);
397 ctrl
.data
= configdesc
;
398 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
400 uint16_t wtotallength
;
401 wtotallength
= EXTRACT_LE_U_2(&configdesc
[2]);
402 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
403 ctrl
.wLength
= wtotallength
;
405 ctrl
.length
= wtotallength
;
407 ctrl
.data
= malloc(wtotallength
);
409 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
417 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
420 usb_create(const char *device
, char *ebuf
, int *is_ours
)
427 /* Does this look like a USB monitoring device? */
428 cp
= strrchr(device
, '/');
431 /* Does it begin with USB_IFACE? */
432 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
433 /* Nope, doesn't begin with USB_IFACE */
437 /* Yes - is USB_IFACE followed by a number? */
438 cp
+= sizeof USB_IFACE
- 1;
439 devnum
= strtol(cp
, &cpend
, 10);
440 if (cpend
== cp
|| *cpend
!= '\0') {
441 /* Not followed by a number. */
446 /* Followed by a non-valid number. */
451 /* OK, it's probably ours. */
454 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_usb_linux
);
458 p
->activate_op
= usb_activate
;
463 usb_activate(pcap_t
* handle
)
465 struct pcap_usb_linux
*handlep
= handle
->priv
;
466 char full_path
[USB_LINE_LEN
];
469 * Turn a negative snapshot value (invalid), a snapshot value of
470 * 0 (unspecified), or a value bigger than the normal maximum
471 * value, into the maximum allowed value.
473 * If some application really *needs* a bigger snapshot
474 * length, we should just increase MAXIMUM_SNAPLEN.
476 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
477 handle
->snapshot
= MAXIMUM_SNAPLEN
;
479 /* Initialize some components of the pcap structure. */
480 handle
->bufsize
= handle
->snapshot
;
482 handle
->linktype
= DLT_USB_LINUX
;
484 handle
->inject_op
= usb_inject_linux
;
485 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
486 handle
->setdirection_op
= usb_setdirection_linux
;
487 handle
->set_datalink_op
= NULL
; /* can't change data link type */
488 handle
->getnonblock_op
= pcap_getnonblock_fd
;
489 handle
->setnonblock_op
= pcap_setnonblock_fd
;
491 /*get usb bus index from device name */
492 if (sscanf(handle
->opt
.device
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
494 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
495 "Can't get USB bus index from %s", handle
->opt
.device
);
500 * We require 2.6.27 or later kernels, so we have binary-mode support.
501 * Try to open the binary interface.
503 snprintf(full_path
, USB_LINE_LEN
, "/dev/"USBMON_DEV_PREFIX
"%d",
505 handle
->fd
= open(full_path
, O_RDONLY
, 0);
509 * The attempt failed; why?
515 * The device doesn't exist.
516 * That could either mean that there's
517 * no support for monitoring USB buses
518 * (which probably means "the usbmon
519 * module isn't loaded") or that there
520 * is but that *particular* device
521 * doesn't exist (no "scan all buses"
522 * device if the bus index is 0, no
523 * such bus if the bus index isn't 0).
525 * For now, don't provide an error message;
526 * if we can determine what the particular
527 * problem is, we should report that.
529 handle
->errbuf
[0] = '\0';
530 return PCAP_ERROR_NO_SUCH_DEVICE
;
534 * We didn't have permission to open it.
536 DIAG_OFF_FORMAT_TRUNCATION
537 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
538 "Attempt to open %s failed with EACCES - root privileges may be required",
540 DIAG_ON_FORMAT_TRUNCATION
541 return PCAP_ERROR_PERM_DENIED
;
545 * Something went wrong.
547 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
548 PCAP_ERRBUF_SIZE
, errno
,
549 "Can't open USB bus file %s", full_path
);
554 if (handle
->opt
.rfmon
)
557 * Monitor mode doesn't apply to USB devices.
560 return PCAP_ERROR_RFMON_NOTSUP
;
563 /* try to use fast mmap access */
564 if (usb_mmap(handle
))
567 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
568 handle
->stats_op
= usb_stats_linux_bin
;
569 handle
->read_op
= usb_read_linux_mmap
;
570 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
571 #ifdef HAVE_LINUX_USBDEVICE_FS_H
572 probe_devices(handlep
->bus_index
);
576 * "handle->fd" is a real file, so
577 * "select()" and "poll()" work on it.
579 handle
->selectable_fd
= handle
->fd
;
584 * We failed; try plain binary interface access.
586 * Attempt to set the ring size as appropriate for
587 * the snapshot length, reducing the snapshot length
588 * if that'd make the ring bigger than the kernel
591 if (usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header
)) == -1) {
596 handle
->stats_op
= usb_stats_linux_bin
;
597 handle
->read_op
= usb_read_linux_bin
;
598 #ifdef HAVE_LINUX_USBDEVICE_FS_H
599 probe_devices(handlep
->bus_index
);
603 * "handle->fd" is a real file, so "select()" and "poll()"
606 handle
->selectable_fd
= handle
->fd
;
608 /* for plain binary access and text access we need to allocate the read
610 handle
->buffer
= malloc(handle
->bufsize
);
611 if (!handle
->buffer
) {
612 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
621 usb_inject_linux(pcap_t
*handle
, const void *buf _U_
, int size _U_
)
623 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
624 "Packet injection is not supported on USB devices");
629 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
632 * It's guaranteed, at this point, that d is a valid
640 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
642 struct pcap_usb_linux
*handlep
= handle
->priv
;
644 struct mon_bin_stats st
;
645 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
648 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
649 errno
, "Can't read stats from fd %d", handle
->fd
);
653 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
654 stats
->ps_drop
= st
.dropped
;
655 stats
->ps_ifdrop
= 0;
660 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
661 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
664 usb_read_linux_bin(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
666 struct pcap_usb_linux
*handlep
= handle
->priv
;
667 struct mon_bin_get info
;
669 struct pcap_pkthdr pkth
;
670 u_int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
672 /* the usb header is going to be part of 'packet' data*/
673 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
674 info
.data
= (u_char
*)handle
->buffer
+ sizeof(pcap_usb_header
);
675 info
.data_len
= clen
;
677 /* ignore interrupt system call errors */
679 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
680 if (handle
->break_loop
)
682 handle
->break_loop
= 0;
685 } while ((ret
== -1) && (errno
== EINTR
));
689 return 0; /* no data there */
691 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
692 errno
, "Can't read from fd %d", handle
->fd
);
697 * info.hdr->data_len is the number of bytes of isochronous
698 * descriptors (if any) plus the number of bytes of data
699 * provided. There are no isochronous descriptors here,
700 * because we're using the old 48-byte header.
702 * If info.hdr->data_flag is non-zero, there's no URB data;
703 * info.hdr->urb_len is the size of the buffer into which
704 * data is to be placed; it does not represent the amount
705 * of data transferred. If info.hdr->data_flag is zero,
706 * there is URB data, and info.hdr->urb_len is the number
707 * of bytes transmitted or received; it doesn't include
708 * isochronous descriptors.
710 * The kernel may give us more data than the snaplen; if it did,
711 * reduce the data length so that the total number of bytes we
712 * tell our client we have is not greater than the snaplen.
714 if (info
.hdr
->data_len
< clen
)
715 clen
= info
.hdr
->data_len
;
716 info
.hdr
->data_len
= clen
;
717 pkth
.caplen
= sizeof(pcap_usb_header
) + clen
;
718 if (info
.hdr
->data_flag
) {
720 * No data; just base the on-the-wire length on
721 * info.hdr->data_len (so that it's >= the captured
724 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->data_len
;
727 * We got data; base the on-the-wire length on
728 * info.hdr->urb_len, so that it includes data
729 * discarded by the USB monitor device due to
730 * its buffer being too small.
732 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->urb_len
;
734 pkth
.ts
.tv_sec
= (time_t)info
.hdr
->ts_sec
;
735 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
737 if (handle
->fcode
.bf_insns
== NULL
||
738 pcap_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
739 pkth
.len
, pkth
.caplen
)) {
740 handlep
->packets_read
++;
741 callback(user
, &pkth
, handle
->buffer
);
745 return 0; /* didn't pass filter */
749 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
750 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
754 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
756 struct pcap_usb_linux
*handlep
= handle
->priv
;
757 struct mon_bin_mfetch fetch
;
758 int32_t vec
[VEC_SIZE
];
759 struct pcap_pkthdr pkth
;
761 pcap_usb_header_mmapped
* hdr
;
764 u_int clen
, max_clen
;
766 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header_mmapped
);
772 if (PACKET_COUNT_IS_UNLIMITED(max_packets
)) {
774 * There's no limit on the number of packets
775 * to process, so try to fetch VEC_SIZE packets.
780 * Try to fetch as many packets as we have left
781 * to process, or VEC_SIZE packets, whichever
784 * At this point, max_packets > 0 (otherwise,
785 * PACKET_COUNT_IS_UNLIMITED(max_packets)
786 * would be true) and max_packets > packets
787 * (packet starts out as 0, and the test
788 * at the bottom of the loop exits if
789 * max_packets <= packets), so limit is
790 * guaranteed to be > 0.
792 limit
= max_packets
- packets
;
793 if (limit
> VEC_SIZE
)
798 * Try to fetch as many events as possible, up to
799 * the limit, and flush the events we've processed
800 * earlier (nflush) - MON_IOCX_MFETCH does both
801 * (presumably to reduce the number of system
802 * calls in loops like this).
805 fetch
.nfetch
= limit
;
806 fetch
.nflush
= nflush
;
807 /* ignore interrupt system call errors */
809 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
810 if (handle
->break_loop
)
812 handle
->break_loop
= 0;
815 } while ((ret
== -1) && (errno
== EINTR
));
819 return 0; /* no data there */
821 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
822 PCAP_ERRBUF_SIZE
, errno
, "Can't mfetch fd %d",
827 /* keep track of processed events, we will flush them later */
828 nflush
= fetch
.nfetch
;
829 for (i
=0; i
<fetch
.nfetch
; ++i
) {
831 * XXX - we can't check break_loop here, as
832 * we read the indices of packets into a
833 * local variable, so if we're later called
834 * to fetch more packets, those packets will
835 * not be seen - and won't be flushed, either.
837 * Instead, we would have to keep the array
838 * of indices in our private data, along
839 * with the count of packets to flush - or
840 * would have to flush the already-processed
841 * packets if we break out of the loop here.
844 /* Get a pointer to this packet's buffer */
845 bp
= &handlep
->mmapbuf
[vec
[i
]];
847 /* That begins with a metadata header */
848 hdr
= (pcap_usb_header_mmapped
*) bp
;
851 if (hdr
->event_type
== '@')
855 * hdr->data_len is the number of bytes of
856 * isochronous descriptors (if any) plus the
857 * number of bytes of data provided.
859 * If hdr->data_flag is non-zero, there's no
860 * URB data; hdr->urb_len is the size of the
861 * buffer into which data is to be placed; it does
862 * not represent the amount of data transferred.
863 * If hdr->data_flag is zero, there is URB data,
864 * and hdr->urb_len is the number of bytes
865 * transmitted or received; it doesn't include
866 * isochronous descriptors.
868 * The kernel may give us more data than the
869 * snaplen; if it did, reduce the data length
870 * so that the total number of bytes we
871 * tell our client we have is not greater than
875 if (hdr
->data_len
< clen
)
876 clen
= hdr
->data_len
;
877 pkth
.caplen
= sizeof(pcap_usb_header_mmapped
) + clen
;
878 set_linux_usb_mmapped_length(&pkth
, bp
);
879 pkth
.ts
.tv_sec
= (time_t)hdr
->ts_sec
;
880 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
882 if (handle
->fcode
.bf_insns
== NULL
||
883 pcap_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
884 pkth
.len
, pkth
.caplen
)) {
885 handlep
->packets_read
++;
886 callback(user
, &pkth
, (u_char
*) hdr
);
892 * If max_packets specifiesg "unlimited", we stop after
895 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) ||
896 (packets
>= max_packets
))
900 /* flush pending events*/
901 if (ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
) == -1) {
902 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
903 errno
, "Can't mflush fd %d", handle
->fd
);
910 usb_cleanup_linux_mmap(pcap_t
* handle
)
912 struct pcap_usb_linux
*handlep
= handle
->priv
;
914 /* if we have a memory-mapped buffer, unmap it */
915 if (handlep
->mmapbuf
!= NULL
) {
916 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
917 handlep
->mmapbuf
= NULL
;
919 pcap_cleanup_live_common(handle
);