]> The Tcpdump Group git mirrors - tcpdump/blob - print-usb.c
Use nd_ types, add host-endian extract routines, clean up signed vs. unsigned.
[tcpdump] / print-usb.c
1 /*
2 * Copyright 2009 Bert Vermeulen <bert@biot.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code distributions
6 * retain the above copyright notice and this paragraph in its entirety, (2)
7 * distributions including binary code include the above copyright notice and
8 * this paragraph in its entirety in the documentation or other materials
9 * provided with the distribution, and (3) all advertising materials mentioning
10 * features or use of this software display the following acknowledgement:
11 * ``This product includes software developed by Paolo Abeni.''
12 * The name of author may not be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * Support for USB packets
19 *
20 */
21
22 /* \summary: USB printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <netdissect-stdinc.h>
29
30 #include "netdissect.h"
31 #include "extract.h"
32
33 #ifdef DLT_USB_LINUX
34 /*
35 * possible transfer mode
36 */
37 #define URB_TRANSFER_IN 0x80
38 #define URB_ISOCHRONOUS 0x0
39 #define URB_INTERRUPT 0x1
40 #define URB_CONTROL 0x2
41 #define URB_BULK 0x3
42
43 /*
44 * possible event type
45 */
46 #define URB_SUBMIT 'S'
47 #define URB_COMPLETE 'C'
48 #define URB_ERROR 'E'
49
50 /*
51 * USB setup header as defined in USB specification.
52 * Appears at the front of each Control S-type packet in DLT_USB captures.
53 */
54 typedef struct _usb_setup {
55 nd_uint8_t bmRequestType;
56 nd_uint8_t bRequest;
57 nd_uint16_t wValue;
58 nd_uint16_t wIndex;
59 nd_uint16_t wLength;
60 } pcap_usb_setup;
61
62 /*
63 * Information from the URB for Isochronous transfers.
64 */
65 typedef struct _iso_rec {
66 nd_int32_t error_count;
67 nd_int32_t numdesc;
68 } iso_rec;
69
70 /*
71 * Header prepended by linux kernel to each event.
72 * Appears at the front of each packet in DLT_USB_LINUX captures.
73 */
74 typedef struct _usb_header {
75 nd_uint64_t id;
76 nd_uint8_t event_type;
77 nd_uint8_t transfer_type;
78 nd_uint8_t endpoint_number;
79 nd_uint8_t device_address;
80 nd_uint16_t bus_id;
81 nd_uint8_t setup_flag;/*if !=0 the urb setup header is not present*/
82 nd_uint8_t data_flag; /*if !=0 no urb data is present*/
83 nd_int64_t ts_sec;
84 nd_int32_t ts_usec;
85 nd_int32_t status;
86 nd_uint32_t urb_len;
87 nd_uint32_t data_len; /* amount of urb data really present in this event*/
88 pcap_usb_setup setup;
89 } pcap_usb_header;
90
91 /*
92 * Header prepended by linux kernel to each event for the 2.6.31
93 * and later kernels; for the 2.6.21 through 2.6.30 kernels, the
94 * "iso_rec" information, and the fields starting with "interval"
95 * are zeroed-out padding fields.
96 *
97 * Appears at the front of each packet in DLT_USB_LINUX_MMAPPED captures.
98 */
99 typedef struct _usb_header_mmapped {
100 nd_uint64_t id;
101 nd_uint8_t event_type;
102 nd_uint8_t transfer_type;
103 nd_uint8_t endpoint_number;
104 nd_uint8_t device_address;
105 nd_uint16_t bus_id;
106 nd_uint8_t setup_flag;/*if !=0 the urb setup header is not present*/
107 nd_uint8_t data_flag; /*if !=0 no urb data is present*/
108 nd_int64_t ts_sec;
109 nd_int32_t ts_usec;
110 nd_int32_t status;
111 nd_uint32_t urb_len;
112 nd_uint32_t data_len; /* amount of urb data really present in this event*/
113 union {
114 pcap_usb_setup setup;
115 iso_rec iso;
116 } s;
117 nd_int32_t interval; /* for Interrupt and Isochronous events */
118 nd_int32_t start_frame; /* for Isochronous events */
119 nd_uint32_t xfer_flags; /* copy of URB's transfer flags */
120 nd_uint32_t ndesc; /* number of isochronous descriptors */
121 } pcap_usb_header_mmapped;
122
123 /*
124 * Isochronous descriptors; for isochronous transfers there might be
125 * one or more of these at the beginning of the packet data. The
126 * number of descriptors is given by the "ndesc" field in the header;
127 * as indicated, in older kernels that don't put the descriptors at
128 * the beginning of the packet, that field is zeroed out, so that field
129 * can be trusted even in captures from older kernels.
130 */
131 typedef struct _usb_isodesc {
132 nd_int32_t status;
133 nd_uint32_t offset;
134 nd_uint32_t len;
135 nd_byte pad[4];
136 } usb_isodesc;
137
138 static const char tstr[] = "[|usb]";
139
140 /* returns direction: 1=inbound 2=outbound -1=invalid */
141 static int
142 get_direction(int transfer_type, int event_type)
143 {
144 int direction;
145
146 direction = -1;
147 switch(transfer_type){
148 case URB_BULK:
149 case URB_CONTROL:
150 case URB_ISOCHRONOUS:
151 switch(event_type)
152 {
153 case URB_SUBMIT:
154 direction = 2;
155 break;
156 case URB_COMPLETE:
157 case URB_ERROR:
158 direction = 1;
159 break;
160 default:
161 direction = -1;
162 }
163 break;
164 case URB_INTERRUPT:
165 switch(event_type)
166 {
167 case URB_SUBMIT:
168 direction = 1;
169 break;
170 case URB_COMPLETE:
171 case URB_ERROR:
172 direction = 2;
173 break;
174 default:
175 direction = -1;
176 }
177 break;
178 default:
179 direction = -1;
180 }
181
182 return direction;
183 }
184
185 static void
186 usb_header_print(netdissect_options *ndo, const pcap_usb_header *uh)
187 {
188 int direction;
189 uint8_t transfer_type, event_type;
190
191 transfer_type = EXTRACT_U_1(uh->transfer_type);
192 switch(transfer_type)
193 {
194 case URB_ISOCHRONOUS:
195 ND_PRINT("ISOCHRONOUS");
196 break;
197 case URB_INTERRUPT:
198 ND_PRINT("INTERRUPT");
199 break;
200 case URB_CONTROL:
201 ND_PRINT("CONTROL");
202 break;
203 case URB_BULK:
204 ND_PRINT("BULK");
205 break;
206 default:
207 ND_PRINT(" ?");
208 }
209
210 event_type = EXTRACT_U_1(uh->event_type);
211 switch(event_type)
212 {
213 case URB_SUBMIT:
214 ND_PRINT(" SUBMIT");
215 break;
216 case URB_COMPLETE:
217 ND_PRINT(" COMPLETE");
218 break;
219 case URB_ERROR:
220 ND_PRINT(" ERROR");
221 break;
222 default:
223 ND_PRINT(" ?");
224 }
225
226 direction = get_direction(transfer_type, event_type);
227 if(direction == 1)
228 ND_PRINT(" from");
229 else if(direction == 2)
230 ND_PRINT(" to");
231 ND_PRINT(" %u:%u:%u", EXTRACT_HE_U_2(uh->bus_id), EXTRACT_U_1(uh->device_address), EXTRACT_U_1(uh->endpoint_number) & 0x7f);
232 }
233
234 /*
235 * This is the top level routine of the printer for captures with a
236 * 48-byte header.
237 *
238 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
239 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
240 * is the number of bytes actually captured.
241 */
242 u_int
243 usb_linux_48_byte_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
244 const u_char *p)
245 {
246 if (h->caplen < sizeof(pcap_usb_header)) {
247 ND_PRINT("%s", tstr);
248 return(sizeof(pcap_usb_header));
249 }
250
251 usb_header_print(ndo, (const pcap_usb_header *) p);
252
253 return(sizeof(pcap_usb_header));
254 }
255
256 #ifdef DLT_USB_LINUX_MMAPPED
257 /*
258 * This is the top level routine of the printer for captures with a
259 * 64-byte header.
260 *
261 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
262 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
263 * is the number of bytes actually captured.
264 */
265 u_int
266 usb_linux_64_byte_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
267 const u_char *p)
268 {
269 if (h->caplen < sizeof(pcap_usb_header_mmapped)) {
270 ND_PRINT("%s", tstr);
271 return(sizeof(pcap_usb_header_mmapped));
272 }
273
274 usb_header_print(ndo, (const pcap_usb_header *) p);
275
276 return(sizeof(pcap_usb_header_mmapped));
277 }
278 #endif /* DLT_USB_LINUX_MMAPPED */
279
280 #endif /* DLT_USB_LINUX */
281