]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-usb-linux.c
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
);
153 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
155 char* name
= data
->d_name
;
156 int len
= strlen(name
);
158 /* if this file name does not end with a number it's not of our interest */
159 if ((len
< 1) || !isdigit(name
[--len
]))
161 while (isdigit(name
[--len
]));
162 if (sscanf(&name
[len
+1], "%d", &n
) != 1)
165 ret
= usb_dev_add(alldevsp
, n
, err_str
);
173 int usb_mmap(pcap_t
* handle
)
175 int len
= ioctl(handle
->fd
, MON_IOCQ_RING_SIZE
);
179 handle
->buffer
= mmap(0, len
, PROT_READ
, MAP_SHARED
, handle
->fd
, 0);
180 return handle
->buffer
!= MAP_FAILED
;
184 usb_open_live(const char* bus
, int snaplen
, int promisc
, int to_ms
, char* errmsg
)
186 char full_path
[USB_LINE_LEN
];
189 /* Allocate a handle for this session. */
190 handle
= malloc(sizeof(*handle
));
191 if (handle
== NULL
) {
192 snprintf(errmsg
, PCAP_ERRBUF_SIZE
, "malloc: %s",
193 pcap_strerror(errno
));
197 /* Initialize some components of the pcap structure. */
198 memset(handle
, 0, sizeof(*handle
));
199 handle
->snapshot
= snaplen
;
200 handle
->md
.timeout
= to_ms
;
201 handle
->bufsize
= snaplen
;
203 handle
->linktype
= DLT_USB_LINUX
;
206 * "handle->fd" is a real file , so "select()" and "poll()"
209 handle
->selectable_fd
= handle
->fd
;
211 handle
->inject_op
= usb_inject_linux
;
212 handle
->setfilter_op
= usb_setfilter_linux
;
213 handle
->setdirection_op
= usb_setdirection_linux
;
214 handle
->set_datalink_op
= NULL
; /* can't change data link type */
215 handle
->getnonblock_op
= pcap_getnonblock_fd
;
216 handle
->setnonblock_op
= pcap_setnonblock_fd
;
217 handle
->close_op
= usb_close_linux
;
219 /*get usb bus index from device name */
220 if (sscanf(bus
, USB_IFACE
"%d", &handle
->md
.ifindex
) != 1)
222 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
223 "Can't get USB bus index from %s", bus
);
228 /*now select the read method: try to open binary interface */
229 snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handle
->md
.ifindex
);
230 handle
->fd
= open(full_path
, O_RDONLY
, 0);
233 /* header endianess can't be fixed for memory mapped access,
234 * due to read only access to mmaped buffer, so disable it*/
235 /* binary api is available, try to use fast mmap access */
236 if (usb_mmap(handle
)) {
237 handle
->stats_op
= usb_stats_linux_bin
;
238 handle
->read_op
= usb_read_linux_mmap
;
239 handle
->close_op
= usb_close_linux_mmap
;
243 /* can't mmap, use plain binary interface access */
244 handle
->stats_op
= usb_stats_linux_bin
;
245 handle
->read_op
= usb_read_linux_bin
;
248 /*Binary interface not available, try open text interface */
249 snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR
"/%dt", handle
->md
.ifindex
);
250 handle
->fd
= open(full_path
, O_RDONLY
, 0);
253 /* no more fallback, give it up*/
254 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
255 "Can't open USB bus file %s: %s", full_path
, strerror(errno
));
259 handle
->stats_op
= usb_stats_linux
;
260 handle
->read_op
= usb_read_linux
;
263 /* for plain binary access and text access we need to allocate the read
265 handle
->buffer
= malloc(handle
->bufsize
);
266 if (!handle
->buffer
) {
267 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
268 "malloc: %s", pcap_strerror(errno
));
269 usb_close_linux(handle
);
278 return c
< 'A' ? c
- '0': ((c
<'a') ? c
- 'A' + 10: c
-'a'+10);
282 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
283 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
287 usb_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
290 * /usr/src/linux/Documentation/usb/usbmon.txt
294 int tag
, cnt
, ep_num
, dev_addr
, dummy
, ret
, urb_len
, data_len
;
295 char etype
, pipeid1
, pipeid2
, status
[16], urb_tag
, line
[USB_LINE_LEN
];
297 u_char
* rawdata
= handle
->buffer
;
298 struct pcap_pkthdr pkth
;
299 pcap_usb_header
* uhdr
= (pcap_usb_header
*)handle
->buffer
;
300 u_char urb_transfer
=0;
303 /* ignore interrupt system call errors */
305 ret
= read(handle
->fd
, line
, USB_LINE_LEN
- 1);
306 if (handle
->break_loop
)
308 handle
->break_loop
= 0;
311 } while ((ret
== -1) && (errno
== EINTR
));
315 return 0; /* no data there */
317 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
318 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
322 /* read urb header; %n argument may increment return value, but it's
323 * not mandatory, so does not count on it*/
325 ret
= sscanf(string
, "%x %d %c %c%c:%d:%d %s%n", &tag
, ×tamp
, &etype
,
326 &pipeid1
, &pipeid2
, &dev_addr
, &ep_num
, status
,
330 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
331 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
336 uhdr
->endpoint_number
= ep_num
;
337 uhdr
->device_address
= dev_addr
;
338 uhdr
->bus_id
= handle
->md
.ifindex
;
342 /* don't use usbmon provided timestamp, since it have low precision*/
343 if (gettimeofday(&pkth
.ts
, NULL
) < 0)
345 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
346 "Can't get timestamp for message '%s' %d:%s",
347 string
, errno
, strerror(errno
));
350 uhdr
->ts_sec
= pkth
.ts
.tv_sec
;
351 uhdr
->ts_usec
= pkth
.ts
.tv_usec
;
353 /* parse endpoint information */
355 urb_transfer
= URB_CONTROL
;
356 else if (pipeid1
== 'Z')
357 urb_transfer
= URB_ISOCHRONOUS
;
358 else if (pipeid1
== 'I')
359 urb_transfer
= URB_INTERRUPT
;
360 else if (pipeid1
== 'B')
361 urb_transfer
= URB_BULK
;
362 if (pipeid2
== 'i') {
363 urb_transfer
|= URB_TRANSFER_IN
;
367 incoming
= !incoming
;
372 if (handle
->direction
== PCAP_D_OUT
)
376 if (handle
->direction
== PCAP_D_IN
)
378 uhdr
->event_type
= etype
;
379 uhdr
->transfer_type
= urb_transfer
;
380 pkth
.caplen
= sizeof(pcap_usb_header
);
381 rawdata
+= sizeof(pcap_usb_header
);
383 /* check if this is a setup packet */
384 ret
= sscanf(status
, "%d", &dummy
);
387 /* this a setup packet, setup data can be filled with underscore if
388 * usbmon has not been able to read them, so we must parse this fields as
390 pcap_usb_setup
* shdr
;
391 char str1
[3], str2
[3], str3
[5], str4
[5], str5
[5];
392 ret
= sscanf(string
, "%s %s %s %s %s%n", str1
, str2
, str3
, str4
,
396 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
397 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
403 /* try to convert to corresponding integer */
405 shdr
->bmRequestType
= strtoul(str1
, 0, 16);
406 shdr
->bRequest
= strtoul(str2
, 0, 16);
407 shdr
->wValue
= htols(strtoul(str3
, 0, 16));
408 shdr
->wIndex
= htols(strtoul(str4
, 0, 16));
409 shdr
->wLength
= htols(strtoul(str5
, 0, 16));
411 uhdr
->setup_flag
= 0;
414 uhdr
->setup_flag
= 1;
417 ret
= sscanf(string
, " %d%n", &urb_len
, &cnt
);
420 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
421 "Can't parse urb length from '%s'", string
);
426 /* urb tag is not present if urb length is 0, so we can stop here
428 pkth
.len
= urb_len
+pkth
.caplen
;
429 uhdr
->urb_len
= urb_len
;
432 if (uhdr
->urb_len
== pkth
.caplen
)
435 /* check for data presence; data is present if and only if urb tag is '=' */
436 if (sscanf(string
, " %c", &urb_tag
) != 1)
438 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
439 "Can't parse urb tag from '%s'", string
);
446 /* skip urb tag and following space */
449 /* if we reach this point we got some urb data*/
452 /* read all urb data; if urb length is greater then the usbmon internal
453 * buffer length used by the kernel to spool the URB, we get only
454 * a partial information.
455 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
456 * length and default value is 130. */
457 while ((string
[0] != 0) && (string
[1] != 0) && (pkth
.caplen
< handle
->snapshot
))
459 rawdata
[0] = ascii_to_int(string
[0]) * 16 + ascii_to_int(string
[1]);
462 if (string
[0] == ' ')
469 uhdr
->data_len
= data_len
;
470 handle
->md
.packets_read
++;
471 if (pkth
.caplen
> handle
->snapshot
)
472 pkth
.caplen
= handle
->snapshot
;
474 callback(user
, &pkth
, handle
->buffer
);
479 usb_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
481 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on "
487 usb_close_linux(pcap_t
* handle
)
489 /* handle fill be freed in pcap_close() 'common' code */
492 free(handle
->buffer
);
496 usb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
498 int dummy
, ret
, consumed
, cnt
;
499 char string
[USB_LINE_LEN
];
500 char token
[USB_LINE_LEN
];
502 snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR
"/%ds", handle
->md
.ifindex
);
504 int fd
= open(string
, O_RDONLY
, 0);
507 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
508 "Can't open USB stats file %s: %s",
509 string
, strerror(errno
));
513 /* read stats line */
515 ret
= read(fd
, string
, USB_LINE_LEN
-1);
516 } while ((ret
== -1) && (errno
== EINTR
));
521 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
522 "Can't read stats from fd %d ", fd
);
527 /* extract info on dropped urbs */
528 for (consumed
=0; consumed
< ret
; ) {
529 int ntok
= sscanf(ptr
, "%s%n", token
, &cnt
);
534 if (strcmp(token
, "nreaders") == 0)
535 ret
= sscanf(ptr
, "%d", &stats
->ps_drop
);
537 ret
= sscanf(ptr
, "%d", &dummy
);
544 stats
->ps_recv
= handle
->md
.packets_read
;
545 stats
->ps_ifdrop
= 0;
550 usb_setfilter_linux(pcap_t
*p
, struct bpf_program
*fp
)
556 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
564 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
567 struct mon_bin_stats st
;
568 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
571 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
572 "Can't read stats from fd %d:%s ", handle
->fd
, strerror(errno
));
576 stats
->ps_recv
= handle
->md
.packets_read
+ st
.queued
;
577 stats
->ps_ifdrop
= st
.dropped
;
582 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
583 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
586 usb_read_linux_bin(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
588 struct mon_bin_get info
;
590 struct pcap_pkthdr pkth
;
591 int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
593 /* the usb header is going to be part of 'packet' data*/
594 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
595 info
.data
= handle
->buffer
+ sizeof(pcap_usb_header
);
596 info
.data_len
= clen
;
597 /* ignore interrupt system call errors */
599 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
600 if (handle
->break_loop
)
602 handle
->break_loop
= 0;
605 } while ((ret
== -1) && (errno
== EINTR
));
609 return 0; /* no data there */
611 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
612 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
616 /* we can get less that than really captured from kernel, depending on
617 * snaplen, so adjust header accordingly */
618 if (info
.hdr
->data_len
< clen
)
619 clen
= info
.hdr
->data_len
;
620 info
.hdr
->data_len
= clen
;
621 pkth
.caplen
= clen
+ sizeof(pcap_usb_header
);
622 pkth
.len
= info
.hdr
->urb_len
+ sizeof(pcap_usb_header
);
623 pkth
.ts
.tv_sec
= info
.hdr
->ts_sec
;
624 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
626 handle
->md
.packets_read
++;
627 callback(user
, &pkth
, handle
->buffer
);
632 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
633 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
637 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
639 struct mon_bin_mfetch fetch
;
640 int32_t vec
[VEC_SIZE
];
641 struct pcap_pkthdr pkth
;
642 pcap_usb_header
* hdr
;
648 int limit
= max_packets
- packets
;
651 if (limit
> VEC_SIZE
)
654 /* try to fetch as many events as possible*/
656 fetch
.nfetch
= limit
;
657 fetch
.nflush
= nflush
;
658 /* ignore interrupt system call errors */
660 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
661 if (handle
->break_loop
)
663 handle
->break_loop
= 0;
666 } while ((ret
== -1) && (errno
== EINTR
));
670 return 0; /* no data there */
672 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
673 "Can't mfetch fd %d: %s", handle
->fd
, strerror(errno
));
677 /* keep track of processed events, we will flush them later */
678 nflush
= fetch
.nfetch
;
679 for (i
=0; i
<fetch
.nfetch
; ++i
) {
681 hdr
= (pcap_usb_header
*) &handle
->buffer
[vec
[i
]];
682 if (hdr
->event_type
== '@')
685 /* get packet info from header*/
686 pkth
.caplen
= hdr
->data_len
+ sizeof(pcap_usb_header
);
687 pkth
.len
= hdr
->urb_len
+ sizeof(pcap_usb_header
);
688 pkth
.ts
.tv_sec
= hdr
->ts_sec
;
689 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
691 handle
->md
.packets_read
++;
692 callback(user
, &pkth
, (u_char
*) hdr
);
696 /* with max_packets == -1 we stop afer the first chunk*/
697 if ((max_packets
== -1) || (packets
== max_packets
))
701 /* flush pending events*/
702 ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
);
707 usb_close_linux_mmap(pcap_t
* handle
)
709 /* handle fill be freed in pcap_close() 'common' code, buffer must not
710 * be freed because it's memory mapped */