]> The Tcpdump Group git mirrors - libpcap/blob - pcap-usb-linux.c
Update CHANGES for USB fixes.
[libpcap] / pcap-usb-linux.c
1 /*
2 * Copyright (c) 2006 Paolo Abeni (Italy)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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
16 * permission.
17 *
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.
29 *
30 * USB sniffing API implementation for Linux platform
31 * By Paolo Abeni <paolo.abeni@email.it>
32 * Modifications: Kris Katterjohn <katterjohn@gmail.com>
33 *
34 */
35 #ifndef lint
36 static const char rcsid[] _U_ =
37 "@(#) $Header: /tcpdump/master/libpcap/pcap-usb-linux.c,v 1.16.2.14 2008-12-23 20:14:13 guy Exp $ (LBL)";
38 #endif
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #include "pcap-int.h"
45 #include "pcap-usb-linux.h"
46 #include "pcap/usb.h"
47
48 #ifdef NEED_STRERROR_H
49 #include "strerror.h"
50 #endif
51
52 #include <ctype.h>
53 #include <errno.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <fcntl.h>
57 #include <string.h>
58 #include <dirent.h>
59 #include <byteswap.h>
60 #include <netinet/in.h>
61 #include <sys/ioctl.h>
62 #include <sys/mman.h>
63
64 #define USB_IFACE "usb"
65 #define USB_TEXT_DIR "/sys/kernel/debug/usbmon"
66 #define SYS_USB_BUS_DIR "/sys/bus/usb/devices"
67 #define PROC_USB_BUS_DIR "/proc/bus/usb"
68 #define USB_LINE_LEN 4096
69
70 #if __BYTE_ORDER == __LITTLE_ENDIAN
71 #define htols(s) s
72 #define htoll(l) l
73 #define htol64(ll) ll
74 #else
75 #define htols(s) bswap_16(s)
76 #define htoll(l) bswap_32(l)
77 #define htol64(ll) bswap_64(ll)
78 #endif
79
80 struct mon_bin_stats {
81 u_int32_t queued;
82 u_int32_t dropped;
83 };
84
85 struct mon_bin_get {
86 pcap_usb_header *hdr;
87 void *data;
88 size_t data_len; /* Length of data (can be zero) */
89 };
90
91 struct mon_bin_mfetch {
92 int32_t *offvec; /* Vector of events fetched */
93 int32_t nfetch; /* Number of events to fetch (out: fetched) */
94 int32_t nflush; /* Number of events to flush */
95 };
96
97 #define MON_IOC_MAGIC 0x92
98
99 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
100 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
101 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
102 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
103 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
104 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
105 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
106 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
107
108 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
109 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
110 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
111 #define MON_BIN_ERROR 0x8
112
113 /* forward declaration */
114 static int usb_activate(pcap_t *);
115 static int usb_stats_linux(pcap_t *, struct pcap_stat *);
116 static int usb_stats_linux_bin(pcap_t *, struct pcap_stat *);
117 static int usb_read_linux(pcap_t *, int , pcap_handler , u_char *);
118 static int usb_read_linux_bin(pcap_t *, int , pcap_handler , u_char *);
119 static int usb_read_linux_mmap(pcap_t *, int , pcap_handler , u_char *);
120 static int usb_inject_linux(pcap_t *, const void *, size_t);
121 static int usb_setfilter_linux(pcap_t *, struct bpf_program *);
122 static int usb_setdirection_linux(pcap_t *, pcap_direction_t);
123 static void usb_cleanup_linux_mmap(pcap_t *);
124
125 /* facility to add an USB device to the device list*/
126 static int
127 usb_dev_add(pcap_if_t** alldevsp, int n, char *err_str)
128 {
129 char dev_name[10];
130 char dev_descr[30];
131 snprintf(dev_name, 10, USB_IFACE"%d", n);
132 snprintf(dev_descr, 30, "USB bus number %d", n);
133
134 if (pcap_add_if(alldevsp, dev_name, 0,
135 dev_descr, err_str) < 0)
136 return -1;
137 return 0;
138 }
139
140 int
141 usb_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
142 {
143 struct dirent* data;
144 int ret = 0;
145 DIR* dir;
146 int n;
147 char* name;
148 size_t len;
149
150 /* try scanning sysfs usb bus directory */
151 dir = opendir(SYS_USB_BUS_DIR);
152 if (dir != NULL) {
153 while ((ret == 0) && ((data = readdir(dir)) != 0)) {
154 name = data->d_name;
155
156 if (strncmp(name, "usb", 3) != 0)
157 continue;
158
159 if (sscanf(&name[3], "%d", &n) == 0)
160 continue;
161
162 ret = usb_dev_add(alldevsp, n, err_str);
163 }
164
165 closedir(dir);
166 return ret;
167 }
168
169 /* that didn't work; try scanning procfs usb bus directory */
170 dir = opendir(PROC_USB_BUS_DIR);
171 if (dir != NULL) {
172 while ((ret == 0) && ((data = readdir(dir)) != 0)) {
173 name = data->d_name;
174 len = strlen(name);
175
176 /* if this file name does not end with a number it's not of our interest */
177 if ((len < 1) || !isdigit(name[--len]))
178 continue;
179 while (isdigit(name[--len]));
180 if (sscanf(&name[len+1], "%d", &n) != 1)
181 continue;
182
183 ret = usb_dev_add(alldevsp, n, err_str);
184 }
185
186 closedir(dir);
187 return ret;
188 }
189
190 /* neither of them worked */
191 return 0;
192 }
193
194 static
195 int usb_mmap(pcap_t* handle)
196 {
197 int len = ioctl(handle->fd, MON_IOCQ_RING_SIZE);
198 if (len < 0)
199 return 0;
200
201 handle->buffer = mmap(0, len, PROT_READ, MAP_SHARED, handle->fd, 0);
202 return handle->buffer != MAP_FAILED;
203 }
204
205 pcap_t *
206 usb_create(const char *device, char *ebuf)
207 {
208 pcap_t *p;
209
210 p = pcap_create_common(device, ebuf);
211 if (p == NULL)
212 return (NULL);
213
214 p->activate_op = usb_activate;
215 return (p);
216 }
217
218 static int
219 usb_activate(pcap_t* handle)
220 {
221 char full_path[USB_LINE_LEN];
222
223 /* Initialize some components of the pcap structure. */
224 handle->bufsize = handle->snapshot;
225 handle->offset = 0;
226 handle->linktype = DLT_USB_LINUX;
227
228 handle->inject_op = usb_inject_linux;
229 handle->setfilter_op = usb_setfilter_linux;
230 handle->setdirection_op = usb_setdirection_linux;
231 handle->set_datalink_op = NULL; /* can't change data link type */
232 handle->getnonblock_op = pcap_getnonblock_fd;
233 handle->setnonblock_op = pcap_setnonblock_fd;
234
235 /*get usb bus index from device name */
236 if (sscanf(handle->opt.source, USB_IFACE"%d", &handle->md.ifindex) != 1)
237 {
238 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
239 "Can't get USB bus index from %s", handle->opt.source);
240 return PCAP_ERROR;
241 }
242
243 /*now select the read method: try to open binary interface */
244 snprintf(full_path, USB_LINE_LEN, LINUX_USB_MON_DEV"%d", handle->md.ifindex);
245 handle->fd = open(full_path, O_RDONLY, 0);
246 if (handle->fd >= 0)
247 {
248 if (handle->opt.rfmon) {
249 /*
250 * Monitor mode doesn't apply to USB devices.
251 */
252 close(handle->fd);
253 return PCAP_ERROR_RFMON_NOTSUP;
254 }
255
256 /* binary api is available, try to use fast mmap access */
257 if (usb_mmap(handle)) {
258 handle->linktype = DLT_USB_LINUX_MMAPPED;
259 handle->stats_op = usb_stats_linux_bin;
260 handle->read_op = usb_read_linux_mmap;
261 handle->cleanup_op = usb_cleanup_linux_mmap;
262
263 /*
264 * "handle->fd" is a real file, so "select()" and
265 * "poll()" work on it.
266 */
267 handle->selectable_fd = handle->fd;
268 return 0;
269 }
270
271 /* can't mmap, use plain binary interface access */
272 handle->stats_op = usb_stats_linux_bin;
273 handle->read_op = usb_read_linux_bin;
274 }
275 else {
276 /*Binary interface not available, try open text interface */
277 snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR"/%dt", handle->md.ifindex);
278 handle->fd = open(full_path, O_RDONLY, 0);
279 if (handle->fd < 0)
280 {
281 /* no more fallback, give it up*/
282 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
283 "Can't open USB bus file %s: %s", full_path, strerror(errno));
284 return PCAP_ERROR;
285 }
286
287 if (handle->opt.rfmon) {
288 /*
289 * Monitor mode doesn't apply to USB devices.
290 */
291 close(handle->fd);
292 return PCAP_ERROR_RFMON_NOTSUP;
293 }
294
295 handle->stats_op = usb_stats_linux;
296 handle->read_op = usb_read_linux;
297 }
298
299 /*
300 * "handle->fd" is a real file, so "select()" and "poll()"
301 * work on it.
302 */
303 handle->selectable_fd = handle->fd;
304
305 /* for plain binary access and text access we need to allocate the read
306 * buffer */
307 handle->buffer = malloc(handle->bufsize);
308 if (!handle->buffer) {
309 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
310 "malloc: %s", pcap_strerror(errno));
311 return PCAP_ERROR;
312 }
313 return 0;
314 }
315
316 static inline int
317 ascii_to_int(char c)
318 {
319 return c < 'A' ? c- '0': ((c<'a') ? c - 'A' + 10: c-'a'+10);
320 }
321
322 /*
323 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
324 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
325 * format description
326 */
327 static int
328 usb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
329 {
330 /* see:
331 * /usr/src/linux/Documentation/usb/usbmon.txt
332 * for message format
333 */
334 unsigned timestamp;
335 int tag, cnt, ep_num, dev_addr, dummy, ret, urb_len, data_len;
336 char etype, pipeid1, pipeid2, status[16], urb_tag, line[USB_LINE_LEN];
337 char *string = line;
338 u_char * rawdata = handle->buffer;
339 struct pcap_pkthdr pkth;
340 pcap_usb_header* uhdr = (pcap_usb_header*)handle->buffer;
341 u_char urb_transfer=0;
342 int incoming=0;
343
344 /* ignore interrupt system call errors */
345 do {
346 ret = read(handle->fd, line, USB_LINE_LEN - 1);
347 if (handle->break_loop)
348 {
349 handle->break_loop = 0;
350 return -2;
351 }
352 } while ((ret == -1) && (errno == EINTR));
353 if (ret < 0)
354 {
355 if (errno == EAGAIN)
356 return 0; /* no data there */
357
358 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
359 "Can't read from fd %d: %s", handle->fd, strerror(errno));
360 return -1;
361 }
362
363 /* read urb header; %n argument may increment return value, but it's
364 * not mandatory, so does not count on it*/
365 string[ret] = 0;
366 ret = sscanf(string, "%x %d %c %c%c:%d:%d %s%n", &tag, &timestamp, &etype,
367 &pipeid1, &pipeid2, &dev_addr, &ep_num, status,
368 &cnt);
369 if (ret < 8)
370 {
371 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
372 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
373 string, ret);
374 return -1;
375 }
376 uhdr->id = tag;
377 uhdr->device_address = dev_addr;
378 uhdr->bus_id = handle->md.ifindex;
379 uhdr->status = 0;
380 string += cnt;
381
382 /* don't use usbmon provided timestamp, since it have low precision*/
383 if (gettimeofday(&pkth.ts, NULL) < 0)
384 {
385 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
386 "Can't get timestamp for message '%s' %d:%s",
387 string, errno, strerror(errno));
388 return -1;
389 }
390 uhdr->ts_sec = pkth.ts.tv_sec;
391 uhdr->ts_usec = pkth.ts.tv_usec;
392
393 /* parse endpoint information */
394 if (pipeid1 == 'C')
395 urb_transfer = URB_CONTROL;
396 else if (pipeid1 == 'Z')
397 urb_transfer = URB_ISOCHRONOUS;
398 else if (pipeid1 == 'I')
399 urb_transfer = URB_INTERRUPT;
400 else if (pipeid1 == 'B')
401 urb_transfer = URB_BULK;
402 if (pipeid2 == 'i') {
403 ep_num |= URB_TRANSFER_IN;
404 incoming = 1;
405 }
406 if (etype == 'C')
407 incoming = !incoming;
408
409 /* direction check*/
410 if (incoming)
411 {
412 if (handle->direction == PCAP_D_OUT)
413 return 0;
414 }
415 else
416 if (handle->direction == PCAP_D_IN)
417 return 0;
418 uhdr->event_type = etype;
419 uhdr->transfer_type = urb_transfer;
420 uhdr->endpoint_number = ep_num;
421 pkth.caplen = sizeof(pcap_usb_header);
422 rawdata += sizeof(pcap_usb_header);
423
424 /* check if this is a setup packet */
425 ret = sscanf(status, "%d", &dummy);
426 if (ret != 1)
427 {
428 /* this a setup packet, setup data can be filled with underscore if
429 * usbmon has not been able to read them, so we must parse this fields as
430 * strings */
431 pcap_usb_setup* shdr;
432 char str1[3], str2[3], str3[5], str4[5], str5[5];
433 ret = sscanf(string, "%s %s %s %s %s%n", str1, str2, str3, str4,
434 str5, &cnt);
435 if (ret < 5)
436 {
437 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
438 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
439 string, ret);
440 return -1;
441 }
442 string += cnt;
443
444 /* try to convert to corresponding integer */
445 shdr = &uhdr->setup;
446 shdr->bmRequestType = strtoul(str1, 0, 16);
447 shdr->bRequest = strtoul(str2, 0, 16);
448 shdr->wValue = htols(strtoul(str3, 0, 16));
449 shdr->wIndex = htols(strtoul(str4, 0, 16));
450 shdr->wLength = htols(strtoul(str5, 0, 16));
451
452 uhdr->setup_flag = 0;
453 }
454 else
455 uhdr->setup_flag = 1;
456
457 /* read urb data */
458 ret = sscanf(string, " %d%n", &urb_len, &cnt);
459 if (ret < 1)
460 {
461 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
462 "Can't parse urb length from '%s'", string);
463 return -1;
464 }
465 string += cnt;
466
467 /* urb tag is not present if urb length is 0, so we can stop here
468 * text parsing */
469 pkth.len = urb_len+pkth.caplen;
470 uhdr->urb_len = urb_len;
471 uhdr->data_flag = 1;
472 data_len = 0;
473 if (uhdr->urb_len == 0)
474 goto got;
475
476 /* check for data presence; data is present if and only if urb tag is '=' */
477 if (sscanf(string, " %c", &urb_tag) != 1)
478 {
479 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
480 "Can't parse urb tag from '%s'", string);
481 return -1;
482 }
483
484 if (urb_tag != '=')
485 goto got;
486
487 /* skip urb tag and following space */
488 string += 3;
489
490 /* if we reach this point we got some urb data*/
491 uhdr->data_flag = 0;
492
493 /* read all urb data; if urb length is greater then the usbmon internal
494 * buffer length used by the kernel to spool the URB, we get only
495 * a partial information.
496 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
497 * length and default value is 130. */
498 while ((string[0] != 0) && (string[1] != 0) && (pkth.caplen < handle->snapshot))
499 {
500 rawdata[0] = ascii_to_int(string[0]) * 16 + ascii_to_int(string[1]);
501 rawdata++;
502 string+=2;
503 if (string[0] == ' ')
504 string++;
505 pkth.caplen++;
506 data_len++;
507 }
508
509 got:
510 uhdr->data_len = data_len;
511 handle->md.packets_read++;
512 if (pkth.caplen > handle->snapshot)
513 pkth.caplen = handle->snapshot;
514
515 callback(user, &pkth, handle->buffer);
516 return 1;
517 }
518
519 static int
520 usb_inject_linux(pcap_t *handle, const void *buf, size_t size)
521 {
522 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
523 "USB devices");
524 return (-1);
525 }
526
527 static int
528 usb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
529 {
530 int dummy, ret, consumed, cnt;
531 char string[USB_LINE_LEN];
532 char token[USB_LINE_LEN];
533 char * ptr = string;
534 snprintf(string, USB_LINE_LEN, USB_TEXT_DIR"/%ds", handle->md.ifindex);
535
536 int fd = open(string, O_RDONLY, 0);
537 if (fd < 0)
538 {
539 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
540 "Can't open USB stats file %s: %s",
541 string, strerror(errno));
542 return -1;
543 }
544
545 /* read stats line */
546 do {
547 ret = read(fd, string, USB_LINE_LEN-1);
548 } while ((ret == -1) && (errno == EINTR));
549 close(fd);
550
551 if (ret < 0)
552 {
553 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
554 "Can't read stats from fd %d ", fd);
555 return -1;
556 }
557 string[ret] = 0;
558
559 /* extract info on dropped urbs */
560 for (consumed=0; consumed < ret; ) {
561 /* from the sscanf man page:
562 * The C standard says: "Execution of a %n directive does
563 * not increment the assignment count returned at the completion
564 * of execution" but the Corrigendum seems to contradict this.
565 * Do not make any assumptions on the effect of %n conversions
566 * on the return value and explicitly check for cnt assignmet*/
567 cnt = -1;
568 int ntok = sscanf(ptr, "%s%n", token, &cnt);
569 if ((ntok < 1) || (cnt < 0))
570 break;
571 consumed += cnt;
572 ptr += cnt;
573 if (strcmp(token, "nreaders") == 0)
574 ret = sscanf(ptr, "%d", &stats->ps_drop);
575 else
576 ret = sscanf(ptr, "%d", &dummy);
577 if (ntok != 1)
578 break;
579 consumed += cnt;
580 ptr += cnt;
581 }
582
583 stats->ps_recv = handle->md.packets_read;
584 stats->ps_ifdrop = 0;
585 return 0;
586 }
587
588 static int
589 usb_setfilter_linux(pcap_t *p, struct bpf_program *fp)
590 {
591 return 0;
592 }
593
594 static int
595 usb_setdirection_linux(pcap_t *p, pcap_direction_t d)
596 {
597 p->direction = d;
598 return 0;
599 }
600
601
602 static int
603 usb_stats_linux_bin(pcap_t *handle, struct pcap_stat *stats)
604 {
605 int ret;
606 struct mon_bin_stats st;
607 ret = ioctl(handle->fd, MON_IOCG_STATS, &st);
608 if (ret < 0)
609 {
610 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
611 "Can't read stats from fd %d:%s ", handle->fd, strerror(errno));
612 return -1;
613 }
614
615 stats->ps_recv = handle->md.packets_read + st.queued;
616 stats->ps_ifdrop = 0;
617 stats->ps_drop = st.dropped;
618 return 0;
619 }
620
621 /*
622 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
623 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
624 */
625 static int
626 usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
627 {
628 struct mon_bin_get info;
629 int ret;
630 struct pcap_pkthdr pkth;
631 int clen = handle->snapshot - sizeof(pcap_usb_header);
632
633 /* the usb header is going to be part of 'packet' data*/
634 info.hdr = (pcap_usb_header*) handle->buffer;
635 info.data = handle->buffer + sizeof(pcap_usb_header);
636 info.data_len = clen;
637
638 /* ignore interrupt system call errors */
639 do {
640 ret = ioctl(handle->fd, MON_IOCX_GET, &info);
641 if (handle->break_loop)
642 {
643 handle->break_loop = 0;
644 return -2;
645 }
646 } while ((ret == -1) && (errno == EINTR));
647 if (ret < 0)
648 {
649 if (errno == EAGAIN)
650 return 0; /* no data there */
651
652 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
653 "Can't read from fd %d: %s", handle->fd, strerror(errno));
654 return -1;
655 }
656
657 /* we can get less that than really captured from kernel, depending on
658 * snaplen, so adjust header accordingly */
659 if (info.hdr->data_len < clen)
660 clen = info.hdr->data_len;
661 info.hdr->data_len = clen;
662 pkth.caplen = clen + sizeof(pcap_usb_header);
663 pkth.len = info.hdr->data_len + sizeof(pcap_usb_header);
664 pkth.ts.tv_sec = info.hdr->ts_sec;
665 pkth.ts.tv_usec = info.hdr->ts_usec;
666
667 handle->md.packets_read++;
668 callback(user, &pkth, handle->buffer);
669 return 1;
670 }
671
672 /*
673 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
674 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
675 */
676 #define VEC_SIZE 32
677 static int
678 usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
679 {
680 struct mon_bin_mfetch fetch;
681 int32_t vec[VEC_SIZE];
682 struct pcap_pkthdr pkth;
683 pcap_usb_header* hdr;
684 int nflush = 0;
685 int packets = 0;
686 int clen, max_clen;
687
688 max_clen = handle->snapshot - sizeof(pcap_usb_header);
689
690 for (;;) {
691 int i, ret;
692 int limit = max_packets - packets;
693 if (limit <= 0)
694 limit = VEC_SIZE;
695 if (limit > VEC_SIZE)
696 limit = VEC_SIZE;
697
698 /* try to fetch as many events as possible*/
699 fetch.offvec = vec;
700 fetch.nfetch = limit;
701 fetch.nflush = nflush;
702 /* ignore interrupt system call errors */
703 do {
704 ret = ioctl(handle->fd, MON_IOCX_MFETCH, &fetch);
705 if (handle->break_loop)
706 {
707 handle->break_loop = 0;
708 return -2;
709 }
710 } while ((ret == -1) && (errno == EINTR));
711 if (ret < 0)
712 {
713 if (errno == EAGAIN)
714 return 0; /* no data there */
715
716 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
717 "Can't mfetch fd %d: %s", handle->fd, strerror(errno));
718 return -1;
719 }
720
721 /* keep track of processed events, we will flush them later */
722 nflush = fetch.nfetch;
723 for (i=0; i<fetch.nfetch; ++i) {
724 /* discard filler */
725 hdr = (pcap_usb_header*) &handle->buffer[vec[i]];
726 if (hdr->event_type == '@')
727 continue;
728
729 /* we can get less that than really captured from kernel, depending on
730 * snaplen, so adjust header accordingly */
731 clen = max_clen;
732 if (hdr->data_len < clen)
733 clen = hdr->data_len;
734
735 /* get packet info from header*/
736 pkth.caplen = clen + sizeof(pcap_usb_header_mmapped);
737 pkth.len = hdr->data_len + sizeof(pcap_usb_header_mmapped);
738 pkth.ts.tv_sec = hdr->ts_sec;
739 pkth.ts.tv_usec = hdr->ts_usec;
740
741 handle->md.packets_read++;
742 callback(user, &pkth, (u_char*) hdr);
743 packets++;
744 }
745
746 /* with max_packets <= 0 we stop afer the first chunk*/
747 if ((max_packets <= 0) || (packets == max_packets))
748 break;
749 }
750
751 /* flush pending events*/
752 ioctl(handle->fd, MON_IOCH_MFLUSH, nflush);
753 return packets;
754 }
755
756 static void
757 usb_cleanup_linux_mmap(pcap_t* handle)
758 {
759 /* buffer must not be freed because it's memory mapped */
760 /* XXX - does it need to be unmapped? */
761 handle->buffer = NULL;
762 pcap_cleanup_live_common(handle);
763 }