-
Notifications
You must be signed in to change notification settings - Fork 346
/
arp_responder.go
145 lines (130 loc) · 3.54 KB
/
arp_responder.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
// Copyright 2022 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package responder
import (
"fmt"
"net"
"sync"
"github.com/mdlayher/arp"
"github.com/mdlayher/ethernet"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
utilnet "k8s.io/utils/net"
)
type arpResponder struct {
iface *net.Interface
conn *arp.Client
assignedIPs sets.String
mutex sync.Mutex
}
var _ Responder = (*arpResponder)(nil)
func NewARPResponder(iface *net.Interface) (*arpResponder, error) {
conn, err := arp.Dial(iface)
if err != nil {
return nil, fmt.Errorf("creating ARP responder for %q: %s", iface.Name, err)
}
return &arpResponder{
iface: iface,
conn: conn,
assignedIPs: sets.NewString(),
}, nil
}
// advertise sends an gratuitous ARP packet for the IP.
func (r *arpResponder) advertise(ip net.IP) error {
pkt, err := arp.NewPacket(arp.OperationRequest, r.iface.HardwareAddr, ip, ethernet.Broadcast, ip)
if err != nil {
return err
}
return r.conn.WriteTo(pkt, ethernet.Broadcast)
}
func (r *arpResponder) InterfaceName() string {
return r.iface.Name
}
func (r *arpResponder) AddIP(ip net.IP) error {
if !utilnet.IsIPv4(ip) {
return fmt.Errorf("only IPv4 is supported")
}
if r.addIP(ip) {
klog.InfoS("Assigned IP to ARP responder", "ip", ip, "interface", r.iface.Name)
err := r.advertise(ip)
if err != nil {
klog.ErrorS(err, "Failed to advertise", "ip", ip, "interface", r.iface.Name)
}
}
return nil
}
func (r *arpResponder) RemoveIP(ip net.IP) error {
if !utilnet.IsIPv4(ip) {
return fmt.Errorf("only IPv4 is supported")
}
if r.deleteIP(ip) {
klog.InfoS("Removed IP from ARP responder", "ip", ip, "interface", r.iface.Name)
}
return nil
}
func (r *arpResponder) handleARPRequest() error {
pkt, _, err := r.conn.Read()
if err != nil {
return err
}
if pkt.Operation != arp.OperationRequest {
return nil
}
if !r.isIPAssigned(pkt.TargetIP) {
klog.V(4).InfoS("Ignored ARP request", "ip", pkt.TargetIP, "interface", r.iface.Name)
return nil
}
if err := r.conn.Reply(pkt, r.iface.HardwareAddr, pkt.TargetIP); err != nil {
return fmt.Errorf("failed to reply ARP packet for IP %s: %v", pkt.TargetIP, err)
}
klog.V(4).InfoS("Sent ARP response", "ip", pkt.TargetIP, "interface", r.iface.Name)
return nil
}
func (r *arpResponder) Run(stopCh <-chan struct{}) {
for {
select {
case <-stopCh:
r.conn.Close()
return
default:
err := r.handleARPRequest()
if err != nil {
klog.ErrorS(err, "Failed to handle ARP request", "deviceName", r.iface.Name)
}
}
}
}
func (r *arpResponder) isIPAssigned(ip net.IP) bool {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.assignedIPs.Has(ip.String())
}
func (r *arpResponder) deleteIP(ip net.IP) bool {
r.mutex.Lock()
defer r.mutex.Unlock()
exist := r.assignedIPs.Has(ip.String())
if exist {
r.assignedIPs.Delete(ip.String())
}
return exist
}
func (r *arpResponder) addIP(ip net.IP) bool {
r.mutex.Lock()
defer r.mutex.Unlock()
exist := r.assignedIPs.Has(ip.String())
if !exist {
r.assignedIPs.Insert(ip.String())
}
return !exist
}