2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * pcap-usb-linux-common.c - common code for everything that needs to
22 * deal with Linux USB captures.
25 #include "pcap/pcap.h"
28 #include "pcap-usb-linux-common.h"
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
36 * Set the "unsliced length" field of the packet header to that value.
39 set_linux_usb_mmapped_length(struct pcap_pkthdr
*pkth
, const u_char
*bp
)
41 const pcap_usb_header_mmapped
*hdr
;
44 bytes_left
= pkth
->caplen
;
45 if (bytes_left
< sizeof (pcap_usb_header_mmapped
)) {
47 * We don't have the full metadata header, so give up.
51 bytes_left
-= sizeof (pcap_usb_header_mmapped
);
53 hdr
= (const pcap_usb_header_mmapped
*) bp
;
56 * No data; just base the on-the-bus length on hdr->data_len
57 * (so that it's >= the captured length).
59 pkth
->len
= sizeof(pcap_usb_header_mmapped
) + hdr
->data_len
;
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.
67 u_int pre_truncation_data_len
;
69 descs
= (usb_isodesc
*) (bp
+ sizeof(pcap_usb_header_mmapped
));
72 * For most transfers, urb_len is that amount.
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
)) {
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.
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.
89 * Make sure we don't run past the end of the
90 * captured data while processing the isochronous
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
)) {
99 desc_end
= descs
[desc
].offset
+ descs
[desc
].len
;
100 if (desc_end
> pre_truncation_data_len
)
101 pre_truncation_data_len
= desc_end
;
104 pkth
->len
= sizeof(pcap_usb_header_mmapped
) +
105 (hdr
->ndesc
* sizeof (usb_isodesc
)) +
106 pre_truncation_data_len
;