]>
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>
37 static const char rcsid
[] _U_
=
38 "@(#) $Header: /tcpdump/master/libpcap/pcap-usb-linux.c,v 1.15 2007-04-01 21:43:55 guy Exp $ (LBL)";
46 #include "pcap-usb-linux.h"
49 #ifdef NEED_STRERROR_H
61 #include <netinet/in.h>
62 #include <sys/ioctl.h>
65 #define USB_IFACE "usb"
66 #define USB_TEXT_DIR "/sys/kernel/debug/usbmon"
67 #define USB_BUS_DIR "/proc/bus/usb"
68 #define USB_LINE_LEN 4096
72 #define PIPE_ISOCHRONOUS 0
73 #define PIPE_INTERRUPT 1
74 #define PIPE_CONTROL 2
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
120 /* forward declaration */
121 static int usb_stats_linux(pcap_t
*, struct pcap_stat
*);
122 static int usb_stats_linux_bin(pcap_t
*, struct pcap_stat
*);
123 static int usb_read_linux(pcap_t
*, int , pcap_handler
, u_char
*);
124 static int usb_read_linux_bin(pcap_t
*, int , pcap_handler
, u_char
*);
125 static int usb_read_linux_mmap(pcap_t
*, int , pcap_handler
, u_char
*);
126 static int usb_inject_linux(pcap_t
*, const void *, size_t);
127 static int usb_setfilter_linux(pcap_t
*, struct bpf_program
*);
128 static int usb_setdirection_linux(pcap_t
*, pcap_direction_t
);
129 static void usb_close_linux(pcap_t
*);
130 static void usb_close_linux_mmap(pcap_t
*);
132 /* facility to add an USB device to the device list*/
134 usb_dev_add(pcap_if_t
** alldevsp
, int n
, char *err_str
)
138 snprintf(dev_name
, 10, USB_IFACE
"%d", n
);
139 snprintf(dev_descr
, 30, "USB bus number %d", n
);
141 if (pcap_add_if(alldevsp
, dev_name
, 0,
142 dev_descr
, err_str
) < 0)
148 usb_platform_finddevs(pcap_if_t
**alldevsp
, char *err_str
)
154 /* scan profs usb bus directorys */
155 dir
= opendir(USB_BUS_DIR
);
157 while ((ret
== 0) && ((data
= readdir(dir
)) != 0)) {
159 char* name
= data
->d_name
;
160 int len
= strlen(name
);
162 /* if this file name does not end with a number it's not of our interest */
163 if ((len
< 1) || !isdigit(name
[--len
]))
165 while (isdigit(name
[--len
]));
166 if (sscanf(&name
[len
+1], "%d", &n
) != 1)
169 ret
= usb_dev_add(alldevsp
, n
, err_str
);
177 int usb_mmap(pcap_t
* handle
)
179 int len
= ioctl(handle
->fd
, MON_IOCQ_RING_SIZE
);
183 handle
->buffer
= mmap(0, len
, PROT_READ
, MAP_SHARED
, handle
->fd
, 0);
184 return handle
->buffer
!= MAP_FAILED
;
188 usb_open_live(const char* bus
, int snaplen
, int promisc
, int to_ms
, char* errmsg
)
190 char full_path
[USB_LINE_LEN
];
193 /* Allocate a handle for this session. */
194 handle
= malloc(sizeof(*handle
));
195 if (handle
== NULL
) {
196 snprintf(errmsg
, PCAP_ERRBUF_SIZE
, "malloc: %s",
197 pcap_strerror(errno
));
201 /* Initialize some components of the pcap structure. */
202 memset(handle
, 0, sizeof(*handle
));
203 handle
->snapshot
= snaplen
;
204 handle
->md
.timeout
= to_ms
;
205 handle
->bufsize
= snaplen
;
207 handle
->linktype
= DLT_USB_LINUX
;
210 * "handle->fd" is a real file , so "select()" and "poll()"
213 handle
->selectable_fd
= handle
->fd
;
215 handle
->inject_op
= usb_inject_linux
;
216 handle
->setfilter_op
= usb_setfilter_linux
;
217 handle
->setdirection_op
= usb_setdirection_linux
;
218 handle
->set_datalink_op
= NULL
; /* can't change data link type */
219 handle
->getnonblock_op
= pcap_getnonblock_fd
;
220 handle
->setnonblock_op
= pcap_setnonblock_fd
;
221 handle
->close_op
= usb_close_linux
;
223 /*get usb bus index from device name */
224 if (sscanf(bus
, USB_IFACE
"%d", &handle
->md
.ifindex
) != 1)
226 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
227 "Can't get USB bus index from %s", bus
);
232 /*now select the read method: try to open binary interface */
233 snprintf(full_path
, USB_LINE_LEN
, LINUX_USB_MON_DEV
"%d", handle
->md
.ifindex
);
234 handle
->fd
= open(full_path
, O_RDONLY
, 0);
237 /* header endianess can't be fixed for memory mapped access,
238 * due to read only access to mmaped buffer, so disable it*/
239 /* binary api is available, try to use fast mmap access */
240 if (usb_mmap(handle
)) {
241 handle
->stats_op
= usb_stats_linux_bin
;
242 handle
->read_op
= usb_read_linux_mmap
;
243 handle
->close_op
= usb_close_linux_mmap
;
247 /* can't mmap, use plain binary interface access */
248 handle
->stats_op
= usb_stats_linux_bin
;
249 handle
->read_op
= usb_read_linux_bin
;
252 /*Binary interface not available, try open text interface */
253 snprintf(full_path
, USB_LINE_LEN
, USB_TEXT_DIR
"/%dt", handle
->md
.ifindex
);
254 handle
->fd
= open(full_path
, O_RDONLY
, 0);
257 /* no more fallback, give it up*/
258 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
259 "Can't open USB bus file %s: %s", full_path
, strerror(errno
));
263 handle
->stats_op
= usb_stats_linux
;
264 handle
->read_op
= usb_read_linux
;
267 /* for plain binary access and text access we need to allocate the read
269 handle
->buffer
= malloc(handle
->bufsize
);
270 if (!handle
->buffer
) {
271 snprintf(errmsg
, PCAP_ERRBUF_SIZE
,
272 "malloc: %s", pcap_strerror(errno
));
273 usb_close_linux(handle
);
282 return c
< 'A' ? c
- '0': ((c
<'a') ? c
- 'A' + 10: c
-'a'+10);
286 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
287 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
291 usb_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
294 * /usr/src/linux/Documentation/usb/usbmon.txt
298 int tag
, cnt
, ep_num
, dev_addr
, dummy
, ret
, urb_len
, data_len
;
299 char etype
, pipeid1
, pipeid2
, status
[16], urb_tag
, line
[USB_LINE_LEN
];
301 u_char
* rawdata
= handle
->buffer
;
302 struct pcap_pkthdr pkth
;
303 pcap_usb_header
* uhdr
= (pcap_usb_header
*)handle
->buffer
;
304 u_char urb_transfer
=0;
307 /* ignore interrupt system call errors */
309 ret
= read(handle
->fd
, line
, USB_LINE_LEN
- 1);
310 if (handle
->break_loop
)
312 handle
->break_loop
= 0;
315 } while ((ret
== -1) && (errno
== EINTR
));
319 return 0; /* no data there */
321 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
322 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
326 /* read urb header; %n argument may increment return value, but it's
327 * not mandatory, so does not count on it*/
329 ret
= sscanf(string
, "%x %d %c %c%c:%d:%d %s%n", &tag
, ×tamp
, &etype
,
330 &pipeid1
, &pipeid2
, &dev_addr
, &ep_num
, status
,
334 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
335 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
340 uhdr
->endpoint_number
= ep_num
;
341 uhdr
->device_address
= dev_addr
;
342 uhdr
->bus_id
= handle
->md
.ifindex
;
346 /* don't use usbmon provided timestamp, since it have low precision*/
347 if (gettimeofday(&pkth
.ts
, NULL
) < 0)
349 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
350 "Can't get timestamp for message '%s' %d:%s",
351 string
, errno
, strerror(errno
));
354 uhdr
->ts_sec
= pkth
.ts
.tv_sec
;
355 uhdr
->ts_usec
= pkth
.ts
.tv_usec
;
357 /* parse endpoint information */
359 urb_transfer
= URB_CONTROL
;
360 else if (pipeid1
== 'Z')
361 urb_transfer
= URB_ISOCHRONOUS
;
362 else if (pipeid1
== 'I')
363 urb_transfer
= URB_INTERRUPT
;
364 else if (pipeid1
== 'B')
365 urb_transfer
= URB_BULK
;
366 if (pipeid2
== 'i') {
367 urb_transfer
|= URB_TRANSFER_IN
;
371 incoming
= !incoming
;
376 if (handle
->direction
== PCAP_D_OUT
)
380 if (handle
->direction
== PCAP_D_IN
)
382 uhdr
->event_type
= etype
;
383 uhdr
->transfer_type
= urb_transfer
;
384 pkth
.caplen
= sizeof(pcap_usb_header
);
385 rawdata
+= sizeof(pcap_usb_header
);
387 /* check if this is a setup packet */
388 ret
= sscanf(status
, "%d", &dummy
);
391 /* this a setup packet, setup data can be filled with underscore if
392 * usbmon has not been able to read them, so we must parse this fields as
394 pcap_usb_setup
* shdr
;
395 char str1
[3], str2
[3], str3
[5], str4
[5], str5
[5];
396 ret
= sscanf(string
, "%s %s %s %s %s%n", str1
, str2
, str3
, str4
,
400 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
401 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
407 /* try to convert to corresponding integer */
409 shdr
->bmRequestType
= strtoul(str1
, 0, 16);
410 shdr
->bRequest
= strtoul(str2
, 0, 16);
411 shdr
->wValue
= htols(strtoul(str3
, 0, 16));
412 shdr
->wIndex
= htols(strtoul(str4
, 0, 16));
413 shdr
->wLength
= htols(strtoul(str5
, 0, 16));
415 uhdr
->setup_flag
= 0;
418 uhdr
->setup_flag
= 1;
421 ret
= sscanf(string
, " %d%n", &urb_len
, &cnt
);
424 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
425 "Can't parse urb length from '%s'", string
);
430 /* urb tag is not present if urb length is 0, so we can stop here
432 pkth
.len
= urb_len
+pkth
.caplen
;
433 uhdr
->urb_len
= urb_len
;
436 if (uhdr
->urb_len
== pkth
.caplen
)
439 /* check for data presence; data is present if and only if urb tag is '=' */
440 if (sscanf(string
, " %c", &urb_tag
) != 1)
442 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
443 "Can't parse urb tag from '%s'", string
);
450 /* skip urb tag and following space */
453 /* if we reach this point we got some urb data*/
456 /* read all urb data; if urb length is greater then the usbmon internal
457 * buffer length used by the kernel to spool the URB, we get only
458 * a partial information.
459 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
460 * length and default value is 130. */
461 while ((string
[0] != 0) && (string
[1] != 0) && (pkth
.caplen
< handle
->snapshot
))
463 rawdata
[0] = ascii_to_int(string
[0]) * 16 + ascii_to_int(string
[1]);
466 if (string
[0] == ' ')
473 uhdr
->data_len
= data_len
;
474 handle
->md
.packets_read
++;
475 if (pkth
.caplen
> handle
->snapshot
)
476 pkth
.caplen
= handle
->snapshot
;
478 callback(user
, &pkth
, handle
->buffer
);
483 usb_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
485 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on "
491 usb_close_linux(pcap_t
* handle
)
493 /* handle fill be freed in pcap_close() 'common' code */
496 free(handle
->buffer
);
500 usb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
502 int dummy
, ret
, consumed
, cnt
;
503 char string
[USB_LINE_LEN
];
504 char token
[USB_LINE_LEN
];
506 snprintf(string
, USB_LINE_LEN
, USB_TEXT_DIR
"/%ds", handle
->md
.ifindex
);
508 int fd
= open(string
, O_RDONLY
, 0);
511 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
512 "Can't open USB stats file %s: %s",
513 string
, strerror(errno
));
517 /* read stats line */
519 ret
= read(fd
, string
, USB_LINE_LEN
-1);
520 } while ((ret
== -1) && (errno
== EINTR
));
525 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
526 "Can't read stats from fd %d ", fd
);
531 /* extract info on dropped urbs */
532 for (consumed
=0; consumed
< ret
; ) {
533 int ntok
= sscanf(ptr
, "%s%n", token
, &cnt
);
538 if (strcmp(token
, "nreaders") == 0)
539 ret
= sscanf(ptr
, "%d", &stats
->ps_drop
);
541 ret
= sscanf(ptr
, "%d", &dummy
);
548 stats
->ps_recv
= handle
->md
.packets_read
;
549 stats
->ps_ifdrop
= 0;
554 usb_setfilter_linux(pcap_t
*p
, struct bpf_program
*fp
)
560 usb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
568 usb_stats_linux_bin(pcap_t
*handle
, struct pcap_stat
*stats
)
571 struct mon_bin_stats st
;
572 ret
= ioctl(handle
->fd
, MON_IOCG_STATS
, &st
);
575 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
576 "Can't read stats from fd %d:%s ", handle
->fd
, strerror(errno
));
580 stats
->ps_recv
= handle
->md
.packets_read
+ st
.queued
;
581 stats
->ps_ifdrop
= st
.dropped
;
586 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
587 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
590 usb_read_linux_bin(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
592 struct mon_bin_get info
;
594 struct pcap_pkthdr pkth
;
595 int clen
= handle
->snapshot
- sizeof(pcap_usb_header
);
597 /* the usb header is going to be part of 'packet' data*/
598 info
.hdr
= (pcap_usb_header
*) handle
->buffer
;
599 info
.data
= handle
->buffer
+ sizeof(pcap_usb_header
);
600 info
.data_len
= clen
;
601 /* ignore interrupt system call errors */
603 ret
= ioctl(handle
->fd
, MON_IOCX_GET
, &info
);
604 if (handle
->break_loop
)
606 handle
->break_loop
= 0;
609 } while ((ret
== -1) && (errno
== EINTR
));
613 return 0; /* no data there */
615 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
616 "Can't read from fd %d: %s", handle
->fd
, strerror(errno
));
620 /* we can get less that than really captured from kernel, depending on
621 * snaplen, so adjust header accordingly */
622 if (info
.hdr
->data_len
< clen
)
623 clen
= info
.hdr
->data_len
;
624 info
.hdr
->data_len
= clen
;
625 pkth
.caplen
= clen
+ sizeof(pcap_usb_header
);
626 pkth
.len
= info
.hdr
->urb_len
+ sizeof(pcap_usb_header
);
627 pkth
.ts
.tv_sec
= info
.hdr
->ts_sec
;
628 pkth
.ts
.tv_usec
= info
.hdr
->ts_usec
;
630 handle
->md
.packets_read
++;
631 callback(user
, &pkth
, handle
->buffer
);
636 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
637 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
641 usb_read_linux_mmap(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
643 struct mon_bin_mfetch fetch
;
644 int32_t vec
[VEC_SIZE
];
645 struct pcap_pkthdr pkth
;
646 pcap_usb_header
* hdr
;
652 int limit
= max_packets
- packets
;
655 if (limit
> VEC_SIZE
)
658 /* try to fetch as many events as possible*/
660 fetch
.nfetch
= limit
;
661 fetch
.nflush
= nflush
;
662 /* ignore interrupt system call errors */
664 ret
= ioctl(handle
->fd
, MON_IOCX_MFETCH
, &fetch
);
665 if (handle
->break_loop
)
667 handle
->break_loop
= 0;
670 } while ((ret
== -1) && (errno
== EINTR
));
674 return 0; /* no data there */
676 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
677 "Can't mfetch fd %d: %s", handle
->fd
, strerror(errno
));
681 /* keep track of processed events, we will flush them later */
682 nflush
= fetch
.nfetch
;
683 for (i
=0; i
<fetch
.nfetch
; ++i
) {
685 hdr
= (pcap_usb_header
*) &handle
->buffer
[vec
[i
]];
686 if (hdr
->event_type
== '@')
689 /* get packet info from header*/
690 pkth
.caplen
= hdr
->data_len
+ sizeof(pcap_usb_header
);
691 pkth
.len
= hdr
->urb_len
+ sizeof(pcap_usb_header
);
692 pkth
.ts
.tv_sec
= hdr
->ts_sec
;
693 pkth
.ts
.tv_usec
= hdr
->ts_usec
;
695 handle
->md
.packets_read
++;
696 callback(user
, &pkth
, (u_char
*) hdr
);
700 /* with max_packets == -1 we stop afer the first chunk*/
701 if ((max_packets
== -1) || (packets
== max_packets
))
705 /* flush pending events*/
706 ioctl(handle
->fd
, MON_IOCH_MFLUSH
, nflush
);
711 usb_close_linux_mmap(pcap_t
* handle
)
713 /* handle fill be freed in pcap_close() 'common' code, buffer must not
714 * be freed because it's memory mapped */