]>
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
56 #include <netinet/in.h>
57 #include <sys/ioctl.h>
60 #define USB_IFACE "usb"
61 #define USB_DIR "/sys/kernel/debug/usbmon"
62 #define USB_LINE_LEN 4096
66 #define PIPE_ISOCHRONOUS 0
67 #define PIPE_INTERRUPT 1
68 #define PIPE_CONTROL 2
71 #if __BYTE_ORDER == __LITTLE_ENDIAN
75 #define htols(s) bswap_16(s)
76 #define htoll(l) bswap_32(l)
79 struct mon_bin_stats
{
87 size_t data_len
; /* Length of data (can be zero) */
90 struct mon_bin_mfetch
{
91 int32_t *offvec
; /* Vector of events fetched */
92 int32_t nfetch
; /* Number of events to fetch (out: fetched) */
93 int32_t nflush
; /* Number of events to flush */
96 #define MON_IOC_MAGIC 0x92
98 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
99 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
100 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
101 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
102 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
103 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
104 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
105 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
107 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
108 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
109 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
110 #define MON_BIN_ERROR 0x8
112 /* forward declaration */
113 static int usb_stats_linux(pcap_t
*, struct pcap_stat
*);
114 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
115 static int usb_read_linux(pcap_t
*, int , pcap_handler
, u_char
*);
116 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
117 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
118 static int usb_inject_linux(pcap_t
*, const void *, size_t);
119 static int usb_setfilter_linux(pcap_t
*, struct bpf_program
*);
120 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
121 static void usb_close_linux(pcap_t
*);
122 static void usb_close_linux_mmap(pcap_t
*);
125 usb_platform_finddevs(pcap_if_t
**alldevsp
, char *err_str
)
127 pcap_if_t
*devlist
= *alldevsp
;
129 DIR* dir
= opendir(USB_DIR
);
131 /* it's not fatal, but it would be useful to give a message
134 mount -t debugfs none_debugs /sys/kernel/debug
139 /* scan usbmon directory */
141 while ((data
= readdir(dir
)) != 0)
143 char* name
= data
->d_name
;
144 int len
= strlen(name
);
146 if ((len
>= 2) && name
[len
-1]== 't')
148 int n
= name
[0] - '0';
149 char dev_name
[10], dev_descr
[30];
150 snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
151 snprintf(dev_descr
, 30, "USB bus number %d", n
);
153 if (pcap_add_if(&devlist
, dev_name
, 0,
154 dev_descr
, err_str
) < 0)
168 int usb_mmap(pcap_t
* handle
)
170 int len
= ioctl(handle
->fd
, MON_IOCQ_RING_SIZE
);
174 handle
->buffer
= mmap(0, len
, PROT_READ
, MAP_SHARED
, handle
->fd
, 0);
175 return handle
->buffer
!= MAP_FAILED
;
179 usb_open_live(const char* bus
, int snaplen
, int promisc
, int to_ms
, char* errmsg
)
181 char full_path
[USB_LINE_LEN
];
184 /* Allocate a handle for this session. */
185 handle
= malloc(sizeof(*handle
));
186 if (handle
== NULL
) {
187 snprintf(errmsg
, PCAP_ERRBUF_SIZE
, "malloc: %s",
188 pcap_strerror(errno
));
192 /* Initialize some components of the pcap structure. */
193 memset(handle
, 0, sizeof(*handle
));
194 handle
->snapshot
= snaplen
;
195 handle
->md
.timeout
= to_ms
;
196 handle
->bufsize
= snaplen
;
198 handle
->linktype
= DLT_USB
;
201 * "handle->fd" is a real file , so "select()" and "poll()"
204 handle
->selectable_fd
= handle
->fd
;
206 handle
->inject_op
= usb_inject_linux
;
207 handle
->setfilter_op
= usb_setfilter_linux
;
208 handle
->setdirection_op
= usb_setdirection_linux
;
209 handle
->set_datalink_op
= NULL
; /* can't change data link type */
210 handle
->getnonblock_op
= pcap_getnonblock_fd
;
211 handle
->setnonblock_op
= pcap_setnonblock_fd
;
212 handle
->close_op
= usb_close_linux
;
214 /*get usb bus index from device name */
215 if (sscanf(bus
, USB_IFACE
"%d", &handle
->md
.ifindex
) != 1)
217 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
218 "Can't get USB bus index from %s", bus
);
223 /*now select the read method: try to open binary interface */
224 snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handle
->md
.ifindex
);
225 handle
->fd
= open(full_path
, O_RDONLY
, 0);
228 /* binary api is available, try to use fast mmap access */
229 if (usb_mmap(handle
)) {
230 handle
->stats_op
= usb_stats_linux_bin
;
231 handle
->read_op
= usb_read_linux_mmap
;
232 handle
->close_op
= usb_close_linux_mmap
;
236 /* can't mmap, use plain binary interface access */
237 handle
->stats_op
= usb_stats_linux_bin
;
238 handle
->read_op
= usb_read_linux_bin
;
241 /*Binary interface not available, try open text interface */
242 snprintf(full_path
, USB_LINE_LEN
, USB_DIR
"/%dt", handle
->md
.ifindex
);
243 handle
->fd
= open(full_path
, O_RDONLY
, 0);
246 /* no more fallback, give it up*/
247 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
248 "Can't open USB bus file %s: %s", full_path
, strerror(errno
));
252 handle
->stats_op
= usb_stats_linux
;
253 handle
->read_op
= usb_read_linux
;
256 /* for plain binary access and text access we need to allocate the read
258 handle
->buffer
= malloc(handle
->bufsize
);
259 if (!handle
->buffer
) {
260 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
261 "malloc: %s", pcap_strerror(errno
));
262 usb_close_linux(handle
);
271 return c
< 'A' ? c
- '0': ((c
<'a') ? c
- 'A' + 10: c
-'a'+10);
275 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
276 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
280 usb_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
283 * /usr/src/linux/Documentation/usb/usbmon.txt
287 int tag
, cnt
, ep_num
, dev_addr
, dummy
, ret
;
288 char etype
, pipeid1
, pipeid2
, status
[16], urb_tag
, line
[USB_LINE_LEN
];
290 u_char
* rawdata
= handle
->buffer
;
291 struct pcap_pkthdr pkth
;
292 pcap_usb_header
* uhdr
= (pcap_usb_header
*)handle
->buffer
;
293 u_char urb_transfer
=0;
296 /* ignore interrupt system call errors */
298 ret
= read(handle
->fd
, line
, USB_LINE_LEN
- 1);
299 if (handle
->break_loop
)
301 handle
->break_loop
= 0;
304 } while ((ret
== -1) && (errno
== EINTR
));
308 return 0; /* no data there */
310 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
311 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
315 /* read urb header; %n argument may increment return value, but it's
316 * not mandatory, so does not count on it*/
318 ret
= sscanf(string
, "%x %d %c %c%c:%d:%d %s%n", &tag
, ×tamp
, &etype
,
319 &pipeid1
, &pipeid2
, &dev_addr
, &ep_num
, status
,
323 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
324 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
329 uhdr
->endpoint_number
= ep_num
;
330 uhdr
->device_address
= dev_addr
;
331 uhdr
->bus_id
= htols(handle
->md
.ifindex
);
335 /* don't use usbmon provided timestamp, since it have low precision*/
336 if (gettimeofday(&pkth
.ts
, NULL
) < 0)
338 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
339 "Can't get timestamp for message '%s' %d:%s",
340 string
, errno
, strerror(errno
));
343 uhdr
->ts_sec
= pkth
.ts
.tv_sec
;
344 uhdr
->ts_usec
= pkth
.ts
.tv_usec
;
346 /* parse endpoint information */
348 urb_transfer
= URB_CONTROL
;
349 else if (pipeid1
== 'Z')
350 urb_transfer
= URB_ISOCHRONOUS
;
351 else if (pipeid1
== 'I')
352 urb_transfer
= URB_INTERRUPT
;
353 else if (pipeid1
== 'B')
354 urb_transfer
= URB_BULK
;
355 if (pipeid2
== 'i') {
356 urb_transfer
|= URB_TRANSFER_IN
;
360 incoming
= !incoming
;
365 if (handle
->direction
== PCAP_D_OUT
)
369 if (handle
->direction
== PCAP_D_IN
)
371 uhdr
->event_type
= etype
;
372 uhdr
->transfer_type
= urb_transfer
;
373 pkth
.caplen
= sizeof(pcap_usb_header
);
374 rawdata
+= sizeof(pcap_usb_header
);
376 /* check if this is a setup packet */
377 ret
= sscanf(status
, "%d", &dummy
);
380 /* this a setup packet, setup data can be filled with underscore if
381 * usbmon has not been able to read them, so we must parse this fields as
383 pcap_usb_setup
* shdr
;
384 char str1
[3], str2
[3], str3
[5], str4
[5], str5
[5];
385 ret
= sscanf(string
, "%s %s %s %s %s%n", str1
, str2
, str3
, str4
,
389 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
390 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
396 /* try to convert to corresponding integer */
398 shdr
->bmRequestType
= strtoul(str1
, 0, 16);
399 shdr
->bRequest
= strtoul(str2
, 0, 16);
400 shdr
->wValue
= htols(strtoul(str3
, 0, 16));
401 shdr
->wIndex
= htols(strtoul(str4
, 0, 16));
402 shdr
->wLength
= htols(strtoul(str5
, 0, 16));
404 uhdr
->setup_flag
= 0;
407 uhdr
->setup_flag
= 1;
410 ret
= sscanf(string
, " %d%n", &uhdr
->urb_len
, &cnt
);
413 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
414 "Can't parse urb length from '%s'", string
);
419 /* urb tag is not present if urb length is 0, so we can stop here
421 pkth
.len
= uhdr
->urb_len
+pkth
.caplen
;
424 if (uhdr
->urb_len
== pkth
.caplen
)
427 /* check for data presence; data is present if and only if urb tag is '=' */
428 if (sscanf(string
, " %c", &urb_tag
) != 1)
430 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
431 "Can't parse urb tag from '%s'", string
);
438 /* skip urb tag and following space */
441 /* if we reach this point we got some urb data*/
444 /* read all urb data; if urb length is greater then the usbmon internal
445 * buffer length used by the kernel to spool the URB, we get only
446 * a partial information.
447 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
448 * length and default value is 130. */
449 while ((string
[0] != 0) && (string
[1] != 0) && (pkth
.caplen
< handle
->snapshot
))
451 rawdata
[0] = ascii_to_int(string
[0]) * 16 + ascii_to_int(string
[1]);
454 if (string
[0] == ' ')
461 handle
->md
.packets_read
++;
462 if (pkth
.caplen
> handle
->snapshot
)
463 pkth
.caplen
= handle
->snapshot
;
465 callback(user
, &pkth
, handle
->buffer
);
470 usb_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
472 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on "
478 usb_close_linux(pcap_t
* handle
)
480 /* handle fill be freed in pcap_close() 'common' code */
483 free(handle
->buffer
);
487 usb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
489 int dummy
, ret
, consumed
, cnt
;
490 char string
[USB_LINE_LEN
];
491 char token
[USB_LINE_LEN
];
493 snprintf(string
, USB_LINE_LEN
, USB_DIR
"/%ds", handle
->md
.ifindex
);
495 int fd
= open(string
, O_RDONLY
, 0);
498 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
499 "Can't open USB stats file %s: %s",
500 string
, strerror(errno
));
504 /* read stats line */
506 ret
= read(fd
, string
, USB_LINE_LEN
-1);
507 } while ((ret
== -1) && (errno
== EINTR
));
512 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
513 "Can't read stats from fd %d ", fd
);
518 /* extract info on dropped urbs */
519 for (consumed
=0; consumed
< ret
; ) {
520 int ntok
= sscanf(ptr
, "%s%n", token
, &cnt
);
525 if (strcmp(token
, "nreaders") == 0)
526 ret
= sscanf(ptr
, "%d", &stats
->ps_drop
);
528 ret
= sscanf(ptr
, "%d", &dummy
);
535 stats
->ps_recv
= handle
->md
.packets_read
;
536 stats
->ps_ifdrop
= 0;
541 usb_setfilter_linux(pcap_t
*p
, struct bpf_program
*fp
)
547 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
555 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
558 struct mon_bin_stats st
;
559 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
562 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
563 "Can't read stats from fd %d:%s ", handle
->fd
, strerror(errno
));
567 stats
->ps_recv
= handle
->md
.packets_read
+ st
.queued
;
568 stats
->ps_ifdrop
= st
.dropped
;
573 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
574 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
577 usb_read_linux_bin(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
579 struct mon_bin_get info
;
581 struct pcap_pkthdr pkth
;
584 int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
586 /* the usb header is going to be part of 'packet' data*/
587 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
588 info
.data
= handle
->buffer
+ sizeof(pcap_usb_header
);
589 info
.data_len
= clen
;
590 /* ignore interrupt system call errors */
592 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
593 if (handle
->break_loop
)
595 handle
->break_loop
= 0;
598 } while ((ret
== -1) && (errno
== EINTR
));
602 return 0; /* no data there */
604 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
605 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
609 /* we can get less that than really captured from kernel, depending on
610 * snaplen, so adjust header accordingly */
611 if (info
.hdr
->data_len
< clen
)
612 clen
= info
.hdr
->data_len
;
613 info
.hdr
->data_len
= clen
;
614 pkth
.caplen
= clen
+ sizeof(pcap_usb_header
);
615 pkth
.len
= info
.hdr
->urb_len
+ sizeof(pcap_usb_header
);
616 pkth
.ts
.tv_sec
= info
.hdr
->ts_sec
;
617 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
619 handle
->md
.packets_read
++;
620 callback(user
, &pkth
, handle
->buffer
);
625 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
626 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
630 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
632 struct mon_bin_mfetch fetch
;
633 uint32_t vec
[VEC_SIZE
];
634 struct pcap_pkthdr pkth
;
635 pcap_usb_header
* hdr
;
641 int limit
= max_packets
- packets
;
644 if (limit
> VEC_SIZE
)
647 /* try to fetch as many events as possible*/
649 fetch
.nfetch
= limit
;
650 fetch
.nflush
= nflush
;
651 /* ignore interrupt system call errors */
653 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
654 if (handle
->break_loop
)
656 handle
->break_loop
= 0;
659 } while ((ret
== -1) && (errno
== EINTR
));
663 return 0; /* no data there */
665 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
666 "Can't mfetch fd %d: %s", handle
->fd
, strerror(errno
));
670 /* keep track of processed events, we will flush them later */
671 nflush
= fetch
.nfetch
;
672 for (i
=0; i
<fetch
.nfetch
; ++i
) {
674 hdr
= (pcap_usb_header
*) &handle
->buffer
[vec
[i
]];
675 if (hdr
->event_type
== '@')
678 /* get packet info from header*/
679 pkth
.caplen
= hdr
->data_len
+ sizeof(pcap_usb_header
);
680 pkth
.len
= hdr
->urb_len
+ sizeof(pcap_usb_header
);
681 pkth
.ts
.tv_sec
= hdr
->ts_sec
;
682 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
684 handle
->md
.packets_read
++;
685 callback(user
, &pkth
, (u_char
*) hdr
);
689 /* with max_packets == -1 we stop afer the first chunk*/
690 if ((max_packets
== -1) || (packets
== max_packets
))
694 /* flush pending events*/
695 ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
);
700 usb_close_linux_mmap(pcap_t
* handle
)
702 /* handle fill be freed in pcap_close() 'common' code, buffer must not
703 * be freed because it's memory mapped */