/
nta.go
285 lines (241 loc) · 6.89 KB
/
nta.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
package nta
import (
"encoding/binary"
"fmt"
"github.com/CN-TU/go-flows/flows"
"github.com/CN-TU/go-flows/packet"
ipfix "github.com/CN-TU/go-ipfix"
"github.com/google/gopacket/layers"
)
type flowID struct {
flows.BaseFeature
}
func (f *flowID) Event(new interface{}, context *flows.EventContext, src interface{}) {
if f.Value() == nil {
nl := new.(packet.Buffer).NetworkLayer()
if nl == nil {
return
}
flow := nl.NetworkFlow()
f.SetValue(fmt.Sprintf("%s > %s", flow.Src(), flow.Dst()), context, f)
}
}
func init() {
flows.RegisterTemporaryFeature("__NTAFlowID", "src address > dst address", ipfix.StringType, 0, flows.FlowFeature, func() flows.Feature { return &flowID{} }, flows.RawPacket)
}
////////////////////////////////////////////////////////////////////////////////
type protocol struct {
flows.BaseFeature
}
func (f *protocol) Event(new interface{}, context *flows.EventContext, src interface{}) {
v := f.Value()
if v == -1 {
return
}
p := new.(packet.Buffer).Proto()
if v == nil {
f.SetValue(p, context, f)
return
}
if v != p {
f.SetValue(-1, context, f)
}
}
func init() {
flows.RegisterTemporaryFeature("__NTAProtocol", "protocol or -1 if multiple protocols", ipfix.Signed16Type, 0, flows.FlowFeature, func() flows.Feature { return &protocol{} }, flows.RawPacket)
}
////////////////////////////////////////////////////////////////////////////////
type ports struct {
flows.BaseFeature
src uint16
dst uint16
proto uint8
initPort bool
initProto bool
}
func (f *ports) Start(context *flows.EventContext) {
f.BaseFeature.Start(context)
f.src = 0
f.dst = 0
f.proto = 0
f.initPort = false
f.initProto = false
}
func (f *ports) Event(new interface{}, context *flows.EventContext, src interface{}) {
if f.Value() != nil {
return
}
buffer := new.(packet.Buffer)
if !f.initProto {
f.proto = buffer.Proto()
f.initProto = true
} else if f.proto != buffer.Proto() {
f.SetValue("-1", context, f)
return
}
transport := buffer.TransportLayer()
if transport == nil {
return
}
flow := transport.TransportFlow()
srcRaw := flow.Src().Raw()
dstRaw := flow.Dst().Raw()
// assume that both are non nil if one is
if srcRaw == nil {
return
}
s := binary.BigEndian.Uint16(srcRaw)
dst := binary.BigEndian.Uint16(dstRaw)
if !f.initPort {
f.src = s
f.dst = dst
return
}
if f.src != s {
f.SetValue("-1", context, f)
return
}
if f.dst != dst {
f.SetValue("-1", context, f)
return
}
}
func (f *ports) Stop(reason flows.FlowEndReason, context *flows.EventContext) {
if f.Value() == nil {
f.SetValue(fmt.Sprintf("%d:%d", f.src, f.dst), context, f)
}
}
func init() {
flows.RegisterTemporaryFeature("__NTAPorts", "srcport:dstport or -1 if multiple ports", ipfix.StringType, 0, flows.FlowFeature, func() flows.Feature { return &ports{} }, flows.RawPacket)
}
////////////////////////////////////////////////////////////////////////////////
type tdata struct {
flows.BaseFeature
bytes uint64
}
func (f *tdata) Start(context *flows.EventContext) {
f.BaseFeature.Start(context)
f.bytes = 0
}
func (f *tdata) Event(new interface{}, context *flows.EventContext, src interface{}) {
buffer := new.(packet.Buffer)
f.bytes += uint64(buffer.PayloadLength())
tl := buffer.TransportLayer()
if tl != nil {
tlt := tl.LayerType()
if tlt != layers.LayerTypeTCP && tlt != layers.LayerTypeUDP {
// add size of transport layer header if non-TCP and non-UDP
f.bytes += uint64(len(tl.LayerContents()))
}
}
}
func (f *tdata) Stop(reason flows.FlowEndReason, context *flows.EventContext) {
if f.Value() == nil {
f.SetValue(f.bytes, context, f)
}
}
func init() {
flows.RegisterTemporaryFeature("__NTATData", "sum of packet lengths (=payload length + transport header if nontcp/nonudp)", ipfix.Unsigned64Type, 0, flows.FlowFeature, func() flows.Feature { return &tdata{} }, flows.RawPacket)
}
////////////////////////////////////////////////////////////////////////////////
type secwindow struct {
flows.EmptyBaseFeature
tsec uint32
}
func (f *secwindow) Start(context *flows.EventContext) {
f.tsec = 0
}
func (f *secwindow) Event(new interface{}, context *flows.EventContext, src interface{}) {
tsec := uint32(new.(packet.Buffer).Timestamp() / flows.SecondsInNanoseconds)
if f.tsec != tsec {
f.tsec = tsec
f.Emit(tsec, context, src)
}
}
func init() {
flows.RegisterTemporaryFeature("__NTASecWindow", "1s time window for packets", ipfix.Unsigned64Type, 0, flows.Selection, func() flows.Feature { return &secwindow{} }, flows.RawPacket)
}
////////////////////////////////////////////////////////////////////////////////
type ton struct {
flows.BaseFeature
last uint32
ton uint32
}
func (f *ton) Start(context *flows.EventContext) {
f.BaseFeature.Start(context)
f.last = 0
f.ton = 0
}
func (f *ton) Event(new interface{}, context *flows.EventContext, src interface{}) {
t := new.(uint32)
if f.ton == 0 {
f.last = t
f.ton = 1
return
}
if t == f.last+1 {
f.last = t
f.ton++
return
}
f.SetValue(f.ton, context, f)
f.last = t
f.ton = 1
}
func (f *ton) Stop(reason flows.FlowEndReason, context *flows.EventContext) {
if f.ton != 0 {
f.SetValue(f.ton, context, f)
}
}
func init() {
flows.RegisterTemporaryFeature("__NTATOn", "returns on time (consec. seconds with packet)", ipfix.Unsigned64Type, 0, flows.PacketFeature, func() flows.Feature { return &ton{} }, flows.Selection)
}
////////////////////////////////////////////////////////////////////////////////
type toff struct {
flows.BaseFeature
last uint32
}
func (f *toff) Start(context *flows.EventContext) {
f.BaseFeature.Start(context)
f.last = 0
}
func (f *toff) Event(new interface{}, context *flows.EventContext, src interface{}) {
t := new.(uint32)
if f.last == 0 {
f.last = t
return
}
if t == f.last+1 {
f.last = t
return
}
f.SetValue(t-f.last-1, context, f)
f.last = t
}
func (f *toff) Stop(reason flows.FlowEndReason, context *flows.EventContext) {
if f.Value() == nil {
f.SetValue(0, context, f)
}
}
func init() {
flows.RegisterTemporaryFeature("__NTATOff", "returns off time (consec. seconds without packet)", ipfix.Unsigned64Type, 0, flows.PacketFeature, func() flows.Feature { return &toff{} }, flows.Selection)
}
////////////////////////////////////////////////////////////////////////////////
type milliSecwindow struct {
flows.EmptyBaseFeature
tsec uint32
}
func (f *milliSecwindow) Start(context *flows.EventContext) {
f.tsec = 0
}
func (f *milliSecwindow) Event(new interface{}, context *flows.EventContext, src interface{}) {
tsec := uint32(new.(packet.Buffer).Timestamp() / flows.MillisecondsInNanoseconds)
if f.tsec != tsec {
f.tsec = tsec
f.Emit(tsec, context, src)
}
}
func init() {
flows.RegisterTemporaryFeature("__NTAMilliSecWindow", "1ms time window for packets", ipfix.Unsigned64Type, 0, flows.Selection, func() flows.Feature { return &milliSecwindow{} }, flows.RawPacket)
}
////////////////////////////////////////////////////////////////////////////////