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