-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapability_arp.go
73 lines (61 loc) · 1.94 KB
/
capability_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
package exu
import log "github.com/sirupsen/logrus"
type CapabilityArp struct {
*IpDevice
}
func (c CapabilityArp) HandleRequest(port *VPort, data *EthernetFrame) CapabilityStatus {
// get the ARP payload
arpPayload := &ArpPacket{}
err := arpPayload.FromBytes(data.Payload())
if err != nil {
return CapabilityStatusFail
}
// if this is an ARP response, check if it's for one of our ports
if arpPayload.Opcode == ArpOpcodeReply && arpPayload.TargetIP.Equal(c.portIPs[port].IP) {
c.arpTableMu.Lock()
defer c.arpTableMu.Unlock()
c.arpTable[arpPayload.SenderIP.String()] = arpPayload.SenderMac
log.WithFields(log.Fields{
"device": c.name,
"port": port.portCname,
"ip": arpPayload.SenderIP,
"learned_mac": arpPayload.SenderMac,
"capabilty": "arp",
}).Debug("learned ARP entry")
return CapabilityStatusDone
}
// if the ARP packet is for one of our ports, reply with our MAC address
if c.portIPs[port].IP.Equal(arpPayload.TargetIP) {
// create the ARP payload
arpResponsePayload := &ArpPacket{
HardwareType: arpPayload.HardwareType,
ProtocolType: arpPayload.ProtocolType,
Opcode: ArpOpcodeReply,
SenderIP: arpPayload.TargetIP,
TargetIP: arpPayload.SenderIP,
SenderMac: port.mac,
TargetMac: arpPayload.SenderMac,
}
// create the ethernet frame
var ethernetFrame *EthernetFrame
ethernetFrame, err = NewEthernetFrame(data.Source(), data.Destination(), WithTagging(TaggingUntagged), arpResponsePayload)
if err != nil {
return CapabilityStatusFail
}
// write the frame to the source port
_ = port.Write(ethernetFrame)
log.WithFields(log.Fields{
"device": c.name,
"port": port.portCname,
"capabilty": "arp",
}).Debug("sent ARP response")
return CapabilityStatusDone
}
return CapabilityStatusPass
}
func (c CapabilityArp) Match(_ *VPort, data *EthernetFrame) bool {
if data.EtherType() != EtherTypeARP {
return false
}
return true
}