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