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