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 #ifdef HAVE_LINUX_USBDEVICE_FS_H
61 * We might need <linux/compiler.h> to define __user for
62 * <linux/usbdevice_fs.h>.
64 #ifdef HAVE_LINUX_COMPILER_H
65 #include <linux/compiler.h>
66 #endif /* HAVE_LINUX_COMPILER_H */
67 #include <linux/usbdevice_fs.h>
68 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
70 #define USB_IFACE "usbmon"
71 #define USB_TEXT_DIR_OLD "/sys/kernel/debug/usbmon"
72 #define USB_TEXT_DIR "/sys/kernel/debug/usb/usbmon"
73 #define SYS_USB_BUS_DIR "/sys/bus/usb/devices"
74 #define PROC_USB_BUS_DIR "/proc/bus/usb"
75 #define USB_LINE_LEN 4096
77 #if __BYTE_ORDER == __LITTLE_ENDIAN
82 #define htols(s) bswap_16(s)
83 #define htoll(l) bswap_32(l)
84 #define htol64(ll) bswap_64(ll)
87 struct mon_bin_stats
{
95 size_t data_len
; /* Length of data (can be zero) */
98 struct mon_bin_mfetch
{
99 int32_t *offvec
; /* Vector of events fetched */
100 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
101 int32_t nflush
; /* Number of events to flush */
104 #define MON_IOC_MAGIC 0x92
106 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
107 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
108 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
109 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
110 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
111 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
112 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
113 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
115 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
116 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
117 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
118 #define MON_BIN_ERROR 0x8
121 * Private data for capturing on Linux USB.
123 struct pcap_usb_linux
{
124 u_char
*mmapbuf
; /* memory-mapped region pointer */
125 size_t mmapbuflen
; /* size of region */
130 /* forward declaration */
131 static int usb_activate(pcap_t
*);
132 static int usb_stats_linux(pcap_t
*, struct pcap_stat
*);
133 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
134 static int usb_read_linux(pcap_t
*, int , pcap_handler
, u_char
*);
135 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
136 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
137 static int usb_inject_linux(pcap_t
*, const void *, size_t);
138 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
139 static void usb_cleanup_linux_mmap(pcap_t
*);
141 /* facility to add an USB device to the device list*/
143 usb_dev_add(pcap_if_t
** alldevsp
, int n
, char *err_str
)
147 pcap_snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
148 pcap_snprintf(dev_descr
, 30, "USB bus number %d", n
);
150 if (pcap_add_if(alldevsp
, dev_name
, 0,
151 dev_descr
, err_str
) < 0)
157 usb_findalldevs(pcap_if_t
**alldevsp
, char *err_str
)
168 * Do we have a "scan all buses" device?
169 * First, try the binary device.
171 fd
= open(LINUX_USB_MON_DEV
"0", O_RDONLY
, 0);
177 if (pcap_add_if(alldevsp
, "usbmon0", 0, "All USB buses",
182 * No binary device; do we have the text device?
184 fd
= open(USB_TEXT_DIR
"/0t", O_RDONLY
, 0);
187 * Not at the new location; try the old location.
189 fd
= open(USB_TEXT_DIR_OLD
"/0t", O_RDONLY
, 0);
196 if (pcap_add_if(alldevsp
, "usbmon0", 0, "All USB buses",
203 * Now look for individual USB buses.
205 * First, try scanning sysfs USB bus directory.
207 dir
= opendir(SYS_USB_BUS_DIR
);
209 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
212 if (strncmp(name
, "usb", 3) != 0)
215 if (sscanf(&name
[3], "%d", &n
) == 0)
218 ret
= usb_dev_add(alldevsp
, n
, err_str
);
225 /* That didn't work; try scanning procfs USB bus directory. */
226 dir
= opendir(PROC_USB_BUS_DIR
);
228 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
232 /* if this file name does not end with a number it's not of our interest */
233 if ((len
< 1) || !isdigit(name
[--len
]))
235 while (isdigit(name
[--len
]));
236 if (sscanf(&name
[len
+1], "%d", &n
) != 1)
239 ret
= usb_dev_add(alldevsp
, n
, err_str
);
246 /* neither of them worked */
251 int usb_mmap(pcap_t
* handle
)
253 struct pcap_usb_linux
*handlep
= handle
->priv
;
254 int len
= ioctl(handle
->fd
, MON_IOCQ_RING_SIZE
);
258 handlep
->mmapbuflen
= len
;
259 handlep
->mmapbuf
= mmap(0, handlep
->mmapbuflen
, PROT_READ
,
260 MAP_SHARED
, handle
->fd
, 0);
261 return handlep
->mmapbuf
!= MAP_FAILED
;
264 #ifdef HAVE_LINUX_USBDEVICE_FS_H
266 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
268 #define USB_DIR_IN 0x80
269 #define USB_TYPE_STANDARD 0x00
270 #define USB_RECIP_DEVICE 0x00
272 #define USB_REQ_GET_DESCRIPTOR 6
274 #define USB_DT_DEVICE 1
276 /* probe the descriptors of the devices attached to the bus */
277 /* the descriptors will end up in the captured packet stream */
278 /* and be decoded by external apps like wireshark */
279 /* without these identifying probes packet data can't be fully decoded */
281 probe_devices(int bus
)
283 struct usbdevfs_ctrltransfer ctrl
;
289 /* scan usb bus directories for device nodes */
290 pcap_snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d", bus
);
295 while ((ret
>= 0) && ((data
= readdir(dir
)) != 0)) {
297 char* name
= data
->d_name
;
302 pcap_snprintf(buf
, sizeof(buf
), "/dev/bus/usb/%03d/%s", bus
, data
->d_name
);
304 fd
= open(buf
, O_RDWR
);
309 * Sigh. Different kernels have different member names
310 * for this structure.
312 #ifdef HAVE_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
313 ctrl
.bRequestType
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
314 ctrl
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
315 ctrl
.wValue
= USB_DT_DEVICE
<< 8;
317 ctrl
.wLength
= sizeof(buf
);
319 ctrl
.requesttype
= USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
320 ctrl
.request
= USB_REQ_GET_DESCRIPTOR
;
321 ctrl
.value
= USB_DT_DEVICE
<< 8;
323 ctrl
.length
= sizeof(buf
);
326 ctrl
.timeout
= CTRL_TIMEOUT
;
328 ret
= ioctl(fd
, USBDEVFS_CONTROL
, &ctrl
);
334 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
337 usb_create(const char *device
, char *ebuf
, int *is_ours
)
344 /* Does this look like a USB monitoring device? */
345 cp
= strrchr(device
, '/');
348 /* Does it begin with USB_IFACE? */
349 if (strncmp(cp
, USB_IFACE
, sizeof USB_IFACE
- 1) != 0) {
350 /* Nope, doesn't begin with USB_IFACE */
354 /* Yes - is USB_IFACE followed by a number? */
355 cp
+= sizeof USB_IFACE
- 1;
356 devnum
= strtol(cp
, &cpend
, 10);
357 if (cpend
== cp
|| *cpend
!= '\0') {
358 /* Not followed by a number. */
363 /* Followed by a non-valid number. */
368 /* OK, it's probably ours. */
371 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_usb_linux
));
375 p
->activate_op
= usb_activate
;
380 usb_activate(pcap_t
* handle
)
382 struct pcap_usb_linux
*handlep
= handle
->priv
;
383 char full_path
[USB_LINE_LEN
];
385 /* Initialize some components of the pcap structure. */
386 handle
->bufsize
= handle
->snapshot
;
388 handle
->linktype
= DLT_USB_LINUX
;
390 handle
->inject_op
= usb_inject_linux
;
391 handle
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
392 handle
->setdirection_op
= usb_setdirection_linux
;
393 handle
->set_datalink_op
= NULL
; /* can't change data link type */
394 handle
->getnonblock_op
= pcap_getnonblock_fd
;
395 handle
->setnonblock_op
= pcap_setnonblock_fd
;
397 /*get usb bus index from device name */
398 if (sscanf(handle
->opt
.source
, USB_IFACE
"%d", &handlep
->bus_index
) != 1)
400 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
401 "Can't get USB bus index from %s", handle
->opt
.source
);
405 /*now select the read method: try to open binary interface */
406 pcap_snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handlep
->bus_index
);
407 handle
->fd
= open(full_path
, O_RDONLY
, 0);
410 if (handle
->opt
.rfmon
) {
412 * Monitor mode doesn't apply to USB devices.
415 return PCAP_ERROR_RFMON_NOTSUP
;
418 /* binary api is available, try to use fast mmap access */
419 if (usb_mmap(handle
)) {
420 handle
->linktype
= DLT_USB_LINUX_MMAPPED
;
421 handle
->stats_op
= usb_stats_linux_bin
;
422 handle
->read_op
= usb_read_linux_mmap
;
423 handle
->cleanup_op
= usb_cleanup_linux_mmap
;
424 #ifdef HAVE_LINUX_USBDEVICE_FS_H
425 probe_devices(handlep
->bus_index
);
429 * "handle->fd" is a real file, so "select()" and
430 * "poll()" work on it.
432 handle
->selectable_fd
= handle
->fd
;
436 /* can't mmap, use plain binary interface access */
437 handle
->stats_op
= usb_stats_linux_bin
;
438 handle
->read_op
= usb_read_linux_bin
;
439 #ifdef HAVE_LINUX_USBDEVICE_FS_H
440 probe_devices(handlep
->bus_index
);
444 /*Binary interface not available, try open text interface */
445 pcap_snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR
"/%dt", handlep
->bus_index
);
446 handle
->fd
= open(full_path
, O_RDONLY
, 0);
452 * Not found at the new location; try
455 pcap_snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%dt", handlep
->bus_index
);
456 handle
->fd
= open(full_path
, O_RDONLY
, 0);
458 if (handle
->fd
< 0) {
459 /* no more fallback, give it up*/
460 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
461 "Can't open USB bus file %s: %s", full_path
, strerror(errno
));
466 if (handle
->opt
.rfmon
) {
468 * Monitor mode doesn't apply to USB devices.
471 return PCAP_ERROR_RFMON_NOTSUP
;
474 handle
->stats_op
= usb_stats_linux
;
475 handle
->read_op
= usb_read_linux
;
479 * "handle->fd" is a real file, so "select()" and "poll()"
482 handle
->selectable_fd
= handle
->fd
;
484 /* for plain binary access and text access we need to allocate the read
486 handle
->buffer
= malloc(handle
->bufsize
);
487 if (!handle
->buffer
) {
488 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
489 "malloc: %s", pcap_strerror(errno
));
499 return c
< 'A' ? c
- '0': ((c
<'a') ? c
- 'A' + 10: c
-'a'+10);
503 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
504 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
508 usb_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
511 * /usr/src/linux/Documentation/usb/usbmon.txt
514 struct pcap_usb_linux
*handlep
= handle
->priv
;
516 int tag
, cnt
, ep_num
, dev_addr
, dummy
, ret
, urb_len
, data_len
;
517 char etype
, pipeid1
, pipeid2
, status
[16], urb_tag
, line
[USB_LINE_LEN
];
519 u_char
* rawdata
= handle
->buffer
;
520 struct pcap_pkthdr pkth
;
521 pcap_usb_header
* uhdr
= (pcap_usb_header
*)handle
->buffer
;
522 u_char urb_transfer
=0;
525 /* ignore interrupt system call errors */
527 ret
= read(handle
->fd
, line
, USB_LINE_LEN
- 1);
528 if (handle
->break_loop
)
530 handle
->break_loop
= 0;
533 } while ((ret
== -1) && (errno
== EINTR
));
537 return 0; /* no data there */
539 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
540 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
544 /* read urb header; %n argument may increment return value, but it's
545 * not mandatory, so does not count on it*/
547 ret
= sscanf(string
, "%x %d %c %c%c:%d:%d %s%n", &tag
, ×tamp
, &etype
,
548 &pipeid1
, &pipeid2
, &dev_addr
, &ep_num
, status
,
552 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
553 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
558 uhdr
->device_address
= dev_addr
;
559 uhdr
->bus_id
= handlep
->bus_index
;
563 /* don't use usbmon provided timestamp, since it have low precision*/
564 if (gettimeofday(&pkth
.ts
, NULL
) < 0)
566 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
567 "Can't get timestamp for message '%s' %d:%s",
568 string
, errno
, strerror(errno
));
571 uhdr
->ts_sec
= pkth
.ts
.tv_sec
;
572 uhdr
->ts_usec
= pkth
.ts
.tv_usec
;
574 /* parse endpoint information */
576 urb_transfer
= URB_CONTROL
;
577 else if (pipeid1
== 'Z')
578 urb_transfer
= URB_ISOCHRONOUS
;
579 else if (pipeid1
== 'I')
580 urb_transfer
= URB_INTERRUPT
;
581 else if (pipeid1
== 'B')
582 urb_transfer
= URB_BULK
;
583 if (pipeid2
== 'i') {
584 ep_num
|= URB_TRANSFER_IN
;
588 incoming
= !incoming
;
593 if (handle
->direction
== PCAP_D_OUT
)
597 if (handle
->direction
== PCAP_D_IN
)
599 uhdr
->event_type
= etype
;
600 uhdr
->transfer_type
= urb_transfer
;
601 uhdr
->endpoint_number
= ep_num
;
602 pkth
.caplen
= sizeof(pcap_usb_header
);
603 rawdata
+= sizeof(pcap_usb_header
);
605 /* check if this is a setup packet */
606 ret
= sscanf(status
, "%d", &dummy
);
609 /* this a setup packet, setup data can be filled with underscore if
610 * usbmon has not been able to read them, so we must parse this fields as
612 pcap_usb_setup
* shdr
;
613 char str1
[3], str2
[3], str3
[5], str4
[5], str5
[5];
614 ret
= sscanf(string
, "%s %s %s %s %s%n", str1
, str2
, str3
, str4
,
618 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
619 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
625 /* try to convert to corresponding integer */
627 shdr
->bmRequestType
= strtoul(str1
, 0, 16);
628 shdr
->bRequest
= strtoul(str2
, 0, 16);
629 shdr
->wValue
= htols(strtoul(str3
, 0, 16));
630 shdr
->wIndex
= htols(strtoul(str4
, 0, 16));
631 shdr
->wLength
= htols(strtoul(str5
, 0, 16));
633 uhdr
->setup_flag
= 0;
636 uhdr
->setup_flag
= 1;
639 ret
= sscanf(string
, " %d%n", &urb_len
, &cnt
);
642 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
643 "Can't parse urb length from '%s'", string
);
648 /* urb tag is not present if urb length is 0, so we can stop here
650 pkth
.len
= urb_len
+pkth
.caplen
;
651 uhdr
->urb_len
= urb_len
;
654 if (uhdr
->urb_len
== 0)
657 /* check for data presence; data is present if and only if urb tag is '=' */
658 if (sscanf(string
, " %c", &urb_tag
) != 1)
660 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
661 "Can't parse urb tag from '%s'", string
);
668 /* skip urb tag and following space */
671 /* if we reach this point we got some urb data*/
674 /* read all urb data; if urb length is greater then the usbmon internal
675 * buffer length used by the kernel to spool the URB, we get only
676 * a partial information.
677 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
678 * length and default value is 130. */
679 while ((string
[0] != 0) && (string
[1] != 0) && (pkth
.caplen
< handle
->snapshot
))
681 rawdata
[0] = ascii_to_int(string
[0]) * 16 + ascii_to_int(string
[1]);
684 if (string
[0] == ' ')
691 uhdr
->data_len
= data_len
;
692 if (pkth
.caplen
> handle
->snapshot
)
693 pkth
.caplen
= handle
->snapshot
;
695 if (handle
->fcode
.bf_insns
== NULL
||
696 bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
697 pkth
.len
, pkth
.caplen
)) {
698 handlep
->packets_read
++;
699 callback(user
, &pkth
, handle
->buffer
);
702 return 0; /* didn't pass filter */
706 usb_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
708 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on "
714 usb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
716 struct pcap_usb_linux
*handlep
= handle
->priv
;
717 int dummy
, ret
, consumed
, cnt
;
718 char string
[USB_LINE_LEN
];
719 char token
[USB_LINE_LEN
];
723 pcap_snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR
"/%ds", handlep
->bus_index
);
724 fd
= open(string
, O_RDONLY
, 0);
730 * Not found at the new location; try the old
733 pcap_snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR_OLD
"/%ds", handlep
->bus_index
);
734 fd
= open(string
, O_RDONLY
, 0);
737 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
738 "Can't open USB stats file %s: %s",
739 string
, strerror(errno
));
744 /* read stats line */
746 ret
= read(fd
, string
, USB_LINE_LEN
-1);
747 } while ((ret
== -1) && (errno
== EINTR
));
752 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
753 "Can't read stats from fd %d ", fd
);
758 /* extract info on dropped urbs */
759 for (consumed
=0; consumed
< ret
; ) {
760 /* from the sscanf man page:
761 * The C standard says: "Execution of a %n directive does
762 * not increment the assignment count returned at the completion
763 * of execution" but the Corrigendum seems to contradict this.
764 * Do not make any assumptions on the effect of %n conversions
765 * on the return value and explicitly check for cnt assignmet*/
769 ntok
= sscanf(ptr
, "%s%n", token
, &cnt
);
770 if ((ntok
< 1) || (cnt
< 0))
774 if (strcmp(token
, "nreaders") == 0)
775 ret
= sscanf(ptr
, "%d", &stats
->ps_drop
);
777 ret
= sscanf(ptr
, "%d", &dummy
);
784 stats
->ps_recv
= handlep
->packets_read
;
785 stats
->ps_ifdrop
= 0;
790 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
798 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
800 struct pcap_usb_linux
*handlep
= handle
->priv
;
802 struct mon_bin_stats st
;
803 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
806 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
807 "Can't read stats from fd %d:%s ", handle
->fd
, strerror(errno
));
811 stats
->ps_recv
= handlep
->packets_read
+ st
.queued
;
812 stats
->ps_drop
= st
.dropped
;
813 stats
->ps_ifdrop
= 0;
818 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
819 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
822 usb_read_linux_bin(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
824 struct pcap_usb_linux
*handlep
= handle
->priv
;
825 struct mon_bin_get info
;
827 struct pcap_pkthdr pkth
;
828 int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
830 /* the usb header is going to be part of 'packet' data*/
831 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
832 info
.data
= (u_char
*)handle
->buffer
+ sizeof(pcap_usb_header
);
833 info
.data_len
= clen
;
835 /* ignore interrupt system call errors */
837 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
838 if (handle
->break_loop
)
840 handle
->break_loop
= 0;
843 } while ((ret
== -1) && (errno
== EINTR
));
847 return 0; /* no data there */
849 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
850 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
854 /* we can get less that than really captured from kernel, depending on
855 * snaplen, so adjust header accordingly */
856 if (info
.hdr
->data_len
< clen
)
857 clen
= info
.hdr
->data_len
;
858 info
.hdr
->data_len
= clen
;
859 pkth
.caplen
= clen
+ sizeof(pcap_usb_header
);
860 pkth
.len
= info
.hdr
->data_len
+ sizeof(pcap_usb_header
);
861 pkth
.ts
.tv_sec
= info
.hdr
->ts_sec
;
862 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
864 if (handle
->fcode
.bf_insns
== NULL
||
865 bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
866 pkth
.len
, pkth
.caplen
)) {
867 handlep
->packets_read
++;
868 callback(user
, &pkth
, handle
->buffer
);
872 return 0; /* didn't pass filter */
876 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
877 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
881 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
883 struct pcap_usb_linux
*handlep
= handle
->priv
;
884 struct mon_bin_mfetch fetch
;
885 int32_t vec
[VEC_SIZE
];
886 struct pcap_pkthdr pkth
;
887 pcap_usb_header
* hdr
;
892 max_clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
896 int limit
= max_packets
- packets
;
899 if (limit
> VEC_SIZE
)
902 /* try to fetch as many events as possible*/
904 fetch
.nfetch
= limit
;
905 fetch
.nflush
= nflush
;
906 /* ignore interrupt system call errors */
908 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
909 if (handle
->break_loop
)
911 handle
->break_loop
= 0;
914 } while ((ret
== -1) && (errno
== EINTR
));
918 return 0; /* no data there */
920 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
921 "Can't mfetch fd %d: %s", handle
->fd
, strerror(errno
));
925 /* keep track of processed events, we will flush them later */
926 nflush
= fetch
.nfetch
;
927 for (i
=0; i
<fetch
.nfetch
; ++i
) {
929 hdr
= (pcap_usb_header
*) &handlep
->mmapbuf
[vec
[i
]];
930 if (hdr
->event_type
== '@')
933 /* we can get less that than really captured from kernel, depending on
934 * snaplen, so adjust header accordingly */
936 if (hdr
->data_len
< clen
)
937 clen
= hdr
->data_len
;
939 /* get packet info from header*/
940 pkth
.caplen
= clen
+ sizeof(pcap_usb_header_mmapped
);
941 pkth
.len
= hdr
->data_len
+ sizeof(pcap_usb_header_mmapped
);
942 pkth
.ts
.tv_sec
= hdr
->ts_sec
;
943 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
945 if (handle
->fcode
.bf_insns
== NULL
||
946 bpf_filter(handle
->fcode
.bf_insns
, (u_char
*) hdr
,
947 pkth
.len
, pkth
.caplen
)) {
948 handlep
->packets_read
++;
949 callback(user
, &pkth
, (u_char
*) hdr
);
954 /* with max_packets specifying "unlimited" we stop afer the first chunk*/
955 if (PACKET_COUNT_IS_UNLIMITED(max_packets
) || (packets
== max_packets
))
959 /* flush pending events*/
960 if (ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
) == -1) {
961 pcap_snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
962 "Can't mflush fd %d: %s", handle
->fd
, strerror(errno
));
969 usb_cleanup_linux_mmap(pcap_t
* handle
)
971 struct pcap_usb_linux
*handlep
= handle
->priv
;
973 /* if we have a memory-mapped buffer, unmap it */
974 if (handlep
->mmapbuf
!= NULL
) {
975 munmap(handlep
->mmapbuf
, handlep
->mmapbuflen
);
976 handlep
->mmapbuf
= NULL
;
978 pcap_cleanup_live_common(handle
);