blob: 3f5937ba16cab5037183b22ad9b2c45f8d74eb22 [file] [log] [blame]
JP Abgrall53f17a92014-02-12 14:02:41 -08001/*
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
Elliott Hughese2e3bd12017-05-15 10:59:29 -070022/* \summary: USB printer */
23
JP Abgrall53f17a92014-02-12 14:02:41 -080024#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070025#include <config.h>
JP Abgrall53f17a92014-02-12 14:02:41 -080026#endif
27
Elliott Hughes820eced2021-08-20 18:00:50 -070028#include "netdissect-stdinc.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080029
Elliott Hughes820eced2021-08-20 18:00:50 -070030#define ND_LONGJMP_FROM_TCHECK
Elliott Hughese2e3bd12017-05-15 10:59:29 -070031#include "netdissect.h"
Elliott Hughes820eced2021-08-20 18:00:50 -070032#include "extract.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080033
Elliott Hughes820eced2021-08-20 18:00:50 -070034#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
JP Abgrall53f17a92014-02-12 14:02:41 -080043
Elliott Hughes820eced2021-08-20 18:00:50 -070044/*
45 * possible event type
46 */
47#define URB_SUBMIT 'S'
48#define URB_COMPLETE 'C'
49#define URB_ERROR 'E'
JP Abgrall53f17a92014-02-12 14:02:41 -080050
Elliott Hughes820eced2021-08-20 18:00:50 -070051/*
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 */
55typedef 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 */
66typedef 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 */
75typedef 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 */
100typedef 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 */
132typedef 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
Elliott Hughes892a68b2015-10-19 14:43:53 -0700139
JP Abgrall53f17a92014-02-12 14:02:41 -0800140/* returns direction: 1=inbound 2=outbound -1=invalid */
141static int
142get_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
185static void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700186usb_header_print(netdissect_options *ndo, const pcap_usb_header *uh)
JP Abgrall53f17a92014-02-12 14:02:41 -0800187{
188 int direction;
Elliott Hughes820eced2021-08-20 18:00:50 -0700189 uint8_t transfer_type, event_type;
JP Abgrall53f17a92014-02-12 14:02:41 -0800190
Elliott Hughes820eced2021-08-20 18:00:50 -0700191 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)
JP Abgrall53f17a92014-02-12 14:02:41 -0800200 {
201 case URB_ISOCHRONOUS:
Elliott Hughes820eced2021-08-20 18:00:50 -0700202 ND_PRINT("ISOCHRONOUS");
JP Abgrall53f17a92014-02-12 14:02:41 -0800203 break;
204 case URB_INTERRUPT:
Elliott Hughes820eced2021-08-20 18:00:50 -0700205 ND_PRINT("INTERRUPT");
JP Abgrall53f17a92014-02-12 14:02:41 -0800206 break;
207 case URB_CONTROL:
Elliott Hughes820eced2021-08-20 18:00:50 -0700208 ND_PRINT("CONTROL");
JP Abgrall53f17a92014-02-12 14:02:41 -0800209 break;
210 case URB_BULK:
Elliott Hughes820eced2021-08-20 18:00:50 -0700211 ND_PRINT("BULK");
JP Abgrall53f17a92014-02-12 14:02:41 -0800212 break;
213 default:
Elliott Hughes820eced2021-08-20 18:00:50 -0700214 ND_PRINT(" ?");
JP Abgrall53f17a92014-02-12 14:02:41 -0800215 }
216
Elliott Hughes820eced2021-08-20 18:00:50 -0700217 event_type = GET_U_1(uh->event_type);
218 switch(event_type)
JP Abgrall53f17a92014-02-12 14:02:41 -0800219 {
220 case URB_SUBMIT:
Elliott Hughes820eced2021-08-20 18:00:50 -0700221 ND_PRINT(" SUBMIT");
JP Abgrall53f17a92014-02-12 14:02:41 -0800222 break;
223 case URB_COMPLETE:
Elliott Hughes820eced2021-08-20 18:00:50 -0700224 ND_PRINT(" COMPLETE");
JP Abgrall53f17a92014-02-12 14:02:41 -0800225 break;
226 case URB_ERROR:
Elliott Hughes820eced2021-08-20 18:00:50 -0700227 ND_PRINT(" ERROR");
JP Abgrall53f17a92014-02-12 14:02:41 -0800228 break;
229 default:
Elliott Hughes820eced2021-08-20 18:00:50 -0700230 ND_PRINT(" ?");
JP Abgrall53f17a92014-02-12 14:02:41 -0800231 }
232
Elliott Hughes820eced2021-08-20 18:00:50 -0700233 direction = get_direction(transfer_type, event_type);
JP Abgrall53f17a92014-02-12 14:02:41 -0800234 if(direction == 1)
Elliott Hughes820eced2021-08-20 18:00:50 -0700235 ND_PRINT(" from");
JP Abgrall53f17a92014-02-12 14:02:41 -0800236 else if(direction == 2)
Elliott Hughes820eced2021-08-20 18:00:50 -0700237 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);
JP Abgrall53f17a92014-02-12 14:02:41 -0800241}
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 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700251void
252usb_linux_48_byte_if_print(netdissect_options *ndo,
253 const struct pcap_pkthdr *h _U_, const u_char *p)
JP Abgrall53f17a92014-02-12 14:02:41 -0800254{
Elliott Hughes820eced2021-08-20 18:00:50 -0700255 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);
JP Abgrall53f17a92014-02-12 14:02:41 -0800258
Elliott Hughes892a68b2015-10-19 14:43:53 -0700259 usb_header_print(ndo, (const pcap_usb_header *) p);
JP Abgrall53f17a92014-02-12 14:02:41 -0800260}
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 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700271void
272usb_linux_64_byte_if_print(netdissect_options *ndo,
273 const struct pcap_pkthdr *h _U_, const u_char *p)
JP Abgrall53f17a92014-02-12 14:02:41 -0800274{
Elliott Hughes820eced2021-08-20 18:00:50 -0700275 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);
JP Abgrall53f17a92014-02-12 14:02:41 -0800278
Elliott Hughes892a68b2015-10-19 14:43:53 -0700279 usb_header_print(ndo, (const pcap_usb_header *) p);
JP Abgrall53f17a92014-02-12 14:02:41 -0800280}
281#endif /* DLT_USB_LINUX_MMAPPED */
282
Elliott Hughes820eced2021-08-20 18:00:50 -0700283#endif /* DLT_USB_LINUX */
JP Abgrall53f17a92014-02-12 14:02:41 -0800284