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 #define USB_IFACE "usbmon"
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
, 10, 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 (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
, 30, "Raw USB traffic, bus number %d", n
);
166 if (add_dev(devlistp
, dev_name
, 0, dev_descr
, err_str
) == NULL
)
174 usb_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
176 char usb_mon_dir
[PATH_MAX
];
177 char *usb_mon_prefix
;
178 size_t usb_mon_prefix_len
;
186 * We require 2.6.27 or later kernels, so we have binary-mode support.
187 * What do the device names look like?
188 * Split LINUX_USB_MON_DEV into a directory that we'll
189 * scan and a file name prefix that we'll check for.
191 pcap_strlcpy(usb_mon_dir
, LINUX_USB_MON_DEV
, sizeof usb_mon_dir
);
192 usb_mon_prefix
= strrchr(usb_mon_dir
, '/');
193 if (usb_mon_prefix
== NULL
) {
195 * This "shouldn't happen". Just give up if it
200 *usb_mon_prefix
++ = '\0';
201 usb_mon_prefix_len
= strlen(usb_mon_prefix
);
204 * Open the directory and scan it.
206 dir
= opendir(usb_mon_dir
);
208 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
212 * Is this a usbmon device?
214 if (strncmp(name
, usb_mon_prefix
, usb_mon_prefix_len
) != 0)
218 * What's the device number?
220 if (sscanf(&name
[usb_mon_prefix_len
], "%d", &n
) == 0)
221 continue; /* failed */
223 ret
= usb_dev_add(devlistp
, n
, err_str
);
232 * Matches what's in mon_bin.c in the Linux kernel.
234 #define MIN_RING_SIZE (8*1024)
235 #define MAX_RING_SIZE (1200*1024)
238 usb_set_ring_size(pcap_t
* handle
, int header_size
)
241 * A packet from binary usbmon has:
243 * 1) a fixed-length header, of size header_size;
244 * 2) descriptors, for isochronous transfers;
247 * The kernel buffer has a size, defaulting to 300KB, with a
248 * minimum of 8KB and a maximum of 1200KB. The size is set with
249 * the MON_IOCT_RING_SIZE ioctl; the size passed in is rounded up
252 * No more than {buffer size}/5 bytes worth of payload is saved.
253 * Therefore, if we subtract the fixed-length size from the
254 * snapshot length, we have the biggest payload we want (we
255 * don't worry about the descriptors - if we have descriptors,
256 * we'll just discard the last bit of the payload to get it
257 * to fit). We multiply that result by 5 and set the buffer
258 * size to that value.
262 if (handle
->snapshot
< header_size
)
263 handle
->snapshot
= header_size
;
264 /* The maximum snapshot size is small enough that this won't overflow */
265 ring_size
= (handle
->snapshot
- header_size
) * 5;
268 * Will this get an error?
269 * (There's no wqy to query the minimum or maximum, so we just
270 * copy the value from the kernel source. We don't round it
271 * up to a multiple of the page size.)
273 if (ring_size
> MAX_RING_SIZE
) {
275 * Yes. Lower the ring size to the maximum, and set the
276 * snapshot length to the value that would give us a
279 ring_size
= MAX_RING_SIZE
;
280 handle
->snapshot
= header_size
+ (MAX_RING_SIZE
/5);
281 } else if (ring_size
< MIN_RING_SIZE
) {
283 * Yes. Raise the ring size to the minimum, but leave
284 * the snapshot length unchanged, so we show the
285 * callback no more data than specified by the
288 ring_size
= MIN_RING_SIZE
;
291 if (ioctl(handle
->fd
, MON_IOCT_RING_SIZE
, ring_size
) == -1) {
292 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
293 errno
, "Can't set ring size from fd %d", handle
->fd
);
300 int usb_mmap(pcap_t
* handle
)
302 struct pcap_usb_linux
*handlep
= handle
->priv
;
306 * Attempt to set the ring size as appropriate for the snapshot
307 * length, reducing the snapshot length if that'd make the ring
308 * bigger than the kernel supports.
310 len
= usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header_mmapped
));
312 /* Failed. Fall back on non-memory-mapped access. */
316 handlep
->mmapbuflen
= len
;
317 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
318 MAP_SHARED
, handle
->fd
, 0);
319 if (handlep
->mmapbuf
== MAP_FAILED
) {
321 * Failed. We don't treat that as a fatal error, we
322 * just try to fall back on non-memory-mapped access.
329 #ifdef HAVE_LINUX_USBDEVICE_FS_H
331 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
333 #define USB_DIR_IN 0x80
334 #define USB_TYPE_STANDARD 0x00
335 #define USB_RECIP_DEVICE 0x00
337 #define USB_REQ_GET_DESCRIPTOR 6
339 #define USB_DT_DEVICE 1
340 #define USB_DT_CONFIG 2
342 #define USB_DEVICE_DESCRIPTOR_SIZE 18
343 #define USB_CONFIG_DESCRIPTOR_SIZE 9
345 /* probe the descriptors of the devices attached to the bus */
346 /* the descriptors will end up in the captured packet stream */
347 /* and be decoded by external apps like wireshark */
348 /* without these identifying probes packet data can't be fully decoded */
350 probe_devices(int bus
)
352 struct usbdevfs_ctrltransfer ctrl
;
355 char busdevpath
[sizeof("/dev/bus/usb/000/") + NAME_MAX
];
357 uint8_t descriptor
[USB_DEVICE_DESCRIPTOR_SIZE
];
358 uint8_t configdesc
[USB_CONFIG_DESCRIPTOR_SIZE
];
360 /* scan usb bus directories for device nodes */
361 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d", bus
);
362 dir
= opendir(busdevpath
);
366 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
368 char* name
= data
->d_name
;
373 snprintf(busdevpath
, sizeof(busdevpath
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
375 fd
= open(busdevpath
, O_RDWR
);
380 * Sigh. Different kernels have different member names
381 * for this structure.
383 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
384 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
385 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
386 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
388 ctrl
.wLength
= sizeof(descriptor
);
390 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
391 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
392 ctrl
.value
= USB_DT_DEVICE
<< 8;
394 ctrl
.length
= sizeof(descriptor
);
396 ctrl
.data
= descriptor
;
397 ctrl
.timeout
= CTRL_TIMEOUT
;
399 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
401 /* Request CONFIGURATION descriptor alone to know wTotalLength */
402 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
403 ctrl
.wValue
= USB_DT_CONFIG
<< 8;
404 ctrl
.wLength
= sizeof(configdesc
);
406 ctrl
.value
= USB_DT_CONFIG
<< 8;
407 ctrl
.length
= sizeof(configdesc
);
409 ctrl
.data
= configdesc
;
410 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
412 uint16_t wtotallength
;
413 wtotallength
= EXTRACT_LE_U_2(&configdesc
[2]);
414 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
415 ctrl
.wLength
= wtotallength
;
417 ctrl
.length
= wtotallength
;
419 ctrl
.data
= malloc(wtotallength
);
421 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
429 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
432 usb_create(const char *device
, char *ebuf
, int *is_ours
)
439 /* Does this look like a USB monitoring device? */
440 cp
= strrchr(device
, '/');
443 /* Does it begin with USB_IFACE? */
444 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
445 /* Nope, doesn't begin with USB_IFACE */
449 /* Yes - is USB_IFACE followed by a number? */
450 cp
+= sizeof USB_IFACE
- 1;
451 devnum
= strtol(cp
, &cpend
, 10);
452 if (cpend
== cp
|| *cpend
!= '\0') {
453 /* Not followed by a number. */
458 /* Followed by a non-valid number. */
463 /* OK, it's probably ours. */
466 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_usb_linux
);
470 p
->activate_op
= usb_activate
;
475 usb_activate(pcap_t
* handle
)
477 struct pcap_usb_linux
*handlep
= handle
->priv
;
478 char full_path
[USB_LINE_LEN
];
481 * Turn a negative snapshot value (invalid), a snapshot value of
482 * 0 (unspecified), or a value bigger than the normal maximum
483 * value, into the maximum allowed value.
485 * If some application really *needs* a bigger snapshot
486 * length, we should just increase MAXIMUM_SNAPLEN.
488 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
489 handle
->snapshot
= MAXIMUM_SNAPLEN
;
491 /* Initialize some components of the pcap structure. */
492 handle
->bufsize
= handle
->snapshot
;
494 handle
->linktype
= DLT_USB_LINUX
;
496 handle
->inject_op
= usb_inject_linux
;
497 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
498 handle
->setdirection_op
= usb_setdirection_linux
;
499 handle
->set_datalink_op
= NULL
; /* can't change data link type */
500 handle
->getnonblock_op
= pcap_getnonblock_fd
;
501 handle
->setnonblock_op
= pcap_setnonblock_fd
;
503 /*get usb bus index from device name */
504 if (sscanf(handle
->opt
.device
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
506 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
507 "Can't get USB bus index from %s", handle
->opt
.device
);
512 * We require 2.6.27 or later kernels, so we have binary-mode support.
513 * Try to open the binary interface.
515 snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handlep
->bus_index
);
516 handle
->fd
= open(full_path
, O_RDONLY
, 0);
520 * The attempt failed; why?
526 * The device doesn't exist.
527 * That could either mean that there's
528 * no support for monitoring USB buses
529 * (which probably means "the usbmon
530 * module isn't loaded") or that there
531 * is but that *particular* device
532 * doesn't exist (no "scan all buses"
533 * device if the bus index is 0, no
534 * such bus if the bus index isn't 0).
536 return PCAP_ERROR_NO_SUCH_DEVICE
;
540 * We didn't have permission to open it.
542 DIAG_OFF_FORMAT_TRUNCATION
543 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
544 "Attempt to open %s failed with EACCES - root privileges may be required",
546 DIAG_ON_FORMAT_TRUNCATION
547 return PCAP_ERROR_PERM_DENIED
;
551 * Something went wrong.
553 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
554 PCAP_ERRBUF_SIZE
, errno
,
555 "Can't open USB bus file %s", full_path
);
560 if (handle
->opt
.rfmon
)
563 * Monitor mode doesn't apply to USB devices.
566 return PCAP_ERROR_RFMON_NOTSUP
;
569 /* try to use fast mmap access */
570 if (usb_mmap(handle
))
573 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
574 handle
->stats_op
= usb_stats_linux_bin
;
575 handle
->read_op
= usb_read_linux_mmap
;
576 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
577 #ifdef HAVE_LINUX_USBDEVICE_FS_H
578 probe_devices(handlep
->bus_index
);
582 * "handle->fd" is a real file, so
583 * "select()" and "poll()" work on it.
585 handle
->selectable_fd
= handle
->fd
;
590 * We failed; try plain binary interface access.
592 * Attempt to set the ring size as appropriate for
593 * the snapshot length, reducing the snapshot length
594 * if that'd make the ring bigger than the kernel
597 if (usb_set_ring_size(handle
, (int)sizeof(pcap_usb_header
)) == -1) {
602 handle
->stats_op
= usb_stats_linux_bin
;
603 handle
->read_op
= usb_read_linux_bin
;
604 #ifdef HAVE_LINUX_USBDEVICE_FS_H
605 probe_devices(handlep
->bus_index
);
609 * "handle->fd" is a real file, so "select()" and "poll()"
612 handle
->selectable_fd
= handle
->fd
;
614 /* for plain binary access and text access we need to allocate the read
616 handle
->buffer
= malloc(handle
->bufsize
);
617 if (!handle
->buffer
) {
618 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
627 usb_inject_linux(pcap_t
*handle
, const void *buf _U_
, int size _U_
)
629 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
630 "Packet injection is not supported on USB devices");
635 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
638 * It's guaranteed, at this point, that d is a valid
646 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
648 struct pcap_usb_linux
*handlep
= handle
->priv
;
650 struct mon_bin_stats st
;
651 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
654 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
655 errno
, "Can't read stats from fd %d", handle
->fd
);
659 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
660 stats
->ps_drop
= st
.dropped
;
661 stats
->ps_ifdrop
= 0;
666 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
667 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
670 usb_read_linux_bin(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
672 struct pcap_usb_linux
*handlep
= handle
->priv
;
673 struct mon_bin_get info
;
675 struct pcap_pkthdr pkth
;
676 u_int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
678 /* the usb header is going to be part of 'packet' data*/
679 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
680 info
.data
= (u_char
*)handle
->buffer
+ sizeof(pcap_usb_header
);
681 info
.data_len
= clen
;
683 /* ignore interrupt system call errors */
685 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
686 if (handle
->break_loop
)
688 handle
->break_loop
= 0;
691 } while ((ret
== -1) && (errno
== EINTR
));
695 return 0; /* no data there */
697 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
698 errno
, "Can't read from fd %d", handle
->fd
);
703 * info.hdr->data_len is the number of bytes of isochronous
704 * descriptors (if any) plus the number of bytes of data
705 * provided. There are no isochronous descriptors here,
706 * because we're using the old 48-byte header.
708 * If info.hdr->data_flag is non-zero, there's no URB data;
709 * info.hdr->urb_len is the size of the buffer into which
710 * data is to be placed; it does not represent the amount
711 * of data transferred. If info.hdr->data_flag is zero,
712 * there is URB data, and info.hdr->urb_len is the number
713 * of bytes transmitted or received; it doesn't include
714 * isochronous descriptors.
716 * The kernel may give us more data than the snaplen; if it did,
717 * reduce the data length so that the total number of bytes we
718 * tell our client we have is not greater than the snaplen.
720 if (info
.hdr
->data_len
< clen
)
721 clen
= info
.hdr
->data_len
;
722 info
.hdr
->data_len
= clen
;
723 pkth
.caplen
= sizeof(pcap_usb_header
) + clen
;
724 if (info
.hdr
->data_flag
) {
726 * No data; just base the on-the-wire length on
727 * info.hdr->data_len (so that it's >= the captured
730 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->data_len
;
733 * We got data; base the on-the-wire length on
734 * info.hdr->urb_len, so that it includes data
735 * discarded by the USB monitor device due to
736 * its buffer being too small.
738 pkth
.len
= sizeof(pcap_usb_header
) + info
.hdr
->urb_len
;
740 pkth
.ts
.tv_sec
= (time_t)info
.hdr
->ts_sec
;
741 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
743 if (handle
->fcode
.bf_insns
== NULL
||
744 pcap_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
745 pkth
.len
, pkth
.caplen
)) {
746 handlep
->packets_read
++;
747 callback(user
, &pkth
, handle
->buffer
);
751 return 0; /* didn't pass filter */
755 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
756 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
760 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
762 struct pcap_usb_linux
*handlep
= handle
->priv
;
763 struct mon_bin_mfetch fetch
;
764 int32_t vec
[VEC_SIZE
];
765 struct pcap_pkthdr pkth
;
766 pcap_usb_header_mmapped
* hdr
;
769 u_int clen
, max_clen
;
771 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header_mmapped
);
777 if (PACKET_COUNT_IS_UNLIMITED(max_packets
)) {
779 * There's no limit on the number of packets
780 * to process, so try to fetch VEC_SIZE packets.
785 * Try to fetch as many packets as we have left
786 * to process, or VEC_SIZE packets, whichever
789 * At this point, max_packets > 0 (otherwise,
790 * PACKET_COUNT_IS_UNLIMITED(max_packets)
791 * would be true) and max_packets > packets
792 * (packet starts out as 0, and the test
793 * at the bottom of the loop exits if
794 * max_packets <= packets), so limit is
795 * guaranteed to be > 0.
797 limit
= max_packets
- packets
;
798 if (limit
> VEC_SIZE
)
803 * Try to fetch as many events as possible, up to
804 * the limit, and flush the events we've processed
805 * earlier (nflush) - MON_IOCX_MFETCH does both
806 * (presumably to reduce the number of system
807 * calls in loops like this).
810 fetch
.nfetch
= limit
;
811 fetch
.nflush
= nflush
;
812 /* ignore interrupt system call errors */
814 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
815 if (handle
->break_loop
)
817 handle
->break_loop
= 0;
820 } while ((ret
== -1) && (errno
== EINTR
));
824 return 0; /* no data there */
826 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
827 PCAP_ERRBUF_SIZE
, errno
, "Can't mfetch fd %d",
832 /* keep track of processed events, we will flush them later */
833 nflush
= fetch
.nfetch
;
834 for (i
=0; i
<fetch
.nfetch
; ++i
) {
836 * XXX - we can't check break_loop here, as
837 * we read the indices of packets into a
838 * local variable, so if we're later called
839 * to fetch more packets, those packets will
840 * not be seen - and won't be flushed, either.
842 * Instead, we would have to keep the array
843 * of indices in our private data, along
844 * with the count of packets to flush - or
845 * would have to flush the already-processed
846 * packets if we break out of the loop here.
850 hdr
= (pcap_usb_header_mmapped
*) &handlep
->mmapbuf
[vec
[i
]];
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 if (hdr
->data_flag
) {
880 * No data; just base the on-the-wire length
881 * on hdr->data_len (so that it's >= the
884 pkth
.len
= sizeof(pcap_usb_header_mmapped
) +
888 * We got data; base the on-the-wire length
889 * on hdr->urb_len, so that it includes
890 * data discarded by the USB monitor device
891 * due to its buffer being too small.
893 pkth
.len
= sizeof(pcap_usb_header_mmapped
) +
894 (hdr
->ndesc
* sizeof (usb_isodesc
)) + hdr
->urb_len
;
896 pkth
.ts
.tv_sec
= (time_t)hdr
->ts_sec
;
897 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
899 if (handle
->fcode
.bf_insns
== NULL
||
900 pcap_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
901 pkth
.len
, pkth
.caplen
)) {
902 handlep
->packets_read
++;
903 callback(user
, &pkth
, (u_char
*) hdr
);
909 * If max_packets specifiesg "unlimited", we stop after
912 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) ||
913 (packets
>= max_packets
))
917 /* flush pending events*/
918 if (ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
) == -1) {
919 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
920 errno
, "Can't mflush fd %d", handle
->fd
);
927 usb_cleanup_linux_mmap(pcap_t
* handle
)
929 struct pcap_usb_linux
*handlep
= handle
->priv
;
931 /* if we have a memory-mapped buffer, unmap it */
932 if (handlep
->mmapbuf
!= NULL
) {
933 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
934 handlep
->mmapbuf
= NULL
;
936 pcap_cleanup_live_common(handle
);