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>
36 static const char rcsid
[] _U_
=
37 "@(#) $Header: /tcpdump/master/libpcap/pcap-usb-linux.c,v 1.33 2008-12-23 21:38:50 guy Exp $ (LBL)";
45 #include "pcap-usb-linux.h"
48 #ifdef NEED_STRERROR_H
60 #include <netinet/in.h>
61 #include <sys/ioctl.h>
63 #ifdef HAVE_LINUX_USBDEVICE_FS_H
65 * We might need <linux/compiler.h> to define __user for
66 * <linux/usbdevice_fs.h>.
68 #ifdef HAVE_LINUX_COMPILER_H
69 #include <linux/compiler.h>
70 #endif /* HAVE_LINUX_COMPILER_H */
71 #include <linux/usbdevice_fs.h>
72 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
74 #define USB_IFACE "usbmon"
75 #define USB_TEXT_DIR_OLD "/sys/kernel/debug/usbmon"
76 #define USB_TEXT_DIR "/sys/kernel/debug/usb/usbmon"
77 #define SYS_USB_BUS_DIR "/sys/bus/usb/devices"
78 #define PROC_USB_BUS_DIR "/proc/bus/usb"
79 #define USB_LINE_LEN 4096
81 #if __BYTE_ORDER == __LITTLE_ENDIAN
86 #define htols(s) bswap_16(s)
87 #define htoll(l) bswap_32(l)
88 #define htol64(ll) bswap_64(ll)
91 struct mon_bin_stats
{
99 size_t data_len
; /* Length of data (can be zero) */
102 struct mon_bin_mfetch
{
103 int32_t *offvec
; /* Vector of events fetched */
104 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
105 int32_t nflush
; /* Number of events to flush */
108 #define MON_IOC_MAGIC 0x92
110 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
111 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
112 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
113 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
114 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
115 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
116 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
117 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
119 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
120 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
121 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
122 #define MON_BIN_ERROR 0x8
125 * Private data for capturing on Linux USB.
127 struct pcap_usb_linux
{
128 u_char
*mmapbuf
; /* memory-mapped region pointer */
129 size_t mmapbuflen
; /* size of region */
134 /* forward declaration */
135 static int usb_activate(pcap_t
*);
136 static int usb_stats_linux(pcap_t
*, struct pcap_stat
*);
137 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
138 static int usb_read_linux(pcap_t
*, int , pcap_handler
, u_char
*);
139 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
140 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
141 static int usb_inject_linux(pcap_t
*, const void *, size_t);
142 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
143 static void usb_cleanup_linux_mmap(pcap_t
*);
145 /* facility to add an USB device to the device list*/
147 usb_dev_add(pcap_if_t
** alldevsp
, int n
, char *err_str
)
151 snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
152 snprintf(dev_descr
, 30, "USB bus number %d", n
);
154 if (pcap_add_if(alldevsp
, dev_name
, 0,
155 dev_descr
, err_str
) < 0)
161 usb_findalldevs(pcap_if_t
**alldevsp
, char *err_str
)
170 /* try scanning sysfs usb bus directory */
171 dir
= opendir(SYS_USB_BUS_DIR
);
173 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
176 if (strncmp(name
, "usb", 3) != 0)
179 if (sscanf(&name
[3], "%d", &n
) == 0)
182 ret
= usb_dev_add(alldevsp
, n
, err_str
);
189 /* that didn't work; try scanning procfs usb bus directory */
190 dir
= opendir(PROC_USB_BUS_DIR
);
192 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
196 /* if this file name does not end with a number it's not of our interest */
197 if ((len
< 1) || !isdigit(name
[--len
]))
199 while (isdigit(name
[--len
]));
200 if (sscanf(&name
[len
+1], "%d", &n
) != 1)
203 ret
= usb_dev_add(alldevsp
, n
, err_str
);
210 /* neither of them worked */
215 int usb_mmap(pcap_t
* handle
)
217 struct pcap_usb_linux
*handlep
= handle
->priv
;
218 int len
= ioctl(handle
->fd
, MON_IOCQ_RING_SIZE
);
222 handlep
->mmapbuflen
= len
;
223 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
224 MAP_SHARED
, handle
->fd
, 0);
225 return handlep
->mmapbuf
!= MAP_FAILED
;
228 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
230 #define USB_DIR_IN 0x80
231 #define USB_TYPE_STANDARD 0x00
232 #define USB_RECIP_DEVICE 0x00
234 #define USB_REQ_GET_DESCRIPTOR 6
236 #define USB_DT_DEVICE 1
238 /* probe the descriptors of the devices attached to the bus */
239 /* the descriptors will end up in the captured packet stream */
240 /* and be decoded by external apps like wireshark */
241 /* without these identifying probes packet data can't be fully decoded */
243 probe_devices(int bus
)
245 struct usbdevfs_ctrltransfer ctrl
;
251 /* scan usb bus directories for device nodes */
252 snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d", bus
);
257 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
259 char* name
= data
->d_name
;
264 snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
266 fd
= open(buf
, O_RDWR
);
271 * Sigh. Different kernels have different member names
272 * for this structure.
274 #ifdef HAVE_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
275 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
276 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
277 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
279 ctrl
.wLength
= sizeof(buf
);
281 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
282 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
283 ctrl
.value
= USB_DT_DEVICE
<< 8;
285 ctrl
.length
= sizeof(buf
);
288 ctrl
.timeout
= CTRL_TIMEOUT
;
290 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
298 usb_create(const char *device
, char *ebuf
, int *is_ours
)
305 /* Does this look like a USB monitoring device? */
306 cp
= strrchr(device
, '/');
309 /* Does it begin with USB_IFACE? */
310 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
311 /* Nope, doesn't begin with USB_IFACE */
315 /* Yes - is USB_IFACE followed by a number? */
316 cp
+= sizeof USB_IFACE
- 1;
317 devnum
= strtol(cp
, &cpend
, 10);
318 if (cpend
== cp
|| *cpend
!= '\0') {
319 /* Not followed by a number. */
324 /* Followed by a non-valid number. */
329 /* OK, it's probably ours. */
332 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_usb_linux
));
336 p
->activate_op
= usb_activate
;
341 usb_activate(pcap_t
* handle
)
343 struct pcap_usb_linux
*handlep
= handle
->priv
;
344 char full_path
[USB_LINE_LEN
];
346 /* Initialize some components of the pcap structure. */
347 handle
->bufsize
= handle
->snapshot
;
349 handle
->linktype
= DLT_USB_LINUX
;
351 handle
->inject_op
= usb_inject_linux
;
352 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
353 handle
->setdirection_op
= usb_setdirection_linux
;
354 handle
->set_datalink_op
= NULL
; /* can't change data link type */
355 handle
->getnonblock_op
= pcap_getnonblock_fd
;
356 handle
->setnonblock_op
= pcap_setnonblock_fd
;
358 /*get usb bus index from device name */
359 if (sscanf(handle
->opt
.source
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
361 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
362 "Can't get USB bus index from %s", handle
->opt
.source
);
366 /*now select the read method: try to open binary interface */
367 snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handlep
->bus_index
);
368 handle
->fd
= open(full_path
, O_RDONLY
, 0);
371 if (handle
->opt
.rfmon
) {
373 * Monitor mode doesn't apply to USB devices.
376 return PCAP_ERROR_RFMON_NOTSUP
;
379 /* binary api is available, try to use fast mmap access */
380 if (usb_mmap(handle
)) {
381 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
382 handle
->stats_op
= usb_stats_linux_bin
;
383 handle
->read_op
= usb_read_linux_mmap
;
384 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
385 probe_devices(handlep
->bus_index
);
388 * "handle->fd" is a real file, so "select()" and
389 * "poll()" work on it.
391 handle
->selectable_fd
= handle
->fd
;
395 /* can't mmap, use plain binary interface access */
396 handle
->stats_op
= usb_stats_linux_bin
;
397 handle
->read_op
= usb_read_linux_bin
;
398 probe_devices(handlep
->bus_index
);
401 /*Binary interface not available, try open text interface */
402 snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR
"/%dt", handlep
->bus_index
);
403 handle
->fd
= open(full_path
, O_RDONLY
, 0);
409 * Not found at the new location; try
412 snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%dt", handlep
->bus_index
);
413 handle
->fd
= open(full_path
, O_RDONLY
, 0);
415 if (handle
->fd
< 0) {
416 /* no more fallback, give it up*/
417 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
418 "Can't open USB bus file %s: %s", full_path
, strerror(errno
));
423 if (handle
->opt
.rfmon
) {
425 * Monitor mode doesn't apply to USB devices.
428 return PCAP_ERROR_RFMON_NOTSUP
;
431 handle
->stats_op
= usb_stats_linux
;
432 handle
->read_op
= usb_read_linux
;
436 * "handle->fd" is a real file, so "select()" and "poll()"
439 handle
->selectable_fd
= handle
->fd
;
441 /* for plain binary access and text access we need to allocate the read
443 handle
->buffer
= malloc(handle
->bufsize
);
444 if (!handle
->buffer
) {
445 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
446 "malloc: %s", pcap_strerror(errno
));
456 return c
< 'A' ? c
- '0': ((c
<'a') ? c
- 'A' + 10: c
-'a'+10);
460 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
461 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
465 usb_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
468 * /usr/src/linux/Documentation/usb/usbmon.txt
471 struct pcap_usb_linux
*handlep
= handle
->priv
;
473 int tag
, cnt
, ep_num
, dev_addr
, dummy
, ret
, urb_len
, data_len
;
474 char etype
, pipeid1
, pipeid2
, status
[16], urb_tag
, line
[USB_LINE_LEN
];
476 u_char
* rawdata
= handle
->buffer
;
477 struct pcap_pkthdr pkth
;
478 pcap_usb_header
* uhdr
= (pcap_usb_header
*)handle
->buffer
;
479 u_char urb_transfer
=0;
482 /* ignore interrupt system call errors */
484 ret
= read(handle
->fd
, line
, USB_LINE_LEN
- 1);
485 if (handle
->break_loop
)
487 handle
->break_loop
= 0;
490 } while ((ret
== -1) && (errno
== EINTR
));
494 return 0; /* no data there */
496 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
497 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
501 /* read urb header; %n argument may increment return value, but it's
502 * not mandatory, so does not count on it*/
504 ret
= sscanf(string
, "%x %d %c %c%c:%d:%d %s%n", &tag
, ×tamp
, &etype
,
505 &pipeid1
, &pipeid2
, &dev_addr
, &ep_num
, status
,
509 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
510 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
515 uhdr
->device_address
= dev_addr
;
516 uhdr
->bus_id
= handlep
->bus_index
;
520 /* don't use usbmon provided timestamp, since it have low precision*/
521 if (gettimeofday(&pkth
.ts
, NULL
) < 0)
523 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
524 "Can't get timestamp for message '%s' %d:%s",
525 string
, errno
, strerror(errno
));
528 uhdr
->ts_sec
= pkth
.ts
.tv_sec
;
529 uhdr
->ts_usec
= pkth
.ts
.tv_usec
;
531 /* parse endpoint information */
533 urb_transfer
= URB_CONTROL
;
534 else if (pipeid1
== 'Z')
535 urb_transfer
= URB_ISOCHRONOUS
;
536 else if (pipeid1
== 'I')
537 urb_transfer
= URB_INTERRUPT
;
538 else if (pipeid1
== 'B')
539 urb_transfer
= URB_BULK
;
540 if (pipeid2
== 'i') {
541 ep_num
|= URB_TRANSFER_IN
;
545 incoming
= !incoming
;
550 if (handle
->direction
== PCAP_D_OUT
)
554 if (handle
->direction
== PCAP_D_IN
)
556 uhdr
->event_type
= etype
;
557 uhdr
->transfer_type
= urb_transfer
;
558 uhdr
->endpoint_number
= ep_num
;
559 pkth
.caplen
= sizeof(pcap_usb_header
);
560 rawdata
+= sizeof(pcap_usb_header
);
562 /* check if this is a setup packet */
563 ret
= sscanf(status
, "%d", &dummy
);
566 /* this a setup packet, setup data can be filled with underscore if
567 * usbmon has not been able to read them, so we must parse this fields as
569 pcap_usb_setup
* shdr
;
570 char str1
[3], str2
[3], str3
[5], str4
[5], str5
[5];
571 ret
= sscanf(string
, "%s %s %s %s %s%n", str1
, str2
, str3
, str4
,
575 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
576 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
582 /* try to convert to corresponding integer */
584 shdr
->bmRequestType
= strtoul(str1
, 0, 16);
585 shdr
->bRequest
= strtoul(str2
, 0, 16);
586 shdr
->wValue
= htols(strtoul(str3
, 0, 16));
587 shdr
->wIndex
= htols(strtoul(str4
, 0, 16));
588 shdr
->wLength
= htols(strtoul(str5
, 0, 16));
590 uhdr
->setup_flag
= 0;
593 uhdr
->setup_flag
= 1;
596 ret
= sscanf(string
, " %d%n", &urb_len
, &cnt
);
599 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
600 "Can't parse urb length from '%s'", string
);
605 /* urb tag is not present if urb length is 0, so we can stop here
607 pkth
.len
= urb_len
+pkth
.caplen
;
608 uhdr
->urb_len
= urb_len
;
611 if (uhdr
->urb_len
== 0)
614 /* check for data presence; data is present if and only if urb tag is '=' */
615 if (sscanf(string
, " %c", &urb_tag
) != 1)
617 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
618 "Can't parse urb tag from '%s'", string
);
625 /* skip urb tag and following space */
628 /* if we reach this point we got some urb data*/
631 /* read all urb data; if urb length is greater then the usbmon internal
632 * buffer length used by the kernel to spool the URB, we get only
633 * a partial information.
634 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
635 * length and default value is 130. */
636 while ((string
[0] != 0) && (string
[1] != 0) && (pkth
.caplen
< handle
->snapshot
))
638 rawdata
[0] = ascii_to_int(string
[0]) * 16 + ascii_to_int(string
[1]);
641 if (string
[0] == ' ')
648 uhdr
->data_len
= data_len
;
649 if (pkth
.caplen
> handle
->snapshot
)
650 pkth
.caplen
= handle
->snapshot
;
652 if (handle
->fcode
.bf_insns
== NULL
||
653 bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
654 pkth
.len
, pkth
.caplen
)) {
655 handlep
->packets_read
++;
656 callback(user
, &pkth
, handle
->buffer
);
659 return 0; /* didn't pass filter */
663 usb_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
665 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on "
671 usb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
673 struct pcap_usb_linux
*handlep
= handle
->priv
;
674 int dummy
, ret
, consumed
, cnt
;
675 char string
[USB_LINE_LEN
];
676 char token
[USB_LINE_LEN
];
680 snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR
"/%ds", handlep
->bus_index
);
681 fd
= open(string
, O_RDONLY
, 0);
687 * Not found at the new location; try the old
690 snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%ds", handlep
->bus_index
);
691 fd
= open(string
, O_RDONLY
, 0);
694 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
695 "Can't open USB stats file %s: %s",
696 string
, strerror(errno
));
701 /* read stats line */
703 ret
= read(fd
, string
, USB_LINE_LEN
-1);
704 } while ((ret
== -1) && (errno
== EINTR
));
709 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
710 "Can't read stats from fd %d ", fd
);
715 /* extract info on dropped urbs */
716 for (consumed
=0; consumed
< ret
; ) {
717 /* from the sscanf man page:
718 * The C standard says: "Execution of a %n directive does
719 * not increment the assignment count returned at the completion
720 * of execution" but the Corrigendum seems to contradict this.
721 * Do not make any assumptions on the effect of %n conversions
722 * on the return value and explicitly check for cnt assignmet*/
726 ntok
= sscanf(ptr
, "%s%n", token
, &cnt
);
727 if ((ntok
< 1) || (cnt
< 0))
731 if (strcmp(token
, "nreaders") == 0)
732 ret
= sscanf(ptr
, "%d", &stats
->ps_drop
);
734 ret
= sscanf(ptr
, "%d", &dummy
);
741 stats
->ps_recv
= handlep
->packets_read
;
742 stats
->ps_ifdrop
= 0;
747 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
755 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
757 struct pcap_usb_linux
*handlep
= handle
->priv
;
759 struct mon_bin_stats st
;
760 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
763 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
764 "Can't read stats from fd %d:%s ", handle
->fd
, strerror(errno
));
768 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
769 stats
->ps_drop
= st
.dropped
;
770 stats
->ps_ifdrop
= 0;
775 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
776 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
779 usb_read_linux_bin(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
781 struct pcap_usb_linux
*handlep
= handle
->priv
;
782 struct mon_bin_get info
;
784 struct pcap_pkthdr pkth
;
785 int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
787 /* the usb header is going to be part of 'packet' data*/
788 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
789 info
.data
= handle
->buffer
+ sizeof(pcap_usb_header
);
790 info
.data_len
= clen
;
792 /* ignore interrupt system call errors */
794 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
795 if (handle
->break_loop
)
797 handle
->break_loop
= 0;
800 } while ((ret
== -1) && (errno
== EINTR
));
804 return 0; /* no data there */
806 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
807 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
811 /* we can get less that than really captured from kernel, depending on
812 * snaplen, so adjust header accordingly */
813 if (info
.hdr
->data_len
< clen
)
814 clen
= info
.hdr
->data_len
;
815 info
.hdr
->data_len
= clen
;
816 pkth
.caplen
= clen
+ sizeof(pcap_usb_header
);
817 pkth
.len
= info
.hdr
->data_len
+ sizeof(pcap_usb_header
);
818 pkth
.ts
.tv_sec
= info
.hdr
->ts_sec
;
819 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
821 if (handle
->fcode
.bf_insns
== NULL
||
822 bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
823 pkth
.len
, pkth
.caplen
)) {
824 handlep
->packets_read
++;
825 callback(user
, &pkth
, handle
->buffer
);
829 return 0; /* didn't pass filter */
833 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
834 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
838 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
840 struct pcap_usb_linux
*handlep
= handle
->priv
;
841 struct mon_bin_mfetch fetch
;
842 int32_t vec
[VEC_SIZE
];
843 struct pcap_pkthdr pkth
;
844 pcap_usb_header
* hdr
;
849 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
853 int limit
= max_packets
- packets
;
856 if (limit
> VEC_SIZE
)
859 /* try to fetch as many events as possible*/
861 fetch
.nfetch
= limit
;
862 fetch
.nflush
= nflush
;
863 /* ignore interrupt system call errors */
865 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
866 if (handle
->break_loop
)
868 handle
->break_loop
= 0;
871 } while ((ret
== -1) && (errno
== EINTR
));
875 return 0; /* no data there */
877 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
878 "Can't mfetch fd %d: %s", handle
->fd
, strerror(errno
));
882 /* keep track of processed events, we will flush them later */
883 nflush
= fetch
.nfetch
;
884 for (i
=0; i
<fetch
.nfetch
; ++i
) {
886 hdr
= (pcap_usb_header
*) &handlep
->mmapbuf
[vec
[i
]];
887 if (hdr
->event_type
== '@')
890 /* we can get less that than really captured from kernel, depending on
891 * snaplen, so adjust header accordingly */
893 if (hdr
->data_len
< clen
)
894 clen
= hdr
->data_len
;
896 /* get packet info from header*/
897 pkth
.caplen
= clen
+ sizeof(pcap_usb_header_mmapped
);
898 pkth
.len
= hdr
->data_len
+ sizeof(pcap_usb_header_mmapped
);
899 pkth
.ts
.tv_sec
= hdr
->ts_sec
;
900 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
902 if (handle
->fcode
.bf_insns
== NULL
||
903 bpf_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
904 pkth
.len
, pkth
.caplen
)) {
905 handlep
->packets_read
++;
906 callback(user
, &pkth
, (u_char
*) hdr
);
911 /* with max_packets specifying "unlimited" we stop afer the first chunk*/
912 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) || (packets
== max_packets
))
916 /* flush pending events*/
917 ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
);
922 usb_cleanup_linux_mmap(pcap_t
* handle
)
924 struct pcap_usb_linux
*handlep
= handle
->priv
;
926 /* if we have a memory-mapped buffer, unmap it */
927 if (handlep
->mmapbuf
!= NULL
) {
928 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
929 handlep
->mmapbuf
= NULL
;
931 pcap_cleanup_live_common(handle
);