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
57 #include <netinet/in.h>
58 #include <sys/ioctl.h>
60 #include <sys/utsname.h>
61 #ifdef HAVE_LINUX_USBDEVICE_FS_H
63 * We might need <linux/compiler.h> to define __user for
64 * <linux/usbdevice_fs.h>.
66 #ifdef HAVE_LINUX_COMPILER_H
67 #include <linux/compiler.h>
68 #endif /* HAVE_LINUX_COMPILER_H */
69 #include <linux/usbdevice_fs.h>
70 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
72 #define USB_IFACE "usbmon"
73 #define USB_TEXT_DIR_OLD "/sys/kernel/debug/usbmon"
74 #define USB_TEXT_DIR "/sys/kernel/debug/usb/usbmon"
75 #define SYS_USB_BUS_DIR "/sys/bus/usb/devices"
76 #define PROC_USB_BUS_DIR "/proc/bus/usb"
77 #define USB_LINE_LEN 4096
79 #if __BYTE_ORDER == __LITTLE_ENDIAN
84 #define htols(s) bswap_16(s)
85 #define htoll(l) bswap_32(l)
86 #define htol64(ll) bswap_64(ll)
89 struct mon_bin_stats
{
97 size_t data_len
; /* Length of data (can be zero) */
100 struct mon_bin_mfetch
{
101 int32_t *offvec
; /* Vector of events fetched */
102 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
103 int32_t nflush
; /* Number of events to flush */
106 #define MON_IOC_MAGIC 0x92
108 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
109 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
110 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
111 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
112 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
113 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
114 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
115 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
117 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
118 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
119 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
120 #define MON_BIN_ERROR 0x8
123 * Private data for capturing on Linux USB.
125 struct pcap_usb_linux
{
126 u_char
*mmapbuf
; /* memory-mapped region pointer */
127 size_t mmapbuflen
; /* size of region */
132 /* forward declaration */
133 static int usb_activate(pcap_t
*);
134 static int usb_stats_linux(pcap_t
*, struct pcap_stat
*);
135 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
136 static int usb_read_linux(pcap_t
*, int , pcap_handler
, u_char
*);
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
*);
144 have_binary_usbmon(void)
146 struct utsname utsname
;
147 char *version_component
, *endp
;
148 long major
, minor
, subminor
;
150 if (uname(&utsname
) == 0) {
152 * 2.6.21 is the first release with the binary-mode
155 version_component
= utsname
.release
;
156 major
= strtol(version_component
, &endp
, 10);
157 if (endp
!= version_component
&& *endp
== '.') {
159 * OK, that was a valid major version.
160 * Is it 3 or greater? If so, we have binary
167 * Is it 1 or less? If so, we don't have binary
168 * mode support. (In fact, we don't have any
169 * USB monitoring....)
176 * OK, this is a 2.x kernel.
177 * What's the minor version?
179 version_component
= endp
+ 1;
180 minor
= strtol(version_component
, &endp
, 10);
181 if (endp
!= version_component
&&
182 (*endp
== '.' || *endp
== '\0')) {
184 * OK, that was a valid minor version.
185 * Is is 2.6 or later? (There shouldn't be a
186 * "later", as 2.6.x went to 3.x, but we'll
191 * No, so no binary support (did 2.4 have
192 * any USB monitoring at all?)
198 * OK, this is a 2.6.x kernel.
199 * What's the subminor version?
201 version_component
= endp
+ 1;
202 subminor
= strtol(version_component
, &endp
, 10);
203 if (endp
!= version_component
&&
204 (*endp
== '.' || *endp
== '\0')) {
206 * OK, that was a valid subminor version.
207 * Is it 21 or greater?
209 if (subminor
>= 21) {
211 * Yes - we have binary mode
221 * Either uname() failed, in which case we just say "no binary
222 * mode support", or we don't have binary mode support.
227 /* facility to add an USB device to the device list*/
229 usb_dev_add(pcap_if_list_t
*devlistp
, int n
, char *err_str
)
233 pcap_snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
235 * XXX - is there any notion of "up" and "running"?
239 * As this refers to all buses, there's no notion of
240 * "connected" vs. "disconnected", as that's a property
241 * that would apply to a particular USB interface.
243 if (add_dev(devlistp
, dev_name
,
244 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
,
245 "All USB buses", err_str
) == NULL
)
249 * XXX - is there a way to determine whether anything's
250 * plugged into this bus interface or not, and set
251 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
252 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
254 pcap_snprintf(dev_descr
, 30, "USB bus number %d", n
);
255 if (add_dev(devlistp
, dev_name
, 0, dev_descr
, err_str
) == NULL
)
263 usb_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
265 char usb_mon_dir
[PATH_MAX
];
266 char *usb_mon_prefix
;
267 size_t usb_mon_prefix_len
;
275 if (have_binary_usbmon()) {
277 * We have binary-mode support.
278 * What do the device names look like?
279 * Split LINUX_USB_MON_DEV into a directory that we'll
280 * scan and a file name prefix that we'll check for.
282 pcap_strlcpy(usb_mon_dir
, LINUX_USB_MON_DEV
, sizeof usb_mon_dir
);
283 usb_mon_prefix
= strrchr(usb_mon_dir
, '/');
284 if (usb_mon_prefix
== NULL
) {
286 * This "shouldn't happen". Just give up if it
291 *usb_mon_prefix
++ = '\0';
292 usb_mon_prefix_len
= strlen(usb_mon_prefix
);
295 * Open the directory and scan it.
297 dir
= opendir(usb_mon_dir
);
299 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
303 * Is this a usbmon device?
305 if (strncmp(name
, usb_mon_prefix
, usb_mon_prefix_len
) != 0)
309 * What's the device number?
311 if (sscanf(&name
[usb_mon_prefix_len
], "%d", &n
) == 0)
312 continue; /* failed */
314 ret
= usb_dev_add(devlistp
, n
, err_str
);
322 * We have only text mode support.
323 * We don't look for the text devices because we can't
324 * look for them without root privileges, and we don't
325 * want to require root privileges to enumerate devices
326 * (we want to let the user to try a device and get
327 * an error, rather than seeing no devices and asking
328 * "why am I not seeing devices" and forcing a long
329 * process of poking to figure out whether it's "no
330 * privileges" or "your kernel is too old" or "the
331 * usbmon module isn't loaded" or...).
333 * Instead, we look to see what buses we have.
334 * If the kernel is so old that it doesn't have
335 * binary-mode support, it's also so old that
336 * it doesn't have a "scan all buses" device.
338 * First, try scanning sysfs USB bus directory.
340 dir
= opendir(SYS_USB_BUS_DIR
);
342 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
345 if (strncmp(name
, "usb", 3) != 0)
348 if (sscanf(&name
[3], "%d", &n
) == 0)
351 ret
= usb_dev_add(devlistp
, n
, err_str
);
358 /* That didn't work; try scanning procfs USB bus directory. */
359 dir
= opendir(PROC_USB_BUS_DIR
);
361 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
365 /* if this file name does not end with a number it's not of our interest */
366 if ((len
< 1) || !isdigit(name
[--len
]))
368 while (isdigit(name
[--len
]));
369 if (sscanf(&name
[len
+1], "%d", &n
) != 1)
372 ret
= usb_dev_add(devlistp
, n
, err_str
);
379 /* neither of them worked */
385 int usb_mmap(pcap_t
* handle
)
387 struct pcap_usb_linux
*handlep
= handle
->priv
;
388 int len
= ioctl(handle
->fd
, MON_IOCQ_RING_SIZE
);
392 handlep
->mmapbuflen
= len
;
393 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
394 MAP_SHARED
, handle
->fd
, 0);
395 return handlep
->mmapbuf
!= MAP_FAILED
;
398 #ifdef HAVE_LINUX_USBDEVICE_FS_H
400 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
402 #define USB_DIR_IN 0x80
403 #define USB_TYPE_STANDARD 0x00
404 #define USB_RECIP_DEVICE 0x00
406 #define USB_REQ_GET_DESCRIPTOR 6
408 #define USB_DT_DEVICE 1
410 /* probe the descriptors of the devices attached to the bus */
411 /* the descriptors will end up in the captured packet stream */
412 /* and be decoded by external apps like wireshark */
413 /* without these identifying probes packet data can't be fully decoded */
415 probe_devices(int bus
)
417 struct usbdevfs_ctrltransfer ctrl
;
420 char buf
[sizeof("/dev/bus/usb/000/") + NAME_MAX
];
423 /* scan usb bus directories for device nodes */
424 pcap_snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d", bus
);
429 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
431 char* name
= data
->d_name
;
436 pcap_snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
438 fd
= open(buf
, O_RDWR
);
443 * Sigh. Different kernels have different member names
444 * for this structure.
446 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
447 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
448 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
449 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
451 ctrl
.wLength
= sizeof(buf
);
453 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
454 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
455 ctrl
.value
= USB_DT_DEVICE
<< 8;
457 ctrl
.length
= sizeof(buf
);
460 ctrl
.timeout
= CTRL_TIMEOUT
;
462 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
468 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
471 usb_create(const char *device
, char *ebuf
, int *is_ours
)
478 /* Does this look like a USB monitoring device? */
479 cp
= strrchr(device
, '/');
482 /* Does it begin with USB_IFACE? */
483 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
484 /* Nope, doesn't begin with USB_IFACE */
488 /* Yes - is USB_IFACE followed by a number? */
489 cp
+= sizeof USB_IFACE
- 1;
490 devnum
= strtol(cp
, &cpend
, 10);
491 if (cpend
== cp
|| *cpend
!= '\0') {
492 /* Not followed by a number. */
497 /* Followed by a non-valid number. */
502 /* OK, it's probably ours. */
505 p
= pcap_create_common(ebuf
, sizeof (struct pcap_usb_linux
));
509 p
->activate_op
= usb_activate
;
514 usb_activate(pcap_t
* handle
)
516 struct pcap_usb_linux
*handlep
= handle
->priv
;
517 char full_path
[USB_LINE_LEN
];
520 * Turn a negative snapshot value (invalid), a snapshot value of
521 * 0 (unspecified), or a value bigger than the normal maximum
522 * value, into the maximum allowed value.
524 * If some application really *needs* a bigger snapshot
525 * length, we should just increase MAXIMUM_SNAPLEN.
527 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
528 handle
->snapshot
= MAXIMUM_SNAPLEN
;
530 /* Initialize some components of the pcap structure. */
531 handle
->bufsize
= handle
->snapshot
;
533 handle
->linktype
= DLT_USB_LINUX
;
535 handle
->inject_op
= usb_inject_linux
;
536 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
537 handle
->setdirection_op
= usb_setdirection_linux
;
538 handle
->set_datalink_op
= NULL
; /* can't change data link type */
539 handle
->getnonblock_op
= pcap_getnonblock_fd
;
540 handle
->setnonblock_op
= pcap_setnonblock_fd
;
542 /*get usb bus index from device name */
543 if (sscanf(handle
->opt
.device
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
545 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
546 "Can't get USB bus index from %s", handle
->opt
.device
);
550 if (have_binary_usbmon())
553 * We have binary-mode support.
554 * Try to open the binary interface.
556 pcap_snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handlep
->bus_index
);
557 handle
->fd
= open(full_path
, O_RDONLY
, 0);
561 * The attempt failed; why?
567 * The device doesn't exist.
568 * That could either mean that there's
569 * no support for monitoring USB buses
570 * (which probably means "the usbmon
571 * module isn't loaded") or that there
572 * is but that *particular* device
573 * doesn't exist (no "scan all buses"
574 * device if the bus index is 0, no
575 * such bus if the bus index isn't 0).
577 return PCAP_ERROR_NO_SUCH_DEVICE
;
581 * We didn't have permission to open it.
583 return PCAP_ERROR_PERM_DENIED
;
587 * Something went wrong.
589 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
590 PCAP_ERRBUF_SIZE
, errno
,
591 "Can't open USB bus file %s", full_path
);
596 if (handle
->opt
.rfmon
)
599 * Monitor mode doesn't apply to USB devices.
602 return PCAP_ERROR_RFMON_NOTSUP
;
605 /* try to use fast mmap access */
606 if (usb_mmap(handle
))
608 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
609 handle
->stats_op
= usb_stats_linux_bin
;
610 handle
->read_op
= usb_read_linux_mmap
;
611 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
612 #ifdef HAVE_LINUX_USBDEVICE_FS_H
613 probe_devices(handlep
->bus_index
);
617 * "handle->fd" is a real file, so
618 * "select()" and "poll()" work on it.
620 handle
->selectable_fd
= handle
->fd
;
624 /* can't mmap, use plain binary interface access */
625 handle
->stats_op
= usb_stats_linux_bin
;
626 handle
->read_op
= usb_read_linux_bin
;
627 #ifdef HAVE_LINUX_USBDEVICE_FS_H
628 probe_devices(handlep
->bus_index
);
633 * We don't have binary mode support.
634 * Try opening the text-mode device.
636 pcap_snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR
"/%dt", handlep
->bus_index
);
637 handle
->fd
= open(full_path
, O_RDONLY
, 0);
643 * Not found at the new location; try
646 pcap_snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%dt", handlep
->bus_index
);
647 handle
->fd
= open(full_path
, O_RDONLY
, 0);
649 if (handle
->fd
< 0) {
651 * Is the problem that we didn't have
652 * sufficient permission to open it?
654 if (errno
== EACCES
) {
656 * Yes - return that error.
658 return PCAP_ERROR_PERM_DENIED
;
662 * No - was the problem something other
663 * than "it doesn't exist"?
665 if (errno
!= ENOENT
) {
667 * Yes - return *that* error.
669 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
670 PCAP_ERRBUF_SIZE
, errno
,
671 "Can't open USB bus file %s",
677 * No. Report that as "no such device".
678 * (That could mean "no such USB bus"
679 * or "monitoring not supported".)
681 return PCAP_ERROR_NO_SUCH_DEVICE
;
685 if (handle
->opt
.rfmon
)
688 * Monitor mode doesn't apply to USB devices.
691 return PCAP_ERROR_RFMON_NOTSUP
;
694 handle
->stats_op
= usb_stats_linux
;
695 handle
->read_op
= usb_read_linux
;
699 * "handle->fd" is a real file, so "select()" and "poll()"
702 handle
->selectable_fd
= handle
->fd
;
704 /* for plain binary access and text access we need to allocate the read
706 handle
->buffer
= malloc(handle
->bufsize
);
707 if (!handle
->buffer
) {
708 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
719 return c
< 'A' ? c
- '0': ((c
<'a') ? c
- 'A' + 10: c
-'a'+10);
723 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
724 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
728 usb_read_linux(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
731 * /usr/src/linux/Documentation/usb/usbmon.txt
734 struct pcap_usb_linux
*handlep
= handle
->priv
;
736 int tag
, cnt
, ep_num
, dev_addr
, dummy
, ret
, urb_len
, data_len
;
738 char etype
, pipeid1
, pipeid2
, status
[16], urb_tag
, line
[USB_LINE_LEN
];
740 u_char
* rawdata
= handle
->buffer
;
741 struct pcap_pkthdr pkth
;
742 pcap_usb_header
* uhdr
= (pcap_usb_header
*)handle
->buffer
;
743 u_char urb_transfer
=0;
746 /* ignore interrupt system call errors */
748 read_ret
= read(handle
->fd
, line
, USB_LINE_LEN
- 1);
749 if (handle
->break_loop
)
751 handle
->break_loop
= 0;
754 } while ((read_ret
== -1) && (errno
== EINTR
));
758 return 0; /* no data there */
760 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
761 errno
, "Can't read from fd %d", handle
->fd
);
765 /* read urb header; %n argument may increment return value, but it's
766 * not mandatory, so does not count on it*/
767 string
[read_ret
] = 0;
768 ret
= sscanf(string
, "%x %d %c %c%c:%d:%d %s%n", &tag
, ×tamp
, &etype
,
769 &pipeid1
, &pipeid2
, &dev_addr
, &ep_num
, status
,
773 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
774 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
779 uhdr
->device_address
= dev_addr
;
780 uhdr
->bus_id
= handlep
->bus_index
;
784 /* don't use usbmon provided timestamp, since it have low precision*/
785 if (gettimeofday(&pkth
.ts
, NULL
) < 0)
787 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
788 errno
, "Can't get timestamp for message '%s'", string
);
791 uhdr
->ts_sec
= pkth
.ts
.tv_sec
;
792 uhdr
->ts_usec
= (int32_t)pkth
.ts
.tv_usec
;
794 /* parse endpoint information */
796 urb_transfer
= URB_CONTROL
;
797 else if (pipeid1
== 'Z')
798 urb_transfer
= URB_ISOCHRONOUS
;
799 else if (pipeid1
== 'I')
800 urb_transfer
= URB_INTERRUPT
;
801 else if (pipeid1
== 'B')
802 urb_transfer
= URB_BULK
;
803 if (pipeid2
== 'i') {
804 ep_num
|= URB_TRANSFER_IN
;
808 incoming
= !incoming
;
813 if (handle
->direction
== PCAP_D_OUT
)
817 if (handle
->direction
== PCAP_D_IN
)
819 uhdr
->event_type
= etype
;
820 uhdr
->transfer_type
= urb_transfer
;
821 uhdr
->endpoint_number
= ep_num
;
822 pkth
.caplen
= sizeof(pcap_usb_header
);
823 rawdata
+= sizeof(pcap_usb_header
);
825 /* check if this is a setup packet */
826 ret
= sscanf(status
, "%d", &dummy
);
829 /* this a setup packet, setup data can be filled with underscore if
830 * usbmon has not been able to read them, so we must parse this fields as
832 pcap_usb_setup
* shdr
;
833 char str1
[3], str2
[3], str3
[5], str4
[5], str5
[5];
834 ret
= sscanf(string
, "%s %s %s %s %s%n", str1
, str2
, str3
, str4
,
838 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
839 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
845 /* try to convert to corresponding integer */
847 shdr
->bmRequestType
= strtoul(str1
, 0, 16);
848 shdr
->bRequest
= strtoul(str2
, 0, 16);
849 shdr
->wValue
= htols(strtoul(str3
, 0, 16));
850 shdr
->wIndex
= htols(strtoul(str4
, 0, 16));
851 shdr
->wLength
= htols(strtoul(str5
, 0, 16));
853 uhdr
->setup_flag
= 0;
856 uhdr
->setup_flag
= 1;
859 ret
= sscanf(string
, " %d%n", &urb_len
, &cnt
);
862 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
863 "Can't parse urb length from '%s'", string
);
868 /* urb tag is not present if urb length is 0, so we can stop here
870 pkth
.len
= urb_len
+pkth
.caplen
;
871 uhdr
->urb_len
= urb_len
;
874 if (uhdr
->urb_len
== 0)
877 /* check for data presence; data is present if and only if urb tag is '=' */
878 if (sscanf(string
, " %c", &urb_tag
) != 1)
880 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
881 "Can't parse urb tag from '%s'", string
);
888 /* skip urb tag and following space */
891 /* if we reach this point we got some urb data*/
894 /* read all urb data; if urb length is greater then the usbmon internal
895 * buffer length used by the kernel to spool the URB, we get only
896 * a partial information.
897 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
898 * length and default value is 130. */
899 while ((string
[0] != 0) && (string
[1] != 0) && (pkth
.caplen
< (bpf_u_int32
)handle
->snapshot
))
901 rawdata
[0] = ascii_to_int(string
[0]) * 16 + ascii_to_int(string
[1]);
904 if (string
[0] == ' ')
911 uhdr
->data_len
= data_len
;
912 if (pkth
.caplen
> (bpf_u_int32
)handle
->snapshot
)
913 pkth
.caplen
= (bpf_u_int32
)handle
->snapshot
;
915 if (handle
->fcode
.bf_insns
== NULL
||
916 pcap_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
917 pkth
.len
, pkth
.caplen
)) {
918 handlep
->packets_read
++;
919 callback(user
, &pkth
, handle
->buffer
);
922 return 0; /* didn't pass filter */
926 usb_inject_linux(pcap_t
*handle
, const void *buf _U_
, int size _U_
)
928 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on "
934 usb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
936 struct pcap_usb_linux
*handlep
= handle
->priv
;
938 ssize_t ret
, consumed
;
939 char string
[USB_LINE_LEN
];
940 char token
[USB_LINE_LEN
];
944 pcap_snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR
"/%ds", handlep
->bus_index
);
945 fd
= open(string
, O_RDONLY
, 0);
951 * Not found at the new location; try the old
954 pcap_snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%ds", handlep
->bus_index
);
955 fd
= open(string
, O_RDONLY
, 0);
958 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
959 PCAP_ERRBUF_SIZE
, errno
,
960 "Can't open USB stats file %s", string
);
965 /* read stats line */
967 ret
= read(fd
, string
, USB_LINE_LEN
-1);
968 } while ((ret
== -1) && (errno
== EINTR
));
973 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
974 "Can't read stats from fd %d ", fd
);
979 stats
->ps_recv
= handlep
->packets_read
;
980 stats
->ps_drop
= 0; /* unless we find text_lost */
981 stats
->ps_ifdrop
= 0;
983 /* extract info on dropped urbs */
984 for (consumed
=0; consumed
< ret
; ) {
985 /* from the sscanf man page:
986 * The C standard says: "Execution of a %n directive does
987 * not increment the assignment count returned at the completion
988 * of execution" but the Corrigendum seems to contradict this.
989 * Do not make any assumptions on the effect of %n conversions
990 * on the return value and explicitly check for cnt assignmet*/
994 ntok
= sscanf(ptr
, "%s%n", token
, &cnt
);
995 if ((ntok
< 1) || (cnt
< 0))
999 if (strcmp(token
, "text_lost") == 0)
1000 ntok
= sscanf(ptr
, "%d%n", &stats
->ps_drop
, &cnt
);
1002 ntok
= sscanf(ptr
, "%d%n", &dummy
, &cnt
);
1003 if ((ntok
!= 1) || (cnt
< 0))
1013 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
1021 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
1023 struct pcap_usb_linux
*handlep
= handle
->priv
;
1025 struct mon_bin_stats st
;
1026 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
1029 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1030 errno
, "Can't read stats from fd %d", handle
->fd
);
1034 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
1035 stats
->ps_drop
= st
.dropped
;
1036 stats
->ps_ifdrop
= 0;
1041 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
1042 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
1045 usb_read_linux_bin(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
1047 struct pcap_usb_linux
*handlep
= handle
->priv
;
1048 struct mon_bin_get info
;
1050 struct pcap_pkthdr pkth
;
1051 u_int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
1053 /* the usb header is going to be part of 'packet' data*/
1054 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
1055 info
.data
= (u_char
*)handle
->buffer
+ sizeof(pcap_usb_header
);
1056 info
.data_len
= clen
;
1058 /* ignore interrupt system call errors */
1060 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
1061 if (handle
->break_loop
)
1063 handle
->break_loop
= 0;
1066 } while ((ret
== -1) && (errno
== EINTR
));
1069 if (errno
== EAGAIN
)
1070 return 0; /* no data there */
1072 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1073 errno
, "Can't read from fd %d", handle
->fd
);
1077 /* we can get less that than really captured from kernel, depending on
1078 * snaplen, so adjust header accordingly */
1079 if (info
.hdr
->data_len
< clen
)
1080 clen
= info
.hdr
->data_len
;
1081 info
.hdr
->data_len
= clen
;
1082 pkth
.caplen
= clen
+ sizeof(pcap_usb_header
);
1083 pkth
.len
= info
.hdr
->data_len
+ sizeof(pcap_usb_header
);
1084 pkth
.ts
.tv_sec
= info
.hdr
->ts_sec
;
1085 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
1087 if (handle
->fcode
.bf_insns
== NULL
||
1088 pcap_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
1089 pkth
.len
, pkth
.caplen
)) {
1090 handlep
->packets_read
++;
1091 callback(user
, &pkth
, handle
->buffer
);
1095 return 0; /* didn't pass filter */
1099 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
1100 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
1104 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
1106 struct pcap_usb_linux
*handlep
= handle
->priv
;
1107 struct mon_bin_mfetch fetch
;
1108 int32_t vec
[VEC_SIZE
];
1109 struct pcap_pkthdr pkth
;
1110 pcap_usb_header
* hdr
;
1113 u_int clen
, max_clen
;
1115 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
1119 int limit
= max_packets
- packets
;
1122 if (limit
> VEC_SIZE
)
1125 /* try to fetch as many events as possible*/
1127 fetch
.nfetch
= limit
;
1128 fetch
.nflush
= nflush
;
1129 /* ignore interrupt system call errors */
1131 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
1132 if (handle
->break_loop
)
1134 handle
->break_loop
= 0;
1137 } while ((ret
== -1) && (errno
== EINTR
));
1140 if (errno
== EAGAIN
)
1141 return 0; /* no data there */
1143 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
1144 PCAP_ERRBUF_SIZE
, errno
, "Can't mfetch fd %d",
1149 /* keep track of processed events, we will flush them later */
1150 nflush
= fetch
.nfetch
;
1151 for (i
=0; i
<fetch
.nfetch
; ++i
) {
1152 /* discard filler */
1153 hdr
= (pcap_usb_header
*) &handlep
->mmapbuf
[vec
[i
]];
1154 if (hdr
->event_type
== '@')
1157 /* we can get less that than really captured from kernel, depending on
1158 * snaplen, so adjust header accordingly */
1160 if (hdr
->data_len
< clen
)
1161 clen
= hdr
->data_len
;
1163 /* get packet info from header*/
1164 pkth
.caplen
= clen
+ sizeof(pcap_usb_header_mmapped
);
1165 pkth
.len
= hdr
->data_len
+ sizeof(pcap_usb_header_mmapped
);
1166 pkth
.ts
.tv_sec
= hdr
->ts_sec
;
1167 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
1169 if (handle
->fcode
.bf_insns
== NULL
||
1170 pcap_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
1171 pkth
.len
, pkth
.caplen
)) {
1172 handlep
->packets_read
++;
1173 callback(user
, &pkth
, (u_char
*) hdr
);
1178 /* with max_packets specifying "unlimited" we stop afer the first chunk*/
1179 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) || (packets
== max_packets
))
1183 /* flush pending events*/
1184 if (ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
) == -1) {
1185 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1186 errno
, "Can't mflush fd %d", handle
->fd
);
1193 usb_cleanup_linux_mmap(pcap_t
* handle
)
1195 struct pcap_usb_linux
*handlep
= handle
->priv
;
1197 /* if we have a memory-mapped buffer, unmap it */
1198 if (handlep
->mmapbuf
!= NULL
) {
1199 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
1200 handlep
->mmapbuf
= NULL
;
1202 pcap_cleanup_live_common(handle
);