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