]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-usb-linux.c
7265a9c1300b68ee272f2602068a99d592832cc1
2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * USB sniffing API implementation for Linux platform
33 * By Paolo Abeni <paolo.abeni@email.it>
42 #include "pcap-usb-linux.h"
45 #ifdef NEED_STRERROR_H
57 #include <netinet/in.h>
58 #include <sys/ioctl.h>
61 #define USB_IFACE "usb"
62 #define USB_TEXT_DIR "/sys/kernel/debug/usbmon"
63 #define USB_BUS_DIR "/proc/bus/usb"
64 #define USB_LINE_LEN 4096
68 #define PIPE_ISOCHRONOUS 0
69 #define PIPE_INTERRUPT 1
70 #define PIPE_CONTROL 2
73 #if __BYTE_ORDER == __LITTLE_ENDIAN
78 #define htols(s) bswap_16(s)
79 #define htoll(l) bswap_32(l)
80 #define htol64(ll) bswap_64(ll)
83 struct mon_bin_stats
{
91 size_t data_len
; /* Length of data (can be zero) */
94 struct mon_bin_mfetch
{
95 int32_t *offvec
; /* Vector of events fetched */
96 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
97 int32_t nflush
; /* Number of events to flush */
100 #define MON_IOC_MAGIC 0x92
102 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
103 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
104 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
105 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
106 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
107 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
108 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
109 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
111 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
112 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
113 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
114 #define MON_BIN_ERROR 0x8
116 /* forward declaration */
117 static int usb_stats_linux(pcap_t
*, struct pcap_stat
*);
118 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
119 static int usb_read_linux(pcap_t
*, int , pcap_handler
, u_char
*);
120 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
121 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
122 static int usb_inject_linux(pcap_t
*, const void *, size_t);
123 static int usb_setfilter_linux(pcap_t
*, struct bpf_program
*);
124 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
125 static void usb_close_linux(pcap_t
*);
126 static void usb_close_linux_mmap(pcap_t
*);
128 /* facility to add an USB device to the device list*/
130 usb_dev_add(pcap_if_t
** alldevsp
, int n
, char *err_str
)
134 snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
135 snprintf(dev_descr
, 30, "USB bus number %d", n
);
137 if (pcap_add_if(alldevsp
, dev_name
, 0,
138 dev_descr
, err_str
) < 0)
144 usb_platform_finddevs(pcap_if_t
**alldevsp
, char *err_str
)
150 /* scan profs usb bus directorys */
151 dir
= opendir(USB_BUS_DIR
);
152 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
154 char* name
= data
->d_name
;
155 int len
= strlen(name
);
157 /* if this file name does not end with a number it's not of our interest */
158 if ((len
< 1) || !isdigit(name
[--len
]))
160 while (isdigit(name
[--len
]));
161 if (sscanf(&name
[len
+1], "%d", &n
) != 1)
164 ret
= usb_dev_add(alldevsp
, n
, err_str
);
172 int usb_mmap(pcap_t
* handle
)
174 int len
= ioctl(handle
->fd
, MON_IOCQ_RING_SIZE
);
178 handle
->buffer
= mmap(0, len
, PROT_READ
, MAP_SHARED
, handle
->fd
, 0);
179 return handle
->buffer
!= MAP_FAILED
;
183 usb_open_live(const char* bus
, int snaplen
, int promisc
, int to_ms
, char* errmsg
)
185 char full_path
[USB_LINE_LEN
];
188 /* Allocate a handle for this session. */
189 handle
= malloc(sizeof(*handle
));
190 if (handle
== NULL
) {
191 snprintf(errmsg
, PCAP_ERRBUF_SIZE
, "malloc: %s",
192 pcap_strerror(errno
));
196 /* Initialize some components of the pcap structure. */
197 memset(handle
, 0, sizeof(*handle
));
198 handle
->snapshot
= snaplen
;
199 handle
->md
.timeout
= to_ms
;
200 handle
->bufsize
= snaplen
;
202 handle
->linktype
= DLT_USB_LINUX
;
205 * "handle->fd" is a real file , so "select()" and "poll()"
208 handle
->selectable_fd
= handle
->fd
;
210 handle
->inject_op
= usb_inject_linux
;
211 handle
->setfilter_op
= usb_setfilter_linux
;
212 handle
->setdirection_op
= usb_setdirection_linux
;
213 handle
->set_datalink_op
= NULL
; /* can't change data link type */
214 handle
->getnonblock_op
= pcap_getnonblock_fd
;
215 handle
->setnonblock_op
= pcap_setnonblock_fd
;
216 handle
->close_op
= usb_close_linux
;
218 /*get usb bus index from device name */
219 if (sscanf(bus
, USB_IFACE
"%d", &handle
->md
.ifindex
) != 1)
221 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
222 "Can't get USB bus index from %s", bus
);
227 /*now select the read method: try to open binary interface */
228 snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handle
->md
.ifindex
);
229 handle
->fd
= open(full_path
, O_RDONLY
, 0);
232 /* header endianess can't be fixed for memory mapped access,
233 * due to read only access to mmaped buffer, so disable it*/
234 /* binary api is available, try to use fast mmap access */
235 if (usb_mmap(handle
)) {
236 handle
->stats_op
= usb_stats_linux_bin
;
237 handle
->read_op
= usb_read_linux_mmap
;
238 handle
->close_op
= usb_close_linux_mmap
;
242 /* can't mmap, use plain binary interface access */
243 handle
->stats_op
= usb_stats_linux_bin
;
244 handle
->read_op
= usb_read_linux_bin
;
247 /*Binary interface not available, try open text interface */
248 snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR
"/%dt", handle
->md
.ifindex
);
249 handle
->fd
= open(full_path
, O_RDONLY
, 0);
252 /* no more fallback, give it up*/
253 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
254 "Can't open USB bus file %s: %s", full_path
, strerror(errno
));
258 handle
->stats_op
= usb_stats_linux
;
259 handle
->read_op
= usb_read_linux
;
262 /* for plain binary access and text access we need to allocate the read
264 handle
->buffer
= malloc(handle
->bufsize
);
265 if (!handle
->buffer
) {
266 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
267 "malloc: %s", pcap_strerror(errno
));
268 usb_close_linux(handle
);
277 return c
< 'A' ? c
- '0': ((c
<'a') ? c
- 'A' + 10: c
-'a'+10);
281 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
282 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
286 usb_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
289 * /usr/src/linux/Documentation/usb/usbmon.txt
293 int tag
, cnt
, ep_num
, dev_addr
, dummy
, ret
, urb_len
, data_len
;
294 char etype
, pipeid1
, pipeid2
, status
[16], urb_tag
, line
[USB_LINE_LEN
];
296 u_char
* rawdata
= handle
->buffer
;
297 struct pcap_pkthdr pkth
;
298 pcap_usb_header
* uhdr
= (pcap_usb_header
*)handle
->buffer
;
299 u_char urb_transfer
=0;
302 /* ignore interrupt system call errors */
304 ret
= read(handle
->fd
, line
, USB_LINE_LEN
- 1);
305 if (handle
->break_loop
)
307 handle
->break_loop
= 0;
310 } while ((ret
== -1) && (errno
== EINTR
));
314 return 0; /* no data there */
316 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
317 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
321 /* read urb header; %n argument may increment return value, but it's
322 * not mandatory, so does not count on it*/
324 ret
= sscanf(string
, "%x %d %c %c%c:%d:%d %s%n", &tag
, ×tamp
, &etype
,
325 &pipeid1
, &pipeid2
, &dev_addr
, &ep_num
, status
,
329 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
330 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
335 uhdr
->endpoint_number
= ep_num
;
336 uhdr
->device_address
= dev_addr
;
337 uhdr
->bus_id
= handle
->md
.ifindex
;
341 /* don't use usbmon provided timestamp, since it have low precision*/
342 if (gettimeofday(&pkth
.ts
, NULL
) < 0)
344 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
345 "Can't get timestamp for message '%s' %d:%s",
346 string
, errno
, strerror(errno
));
349 uhdr
->ts_sec
= pkth
.ts
.tv_sec
;
350 uhdr
->ts_usec
= pkth
.ts
.tv_usec
;
352 /* parse endpoint information */
354 urb_transfer
= URB_CONTROL
;
355 else if (pipeid1
== 'Z')
356 urb_transfer
= URB_ISOCHRONOUS
;
357 else if (pipeid1
== 'I')
358 urb_transfer
= URB_INTERRUPT
;
359 else if (pipeid1
== 'B')
360 urb_transfer
= URB_BULK
;
361 if (pipeid2
== 'i') {
362 urb_transfer
|= URB_TRANSFER_IN
;
366 incoming
= !incoming
;
371 if (handle
->direction
== PCAP_D_OUT
)
375 if (handle
->direction
== PCAP_D_IN
)
377 uhdr
->event_type
= etype
;
378 uhdr
->transfer_type
= urb_transfer
;
379 pkth
.caplen
= sizeof(pcap_usb_header
);
380 rawdata
+= sizeof(pcap_usb_header
);
382 /* check if this is a setup packet */
383 ret
= sscanf(status
, "%d", &dummy
);
386 /* this a setup packet, setup data can be filled with underscore if
387 * usbmon has not been able to read them, so we must parse this fields as
389 pcap_usb_setup
* shdr
;
390 char str1
[3], str2
[3], str3
[5], str4
[5], str5
[5];
391 ret
= sscanf(string
, "%s %s %s %s %s%n", str1
, str2
, str3
, str4
,
395 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
396 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
402 /* try to convert to corresponding integer */
404 shdr
->bmRequestType
= strtoul(str1
, 0, 16);
405 shdr
->bRequest
= strtoul(str2
, 0, 16);
406 shdr
->wValue
= htols(strtoul(str3
, 0, 16));
407 shdr
->wIndex
= htols(strtoul(str4
, 0, 16));
408 shdr
->wLength
= htols(strtoul(str5
, 0, 16));
410 uhdr
->setup_flag
= 0;
413 uhdr
->setup_flag
= 1;
416 ret
= sscanf(string
, " %d%n", &urb_len
, &cnt
);
419 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
420 "Can't parse urb length from '%s'", string
);
425 /* urb tag is not present if urb length is 0, so we can stop here
427 pkth
.len
= urb_len
+pkth
.caplen
;
428 uhdr
->urb_len
= urb_len
;
431 if (uhdr
->urb_len
== pkth
.caplen
)
434 /* check for data presence; data is present if and only if urb tag is '=' */
435 if (sscanf(string
, " %c", &urb_tag
) != 1)
437 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
438 "Can't parse urb tag from '%s'", string
);
445 /* skip urb tag and following space */
448 /* if we reach this point we got some urb data*/
451 /* read all urb data; if urb length is greater then the usbmon internal
452 * buffer length used by the kernel to spool the URB, we get only
453 * a partial information.
454 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
455 * length and default value is 130. */
456 while ((string
[0] != 0) && (string
[1] != 0) && (pkth
.caplen
< handle
->snapshot
))
458 rawdata
[0] = ascii_to_int(string
[0]) * 16 + ascii_to_int(string
[1]);
461 if (string
[0] == ' ')
468 uhdr
->data_len
= data_len
;
469 handle
->md
.packets_read
++;
470 if (pkth
.caplen
> handle
->snapshot
)
471 pkth
.caplen
= handle
->snapshot
;
473 callback(user
, &pkth
, handle
->buffer
);
478 usb_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
480 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on "
486 usb_close_linux(pcap_t
* handle
)
488 /* handle fill be freed in pcap_close() 'common' code */
491 free(handle
->buffer
);
495 usb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
497 int dummy
, ret
, consumed
, cnt
;
498 char string
[USB_LINE_LEN
];
499 char token
[USB_LINE_LEN
];
501 snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR
"/%ds", handle
->md
.ifindex
);
503 int fd
= open(string
, O_RDONLY
, 0);
506 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
507 "Can't open USB stats file %s: %s",
508 string
, strerror(errno
));
512 /* read stats line */
514 ret
= read(fd
, string
, USB_LINE_LEN
-1);
515 } while ((ret
== -1) && (errno
== EINTR
));
520 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
521 "Can't read stats from fd %d ", fd
);
526 /* extract info on dropped urbs */
527 for (consumed
=0; consumed
< ret
; ) {
528 int ntok
= sscanf(ptr
, "%s%n", token
, &cnt
);
533 if (strcmp(token
, "nreaders") == 0)
534 ret
= sscanf(ptr
, "%d", &stats
->ps_drop
);
536 ret
= sscanf(ptr
, "%d", &dummy
);
543 stats
->ps_recv
= handle
->md
.packets_read
;
544 stats
->ps_ifdrop
= 0;
549 usb_setfilter_linux(pcap_t
*p
, struct bpf_program
*fp
)
555 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
563 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
566 struct mon_bin_stats st
;
567 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
570 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
571 "Can't read stats from fd %d:%s ", handle
->fd
, strerror(errno
));
575 stats
->ps_recv
= handle
->md
.packets_read
+ st
.queued
;
576 stats
->ps_ifdrop
= st
.dropped
;
581 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
582 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
585 usb_read_linux_bin(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
587 struct mon_bin_get info
;
589 struct pcap_pkthdr pkth
;
590 int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
592 /* the usb header is going to be part of 'packet' data*/
593 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
594 info
.data
= handle
->buffer
+ sizeof(pcap_usb_header
);
595 info
.data_len
= clen
;
596 /* ignore interrupt system call errors */
598 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
599 if (handle
->break_loop
)
601 handle
->break_loop
= 0;
604 } while ((ret
== -1) && (errno
== EINTR
));
608 return 0; /* no data there */
610 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
611 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
615 /* we can get less that than really captured from kernel, depending on
616 * snaplen, so adjust header accordingly */
617 if (info
.hdr
->data_len
< clen
)
618 clen
= info
.hdr
->data_len
;
619 info
.hdr
->data_len
= clen
;
620 pkth
.caplen
= clen
+ sizeof(pcap_usb_header
);
621 pkth
.len
= info
.hdr
->urb_len
+ sizeof(pcap_usb_header
);
622 pkth
.ts
.tv_sec
= info
.hdr
->ts_sec
;
623 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
625 handle
->md
.packets_read
++;
626 callback(user
, &pkth
, handle
->buffer
);
631 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
632 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
636 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
638 struct mon_bin_mfetch fetch
;
639 int32_t vec
[VEC_SIZE
];
640 struct pcap_pkthdr pkth
;
641 pcap_usb_header
* hdr
;
647 int limit
= max_packets
- packets
;
650 if (limit
> VEC_SIZE
)
653 /* try to fetch as many events as possible*/
655 fetch
.nfetch
= limit
;
656 fetch
.nflush
= nflush
;
657 /* ignore interrupt system call errors */
659 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
660 if (handle
->break_loop
)
662 handle
->break_loop
= 0;
665 } while ((ret
== -1) && (errno
== EINTR
));
669 return 0; /* no data there */
671 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
672 "Can't mfetch fd %d: %s", handle
->fd
, strerror(errno
));
676 /* keep track of processed events, we will flush them later */
677 nflush
= fetch
.nfetch
;
678 for (i
=0; i
<fetch
.nfetch
; ++i
) {
680 hdr
= (pcap_usb_header
*) &handle
->buffer
[vec
[i
]];
681 if (hdr
->event_type
== '@')
684 /* get packet info from header*/
685 pkth
.caplen
= hdr
->data_len
+ sizeof(pcap_usb_header
);
686 pkth
.len
= hdr
->urb_len
+ sizeof(pcap_usb_header
);
687 pkth
.ts
.tv_sec
= hdr
->ts_sec
;
688 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
690 handle
->md
.packets_read
++;
691 callback(user
, &pkth
, (u_char
*) hdr
);
695 /* with max_packets == -1 we stop afer the first chunk*/
696 if ((max_packets
== -1) || (packets
== max_packets
))
700 /* flush pending events*/
701 ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
);
706 usb_close_linux_mmap(pcap_t
* handle
)
708 /* handle fill be freed in pcap_close() 'common' code, buffer must not
709 * be freed because it's memory mapped */