-
Notifications
You must be signed in to change notification settings - Fork 69
/
router_advertisement.go
187 lines (156 loc) · 5.25 KB
/
router_advertisement.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
package main
import (
"context"
"errors"
"fmt"
"net"
"net/netip"
"time"
"github.com/mdlayher/ndp"
)
const (
raHopLimit = 0
raDefaultRouterLifetime = 0 * time.Second
raDelay = 500 * time.Millisecond
raDefaultPeriod = 3 * time.Minute
)
var (
ipv6LinkLocalAllRouters = netip.MustParseAddr(net.IPv6linklocalallrouters.String())
ipv6LinkLocalAllNodes = netip.MustParseAddr(net.IPv6linklocalallnodes.String())
)
// Material to understand Windows RA behavior:
// * https://insinuator.net/2017/01/ipv6-properties-of-windows-server-2016-windows-10/
// * https://insinuator.net/2017/05/one-step-closer-rdnss-rfc-8106-support-in-windows-10-creators-update/
// * https://ernw.de/download/ERNW_Whitepaper_IPv6_RAs_RDNSS_Conflicting_Parameters_2nd_Iteration_July_2017_v1.1.pdf
// * https://learn.microsoft.com/en-us/answers/questions/884756/after-update-from-windows-10-to-windows-11-ipv6-rf
// SendRouterAdvertisements sends periodic router advertisement messages and
// responds to router solicitation messages.
func SendRouterAdvertisements(ctx context.Context, logger *Logger, config Config) error {
iface, err := net.InterfaceByName(config.Interface.Name)
if err != nil {
return fmt.Errorf("selecting interface %q: %w", config.Interface.Name, err)
}
conn, _, err := ndp.Listen(config.Interface, ndp.LinkLocal)
if err != nil {
return fmt.Errorf("dialing (%s): %w", config.Interface.Name, err)
}
defer func() { _ = conn.Close() }()
err = conn.JoinGroup(ipv6LinkLocalAllRouters)
if err != nil {
return fmt.Errorf("joining multicast group: %w", err)
}
var (
advertizedDNSServer net.IP
dnsLifetime = config.RAPeriod
)
if !config.NoRADNS {
advertizedDNSServer = config.RelayIPv6
}
go respondToRouterSolicit(ctx, conn, logger, config.StatelessRA,
iface.HardwareAddr, config.RouterLifetime, advertizedDNSServer, dnsLifetime)
sleepCtx(ctx, raDelay) // time for DHCPv6 server to start
for {
if ctx.Err() == nil {
err := sendRouterAdvertisement(conn, ipv6LinkLocalAllNodes, config.StatelessRA, iface.HardwareAddr,
config.RouterLifetime, advertizedDNSServer, dnsLifetime, logger, false)
if err != nil {
return err
}
}
timer := time.NewTimer(config.RAPeriod)
select {
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
// de-advertise to remove gateway and DNS server from client configuration
return sendRouterAdvertisement(conn, ipv6LinkLocalAllNodes, config.StatelessRA,
iface.HardwareAddr, 0, advertizedDNSServer, 0, logger, true)
case <-timer.C:
continue
}
}
}
func respondToRouterSolicit(ctx context.Context, c *ndp.Conn, logger *Logger, stateless bool,
routerMac net.HardwareAddr, routerLifetime time.Duration, dnsAddr net.IP, dnsLifetime time.Duration,
) {
for ctx.Err() == nil {
msg, _, addr, err := c.ReadFrom()
if errors.Is(err, net.ErrClosed) {
return
} else if err != nil {
logger.Debugf("receiving NDP message: %v", err)
continue
}
switch m := msg.(type) {
case *ndp.RouterSolicitation:
err = sendRouterAdvertisement(c, addr, stateless, routerMac, routerLifetime,
dnsAddr, dnsLifetime, logger, false)
if errors.Is(err, net.ErrClosed) {
return
} else if err != nil {
logger.Errorf("sending solicited router advertisement: %v", err)
}
case *ndp.RouterAdvertisement:
logger.Debugf("received router advertisement from %s (M=%v, O=%v)",
addr, m.ManagedConfiguration, m.OtherConfiguration)
default:
}
}
}
func sendRouterAdvertisement(c *ndp.Conn, receiver netip.Addr, stateless bool, routerMAC net.HardwareAddr,
routerLifetime time.Duration, dnsAddr net.IP, dnsLifetime time.Duration, logger *Logger, deadvertisement bool,
) error {
if receiver.IsUnspecified() {
receiver = ipv6LinkLocalAllNodes
}
raMessage := &ndp.RouterAdvertisement{
CurrentHopLimit: raHopLimit,
ManagedConfiguration: !stateless,
OtherConfiguration: !stateless,
RouterSelectionPreference: ndp.High,
RouterLifetime: routerLifetime,
Options: []ndp.Option{
&ndp.LinkLayerAddress{
Direction: ndp.Source,
Addr: routerMAC,
},
},
}
if dnsAddr != nil {
netipDNSAddr, ok := netip.AddrFromSlice(dnsAddr)
if !ok {
return fmt.Errorf("converting DNS address %s failed", dnsAddr)
}
// RecursiveDNSServer is supported by Windows 10 since the creators
// update. Setting this option during advertisement and de-advertisement
// prevents situations where the gateway is cleared but the rogoue DNS
// server persists and situations where the gateway is configured but
// not the rogue DNS server. See line "RA Based DNS Config (RFC 6106)"
// in `netsh int ipv6 sh int <interface number>`.
raMessage.Options = append(raMessage.Options, &ndp.RecursiveDNSServer{
Lifetime: dnsLifetime,
Servers: []netip.Addr{netipDNSAddr},
})
}
var r net.IP
if receiver != ipv6LinkLocalAllNodes {
r = receiver.AsSlice()
}
logger.RA(r, routerLifetime != 0, dnsAddr != nil && dnsLifetime != 0, deadvertisement)
err := c.WriteTo(raMessage, nil, receiver)
if err != nil {
return fmt.Errorf("sending router advertisement: %w", err)
}
return nil
}
func sleepCtx(ctx context.Context, d time.Duration) {
timer := time.NewTimer(d)
select {
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
}
}