2 * Copyright 2009 Bert Vermeulen <bert@biot.com>
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.
18 * Support for USB packets
22 /* \summary: USB printer */
28 #include "netdissect-stdinc.h"
30 #include "netdissect.h"
35 * possible transfer mode
37 #define URB_TRANSFER_IN 0x80
38 #define URB_ISOCHRONOUS 0x0
39 #define URB_INTERRUPT 0x1
40 #define URB_CONTROL 0x2
46 #define URB_SUBMIT 'S'
47 #define URB_COMPLETE 'C'
51 * USB setup header as defined in USB specification.
52 * Appears at the front of each Control S-type packet in DLT_USB captures.
54 typedef struct _usb_setup
{
55 nd_uint8_t bmRequestType
;
63 * Information from the URB for Isochronous transfers.
65 typedef struct _iso_rec
{
66 nd_int32_t error_count
;
71 * Header prepended by linux kernel to each event.
72 * Appears at the front of each packet in DLT_USB_LINUX captures.
74 typedef struct _usb_header
{
76 nd_uint8_t event_type
;
77 nd_uint8_t transfer_type
;
78 nd_uint8_t endpoint_number
;
79 nd_uint8_t device_address
;
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*/
87 nd_uint32_t data_len
; /* amount of urb data really present in this event*/
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.
97 * Appears at the front of each packet in DLT_USB_LINUX_MMAPPED captures.
99 typedef struct _usb_header_mmapped
{
101 nd_uint8_t event_type
;
102 nd_uint8_t transfer_type
;
103 nd_uint8_t endpoint_number
;
104 nd_uint8_t device_address
;
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*/
112 nd_uint32_t data_len
; /* amount of urb data really present in this event*/
114 pcap_usb_setup setup
;
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
;
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.
131 typedef struct _usb_isodesc
{
139 /* returns direction: 1=inbound 2=outbound -1=invalid */
141 get_direction(int transfer_type
, int event_type
)
146 switch(transfer_type
){
149 case URB_ISOCHRONOUS
:
185 usb_header_print(netdissect_options
*ndo
, const pcap_usb_header
*uh
)
188 uint8_t transfer_type
, event_type
;
190 ndo
->ndo_protocol
= "usb";
192 nd_print_protocol_caps(ndo
);
197 transfer_type
= GET_U_1(uh
->transfer_type
);
198 switch(transfer_type
)
200 case URB_ISOCHRONOUS
:
201 ND_PRINT("ISOCHRONOUS");
204 ND_PRINT("INTERRUPT");
216 event_type
= GET_U_1(uh
->event_type
);
223 ND_PRINT(" COMPLETE");
232 direction
= get_direction(transfer_type
, event_type
);
235 else if(direction
== 2)
237 ND_PRINT(" %u:%u:%u", GET_HE_U_2(uh
->bus_id
),
238 GET_U_1(uh
->device_address
),
239 GET_U_1(uh
->endpoint_number
) & 0x7f);
243 * This is the top level routine of the printer for captures with a
246 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
247 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
248 * is the number of bytes actually captured.
251 usb_linux_48_byte_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
254 ndo
->ndo_protocol
= "usb_linux_48_byte";
255 if (h
->caplen
< sizeof(pcap_usb_header
)) {
256 ndo
->ndo_ll_hdr_len
+= h
->caplen
;
260 ndo
->ndo_ll_hdr_len
+= sizeof (pcap_usb_header
);
262 usb_header_print(ndo
, (const pcap_usb_header
*) p
);
267 #ifdef DLT_USB_LINUX_MMAPPED
269 * This is the top level routine of the printer for captures with a
272 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
273 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
274 * is the number of bytes actually captured.
277 usb_linux_64_byte_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
280 ndo
->ndo_protocol
= "usb_linux_64_byte";
281 if (h
->caplen
< sizeof(pcap_usb_header_mmapped
)) {
282 ndo
->ndo_ll_hdr_len
+= h
->caplen
;
286 ndo
->ndo_ll_hdr_len
+= sizeof (pcap_usb_header_mmapped
);
288 usb_header_print(ndo
, (const pcap_usb_header
*) p
);
292 #endif /* DLT_USB_LINUX_MMAPPED */
294 #endif /* DLT_USB_LINUX */