-
Notifications
You must be signed in to change notification settings - Fork 12
/
common.go
277 lines (250 loc) · 7.11 KB
/
common.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
package engine
import (
"github.com/fs714/goiftop/accounting"
"github.com/fs714/goiftop/decoder"
"github.com/fs714/goiftop/utils/log"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"strings"
"time"
)
const LibPcapEngineName = "libpcap"
const AfpacketEngineName = "afpacket"
const NflogEngineName = "nflog"
const DefaultFlowColResetInterval = 1
type PktCapEngine interface {
StartEngine() error
GetDirection() pcap.Direction
GetFlowCollection() *accounting.FlowCollection
GetResetInterval() int64
GetIsDecodeL4() bool
GetNotifyChannel() chan *accounting.FlowCollection
}
func Nofify(engine PktCapEngine) {
direction := engine.GetDirection()
flowCol := engine.GetFlowCollection()
resetInterval := engine.GetResetInterval()
notifyChannel := engine.GetNotifyChannel()
ticker := time.NewTicker(time.Duration(resetInterval) * time.Second)
for {
select {
case <-ticker.C:
flowCol.Mu.Lock()
flowColCopy := flowCol.Copy()
flowCol.Reset()
flowCol.Mu.Unlock()
now := time.Now().Unix()
flowColCopy.SetTimestamp(now-resetInterval, now)
if direction == pcap.DirectionOut {
for _, f := range flowColCopy.L3FlowMap {
f.OutboundDuration = resetInterval
}
for _, f := range flowColCopy.L4FlowMap {
f.OutboundDuration = resetInterval
}
} else {
for _, f := range flowColCopy.L3FlowMap {
f.InboundDuration = resetInterval
}
for _, f := range flowColCopy.L4FlowMap {
f.InboundDuration = resetInterval
}
}
notifyChannel <- flowColCopy
}
}
}
type CaptureLayers struct {
eth *layers.Ethernet
linuxSll *layers.LinuxSLL
dot1q *layers.Dot1Q
ipv4 *layers.IPv4
tcp *layers.TCP
udp *layers.UDP
dns *layers.DNS
icmpv4 *layers.ICMPv4
gre *layers.GRE
llc *layers.LLC
arp *layers.ARP
payload *gopacket.Payload
}
type Capture struct {
CaptureLayers
IsDecodeL4 bool
Direction pcap.Direction
FlowCol *accounting.FlowCollection
DecodingLayerList []gopacket.DecodingLayer
Dec *decoder.LayerDecoder
Decoded []gopacket.LayerType
FirstLayer gopacket.LayerType
L3Fingerprint *accounting.FlowFingerprint
L4Fingerprint *accounting.FlowFingerprint
L3Bytes *int64
L4Bytes *int64
}
func NewCapture(engine PktCapEngine) (capture *Capture) {
capture = &Capture{}
capture.eth = &layers.Ethernet{}
capture.linuxSll = &layers.LinuxSLL{}
capture.dot1q = &layers.Dot1Q{}
capture.ipv4 = &layers.IPv4{}
capture.tcp = &layers.TCP{}
capture.udp = &layers.UDP{}
capture.dns = &layers.DNS{}
capture.icmpv4 = &layers.ICMPv4{}
capture.gre = &layers.GRE{}
capture.llc = &layers.LLC{}
capture.arp = &layers.ARP{}
capture.payload = &gopacket.Payload{}
capture.IsDecodeL4 = engine.GetIsDecodeL4()
capture.Direction = engine.GetDirection()
capture.FlowCol = engine.GetFlowCollection()
if capture.IsDecodeL4 {
capture.DecodingLayerList = []gopacket.DecodingLayer{
capture.eth,
capture.linuxSll,
capture.dot1q,
capture.ipv4,
capture.tcp,
capture.udp,
capture.dns,
capture.icmpv4,
capture.gre,
capture.llc,
capture.arp,
capture.payload,
}
} else {
capture.DecodingLayerList = []gopacket.DecodingLayer{
capture.eth,
capture.linuxSll,
capture.dot1q,
capture.ipv4,
capture.payload,
}
}
capture.Dec = decoder.NewLayerDecoder(capture.DecodingLayerList...)
capture.Decoded = make([]gopacket.LayerType, 0, 8)
capture.L3Fingerprint = &accounting.FlowFingerprint{}
capture.L4Fingerprint = &accounting.FlowFingerprint{}
capture.L3Bytes = new(int64)
capture.L4Bytes = new(int64)
return
}
func (c *Capture) SetFirstLayer(l gopacket.LayerType) {
c.FirstLayer = l
}
func (c *Capture) DecodeAndAccount(data []byte) {
err := c.Dec.DecodeLayers(data, c.FirstLayer, &c.Decoded)
if err != nil {
if c.IsDecodeL4 {
ignoreErr := false
for _, s := range []string{"IPv6", "DHCPv4", "IGMP", "TLS", "STP", "NTP", "VRRP", "SNAP", "LinkLayerDiscovery", "Fragment"} {
if strings.Contains(err.Error(), s) {
ignoreErr = true
break
}
}
if !ignoreErr {
log.Errorf("error decoding packet with err: %s", err.Error())
}
}
}
if !c.IsDecodeL4 {
for _, ly := range c.Decoded {
switch ly {
case layers.LayerTypeIPv4:
if c.Direction == pcap.DirectionOut {
c.L3Fingerprint.SrcAddr = c.ipv4.DstIP.String()
c.L3Fingerprint.DstAddr = c.ipv4.SrcIP.String()
} else {
c.L3Fingerprint.SrcAddr = c.ipv4.SrcIP.String()
c.L3Fingerprint.DstAddr = c.ipv4.DstIP.String()
}
*c.L3Bytes = int64(c.ipv4.Length)
break
}
}
c.FlowCol.Mu.Lock()
if c.L3Fingerprint.SrcAddr != "" {
if c.Direction == pcap.DirectionOut {
c.FlowCol.UpdateL3Outbound(*c.L3Fingerprint, *c.L3Bytes, 1)
} else {
c.FlowCol.UpdateL3Inbound(*c.L3Fingerprint, *c.L3Bytes, 1)
}
}
c.FlowCol.Mu.Unlock()
c.L3Fingerprint.SrcAddr = ""
c.L3Fingerprint.DstAddr = ""
*c.L3Bytes = 0
} else {
for _, ly := range c.Decoded {
switch ly {
case layers.LayerTypeIPv4:
if c.Direction == pcap.DirectionOut {
c.L3Fingerprint.SrcAddr = c.ipv4.DstIP.String()
c.L3Fingerprint.DstAddr = c.ipv4.SrcIP.String()
} else {
c.L3Fingerprint.SrcAddr = c.ipv4.SrcIP.String()
c.L3Fingerprint.DstAddr = c.ipv4.DstIP.String()
}
*c.L3Bytes = int64(c.ipv4.Length)
c.L4Fingerprint.SrcAddr = c.L3Fingerprint.SrcAddr
c.L4Fingerprint.DstAddr = c.L3Fingerprint.DstAddr
break
case layers.LayerTypeTCP:
if c.Direction == pcap.DirectionOut {
c.L4Fingerprint.SrcPort = uint16(c.tcp.DstPort)
c.L4Fingerprint.DstPort = uint16(c.tcp.SrcPort)
} else {
c.L4Fingerprint.SrcPort = uint16(c.tcp.SrcPort)
c.L4Fingerprint.DstPort = uint16(c.tcp.DstPort)
}
c.L4Fingerprint.Protocol = "tcp"
*c.L4Bytes = int64(len(c.tcp.Contents) + len(c.tcp.LayerPayload()))
break
case layers.LayerTypeUDP:
if c.Direction == pcap.DirectionOut {
c.L4Fingerprint.SrcPort = uint16(c.udp.DstPort)
c.L4Fingerprint.DstPort = uint16(c.udp.SrcPort)
} else {
c.L4Fingerprint.SrcPort = uint16(c.udp.SrcPort)
c.L4Fingerprint.DstPort = uint16(c.udp.DstPort)
}
c.L4Fingerprint.Protocol = "udp"
*c.L4Bytes = int64(c.udp.Length)
break
case layers.LayerTypeICMPv4:
c.L4Fingerprint.Protocol = "icmp"
*c.L4Bytes = int64(len(c.icmpv4.Contents) + len(c.icmpv4.LayerPayload()))
break
}
}
c.FlowCol.Mu.Lock()
if c.L3Fingerprint.SrcAddr != "" {
if c.Direction == pcap.DirectionOut {
c.FlowCol.UpdateL3Outbound(*c.L3Fingerprint, *c.L3Bytes, 1)
} else {
c.FlowCol.UpdateL3Inbound(*c.L3Fingerprint, *c.L3Bytes, 1)
}
if c.L4Fingerprint.Protocol != "" {
if c.Direction == pcap.DirectionOut {
c.FlowCol.UpdateL4Outbound(*c.L4Fingerprint, *c.L4Bytes, 1)
} else {
c.FlowCol.UpdateL4Inbound(*c.L4Fingerprint, *c.L4Bytes, 1)
}
}
}
c.FlowCol.Mu.Unlock()
c.L3Fingerprint.SrcAddr = ""
c.L3Fingerprint.DstAddr = ""
c.L4Fingerprint.SrcAddr = ""
c.L4Fingerprint.DstAddr = ""
c.L4Fingerprint.SrcPort = 0
c.L4Fingerprint.DstPort = 0
c.L4Fingerprint.Protocol = ""
*c.L3Bytes = 0
*c.L4Bytes = 0
}
}