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