usb.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright 2014 Google, Inc. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. package layers
  7. import (
  8. "encoding/binary"
  9. "github.com/google/gopacket"
  10. )
  11. type USBEventType uint8
  12. const (
  13. USBEventTypeSubmit USBEventType = 'S'
  14. USBEventTypeComplete USBEventType = 'C'
  15. USBEventTypeError USBEventType = 'E'
  16. )
  17. func (a USBEventType) String() string {
  18. switch a {
  19. case USBEventTypeSubmit:
  20. return "SUBMIT"
  21. case USBEventTypeComplete:
  22. return "COMPLETE"
  23. case USBEventTypeError:
  24. return "ERROR"
  25. default:
  26. return "Unknown event type"
  27. }
  28. }
  29. type USBRequestBlockSetupRequest uint8
  30. const (
  31. USBRequestBlockSetupRequestGetStatus USBRequestBlockSetupRequest = 0x00
  32. USBRequestBlockSetupRequestClearFeature USBRequestBlockSetupRequest = 0x01
  33. USBRequestBlockSetupRequestSetFeature USBRequestBlockSetupRequest = 0x03
  34. USBRequestBlockSetupRequestSetAddress USBRequestBlockSetupRequest = 0x05
  35. USBRequestBlockSetupRequestGetDescriptor USBRequestBlockSetupRequest = 0x06
  36. USBRequestBlockSetupRequestSetDescriptor USBRequestBlockSetupRequest = 0x07
  37. USBRequestBlockSetupRequestGetConfiguration USBRequestBlockSetupRequest = 0x08
  38. USBRequestBlockSetupRequestSetConfiguration USBRequestBlockSetupRequest = 0x09
  39. USBRequestBlockSetupRequestSetIdle USBRequestBlockSetupRequest = 0x0a
  40. )
  41. func (a USBRequestBlockSetupRequest) String() string {
  42. switch a {
  43. case USBRequestBlockSetupRequestGetStatus:
  44. return "GET_STATUS"
  45. case USBRequestBlockSetupRequestClearFeature:
  46. return "CLEAR_FEATURE"
  47. case USBRequestBlockSetupRequestSetFeature:
  48. return "SET_FEATURE"
  49. case USBRequestBlockSetupRequestSetAddress:
  50. return "SET_ADDRESS"
  51. case USBRequestBlockSetupRequestGetDescriptor:
  52. return "GET_DESCRIPTOR"
  53. case USBRequestBlockSetupRequestSetDescriptor:
  54. return "SET_DESCRIPTOR"
  55. case USBRequestBlockSetupRequestGetConfiguration:
  56. return "GET_CONFIGURATION"
  57. case USBRequestBlockSetupRequestSetConfiguration:
  58. return "SET_CONFIGURATION"
  59. case USBRequestBlockSetupRequestSetIdle:
  60. return "SET_IDLE"
  61. default:
  62. return "UNKNOWN"
  63. }
  64. }
  65. type USBTransportType uint8
  66. const (
  67. USBTransportTypeTransferIn USBTransportType = 0x80 // Indicates send or receive
  68. USBTransportTypeIsochronous USBTransportType = 0x00 // Isochronous transfers occur continuously and periodically. They typically contain time sensitive information, such as an audio or video stream.
  69. USBTransportTypeInterrupt USBTransportType = 0x01 // Interrupt transfers are typically non-periodic, small device "initiated" communication requiring bounded latency, such as pointing devices or keyboards.
  70. USBTransportTypeControl USBTransportType = 0x02 // Control transfers are typically used for command and status operations.
  71. USBTransportTypeBulk USBTransportType = 0x03 // Bulk transfers can be used for large bursty data, using all remaining available bandwidth, no guarantees on bandwidth or latency, such as file transfers.
  72. )
  73. type USBDirectionType uint8
  74. const (
  75. USBDirectionTypeUnknown USBDirectionType = iota
  76. USBDirectionTypeIn
  77. USBDirectionTypeOut
  78. )
  79. func (a USBDirectionType) String() string {
  80. switch a {
  81. case USBDirectionTypeIn:
  82. return "In"
  83. case USBDirectionTypeOut:
  84. return "Out"
  85. default:
  86. return "Unknown direction type"
  87. }
  88. }
  89. // The reference at http://www.beyondlogic.org/usbnutshell/usb1.shtml contains more information about the protocol.
  90. type USB struct {
  91. BaseLayer
  92. ID uint64
  93. EventType USBEventType
  94. TransferType USBTransportType
  95. Direction USBDirectionType
  96. EndpointNumber uint8
  97. DeviceAddress uint8
  98. BusID uint16
  99. TimestampSec int64
  100. TimestampUsec int32
  101. Setup bool
  102. Data bool
  103. Status int32
  104. UrbLength uint32
  105. UrbDataLength uint32
  106. UrbInterval uint32
  107. UrbStartFrame uint32
  108. UrbCopyOfTransferFlags uint32
  109. IsoNumDesc uint32
  110. }
  111. func (u *USB) LayerType() gopacket.LayerType { return LayerTypeUSB }
  112. func (m *USB) NextLayerType() gopacket.LayerType {
  113. if m.Setup {
  114. return LayerTypeUSBRequestBlockSetup
  115. } else if m.Data {
  116. }
  117. return m.TransferType.LayerType()
  118. }
  119. func decodeUSB(data []byte, p gopacket.PacketBuilder) error {
  120. d := &USB{}
  121. return decodingLayerDecoder(d, data, p)
  122. }
  123. func (m *USB) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  124. m.ID = binary.LittleEndian.Uint64(data[0:8])
  125. m.EventType = USBEventType(data[8])
  126. m.TransferType = USBTransportType(data[9])
  127. m.EndpointNumber = data[10] & 0x7f
  128. if data[10]&uint8(USBTransportTypeTransferIn) > 0 {
  129. m.Direction = USBDirectionTypeIn
  130. } else {
  131. m.Direction = USBDirectionTypeOut
  132. }
  133. m.DeviceAddress = data[11]
  134. m.BusID = binary.LittleEndian.Uint16(data[12:14])
  135. if uint(data[14]) == 0 {
  136. m.Setup = true
  137. }
  138. if uint(data[15]) == 0 {
  139. m.Data = true
  140. }
  141. m.TimestampSec = int64(binary.LittleEndian.Uint64(data[16:24]))
  142. m.TimestampUsec = int32(binary.LittleEndian.Uint32(data[24:28]))
  143. m.Status = int32(binary.LittleEndian.Uint32(data[28:32]))
  144. m.UrbLength = binary.LittleEndian.Uint32(data[32:36])
  145. m.UrbDataLength = binary.LittleEndian.Uint32(data[36:40])
  146. m.Contents = data[:40]
  147. m.Payload = data[40:]
  148. if m.Setup {
  149. m.Payload = data[40:]
  150. } else if m.Data {
  151. m.Payload = data[uint32(len(data))-m.UrbDataLength:]
  152. }
  153. // if 64 bit, dissect_linux_usb_pseudo_header_ext
  154. if false {
  155. m.UrbInterval = binary.LittleEndian.Uint32(data[40:44])
  156. m.UrbStartFrame = binary.LittleEndian.Uint32(data[44:48])
  157. m.UrbDataLength = binary.LittleEndian.Uint32(data[48:52])
  158. m.IsoNumDesc = binary.LittleEndian.Uint32(data[52:56])
  159. m.Contents = data[:56]
  160. m.Payload = data[56:]
  161. }
  162. // crc5 or crc16
  163. // eop (end of packet)
  164. return nil
  165. }
  166. type USBRequestBlockSetup struct {
  167. BaseLayer
  168. RequestType uint8
  169. Request USBRequestBlockSetupRequest
  170. Value uint16
  171. Index uint16
  172. Length uint16
  173. }
  174. func (u *USBRequestBlockSetup) LayerType() gopacket.LayerType { return LayerTypeUSBRequestBlockSetup }
  175. func (m *USBRequestBlockSetup) NextLayerType() gopacket.LayerType {
  176. return gopacket.LayerTypePayload
  177. }
  178. func (m *USBRequestBlockSetup) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  179. m.RequestType = data[0]
  180. m.Request = USBRequestBlockSetupRequest(data[1])
  181. m.Value = binary.LittleEndian.Uint16(data[2:4])
  182. m.Index = binary.LittleEndian.Uint16(data[4:6])
  183. m.Length = binary.LittleEndian.Uint16(data[6:8])
  184. m.Contents = data[:8]
  185. m.Payload = data[8:]
  186. return nil
  187. }
  188. func decodeUSBRequestBlockSetup(data []byte, p gopacket.PacketBuilder) error {
  189. d := &USBRequestBlockSetup{}
  190. return decodingLayerDecoder(d, data, p)
  191. }
  192. type USBControl struct {
  193. BaseLayer
  194. }
  195. func (u *USBControl) LayerType() gopacket.LayerType { return LayerTypeUSBControl }
  196. func (m *USBControl) NextLayerType() gopacket.LayerType {
  197. return gopacket.LayerTypePayload
  198. }
  199. func (m *USBControl) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  200. m.Contents = data
  201. return nil
  202. }
  203. func decodeUSBControl(data []byte, p gopacket.PacketBuilder) error {
  204. d := &USBControl{}
  205. return decodingLayerDecoder(d, data, p)
  206. }
  207. type USBInterrupt struct {
  208. BaseLayer
  209. }
  210. func (u *USBInterrupt) LayerType() gopacket.LayerType { return LayerTypeUSBInterrupt }
  211. func (m *USBInterrupt) NextLayerType() gopacket.LayerType {
  212. return gopacket.LayerTypePayload
  213. }
  214. func (m *USBInterrupt) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  215. m.Contents = data
  216. return nil
  217. }
  218. func decodeUSBInterrupt(data []byte, p gopacket.PacketBuilder) error {
  219. d := &USBInterrupt{}
  220. return decodingLayerDecoder(d, data, p)
  221. }
  222. type USBBulk struct {
  223. BaseLayer
  224. }
  225. func (u *USBBulk) LayerType() gopacket.LayerType { return LayerTypeUSBBulk }
  226. func (m *USBBulk) NextLayerType() gopacket.LayerType {
  227. return gopacket.LayerTypePayload
  228. }
  229. func (m *USBBulk) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  230. m.Contents = data
  231. return nil
  232. }
  233. func decodeUSBBulk(data []byte, p gopacket.PacketBuilder) error {
  234. d := &USBBulk{}
  235. return decodingLayerDecoder(d, data, p)
  236. }