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