Skip to content

Commit 8ac4992

Browse files
authored
Merge pull request #31 from negbie/master
Decode erspan
2 parents 1eaff5c + 6ffa236 commit 8ac4992

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type InterfacesConfig struct {
2929
RotationTime int `config:"rotation_time"`
3030
PortRange string `config:"port_range"`
3131
WithVlan bool `config:"with_vlan"`
32+
WithErspan bool `config:"with_erspan"`
3233
Snaplen int `config:"snaplen"`
3334
BufferSizeMb int `config:"buffer_size_mb"`
3435
ReadSpeed bool `config:"top_speed"`

decoder/decoder.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ func (d *Decoder) Process(data []byte, ci *gopacket.CaptureInfo) (*Packet, error
131131
packet := gopacket.NewPacket(data, d.LayerType, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
132132
logp.Debug("layer", "\n%v", packet)
133133

134+
if greLayer := packet.Layer(layers.LayerTypeGRE); greLayer != nil {
135+
gre, ok := greLayer.(*layers.GRE)
136+
if !ok {
137+
return nil, nil
138+
}
139+
140+
if config.Cfg.Iface.WithErspan {
141+
packet = gopacket.NewPacket(gre.Payload[8:], d.LayerType, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
142+
} else {
143+
packet = gopacket.NewPacket(gre.Payload, d.LayerType, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
144+
}
145+
logp.Debug("layer", "\nlayer inside GRE\n%v", packet)
146+
}
147+
134148
if dot1qLayer := packet.Layer(layers.LayerTypeDot1Q); dot1qLayer != nil {
135149
dot1q, ok := dot1qLayer.(*layers.Dot1Q)
136150
if !ok {
@@ -205,7 +219,9 @@ func (d *Decoder) Process(data []byte, ci *gopacket.CaptureInfo) (*Packet, error
205219
return nil, nil
206220
}
207221

208-
pkt.ProtoType = 1
222+
if bytes.Contains(udp.Payload, []byte("sip")) {
223+
pkt.ProtoType = 1
224+
}
209225
pkt.SrcPort = uint16(udp.SrcPort)
210226
pkt.DstPort = uint16(udp.DstPort)
211227
pkt.Payload = udp.Payload
@@ -254,7 +270,9 @@ func (d *Decoder) Process(data []byte, ci *gopacket.CaptureInfo) (*Packet, error
254270
return nil, nil
255271
}
256272

257-
pkt.ProtoType = 1
273+
if bytes.Contains(tcp.Payload, []byte("sip")) {
274+
pkt.ProtoType = 1
275+
}
258276
pkt.SrcPort = uint16(tcp.SrcPort)
259277
pkt.DstPort = uint16(tcp.DstPort)
260278
pkt.Payload = tcp.Payload

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func parseFlags() {
3636
flag.BoolVar(&ifaceConfig.ReadSpeed, "rs", false, "Maximum pcap read speed. Doesn't use packet timestamps")
3737
flag.IntVar(&ifaceConfig.Snaplen, "s", 16384, "Snaplength")
3838
flag.StringVar(&ifaceConfig.PortRange, "pr", "5060-5090", "Portrange to capture SIP")
39-
flag.BoolVar(&ifaceConfig.WithVlan, "vl", false, "Vlan")
39+
flag.BoolVar(&ifaceConfig.WithVlan, "vlan", false, "vlan")
40+
flag.BoolVar(&ifaceConfig.WithErspan, "erspan", false, "erspan")
4041
flag.IntVar(&ifaceConfig.BufferSizeMb, "b", 32, "Interface buffersize (MB)")
4142
flag.StringVar(&logging.Level, "l", "info", "Log level [debug, info, warning, error]")
4243
flag.BoolVar(&ifaceConfig.OneAtATime, "o", false, "Read packet for packet")

sniffer/sniffer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func (sniffer *SnifferSetup) setFromConfig() error {
109109
if sniffer.config.WithVlan {
110110
sniffer.filter = fmt.Sprintf("%s or (vlan and (%s))", sniffer.filter, sniffer.filter)
111111
}
112+
if sniffer.config.WithErspan {
113+
sniffer.filter = fmt.Sprintf("%s or proto GRE", sniffer.filter)
114+
}
112115

113116
logp.Info("Sniffer [type:%s, device:%s, mode:%s] OS [type:%s, arch:%s]",
114117
sniffer.config.Type, sniffer.config.Device, sniffer.mode, runtime.GOOS, runtime.GOARCH)

0 commit comments

Comments
 (0)