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 #include <config.h>
25
26 #include "netdissect-stdinc.h"
27
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
30 #include "extract.h"
31
32 #ifdef DLT_USB_LINUX
33 /*
34 * possible transfer mode
35 */
36 #define URB_TRANSFER_IN 0x80
37 #define URB_ISOCHRONOUS 0x0
38 #define URB_INTERRUPT 0x1
39 #define URB_CONTROL 0x2
40 #define URB_BULK 0x3
41
42 /*
43 * possible event type
44 */
45 #define URB_SUBMIT 'S'
46 #define URB_COMPLETE 'C'
47 #define URB_ERROR 'E'
48
49 /*
50 * USB setup header as defined in USB specification.
51 * Appears at the front of each Control S-type packet in DLT_USB captures.
52 */
53 typedef struct _usb_setup {
54 nd_uint8_t bmRequestType;
55 nd_uint8_t bRequest;
56 nd_uint16_t wValue;
57 nd_uint16_t wIndex;
58 nd_uint16_t wLength;
59 } pcap_usb_setup;
60
61 /*
62 * Information from the URB for Isochronous transfers.
63 */
64 typedef struct _iso_rec {
65 nd_int32_t error_count;
66 nd_int32_t numdesc;
67 } iso_rec;
68
69 /*
70 * Header prepended by linux kernel to each event.
71 * Appears at the front of each packet in DLT_USB_LINUX captures.
72 */
73 typedef struct _usb_header {
74 nd_uint64_t id;
75 nd_uint8_t event_type;
76 nd_uint8_t transfer_type;
77 nd_uint8_t endpoint_number;
78 nd_uint8_t device_address;
79 nd_uint16_t bus_id;
80 nd_uint8_t setup_flag;/*if !=0 the urb setup header is not present*/
81 nd_uint8_t data_flag; /*if !=0 no urb data is present*/
82 nd_int64_t ts_sec;
83 nd_int32_t ts_usec;
84 nd_int32_t status;
85 nd_uint32_t urb_len;
86 nd_uint32_t data_len; /* amount of urb data really present in this event*/
87 pcap_usb_setup setup;
88 } pcap_usb_header;
89
90 /*
91 * Header prepended by linux kernel to each event for the 2.6.31
92 * and later kernels; for the 2.6.21 through 2.6.30 kernels, the
93 * "iso_rec" information, and the fields starting with "interval"
94 * are zeroed-out padding fields.
95 *
96 * Appears at the front of each packet in DLT_USB_LINUX_MMAPPED captures.
97 */
98 typedef struct _usb_header_mmapped {
99 nd_uint64_t id;
100 nd_uint8_t event_type;
101 nd_uint8_t transfer_type;
102 nd_uint8_t endpoint_number;
103 nd_uint8_t device_address;
104 nd_uint16_t bus_id;
105 nd_uint8_t setup_flag;/*if !=0 the urb setup header is not present*/
106 nd_uint8_t data_flag; /*if !=0 no urb data is present*/
107 nd_int64_t ts_sec;
108 nd_int32_t ts_usec;
109 nd_int32_t status;
110 nd_uint32_t urb_len;
111 nd_uint32_t data_len; /* amount of urb data really present in this event*/
112 union {
113 pcap_usb_setup setup;
114 iso_rec iso;
115 } s;
116 nd_int32_t interval; /* for Interrupt and Isochronous events */
117 nd_int32_t start_frame; /* for Isochronous events */
118 nd_uint32_t xfer_flags; /* copy of URB's transfer flags */
119 nd_uint32_t ndesc; /* number of isochronous descriptors */
120 } pcap_usb_header_mmapped;
121
122 /*
123 * Isochronous descriptors; for isochronous transfers there might be
124 * one or more of these at the beginning of the packet data. The
125 * number of descriptors is given by the "ndesc" field in the header;
126 * as indicated, in older kernels that don't put the descriptors at
127 * the beginning of the packet, that field is zeroed out, so that field
128 * can be trusted even in captures from older kernels.
129 */
130 typedef struct _usb_isodesc {
131 nd_int32_t status;
132 nd_uint32_t offset;
133 nd_uint32_t len;
134 nd_byte pad[4];
135 } usb_isodesc;
136
137
138 /* returns direction: 1=inbound 2=outbound -1=invalid */
139 static int
get_direction(int transfer_type,int event_type)140 get_direction(int transfer_type, int event_type)
141 {
142 int direction;
143
144 direction = -1;
145 switch(transfer_type){
146 case URB_BULK:
147 case URB_CONTROL:
148 case URB_ISOCHRONOUS:
149 switch(event_type) {
150 case URB_SUBMIT:
151 direction = 2;
152 break;
153 case URB_COMPLETE:
154 case URB_ERROR:
155 direction = 1;
156 break;
157 default:
158 direction = -1;
159 }
160 break;
161 case URB_INTERRUPT:
162 switch(event_type) {
163 case URB_SUBMIT:
164 direction = 1;
165 break;
166 case URB_COMPLETE:
167 case URB_ERROR:
168 direction = 2;
169 break;
170 default:
171 direction = -1;
172 }
173 break;
174 default:
175 direction = -1;
176 }
177
178 return direction;
179 }
180
181 static void
usb_header_print(netdissect_options * ndo,const pcap_usb_header * uh)182 usb_header_print(netdissect_options *ndo, const pcap_usb_header *uh)
183 {
184 int direction;
185 uint8_t transfer_type, event_type;
186
187 ndo->ndo_protocol = "usb";
188
189 nd_print_protocol_caps(ndo);
190 if (ndo->ndo_qflag)
191 return;
192
193 ND_PRINT(" ");
194 transfer_type = GET_U_1(uh->transfer_type);
195 switch(transfer_type) {
196 case URB_ISOCHRONOUS:
197 ND_PRINT("ISOCHRONOUS");
198 break;
199 case URB_INTERRUPT:
200 ND_PRINT("INTERRUPT");
201 break;
202 case URB_CONTROL:
203 ND_PRINT("CONTROL");
204 break;
205 case URB_BULK:
206 ND_PRINT("BULK");
207 break;
208 default:
209 ND_PRINT(" ?");
210 }
211
212 event_type = GET_U_1(uh->event_type);
213 switch(event_type) {
214 case URB_SUBMIT:
215 ND_PRINT(" SUBMIT");
216 break;
217 case URB_COMPLETE:
218 ND_PRINT(" COMPLETE");
219 break;
220 case URB_ERROR:
221 ND_PRINT(" ERROR");
222 break;
223 default:
224 ND_PRINT(" ?");
225 }
226
227 direction = get_direction(transfer_type, event_type);
228 if(direction == 1)
229 ND_PRINT(" from");
230 else if(direction == 2)
231 ND_PRINT(" to");
232 ND_PRINT(" %u:%u:%u", GET_HE_U_2(uh->bus_id),
233 GET_U_1(uh->device_address),
234 GET_U_1(uh->endpoint_number) & 0x7f);
235 }
236
237 /*
238 * This is the top level routine of the printer for captures with a
239 * 48-byte header.
240 *
241 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
242 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
243 * is the number of bytes actually captured.
244 */
245 void
usb_linux_48_byte_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h _U_,const u_char * p)246 usb_linux_48_byte_if_print(netdissect_options *ndo,
247 const struct pcap_pkthdr *h _U_, const u_char *p)
248 {
249 ndo->ndo_protocol = "usb_linux_48_byte";
250 ND_TCHECK_LEN(p, sizeof(pcap_usb_header));
251 ndo->ndo_ll_hdr_len += sizeof (pcap_usb_header);
252
253 usb_header_print(ndo, (const pcap_usb_header *) p);
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 void
usb_linux_64_byte_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h _U_,const u_char * p)266 usb_linux_64_byte_if_print(netdissect_options *ndo,
267 const struct pcap_pkthdr *h _U_, const u_char *p)
268 {
269 ndo->ndo_protocol = "usb_linux_64_byte";
270 ND_TCHECK_LEN(p, sizeof(pcap_usb_header_mmapped));
271 ndo->ndo_ll_hdr_len += sizeof (pcap_usb_header_mmapped);
272
273 usb_header_print(ndo, (const pcap_usb_header *) p);
274 }
275 #endif /* DLT_USB_LINUX_MMAPPED */
276
277 #endif /* DLT_USB_LINUX */
278
279