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