-
Notifications
You must be signed in to change notification settings - Fork 1
/
dissect.go
436 lines (385 loc) · 11.1 KB
/
dissect.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package netem
//
// Protocol dissector
//
import (
"errors"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
// DissectedPacket is a dissected IP packet. The zero-value is invalid; you
// MUST use the [dissectPacket] factory to create a new instance.
type DissectedPacket struct {
// Packet is the underlying packet.
Packet gopacket.Packet
// IP is the network layer (either IPv4 or IPv6).
IP gopacket.NetworkLayer
// TCP is the POSSIBLY NIL tcp layer.
TCP *layers.TCP
// UDP is the POSSIBLY NIL UDP layer.
UDP *layers.UDP
}
// ErrDissectShortPacket indicates the packet is too short.
var ErrDissectShortPacket = errors.New("netem: dissect: packet too short")
// ErrDissectNetwork indicates that we do not support the packet's network protocol.
var ErrDissectNetwork = errors.New("netem: dissect: unsupported network protocol")
// ErrDissectTransport indicates that we do not support the packet's transport protocol.
var ErrDissectTransport = errors.New("netem: dissect: unsupported transport protocol")
// DissectPacket parses a packet TCP/IP layers.
func DissectPacket(rawPacket []byte) (*DissectedPacket, error) {
dp := &DissectedPacket{}
// [UNetStack] emits raw IPv4 or IPv6 packets and we need to
// sniff the actual version from the first octet
if len(rawPacket) < 1 {
return nil, ErrDissectShortPacket
}
version := uint8(rawPacket[0]) >> 4
// parse the IP layer
switch {
case version == 4:
dp.Packet = gopacket.NewPacket(rawPacket, layers.LayerTypeIPv4, gopacket.Lazy)
ipLayer := dp.Packet.Layer(layers.LayerTypeIPv4)
if ipLayer == nil {
return nil, ErrDissectNetwork
}
dp.IP = ipLayer.(*layers.IPv4)
case version == 6:
dp.Packet = gopacket.NewPacket(rawPacket, layers.LayerTypeIPv6, gopacket.Lazy)
ipLayer := dp.Packet.Layer(layers.LayerTypeIPv6)
if ipLayer == nil {
return nil, ErrDissectNetwork
}
dp.IP = ipLayer.(*layers.IPv6)
default:
return nil, ErrDissectNetwork
}
// parse the transport layer
switch dp.TransportProtocol() {
case layers.IPProtocolTCP:
dp.TCP = dp.Packet.Layer(layers.LayerTypeTCP).(*layers.TCP)
case layers.IPProtocolUDP:
dp.UDP = dp.Packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
default:
return nil, ErrDissectTransport
}
return dp, nil
}
// DecrementTimeToLive decrements the IPv4 or IPv6 time to live.
func (dp *DissectedPacket) DecrementTimeToLive() {
switch v := dp.IP.(type) {
case *layers.IPv4:
if v.TTL > 0 {
v.TTL--
}
case *layers.IPv6:
if v.HopLimit > 0 {
v.HopLimit--
}
default:
panic(ErrDissectNetwork)
}
}
// TimeToLive returns the packet's IPv4 or IPv6 time to live.
func (dp *DissectedPacket) TimeToLive() int64 {
switch v := dp.IP.(type) {
case *layers.IPv4:
return int64(v.TTL)
case *layers.IPv6:
return int64(v.HopLimit)
default:
panic(ErrDissectNetwork)
}
}
// DestinationIPAddress returns the packet's destination IP address.
func (dp *DissectedPacket) DestinationIPAddress() string {
switch v := dp.IP.(type) {
case *layers.IPv4:
return v.DstIP.String()
case *layers.IPv6:
return v.DstIP.String()
default:
panic(ErrDissectNetwork)
}
}
// DestinationPort returns the packet's destination port.
func (dp *DissectedPacket) DestinationPort() uint16 {
switch {
case dp.TCP != nil:
return uint16(dp.TCP.DstPort)
case dp.UDP != nil:
return uint16(dp.UDP.DstPort)
default:
panic(ErrDissectTransport)
}
}
// SourceIPAddress returns the packet's source IP address.
func (dp *DissectedPacket) SourceIPAddress() string {
switch v := dp.IP.(type) {
case *layers.IPv4:
return v.SrcIP.String()
case *layers.IPv6:
return v.SrcIP.String()
default:
panic(ErrDissectNetwork)
}
}
// SourcePort returns the packet's source port.
func (dp *DissectedPacket) SourcePort() uint16 {
switch {
case dp.TCP != nil:
return uint16(dp.TCP.SrcPort)
case dp.UDP != nil:
return uint16(dp.UDP.SrcPort)
default:
panic(ErrDissectTransport)
}
}
// TransportProtocol returns the packet's transport protocol.
func (dp *DissectedPacket) TransportProtocol() layers.IPProtocol {
switch v := dp.IP.(type) {
case *layers.IPv4:
return v.Protocol
case *layers.IPv6:
return v.NextHeader
default:
panic(ErrDissectNetwork)
}
}
// Serialize serializes a previously dissected and modified packet.
func (dp *DissectedPacket) Serialize() ([]byte, error) {
switch {
case dp.TCP != nil:
dp.TCP.SetNetworkLayerForChecksum(dp.IP)
case dp.UDP != nil:
dp.UDP.SetNetworkLayerForChecksum(dp.IP)
default:
return nil, ErrDissectTransport
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
if err := gopacket.SerializePacket(buf, opts, dp.Packet); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// MatchesDestination returns true when the given IPv4 packet has the
// expected protocol, destination address, and port.
func (dp *DissectedPacket) MatchesDestination(proto layers.IPProtocol, address string, port uint16) bool {
if dp.TransportProtocol() != proto {
return false
}
switch {
case dp.TCP != nil:
return dp.DestinationIPAddress() == address && dp.TCP.DstPort == layers.TCPPort(port)
case dp.UDP != nil:
return dp.DestinationIPAddress() == address && dp.UDP.DstPort == layers.UDPPort(port)
default:
return false
}
}
// MatchesSource returns true when the given IPv4 packet has the
// expected protocol, source address, and port.
func (dp *DissectedPacket) MatchesSource(proto layers.IPProtocol, address string, port uint16) bool {
if dp.TransportProtocol() != proto {
return false
}
switch {
case dp.TCP != nil:
return dp.SourceIPAddress() == address && dp.TCP.SrcPort == layers.TCPPort(port)
case dp.UDP != nil:
return dp.SourceIPAddress() == address && dp.UDP.SrcPort == layers.UDPPort(port)
default:
return false
}
}
// FlowHash returns the hash uniquely identifying the transport flow. Both
// directions of a flow will have the same hash.
func (dp *DissectedPacket) FlowHash() uint64 {
switch {
case dp.TCP != nil:
return dp.TCP.TransportFlow().FastHash()
case dp.UDP != nil:
return dp.UDP.TransportFlow().FastHash()
default:
panic(ErrDissectTransport)
}
}
// parseTLSServerName attempts to parse this packet as
// a TLS client hello and to return the SNI.
func (dp *DissectedPacket) parseTLSServerName() (string, error) {
switch {
case dp.TCP != nil:
return ExtractTLSServerName(dp.TCP.Payload)
case dp.UDP != nil:
return ExtractTLSServerName(dp.UDP.Payload)
default:
return "", ErrDissectTransport
}
}
// reflectDissectedTCPSegmentWithRSTFlag assumes that packet is an IPv4 packet
// containing a TCP segment, and constructs a new serialized packet where
// we reflect incoming fields and set the RST flag.
func reflectDissectedTCPSegmentWithRSTFlag(packet *DissectedPacket) ([]byte, error) {
return reflectDissectedTCPSegmentWithSetter(packet, func(tcp *layers.TCP) {
tcp.RST = true
})
}
// reflectDissectedTCPSegmentWithFINACKFlag assumes that packet is an IPv4 packet
// containing a TCP segment, and constructs a new serialized packet where
// we reflect incoming fields and set the FIN|ACK flag.
func reflectDissectedTCPSegmentWithFINACKFlag(packet *DissectedPacket) ([]byte, error) {
return reflectDissectedTCPSegmentWithSetter(packet, func(tcp *layers.TCP) {
tcp.FIN = true
tcp.ACK = true
})
}
type reflectedSegment struct {
ipv4 *layers.IPv4
tcp *layers.TCP
}
func (dp *DissectedPacket) reflectSegment() (*reflectedSegment, error) {
var (
ipv4 *layers.IPv4
tcp *layers.TCP
)
// reflect the network layer first
switch v := dp.IP.(type) {
case *layers.IPv4:
ipv4 = &layers.IPv4{
BaseLayer: layers.BaseLayer{},
Version: 4,
IHL: 0,
TOS: 0,
Length: 0,
Id: v.Id,
Flags: 0,
FragOffset: 0,
TTL: 60,
Protocol: v.Protocol,
Checksum: 0,
SrcIP: v.DstIP,
DstIP: v.SrcIP,
Options: []layers.IPv4Option{},
Padding: []byte{},
}
default:
return nil, ErrDissectNetwork
}
// additionally reflect the transport layer
switch {
case dp.TCP != nil:
tcp = &layers.TCP{
BaseLayer: layers.BaseLayer{},
SrcPort: dp.TCP.DstPort,
DstPort: dp.TCP.SrcPort,
Seq: dp.TCP.Ack,
Ack: dp.TCP.Seq + 1, // ACK past last observed byte
DataOffset: 0,
FIN: false,
SYN: false,
RST: false,
PSH: false,
ACK: false,
URG: false,
ECE: false,
CWR: false,
NS: false,
Window: dp.TCP.Window,
Checksum: 0,
Urgent: 0,
Options: []layers.TCPOption{},
Padding: []byte{},
}
default:
return nil, ErrDissectTransport
}
rs := &reflectedSegment{ipv4: ipv4, tcp: tcp}
return rs, nil
}
// reflectDissectedTCPSegmentWithSetter assumes that packet is an IPv4 packet
// containing a TCP segment, and constructs a new serialized packet where
// we reflect incoming fields. This function calls the given setter function to
// additionally edit the packet before it is serialized to bytes.
func reflectDissectedTCPSegmentWithSetter(packet *DissectedPacket, setter func(tcp *layers.TCP)) ([]byte, error) {
rs, err := packet.reflectSegment()
if err != nil {
return nil, err
}
// invoke the setter to modify the TCP segment
setter(rs.tcp)
return rs.serialize()
}
func (rs *reflectedSegment) serialize(extraLayers ...gopacket.SerializableLayer) ([]byte, error) {
rs.tcp.SetNetworkLayerForChecksum(rs.ipv4)
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
all := append([]gopacket.SerializableLayer{rs.ipv4, rs.tcp}, extraLayers...)
if err := gopacket.SerializeLayers(buf, opts, all...); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// reflectDissectedUDPDatagramWithPayload assumes that packet is an IPv4 packet
// containing a UDP datagram, and constructs a new serialized packet where
// we reflect the incoming fields and set the given payload.
func reflectDissectedUDPDatagramWithPayload(packet *DissectedPacket, rawPayload []byte) ([]byte, error) {
var (
ipv4 *layers.IPv4
udp *layers.UDP
)
// reflect the network layer first
switch v := packet.IP.(type) {
case *layers.IPv4:
ipv4 = &layers.IPv4{
BaseLayer: layers.BaseLayer{},
Version: 4,
IHL: 0,
TOS: 0,
Length: 0,
Id: v.Id,
Flags: 0,
FragOffset: 0,
TTL: 60,
Protocol: v.Protocol,
Checksum: 0,
SrcIP: v.DstIP,
DstIP: v.SrcIP,
Options: []layers.IPv4Option{},
Padding: []byte{},
}
default:
return nil, ErrDissectNetwork
}
// additionally reflect the transport layer
switch {
case packet.UDP != nil:
udp = &layers.UDP{
BaseLayer: layers.BaseLayer{},
SrcPort: packet.UDP.DstPort,
DstPort: packet.UDP.SrcPort,
Length: 0,
Checksum: 0,
}
default:
return nil, ErrDissectTransport
}
// construct the payload
payload := gopacket.Payload(rawPayload)
// serialize the layers
udp.SetNetworkLayerForChecksum(ipv4)
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
if err := gopacket.SerializeLayers(buf, opts, ipv4, udp, payload); err != nil {
return nil, err
}
return buf.Bytes(), nil
}