-
Notifications
You must be signed in to change notification settings - Fork 7
/
capture.go
74 lines (64 loc) · 2.11 KB
/
capture.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
package main
import (
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/sirupsen/logrus"
)
// ListenInterface contains the data structures for packet sniffing/copying.
type ListenInterface struct {
fd int
handle *pcap.Handle
}
const hwAddressLength = 0x6
const bpfEapFilter = "ether proto 0x888e"
// setupCaptureDevice opens the given device name for live capture.
// It will only capture packets coming into the interface from the network.
func setupCaptureDevice(device string) ListenInterface {
var filter = bpfEapFilter
if config.Network.VlanID >= 0 {
filter = fmt.Sprintf("%s or (vlan %d and %s)", filter, config.Network.VlanID, filter)
}
handle, err := pcap.OpenLive(device, 9000, config.Network.Promiscuous, pcap.BlockForever)
if err != nil {
log.WithFields(logrus.Fields{"interface": device}).Fatal(err)
}
err = handle.SetDirection(pcap.DirectionIn)
if err != nil {
log.WithFields(logrus.Fields{"interface": device}).Fatal(err)
}
err = handle.SetBPFFilter(filter)
if err != nil {
log.WithFields(logrus.Fields{"interface": device}).Fatal(err)
}
fd := joinMulticastGroup(device)
return ListenInterface{
fd: fd,
handle: handle,
}
}
// Decide if we want to forward a packet from the router.
// We might want to ignore START and LOGOFF packets emitted by the AT&T CPE.
func handleRouterPacket(packet gopacket.Packet) bool {
if eapolLayer := packet.Layer(layers.LayerTypeEAPOL); eapolLayer != nil {
eapol, _ := eapolLayer.(*layers.EAPOL)
if config.Ignore.Start && eapol.Type == layers.EAPOLTypeStart {
log.Debug("Ignoring START packet from Router")
return false
}
if config.Ignore.Logoff && eapol.Type == layers.EAPOLTypeLogOff {
log.Debug("Ignoring LOGOFF packet from Router")
return false
}
}
return true
}
// Emit a captured packet onto the wire through the given pcap handle.
// No modifications are being done to the packet, this is a 1:1 mirror.
func emitPacket(packet gopacket.Packet, destination *pcap.Handle) {
err := destination.WritePacketData(packet.Data())
if err != nil {
log.Fatal(err)
}
}