]> The Tcpdump Group git mirrors - libpcap/blob - pcap-usb-linux-common.c
Fix building without protochain support. (GH #852)
[libpcap] / pcap-usb-linux-common.c
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * pcap-usb-linux-common.c - common code for everything that needs to
22 * deal with Linux USB captures.
23 */
24
25 #include "pcap/pcap.h"
26 #include "pcap/usb.h"
27
28 #include "pcap-usb-linux-common.h"
29
30 /*
31 * Compute, from the data provided by the Linux USB memory-mapped capture
32 * mechanism, the amount of packet data that would have been provided
33 * had the capture mechanism not chopped off any data at the end, if, in
34 * fact, it did so.
35 *
36 * Set the "unsliced length" field of the packet header to that value.
37 */
38 void
39 set_linux_usb_mmapped_length(struct pcap_pkthdr *pkth, const u_char *bp)
40 {
41 const pcap_usb_header_mmapped *hdr;
42 u_int bytes_left;
43
44 bytes_left = pkth->caplen;
45 if (bytes_left < sizeof (pcap_usb_header_mmapped)) {
46 /*
47 * We don't have the full metadata header, so give up.
48 */
49 return;
50 }
51 bytes_left -= sizeof (pcap_usb_header_mmapped);
52
53 hdr = (const pcap_usb_header_mmapped *) bp;
54 if (hdr->data_flag) {
55 /*
56 * No data; just base the on-the-bus length on hdr->data_len
57 * (so that it's >= the captured length).
58 */
59 pkth->len = sizeof(pcap_usb_header_mmapped) + hdr->data_len;
60 } else {
61 /*
62 * We got data; calculate the on-the-bus length based on
63 * the data length prior to the USB monitor device discarding
64 * data due to its buffer being too small.
65 */
66 usb_isodesc *descs;
67 u_int pre_truncation_data_len;
68
69 descs = (usb_isodesc *) (bp + sizeof(pcap_usb_header_mmapped));
70
71 /*
72 * For most transfers, urb_len is that amount.
73 */
74 pre_truncation_data_len = hdr->urb_len;
75 if (hdr->transfer_type == URB_ISOCHRONOUS &&
76 hdr->event_type == URB_COMPLETE &&
77 (hdr->endpoint_number & URB_TRANSFER_IN)) {
78 /*
79 * For "this is complete" incoming isochronous
80 * transfer events, however, the data isn't
81 * contiguous, and the isochronous descriptos
82 * show how it's scattered.
83 *
84 * Find the end of the last chunk of data in
85 * the buffer referred to by the isochronous
86 * descriptors; that indicates how far into
87 * the buffer the data would have gone.
88 *
89 * Make sure we don't run past the end of the
90 * captured data while processing the isochronous
91 * descriptors.
92 */
93 pre_truncation_data_len = 0;
94 for (uint32_t desc = 0;
95 desc < hdr->ndesc && bytes_left >= sizeof (usb_isodesc);
96 desc++, bytes_left -= sizeof (usb_isodesc)) {
97 u_int desc_end;
98
99 desc_end = descs[desc].offset + descs[desc].len;
100 if (desc_end > pre_truncation_data_len)
101 pre_truncation_data_len = desc_end;
102 }
103 }
104 pkth->len = sizeof(pcap_usb_header_mmapped) +
105 (hdr->ndesc * sizeof (usb_isodesc)) +
106 pre_truncation_data_len;
107 }
108 }