-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.go
320 lines (292 loc) · 7.01 KB
/
capture.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
package main
import (
"encoding/binary"
"log"
"math"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
type ECN uint8
const (
NotECT ECN = 0x00
SCE ECN = 0x01
ECT0 ECN = 0x02
CE ECN = 0x03
)
func Capture(pch <-chan gopacket.Packet, d *Data) {
var eth layers.Ethernet
var ip4 layers.IPv4
var ip6 layers.IPv6
var tcp layers.TCP
var to *TCPOneWayData
var tor *TCPOneWayData
var tk4 TCP4FlowKey
var tk6 TCP6FlowKey
var lastErr error
var lastErrCount int
var flowIndex int
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet)
parser.DecodingLayerParserOptions.IgnoreUnsupported = true
parser.SetDecodingLayerContainer(gopacket.DecodingLayerSparse(nil))
parser.AddDecodingLayer(ð)
parser.AddDecodingLayer(&ip4)
parser.AddDecodingLayer(&ip6)
parser.AddDecodingLayer(&tcp)
dec := []gopacket.LayerType{}
d.Meta.ParseStartTime = time.Now()
for p := range pch {
// decode packet
if err := parser.DecodeLayers(p.Data(), &dec); err != nil {
if lastErr != nil && err.Error() == lastErr.Error() {
lastErrCount++
} else {
log.Printf("decode error: %s", err)
lastErr = err
lastErrCount = 1
}
continue
} else if lastErrCount > 0 {
if lastErrCount > 1 {
log.Printf("last error repeated %d times", lastErrCount-1)
}
lastErrCount = 0
lastErr = nil
}
// lock data while updating
d.Lock()
// identify parsed layers
isTCP := false
isIP4 := true
for _, lt := range dec {
switch lt {
case layers.LayerTypeTCP:
isTCP = true
case layers.LayerTypeIPv6:
isIP4 = false
}
}
// get timestamp and update capture times
tstamp := p.Metadata().Timestamp
if d.IP.Packets == 0 {
d.Meta.CaptureStartTime = tstamp
}
d.Meta.CaptureEndTime = tstamp
// update IP stats
d.IP.Packets++
var ipLen uint
if isIP4 {
ipLen = uint(ip4.Length)
} else {
ipLen = uint(ip6.Length) + 40
}
d.IP.Bytes += uint64(ipLen)
// go to next packet if not TCP or it's a TCP reset
if !isTCP || tcp.RST {
d.Unlock()
continue
}
// get addresses and ports for flow identification
var ok, rok bool
var f *TCPFlowData
up := true
if isIP4 {
copy(tk4.SrcIP[:], ip4.SrcIP)
tk4.SrcPort = tcp.SrcPort
copy(tk4.DstIP[:], ip4.DstIP)
tk4.DstPort = tcp.DstPort
if f, ok = d.TCP4[tk4]; !ok {
if f, rok = d.TCP4[tk4.Reverse()]; !rok {
f = &TCPFlowData{
Index: flowIndex,
SrcIP: ip4.SrcIP,
DstIP: ip4.DstIP,
SrcPort: tcp.SrcPort,
DstPort: tcp.DstPort,
Up: NewTCPOneWayData(),
Down: NewTCPOneWayData(),
}
d.TCP4[tk4] = f
flowIndex++
} else {
up = false
}
}
} else {
copy(tk6.SrcIP[:], ip6.SrcIP)
tk6.SrcPort = tcp.SrcPort
copy(tk6.DstIP[:], ip6.DstIP)
tk6.DstPort = tcp.DstPort
if f, ok = d.TCP6[tk6]; !ok {
if f, rok = d.TCP6[tk6.Reverse()]; !rok {
f = &TCPFlowData{
Index: flowIndex,
SrcIP: ip6.SrcIP,
DstIP: ip6.DstIP,
SrcPort: tcp.SrcPort,
DstPort: tcp.DstPort,
Up: NewTCPOneWayData(),
Down: NewTCPOneWayData(),
}
d.TCP6[tk6] = f
flowIndex++
} else {
up = false
}
}
}
// set one-way stats pointers based on direction
if up {
to = f.Up
tor = f.Down
} else {
to = f.Down
tor = f.Up
}
// read timestamps
var tsval, tsecr uint32
for _, opt := range tcp.Options {
if opt.OptionType == layers.TCPOptionKindTimestamps &&
opt.OptionLength == 10 {
tsval = binary.BigEndian.Uint32(opt.OptionData[:4])
tsecr = binary.BigEndian.Uint32(opt.OptionData[4:])
to.TSValTimes[tsval] = tstamp
if pt, ok := tor.TSValTimes[tsecr]; ok {
tor.TSValRTT.Push(tstamp.Sub(pt))
delete(tor.TSValTimes, tsecr)
}
break
}
}
// handle connection initiation
if !to.Initialized {
to.ExpSeq = tcp.Seq
if tcp.SYN {
if tcp.ACK {
f.ECNAccepted = tcp.ECE
} else {
f.ECNInitiated = tcp.ECE && tcp.CWR
}
to.ExpSeq++
}
to.HiTSVal = tsval
to.Initialized = true
}
// get dscp and segment length according to IP version
var dscp uint8
var segLen uint32
if isIP4 {
segLen = uint32(ipLen) - 4*uint32(ip4.IHL) - 4*uint32(tcp.DataOffset)
dscp = ip4.TOS
} else {
segLen = uint32(ipLen) - 40
dscp = ip6.TrafficClass
}
if segLen > 0 {
to.SeqTimes[tcp.Seq] = tstamp
to.DataSegments++
}
// handle acks
if tcp.ACK {
var ackedBytes uint32
if to.Acks > 0 {
ackedBytes = tcp.Ack - to.PriorAck
if ackedBytes == 0 { // duplicate ack
for _, opt := range tcp.Options {
if opt.OptionType == layers.TCPOptionKindSACK {
n := int(opt.OptionLength) - 2
for l, r := 0, 4; l < n; l, r = l+8, r+8 {
le := binary.BigEndian.Uint32(opt.OptionData[l : l+4])
re := binary.BigEndian.Uint32(opt.OptionData[r : r+4])
ackedBytes += (re - le)
//log.Printf("SACK %d %d", ackedBytes, to.SackedBytesCtr)
}
to.SackedBytesCtr += ackedBytes
to.SackedBytes += uint64(ackedBytes)
break
}
}
to.DuplicateAcks++
} else { // standard ack
if to.SackedBytesCtr > 0 {
ackedBytes -= to.SackedBytesCtr
to.SackedBytesCtr = 0
}
to.AckedBytes += uint64(ackedBytes)
to.LastAckTime = tstamp
if pt, ok := tor.SeqTimes[to.PriorAck]; ok {
tor.SeqRTT.Push(tstamp.Sub(pt))
delete(tor.SeqTimes, to.PriorAck)
}
// Note: if SACK is not supported, implementations count one
// segment of ESCE acked bytes, for what that's worth. Also
// in rare cases might encounter window probes.
to.PriorAck = tcp.Ack
}
} else {
to.FirstAckTime = tstamp
to.LastAckTime = tstamp
to.PriorAck = tcp.Ack
}
if !tcp.SYN && !tcp.FIN && !to.FinSeen {
// detect retransmitted and late (out-of-order) segments
seqDelta := tcp.Seq - to.ExpSeq
if seqDelta > math.MaxUint32/2 {
to.RetransmittedSegments++
} else {
if seqDelta > 0 {
to.Gaps++
to.GapBytes += uint64(seqDelta)
}
to.ExpSeq = tcp.Seq + segLen
}
if tsval-to.HiTSVal > math.MaxUint32/2 {
to.LateSegments++
} else {
to.HiTSVal = tsval
}
// record congestion related stats
if tcp.CWR {
to.CWR++
}
if tcp.ECE {
to.ECE++
}
if tcp.NS {
to.ESCE++
to.ESCEAckedBytes += uint64(ackedBytes)
}
ecn := ECN(dscp & 0x03)
if ecn == CE {
to.CE++
}
if ecn == SCE {
to.SCE++
if !to.PriorSCETime.IsZero() {
to.SCEIPG.Push(tstamp.Sub(to.PriorSCETime))
}
to.PriorSCETime = tstamp
to.SCERunCount++
} else if to.SCERunCount > 0 {
to.SCERunLength.Push(float64(to.SCERunCount))
to.SCERunCount = 0
}
}
to.Acks++
}
// record inter-packet gap stats
if !to.PriorPacketTime.IsZero() {
to.IPG.Push(tstamp.Sub(to.PriorPacketTime))
}
to.PriorPacketTime = tstamp
// increment segment count
to.Segments++
// set if FIN seen
if tcp.FIN {
to.FinSeen = true
}
// unlock data
d.Unlock()
}
return
}