forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sniffer.go
223 lines (184 loc) · 5.46 KB
/
sniffer.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
package main
import (
"fmt"
"os"
"time"
"github.com/packetbeat/gopacket"
"github.com/packetbeat/gopacket/layers"
"github.com/packetbeat/gopacket/pcap"
)
type SnifferSetup struct {
pcapHandle *pcap.Handle
afpacketHandle *AfpacketHandle
pfringHandle *PfringHandle
config *tomlInterfaces
DataSource gopacket.PacketDataSource
}
type tomlInterfaces struct {
Device string
Devices []string
Type string
File string
With_vlans bool
Bpf_filter string
Snaplen int
Buffer_size_mb int
}
// Computes the block_size and the num_blocks in such a way that the
// allocated mmap buffer is close to but smaller than target_size_mb.
// The restriction is that the block_size must be divisible by both the
// frame size and page size.
func afpacketComputeSize(target_size_mb int, snaplen int, page_size int) (
frame_size int, block_size int, num_blocks int, err error) {
if snaplen < page_size {
frame_size = page_size / (page_size / snaplen)
} else {
frame_size = (snaplen/page_size + 1) * page_size
}
// 128 is the default from the gopacket library so just use that
block_size = frame_size * 128
num_blocks = (target_size_mb * 1024 * 1024) / block_size
if num_blocks == 0 {
return 0, 0, 0, fmt.Errorf("Buffer size too small")
}
return frame_size, block_size, num_blocks, nil
}
func CreateSniffer(config *tomlInterfaces, file *string) (*SnifferSetup, error) {
var sniffer SnifferSetup
var err error
sniffer.config = config
if file != nil && len(*file) > 0 {
DEBUG("sniffer", "Reading from file: %s", *file)
// we read file with the pcap provider
sniffer.config.Type = "pcap"
sniffer.config.File = *file
}
// set defaults
if len(sniffer.config.Device) == 0 {
sniffer.config.Device = "any"
}
if len(sniffer.config.Devices) == 0 {
// 'devices' not set but 'device' is set. For backwards compatibility,
// use the one configured device
if len(sniffer.config.Device) > 0 {
sniffer.config.Devices = []string{sniffer.config.Device}
}
}
if sniffer.config.Snaplen == 0 {
if sniffer.config.Devices[0] == "any" || sniffer.config.Devices[0] == "lo" {
sniffer.config.Snaplen = 16436
} else {
sniffer.config.Snaplen = 1514
}
}
if sniffer.config.Type == "autodetect" || sniffer.config.Type == "" {
sniffer.config.Type = "pcap"
}
DEBUG("sniffer", "Sniffer type: %s devices: %s", sniffer.config.Type, sniffer.config.Devices)
switch sniffer.config.Type {
case "pcap":
if len(sniffer.config.File) > 0 {
sniffer.pcapHandle, err = pcap.OpenOffline(sniffer.config.File)
if err != nil {
return nil, err
}
} else {
if len(sniffer.config.Devices) > 1 {
return nil, fmt.Errorf("Pcap sniffer only supports one device. You can use 'any' if you want")
}
sniffer.pcapHandle, err = pcap.OpenLive(
sniffer.config.Devices[0],
int32(sniffer.config.Snaplen),
true,
500*time.Millisecond)
if err != nil {
return nil, err
}
err = sniffer.pcapHandle.SetBPFFilter(sniffer.config.Bpf_filter)
if err != nil {
return nil, err
}
}
sniffer.DataSource = gopacket.PacketDataSource(sniffer.pcapHandle)
case "af_packet":
if sniffer.config.Buffer_size_mb == 0 {
sniffer.config.Buffer_size_mb = 24
}
if len(sniffer.config.Devices) > 1 {
return nil, fmt.Errorf("Afpacket sniffer only supports one device. You can use 'any' if you want")
}
frame_size, block_size, num_blocks, err := afpacketComputeSize(
sniffer.config.Buffer_size_mb,
sniffer.config.Snaplen,
os.Getpagesize())
if err != nil {
return nil, err
}
sniffer.afpacketHandle, err = NewAfpacketHandle(
sniffer.config.Devices[0],
frame_size,
block_size,
num_blocks,
500*time.Millisecond)
if err != nil {
return nil, err
}
err = sniffer.afpacketHandle.SetBPFFilter(sniffer.config.Bpf_filter)
if err != nil {
return nil, fmt.Errorf("SetBPFFilter failed: %s", err)
}
sniffer.DataSource = gopacket.PacketDataSource(sniffer.afpacketHandle)
case "pfring":
if len(sniffer.config.Devices) > 1 {
return nil, fmt.Errorf("Afpacket sniffer only supports one device. You can use 'any' if you want")
}
sniffer.pfringHandle, err = NewPfringHandle(
sniffer.config.Devices[0],
sniffer.config.Snaplen,
true)
if err != nil {
return nil, err
}
err = sniffer.pfringHandle.SetBPFFilter(sniffer.config.Bpf_filter)
if err != nil {
return nil, fmt.Errorf("SetBPFFilter failed: %s", err)
}
err = sniffer.pfringHandle.Enable()
if err != nil {
return nil, fmt.Errorf("Enable failed: %s", err)
}
sniffer.DataSource = gopacket.PacketDataSource(sniffer.pfringHandle)
default:
return nil, fmt.Errorf("Unknown sniffer type: %s", sniffer.config.Type)
}
return &sniffer, nil
}
func (sniffer *SnifferSetup) Reopen() error {
var err error
if sniffer.config.Type != "pcap" || sniffer.config.File == "" {
return fmt.Errorf("Reopen is only possible for files")
}
sniffer.pcapHandle.Close()
sniffer.pcapHandle, err = pcap.OpenOffline(sniffer.config.File)
if err != nil {
return err
}
sniffer.DataSource = gopacket.PacketDataSource(sniffer.pcapHandle)
return nil
}
func (sniffer *SnifferSetup) Close() {
switch sniffer.config.Type {
case "pcap":
sniffer.pcapHandle.Close()
case "af_packet":
sniffer.afpacketHandle.Close()
case "pfring":
sniffer.pfringHandle.Close()
}
}
func (sniffer *SnifferSetup) Datalink() layers.LinkType {
if sniffer.config.Type == "pcap" {
return sniffer.pcapHandle.LinkType()
}
return layers.LinkTypeEthernet
}