-
Notifications
You must be signed in to change notification settings - Fork 884
/
ndp.go
171 lines (151 loc) · 4.53 KB
/
ndp.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// SPDX-License-Identifier:Apache-2.0
package layer2
import (
"errors"
"fmt"
"io"
"net"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/mdlayher/ndp"
)
type ndpResponder struct {
logger log.Logger
intf string
hardwareAddr net.HardwareAddr
conn *ndp.Conn
closed chan struct{}
announce announceFunc
// Refcount of how many watchers for each solicited node
// multicast group.
solicitedNodeGroups map[string]int64
}
func newNDPResponder(logger log.Logger, ifi *net.Interface, ann announceFunc) (*ndpResponder, error) {
// Use link-local address as the source IPv6 address for NDP communications.
conn, _, err := ndp.Dial(ifi, ndp.LinkLocal)
if err != nil {
return nil, fmt.Errorf("creating NDP responder for %q: %s", ifi.Name, err)
}
ret := &ndpResponder{
logger: logger,
intf: ifi.Name,
hardwareAddr: ifi.HardwareAddr,
conn: conn,
closed: make(chan struct{}),
announce: ann,
solicitedNodeGroups: map[string]int64{},
}
go ret.run()
return ret, nil
}
func (n *ndpResponder) Interface() string { return n.intf }
func (n *ndpResponder) Close() error {
close(n.closed)
return n.conn.Close()
}
func (n *ndpResponder) Gratuitous(ip net.IP) error {
err := n.advertise(net.IPv6linklocalallnodes, ip, true)
stats.SentGratuitous(ip.String())
return err
}
func (n *ndpResponder) Watch(ip net.IP) error {
if ip.To4() != nil {
return nil
}
group, err := ndp.SolicitedNodeMulticast(ip)
if err != nil {
return fmt.Errorf("looking up solicited node multicast group for %q: %s", ip, err)
}
if n.solicitedNodeGroups[group.String()] == 0 {
if err = n.conn.JoinGroup(group); err != nil {
return fmt.Errorf("joining solicited node multicast group for %q: %s", ip, err)
}
}
n.solicitedNodeGroups[group.String()]++
return nil
}
func (n *ndpResponder) Unwatch(ip net.IP) error {
if ip.To4() != nil {
return nil
}
group, err := ndp.SolicitedNodeMulticast(ip)
if err != nil {
return fmt.Errorf("looking up solicited node multicast group for %q: %s", ip, err)
}
n.solicitedNodeGroups[group.String()]--
if n.solicitedNodeGroups[group.String()] == 0 {
if err = n.conn.LeaveGroup(group); err != nil {
return fmt.Errorf("leaving solicited node multicast group for %q: %s", ip, err)
}
}
return nil
}
func (n *ndpResponder) run() {
for n.processRequest() != dropReasonClosed {
}
}
func (n *ndpResponder) processRequest() dropReason {
msg, _, src, err := n.conn.ReadFrom()
if err != nil {
select {
case <-n.closed:
return dropReasonClosed
default:
}
if errors.Is(err, io.EOF) {
return dropReasonClosed
}
return dropReasonError
}
ns, ok := msg.(*ndp.NeighborSolicitation)
if !ok {
return dropReasonMessageType
}
// Retrieve sender's source link-layer address
var nsLLAddr net.HardwareAddr
for _, o := range ns.Options {
// Ignore other options, including target link-layer address instead of source.
lla, ok := o.(*ndp.LinkLayerAddress)
if !ok {
continue
}
if lla.Direction != ndp.Source {
continue
}
nsLLAddr = lla.Addr
break
}
if nsLLAddr == nil {
return dropReasonNoSourceLL
}
// Ignore NDP requests that the announcer tells us to ignore.
reason := n.announce(ns.TargetAddress, n.intf)
if reason == dropReasonNotMatchInterface {
level.Debug(n.logger).Log("op", "ndpRequestIgnore", "ip", ns.TargetAddress, "interface", n.intf, "reason", "notMatchInterface")
}
if reason != dropReasonNone {
return reason
}
stats.GotRequest(ns.TargetAddress.String())
level.Debug(n.logger).Log("interface", n.intf, "ip", ns.TargetAddress, "senderIP", src, "senderLLAddr", nsLLAddr, "responseMAC", n.hardwareAddr, "msg", "got NDP request for service IP, sending response")
if err := n.advertise(src, ns.TargetAddress, false); err != nil {
level.Error(n.logger).Log("op", "ndpReply", "interface", n.intf, "ip", ns.TargetAddress, "senderIP", src, "senderLLAddr", nsLLAddr, "responseMAC", n.hardwareAddr, "error", err, "msg", "failed to send ARP reply")
} else {
stats.SentResponse(ns.TargetAddress.String())
}
return dropReasonNone
}
func (n *ndpResponder) advertise(dst, target net.IP, gratuitous bool) error {
m := &ndp.NeighborAdvertisement{
Solicited: !gratuitous, // <Adam Jensen> I never asked for this...
Override: gratuitous, // Should clients replace existing cache entries
TargetAddress: target,
Options: []ndp.Option{
&ndp.LinkLayerAddress{
Direction: ndp.Target,
Addr: n.hardwareAddr,
},
},
}
return n.conn.WriteTo(m, nil, dst)
}