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 * XXX - is there any notion of "up" and "running"?
238 * As this refers to all buses, there's no notion of
239 * "connected" vs. "disconnected", as that's a property
240 * that would apply to a particular USB interface.
242 if (add_dev(devlistp
, dev_name
,
243 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
,
244 "All USB buses", err_str
) == NULL
)
248 * XXX - is there a way to determine whether anything's
249 * plugged into this bus interface or not, and set
250 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
251 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
253 pcap_snprintf(dev_descr
, 30, "USB bus number %d", n
);
254 if (add_dev(devlistp
, dev_name
, 0, dev_descr
, err_str
) == NULL
)
262 usb_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
264 char usb_mon_dir
[PATH_MAX
];
265 char *usb_mon_prefix
;
266 size_t usb_mon_prefix_len
;
274 if (have_binary_usbmon()) {
276 * We have binary-mode support.
277 * What do the device names look like?
278 * Split LINUX_USB_MON_DEV into a directory that we'll
279 * scan and a file name prefix that we'll check for.
281 strlcpy(usb_mon_dir
, LINUX_USB_MON_DEV
, sizeof usb_mon_dir
);
282 usb_mon_prefix
= strrchr(usb_mon_dir
, '/');
283 if (usb_mon_prefix
== NULL
) {
285 * This "shouldn't happen". Just give up if it
290 *usb_mon_prefix
++ = '\0';
291 usb_mon_prefix_len
= strlen(usb_mon_prefix
);
294 * Open the directory and scan it.
296 dir
= opendir(usb_mon_dir
);
298 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
302 * Is this a usbmon device?
304 if (strncmp(name
, usb_mon_prefix
, usb_mon_prefix_len
) != 0)
308 * What's the device number?
310 if (sscanf(&name
[usb_mon_prefix_len
], "%d", &n
) == 0)
311 continue; /* failed */
313 ret
= usb_dev_add(devlistp
, n
, err_str
);
321 * We have only text mode support.
322 * We don't look for the text devices because we can't
323 * look for them without root privileges, and we don't
324 * want to require root privileges to enumerate devices
325 * (we want to let the user to try a device and get
326 * an error, rather than seeing no devices and asking
327 * "why am I not seeing devices" and forcing a long
328 * process of poking to figure out whether it's "no
329 * privileges" or "your kernel is too old" or "the
330 * usbmon module isn't loaded" or...).
332 * Instead, we look to see what buses we have.
333 * If the kernel is so old that it doesn't have
334 * binary-mode support, it's also so old that
335 * it doesn't have a "scan all buses" device.
337 * First, try scanning sysfs USB bus directory.
339 dir
= opendir(SYS_USB_BUS_DIR
);
341 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
344 if (strncmp(name
, "usb", 3) != 0)
347 if (sscanf(&name
[3], "%d", &n
) == 0)
350 ret
= usb_dev_add(devlistp
, n
, err_str
);
357 /* That didn't work; try scanning procfs USB bus directory. */
358 dir
= opendir(PROC_USB_BUS_DIR
);
360 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
364 /* if this file name does not end with a number it's not of our interest */
365 if ((len
< 1) || !isdigit(name
[--len
]))
367 while (isdigit(name
[--len
]));
368 if (sscanf(&name
[len
+1], "%d", &n
) != 1)
371 ret
= usb_dev_add(devlistp
, n
, err_str
);
378 /* neither of them worked */
384 int usb_mmap(pcap_t
* handle
)
386 struct pcap_usb_linux
*handlep
= handle
->priv
;
387 int len
= ioctl(handle
->fd
, MON_IOCQ_RING_SIZE
);
391 handlep
->mmapbuflen
= len
;
392 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
393 MAP_SHARED
, handle
->fd
, 0);
394 return handlep
->mmapbuf
!= MAP_FAILED
;
397 #ifdef HAVE_LINUX_USBDEVICE_FS_H
399 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
401 #define USB_DIR_IN 0x80
402 #define USB_TYPE_STANDARD 0x00
403 #define USB_RECIP_DEVICE 0x00
405 #define USB_REQ_GET_DESCRIPTOR 6
407 #define USB_DT_DEVICE 1
409 /* probe the descriptors of the devices attached to the bus */
410 /* the descriptors will end up in the captured packet stream */
411 /* and be decoded by external apps like wireshark */
412 /* without these identifying probes packet data can't be fully decoded */
414 probe_devices(int bus
)
416 struct usbdevfs_ctrltransfer ctrl
;
419 char buf
[sizeof("/dev/bus/usb/000/") + NAME_MAX
];
422 /* scan usb bus directories for device nodes */
423 pcap_snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d", bus
);
428 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
430 char* name
= data
->d_name
;
435 pcap_snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
437 fd
= open(buf
, O_RDWR
);
442 * Sigh. Different kernels have different member names
443 * for this structure.
445 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
446 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
447 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
448 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
450 ctrl
.wLength
= sizeof(buf
);
452 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
453 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
454 ctrl
.value
= USB_DT_DEVICE
<< 8;
456 ctrl
.length
= sizeof(buf
);
459 ctrl
.timeout
= CTRL_TIMEOUT
;
461 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
467 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
470 usb_create(const char *device
, char *ebuf
, int *is_ours
)
477 /* Does this look like a USB monitoring device? */
478 cp
= strrchr(device
, '/');
481 /* Does it begin with USB_IFACE? */
482 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
483 /* Nope, doesn't begin with USB_IFACE */
487 /* Yes - is USB_IFACE followed by a number? */
488 cp
+= sizeof USB_IFACE
- 1;
489 devnum
= strtol(cp
, &cpend
, 10);
490 if (cpend
== cp
|| *cpend
!= '\0') {
491 /* Not followed by a number. */
496 /* Followed by a non-valid number. */
501 /* OK, it's probably ours. */
504 p
= pcap_create_common(ebuf
, sizeof (struct pcap_usb_linux
));
508 p
->activate_op
= usb_activate
;
513 usb_activate(pcap_t
* handle
)
515 struct pcap_usb_linux
*handlep
= handle
->priv
;
516 char full_path
[USB_LINE_LEN
];
519 * Turn a negative snapshot value (invalid), a snapshot value of
520 * 0 (unspecified), or a value bigger than the normal maximum
521 * value, into the maximum allowed value.
523 * If some application really *needs* a bigger snapshot
524 * length, we should just increase MAXIMUM_SNAPLEN.
526 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
527 handle
->snapshot
= MAXIMUM_SNAPLEN
;
529 /* Initialize some components of the pcap structure. */
530 handle
->bufsize
= handle
->snapshot
;
532 handle
->linktype
= DLT_USB_LINUX
;
534 handle
->inject_op
= usb_inject_linux
;
535 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
536 handle
->setdirection_op
= usb_setdirection_linux
;
537 handle
->set_datalink_op
= NULL
; /* can't change data link type */
538 handle
->getnonblock_op
= pcap_getnonblock_fd
;
539 handle
->setnonblock_op
= pcap_setnonblock_fd
;
541 /*get usb bus index from device name */
542 if (sscanf(handle
->opt
.device
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
544 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
545 "Can't get USB bus index from %s", handle
->opt
.device
);
549 if (have_binary_usbmon())
552 * We have binary-mode support.
553 * Try to open the binary interface.
555 pcap_snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handlep
->bus_index
);
556 handle
->fd
= open(full_path
, O_RDONLY
, 0);
560 * The attempt failed; why?
566 * The device doesn't exist.
567 * That could either mean that there's
568 * no support for monitoring USB buses
569 * (which probably means "the usbmon
570 * module isn't loaded") or that there
571 * is but that *particular* device
572 * doesn't exist (no "scan all buses"
573 * device if the bus index is 0, no
574 * such bus if the bus index isn't 0).
576 return PCAP_ERROR_NO_SUCH_DEVICE
;
580 * We didn't have permission to open it.
582 return PCAP_ERROR_PERM_DENIED
;
586 * Something went wrong.
588 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
589 PCAP_ERRBUF_SIZE
, errno
,
590 "Can't open USB bus file %s", full_path
);
595 if (handle
->opt
.rfmon
)
598 * Monitor mode doesn't apply to USB devices.
601 return PCAP_ERROR_RFMON_NOTSUP
;
604 /* try to use fast mmap access */
605 if (usb_mmap(handle
))
607 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
608 handle
->stats_op
= usb_stats_linux_bin
;
609 handle
->read_op
= usb_read_linux_mmap
;
610 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
611 #ifdef HAVE_LINUX_USBDEVICE_FS_H
612 probe_devices(handlep
->bus_index
);
616 * "handle->fd" is a real file, so
617 * "select()" and "poll()" work on it.
619 handle
->selectable_fd
= handle
->fd
;
623 /* can't mmap, use plain binary interface access */
624 handle
->stats_op
= usb_stats_linux_bin
;
625 handle
->read_op
= usb_read_linux_bin
;
626 #ifdef HAVE_LINUX_USBDEVICE_FS_H
627 probe_devices(handlep
->bus_index
);
632 * We don't have binary mode support.
633 * Try opening the text-mode device.
635 pcap_snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR
"/%dt", handlep
->bus_index
);
636 handle
->fd
= open(full_path
, O_RDONLY
, 0);
642 * Not found at the new location; try
645 pcap_snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%dt", handlep
->bus_index
);
646 handle
->fd
= open(full_path
, O_RDONLY
, 0);
648 if (handle
->fd
< 0) {
650 * Is the problem that we didn't have
651 * sufficient permission to open it?
653 if (errno
== EACCES
) {
655 * Yes - return that error.
657 return PCAP_ERROR_PERM_DENIED
;
661 * No - was the problem something other
662 * than "it doesn't exist"?
664 if (errno
!= ENOENT
) {
666 * Yes - return *that* error.
668 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
669 PCAP_ERRBUF_SIZE
, errno
,
670 "Can't open USB bus file %s",
676 * No. Report that as "no such device".
677 * (That could mean "no such USB bus"
678 * or "monitoring not supported".)
680 return PCAP_ERROR_NO_SUCH_DEVICE
;
684 if (handle
->opt
.rfmon
)
687 * Monitor mode doesn't apply to USB devices.
690 return PCAP_ERROR_RFMON_NOTSUP
;
693 handle
->stats_op
= usb_stats_linux
;
694 handle
->read_op
= usb_read_linux
;
698 * "handle->fd" is a real file, so "select()" and "poll()"
701 handle
->selectable_fd
= handle
->fd
;
703 /* for plain binary access and text access we need to allocate the read
705 handle
->buffer
= malloc(handle
->bufsize
);
706 if (!handle
->buffer
) {
707 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
718 return c
< 'A' ? c
- '0': ((c
<'a') ? c
- 'A' + 10: c
-'a'+10);
722 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
723 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
727 usb_read_linux(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
730 * /usr/src/linux/Documentation/usb/usbmon.txt
733 struct pcap_usb_linux
*handlep
= handle
->priv
;
735 int tag
, cnt
, ep_num
, dev_addr
, dummy
, ret
, urb_len
, data_len
;
736 char etype
, pipeid1
, pipeid2
, status
[16], urb_tag
, line
[USB_LINE_LEN
];
738 u_char
* rawdata
= handle
->buffer
;
739 struct pcap_pkthdr pkth
;
740 pcap_usb_header
* uhdr
= (pcap_usb_header
*)handle
->buffer
;
741 u_char urb_transfer
=0;
744 /* ignore interrupt system call errors */
746 ret
= read(handle
->fd
, line
, USB_LINE_LEN
- 1);
747 if (handle
->break_loop
)
749 handle
->break_loop
= 0;
752 } while ((ret
== -1) && (errno
== EINTR
));
756 return 0; /* no data there */
758 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
759 errno
, "Can't read from fd %d", handle
->fd
);
763 /* read urb header; %n argument may increment return value, but it's
764 * not mandatory, so does not count on it*/
766 ret
= sscanf(string
, "%x %d %c %c%c:%d:%d %s%n", &tag
, ×tamp
, &etype
,
767 &pipeid1
, &pipeid2
, &dev_addr
, &ep_num
, status
,
771 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
772 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
777 uhdr
->device_address
= dev_addr
;
778 uhdr
->bus_id
= handlep
->bus_index
;
782 /* don't use usbmon provided timestamp, since it have low precision*/
783 if (gettimeofday(&pkth
.ts
, NULL
) < 0)
785 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
786 errno
, "Can't get timestamp for message '%s'", string
);
789 uhdr
->ts_sec
= pkth
.ts
.tv_sec
;
790 uhdr
->ts_usec
= pkth
.ts
.tv_usec
;
792 /* parse endpoint information */
794 urb_transfer
= URB_CONTROL
;
795 else if (pipeid1
== 'Z')
796 urb_transfer
= URB_ISOCHRONOUS
;
797 else if (pipeid1
== 'I')
798 urb_transfer
= URB_INTERRUPT
;
799 else if (pipeid1
== 'B')
800 urb_transfer
= URB_BULK
;
801 if (pipeid2
== 'i') {
802 ep_num
|= URB_TRANSFER_IN
;
806 incoming
= !incoming
;
811 if (handle
->direction
== PCAP_D_OUT
)
815 if (handle
->direction
== PCAP_D_IN
)
817 uhdr
->event_type
= etype
;
818 uhdr
->transfer_type
= urb_transfer
;
819 uhdr
->endpoint_number
= ep_num
;
820 pkth
.caplen
= sizeof(pcap_usb_header
);
821 rawdata
+= sizeof(pcap_usb_header
);
823 /* check if this is a setup packet */
824 ret
= sscanf(status
, "%d", &dummy
);
827 /* this a setup packet, setup data can be filled with underscore if
828 * usbmon has not been able to read them, so we must parse this fields as
830 pcap_usb_setup
* shdr
;
831 char str1
[3], str2
[3], str3
[5], str4
[5], str5
[5];
832 ret
= sscanf(string
, "%s %s %s %s %s%n", str1
, str2
, str3
, str4
,
836 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
837 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
843 /* try to convert to corresponding integer */
845 shdr
->bmRequestType
= strtoul(str1
, 0, 16);
846 shdr
->bRequest
= strtoul(str2
, 0, 16);
847 shdr
->wValue
= htols(strtoul(str3
, 0, 16));
848 shdr
->wIndex
= htols(strtoul(str4
, 0, 16));
849 shdr
->wLength
= htols(strtoul(str5
, 0, 16));
851 uhdr
->setup_flag
= 0;
854 uhdr
->setup_flag
= 1;
857 ret
= sscanf(string
, " %d%n", &urb_len
, &cnt
);
860 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
861 "Can't parse urb length from '%s'", string
);
866 /* urb tag is not present if urb length is 0, so we can stop here
868 pkth
.len
= urb_len
+pkth
.caplen
;
869 uhdr
->urb_len
= urb_len
;
872 if (uhdr
->urb_len
== 0)
875 /* check for data presence; data is present if and only if urb tag is '=' */
876 if (sscanf(string
, " %c", &urb_tag
) != 1)
878 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
879 "Can't parse urb tag from '%s'", string
);
886 /* skip urb tag and following space */
889 /* if we reach this point we got some urb data*/
892 /* read all urb data; if urb length is greater then the usbmon internal
893 * buffer length used by the kernel to spool the URB, we get only
894 * a partial information.
895 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
896 * length and default value is 130. */
897 while ((string
[0] != 0) && (string
[1] != 0) && (pkth
.caplen
< (bpf_u_int32
)handle
->snapshot
))
899 rawdata
[0] = ascii_to_int(string
[0]) * 16 + ascii_to_int(string
[1]);
902 if (string
[0] == ' ')
909 uhdr
->data_len
= data_len
;
910 if (pkth
.caplen
> (bpf_u_int32
)handle
->snapshot
)
911 pkth
.caplen
= (bpf_u_int32
)handle
->snapshot
;
913 if (handle
->fcode
.bf_insns
== NULL
||
914 bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
915 pkth
.len
, pkth
.caplen
)) {
916 handlep
->packets_read
++;
917 callback(user
, &pkth
, handle
->buffer
);
920 return 0; /* didn't pass filter */
924 usb_inject_linux(pcap_t
*handle
, const void *buf _U_
, size_t size _U_
)
926 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on "
932 usb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
934 struct pcap_usb_linux
*handlep
= handle
->priv
;
935 int dummy
, ret
, consumed
, cnt
;
936 char string
[USB_LINE_LEN
];
937 char token
[USB_LINE_LEN
];
941 pcap_snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR
"/%ds", handlep
->bus_index
);
942 fd
= open(string
, O_RDONLY
, 0);
948 * Not found at the new location; try the old
951 pcap_snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%ds", handlep
->bus_index
);
952 fd
= open(string
, O_RDONLY
, 0);
955 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
956 PCAP_ERRBUF_SIZE
, errno
,
957 "Can't open USB stats file %s", string
);
962 /* read stats line */
964 ret
= read(fd
, string
, USB_LINE_LEN
-1);
965 } while ((ret
== -1) && (errno
== EINTR
));
970 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
971 "Can't read stats from fd %d ", fd
);
976 /* extract info on dropped urbs */
977 for (consumed
=0; consumed
< ret
; ) {
978 /* from the sscanf man page:
979 * The C standard says: "Execution of a %n directive does
980 * not increment the assignment count returned at the completion
981 * of execution" but the Corrigendum seems to contradict this.
982 * Do not make any assumptions on the effect of %n conversions
983 * on the return value and explicitly check for cnt assignmet*/
987 ntok
= sscanf(ptr
, "%s%n", token
, &cnt
);
988 if ((ntok
< 1) || (cnt
< 0))
992 if (strcmp(token
, "nreaders") == 0)
993 ret
= sscanf(ptr
, "%d", &stats
->ps_drop
);
995 ret
= sscanf(ptr
, "%d", &dummy
);
1002 stats
->ps_recv
= handlep
->packets_read
;
1003 stats
->ps_ifdrop
= 0;
1008 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
1016 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
1018 struct pcap_usb_linux
*handlep
= handle
->priv
;
1020 struct mon_bin_stats st
;
1021 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
1024 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1025 errno
, "Can't read stats from fd %d", handle
->fd
);
1029 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
1030 stats
->ps_drop
= st
.dropped
;
1031 stats
->ps_ifdrop
= 0;
1036 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
1037 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
1040 usb_read_linux_bin(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
1042 struct pcap_usb_linux
*handlep
= handle
->priv
;
1043 struct mon_bin_get info
;
1045 struct pcap_pkthdr pkth
;
1046 u_int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
1048 /* the usb header is going to be part of 'packet' data*/
1049 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
1050 info
.data
= (u_char
*)handle
->buffer
+ sizeof(pcap_usb_header
);
1051 info
.data_len
= clen
;
1053 /* ignore interrupt system call errors */
1055 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
1056 if (handle
->break_loop
)
1058 handle
->break_loop
= 0;
1061 } while ((ret
== -1) && (errno
== EINTR
));
1064 if (errno
== EAGAIN
)
1065 return 0; /* no data there */
1067 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1068 errno
, "Can't read from fd %d", handle
->fd
);
1072 /* we can get less that than really captured from kernel, depending on
1073 * snaplen, so adjust header accordingly */
1074 if (info
.hdr
->data_len
< clen
)
1075 clen
= info
.hdr
->data_len
;
1076 info
.hdr
->data_len
= clen
;
1077 pkth
.caplen
= clen
+ sizeof(pcap_usb_header
);
1078 pkth
.len
= info
.hdr
->data_len
+ sizeof(pcap_usb_header
);
1079 pkth
.ts
.tv_sec
= info
.hdr
->ts_sec
;
1080 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
1082 if (handle
->fcode
.bf_insns
== NULL
||
1083 bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
1084 pkth
.len
, pkth
.caplen
)) {
1085 handlep
->packets_read
++;
1086 callback(user
, &pkth
, handle
->buffer
);
1090 return 0; /* didn't pass filter */
1094 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
1095 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
1099 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
1101 struct pcap_usb_linux
*handlep
= handle
->priv
;
1102 struct mon_bin_mfetch fetch
;
1103 int32_t vec
[VEC_SIZE
];
1104 struct pcap_pkthdr pkth
;
1105 pcap_usb_header
* hdr
;
1108 u_int clen
, max_clen
;
1110 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
1114 int limit
= max_packets
- packets
;
1117 if (limit
> VEC_SIZE
)
1120 /* try to fetch as many events as possible*/
1122 fetch
.nfetch
= limit
;
1123 fetch
.nflush
= nflush
;
1124 /* ignore interrupt system call errors */
1126 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
1127 if (handle
->break_loop
)
1129 handle
->break_loop
= 0;
1132 } while ((ret
== -1) && (errno
== EINTR
));
1135 if (errno
== EAGAIN
)
1136 return 0; /* no data there */
1138 pcap_fmt_errmsg_for_errno(handle
->errbuf
,
1139 PCAP_ERRBUF_SIZE
, errno
, "Can't mfetch fd %d",
1144 /* keep track of processed events, we will flush them later */
1145 nflush
= fetch
.nfetch
;
1146 for (i
=0; i
<fetch
.nfetch
; ++i
) {
1147 /* discard filler */
1148 hdr
= (pcap_usb_header
*) &handlep
->mmapbuf
[vec
[i
]];
1149 if (hdr
->event_type
== '@')
1152 /* we can get less that than really captured from kernel, depending on
1153 * snaplen, so adjust header accordingly */
1155 if (hdr
->data_len
< clen
)
1156 clen
= hdr
->data_len
;
1158 /* get packet info from header*/
1159 pkth
.caplen
= clen
+ sizeof(pcap_usb_header_mmapped
);
1160 pkth
.len
= hdr
->data_len
+ sizeof(pcap_usb_header_mmapped
);
1161 pkth
.ts
.tv_sec
= hdr
->ts_sec
;
1162 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
1164 if (handle
->fcode
.bf_insns
== NULL
||
1165 bpf_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
1166 pkth
.len
, pkth
.caplen
)) {
1167 handlep
->packets_read
++;
1168 callback(user
, &pkth
, (u_char
*) hdr
);
1173 /* with max_packets specifying "unlimited" we stop afer the first chunk*/
1174 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) || (packets
== max_packets
))
1178 /* flush pending events*/
1179 if (ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
) == -1) {
1180 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
1181 errno
, "Can't mflush fd %d", handle
->fd
);
1188 usb_cleanup_linux_mmap(pcap_t
* handle
)
1190 struct pcap_usb_linux
*handlep
= handle
->priv
;
1192 /* if we have a memory-mapped buffer, unmap it */
1193 if (handlep
->mmapbuf
!= NULL
) {
1194 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
1195 handlep
->mmapbuf
= NULL
;
1197 pcap_cleanup_live_common(handle
);