This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
flow.go
355 lines (330 loc) · 8.8 KB
/
flow.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
/*
* Copyright 2011 Daniel Arndt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author: Daniel Arndt <[email protected]>
*
*/
package main
import (
"fmt"
"log"
)
const (
IP_TCP = 6
IP_UDP = 17
P_FORWARD = 0
P_BACKWARD = 1
ADD_SUCCESS = 0
ADD_CLOSED = 1
ADD_IDLE = 2
)
const (
// Configurables. These should at some point be read in from a configuration
// file.
FLOW_TIMEOUT = 600000000
IDLE_THRESHOLD = 1000000
)
// This is how we represent each packet after it is decoded. A simple map from the
// string value for it's name to the value
type packet map[string]int64
const (
// To add new features, add the name here, then initialize the
// value in init(), and calculate it in add(). You can finalize it
// in Export()
TOTAL_FPACKETS = iota
TOTAL_FVOLUME
TOTAL_BPACKETS
TOTAL_BVOLUME
FPKTL
BPKTL
FIAT
BIAT
DURATION
ACTIVE
IDLE
SFLOW_FPACKETS
SFLOW_FBYTES
SFLOW_BPACKETS
SFLOW_BBYTES
FPSH_CNT
BPSH_CNT
FURG_CNT
BURG_CNT
TOTAL_FHLEN
TOTAL_BHLEN
NUM_FEATURES // Not a real feature. Just the total number of features.
)
type Flow struct {
f []Feature // A map of the features to be exported
valid bool // Has the flow met the requirements of a bi-directional flow
activeStart int64 // The starting time of the latest activity
firstTime int64 // The time of the first packet in the flow
flast int64 // The time of the last packet in the forward direction
blast int64 // The time of the last packet in the backward direction
cstate tcpState // Connection state of the client
sstate tcpState // Connection state of the server
hasData bool // Whether the connection has had any data transmitted.
isBidir bool // Is the flow bi-directional?
pdir int8 // Direction of the current packet
srcip string // IP address of the source (client)
srcport uint16 // Port number of the source connection
dstip string // IP address of the destination (server)
dstport uint16 // Port number of the destionation connection.
proto uint8 // The IP protocol being used for the connection.
dscp uint8 // The first set DSCP field for the flow.
}
func (f *Flow) Init(srcip string,
srcport uint16,
dstip string,
dstport uint16,
proto uint8,
pkt packet,
id int64) {
f.f = make([]Feature, NUM_FEATURES)
f.valid = false
f.f[TOTAL_FPACKETS] = new(ValueFeature)
f.f[TOTAL_FVOLUME] = new(ValueFeature)
f.f[TOTAL_BPACKETS] = new(ValueFeature)
f.f[TOTAL_BVOLUME] = new(ValueFeature)
f.f[FPKTL] = new(DistributionFeature)
f.f[BPKTL] = new(DistributionFeature)
f.f[FIAT] = new(DistributionFeature)
f.f[BIAT] = new(DistributionFeature)
f.f[DURATION] = new(ValueFeature)
f.f[ACTIVE] = new(DistributionFeature)
f.f[IDLE] = new(DistributionFeature)
f.f[SFLOW_FPACKETS] = new(ValueFeature)
f.f[SFLOW_FBYTES] = new(ValueFeature)
f.f[SFLOW_BPACKETS] = new(ValueFeature)
f.f[SFLOW_BBYTES] = new(ValueFeature)
f.f[FPSH_CNT] = new(ValueFeature)
f.f[BPSH_CNT] = new(ValueFeature)
f.f[FURG_CNT] = new(ValueFeature)
f.f[BURG_CNT] = new(ValueFeature)
f.f[TOTAL_FHLEN] = new(ValueFeature)
f.f[TOTAL_BHLEN] = new(ValueFeature)
//for i := 0; i < NUM_FEATURES; i++ {
// f.f[i].Set(0)
//}
// Basic flow identification criteria
f.srcip = srcip
f.srcport = srcport
f.dstip = dstip
f.dstport = dstport
f.proto = proto
f.dscp = uint8(pkt["dscp"])
// ---------------------------------------------------------
f.f[TOTAL_FPACKETS].Set(1)
length := pkt["len"]
f.f[TOTAL_FVOLUME].Set(length)
f.f[FPKTL].Add(length)
f.firstTime = pkt["time"]
f.flast = f.firstTime
f.activeStart = f.firstTime
if f.proto == IP_TCP {
// TCP specific code:
f.cstate.State = TCP_STATE_START
f.sstate.State = TCP_STATE_START
if tcpSet(TCP_PSH, pkt["flags"]) {
f.f[FPSH_CNT].Set(1)
}
if tcpSet(TCP_URG, pkt["flags"]) {
f.f[FURG_CNT].Set(1)
}
}
f.f[TOTAL_FHLEN].Set(pkt["iphlen"] + pkt["prhlen"])
f.hasData = false
f.pdir = P_FORWARD
f.updateStatus(pkt)
return
}
func (f *Flow) updateTcpState(pkt packet) {
f.cstate.TcpUpdate(pkt["flags"], P_FORWARD, f.pdir)
f.sstate.TcpUpdate(pkt["flags"], P_BACKWARD, f.pdir)
}
func (f *Flow) updateStatus(pkt packet) {
if f.proto == IP_UDP {
if f.valid {
return
}
if pkt["len"] > 8 {
f.hasData = true
}
if f.hasData && f.isBidir {
f.valid = true
}
} else if f.proto == IP_TCP {
if !f.valid {
if f.cstate.State == TCP_STATE_ESTABLISHED {
if pkt["len"] > (pkt["iphlen"] + pkt["prhlen"]) {
f.valid = true
}
}
}
f.updateTcpState(pkt)
}
}
func (f *Flow) getLastTime() int64 {
if f.blast == 0 {
return f.flast
}
if f.flast == 0 {
return f.blast
}
if f.flast > f.blast {
return f.flast
}
return f.blast
}
func (f *Flow) Add(pkt packet, srcip string) int {
now := pkt["time"]
last := f.getLastTime()
diff := now - last
if diff > FLOW_TIMEOUT {
return ADD_IDLE
}
if now < last {
log.Printf("Flow: ignoring reordered packet. %d < %d\n", now, last)
return ADD_SUCCESS
}
length := pkt["len"]
hlen := pkt["iphlen"] + pkt["prhlen"]
if now < f.firstTime {
log.Fatalf("Current packet is before start of flow. %d < %d\n",
now,
f.firstTime)
}
if srcip == f.srcip {
f.pdir = P_FORWARD // Forward
} else {
f.pdir = P_BACKWARD
}
if diff > IDLE_THRESHOLD {
f.f[IDLE].Add(diff)
// Active time stats - calculated by looking at the previous packet
// time and the packet time for when the last idle time ended.
diff = last - f.activeStart
f.f[ACTIVE].Add(diff)
f.flast = 0
f.blast = 0
f.activeStart = now
}
if f.pdir == P_FORWARD {
if f.dscp == 0 {
f.dscp = uint8(pkt["dscp"])
}
// Packet is travelling in the forward direction
// Calculate some statistics
// Packet length
f.f[FPKTL].Add(length)
f.f[TOTAL_FVOLUME].Add(length)
f.f[TOTAL_FPACKETS].Add(1)
f.f[TOTAL_FHLEN].Add(hlen)
// Interarrival time
if f.flast > 0 {
diff = now - f.flast
f.f[FIAT].Add(diff)
}
if f.proto == IP_TCP {
// Packet is using TCP protocol
if tcpSet(TCP_PSH, pkt["flags"]) {
f.f[FPSH_CNT].Add(1)
}
if tcpSet(TCP_URG, pkt["flags"]) {
f.f[FURG_CNT].Add(1)
}
// Update the last forward packet time stamp
}
f.flast = now
} else {
// Packet is travelling in the backward direction
f.isBidir = true
if f.dscp == 0 {
f.dscp = uint8(pkt["dscp"])
}
// Calculate some statistics
// Packet length
f.f[BPKTL].Add(length)
f.f[TOTAL_BVOLUME].Add(length) // Doubles up as c_bpktl_sum from NM
f.f[TOTAL_BPACKETS].Add(1)
f.f[TOTAL_BHLEN].Add(hlen)
// Inter-arrival time
if f.blast > 0 {
diff = now - f.blast
f.f[BIAT].Add(diff)
}
if f.proto == IP_TCP {
// Packet is using TCP protocol
if tcpSet(TCP_PSH, pkt["flags"]) {
f.f[BPSH_CNT].Add(1)
}
if tcpSet(TCP_URG, pkt["flags"]) {
f.f[BURG_CNT].Add(1)
}
}
// Update the last backward packet time stamp
f.blast = now
}
// Update the status (validity, TCP connection state) of the flow.
f.updateStatus(pkt)
if f.proto == IP_TCP &&
f.cstate.State == TCP_STATE_CLOSED &&
f.sstate.State == TCP_STATE_CLOSED {
return ADD_CLOSED
}
return ADD_SUCCESS
}
func (f *Flow) Export() {
if !f.valid {
return
}
// -----------------------------------
// First, lets consider the last active time in the calculations in case
// this changes something.
// -----------------------------------
diff := f.getLastTime() - f.activeStart
f.f[ACTIVE].Add(diff)
// ---------------------------------
// Update Flow stats which require counters or other final calculations
// ---------------------------------
// More sub-flow calculations
if f.f[ACTIVE].Get() > 0 {
f.f[SFLOW_FPACKETS].Set(f.f[TOTAL_FPACKETS].Get() / f.f[ACTIVE].Get())
f.f[SFLOW_FBYTES].Set(f.f[TOTAL_FVOLUME].Get() / f.f[ACTIVE].Get())
f.f[SFLOW_BPACKETS].Set(f.f[TOTAL_BPACKETS].Get() / f.f[ACTIVE].Get())
f.f[SFLOW_BBYTES].Set(f.f[TOTAL_BVOLUME].Get() / f.f[ACTIVE].Get())
}
f.f[DURATION].Set(f.getLastTime() - f.firstTime)
if f.f[DURATION].Get() < 0 {
log.Fatalf("duration (%d) < 0", f.f[DURATION])
}
fmt.Printf("%s,%d,%s,%d,%d",
f.srcip,
f.srcport,
f.dstip,
f.dstport,
f.proto)
for i := 0; i < NUM_FEATURES; i++ {
fmt.Printf(",%s", f.f[i].Export())
}
fmt.Printf(",%d", f.dscp)
fmt.Println()
}
func (f *Flow) CheckIdle(time int64) bool {
if (time - f.getLastTime()) > FLOW_TIMEOUT {
return true
}
return false
}