-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
230 lines (176 loc) · 5.08 KB
/
interface.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// SPDX-FileCopyrightText: 2023-2024 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package babel
import (
"errors"
"fmt"
"log/slog"
"net"
"time"
netx "cunicu.li/go-babel/internal/net"
"cunicu.li/go-babel/internal/queue"
"cunicu.li/go-babel/proto"
)
const (
// TrafficClassNetworkControl represents a class selector code-point
// as defined by RFC 2474.
// Routing protocols are recommended to use the __network control_ service class (CS6)
// as recommended by RFC 4594.
// We shift it by 2 bits to account for the ECN bits of the traffic class octet.
//
// See:
// - https://datatracker.ietf.org/doc/html/rfc2474#autoid-9
// - https://datatracker.ietf.org/doc/html/rfc4594#section-3.1
TrafficClassNetworkControl = 48 << 2 // DiffServ / DSCP name CS6
)
// 3.2.3. The Interface Table
// https://datatracker.ietf.org/doc/html/rfc8966#section-3.2.3
type Interface struct {
*net.Interface
multicast bool
Neighbours NeighbourTable
helloMulticastSeqNo proto.SequenceNumber
helloMulticastTimer *time.Ticker
periodicUpdateTimer *time.Ticker
queue *queue.Queue
speaker *Speaker
logger *slog.Logger
}
func (s *Speaker) newInterface(index int) (*Interface, error) {
intf, err := net.InterfaceByIndex(index)
if err != nil {
return nil, err
}
i := &Interface{
Interface: intf,
Neighbours: NewNeighbourTable(),
speaker: s,
multicast: s.config.Multicast,
helloMulticastTimer: time.NewTicker(s.config.MulticastHelloInterval),
periodicUpdateTimer: time.NewTicker(s.config.UpdateInterval),
logger: s.config.Logger.With(
slog.String("intf", intf.Name)),
}
if i.multicast {
multicastAddr := &net.UDPAddr{
IP: MulticastGroupIPv6.AsSlice(),
Port: Port,
}
i.queue = queue.NewQueue(intf.MTU, &netx.PacketConnWriter{
PacketConn: i.speaker.conn.PacketConn,
Dest: multicastAddr,
})
if err := i.speaker.conn.JoinGroup(i.Interface, multicastAddr); err != nil {
return nil, fmt.Errorf("failed to join multicast group: %w", err)
}
}
go i.runTimers()
i.logger.Debug("Added new interface")
return i, nil
}
func (i *Interface) Close() error {
i.periodicUpdateTimer.Stop()
if i.multicast {
if err := i.queue.Close(); err != nil {
return fmt.Errorf("failed to close queue: %w", err)
}
}
return nil
}
func (i *Interface) runTimers() {
for {
select {
case <-i.periodicUpdateTimer.C:
if err := i.sendUpdate(); err != nil {
i.logger.Error("Failed to send periodic update", slog.Any("error", err))
}
case <-i.helloMulticastTimer.C:
if err := i.sendMulticastHello(); err != nil {
i.logger.Error("Failed to send multicast hello", slog.Any("error", err))
}
}
}
}
func (i *Interface) onPacket(pkt *proto.Packet, srcAddr, dstAddr proto.Address) error {
isMulticast := dstAddr.IsLinkLocalMulticast()
i.logger.Debug("Received packet",
slog.Any("src_addr", srcAddr),
slog.Any("dst_addr", dstAddr),
slog.Bool("multicast", isMulticast),
slog.Any("packet", pkt))
n, ok := i.Neighbours.Lookup(srcAddr)
if !ok {
var err error
if n, err = i.NewNeighbour(srcAddr); err != nil {
return fmt.Errorf("failed to create neighbour: %w", err)
}
i.logger.Debug("Found new neighbour",
slog.Any("addr", srcAddr))
if h, ok := i.speaker.config.Handler.(NeighbourHandler); ok {
h.NeighbourAdded(n)
}
i.Neighbours.Insert(n)
}
return n.onPacket(pkt, srcAddr, dstAddr)
}
func (i *Interface) sendMulticastHello() error {
i.logger.Debug("Sending multicast hello")
i.helloMulticastSeqNo++
i.sendValue(&proto.Hello{
Seqno: i.helloMulticastSeqNo,
Interval: i.speaker.config.MulticastHelloInterval,
}, i.speaker.config.MulticastHelloInterval/2)
return nil
}
func (i *Interface) sendUpdate() error {
// TODO: Implement sending of updates
// i.logger.Debug("Sending update")
// i.sendValue(&proto.Update{}, i.speaker.config.MulticastHelloInterval/2)
return nil
}
// TODO: Use function
func (i *Interface) sendMulticastRouteRequest() error { //nolint:unused
i.logger.Debug("Sending multicast route request")
i.sendValue(&proto.RouteRequest{}, i.speaker.config.MulticastHelloInterval/2)
return nil
}
// TODO: Use function
func (i *Interface) sendMulticastSeqnoRequest() error { //nolint:unused
i.logger.Debug("Sending multicast seqno request")
i.sendValue(&proto.SeqnoRequest{}, i.speaker.config.MulticastHelloInterval/2)
return nil
}
// TODO: Use function
//
//nolint:unused
func (i *Interface) findLinkLocalAddress() (net.IP, error) {
addrs, err := i.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
ipNetAddr, ok := addr.(*net.IPNet)
if !ok {
continue
}
ipAddr := ipNetAddr.IP
if ipAddr.To4() != nil {
continue // skip IPv4
}
if !ipAddr.IsLinkLocalUnicast() {
continue // skip non link-local
}
return ipAddr, nil
}
return nil, errors.New("failed to find IPv6 link-local address")
}
func (i *Interface) sendValue(v proto.Value, maxDelay time.Duration) {
if i.multicast {
i.queue.SendValue(v, maxDelay)
} else {
i.Neighbours.Foreach(func(n *Neighbour) error { //nolint:errcheck
n.queue.SendValue(v, maxDelay)
return nil
})
}
}