]> The Tcpdump Group git mirrors - tcpdump/blob - print-usb.c
Add more nd_print_trunc() calls
[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 transfer_type = EXTRACT_U_1(uh->transfer_type);
191 switch(transfer_type)
192 {
193 case URB_ISOCHRONOUS:
194 ND_PRINT("ISOCHRONOUS");
195 break;
196 case URB_INTERRUPT:
197 ND_PRINT("INTERRUPT");
198 break;
199 case URB_CONTROL:
200 ND_PRINT("CONTROL");
201 break;
202 case URB_BULK:
203 ND_PRINT("BULK");
204 break;
205 default:
206 ND_PRINT(" ?");
207 }
208
209 event_type = EXTRACT_U_1(uh->event_type);
210 switch(event_type)
211 {
212 case URB_SUBMIT:
213 ND_PRINT(" SUBMIT");
214 break;
215 case URB_COMPLETE:
216 ND_PRINT(" COMPLETE");
217 break;
218 case URB_ERROR:
219 ND_PRINT(" ERROR");
220 break;
221 default:
222 ND_PRINT(" ?");
223 }
224
225 direction = get_direction(transfer_type, event_type);
226 if(direction == 1)
227 ND_PRINT(" from");
228 else if(direction == 2)
229 ND_PRINT(" to");
230 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);
231 }
232
233 /*
234 * This is the top level routine of the printer for captures with a
235 * 48-byte header.
236 *
237 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
238 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
239 * is the number of bytes actually captured.
240 */
241 u_int
242 usb_linux_48_byte_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
243 const u_char *p)
244 {
245 ndo->ndo_protocol = "usb_linux_48_byte_if";
246 if (h->caplen < sizeof(pcap_usb_header)) {
247 nd_print_trunc(ndo);
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 ndo->ndo_protocol = "usb_linux_64_byte_if";
270 if (h->caplen < sizeof(pcap_usb_header_mmapped)) {
271 nd_print_trunc(ndo);
272 return(sizeof(pcap_usb_header_mmapped));
273 }
274
275 usb_header_print(ndo, (const pcap_usb_header *) p);
276
277 return(sizeof(pcap_usb_header_mmapped));
278 }
279 #endif /* DLT_USB_LINUX_MMAPPED */
280
281 #endif /* DLT_USB_LINUX */
282