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