]> The Tcpdump Group git mirrors - libpcap/blob - pcap-usb-linux.c
Probe CONFIGURATION descriptor of connected USB devices
[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
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "pcap-int.h"
41 #include "pcap-usb-linux.h"
42 #include "pcap/usb.h"
43
44 #ifdef NEED_STRERROR_H
45 #include "strerror.h"
46 #endif
47
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <limits.h>
53 #include <string.h>
54 #include <dirent.h>
55 #include <byteswap.h>
56 #include <netinet/in.h>
57 #include <sys/ioctl.h>
58 #include <sys/mman.h>
59 #include <sys/utsname.h>
60 #ifdef HAVE_LINUX_USBDEVICE_FS_H
61 /*
62 * We might need <linux/compiler.h> to define __user for
63 * <linux/usbdevice_fs.h>.
64 */
65 #ifdef HAVE_LINUX_COMPILER_H
66 #include <linux/compiler.h>
67 #endif /* HAVE_LINUX_COMPILER_H */
68 #include <linux/usbdevice_fs.h>
69 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
70
71 #define USB_IFACE "usbmon"
72 #define USB_TEXT_DIR_OLD "/sys/kernel/debug/usbmon"
73 #define USB_TEXT_DIR "/sys/kernel/debug/usb/usbmon"
74 #define SYS_USB_BUS_DIR "/sys/bus/usb/devices"
75 #define PROC_USB_BUS_DIR "/proc/bus/usb"
76 #define USB_LINE_LEN 4096
77
78 #if __BYTE_ORDER == __LITTLE_ENDIAN
79 #define htols(s) s
80 #define htoll(l) l
81 #define htol64(ll) ll
82 #else
83 #define htols(s) bswap_16(s)
84 #define htoll(l) bswap_32(l)
85 #define htol64(ll) bswap_64(ll)
86 #endif
87
88 struct mon_bin_stats {
89 uint32_t queued;
90 uint32_t dropped;
91 };
92
93 struct mon_bin_get {
94 pcap_usb_header *hdr;
95 void *data;
96 size_t data_len; /* Length of data (can be zero) */
97 };
98
99 struct mon_bin_mfetch {
100 int32_t *offvec; /* Vector of events fetched */
101 int32_t nfetch; /* Number of events to fetch (out: fetched) */
102 int32_t nflush; /* Number of events to flush */
103 };
104
105 #define MON_IOC_MAGIC 0x92
106
107 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
108 #define MON_IOCX_URB _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
109 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
110 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
111 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
112 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
113 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
114 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
115
116 #define MON_BIN_SETUP 0x1 /* setup hdr is present*/
117 #define MON_BIN_SETUP_ZERO 0x2 /* setup buffer is not available */
118 #define MON_BIN_DATA_ZERO 0x4 /* data buffer is not available */
119 #define MON_BIN_ERROR 0x8
120
121 /*
122 * Private data for capturing on Linux USB.
123 */
124 struct pcap_usb_linux {
125 u_char *mmapbuf; /* memory-mapped region pointer */
126 size_t mmapbuflen; /* size of region */
127 int bus_index;
128 u_int packets_read;
129 };
130
131 /* forward declaration */
132 static int usb_activate(pcap_t *);
133 static int usb_stats_linux(pcap_t *, struct pcap_stat *);
134 static int usb_stats_linux_bin(pcap_t *, struct pcap_stat *);
135 static int usb_read_linux(pcap_t *, int , pcap_handler , u_char *);
136 static int usb_read_linux_bin(pcap_t *, int , pcap_handler , u_char *);
137 static int usb_read_linux_mmap(pcap_t *, int , pcap_handler , u_char *);
138 static int usb_inject_linux(pcap_t *, const void *, int);
139 static int usb_setdirection_linux(pcap_t *, pcap_direction_t);
140 static void usb_cleanup_linux_mmap(pcap_t *);
141
142 static int
143 have_binary_usbmon(void)
144 {
145 struct utsname utsname;
146 char *version_component, *endp;
147 long major, minor, subminor;
148
149 if (uname(&utsname) == 0) {
150 /*
151 * 2.6.21 is the first release with the binary-mode
152 * USB monitoring.
153 */
154 version_component = utsname.release;
155 major = strtol(version_component, &endp, 10);
156 if (endp != version_component && *endp == '.') {
157 /*
158 * OK, that was a valid major version.
159 * Is it 3 or greater? If so, we have binary
160 * mode support.
161 */
162 if (major >= 3)
163 return 1;
164
165 /*
166 * Is it 1 or less? If so, we don't have binary
167 * mode support. (In fact, we don't have any
168 * USB monitoring....)
169 */
170 if (major <= 1)
171 return 0;
172 }
173
174 /*
175 * OK, this is a 2.x kernel.
176 * What's the minor version?
177 */
178 version_component = endp + 1;
179 minor = strtol(version_component, &endp, 10);
180 if (endp != version_component &&
181 (*endp == '.' || *endp == '\0')) {
182 /*
183 * OK, that was a valid minor version.
184 * Is is 2.6 or later? (There shouldn't be a
185 * "later", as 2.6.x went to 3.x, but we'll
186 * check anyway.)
187 */
188 if (minor < 6) {
189 /*
190 * No, so no binary support (did 2.4 have
191 * any USB monitoring at all?)
192 */
193 return 0;
194 }
195
196 /*
197 * OK, this is a 2.6.x kernel.
198 * What's the subminor version?
199 */
200 version_component = endp + 1;
201 subminor = strtol(version_component, &endp, 10);
202 if (endp != version_component &&
203 (*endp == '.' || *endp == '\0')) {
204 /*
205 * OK, that was a valid subminor version.
206 * Is it 21 or greater?
207 */
208 if (subminor >= 21) {
209 /*
210 * Yes - we have binary mode
211 * support.
212 */
213 return 1;
214 }
215 }
216 }
217 }
218
219 /*
220 * Either uname() failed, in which case we just say "no binary
221 * mode support", or we don't have binary mode support.
222 */
223 return 0;
224 }
225
226 /* facility to add an USB device to the device list*/
227 static int
228 usb_dev_add(pcap_if_list_t *devlistp, int n, char *err_str)
229 {
230 char dev_name[10];
231 char dev_descr[30];
232 snprintf(dev_name, 10, USB_IFACE"%d", n);
233 /*
234 * XXX - is there any notion of "up" and "running"?
235 */
236 if (n == 0) {
237 /*
238 * As this refers to all buses, there's no notion of
239 * "connected" vs. "disconnected", as that's a property
240 * that would apply to a particular USB interface.
241 */
242 if (add_dev(devlistp, dev_name,
243 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE,
244 "Raw USB traffic, all USB buses", err_str) == NULL)
245 return -1;
246 } else {
247 /*
248 * XXX - is there a way to determine whether anything's
249 * plugged into this bus interface or not, and set
250 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
251 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
252 */
253 snprintf(dev_descr, 30, "Raw USB traffic, bus number %d", n);
254 if (add_dev(devlistp, dev_name, 0, dev_descr, err_str) == NULL)
255 return -1;
256 }
257
258 return 0;
259 }
260
261 int
262 usb_findalldevs(pcap_if_list_t *devlistp, char *err_str)
263 {
264 char usb_mon_dir[PATH_MAX];
265 char *usb_mon_prefix;
266 size_t usb_mon_prefix_len;
267 struct dirent* data;
268 int ret = 0;
269 DIR* dir;
270 int n;
271 char* name;
272 size_t len;
273
274 if (have_binary_usbmon()) {
275 /*
276 * We have binary-mode support.
277 * What do the device names look like?
278 * Split LINUX_USB_MON_DEV into a directory that we'll
279 * scan and a file name prefix that we'll check for.
280 */
281 pcap_strlcpy(usb_mon_dir, LINUX_USB_MON_DEV, sizeof usb_mon_dir);
282 usb_mon_prefix = strrchr(usb_mon_dir, '/');
283 if (usb_mon_prefix == NULL) {
284 /*
285 * This "shouldn't happen". Just give up if it
286 * does.
287 */
288 return 0;
289 }
290 *usb_mon_prefix++ = '\0';
291 usb_mon_prefix_len = strlen(usb_mon_prefix);
292
293 /*
294 * Open the directory and scan it.
295 */
296 dir = opendir(usb_mon_dir);
297 if (dir != NULL) {
298 while ((ret == 0) && ((data = readdir(dir)) != 0)) {
299 name = data->d_name;
300
301 /*
302 * Is this a usbmon device?
303 */
304 if (strncmp(name, usb_mon_prefix, usb_mon_prefix_len) != 0)
305 continue; /* no */
306
307 /*
308 * What's the device number?
309 */
310 if (sscanf(&name[usb_mon_prefix_len], "%d", &n) == 0)
311 continue; /* failed */
312
313 ret = usb_dev_add(devlistp, n, err_str);
314 }
315
316 closedir(dir);
317 }
318 return 0;
319 } else {
320 /*
321 * We have only text mode support.
322 * We don't look for the text devices because we can't
323 * look for them without root privileges, and we don't
324 * want to require root privileges to enumerate devices
325 * (we want to let the user to try a device and get
326 * an error, rather than seeing no devices and asking
327 * "why am I not seeing devices" and forcing a long
328 * process of poking to figure out whether it's "no
329 * privileges" or "your kernel is too old" or "the
330 * usbmon module isn't loaded" or...).
331 *
332 * Instead, we look to see what buses we have.
333 * If the kernel is so old that it doesn't have
334 * binary-mode support, it's also so old that
335 * it doesn't have a "scan all buses" device.
336 *
337 * First, try scanning sysfs USB bus directory.
338 */
339 dir = opendir(SYS_USB_BUS_DIR);
340 if (dir != NULL) {
341 while ((ret == 0) && ((data = readdir(dir)) != 0)) {
342 name = data->d_name;
343
344 if (strncmp(name, "usb", 3) != 0)
345 continue;
346
347 if (sscanf(&name[3], "%d", &n) == 0)
348 continue;
349
350 ret = usb_dev_add(devlistp, n, err_str);
351 }
352
353 closedir(dir);
354 return 0;
355 }
356
357 /* That didn't work; try scanning procfs USB bus directory. */
358 dir = opendir(PROC_USB_BUS_DIR);
359 if (dir != NULL) {
360 while ((ret == 0) && ((data = readdir(dir)) != 0)) {
361 name = data->d_name;
362 len = strlen(name);
363
364 /* if this file name does not end with a number it's not of our interest */
365 if ((len < 1) || !PCAP_ISDIGIT(name[--len]))
366 continue;
367 while (PCAP_ISDIGIT(name[--len]));
368 if (sscanf(&name[len+1], "%d", &n) != 1)
369 continue;
370
371 ret = usb_dev_add(devlistp, n, err_str);
372 }
373
374 closedir(dir);
375 return ret;
376 }
377
378 /* neither of them worked */
379 return 0;
380 }
381 }
382
383 /*
384 * Matches what's in mon_bin.c in the Linux kernel.
385 */
386 #define MIN_RING_SIZE (8*1024)
387 #define MAX_RING_SIZE (1200*1024)
388
389 static int
390 usb_set_ring_size(pcap_t* handle, int header_size)
391 {
392 /*
393 * A packet from binary usbmon has:
394 *
395 * 1) a fixed-length header, of size header_size;
396 * 2) descriptors, for isochronous transfers;
397 * 3) the payload.
398 *
399 * The kernel buffer has a size, defaulting to 300KB, with a
400 * minimum of 8KB and a maximum of 1200KB. The size is set with
401 * the MON_IOCT_RING_SIZE ioctl; the size passed in is rounded up
402 * to a page size.
403 *
404 * No more than {buffer size}/5 bytes worth of payload is saved.
405 * Therefore, if we subtract the fixed-length size from the
406 * snapshot length, we have the biggest payload we want (we
407 * don't worry about the descriptors - if we have descriptors,
408 * we'll just discard the last bit of the payload to get it
409 * to fit). We multiply that result by 5 and set the buffer
410 * size to that value.
411 */
412 int ring_size;
413
414 if (handle->snapshot < header_size)
415 handle->snapshot = header_size;
416 /* The maximum snapshot size is small enough that this won't overflow */
417 ring_size = (handle->snapshot - header_size) * 5;
418
419 /*
420 * Will this get an error?
421 * (There's no wqy to query the minimum or maximum, so we just
422 * copy the value from the kernel source. We don't round it
423 * up to a multiple of the page size.)
424 */
425 if (ring_size > MAX_RING_SIZE) {
426 /*
427 * Yes. Lower the ring size to the maximum, and set the
428 * snapshot length to the value that would give us a
429 * maximum-size ring.
430 */
431 ring_size = MAX_RING_SIZE;
432 handle->snapshot = header_size + (MAX_RING_SIZE/5);
433 } else if (ring_size < MIN_RING_SIZE) {
434 /*
435 * Yes. Raise the ring size to the minimum, but leave
436 * the snapshot length unchanged, so we show the
437 * callback no more data than specified by the
438 * snapshot length.
439 */
440 ring_size = MIN_RING_SIZE;
441 }
442
443 if (ioctl(handle->fd, MON_IOCT_RING_SIZE, ring_size) == -1) {
444 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
445 errno, "Can't set ring size from fd %d", handle->fd);
446 return -1;
447 }
448 return ring_size;
449 }
450
451 static
452 int usb_mmap(pcap_t* handle)
453 {
454 struct pcap_usb_linux *handlep = handle->priv;
455 int len;
456
457 /*
458 * Attempt to set the ring size as appropriate for the snapshot
459 * length, reducing the snapshot length if that'd make the ring
460 * bigger than the kernel supports.
461 */
462 len = usb_set_ring_size(handle, (int)sizeof(pcap_usb_header_mmapped));
463 if (len == -1) {
464 /* Failed. Fall back on non-memory-mapped access. */
465 return 0;
466 }
467
468 handlep->mmapbuflen = len;
469 handlep->mmapbuf = mmap(0, handlep->mmapbuflen, PROT_READ,
470 MAP_SHARED, handle->fd, 0);
471 if (handlep->mmapbuf == MAP_FAILED) {
472 /*
473 * Failed. We don't treat that as a fatal error, we
474 * just try to fall back on non-memory-mapped access.
475 */
476 return 0;
477 }
478 return 1;
479 }
480
481 #ifdef HAVE_LINUX_USBDEVICE_FS_H
482
483 #define CTRL_TIMEOUT (5*1000) /* milliseconds */
484
485 #define USB_DIR_IN 0x80
486 #define USB_TYPE_STANDARD 0x00
487 #define USB_RECIP_DEVICE 0x00
488
489 #define USB_REQ_GET_DESCRIPTOR 6
490
491 #define USB_DT_DEVICE 1
492 #define USB_DT_CONFIG 2
493
494 #define USB_DEVICE_DESCRIPTOR_SIZE 18
495 #define USB_CONFIG_DESCRIPTOR_SIZE 9
496
497 /* probe the descriptors of the devices attached to the bus */
498 /* the descriptors will end up in the captured packet stream */
499 /* and be decoded by external apps like wireshark */
500 /* without these identifying probes packet data can't be fully decoded */
501 static void
502 probe_devices(int bus)
503 {
504 struct usbdevfs_ctrltransfer ctrl;
505 struct dirent* data;
506 int ret = 0;
507 char busdevpath[sizeof("/dev/bus/usb/000/") + NAME_MAX];
508 DIR* dir;
509 uint8_t descriptor[USB_DEVICE_DESCRIPTOR_SIZE];
510 uint8_t configdesc[USB_CONFIG_DESCRIPTOR_SIZE];
511
512 /* scan usb bus directories for device nodes */
513 snprintf(busdevpath, sizeof(busdevpath), "/dev/bus/usb/%03d", bus);
514 dir = opendir(busdevpath);
515 if (!dir)
516 return;
517
518 while ((ret >= 0) && ((data = readdir(dir)) != 0)) {
519 int fd;
520 char* name = data->d_name;
521
522 if (name[0] == '.')
523 continue;
524
525 snprintf(busdevpath, sizeof(busdevpath), "/dev/bus/usb/%03d/%s", bus, data->d_name);
526
527 fd = open(busdevpath, O_RDWR);
528 if (fd == -1)
529 continue;
530
531 /*
532 * Sigh. Different kernels have different member names
533 * for this structure.
534 */
535 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
536 ctrl.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
537 ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
538 ctrl.wValue = USB_DT_DEVICE << 8;
539 ctrl.wIndex = 0;
540 ctrl.wLength = sizeof(descriptor);
541 #else
542 ctrl.requesttype = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
543 ctrl.request = USB_REQ_GET_DESCRIPTOR;
544 ctrl.value = USB_DT_DEVICE << 8;
545 ctrl.index = 0;
546 ctrl.length = sizeof(descriptor);
547 #endif
548 ctrl.data = descriptor;
549 ctrl.timeout = CTRL_TIMEOUT;
550
551 ret = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
552
553 /* Request CONFIGURATION descriptor alone to know wTotalLength */
554 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
555 ctrl.wValue = USB_DT_CONFIG << 8;
556 ctrl.wLength = sizeof(configdesc);
557 #else
558 ctrl.value = USB_DT_CONFIG << 8;
559 ctrl.length = sizeof(configdesc);
560 #endif
561 ctrl.data = configdesc;
562 ret = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
563 if (ret >= 0) {
564 uint16_t wtotallength;
565 wtotallength = (configdesc[2]) | (configdesc[3] << 8);
566 #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
567 ctrl.wLength = wtotallength;
568 #else
569 ctrl.length = wtotallength;
570 #endif
571 ctrl.data = malloc(wtotallength);
572 if (ctrl.data) {
573 ret = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
574 free(ctrl.data);
575 }
576 }
577 close(fd);
578 }
579 closedir(dir);
580 }
581 #endif /* HAVE_LINUX_USBDEVICE_FS_H */
582
583 pcap_t *
584 usb_create(const char *device, char *ebuf, int *is_ours)
585 {
586 const char *cp;
587 char *cpend;
588 long devnum;
589 pcap_t *p;
590
591 /* Does this look like a USB monitoring device? */
592 cp = strrchr(device, '/');
593 if (cp == NULL)
594 cp = device;
595 /* Does it begin with USB_IFACE? */
596 if (strncmp(cp, USB_IFACE, sizeof USB_IFACE - 1) != 0) {
597 /* Nope, doesn't begin with USB_IFACE */
598 *is_ours = 0;
599 return NULL;
600 }
601 /* Yes - is USB_IFACE followed by a number? */
602 cp += sizeof USB_IFACE - 1;
603 devnum = strtol(cp, &cpend, 10);
604 if (cpend == cp || *cpend != '\0') {
605 /* Not followed by a number. */
606 *is_ours = 0;
607 return NULL;
608 }
609 if (devnum < 0) {
610 /* Followed by a non-valid number. */
611 *is_ours = 0;
612 return NULL;
613 }
614
615 /* OK, it's probably ours. */
616 *is_ours = 1;
617
618 p = pcap_create_common(ebuf, sizeof (struct pcap_usb_linux));
619 if (p == NULL)
620 return (NULL);
621
622 p->activate_op = usb_activate;
623 return (p);
624 }
625
626 static int
627 usb_activate(pcap_t* handle)
628 {
629 struct pcap_usb_linux *handlep = handle->priv;
630 char full_path[USB_LINE_LEN];
631 int ret;
632
633 /*
634 * Turn a negative snapshot value (invalid), a snapshot value of
635 * 0 (unspecified), or a value bigger than the normal maximum
636 * value, into the maximum allowed value.
637 *
638 * If some application really *needs* a bigger snapshot
639 * length, we should just increase MAXIMUM_SNAPLEN.
640 */
641 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
642 handle->snapshot = MAXIMUM_SNAPLEN;
643
644 /* Initialize some components of the pcap structure. */
645 handle->bufsize = handle->snapshot;
646 handle->offset = 0;
647 handle->linktype = DLT_USB_LINUX;
648
649 handle->inject_op = usb_inject_linux;
650 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
651 handle->setdirection_op = usb_setdirection_linux;
652 handle->set_datalink_op = NULL; /* can't change data link type */
653 handle->getnonblock_op = pcap_getnonblock_fd;
654 handle->setnonblock_op = pcap_setnonblock_fd;
655
656 /*get usb bus index from device name */
657 if (sscanf(handle->opt.device, USB_IFACE"%d", &handlep->bus_index) != 1)
658 {
659 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
660 "Can't get USB bus index from %s", handle->opt.device);
661 return PCAP_ERROR;
662 }
663
664 if (have_binary_usbmon())
665 {
666 /*
667 * We have binary-mode support.
668 * Try to open the binary interface.
669 */
670 snprintf(full_path, USB_LINE_LEN, LINUX_USB_MON_DEV"%d", handlep->bus_index);
671 handle->fd = open(full_path, O_RDONLY, 0);
672 if (handle->fd < 0)
673 {
674 /*
675 * The attempt failed; why?
676 */
677 switch (errno) {
678
679 case ENOENT:
680 /*
681 * The device doesn't exist.
682 * That could either mean that there's
683 * no support for monitoring USB buses
684 * (which probably means "the usbmon
685 * module isn't loaded") or that there
686 * is but that *particular* device
687 * doesn't exist (no "scan all buses"
688 * device if the bus index is 0, no
689 * such bus if the bus index isn't 0).
690 */
691 return PCAP_ERROR_NO_SUCH_DEVICE;
692
693 case EACCES:
694 /*
695 * We didn't have permission to open it.
696 */
697 return PCAP_ERROR_PERM_DENIED;
698
699 default:
700 /*
701 * Something went wrong.
702 */
703 pcap_fmt_errmsg_for_errno(handle->errbuf,
704 PCAP_ERRBUF_SIZE, errno,
705 "Can't open USB bus file %s", full_path);
706 return PCAP_ERROR;
707 }
708 }
709
710 if (handle->opt.rfmon)
711 {
712 /*
713 * Monitor mode doesn't apply to USB devices.
714 */
715 close(handle->fd);
716 return PCAP_ERROR_RFMON_NOTSUP;
717 }
718
719 /* try to use fast mmap access */
720 if (usb_mmap(handle))
721 {
722 /* We succeeded. */
723 handle->linktype = DLT_USB_LINUX_MMAPPED;
724 handle->stats_op = usb_stats_linux_bin;
725 handle->read_op = usb_read_linux_mmap;
726 handle->cleanup_op = usb_cleanup_linux_mmap;
727 #ifdef HAVE_LINUX_USBDEVICE_FS_H
728 probe_devices(handlep->bus_index);
729 #endif
730
731 /*
732 * "handle->fd" is a real file, so
733 * "select()" and "poll()" work on it.
734 */
735 handle->selectable_fd = handle->fd;
736 return 0;
737 }
738
739 /*
740 * We failed; try plain binary interface access.
741 *
742 * Attempt to set the ring size as appropriate for
743 * the snapshot length, reducing the snapshot length
744 * if that'd make the ring bigger than the kernel
745 * supports.
746 */
747 if (usb_set_ring_size(handle, (int)sizeof(pcap_usb_header)) == -1) {
748 /* Failed. */
749 close(handle->fd);
750 return PCAP_ERROR;
751 }
752 handle->stats_op = usb_stats_linux_bin;
753 handle->read_op = usb_read_linux_bin;
754 #ifdef HAVE_LINUX_USBDEVICE_FS_H
755 probe_devices(handlep->bus_index);
756 #endif
757 }
758 else {
759 /*
760 * We don't have binary mode support.
761 * Try opening the text-mode device.
762 */
763 snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR"/%dt", handlep->bus_index);
764 handle->fd = open(full_path, O_RDONLY, 0);
765 if (handle->fd < 0)
766 {
767 if (errno == ENOENT)
768 {
769 /*
770 * Not found at the new location; try
771 * the old location.
772 */
773 snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR_OLD"/%dt", handlep->bus_index);
774 handle->fd = open(full_path, O_RDONLY, 0);
775 }
776 if (handle->fd < 0) {
777 if (errno == ENOENT)
778 {
779 /*
780 * The problem is that the file
781 * doesn't exist. Report that as
782 * "no such device". (That could
783 * mean "no such USB bus" or
784 * "monitoring not supported".)
785 */
786 ret = PCAP_ERROR_NO_SUCH_DEVICE;
787 }
788 else if (errno == EACCES)
789 {
790 /*
791 * The problem is that we don't
792 * have sufficient permission to
793 * open the file. Report that.
794 */
795 ret = PCAP_ERROR_PERM_DENIED;
796 }
797 else
798 {
799 /*
800 * Some other error.
801 */
802 ret = PCAP_ERROR;
803 }
804 pcap_fmt_errmsg_for_errno(handle->errbuf,
805 PCAP_ERRBUF_SIZE, errno,
806 "Can't open USB bus file %s",
807 full_path);
808 return ret;
809 }
810 }
811
812 if (handle->opt.rfmon)
813 {
814 /*
815 * Monitor mode doesn't apply to USB devices.
816 */
817 close(handle->fd);
818 return PCAP_ERROR_RFMON_NOTSUP;
819 }
820
821 handle->stats_op = usb_stats_linux;
822 handle->read_op = usb_read_linux;
823 }
824
825 /*
826 * "handle->fd" is a real file, so "select()" and "poll()"
827 * work on it.
828 */
829 handle->selectable_fd = handle->fd;
830
831 /* for plain binary access and text access we need to allocate the read
832 * buffer */
833 handle->buffer = malloc(handle->bufsize);
834 if (!handle->buffer) {
835 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
836 errno, "malloc");
837 close(handle->fd);
838 return PCAP_ERROR;
839 }
840 return 0;
841 }
842
843 static inline int
844 ascii_to_int(char c)
845 {
846 return c < 'A' ? c- '0': ((c<'a') ? c - 'A' + 10: c-'a'+10);
847 }
848
849 /*
850 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
851 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
852 * format description
853 */
854 static int
855 usb_read_linux(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
856 {
857 /* see:
858 * /usr/src/linux/Documentation/usb/usbmon.txt
859 * for message format
860 */
861 struct pcap_usb_linux *handlep = handle->priv;
862 unsigned timestamp;
863 int tag, cnt, ep_num, dev_addr, dummy, ret, urb_len, data_len;
864 ssize_t read_ret;
865 char etype, pipeid1, pipeid2, status[16], urb_tag, line[USB_LINE_LEN];
866 char *string = line;
867 u_char * rawdata = handle->buffer;
868 struct pcap_pkthdr pkth;
869 pcap_usb_header* uhdr = (pcap_usb_header*)handle->buffer;
870 u_char urb_transfer=0;
871 int incoming=0;
872
873 /* ignore interrupt system call errors */
874 do {
875 read_ret = read(handle->fd, line, USB_LINE_LEN - 1);
876 if (handle->break_loop)
877 {
878 handle->break_loop = 0;
879 return -2;
880 }
881 } while ((read_ret == -1) && (errno == EINTR));
882 if (read_ret < 0)
883 {
884 if (errno == EAGAIN)
885 return 0; /* no data there */
886
887 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
888 errno, "Can't read from fd %d", handle->fd);
889 return -1;
890 }
891
892 /* read urb header; %n argument may increment return value, but it's
893 * not mandatory, so does not count on it*/
894 string[read_ret] = 0;
895 ret = sscanf(string, "%x %d %c %c%c:%d:%d %s%n", &tag, &timestamp, &etype,
896 &pipeid1, &pipeid2, &dev_addr, &ep_num, status,
897 &cnt);
898 if (ret < 8)
899 {
900 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
901 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
902 string, ret);
903 return -1;
904 }
905 uhdr->id = tag;
906 uhdr->device_address = dev_addr;
907 uhdr->bus_id = handlep->bus_index;
908 uhdr->status = 0;
909 string += cnt;
910
911 /* don't use usbmon provided timestamp, since it have low precision*/
912 if (gettimeofday(&pkth.ts, NULL) < 0)
913 {
914 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
915 errno, "Can't get timestamp for message '%s'", string);
916 return -1;
917 }
918 uhdr->ts_sec = pkth.ts.tv_sec;
919 uhdr->ts_usec = (int32_t)pkth.ts.tv_usec;
920
921 /* parse endpoint information */
922 if (pipeid1 == 'C')
923 urb_transfer = URB_CONTROL;
924 else if (pipeid1 == 'Z')
925 urb_transfer = URB_ISOCHRONOUS;
926 else if (pipeid1 == 'I')
927 urb_transfer = URB_INTERRUPT;
928 else if (pipeid1 == 'B')
929 urb_transfer = URB_BULK;
930 if (pipeid2 == 'i') {
931 ep_num |= URB_TRANSFER_IN;
932 incoming = 1;
933 }
934 if (etype == 'C')
935 incoming = !incoming;
936
937 /* direction check*/
938 if (incoming)
939 {
940 if (handle->direction == PCAP_D_OUT)
941 return 0;
942 }
943 else
944 if (handle->direction == PCAP_D_IN)
945 return 0;
946 uhdr->event_type = etype;
947 uhdr->transfer_type = urb_transfer;
948 uhdr->endpoint_number = ep_num;
949 pkth.caplen = sizeof(pcap_usb_header);
950 rawdata += sizeof(pcap_usb_header);
951
952 /* check if this is a setup packet */
953 ret = sscanf(status, "%d", &dummy);
954 if (ret != 1)
955 {
956 /* this a setup packet, setup data can be filled with underscore if
957 * usbmon has not been able to read them, so we must parse this fields as
958 * strings */
959 pcap_usb_setup* shdr;
960 char str1[3], str2[3], str3[5], str4[5], str5[5];
961 ret = sscanf(string, "%s %s %s %s %s%n", str1, str2, str3, str4,
962 str5, &cnt);
963 if (ret < 5)
964 {
965 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
966 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
967 string, ret);
968 return -1;
969 }
970 string += cnt;
971
972 /* try to convert to corresponding integer */
973 shdr = &uhdr->setup;
974 shdr->bmRequestType = strtoul(str1, 0, 16);
975 shdr->bRequest = strtoul(str2, 0, 16);
976 shdr->wValue = htols(strtoul(str3, 0, 16));
977 shdr->wIndex = htols(strtoul(str4, 0, 16));
978 shdr->wLength = htols(strtoul(str5, 0, 16));
979
980 uhdr->setup_flag = 0;
981 }
982 else
983 uhdr->setup_flag = 1;
984
985 /* read urb data */
986 ret = sscanf(string, " %d%n", &urb_len, &cnt);
987 if (ret < 1)
988 {
989 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
990 "Can't parse urb length from '%s'", string);
991 return -1;
992 }
993 string += cnt;
994
995 /* urb tag is not present if urb length is 0, so we can stop here
996 * text parsing */
997 pkth.len = urb_len+pkth.caplen;
998 uhdr->urb_len = urb_len;
999 uhdr->data_flag = 1;
1000 data_len = 0;
1001 if (uhdr->urb_len == 0)
1002 goto got;
1003
1004 /* check for data presence; data is present if and only if urb tag is '=' */
1005 if (sscanf(string, " %c", &urb_tag) != 1)
1006 {
1007 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
1008 "Can't parse urb tag from '%s'", string);
1009 return -1;
1010 }
1011
1012 if (urb_tag != '=')
1013 goto got;
1014
1015 /* skip urb tag and following space */
1016 string += 3;
1017
1018 /* if we reach this point we got some urb data*/
1019 uhdr->data_flag = 0;
1020
1021 /* read all urb data; if urb length is greater then the usbmon internal
1022 * buffer length used by the kernel to spool the URB, we get only
1023 * a partial information.
1024 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
1025 * length and default value is 130. */
1026 while ((string[0] != 0) && (string[1] != 0) && (pkth.caplen < (bpf_u_int32)handle->snapshot))
1027 {
1028 rawdata[0] = ascii_to_int(string[0]) * 16 + ascii_to_int(string[1]);
1029 rawdata++;
1030 string+=2;
1031 if (string[0] == ' ')
1032 string++;
1033 pkth.caplen++;
1034 data_len++;
1035 }
1036
1037 got:
1038 uhdr->data_len = data_len;
1039 if (pkth.caplen > (bpf_u_int32)handle->snapshot)
1040 pkth.caplen = (bpf_u_int32)handle->snapshot;
1041
1042 if (handle->fcode.bf_insns == NULL ||
1043 pcap_filter(handle->fcode.bf_insns, handle->buffer,
1044 pkth.len, pkth.caplen)) {
1045 handlep->packets_read++;
1046 callback(user, &pkth, handle->buffer);
1047 return 1;
1048 }
1049 return 0; /* didn't pass filter */
1050 }
1051
1052 static int
1053 usb_inject_linux(pcap_t *handle, const void *buf _U_, int size _U_)
1054 {
1055 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
1056 "Packet injection is not supported on USB devices");
1057 return (-1);
1058 }
1059
1060 static int
1061 usb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
1062 {
1063 struct pcap_usb_linux *handlep = handle->priv;
1064 int dummy, cnt;
1065 ssize_t ret, consumed;
1066 char string[USB_LINE_LEN];
1067 char token[USB_LINE_LEN];
1068 char * ptr = string;
1069 int fd;
1070
1071 snprintf(string, USB_LINE_LEN, USB_TEXT_DIR"/%ds", handlep->bus_index);
1072 fd = open(string, O_RDONLY, 0);
1073 if (fd < 0)
1074 {
1075 if (errno == ENOENT)
1076 {
1077 /*
1078 * Not found at the new location; try the old
1079 * location.
1080 */
1081 snprintf(string, USB_LINE_LEN, USB_TEXT_DIR_OLD"/%ds", handlep->bus_index);
1082 fd = open(string, O_RDONLY, 0);
1083 }
1084 if (fd < 0) {
1085 pcap_fmt_errmsg_for_errno(handle->errbuf,
1086 PCAP_ERRBUF_SIZE, errno,
1087 "Can't open USB stats file %s", string);
1088 return -1;
1089 }
1090 }
1091
1092 /* read stats line */
1093 do {
1094 ret = read(fd, string, USB_LINE_LEN-1);
1095 } while ((ret == -1) && (errno == EINTR));
1096 close(fd);
1097
1098 if (ret < 0)
1099 {
1100 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
1101 "Can't read stats from fd %d ", fd);
1102 return -1;
1103 }
1104 string[ret] = 0;
1105
1106 stats->ps_recv = handlep->packets_read;
1107 stats->ps_drop = 0; /* unless we find text_lost */
1108 stats->ps_ifdrop = 0;
1109
1110 /* extract info on dropped urbs */
1111 for (consumed=0; consumed < ret; ) {
1112 /* from the sscanf man page:
1113 * The C standard says: "Execution of a %n directive does
1114 * not increment the assignment count returned at the completion
1115 * of execution" but the Corrigendum seems to contradict this.
1116 * Do not make any assumptions on the effect of %n conversions
1117 * on the return value and explicitly check for cnt assignmet*/
1118 int ntok;
1119
1120 cnt = -1;
1121 ntok = sscanf(ptr, "%s%n", token, &cnt);
1122 if ((ntok < 1) || (cnt < 0))
1123 break;
1124 consumed += cnt;
1125 ptr += cnt;
1126 if (strcmp(token, "text_lost") == 0)
1127 ntok = sscanf(ptr, "%d%n", &stats->ps_drop, &cnt);
1128 else
1129 ntok = sscanf(ptr, "%d%n", &dummy, &cnt);
1130 if ((ntok != 1) || (cnt < 0))
1131 break;
1132 consumed += cnt;
1133 ptr += cnt;
1134 }
1135
1136 return 0;
1137 }
1138
1139 static int
1140 usb_setdirection_linux(pcap_t *p, pcap_direction_t d)
1141 {
1142 /*
1143 * It's guaranteed, at this point, that d is a valid
1144 * direction value.
1145 */
1146 p->direction = d;
1147 return 0;
1148 }
1149
1150
1151 static int
1152 usb_stats_linux_bin(pcap_t *handle, struct pcap_stat *stats)
1153 {
1154 struct pcap_usb_linux *handlep = handle->priv;
1155 int ret;
1156 struct mon_bin_stats st;
1157 ret = ioctl(handle->fd, MON_IOCG_STATS, &st);
1158 if (ret < 0)
1159 {
1160 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
1161 errno, "Can't read stats from fd %d", handle->fd);
1162 return -1;
1163 }
1164
1165 stats->ps_recv = handlep->packets_read + st.queued;
1166 stats->ps_drop = st.dropped;
1167 stats->ps_ifdrop = 0;
1168 return 0;
1169 }
1170
1171 /*
1172 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
1173 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
1174 */
1175 static int
1176 usb_read_linux_bin(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
1177 {
1178 struct pcap_usb_linux *handlep = handle->priv;
1179 struct mon_bin_get info;
1180 int ret;
1181 struct pcap_pkthdr pkth;
1182 u_int clen = handle->snapshot - sizeof(pcap_usb_header);
1183
1184 /* the usb header is going to be part of 'packet' data*/
1185 info.hdr = (pcap_usb_header*) handle->buffer;
1186 info.data = (u_char *)handle->buffer + sizeof(pcap_usb_header);
1187 info.data_len = clen;
1188
1189 /* ignore interrupt system call errors */
1190 do {
1191 ret = ioctl(handle->fd, MON_IOCX_GET, &info);
1192 if (handle->break_loop)
1193 {
1194 handle->break_loop = 0;
1195 return -2;
1196 }
1197 } while ((ret == -1) && (errno == EINTR));
1198 if (ret < 0)
1199 {
1200 if (errno == EAGAIN)
1201 return 0; /* no data there */
1202
1203 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
1204 errno, "Can't read from fd %d", handle->fd);
1205 return -1;
1206 }
1207
1208 /*
1209 * info.hdr->data_len is the number of bytes of isochronous
1210 * descriptors (if any) plus the number of bytes of data
1211 * provided. There are no isochronous descriptors here,
1212 * because we're using the old 48-byte header.
1213 *
1214 * If info.hdr->data_flag is non-zero, there's no URB data;
1215 * info.hdr->urb_len is the size of the buffer into which
1216 * data is to be placed; it does not represent the amount
1217 * of data transferred. If info.hdr->data_flag is zero,
1218 * there is URB data, and info.hdr->urb_len is the number
1219 * of bytes transmitted or received; it doesn't include
1220 * isochronous descriptors.
1221 *
1222 * The kernel may give us more data than the snaplen; if it did,
1223 * reduce the data length so that the total number of bytes we
1224 * tell our client we have is not greater than the snaplen.
1225 */
1226 if (info.hdr->data_len < clen)
1227 clen = info.hdr->data_len;
1228 info.hdr->data_len = clen;
1229 pkth.caplen = sizeof(pcap_usb_header) + clen;
1230 if (info.hdr->data_flag) {
1231 /*
1232 * No data; just base the on-the-wire length on
1233 * info.hdr->data_len (so that it's >= the captured
1234 * length).
1235 */
1236 pkth.len = sizeof(pcap_usb_header) + info.hdr->data_len;
1237 } else {
1238 /*
1239 * We got data; base the on-the-wire length on
1240 * info.hdr->urb_len, so that it includes data
1241 * discarded by the USB monitor device due to
1242 * its buffer being too small.
1243 */
1244 pkth.len = sizeof(pcap_usb_header) + info.hdr->urb_len;
1245 }
1246 pkth.ts.tv_sec = (time_t)info.hdr->ts_sec;
1247 pkth.ts.tv_usec = info.hdr->ts_usec;
1248
1249 if (handle->fcode.bf_insns == NULL ||
1250 pcap_filter(handle->fcode.bf_insns, handle->buffer,
1251 pkth.len, pkth.caplen)) {
1252 handlep->packets_read++;
1253 callback(user, &pkth, handle->buffer);
1254 return 1;
1255 }
1256
1257 return 0; /* didn't pass filter */
1258 }
1259
1260 /*
1261 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
1262 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
1263 */
1264 #define VEC_SIZE 32
1265 static int
1266 usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
1267 {
1268 struct pcap_usb_linux *handlep = handle->priv;
1269 struct mon_bin_mfetch fetch;
1270 int32_t vec[VEC_SIZE];
1271 struct pcap_pkthdr pkth;
1272 pcap_usb_header_mmapped* hdr;
1273 int nflush = 0;
1274 int packets = 0;
1275 u_int clen, max_clen;
1276
1277 max_clen = handle->snapshot - sizeof(pcap_usb_header_mmapped);
1278
1279 for (;;) {
1280 int i, ret;
1281 int limit = max_packets - packets;
1282 if (limit <= 0)
1283 limit = VEC_SIZE;
1284 if (limit > VEC_SIZE)
1285 limit = VEC_SIZE;
1286
1287 /* try to fetch as many events as possible*/
1288 fetch.offvec = vec;
1289 fetch.nfetch = limit;
1290 fetch.nflush = nflush;
1291 /* ignore interrupt system call errors */
1292 do {
1293 ret = ioctl(handle->fd, MON_IOCX_MFETCH, &fetch);
1294 if (handle->break_loop)
1295 {
1296 handle->break_loop = 0;
1297 return -2;
1298 }
1299 } while ((ret == -1) && (errno == EINTR));
1300 if (ret < 0)
1301 {
1302 if (errno == EAGAIN)
1303 return 0; /* no data there */
1304
1305 pcap_fmt_errmsg_for_errno(handle->errbuf,
1306 PCAP_ERRBUF_SIZE, errno, "Can't mfetch fd %d",
1307 handle->fd);
1308 return -1;
1309 }
1310
1311 /* keep track of processed events, we will flush them later */
1312 nflush = fetch.nfetch;
1313 for (i=0; i<fetch.nfetch; ++i) {
1314 /* discard filler */
1315 hdr = (pcap_usb_header_mmapped*) &handlep->mmapbuf[vec[i]];
1316 if (hdr->event_type == '@')
1317 continue;
1318
1319 /*
1320 * hdr->data_len is the number of bytes of
1321 * isochronous descriptors (if any) plus the
1322 * number of bytes of data provided.
1323 *
1324 * If hdr->data_flag is non-zero, there's no
1325 * URB data; hdr->urb_len is the size of the
1326 * buffer into which data is to be placed; it does
1327 * not represent the amount of data transferred.
1328 * If hdr->data_flag is zero, there is URB data,
1329 * and hdr->urb_len is the number of bytes
1330 * transmitted or received; it doesn't include
1331 * isochronous descriptors.
1332 *
1333 * The kernel may give us more data than the
1334 * snaplen; if it did, reduce the data length
1335 * so that the total number of bytes we
1336 * tell our client we have is not greater than
1337 * the snaplen.
1338 */
1339 clen = max_clen;
1340 if (hdr->data_len < clen)
1341 clen = hdr->data_len;
1342 pkth.caplen = sizeof(pcap_usb_header_mmapped) + clen;
1343 if (hdr->data_flag) {
1344 /*
1345 * No data; just base the on-the-wire length
1346 * on hdr->data_len (so that it's >= the
1347 * captured length).
1348 */
1349 pkth.len = sizeof(pcap_usb_header_mmapped) +
1350 hdr->data_len;
1351 } else {
1352 /*
1353 * We got data; base the on-the-wire length
1354 * on hdr->urb_len, so that it includes
1355 * data discarded by the USB monitor device
1356 * due to its buffer being too small.
1357 */
1358 pkth.len = sizeof(pcap_usb_header_mmapped) +
1359 (hdr->ndesc * sizeof (usb_isodesc)) + hdr->urb_len;
1360 }
1361 pkth.ts.tv_sec = (time_t)hdr->ts_sec;
1362 pkth.ts.tv_usec = hdr->ts_usec;
1363
1364 if (handle->fcode.bf_insns == NULL ||
1365 pcap_filter(handle->fcode.bf_insns, (u_char*) hdr,
1366 pkth.len, pkth.caplen)) {
1367 handlep->packets_read++;
1368 callback(user, &pkth, (u_char*) hdr);
1369 packets++;
1370 }
1371 }
1372
1373 /* with max_packets specifying "unlimited" we stop afer the first chunk*/
1374 if (PACKET_COUNT_IS_UNLIMITED(max_packets) || (packets == max_packets))
1375 break;
1376 }
1377
1378 /* flush pending events*/
1379 if (ioctl(handle->fd, MON_IOCH_MFLUSH, nflush) == -1) {
1380 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
1381 errno, "Can't mflush fd %d", handle->fd);
1382 return -1;
1383 }
1384 return packets;
1385 }
1386
1387 static void
1388 usb_cleanup_linux_mmap(pcap_t* handle)
1389 {
1390 struct pcap_usb_linux *handlep = handle->priv;
1391
1392 /* if we have a memory-mapped buffer, unmap it */
1393 if (handlep->mmapbuf != NULL) {
1394 munmap(handlep->mmapbuf, handlep->mmapbuflen);
1395 handlep->mmapbuf = NULL;
1396 }
1397 pcap_cleanup_live_common(handle);
1398 }