forked from Gandem/bonjour-reflector
-
Notifications
You must be signed in to change notification settings - Fork 14
/
arp.go
143 lines (126 loc) · 3.86 KB
/
arp.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
package main
import (
"fmt"
"net"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/sirupsen/logrus"
)
func ownupNetworkAddresses(netInterface string, srcMACAddress net.HardwareAddr, vlanIPMap map[uint16]net.IP, stop chan struct{}) {
IPv6Address = generateIPv6FromMac(srcMACAddress)
// Get a handle on the network interface
rawTraffic, err := pcap.OpenLive(netInterface, 65536, true, time.Second)
if err != nil {
logrus.Fatalf("Could not find network interface: %v", netInterface)
}
// Gratuitous ARP just once after startup
for vlan, ip := range vlanIPMap {
err := sendARP(rawTraffic, srcMACAddress, net.HardwareAddr{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, ip, ip, vlan)
if err != nil {
logrus.Error(err)
continue
}
}
// Announce link-local just once after startup
for vlan := range vlanIPMap {
err := sendNA(rawTraffic, srcMACAddress, net.HardwareAddr{0x33, 0x33, 0x00, 0x00, 0x00, 0x01}, IPv6Address, net.IPv6linklocalallnodes, vlan)
if err != nil {
logrus.Error(err)
continue
}
}
filterTemplate := "not (ether src %s) and vlan and (arp or icmp6)"
err = rawTraffic.SetBPFFilter(fmt.Sprintf(filterTemplate, srcMACAddress))
if err != nil {
logrus.Fatalf("Could not apply filter on network interface: %v", err)
}
src := gopacket.NewPacketSource(rawTraffic, layers.LayerTypeEthernet)
in := src.Packets()
for {
var packet gopacket.Packet
select {
case <-stop:
return
case packet = <-in:
if packet.Layer(layers.LayerTypeARP) != nil {
respondToArpRequests(rawTraffic, packet, srcMACAddress, vlanIPMap)
}
if packet.Layer(layers.LayerTypeICMPv6NeighborSolicitation) != nil {
respondToNeighborSolicitation(rawTraffic, packet, srcMACAddress, vlanIPMap)
}
}
}
}
// respondToArpRequests watches a handle for incoming ARP requests we might care about, and replies to them
//
// respondToArpRequests loops until 'stop' is closed.
func respondToArpRequests(rawTraffic *pcap.Handle, packet gopacket.Packet, srcMACAddress net.HardwareAddr, vlanIPMap map[uint16]net.IP) {
tag := parseVLANTag(packet)
ip := vlanIPMap[*tag]
if ip == nil {
return
}
arpLayer := packet.Layer(layers.LayerTypeARP)
if arpLayer == nil {
return
}
arp := arpLayer.(*layers.ARP)
if arp.Operation != layers.ARPRequest {
return
}
if !net.IP(arp.DstProtAddress).Equal(ip) {
return
}
err := sendARP(rawTraffic, srcMACAddress, net.HardwareAddr(arp.SourceHwAddress), ip, arp.SourceProtAddress, *tag)
if err != nil {
logrus.Error(err)
return
}
logrus.Debugf("Replied to %v for ip %s", net.HardwareAddr(arp.SourceHwAddress), ip.String())
}
func sendARP(rawTraffic *pcap.Handle, srcMACAddress net.HardwareAddr, dstMACAddress net.HardwareAddr, srcIP net.IP, dstIP net.IP, vlanTag uint16) error {
if len(srcIP) == 16 {
srcIP = srcIP[12:] // net.IP is 16 bytes, which make the FixLength fail as an ip can only be 4
}
if len(dstIP) == 16 {
dstIP = dstIP[12:]
}
sendEth := layers.Ethernet{
SrcMAC: srcMACAddress,
DstMAC: dstMACAddress,
EthernetType: layers.EthernetTypeDot1Q,
}
sendTag := layers.Dot1Q{
Priority: 0,
DropEligible: false,
VLANIdentifier: vlanTag,
Type: layers.EthernetTypeARP,
}
sendArp := layers.ARP{
AddrType: layers.LinkTypeEthernet,
Protocol: layers.EthernetTypeIPv4,
HwAddressSize: 6,
ProtAddressSize: 4,
Operation: layers.ARPReply,
SourceHwAddress: srcMACAddress,
SourceProtAddress: srcIP,
DstHwAddress: dstMACAddress,
DstProtAddress: dstIP,
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
err := gopacket.SerializeLayers(buf, opts, &sendEth, &sendTag, &sendArp)
if err != nil {
return err
}
err = rawTraffic.WritePacketData(buf.Bytes())
if err != nil {
return err
}
return nil
}