usb.go 8.0 KB

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