pcap.c 7.1 KB

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