1 /* packet-usb.h
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9 
10 #ifndef __PACKET_USB_H__
11 #define __PACKET_USB_H__
12 
13 #include <epan/value_string.h>
14 #include <epan/wmem_scopes.h>
15 #include <epan/conversation.h>
16 
17 typedef struct _usb_address_t {
18     guint32 device;
19     guint32 endpoint;
20     guint16 bus_id;
21 } usb_address_t;
22 #define USB_ADDR_LEN (sizeof(usb_address_t))
23 
24 /* Flag used to mark usb_address_t.endpoint as an interface
25  * address instead of the normal endpoint address.
26  */
27 #define INTERFACE_PORT	0x80000000
28 
29 
30 typedef struct _usb_conv_info_t usb_conv_info_t;
31 
32 /* header type */
33 typedef enum {
34     USB_HEADER_LINUX_48_BYTES,
35     USB_HEADER_LINUX_64_BYTES,
36     USB_HEADER_USBPCAP,
37     USB_HEADER_MAUSB,
38     USB_HEADER_USBIP,
39     USB_HEADER_DARWIN,
40     USB_HEADER_PSEUDO_URB,
41 } usb_header_t;
42 
43 #define USB_HEADER_IS_LINUX(type) \
44     ((type) == USB_HEADER_LINUX_48_BYTES || (type) == USB_HEADER_LINUX_64_BYTES)
45 
46 typedef struct _usb_pseudo_urb_t {
47     gboolean from_host;
48     guint8 transfer_type;
49     guint8 device_address;
50     guint8 endpoint;
51     guint16 bus_id;
52 } usb_pseudo_urb_t;
53 
54 /* there is one such structure for each request/response */
55 typedef struct _usb_trans_info_t {
56     guint32 request_in;
57     guint32 response_in;
58     nstime_t req_time;
59     usb_header_t header_type;
60 
61     /* Valid only for SETUP transactions */
62     struct _usb_setup {
63         guint8 requesttype;
64         guint8 request;
65         guint16 wValue;
66         guint16 wIndex;
67         guint16 wLength;
68     } setup;
69 
70     /* Valid only during GET DESCRIPTOR transactions */
71     union {
72         struct {
73             guint8 type;
74             guint8 usb_index;
75         } get_descriptor;
76     } u;
77 
78 
79     /* used to pass the interface class from the
80      * interface descriptor onto the endpoint
81      * descriptors so that we can create a
82      * conversation with the appropriate class
83      * once we know the endpoint.
84      * Valid only during GET CONFIGURATION response.
85      */
86     usb_conv_info_t *interface_info;
87 
88     guint64 usb_id;
89 } usb_trans_info_t;
90 
91 enum usb_conv_class_data_type {USB_CONV_UNKNOWN = 0, USB_CONV_U3V, USB_CONV_AUDIO, USB_CONV_VIDEO, USB_CONV_MASS_STORAGE};
92 
93 /* Conversation Structure
94  * there is one such structure for each device/endpoint conversation */
95 struct _usb_conv_info_t {
96     guint16  bus_id;
97     guint16  device_address;
98     guint8   endpoint;
99     gint     direction;
100     guint8   transfer_type; /* transfer type from URB */
101     guint8   descriptor_transfer_type; /* transfer type lifted from the configuration descriptor */
102     guint16  max_packet_size; /* max packet size from configuration descriptor */
103     guint32  device_protocol;
104     gboolean is_request;
105     gboolean is_setup;
106     guint8   setup_requesttype;
107 
108     guint16 interfaceClass;     /* Interface Descriptor - class          */
109     guint16 interfaceSubclass;  /* Interface Descriptor - subclass       */
110     guint16 interfaceProtocol;  /* Interface Descriptor - protocol       */
111     guint8  interfaceNum;       /* Most recent interface number          */
112 
113     guint16 deviceVendor;       /* Device    Descriptor - USB Vendor  ID */
114     guint32 deviceProduct;      /* Device    Descriptor - USB Product ID - MSBs only for encoding unknown */
115     guint16 deviceVersion;      /* Device    Descriptor - USB device version number BCD */
116     guint8  iSerialNumber;      /* Device    Descriptor - iSerialNumber (0 if no serial number available) */
117     wmem_tree_t *transactions;
118     usb_trans_info_t *usb_trans_info; /* pointer to the current transaction */
119 
120     void *class_data;           /* private class/id decode data */
121     enum usb_conv_class_data_type class_data_type;
122 
123     wmem_array_t *alt_settings;
124 };
125 
126 /* This is what a tap will tap */
127 typedef struct _usb_tap_data_t {
128     guint8 urb_type;
129     guint8 transfer_type;
130     usb_conv_info_t *conv_info;
131     usb_trans_info_t *trans_info;
132 } usb_tap_data_t;
133 
134 
135 /* the value for "no endpoint" that's used usb_addr_t, e.g. for the address of the host */
136 #define NO_ENDPOINT  0xffffffff
137 /* the 8bit version of NO_ENDPOINT, it's used in usb_conv_info_t
138    0xff would be an invalid endpoint number (reserved bits are 1) */
139 #define NO_ENDPOINT8 ((guint8)(NO_ENDPOINT& G_MAXUINT8))
140 
141 /*
142  * Values from the Linux USB pseudo-header.
143  */
144 
145 /*
146  * event_type values
147  */
148 #define URB_SUBMIT        'S'
149 #define URB_COMPLETE      'C'
150 #define URB_ERROR         'E'
151 
152 /*
153  * URB transfer_type values
154  */
155 #define URB_ISOCHRONOUS   0x0
156 #define URB_INTERRUPT     0x1
157 #define URB_CONTROL       0x2
158 #define URB_BULK          0x3
159 #define URB_UNKNOWN       0xFF
160 
161 #define URB_TRANSFER_IN   0x80		/* to host */
162 
163 
164 /* http://www.usb.org/developers/defined_class */
165 #define IF_CLASS_DEVICE               0x00
166 #define IF_CLASS_AUDIO                0x01
167 #define IF_CLASS_COMMUNICATIONS       0x02
168 #define IF_CLASS_HID                  0x03
169 #define IF_CLASS_PHYSICAL             0x05
170 #define IF_CLASS_IMAGE                0x06
171 #define IF_CLASS_PRINTER              0x07
172 #define IF_CLASS_MASS_STORAGE         0x08
173 #define IF_CLASS_HUB                  0x09
174 #define IF_CLASS_CDC_DATA             0x0a
175 #define IF_CLASS_SMART_CARD           0x0b
176 #define IF_CLASS_CONTENT_SECURITY     0x0d
177 #define IF_CLASS_VIDEO                0x0e
178 #define IF_CLASS_PERSONAL_HEALTHCARE  0x0f
179 #define IF_CLASS_AUDIO_VIDEO          0x10
180 #define IF_CLASS_DIAGNOSTIC_DEVICE    0xdc
181 #define IF_CLASS_WIRELESS_CONTROLLER  0xe0
182 #define IF_CLASS_MISCELLANEOUS        0xef
183 #define IF_CLASS_APPLICATION_SPECIFIC 0xfe
184 #define IF_CLASS_VENDOR_SPECIFIC      0xff
185 
186 #define IF_CLASS_UNKNOWN              0xffff
187 #define IF_SUBCLASS_UNKNOWN           0xffff
188 #define IF_PROTOCOL_UNKNOWN           0xffff
189 #define DEV_VENDOR_UNKNOWN            0x0000  /* this id is unassigned */
190 #define DEV_PRODUCT_UNKNOWN           0xfffffff /* 0x0000 and 0xffff are used values by vendors, so MSBs encode unknown */
191 #define DEV_VERSION_UNKNOWN           0xffff
192 
193 #define IF_SUBCLASS_MISC_U3V          0x05
194 
195 /* bmRequestType values */
196 #define USB_DIR_OUT                     0               /* to device */
197 #define USB_DIR_IN                      0x80            /* to host */
198 
199 #define USB_TYPE_MASK                   (0x03 << 5)
200 #define USB_TYPE(type)                  (((type) & USB_TYPE_MASK) >> 5)
201 #define RQT_SETUP_TYPE_STANDARD	0
202 #define RQT_SETUP_TYPE_CLASS	1
203 #define RQT_SETUP_TYPE_VENDOR	2
204 
205 #define USB_RECIPIENT_MASK              0x1F
206 #define USB_RECIPIENT(type)             ((type) & USB_RECIPIENT_MASK)
207 #define RQT_SETUP_RECIPIENT_DEVICE      0
208 #define RQT_SETUP_RECIPIENT_INTERFACE   1
209 #define RQT_SETUP_RECIPIENT_ENDPOINT    2
210 #define RQT_SETUP_RECIPIENT_OTHER       3
211 
212 /* Endpoint descriptor bmAttributes  */
213 #define ENDPOINT_TYPE(ep_attrib)        ((ep_attrib) & 0x03)
214 #define ENDPOINT_TYPE_CONTROL           0
215 #define ENDPOINT_TYPE_ISOCHRONOUS       1
216 #define ENDPOINT_TYPE_BULK              2
217 #define ENDPOINT_TYPE_INTERRUPT         3
218 #define ENDPOINT_TYPE_NOT_SET         255
219 
220 
221 #define USB_SETUP_GET_STATUS             0
222 #define USB_SETUP_CLEAR_FEATURE          1
223 #define USB_SETUP_SET_FEATURE            3
224 #define USB_SETUP_SET_ADDRESS            5
225 #define USB_SETUP_GET_DESCRIPTOR         6
226 #define USB_SETUP_SET_DESCRIPTOR         7
227 #define USB_SETUP_GET_CONFIGURATION      8
228 #define USB_SETUP_SET_CONFIGURATION      9
229 #define USB_SETUP_GET_INTERFACE         10
230 #define USB_SETUP_SET_INTERFACE         11
231 #define USB_SETUP_SYNCH_FRAME           12
232 #define USB_SETUP_SET_SEL               48
233 #define USB_SETUP_SET_ISOCH_DELAY       49
234 
235 /* transfer_flags */
236 #define URB_SHORT_NOT_OK	0x0001	/* report short reads as errors */
237 #define URB_ISO_ASAP		0x0002	/* iso-only; use the first unexpired
238 					 * slot in the schedule */
239 #define URB_NO_TRANSFER_DMA_MAP	0x0004	/* urb->transfer_dma valid on submit */
240 #define URB_NO_FSBR		0x0020	/* UHCI-specific */
241 #define URB_ZERO_PACKET		0x0040	/* Finish bulk OUT with short packet */
242 #define URB_NO_INTERRUPT	0x0080	/* HINT: no non-error interrupt
243 					 * needed */
244 #define URB_FREE_BUFFER		0x0100	/* Free transfer buffer with the URB */
245 
246 /* The following flags are used internally by usbcore and HCDs */
247 #define URB_DIR_IN		0x0200	/* Transfer from device to host */
248 #define URB_DIR_OUT		0
249 #define URB_DIR_MASK		URB_DIR_IN
250 
251 #define URB_DMA_MAP_SINGLE	0x00010000	/* Non-scatter-gather mapping */
252 #define URB_DMA_MAP_PAGE	0x00020000	/* HCD-unsupported S-G */
253 #define URB_DMA_MAP_SG		0x00040000	/* HCD-supported S-G */
254 #define URB_MAP_LOCAL		0x00080000	/* HCD-local-memory mapping */
255 #define URB_SETUP_MAP_SINGLE	0x00100000	/* Setup packet DMA mapped */
256 #define URB_SETUP_MAP_LOCAL	0x00200000	/* HCD-local setup packet */
257 #define URB_DMA_SG_COMBINED	0x00400000	/* S-G entries were combined */
258 #define URB_ALIGNED_TEMP_BUFFER	0x00800000	/* Temp buffer was alloc'd */
259 
260 
261 /* 9.6.6 */
262 extern const true_false_string tfs_endpoint_direction;
263 
264 extern value_string_ext usb_class_vals_ext;
265 
266 usb_conv_info_t *get_usb_iface_conv_info(packet_info *pinfo, guint8 interface_num);
267 usb_conv_info_t *get_existing_usb_ep_conv_info(packet_info *pinfo, guint16 bus_id,
268                                                guint16 device_address, int endpoint);
269 
270 proto_item * dissect_usb_descriptor_header(proto_tree *tree,
271                                            tvbuff_t *tvb, int offset,
272                                            value_string_ext *type_val_str);
273 
274 void dissect_usb_endpoint_address(proto_tree *tree, tvbuff_t *tvb, int offset);
275 
276 int
277 dissect_usb_endpoint_descriptor(packet_info *pinfo, proto_tree *parent_tree,
278                                 tvbuff_t *tvb, int offset,
279                                 usb_conv_info_t  *usb_conv_info,
280                                 guint8 *out_ep_type);
281 
282 int
283 dissect_usb_unknown_descriptor(packet_info *pinfo _U_, proto_tree *parent_tree,
284                                tvbuff_t *tvb, int offset,
285                                usb_conv_info_t  *usb_conv_info _U_);
286 
287 int
288 dissect_urb_transfer_flags(tvbuff_t *tvb, int offset, proto_tree* tree, int hf, int endian);
289 
290 struct mausb_header;
291 
292 void
293 dissect_usb_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent,
294                    usb_header_t header_type, void *extra_data);
295 
296 #endif
297 
298 /*
299  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
300  *
301  * Local variables:
302  * c-basic-offset: 4
303  * tab-width: 8
304  * indent-tabs-mode: nil
305  * End:
306  *
307  * vi: set shiftwidth=4 tabstop=8 expandtab:
308  * :indentSize=4:tabSize=8:noTabs=true:
309  */
310