| 1 | /* |
| 2 | * usb packet capture |
| 3 | * |
| 4 | * Copyright (c) 2021 Gerd Hoffmann <kraxel@redhat.com> |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 7 | * See the COPYING file in the top-level directory. |
| 8 | */ |
| 9 | |
| 10 | #include "qemu/osdep.h" |
| 11 | #include "qemu/pcap.h" |
| 12 | #include "hw/usb/usb.h" |
| 13 | |
| 14 | /* https://www.tcpdump.org/linktypes.html */ |
| 15 | /* linux: Documentation/usb/usbmon.rst */ |
| 16 | /* linux: drivers/usb/mon/mon_bin.c */ |
| 17 | |
| 18 | #define LINKTYPE_USB_LINUX 189 /* first 48 bytes only */ |
| 19 | #define LINKTYPE_USB_LINUX_MMAPPED 220 /* full 64 byte header */ |
| 20 | |
| 21 | struct usbmon_packet { |
| 22 | uint64_t id; /* 0: URB ID - from submission to callback */ |
| 23 | unsigned char type; /* 8: Same as text; extensible. */ |
| 24 | unsigned char xfer_type; /* ISO (0), Intr, Control, Bulk (3) */ |
| 25 | unsigned char epnum; /* Endpoint number and transfer direction */ |
| 26 | unsigned char devnum; /* Device address */ |
| 27 | uint16_t busnum; /* 12: Bus number */ |
| 28 | char flag_setup; /* 14: Same as text */ |
| 29 | char flag_data; /* 15: Same as text; Binary zero is OK. */ |
| 30 | int64_t ts_sec; /* 16: gettimeofday */ |
| 31 | int32_t ts_usec; /* 24: gettimeofday */ |
| 32 | int32_t status; /* 28: */ |
| 33 | unsigned int length; /* 32: Length of data (submitted or actual) */ |
| 34 | unsigned int len_cap; /* 36: Delivered length */ |
| 35 | union { /* 40: */ |
| 36 | unsigned char setup[8]; /* Only for Control S-type */ |
| 37 | struct iso_rec { /* Only for ISO */ |
| 38 | int32_t error_count; |
| 39 | int32_t numdesc; |
| 40 | } iso; |
| 41 | } s; |
| 42 | int32_t interval; /* 48: Only for Interrupt and ISO */ |
| 43 | int32_t start_frame; /* 52: For ISO */ |
| 44 | uint32_t xfer_flags; /* 56: copy of URB's transfer_flags */ |
| 45 | uint32_t ndesc; /* 60: Actual number of ISO descriptors */ |
| 46 | }; /* 64 total length */ |
| 47 | |
| 48 | /* ------------------------------------------------------------------------ */ |
| 49 | |
| 50 | #define CTRL_LEN 4096 |
| 51 | #define DATA_LEN 256 |
| 52 | |
| 53 | static int usbmon_status(USBPacket *p) |
| 54 | { |
| 55 | switch (p->status) { |
| 56 | case USB_RET_SUCCESS: |
| 57 | return 0; |
| 58 | case USB_RET_NODEV: |
| 59 | return -19; /* -ENODEV */ |
| 60 | default: |
| 61 | return -121; /* -EREMOTEIO */ |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static unsigned int usbmon_epnum(USBPacket *p) |
| 66 | { |
| 67 | unsigned epnum = 0; |
| 68 | |
| 69 | epnum |= p->ep->nr; |
| 70 | epnum |= (p->pid == USB_TOKEN_IN) ? 0x80 : 0; |
| 71 | return epnum; |
| 72 | } |
| 73 | |
| 74 | static unsigned char usbmon_xfer_type[] = { |
| 75 | [USB_ENDPOINT_XFER_CONTROL] = 2, |
| 76 | [USB_ENDPOINT_XFER_ISOC] = 0, |
| 77 | [USB_ENDPOINT_XFER_BULK] = 3, |
| 78 | [USB_ENDPOINT_XFER_INT] = 1, |
| 79 | }; |
| 80 | |
| 81 | static void do_usb_pcap_header(FILE *fp, struct usbmon_packet *packet) |
| 82 | { |
| 83 | struct pcaprec_hdr header; |
| 84 | struct timeval tv; |
| 85 | |
| 86 | gettimeofday(&tv, NULL); |
| 87 | packet->ts_sec = tv.tv_sec; |
| 88 | packet->ts_usec = tv.tv_usec; |
| 89 | |
| 90 | header.ts_sec = packet->ts_sec; |
| 91 | header.ts_usec = packet->ts_usec; |
| 92 | header.incl_len = packet->len_cap; |
| 93 | header.orig_len = packet->length + sizeof(*packet); |
| 94 | fwrite(&header, sizeof(header), 1, fp); |
| 95 | fwrite(packet, sizeof(*packet), 1, fp); |
| 96 | } |
| 97 | |
| 98 | static void do_usb_pcap_ctrl(FILE *fp, USBPacket *p, bool setup) |
| 99 | { |
| 100 | USBDevice *dev = p->ep->dev; |
| 101 | bool in = dev->setup_buf[0] & USB_DIR_IN; |
| 102 | struct usbmon_packet packet = { |
| 103 | .id = 0, |
| 104 | .type = setup ? 'S' : 'C', |
| 105 | .xfer_type = usbmon_xfer_type[USB_ENDPOINT_XFER_CONTROL], |
| 106 | .epnum = in ? 0x80 : 0, |
| 107 | .devnum = dev->addr, |
| 108 | .flag_setup = setup ? 0 : '-', |
| 109 | .flag_data = '=', |
| 110 | .length = dev->setup_len, |
| 111 | }; |
| 112 | int data_len = dev->setup_len; |
| 113 | |
| 114 | if (data_len > CTRL_LEN) { |
| 115 | data_len = CTRL_LEN; |
| 116 | } |
| 117 | if (setup) { |
| 118 | memcpy(packet.s.setup, dev->setup_buf, 8); |
| 119 | } else { |
| 120 | packet.status = usbmon_status(p); |
| 121 | } |
| 122 | |
| 123 | if (in && setup) { |
| 124 | packet.flag_data = '<'; |
| 125 | packet.length = 0; |
| 126 | data_len = 0; |
| 127 | } |
| 128 | if (!in && !setup) { |
| 129 | packet.flag_data = '>'; |
| 130 | packet.length = 0; |
| 131 | data_len = 0; |
| 132 | } |
| 133 | |
| 134 | packet.len_cap = data_len + sizeof(packet); |
| 135 | do_usb_pcap_header(fp, &packet); |
| 136 | if (data_len) { |
| 137 | fwrite(dev->data_buf, data_len, 1, fp); |
| 138 | } |
| 139 | |
| 140 | fflush(fp); |
| 141 | } |
| 142 | |
| 143 | static void do_usb_pcap_data(FILE *fp, USBPacket *p, bool setup) |
| 144 | { |
| 145 | struct usbmon_packet packet = { |
| 146 | .id = p->id, |
| 147 | .type = setup ? 'S' : 'C', |
| 148 | .xfer_type = usbmon_xfer_type[p->ep->type], |
| 149 | .epnum = usbmon_epnum(p), |
| 150 | .devnum = p->ep->dev->addr, |
| 151 | .flag_setup = '-', |
| 152 | .flag_data = '=', |
| 153 | .length = p->iov.size, |
| 154 | }; |
| 155 | int data_len = p->iov.size; |
| 156 | |
| 157 | if (p->ep->nr == 0) { |
| 158 | /* ignore control pipe packets */ |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if (data_len > DATA_LEN) { |
| 163 | data_len = DATA_LEN; |
| 164 | } |
| 165 | if (!setup) { |
| 166 | packet.status = usbmon_status(p); |
| 167 | if (packet.length > p->actual_length) { |
| 168 | packet.length = p->actual_length; |
| 169 | } |
| 170 | if (data_len > p->actual_length) { |
| 171 | data_len = p->actual_length; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (p->pid == USB_TOKEN_IN && setup) { |
| 176 | packet.flag_data = '<'; |
| 177 | packet.length = 0; |
| 178 | data_len = 0; |
| 179 | } |
| 180 | if (p->pid == USB_TOKEN_OUT && !setup) { |
| 181 | packet.flag_data = '>'; |
| 182 | packet.length = 0; |
| 183 | data_len = 0; |
| 184 | } |
| 185 | |
| 186 | packet.len_cap = data_len + sizeof(packet); |
| 187 | do_usb_pcap_header(fp, &packet); |
| 188 | if (data_len) { |
| 189 | void *buf = g_malloc(data_len); |
| 190 | iov_to_buf(p->iov.iov, p->iov.niov, 0, buf, data_len); |
| 191 | fwrite(buf, data_len, 1, fp); |
| 192 | g_free(buf); |
| 193 | } |
| 194 | |
| 195 | fflush(fp); |
| 196 | } |
| 197 | |
| 198 | void usb_pcap_init(FILE *fp) |
| 199 | { |
| 200 | struct pcap_hdr header = { |
| 201 | .magic_number = PCAP_MAGIC, |
| 202 | .version_major = 2, |
| 203 | .version_minor = 4, |
| 204 | .snaplen = MAX(CTRL_LEN, DATA_LEN) + sizeof(struct usbmon_packet), |
| 205 | .network = LINKTYPE_USB_LINUX_MMAPPED, |
| 206 | }; |
| 207 | |
| 208 | fwrite(&header, sizeof(header), 1, fp); |
| 209 | } |
| 210 | |
| 211 | void usb_pcap_ctrl(USBPacket *p, bool setup) |
| 212 | { |
| 213 | FILE *fp = p->ep->dev->pcap; |
| 214 | |
| 215 | if (!fp) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | do_usb_pcap_ctrl(fp, p, setup); |
| 220 | } |
| 221 | |
| 222 | void usb_pcap_data(USBPacket *p, bool setup) |
| 223 | { |
| 224 | FILE *fp = p->ep->dev->pcap; |
| 225 | |
| 226 | if (!fp) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | do_usb_pcap_data(fp, p, setup); |
| 231 | } |