]> The Tcpdump Group git mirrors - tcpdump/blob - print-usb.c
OpenFlow: Have a function for each message type.
[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
139 /* returns direction: 1=inbound 2=outbound -1=invalid */
140 static int
141 get_direction(int transfer_type, int event_type)
142 {
143 int direction;
144
145 direction = -1;
146 switch(transfer_type){
147 case URB_BULK:
148 case URB_CONTROL:
149 case URB_ISOCHRONOUS:
150 switch(event_type)
151 {
152 case URB_SUBMIT:
153 direction = 2;
154 break;
155 case URB_COMPLETE:
156 case URB_ERROR:
157 direction = 1;
158 break;
159 default:
160 direction = -1;
161 }
162 break;
163 case URB_INTERRUPT:
164 switch(event_type)
165 {
166 case URB_SUBMIT:
167 direction = 1;
168 break;
169 case URB_COMPLETE:
170 case URB_ERROR:
171 direction = 2;
172 break;
173 default:
174 direction = -1;
175 }
176 break;
177 default:
178 direction = -1;
179 }
180
181 return direction;
182 }
183
184 static void
185 usb_header_print(netdissect_options *ndo, const pcap_usb_header *uh)
186 {
187 int direction;
188 uint8_t transfer_type, event_type;
189
190 ndo->ndo_protocol = "usb";
191
192 nd_print_protocol_caps(ndo);
193 if (ndo->ndo_qflag)
194 return;
195
196 ND_PRINT(" ");
197 transfer_type = GET_U_1(uh->transfer_type);
198 switch(transfer_type)
199 {
200 case URB_ISOCHRONOUS:
201 ND_PRINT("ISOCHRONOUS");
202 break;
203 case URB_INTERRUPT:
204 ND_PRINT("INTERRUPT");
205 break;
206 case URB_CONTROL:
207 ND_PRINT("CONTROL");
208 break;
209 case URB_BULK:
210 ND_PRINT("BULK");
211 break;
212 default:
213 ND_PRINT(" ?");
214 }
215
216 event_type = GET_U_1(uh->event_type);
217 switch(event_type)
218 {
219 case URB_SUBMIT:
220 ND_PRINT(" SUBMIT");
221 break;
222 case URB_COMPLETE:
223 ND_PRINT(" COMPLETE");
224 break;
225 case URB_ERROR:
226 ND_PRINT(" ERROR");
227 break;
228 default:
229 ND_PRINT(" ?");
230 }
231
232 direction = get_direction(transfer_type, event_type);
233 if(direction == 1)
234 ND_PRINT(" from");
235 else if(direction == 2)
236 ND_PRINT(" to");
237 ND_PRINT(" %u:%u:%u", GET_HE_U_2(uh->bus_id),
238 GET_U_1(uh->device_address),
239 GET_U_1(uh->endpoint_number) & 0x7f);
240 }
241
242 /*
243 * This is the top level routine of the printer for captures with a
244 * 48-byte header.
245 *
246 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
247 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
248 * is the number of bytes actually captured.
249 */
250 void
251 usb_linux_48_byte_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
252 const u_char *p)
253 {
254 ndo->ndo_protocol = "usb_linux_48_byte";
255 if (h->caplen < sizeof(pcap_usb_header)) {
256 ndo->ndo_ll_hdr_len += h->caplen;
257 nd_print_trunc(ndo);
258 return;
259 }
260 ndo->ndo_ll_hdr_len += sizeof (pcap_usb_header);
261
262 usb_header_print(ndo, (const pcap_usb_header *) p);
263 }
264
265 #ifdef DLT_USB_LINUX_MMAPPED
266 /*
267 * This is the top level routine of the printer for captures with a
268 * 64-byte header.
269 *
270 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
271 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
272 * is the number of bytes actually captured.
273 */
274 void
275 usb_linux_64_byte_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
276 const u_char *p)
277 {
278 ndo->ndo_protocol = "usb_linux_64_byte";
279 if (h->caplen < sizeof(pcap_usb_header_mmapped)) {
280 ndo->ndo_ll_hdr_len += h->caplen;
281 nd_print_trunc(ndo);
282 return;
283 }
284 ndo->ndo_ll_hdr_len += sizeof (pcap_usb_header_mmapped);
285
286 usb_header_print(ndo, (const pcap_usb_header *) p);
287 }
288 #endif /* DLT_USB_LINUX_MMAPPED */
289
290 #endif /* DLT_USB_LINUX */
291