]> The Tcpdump Group git mirrors - libpcap/blob - pcap-usb-linux.c
7265a9c1300b68ee272f2602068a99d592832cc1
[libpcap] / pcap-usb-linux.c
1 /*
2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * USB sniffing API implementation for Linux platform
33 * By Paolo Abeni <paolo.abeni@email.it>
34 *
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "pcap-int.h"
42 #include "pcap-usb-linux.h"
43 #include "pcap/usb.h"
44
45 #ifdef NEED_STRERROR_H
46 #include "strerror.h"
47 #endif
48
49 #include <ctype.h>
50 #include <errno.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <string.h>
55 #include <dirent.h>
56 #include <byteswap.h>
57 #include <netinet/in.h>
58 #include <sys/ioctl.h>
59 #include <sys/mman.h>
60
61 #define USB_IFACE "usb"
62 #define USB_TEXT_DIR "/sys/kernel/debug/usbmon"
63 #define USB_BUS_DIR "/proc/bus/usb"
64 #define USB_LINE_LEN 4096
65
66
67 #define PIPE_IN 0x80
68 #define PIPE_ISOCHRONOUS 0
69 #define PIPE_INTERRUPT 1
70 #define PIPE_CONTROL 2
71 #define PIPE_BULK 3
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_stats_linux(pcap_t *, struct pcap_stat *);
118 static int usb_stats_linux_bin(pcap_t *, struct pcap_stat *);
119 static int usb_read_linux(pcap_t *, int , pcap_handler , u_char *);
120 static int usb_read_linux_bin(pcap_t *, int , pcap_handler , u_char *);
121 static int usb_read_linux_mmap(pcap_t *, int , pcap_handler , u_char *);
122 static int usb_inject_linux(pcap_t *, const void *, size_t);
123 static int usb_setfilter_linux(pcap_t *, struct bpf_program *);
124 static int usb_setdirection_linux(pcap_t *, pcap_direction_t);
125 static void usb_close_linux(pcap_t *);
126 static void usb_close_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
150 /* scan profs usb bus directorys */
151 dir = opendir(USB_BUS_DIR);
152 while ((ret == 0) && ((data = readdir(dir)) != 0)) {
153 int n;
154 char* name = data->d_name;
155 int len = strlen(name);
156
157 /* if this file name does not end with a number it's not of our interest */
158 if ((len < 1) || !isdigit(name[--len]))
159 continue;
160 while (isdigit(name[--len]));
161 if (sscanf(&name[len+1], "%d", &n) != 1)
162 continue;
163
164 ret = usb_dev_add(alldevsp, n, err_str);
165 }
166
167 closedir(dir);
168 return ret;
169 }
170
171 static
172 int usb_mmap(pcap_t* handle)
173 {
174 int len = ioctl(handle->fd, MON_IOCQ_RING_SIZE);
175 if (len < 0)
176 return 0;
177
178 handle->buffer = mmap(0, len, PROT_READ, MAP_SHARED, handle->fd, 0);
179 return handle->buffer != MAP_FAILED;
180 }
181
182 pcap_t*
183 usb_open_live(const char* bus, int snaplen, int promisc , int to_ms, char* errmsg)
184 {
185 char full_path[USB_LINE_LEN];
186 pcap_t *handle;
187
188 /* Allocate a handle for this session. */
189 handle = malloc(sizeof(*handle));
190 if (handle == NULL) {
191 snprintf(errmsg, PCAP_ERRBUF_SIZE, "malloc: %s",
192 pcap_strerror(errno));
193 return NULL;
194 }
195
196 /* Initialize some components of the pcap structure. */
197 memset(handle, 0, sizeof(*handle));
198 handle->snapshot = snaplen;
199 handle->md.timeout = to_ms;
200 handle->bufsize = snaplen;
201 handle->offset = 0;
202 handle->linktype = DLT_USB_LINUX;
203
204 /*
205 * "handle->fd" is a real file , so "select()" and "poll()"
206 * work on it.
207 */
208 handle->selectable_fd = handle->fd;
209
210 handle->inject_op = usb_inject_linux;
211 handle->setfilter_op = usb_setfilter_linux;
212 handle->setdirection_op = usb_setdirection_linux;
213 handle->set_datalink_op = NULL; /* can't change data link type */
214 handle->getnonblock_op = pcap_getnonblock_fd;
215 handle->setnonblock_op = pcap_setnonblock_fd;
216 handle->close_op = usb_close_linux;
217
218 /*get usb bus index from device name */
219 if (sscanf(bus, USB_IFACE"%d", &handle->md.ifindex) != 1)
220 {
221 snprintf(errmsg, PCAP_ERRBUF_SIZE,
222 "Can't get USB bus index from %s", bus);
223 free(handle);
224 return NULL;
225 }
226
227 /*now select the read method: try to open binary interface */
228 snprintf(full_path, USB_LINE_LEN, LINUX_USB_MON_DEV"%d", handle->md.ifindex);
229 handle->fd = open(full_path, O_RDONLY, 0);
230 if (handle->fd >= 0)
231 {
232 /* header endianess can't be fixed for memory mapped access,
233 * due to read only access to mmaped buffer, so disable it*/
234 /* binary api is available, try to use fast mmap access */
235 if (usb_mmap(handle)) {
236 handle->stats_op = usb_stats_linux_bin;
237 handle->read_op = usb_read_linux_mmap;
238 handle->close_op = usb_close_linux_mmap;
239 return handle;
240 }
241
242 /* can't mmap, use plain binary interface access */
243 handle->stats_op = usb_stats_linux_bin;
244 handle->read_op = usb_read_linux_bin;
245 }
246 else {
247 /*Binary interface not available, try open text interface */
248 snprintf(full_path, USB_LINE_LEN, USB_TEXT_DIR"/%dt", handle->md.ifindex);
249 handle->fd = open(full_path, O_RDONLY, 0);
250 if (handle->fd < 0)
251 {
252 /* no more fallback, give it up*/
253 snprintf(errmsg, PCAP_ERRBUF_SIZE,
254 "Can't open USB bus file %s: %s", full_path, strerror(errno));
255 free(handle);
256 return NULL;
257 }
258 handle->stats_op = usb_stats_linux;
259 handle->read_op = usb_read_linux;
260 }
261
262 /* for plain binary access and text access we need to allocate the read
263 * buffer */
264 handle->buffer = malloc(handle->bufsize);
265 if (!handle->buffer) {
266 snprintf(errmsg, PCAP_ERRBUF_SIZE,
267 "malloc: %s", pcap_strerror(errno));
268 usb_close_linux(handle);
269 return NULL;
270 }
271 return handle;
272 }
273
274 static inline int
275 ascii_to_int(char c)
276 {
277 return c < 'A' ? c- '0': ((c<'a') ? c - 'A' + 10: c-'a'+10);
278 }
279
280 /*
281 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
282 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
283 * format description
284 */
285 static int
286 usb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
287 {
288 /* see:
289 * /usr/src/linux/Documentation/usb/usbmon.txt
290 * for message format
291 */
292 unsigned timestamp;
293 int tag, cnt, ep_num, dev_addr, dummy, ret, urb_len, data_len;
294 char etype, pipeid1, pipeid2, status[16], urb_tag, line[USB_LINE_LEN];
295 char *string = line;
296 u_char * rawdata = handle->buffer;
297 struct pcap_pkthdr pkth;
298 pcap_usb_header* uhdr = (pcap_usb_header*)handle->buffer;
299 u_char urb_transfer=0;
300 int incoming=0;
301
302 /* ignore interrupt system call errors */
303 do {
304 ret = read(handle->fd, line, USB_LINE_LEN - 1);
305 if (handle->break_loop)
306 {
307 handle->break_loop = 0;
308 return -2;
309 }
310 } while ((ret == -1) && (errno == EINTR));
311 if (ret < 0)
312 {
313 if (errno == EAGAIN)
314 return 0; /* no data there */
315
316 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
317 "Can't read from fd %d: %s", handle->fd, strerror(errno));
318 return -1;
319 }
320
321 /* read urb header; %n argument may increment return value, but it's
322 * not mandatory, so does not count on it*/
323 string[ret] = 0;
324 ret = sscanf(string, "%x %d %c %c%c:%d:%d %s%n", &tag, &timestamp, &etype,
325 &pipeid1, &pipeid2, &dev_addr, &ep_num, status,
326 &cnt);
327 if (ret < 8)
328 {
329 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
330 "Can't parse USB bus message '%s', too few tokens (expected 8 got %d)",
331 string, ret);
332 return -1;
333 }
334 uhdr->id = tag;
335 uhdr->endpoint_number = ep_num;
336 uhdr->device_address = dev_addr;
337 uhdr->bus_id = handle->md.ifindex;
338 uhdr->status = 0;
339 string += cnt;
340
341 /* don't use usbmon provided timestamp, since it have low precision*/
342 if (gettimeofday(&pkth.ts, NULL) < 0)
343 {
344 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
345 "Can't get timestamp for message '%s' %d:%s",
346 string, errno, strerror(errno));
347 return -1;
348 }
349 uhdr->ts_sec = pkth.ts.tv_sec;
350 uhdr->ts_usec = pkth.ts.tv_usec;
351
352 /* parse endpoint information */
353 if (pipeid1 == 'C')
354 urb_transfer = URB_CONTROL;
355 else if (pipeid1 == 'Z')
356 urb_transfer = URB_ISOCHRONOUS;
357 else if (pipeid1 == 'I')
358 urb_transfer = URB_INTERRUPT;
359 else if (pipeid1 == 'B')
360 urb_transfer = URB_BULK;
361 if (pipeid2 == 'i') {
362 urb_transfer |= URB_TRANSFER_IN;
363 incoming = 1;
364 }
365 if (etype == 'C')
366 incoming = !incoming;
367
368 /* direction check*/
369 if (incoming)
370 {
371 if (handle->direction == PCAP_D_OUT)
372 return 0;
373 }
374 else
375 if (handle->direction == PCAP_D_IN)
376 return 0;
377 uhdr->event_type = etype;
378 uhdr->transfer_type = urb_transfer;
379 pkth.caplen = sizeof(pcap_usb_header);
380 rawdata += sizeof(pcap_usb_header);
381
382 /* check if this is a setup packet */
383 ret = sscanf(status, "%d", &dummy);
384 if (ret != 1)
385 {
386 /* this a setup packet, setup data can be filled with underscore if
387 * usbmon has not been able to read them, so we must parse this fields as
388 * strings */
389 pcap_usb_setup* shdr;
390 char str1[3], str2[3], str3[5], str4[5], str5[5];
391 ret = sscanf(string, "%s %s %s %s %s%n", str1, str2, str3, str4,
392 str5, &cnt);
393 if (ret < 5)
394 {
395 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
396 "Can't parse USB bus message '%s', too few tokens (expected 5 got %d)",
397 string, ret);
398 return -1;
399 }
400 string += cnt;
401
402 /* try to convert to corresponding integer */
403 shdr = &uhdr->setup;
404 shdr->bmRequestType = strtoul(str1, 0, 16);
405 shdr->bRequest = strtoul(str2, 0, 16);
406 shdr->wValue = htols(strtoul(str3, 0, 16));
407 shdr->wIndex = htols(strtoul(str4, 0, 16));
408 shdr->wLength = htols(strtoul(str5, 0, 16));
409
410 uhdr->setup_flag = 0;
411 }
412 else
413 uhdr->setup_flag = 1;
414
415 /* read urb data */
416 ret = sscanf(string, " %d%n", &urb_len, &cnt);
417 if (ret < 1)
418 {
419 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
420 "Can't parse urb length from '%s'", string);
421 return -1;
422 }
423 string += cnt;
424
425 /* urb tag is not present if urb length is 0, so we can stop here
426 * text parsing */
427 pkth.len = urb_len+pkth.caplen;
428 uhdr->urb_len = urb_len;
429 uhdr->data_flag = 1;
430 data_len = 0;
431 if (uhdr->urb_len == pkth.caplen)
432 goto got;
433
434 /* check for data presence; data is present if and only if urb tag is '=' */
435 if (sscanf(string, " %c", &urb_tag) != 1)
436 {
437 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
438 "Can't parse urb tag from '%s'", string);
439 return -1;
440 }
441
442 if (urb_tag != '=')
443 goto got;
444
445 /* skip urb tag and following space */
446 string += 3;
447
448 /* if we reach this point we got some urb data*/
449 uhdr->data_flag = 0;
450
451 /* read all urb data; if urb length is greater then the usbmon internal
452 * buffer length used by the kernel to spool the URB, we get only
453 * a partial information.
454 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
455 * length and default value is 130. */
456 while ((string[0] != 0) && (string[1] != 0) && (pkth.caplen < handle->snapshot))
457 {
458 rawdata[0] = ascii_to_int(string[0]) * 16 + ascii_to_int(string[1]);
459 rawdata++;
460 string+=2;
461 if (string[0] == ' ')
462 string++;
463 pkth.caplen++;
464 data_len++;
465 }
466
467 got:
468 uhdr->data_len = data_len;
469 handle->md.packets_read++;
470 if (pkth.caplen > handle->snapshot)
471 pkth.caplen = handle->snapshot;
472
473 callback(user, &pkth, handle->buffer);
474 return 1;
475 }
476
477 static int
478 usb_inject_linux(pcap_t *handle, const void *buf, size_t size)
479 {
480 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
481 "USB devices");
482 return (-1);
483 }
484
485 static void
486 usb_close_linux(pcap_t* handle)
487 {
488 /* handle fill be freed in pcap_close() 'common' code */
489 close(handle->fd);
490 if (handle->buffer)
491 free(handle->buffer);
492 }
493
494 static int
495 usb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
496 {
497 int dummy, ret, consumed, cnt;
498 char string[USB_LINE_LEN];
499 char token[USB_LINE_LEN];
500 char * ptr = string;
501 snprintf(string, USB_LINE_LEN, USB_TEXT_DIR"/%ds", handle->md.ifindex);
502
503 int fd = open(string, O_RDONLY, 0);
504 if (fd < 0)
505 {
506 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
507 "Can't open USB stats file %s: %s",
508 string, strerror(errno));
509 return -1;
510 }
511
512 /* read stats line */
513 do {
514 ret = read(fd, string, USB_LINE_LEN-1);
515 } while ((ret == -1) && (errno == EINTR));
516 close(fd);
517
518 if (ret < 0)
519 {
520 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
521 "Can't read stats from fd %d ", fd);
522 return -1;
523 }
524 string[ret] = 0;
525
526 /* extract info on dropped urbs */
527 for (consumed=0; consumed < ret; ) {
528 int ntok = sscanf(ptr, "%s%n", token, &cnt);
529 if (ntok != 2)
530 break;
531 consumed += cnt;
532 ptr += cnt;
533 if (strcmp(token, "nreaders") == 0)
534 ret = sscanf(ptr, "%d", &stats->ps_drop);
535 else
536 ret = sscanf(ptr, "%d", &dummy);
537 if (ntok != 2)
538 break;
539 consumed += cnt;
540 ptr += cnt;
541 }
542
543 stats->ps_recv = handle->md.packets_read;
544 stats->ps_ifdrop = 0;
545 return 0;
546 }
547
548 static int
549 usb_setfilter_linux(pcap_t *p, struct bpf_program *fp)
550 {
551 return 0;
552 }
553
554 static int
555 usb_setdirection_linux(pcap_t *p, pcap_direction_t d)
556 {
557 p->direction = d;
558 return 0;
559 }
560
561
562 static int
563 usb_stats_linux_bin(pcap_t *handle, struct pcap_stat *stats)
564 {
565 int ret;
566 struct mon_bin_stats st;
567 ret = ioctl(handle->fd, MON_IOCG_STATS, &st);
568 if (ret < 0)
569 {
570 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
571 "Can't read stats from fd %d:%s ", handle->fd, strerror(errno));
572 return -1;
573 }
574
575 stats->ps_recv = handle->md.packets_read + st.queued;
576 stats->ps_ifdrop = st.dropped;
577 return 0;
578 }
579
580 /*
581 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
582 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
583 */
584 static int
585 usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
586 {
587 struct mon_bin_get info;
588 int ret;
589 struct pcap_pkthdr pkth;
590 int clen = handle->snapshot - sizeof(pcap_usb_header);
591
592 /* the usb header is going to be part of 'packet' data*/
593 info.hdr = (pcap_usb_header*) handle->buffer;
594 info.data = handle->buffer + sizeof(pcap_usb_header);
595 info.data_len = clen;
596 /* ignore interrupt system call errors */
597 do {
598 ret = ioctl(handle->fd, MON_IOCX_GET, &info);
599 if (handle->break_loop)
600 {
601 handle->break_loop = 0;
602 return -2;
603 }
604 } while ((ret == -1) && (errno == EINTR));
605 if (ret < 0)
606 {
607 if (errno == EAGAIN)
608 return 0; /* no data there */
609
610 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
611 "Can't read from fd %d: %s", handle->fd, strerror(errno));
612 return -1;
613 }
614
615 /* we can get less that than really captured from kernel, depending on
616 * snaplen, so adjust header accordingly */
617 if (info.hdr->data_len < clen)
618 clen = info.hdr->data_len;
619 info.hdr->data_len = clen;
620 pkth.caplen = clen + sizeof(pcap_usb_header);
621 pkth.len = info.hdr->urb_len + sizeof(pcap_usb_header);
622 pkth.ts.tv_sec = info.hdr->ts_sec;
623 pkth.ts.tv_usec = info.hdr->ts_usec;
624
625 handle->md.packets_read++;
626 callback(user, &pkth, handle->buffer);
627 return 1;
628 }
629
630 /*
631 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
632 * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
633 */
634 #define VEC_SIZE 32
635 static int
636 usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
637 {
638 struct mon_bin_mfetch fetch;
639 int32_t vec[VEC_SIZE];
640 struct pcap_pkthdr pkth;
641 pcap_usb_header* hdr;
642 int nflush = 0;
643 int packets = 0;
644
645 for (;;) {
646 int i, ret;
647 int limit = max_packets - packets;
648 if (limit < 0)
649 limit = VEC_SIZE;
650 if (limit > VEC_SIZE)
651 limit = VEC_SIZE;
652
653 /* try to fetch as many events as possible*/
654 fetch.offvec = vec;
655 fetch.nfetch = limit;
656 fetch.nflush = nflush;
657 /* ignore interrupt system call errors */
658 do {
659 ret = ioctl(handle->fd, MON_IOCX_MFETCH, &fetch);
660 if (handle->break_loop)
661 {
662 handle->break_loop = 0;
663 return -2;
664 }
665 } while ((ret == -1) && (errno == EINTR));
666 if (ret < 0)
667 {
668 if (errno == EAGAIN)
669 return 0; /* no data there */
670
671 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
672 "Can't mfetch fd %d: %s", handle->fd, strerror(errno));
673 return -1;
674 }
675
676 /* keep track of processed events, we will flush them later */
677 nflush = fetch.nfetch;
678 for (i=0; i<fetch.nfetch; ++i) {
679 /* discard filler */
680 hdr = (pcap_usb_header*) &handle->buffer[vec[i]];
681 if (hdr->event_type == '@')
682 continue;
683
684 /* get packet info from header*/
685 pkth.caplen = hdr->data_len + sizeof(pcap_usb_header);
686 pkth.len = hdr->urb_len + sizeof(pcap_usb_header);
687 pkth.ts.tv_sec = hdr->ts_sec;
688 pkth.ts.tv_usec = hdr->ts_usec;
689
690 handle->md.packets_read++;
691 callback(user, &pkth, (u_char*) hdr);
692 packets++;
693 }
694
695 /* with max_packets == -1 we stop afer the first chunk*/
696 if ((max_packets == -1) || (packets == max_packets))
697 break;
698 }
699
700 /* flush pending events*/
701 ioctl(handle->fd, MON_IOCH_MFLUSH, nflush);
702 return packets;
703 }
704
705 static void
706 usb_close_linux_mmap(pcap_t* handle)
707 {
708 /* handle fill be freed in pcap_close() 'common' code, buffer must not
709 * be freed because it's memory mapped */
710 close(handle->fd);
711 }