1 /* 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 22 #include <sys/cdefs.h> 23 #ifndef lint 24 __RCSID("$NetBSD: print-usb.c,v 1.5 2024/09/02 16:15:33 christos Exp $"); 25 #endif 26 27 /* \summary: USB printer */ 28 29 #include <config.h> 30 31 #include "netdissect-stdinc.h" 32 33 #define ND_LONGJMP_FROM_TCHECK 34 #include "netdissect.h" 35 #include "extract.h" 36 37 #ifdef DLT_USB_LINUX 38 /* 39 * possible transfer mode 40 */ 41 #define URB_TRANSFER_IN 0x80 42 #define URB_ISOCHRONOUS 0x0 43 #define URB_INTERRUPT 0x1 44 #define URB_CONTROL 0x2 45 #define URB_BULK 0x3 46 47 /* 48 * possible event type 49 */ 50 #define URB_SUBMIT 'S' 51 #define URB_COMPLETE 'C' 52 #define URB_ERROR 'E' 53 54 /* 55 * USB setup header as defined in USB specification. 56 * Appears at the front of each Control S-type packet in DLT_USB captures. 57 */ 58 typedef struct _usb_setup { 59 nd_uint8_t bmRequestType; 60 nd_uint8_t bRequest; 61 nd_uint16_t wValue; 62 nd_uint16_t wIndex; 63 nd_uint16_t wLength; 64 } pcap_usb_setup; 65 66 /* 67 * Information from the URB for Isochronous transfers. 68 */ 69 typedef struct _iso_rec { 70 nd_int32_t error_count; 71 nd_int32_t numdesc; 72 } iso_rec; 73 74 /* 75 * Header prepended by linux kernel to each event. 76 * Appears at the front of each packet in DLT_USB_LINUX captures. 77 */ 78 typedef struct _usb_header { 79 nd_uint64_t id; 80 nd_uint8_t event_type; 81 nd_uint8_t transfer_type; 82 nd_uint8_t endpoint_number; 83 nd_uint8_t device_address; 84 nd_uint16_t bus_id; 85 nd_uint8_t setup_flag;/*if !=0 the urb setup header is not present*/ 86 nd_uint8_t data_flag; /*if !=0 no urb data is present*/ 87 nd_int64_t ts_sec; 88 nd_int32_t ts_usec; 89 nd_int32_t status; 90 nd_uint32_t urb_len; 91 nd_uint32_t data_len; /* amount of urb data really present in this event*/ 92 pcap_usb_setup setup; 93 } pcap_usb_header; 94 95 /* 96 * Header prepended by linux kernel to each event for the 2.6.31 97 * and later kernels; for the 2.6.21 through 2.6.30 kernels, the 98 * "iso_rec" information, and the fields starting with "interval" 99 * are zeroed-out padding fields. 100 * 101 * Appears at the front of each packet in DLT_USB_LINUX_MMAPPED captures. 102 */ 103 typedef struct _usb_header_mmapped { 104 nd_uint64_t id; 105 nd_uint8_t event_type; 106 nd_uint8_t transfer_type; 107 nd_uint8_t endpoint_number; 108 nd_uint8_t device_address; 109 nd_uint16_t bus_id; 110 nd_uint8_t setup_flag;/*if !=0 the urb setup header is not present*/ 111 nd_uint8_t data_flag; /*if !=0 no urb data is present*/ 112 nd_int64_t ts_sec; 113 nd_int32_t ts_usec; 114 nd_int32_t status; 115 nd_uint32_t urb_len; 116 nd_uint32_t data_len; /* amount of urb data really present in this event*/ 117 union { 118 pcap_usb_setup setup; 119 iso_rec iso; 120 } s; 121 nd_int32_t interval; /* for Interrupt and Isochronous events */ 122 nd_int32_t start_frame; /* for Isochronous events */ 123 nd_uint32_t xfer_flags; /* copy of URB's transfer flags */ 124 nd_uint32_t ndesc; /* number of isochronous descriptors */ 125 } pcap_usb_header_mmapped; 126 127 /* 128 * Isochronous descriptors; for isochronous transfers there might be 129 * one or more of these at the beginning of the packet data. The 130 * number of descriptors is given by the "ndesc" field in the header; 131 * as indicated, in older kernels that don't put the descriptors at 132 * the beginning of the packet, that field is zeroed out, so that field 133 * can be trusted even in captures from older kernels. 134 */ 135 typedef struct _usb_isodesc { 136 nd_int32_t status; 137 nd_uint32_t offset; 138 nd_uint32_t len; 139 nd_byte pad[4]; 140 } usb_isodesc; 141 142 143 /* returns direction: 1=inbound 2=outbound -1=invalid */ 144 static int 145 get_direction(int transfer_type, int event_type) 146 { 147 int direction; 148 149 direction = -1; 150 switch(transfer_type){ 151 case URB_BULK: 152 case URB_CONTROL: 153 case URB_ISOCHRONOUS: 154 switch(event_type) { 155 case URB_SUBMIT: 156 direction = 2; 157 break; 158 case URB_COMPLETE: 159 case URB_ERROR: 160 direction = 1; 161 break; 162 default: 163 direction = -1; 164 } 165 break; 166 case URB_INTERRUPT: 167 switch(event_type) { 168 case URB_SUBMIT: 169 direction = 1; 170 break; 171 case URB_COMPLETE: 172 case URB_ERROR: 173 direction = 2; 174 break; 175 default: 176 direction = -1; 177 } 178 break; 179 default: 180 direction = -1; 181 } 182 183 return direction; 184 } 185 186 static void 187 usb_header_print(netdissect_options *ndo, const pcap_usb_header *uh) 188 { 189 int direction; 190 uint8_t transfer_type, event_type; 191 192 ndo->ndo_protocol = "usb"; 193 194 nd_print_protocol_caps(ndo); 195 if (ndo->ndo_qflag) 196 return; 197 198 ND_PRINT(" "); 199 transfer_type = GET_U_1(uh->transfer_type); 200 switch(transfer_type) { 201 case URB_ISOCHRONOUS: 202 ND_PRINT("ISOCHRONOUS"); 203 break; 204 case URB_INTERRUPT: 205 ND_PRINT("INTERRUPT"); 206 break; 207 case URB_CONTROL: 208 ND_PRINT("CONTROL"); 209 break; 210 case URB_BULK: 211 ND_PRINT("BULK"); 212 break; 213 default: 214 ND_PRINT(" ?"); 215 } 216 217 event_type = GET_U_1(uh->event_type); 218 switch(event_type) { 219 case URB_SUBMIT: 220 ND_PRINT(" SUBMIT"); 221 break; 222 case URB_COMPLETE: 223 ND_PRINT(" COMPLETE"); 224 break; 225 case URB_ERROR: 226 ND_PRINT(" ERROR"); 227 break; 228 default: 229 ND_PRINT(" ?"); 230 } 231 232 direction = get_direction(transfer_type, event_type); 233 if(direction == 1) 234 ND_PRINT(" from"); 235 else if(direction == 2) 236 ND_PRINT(" to"); 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); 240 } 241 242 /* 243 * This is the top level routine of the printer for captures with a 244 * 48-byte header. 245 * 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. 249 */ 250 void 251 usb_linux_48_byte_if_print(netdissect_options *ndo, 252 const struct pcap_pkthdr *h _U_, const u_char *p) 253 { 254 ndo->ndo_protocol = "usb_linux_48_byte"; 255 ND_TCHECK_LEN(p, sizeof(pcap_usb_header)); 256 ndo->ndo_ll_hdr_len += sizeof (pcap_usb_header); 257 258 usb_header_print(ndo, (const pcap_usb_header *) p); 259 } 260 261 #ifdef DLT_USB_LINUX_MMAPPED 262 /* 263 * This is the top level routine of the printer for captures with a 264 * 64-byte header. 265 * 266 * 'p' points to the header of the packet, 'h->ts' is the timestamp, 267 * 'h->len' is the length of the packet off the wire, and 'h->caplen' 268 * is the number of bytes actually captured. 269 */ 270 void 271 usb_linux_64_byte_if_print(netdissect_options *ndo, 272 const struct pcap_pkthdr *h _U_, const u_char *p) 273 { 274 ndo->ndo_protocol = "usb_linux_64_byte"; 275 ND_TCHECK_LEN(p, sizeof(pcap_usb_header_mmapped)); 276 ndo->ndo_ll_hdr_len += sizeof (pcap_usb_header_mmapped); 277 278 usb_header_print(ndo, (const pcap_usb_header *) p); 279 } 280 #endif /* DLT_USB_LINUX_MMAPPED */ 281 282 #endif /* DLT_USB_LINUX */ 283 284