xref: /aosp_15_r20/external/libpcap/pcap-usb-linux-common.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1993, 1994, 1995, 1996, 1997
3*8b26181fSAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker  *
5*8b26181fSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker  * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker  * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker  * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker  * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker  * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker  * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker  * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker  * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker  * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker  * written permission.
17*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker  *
21*8b26181fSAndroid Build Coastguard Worker  * pcap-usb-linux-common.c - common code for everything that needs to
22*8b26181fSAndroid Build Coastguard Worker  * deal with Linux USB captures.
23*8b26181fSAndroid Build Coastguard Worker  */
24*8b26181fSAndroid Build Coastguard Worker 
25*8b26181fSAndroid Build Coastguard Worker #include "pcap/pcap.h"
26*8b26181fSAndroid Build Coastguard Worker #include "pcap/usb.h"
27*8b26181fSAndroid Build Coastguard Worker 
28*8b26181fSAndroid Build Coastguard Worker #include "pcap-usb-linux-common.h"
29*8b26181fSAndroid Build Coastguard Worker 
30*8b26181fSAndroid Build Coastguard Worker /*
31*8b26181fSAndroid Build Coastguard Worker  * Compute, from the data provided by the Linux USB memory-mapped capture
32*8b26181fSAndroid Build Coastguard Worker  * mechanism, the amount of packet data that would have been provided
33*8b26181fSAndroid Build Coastguard Worker  * had the capture mechanism not chopped off any data at the end, if, in
34*8b26181fSAndroid Build Coastguard Worker  * fact, it did so.
35*8b26181fSAndroid Build Coastguard Worker  *
36*8b26181fSAndroid Build Coastguard Worker  * Set the "unsliced length" field of the packet header to that value.
37*8b26181fSAndroid Build Coastguard Worker  */
38*8b26181fSAndroid Build Coastguard Worker void
fix_linux_usb_mmapped_length(struct pcap_pkthdr * pkth,const u_char * bp)39*8b26181fSAndroid Build Coastguard Worker fix_linux_usb_mmapped_length(struct pcap_pkthdr *pkth, const u_char *bp)
40*8b26181fSAndroid Build Coastguard Worker {
41*8b26181fSAndroid Build Coastguard Worker 	const pcap_usb_header_mmapped *hdr;
42*8b26181fSAndroid Build Coastguard Worker 	u_int bytes_left;
43*8b26181fSAndroid Build Coastguard Worker 
44*8b26181fSAndroid Build Coastguard Worker 	/*
45*8b26181fSAndroid Build Coastguard Worker 	 * All callers of this routine must ensure that pkth->caplen is
46*8b26181fSAndroid Build Coastguard Worker 	 * >= sizeof (pcap_usb_header_mmapped).
47*8b26181fSAndroid Build Coastguard Worker 	 */
48*8b26181fSAndroid Build Coastguard Worker 	bytes_left = pkth->caplen;
49*8b26181fSAndroid Build Coastguard Worker 	bytes_left -= sizeof (pcap_usb_header_mmapped);
50*8b26181fSAndroid Build Coastguard Worker 
51*8b26181fSAndroid Build Coastguard Worker 	hdr = (const pcap_usb_header_mmapped *) bp;
52*8b26181fSAndroid Build Coastguard Worker 	if (!hdr->data_flag && hdr->transfer_type == URB_ISOCHRONOUS &&
53*8b26181fSAndroid Build Coastguard Worker 	    hdr->event_type == URB_COMPLETE &&
54*8b26181fSAndroid Build Coastguard Worker 	    (hdr->endpoint_number & URB_TRANSFER_IN) &&
55*8b26181fSAndroid Build Coastguard Worker 	    pkth->len == sizeof(pcap_usb_header_mmapped) +
56*8b26181fSAndroid Build Coastguard Worker 	                 (hdr->ndesc * sizeof (usb_isodesc)) + hdr->urb_len) {
57*8b26181fSAndroid Build Coastguard Worker 		usb_isodesc *descs;
58*8b26181fSAndroid Build Coastguard Worker 		u_int pre_truncation_data_len, pre_truncation_len;
59*8b26181fSAndroid Build Coastguard Worker 
60*8b26181fSAndroid Build Coastguard Worker 		descs = (usb_isodesc *) (bp + sizeof(pcap_usb_header_mmapped));
61*8b26181fSAndroid Build Coastguard Worker 
62*8b26181fSAndroid Build Coastguard Worker 		/*
63*8b26181fSAndroid Build Coastguard Worker 		 * We have data (yes, data_flag is 0 if we *do* have data),
64*8b26181fSAndroid Build Coastguard Worker 		 * and this is a "this is complete" incoming isochronous
65*8b26181fSAndroid Build Coastguard Worker 		 * transfer event, and the length was calculated based
66*8b26181fSAndroid Build Coastguard Worker 		 * on the URB length.
67*8b26181fSAndroid Build Coastguard Worker 		 *
68*8b26181fSAndroid Build Coastguard Worker 		 * That's not correct, because the data isn't contiguous,
69*8b26181fSAndroid Build Coastguard Worker 		 * and the isochronous descriptos show how it's scattered.
70*8b26181fSAndroid Build Coastguard Worker 		 *
71*8b26181fSAndroid Build Coastguard Worker 		 * Find the end of the last chunk of data in the buffer
72*8b26181fSAndroid Build Coastguard Worker 		 * referred to by the isochronous descriptors; that indicates
73*8b26181fSAndroid Build Coastguard Worker 		 * how far into the buffer the data would have gone.
74*8b26181fSAndroid Build Coastguard Worker 		 *
75*8b26181fSAndroid Build Coastguard Worker 		 * Make sure we don't run past the end of the captured data
76*8b26181fSAndroid Build Coastguard Worker 		 * while processing the isochronous descriptors.
77*8b26181fSAndroid Build Coastguard Worker 		 */
78*8b26181fSAndroid Build Coastguard Worker 		pre_truncation_data_len = 0;
79*8b26181fSAndroid Build Coastguard Worker 		for (uint32_t desc = 0;
80*8b26181fSAndroid Build Coastguard Worker 		    desc < hdr->ndesc && bytes_left >= sizeof (usb_isodesc);
81*8b26181fSAndroid Build Coastguard Worker 		    desc++, bytes_left -= sizeof (usb_isodesc)) {
82*8b26181fSAndroid Build Coastguard Worker 			u_int desc_end;
83*8b26181fSAndroid Build Coastguard Worker 
84*8b26181fSAndroid Build Coastguard Worker 			if (descs[desc].len != 0) {
85*8b26181fSAndroid Build Coastguard Worker 				desc_end = descs[desc].offset + descs[desc].len;
86*8b26181fSAndroid Build Coastguard Worker 				if (desc_end > pre_truncation_data_len)
87*8b26181fSAndroid Build Coastguard Worker 					pre_truncation_data_len = desc_end;
88*8b26181fSAndroid Build Coastguard Worker 			}
89*8b26181fSAndroid Build Coastguard Worker 		}
90*8b26181fSAndroid Build Coastguard Worker 
91*8b26181fSAndroid Build Coastguard Worker 		/*
92*8b26181fSAndroid Build Coastguard Worker 		 * Now calculate the total length based on that data
93*8b26181fSAndroid Build Coastguard Worker 		 * length.
94*8b26181fSAndroid Build Coastguard Worker 		 */
95*8b26181fSAndroid Build Coastguard Worker 		pre_truncation_len = sizeof(pcap_usb_header_mmapped) +
96*8b26181fSAndroid Build Coastguard Worker 		    (hdr->ndesc * sizeof (usb_isodesc)) +
97*8b26181fSAndroid Build Coastguard Worker 		    pre_truncation_data_len;
98*8b26181fSAndroid Build Coastguard Worker 
99*8b26181fSAndroid Build Coastguard Worker 		/*
100*8b26181fSAndroid Build Coastguard Worker 		 * If that's greater than or equal to the captured length,
101*8b26181fSAndroid Build Coastguard Worker 		 * use that as the length.
102*8b26181fSAndroid Build Coastguard Worker 		 */
103*8b26181fSAndroid Build Coastguard Worker 		if (pre_truncation_len >= pkth->caplen)
104*8b26181fSAndroid Build Coastguard Worker 			pkth->len = pre_truncation_len;
105*8b26181fSAndroid Build Coastguard Worker 
106*8b26181fSAndroid Build Coastguard Worker 		/*
107*8b26181fSAndroid Build Coastguard Worker 		 * If the captured length is greater than the length,
108*8b26181fSAndroid Build Coastguard Worker 		 * use the captured length.
109*8b26181fSAndroid Build Coastguard Worker 		 *
110*8b26181fSAndroid Build Coastguard Worker 		 * For completion events for incoming isochronous transfers,
111*8b26181fSAndroid Build Coastguard Worker 		 * it's based on data_len, which is calculated the same way
112*8b26181fSAndroid Build Coastguard Worker 		 * we calculated pre_truncation_data_len above, except that
113*8b26181fSAndroid Build Coastguard Worker 		 * it has access to all the isochronous descriptors, not
114*8b26181fSAndroid Build Coastguard Worker 		 * just the ones that the kernel were able to provide us or,
115*8b26181fSAndroid Build Coastguard Worker 		 * for a capture file, that weren't sliced off by a snapshot
116*8b26181fSAndroid Build Coastguard Worker 		 * length.
117*8b26181fSAndroid Build Coastguard Worker 		 *
118*8b26181fSAndroid Build Coastguard Worker 		 * However, it might have been reduced by the USB capture
119*8b26181fSAndroid Build Coastguard Worker 		 * mechanism arbitrarily limiting the amount of data it
120*8b26181fSAndroid Build Coastguard Worker 		 * provides to userland, or by the libpcap capture code
121*8b26181fSAndroid Build Coastguard Worker 		 * limiting it to being no more than the snapshot, so
122*8b26181fSAndroid Build Coastguard Worker 		 * we don't want to just use it all the time; we only
123*8b26181fSAndroid Build Coastguard Worker 		 * do so to try to get a better estimate of the actual
124*8b26181fSAndroid Build Coastguard Worker 		 * length - and to make sure the on-the-network length
125*8b26181fSAndroid Build Coastguard Worker 		 * is always >= the captured length.
126*8b26181fSAndroid Build Coastguard Worker 		 */
127*8b26181fSAndroid Build Coastguard Worker 		if (pkth->caplen > pkth->len)
128*8b26181fSAndroid Build Coastguard Worker 			pkth->len = pkth->caplen;
129*8b26181fSAndroid Build Coastguard Worker 	}
130*8b26181fSAndroid Build Coastguard Worker }
131