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"
44 #ifdef NEED_STRERROR_H
56 #include <netinet/in.h>
57 #include <sys/ioctl.h>
59 #include <sys/utsname.h>
60 #ifdef HAVE_LINUX_USBDEVICE_FS_H
62 * We might need <linux/compiler.h> to define __user for
63 * <linux/usbdevice_fs.h>.
65 #ifdef HAVE_LINUX_COMPILER_H
66 #include <linux/compiler.h>
67 #endif /* HAVE_LINUX_COMPILER_H */
68 #include <linux/usbdevice_fs.h>
69 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
71 #define USB_IFACE "usbmon"
72 #define USB_TEXT_DIR_OLD "/sys/kernel/debug/usbmon"
73 #define USB_TEXT_DIR "/sys/kernel/debug/usb/usbmon"
74 #define SYS_USB_BUS_DIR "/sys/bus/usb/devices"
75 #define PROC_USB_BUS_DIR "/proc/bus/usb"
76 #define USB_LINE_LEN 4096
78 #if __BYTE_ORDER == __LITTLE_ENDIAN
83 #define htols(s) bswap_16(s)
84 #define htoll(l) bswap_32(l)
85 #define htol64(ll) bswap_64(ll)
88 struct mon_bin_stats
{
96 size_t data_len
; /* Length of data (can be zero) */
99 struct mon_bin_mfetch
{
100 int32_t *offvec
; /* Vector of events fetched */
101 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
102 int32_t nflush
; /* Number of events to flush */
105 #define MON_IOC_MAGIC 0x92
107 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
108 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
109 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
110 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
111 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
112 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
113 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
114 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
116 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
117 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
118 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
119 #define MON_BIN_ERROR 0x8
122 * Private data for capturing on Linux USB.
124 struct pcap_usb_linux
{
125 u_char
*mmapbuf
; /* memory-mapped region pointer */
126 size_t mmapbuflen
; /* size of region */
131 /* forward declaration */
132 static int usb_activate(pcap_t
*);
133 static int usb_stats_linux(pcap_t
*, struct pcap_stat
*);
134 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
135 static int usb_read_linux(pcap_t
*, int , pcap_handler
, u_char
*);
136 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
137 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
138 static int usb_inject_linux(pcap_t
*, const void *, size_t);
139 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
140 static void usb_cleanup_linux_mmap(pcap_t
*);
143 have_binary_usbmon(void)
145 struct utsname utsname
;
146 char *version_component
, *endp
;
147 int major
, minor
, subminor
;
149 if (uname(&utsname
) == 0) {
151 * 2.6.21 is the first release with the binary-mode
154 version_component
= utsname
.release
;
155 major
= strtol(version_component
, &endp
, 10);
156 if (endp
!= version_component
&& *endp
== '.') {
158 * OK, that was a valid major version.
159 * Is it 3 or greater? If so, we have binary
166 * Is it 1 or less? If so, we don't have binary
167 * mode support. (In fact, we don't have any
168 * USB monitoring....)
175 * OK, this is a 2.x kernel.
176 * What's the minor version?
178 version_component
= endp
+ 1;
179 minor
= strtol(version_component
, &endp
, 10);
180 if (endp
!= version_component
&&
181 (*endp
== '.' || *endp
== '\0')) {
183 * OK, that was a valid minor version.
184 * Is is 2.6 or later? (There shouldn't be a
185 * "later", as 2.6.x went to 3.x, but we'll
190 * No, so no binary support (did 2.4 have
191 * any USB monitoring at all?)
197 * OK, this is a 2.6.x kernel.
198 * What's the subminor version?
200 version_component
= endp
+ 1;
201 subminor
= strtol(version_component
, &endp
, 10);
202 if (endp
!= version_component
&&
203 (*endp
== '.' || *endp
== '\0')) {
205 * OK, that was a valid subminor version.
206 * Is it 21 or greater?
208 if (subminor
>= 21) {
210 * Yes - we have binary mode
220 * Either uname() failed, in which case we just say "no binary
221 * mode support", or we don't have binary mode support.
226 /* facility to add an USB device to the device list*/
228 usb_dev_add(pcap_if_list_t
*devlistp
, int n
, char *err_str
)
232 pcap_snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
234 pcap_snprintf(dev_descr
, 30, "All USB buses");
236 pcap_snprintf(dev_descr
, 30, "USB bus number %d", n
);
238 if (add_dev(devlistp
, dev_name
, 0, dev_descr
, err_str
) == NULL
)
244 usb_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
246 char usb_mon_dir
[PATH_MAX
];
247 char *usb_mon_prefix
;
248 size_t usb_mon_prefix_len
;
256 if (have_binary_usbmon()) {
258 * We have binary-mode support.
259 * What do the device names look like?
260 * Split LINUX_USB_MON_DEV into a directory that we'll
261 * scan and a file name prefix that we'll check for.
263 strlcpy(usb_mon_dir
, LINUX_USB_MON_DEV
, sizeof usb_mon_dir
);
264 usb_mon_prefix
= strrchr(usb_mon_dir
, '/');
265 if (usb_mon_prefix
== NULL
) {
267 * This "shouldn't happen". Just give up if it
272 *usb_mon_prefix
++ = '\0';
273 usb_mon_prefix_len
= strlen(usb_mon_prefix
);
276 * Open the directory and scan it.
278 dir
= opendir(usb_mon_dir
);
280 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
284 * Is this a usbmon device?
286 if (strncmp(name
, usb_mon_prefix
, usb_mon_prefix_len
) != 0)
290 * What's the device number?
292 if (sscanf(&name
[usb_mon_prefix_len
], "%d", &n
) == 0)
293 continue; /* failed */
295 ret
= usb_dev_add(devlistp
, n
, err_str
);
303 * We have only text mode support.
304 * We don't look for the text devices because we can't
305 * look for them without root privileges, and we don't
306 * want to require root privileges to enumerate devices
307 * (we want to let the user to try a device and get
308 * an error, rather than seeing no devices and asking
309 * "why am I not seeing devices" and forcing a long
310 * process of poking to figure out whether it's "no
311 * privileges" or "your kernel is too old" or "the
312 * usbmon module isn't loaded" or...).
314 * Instead, we look to see what buses we have.
315 * If the kernel is so old that it doesn't have
316 * binary-mode support, it's also so old that
317 * it doesn't have a "scan all buses" device.
319 * First, try scanning sysfs USB bus directory.
321 dir
= opendir(SYS_USB_BUS_DIR
);
323 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
326 if (strncmp(name
, "usb", 3) != 0)
329 if (sscanf(&name
[3], "%d", &n
) == 0)
332 ret
= usb_dev_add(devlistp
, n
, err_str
);
339 /* That didn't work; try scanning procfs USB bus directory. */
340 dir
= opendir(PROC_USB_BUS_DIR
);
342 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
346 /* if this file name does not end with a number it's not of our interest */
347 if ((len
< 1) || !isdigit(name
[--len
]))
349 while (isdigit(name
[--len
]));
350 if (sscanf(&name
[len
+1], "%d", &n
) != 1)
353 ret
= usb_dev_add(devlistp
, n
, err_str
);
360 /* neither of them worked */
366 int usb_mmap(pcap_t
* handle
)
368 struct pcap_usb_linux
*handlep
= handle
->priv
;
369 int len
= ioctl(handle
->fd
, MON_IOCQ_RING_SIZE
);
373 handlep
->mmapbuflen
= len
;
374 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
375 MAP_SHARED
, handle
->fd
, 0);
376 return handlep
->mmapbuf
!= MAP_FAILED
;
379 #ifdef HAVE_LINUX_USBDEVICE_FS_H
381 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
383 #define USB_DIR_IN 0x80
384 #define USB_TYPE_STANDARD 0x00
385 #define USB_RECIP_DEVICE 0x00
387 #define USB_REQ_GET_DESCRIPTOR 6
389 #define USB_DT_DEVICE 1
391 /* probe the descriptors of the devices attached to the bus */
392 /* the descriptors will end up in the captured packet stream */
393 /* and be decoded by external apps like wireshark */
394 /* without these identifying probes packet data can't be fully decoded */
396 probe_devices(int bus
)
398 struct usbdevfs_ctrltransfer ctrl
;
401 char buf
[sizeof("/dev/bus/usb/000/") + NAME_MAX
];
404 /* scan usb bus directories for device nodes */
405 pcap_snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d", bus
);
410 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
412 char* name
= data
->d_name
;
417 pcap_snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
419 fd
= open(buf
, O_RDWR
);
424 * Sigh. Different kernels have different member names
425 * for this structure.
427 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
428 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
429 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
430 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
432 ctrl
.wLength
= sizeof(buf
);
434 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
435 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
436 ctrl
.value
= USB_DT_DEVICE
<< 8;
438 ctrl
.length
= sizeof(buf
);
441 ctrl
.timeout
= CTRL_TIMEOUT
;
443 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
449 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
452 usb_create(const char *device
, char *ebuf
, int *is_ours
)
459 /* Does this look like a USB monitoring device? */
460 cp
= strrchr(device
, '/');
463 /* Does it begin with USB_IFACE? */
464 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
465 /* Nope, doesn't begin with USB_IFACE */
469 /* Yes - is USB_IFACE followed by a number? */
470 cp
+= sizeof USB_IFACE
- 1;
471 devnum
= strtol(cp
, &cpend
, 10);
472 if (cpend
== cp
|| *cpend
!= '\0') {
473 /* Not followed by a number. */
478 /* Followed by a non-valid number. */
483 /* OK, it's probably ours. */
486 p
= pcap_create_common(ebuf
, sizeof (struct pcap_usb_linux
));
490 p
->activate_op
= usb_activate
;
495 usb_activate(pcap_t
* handle
)
497 struct pcap_usb_linux
*handlep
= handle
->priv
;
498 char full_path
[USB_LINE_LEN
];
501 * Turn a negative snapshot value (invalid), a snapshot value of
502 * 0 (unspecified), or a value bigger than the normal maximum
503 * value, into the maximum allowed value.
505 * If some application really *needs* a bigger snapshot
506 * length, we should just increase MAXIMUM_SNAPLEN.
508 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
509 handle
->snapshot
= MAXIMUM_SNAPLEN
;
511 /* Initialize some components of the pcap structure. */
512 handle
->bufsize
= handle
->snapshot
;
514 handle
->linktype
= DLT_USB_LINUX
;
516 handle
->inject_op
= usb_inject_linux
;
517 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
518 handle
->setdirection_op
= usb_setdirection_linux
;
519 handle
->set_datalink_op
= NULL
; /* can't change data link type */
520 handle
->getnonblock_op
= pcap_getnonblock_fd
;
521 handle
->setnonblock_op
= pcap_setnonblock_fd
;
523 /*get usb bus index from device name */
524 if (sscanf(handle
->opt
.device
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
526 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
527 "Can't get USB bus index from %s", handle
->opt
.device
);
531 if (have_binary_usbmon())
534 * We have binary-mode support.
535 * Try to open the binary interface.
537 pcap_snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handlep
->bus_index
);
538 handle
->fd
= open(full_path
, O_RDONLY
, 0);
542 * The attempt failed; why?
548 * The device doesn't exist.
549 * That could either mean that there's
550 * no support for monitoring USB buses
551 * (which probably means "the usbmon
552 * module isn't loaded") or that there
553 * is but that *particular* device
554 * doesn't exist (no "scan all buses"
555 * device if the bus index is 0, no
556 * such bus if the bus index isn't 0).
558 return PCAP_ERROR_NO_SUCH_DEVICE
;
562 * We didn't have permission to open it.
564 return PCAP_ERROR_PERM_DENIED
;
568 * Something went wrong.
570 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
571 PCAP_ERRBUF_SIZE
, errno
,
572 "Can't open USB bus file %s", full_path
);
577 if (handle
->opt
.rfmon
)
580 * Monitor mode doesn't apply to USB devices.
583 return PCAP_ERROR_RFMON_NOTSUP
;
586 /* try to use fast mmap access */
587 if (usb_mmap(handle
))
589 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
590 handle
->stats_op
= usb_stats_linux_bin
;
591 handle
->read_op
= usb_read_linux_mmap
;
592 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
593 #ifdef HAVE_LINUX_USBDEVICE_FS_H
594 probe_devices(handlep
->bus_index
);
598 * "handle->fd" is a real file, so
599 * "select()" and "poll()" work on it.
601 handle
->selectable_fd
= handle
->fd
;
605 /* can't mmap, use plain binary interface access */
606 handle
->stats_op
= usb_stats_linux_bin
;
607 handle
->read_op
= usb_read_linux_bin
;
608 #ifdef HAVE_LINUX_USBDEVICE_FS_H
609 probe_devices(handlep
->bus_index
);
614 * We don't have binary mode support.
615 * Try opening the text-mode device.
617 pcap_snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR
"/%dt", handlep
->bus_index
);
618 handle
->fd
= open(full_path
, O_RDONLY
, 0);
624 * Not found at the new location; try
627 pcap_snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%dt", handlep
->bus_index
);
628 handle
->fd
= open(full_path
, O_RDONLY
, 0);
630 if (handle
->fd
< 0) {
632 * Is the problem that we didn't have
633 * sufficient permission to open it?
635 if (errno
== EACCES
) {
637 * Yes - return that error.
639 return PCAP_ERROR_PERM_DENIED
;
643 * No - was the problem something other
644 * than "it doesn't exist"?
646 if (errno
!= ENOENT
) {
648 * Yes - return *that* error.
650 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
651 PCAP_ERRBUF_SIZE
, errno
,
652 "Can't open USB bus file %s",
658 * No. Report that as "no such device".
659 * (That could mean "no such USB bus"
660 * or "monitoring not supported".)
662 return PCAP_ERROR_NO_SUCH_DEVICE
;
666 if (handle
->opt
.rfmon
)
669 * Monitor mode doesn't apply to USB devices.
672 return PCAP_ERROR_RFMON_NOTSUP
;
675 handle
->stats_op
= usb_stats_linux
;
676 handle
->read_op
= usb_read_linux
;
680 * "handle->fd" is a real file, so "select()" and "poll()"
683 handle
->selectable_fd
= handle
->fd
;
685 /* for plain binary access and text access we need to allocate the read
687 handle
->buffer
= malloc(handle
->bufsize
);
688 if (!handle
->buffer
) {
689 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
700 return c
< 'A' ? c
- '0': ((c
<'a') ? c
- 'A' + 10: c
-'a'+10);
704 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
705 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
709 usb_read_linux(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
712 * /usr/src/linux/Documentation/usb/usbmon.txt
715 struct pcap_usb_linux
*handlep
= handle
->priv
;
717 int tag
, cnt
, ep_num
, dev_addr
, dummy
, ret
, urb_len
, data_len
;
718 char etype
, pipeid1
, pipeid2
, status
[16], urb_tag
, line
[USB_LINE_LEN
];
720 u_char
* rawdata
= handle
->buffer
;
721 struct pcap_pkthdr pkth
;
722 pcap_usb_header
* uhdr
= (pcap_usb_header
*)handle
->buffer
;
723 u_char urb_transfer
=0;
726 /* ignore interrupt system call errors */
728 ret
= read(handle
->fd
, line
, USB_LINE_LEN
- 1);
729 if (handle
->break_loop
)
731 handle
->break_loop
= 0;
734 } while ((ret
== -1) && (errno
== EINTR
));
738 return 0; /* no data there */
740 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
741 errno
, "Can't read from fd %d", handle
->fd
);
745 /* read urb header; %n argument may increment return value, but it's
746 * not mandatory, so does not count on it*/
748 ret
= sscanf(string
, "%x %d %c %c%c:%d:%d %s%n", &tag
, ×tamp
, &etype
,
749 &pipeid1
, &pipeid2
, &dev_addr
, &ep_num
, status
,
753 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
754 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
759 uhdr
->device_address
= dev_addr
;
760 uhdr
->bus_id
= handlep
->bus_index
;
764 /* don't use usbmon provided timestamp, since it have low precision*/
765 if (gettimeofday(&pkth
.ts
, NULL
) < 0)
767 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
768 errno
, "Can't get timestamp for message '%s'", string
);
771 uhdr
->ts_sec
= pkth
.ts
.tv_sec
;
772 uhdr
->ts_usec
= pkth
.ts
.tv_usec
;
774 /* parse endpoint information */
776 urb_transfer
= URB_CONTROL
;
777 else if (pipeid1
== 'Z')
778 urb_transfer
= URB_ISOCHRONOUS
;
779 else if (pipeid1
== 'I')
780 urb_transfer
= URB_INTERRUPT
;
781 else if (pipeid1
== 'B')
782 urb_transfer
= URB_BULK
;
783 if (pipeid2
== 'i') {
784 ep_num
|= URB_TRANSFER_IN
;
788 incoming
= !incoming
;
793 if (handle
->direction
== PCAP_D_OUT
)
797 if (handle
->direction
== PCAP_D_IN
)
799 uhdr
->event_type
= etype
;
800 uhdr
->transfer_type
= urb_transfer
;
801 uhdr
->endpoint_number
= ep_num
;
802 pkth
.caplen
= sizeof(pcap_usb_header
);
803 rawdata
+= sizeof(pcap_usb_header
);
805 /* check if this is a setup packet */
806 ret
= sscanf(status
, "%d", &dummy
);
809 /* this a setup packet, setup data can be filled with underscore if
810 * usbmon has not been able to read them, so we must parse this fields as
812 pcap_usb_setup
* shdr
;
813 char str1
[3], str2
[3], str3
[5], str4
[5], str5
[5];
814 ret
= sscanf(string
, "%s %s %s %s %s%n", str1
, str2
, str3
, str4
,
818 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
819 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
825 /* try to convert to corresponding integer */
827 shdr
->bmRequestType
= strtoul(str1
, 0, 16);
828 shdr
->bRequest
= strtoul(str2
, 0, 16);
829 shdr
->wValue
= htols(strtoul(str3
, 0, 16));
830 shdr
->wIndex
= htols(strtoul(str4
, 0, 16));
831 shdr
->wLength
= htols(strtoul(str5
, 0, 16));
833 uhdr
->setup_flag
= 0;
836 uhdr
->setup_flag
= 1;
839 ret
= sscanf(string
, " %d%n", &urb_len
, &cnt
);
842 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
843 "Can't parse urb length from '%s'", string
);
848 /* urb tag is not present if urb length is 0, so we can stop here
850 pkth
.len
= urb_len
+pkth
.caplen
;
851 uhdr
->urb_len
= urb_len
;
854 if (uhdr
->urb_len
== 0)
857 /* check for data presence; data is present if and only if urb tag is '=' */
858 if (sscanf(string
, " %c", &urb_tag
) != 1)
860 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
861 "Can't parse urb tag from '%s'", string
);
868 /* skip urb tag and following space */
871 /* if we reach this point we got some urb data*/
874 /* read all urb data; if urb length is greater then the usbmon internal
875 * buffer length used by the kernel to spool the URB, we get only
876 * a partial information.
877 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
878 * length and default value is 130. */
879 while ((string
[0] != 0) && (string
[1] != 0) && (pkth
.caplen
< (bpf_u_int32
)handle
->snapshot
))
881 rawdata
[0] = ascii_to_int(string
[0]) * 16 + ascii_to_int(string
[1]);
884 if (string
[0] == ' ')
891 uhdr
->data_len
= data_len
;
892 if (pkth
.caplen
> (bpf_u_int32
)handle
->snapshot
)
893 pkth
.caplen
= (bpf_u_int32
)handle
->snapshot
;
895 if (handle
->fcode
.bf_insns
== NULL
||
896 bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
897 pkth
.len
, pkth
.caplen
)) {
898 handlep
->packets_read
++;
899 callback(user
, &pkth
, handle
->buffer
);
902 return 0; /* didn't pass filter */
906 usb_inject_linux(pcap_t
*handle
, const void *buf _U_
, size_t size _U_
)
908 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on "
914 usb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
916 struct pcap_usb_linux
*handlep
= handle
->priv
;
917 int dummy
, ret
, consumed
, cnt
;
918 char string
[USB_LINE_LEN
];
919 char token
[USB_LINE_LEN
];
923 pcap_snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR
"/%ds", handlep
->bus_index
);
924 fd
= open(string
, O_RDONLY
, 0);
930 * Not found at the new location; try the old
933 pcap_snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%ds", handlep
->bus_index
);
934 fd
= open(string
, O_RDONLY
, 0);
937 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
938 PCAP_ERRBUF_SIZE
, errno
,
939 "Can't open USB stats file %s", string
);
944 /* read stats line */
946 ret
= read(fd
, string
, USB_LINE_LEN
-1);
947 } while ((ret
== -1) && (errno
== EINTR
));
952 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
953 "Can't read stats from fd %d ", fd
);
958 /* extract info on dropped urbs */
959 for (consumed
=0; consumed
< ret
; ) {
960 /* from the sscanf man page:
961 * The C standard says: "Execution of a %n directive does
962 * not increment the assignment count returned at the completion
963 * of execution" but the Corrigendum seems to contradict this.
964 * Do not make any assumptions on the effect of %n conversions
965 * on the return value and explicitly check for cnt assignmet*/
969 ntok
= sscanf(ptr
, "%s%n", token
, &cnt
);
970 if ((ntok
< 1) || (cnt
< 0))
974 if (strcmp(token
, "nreaders") == 0)
975 ret
= sscanf(ptr
, "%d", &stats
->ps_drop
);
977 ret
= sscanf(ptr
, "%d", &dummy
);
984 stats
->ps_recv
= handlep
->packets_read
;
985 stats
->ps_ifdrop
= 0;
990 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
998 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
1000 struct pcap_usb_linux
*handlep
= handle
->priv
;
1002 struct mon_bin_stats st
;
1003 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
1006 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1007 errno
, "Can't read stats from fd %d", handle
->fd
);
1011 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
1012 stats
->ps_drop
= st
.dropped
;
1013 stats
->ps_ifdrop
= 0;
1018 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
1019 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
1022 usb_read_linux_bin(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
1024 struct pcap_usb_linux
*handlep
= handle
->priv
;
1025 struct mon_bin_get info
;
1027 struct pcap_pkthdr pkth
;
1028 u_int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
1030 /* the usb header is going to be part of 'packet' data*/
1031 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
1032 info
.data
= (u_char
*)handle
->buffer
+ sizeof(pcap_usb_header
);
1033 info
.data_len
= clen
;
1035 /* ignore interrupt system call errors */
1037 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
1038 if (handle
->break_loop
)
1040 handle
->break_loop
= 0;
1043 } while ((ret
== -1) && (errno
== EINTR
));
1046 if (errno
== EAGAIN
)
1047 return 0; /* no data there */
1049 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1050 errno
, "Can't read from fd %d", handle
->fd
);
1054 /* we can get less that than really captured from kernel, depending on
1055 * snaplen, so adjust header accordingly */
1056 if (info
.hdr
->data_len
< clen
)
1057 clen
= info
.hdr
->data_len
;
1058 info
.hdr
->data_len
= clen
;
1059 pkth
.caplen
= clen
+ sizeof(pcap_usb_header
);
1060 pkth
.len
= info
.hdr
->data_len
+ sizeof(pcap_usb_header
);
1061 pkth
.ts
.tv_sec
= info
.hdr
->ts_sec
;
1062 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
1064 if (handle
->fcode
.bf_insns
== NULL
||
1065 bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
1066 pkth
.len
, pkth
.caplen
)) {
1067 handlep
->packets_read
++;
1068 callback(user
, &pkth
, handle
->buffer
);
1072 return 0; /* didn't pass filter */
1076 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
1077 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
1081 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
1083 struct pcap_usb_linux
*handlep
= handle
->priv
;
1084 struct mon_bin_mfetch fetch
;
1085 int32_t vec
[VEC_SIZE
];
1086 struct pcap_pkthdr pkth
;
1087 pcap_usb_header
* hdr
;
1090 u_int clen
, max_clen
;
1092 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
1096 int limit
= max_packets
- packets
;
1099 if (limit
> VEC_SIZE
)
1102 /* try to fetch as many events as possible*/
1104 fetch
.nfetch
= limit
;
1105 fetch
.nflush
= nflush
;
1106 /* ignore interrupt system call errors */
1108 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
1109 if (handle
->break_loop
)
1111 handle
->break_loop
= 0;
1114 } while ((ret
== -1) && (errno
== EINTR
));
1117 if (errno
== EAGAIN
)
1118 return 0; /* no data there */
1120 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
1121 PCAP_ERRBUF_SIZE
, errno
, "Can't mfetch fd %d",
1126 /* keep track of processed events, we will flush them later */
1127 nflush
= fetch
.nfetch
;
1128 for (i
=0; i
<fetch
.nfetch
; ++i
) {
1129 /* discard filler */
1130 hdr
= (pcap_usb_header
*) &handlep
->mmapbuf
[vec
[i
]];
1131 if (hdr
->event_type
== '@')
1134 /* we can get less that than really captured from kernel, depending on
1135 * snaplen, so adjust header accordingly */
1137 if (hdr
->data_len
< clen
)
1138 clen
= hdr
->data_len
;
1140 /* get packet info from header*/
1141 pkth
.caplen
= clen
+ sizeof(pcap_usb_header_mmapped
);
1142 pkth
.len
= hdr
->data_len
+ sizeof(pcap_usb_header_mmapped
);
1143 pkth
.ts
.tv_sec
= hdr
->ts_sec
;
1144 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
1146 if (handle
->fcode
.bf_insns
== NULL
||
1147 bpf_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
1148 pkth
.len
, pkth
.caplen
)) {
1149 handlep
->packets_read
++;
1150 callback(user
, &pkth
, (u_char
*) hdr
);
1155 /* with max_packets specifying "unlimited" we stop afer the first chunk*/
1156 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) || (packets
== max_packets
))
1160 /* flush pending events*/
1161 if (ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
) == -1) {
1162 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1163 errno
, "Can't mflush fd %d", handle
->fd
);
1170 usb_cleanup_linux_mmap(pcap_t
* handle
)
1172 struct pcap_usb_linux
*handlep
= handle
->priv
;
1174 /* if we have a memory-mapped buffer, unmap it */
1175 if (handlep
->mmapbuf
!= NULL
) {
1176 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
1177 handlep
->mmapbuf
= NULL
;
1179 pcap_cleanup_live_common(handle
);