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