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 #define ND_LONGJMP_FROM_TCHECK
31 #include "netdissect.h"
36 * possible transfer mode
38 #define URB_TRANSFER_IN 0x80
39 #define URB_ISOCHRONOUS 0x0
40 #define URB_INTERRUPT 0x1
41 #define URB_CONTROL 0x2
47 #define URB_SUBMIT 'S'
48 #define URB_COMPLETE 'C'
52 * USB setup header as defined in USB specification.
53 * Appears at the front of each Control S-type packet in DLT_USB captures.
55 typedef struct _usb_setup
{
56 nd_uint8_t bmRequestType
;
64 * Information from the URB for Isochronous transfers.
66 typedef struct _iso_rec
{
67 nd_int32_t error_count
;
72 * Header prepended by linux kernel to each event.
73 * Appears at the front of each packet in DLT_USB_LINUX captures.
75 typedef struct _usb_header
{
77 nd_uint8_t event_type
;
78 nd_uint8_t transfer_type
;
79 nd_uint8_t endpoint_number
;
80 nd_uint8_t device_address
;
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*/
88 nd_uint32_t data_len
; /* amount of urb data really present in this event*/
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.
98 * Appears at the front of each packet in DLT_USB_LINUX_MMAPPED captures.
100 typedef struct _usb_header_mmapped
{
102 nd_uint8_t event_type
;
103 nd_uint8_t transfer_type
;
104 nd_uint8_t endpoint_number
;
105 nd_uint8_t device_address
;
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*/
113 nd_uint32_t data_len
; /* amount of urb data really present in this event*/
115 pcap_usb_setup setup
;
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
;
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.
132 typedef struct _usb_isodesc
{
140 /* returns direction: 1=inbound 2=outbound -1=invalid */
142 get_direction(int transfer_type
, int event_type
)
147 switch(transfer_type
){
150 case URB_ISOCHRONOUS
:
186 usb_header_print(netdissect_options
*ndo
, const pcap_usb_header
*uh
)
189 uint8_t transfer_type
, event_type
;
191 ndo
->ndo_protocol
= "usb";
193 nd_print_protocol_caps(ndo
);
198 transfer_type
= GET_U_1(uh
->transfer_type
);
199 switch(transfer_type
)
201 case URB_ISOCHRONOUS
:
202 ND_PRINT("ISOCHRONOUS");
205 ND_PRINT("INTERRUPT");
217 event_type
= GET_U_1(uh
->event_type
);
224 ND_PRINT(" COMPLETE");
233 direction
= get_direction(transfer_type
, event_type
);
236 else if(direction
== 2)
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);
244 * This is the top level routine of the printer for captures with a
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.
252 usb_linux_48_byte_if_print(netdissect_options
*ndo
,
253 const struct pcap_pkthdr
*h _U_
, const u_char
*p
)
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
);
259 usb_header_print(ndo
, (const pcap_usb_header
*) p
);
262 #ifdef DLT_USB_LINUX_MMAPPED
264 * This is the top level routine of the printer for captures with a
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.
272 usb_linux_64_byte_if_print(netdissect_options
*ndo
,
273 const struct pcap_pkthdr
*h _U_
, const u_char
*p
)
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
);
279 usb_header_print(ndo
, (const pcap_usb_header
*) p
);
281 #endif /* DLT_USB_LINUX_MMAPPED */
283 #endif /* DLT_USB_LINUX */