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 */
26 #include "netdissect-stdinc.h"
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
34 * possible transfer mode
36 #define URB_TRANSFER_IN 0x80
37 #define URB_ISOCHRONOUS 0x0
38 #define URB_INTERRUPT 0x1
39 #define URB_CONTROL 0x2
45 #define URB_SUBMIT 'S'
46 #define URB_COMPLETE 'C'
50 * USB setup header as defined in USB specification.
51 * Appears at the front of each Control S-type packet in DLT_USB captures.
53 typedef struct _usb_setup
{
54 nd_uint8_t bmRequestType
;
62 * Information from the URB for Isochronous transfers.
64 typedef struct _iso_rec
{
65 nd_int32_t error_count
;
70 * Header prepended by linux kernel to each event.
71 * Appears at the front of each packet in DLT_USB_LINUX captures.
73 typedef struct _usb_header
{
75 nd_uint8_t event_type
;
76 nd_uint8_t transfer_type
;
77 nd_uint8_t endpoint_number
;
78 nd_uint8_t device_address
;
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*/
86 nd_uint32_t data_len
; /* amount of urb data really present in this event*/
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.
96 * Appears at the front of each packet in DLT_USB_LINUX_MMAPPED captures.
98 typedef struct _usb_header_mmapped
{
100 nd_uint8_t event_type
;
101 nd_uint8_t transfer_type
;
102 nd_uint8_t endpoint_number
;
103 nd_uint8_t device_address
;
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*/
111 nd_uint32_t data_len
; /* amount of urb data really present in this event*/
113 pcap_usb_setup setup
;
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
;
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.
130 typedef struct _usb_isodesc
{
138 /* returns direction: 1=inbound 2=outbound -1=invalid */
140 get_direction(int transfer_type
, int event_type
)
145 switch(transfer_type
){
148 case URB_ISOCHRONOUS
:
182 usb_header_print(netdissect_options
*ndo
, const pcap_usb_header
*uh
)
185 uint8_t transfer_type
, event_type
;
187 ndo
->ndo_protocol
= "usb";
189 nd_print_protocol_caps(ndo
);
194 transfer_type
= GET_U_1(uh
->transfer_type
);
195 switch(transfer_type
) {
196 case URB_ISOCHRONOUS
:
197 ND_PRINT("ISOCHRONOUS");
200 ND_PRINT("INTERRUPT");
212 event_type
= GET_U_1(uh
->event_type
);
218 ND_PRINT(" COMPLETE");
227 direction
= get_direction(transfer_type
, event_type
);
230 else if(direction
== 2)
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);
238 * This is the top level routine of the printer for captures with a
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.
246 usb_linux_48_byte_if_print(netdissect_options
*ndo
,
247 const struct pcap_pkthdr
*h _U_
, const u_char
*p
)
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
);
253 usb_header_print(ndo
, (const pcap_usb_header
*) p
);
256 #ifdef DLT_USB_LINUX_MMAPPED
258 * This is the top level routine of the printer for captures with a
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.
266 usb_linux_64_byte_if_print(netdissect_options
*ndo
,
267 const struct pcap_pkthdr
*h _U_
, const u_char
*p
)
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
);
273 usb_header_print(ndo
, (const pcap_usb_header
*) p
);
275 #endif /* DLT_USB_LINUX_MMAPPED */
277 #endif /* DLT_USB_LINUX */