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