Debian Codegraph @

e2e/handshakes_test.go

raw
1 //go:build e2e_testing
2 // +build e2e_testing
3
4 package e2e
5
6 import (
7 "net/netip"
8 "slices"
9 "testing"
10 "time"
11
12 "github.com/google/gopacket"
13 "github.com/google/gopacket/layers"
14 "github.com/slackhq/nebula"
15 "github.com/slackhq/nebula/cert"
16 "github.com/slackhq/nebula/cert_test"
17 "github.com/slackhq/nebula/e2e/router"
18 "github.com/slackhq/nebula/header"
19 "github.com/slackhq/nebula/overlay"
20 "github.com/slackhq/nebula/udp"
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23 "go.yaml.in/yaml/v3"
24 )
25
26 func BenchmarkHotPath(b *testing.B) {
27 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
28 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
29 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
30
31 // Put their info in our lighthouse
32 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
33 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
34
35 // Start the servers
36 myControl.Start()
37 theirControl.Start()
38
39 r := router.NewR(b, myControl, theirControl)
40 r.CancelFlowLogs()
41
42 assertTunnel(b, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
43
44 // Pre-build the IP packet bytes once so the bench measures the data plane,
45 // not gopacket SerializeLayers overhead.
46 prebuilt := BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
47
48 // EnableFanIn switches the router to a 0-alloc routing path. Required
49 // for hot-path benchmarks; would conflict with GetFromUDP-using tests.
50 r.EnableFanIn()
51
52 b.ResetTimer()
53
54 for n := 0; n < b.N; n++ {
55 myControl.InjectTunPacket(prebuilt)
56 // Release the TUN-side bytes back to the harness freelist; the bench
57 // just confirms a packet arrived, the contents aren't inspected.
58 overlay.ReleaseTunBuf(r.RouteForAllUntilTxTun(theirControl))
59 }
60
61 myControl.Stop()
62 theirControl.Stop()
63 }
64
65 func BenchmarkHotPathRelay(b *testing.B) {
66 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
67 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
68 relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
69 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
70
71 // Teach my how to get to the relay and that their can be reached via the relay
72 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
73 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
74 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
75
76 // Build a router so we don't have to reason who gets which packet
77 r := router.NewR(b, myControl, relayControl, theirControl)
78 r.CancelFlowLogs()
79
80 // Start the servers
81 myControl.Start()
82 relayControl.Start()
83 theirControl.Start()
84
85 assertTunnel(b, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
86
87 prebuilt := BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
88 r.EnableFanIn()
89
90 b.ResetTimer()
91
92 for n := 0; n < b.N; n++ {
93 myControl.InjectTunPacket(prebuilt)
94 overlay.ReleaseTunBuf(r.RouteForAllUntilTxTun(theirControl))
95 }
96
97 myControl.Stop()
98 theirControl.Stop()
99 relayControl.Stop()
100 }
101
102 func TestGoodHandshake(t *testing.T) {
103 t.Parallel()
104 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
105 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
106 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
107
108 // Put their info in our lighthouse
109 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
110
111 // Start the servers
112 myControl.Start()
113 theirControl.Start()
114
115 t.Log("Send a udp packet through to begin standing up the tunnel, this should come out the other side")
116 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
117
118 t.Log("Have them consume my stage 0 packet. They have a tunnel now")
119 theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
120
121 t.Log("Get their stage 1 packet so that we can play with it")
122 stage1Packet := theirControl.GetFromUDP(true)
123
124 t.Log("I consume a garbage packet with a proper nebula header for our tunnel")
125 // this should log a statement and get ignored, allowing the real handshake packet to complete the tunnel
126 badPacket := stage1Packet.Copy()
127 badPacket.Data = badPacket.Data[:len(badPacket.Data)-header.Len]
128 myControl.InjectUDPPacket(badPacket)
129
130 t.Log("Have me consume their real stage 1 packet. I have a tunnel now")
131 myControl.InjectUDPPacket(stage1Packet)
132
133 t.Log("Wait until we see my cached packet come through")
134 myControl.WaitForType(1, 0, theirControl)
135
136 t.Log("Make sure our host infos are correct")
137 assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
138
139 t.Log("Get that cached packet and make sure it looks right")
140 myCachedPacket := theirControl.GetFromTun(true)
141 assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
142
143 t.Log("Do a bidirectional tunnel test")
144 r := router.NewR(t, myControl, theirControl)
145 defer r.RenderFlow()
146 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
147
148 r.RenderHostmaps("Final hostmaps", myControl, theirControl)
149 myControl.Stop()
150 theirControl.Stop()
151 }
152
153 func TestGoodHandshakeNoOverlap(t *testing.T) {
154 t.Parallel()
155 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
156 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.1/24", nil)
157 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "2001::69/24", nil) //look ma, cross-stack!
158
159 // Put their info in our lighthouse
160 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
161
162 // Start the servers
163 myControl.Start()
164 theirControl.Start()
165
166 empty := []byte{}
167 t.Log("do something to cause a handshake")
168 myControl.GetF().SendMessageToVpnAddr(header.Test, header.MessageNone, theirVpnIpNet[0].Addr(), empty, empty, empty)
169
170 t.Log("Have them consume my stage 0 packet. They have a tunnel now")
171 theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
172
173 t.Log("Get their stage 1 packet")
174 stage1Packet := theirControl.GetFromUDP(true)
175
176 t.Log("Have me consume their stage 1 packet. I have a tunnel now")
177 myControl.InjectUDPPacket(stage1Packet)
178
179 t.Log("Wait until we see a test packet come through to make sure we give the tunnel time to complete")
180 myControl.WaitForType(header.Test, 0, theirControl)
181
182 t.Log("Make sure our host infos are correct")
183 assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
184
185 myControl.Stop()
186 theirControl.Stop()
187 }
188
189 func TestWrongResponderHandshake(t *testing.T) {
190 t.Parallel()
191 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
192
193 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.100/24", nil)
194 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.99/24", nil)
195 evilControl, evilVpnIp, evilUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "evil", "10.128.0.2/24", nil)
196
197 // Put the evil udp addr in for their vpn Ip, this is a case of being lied to by the lighthouse.
198 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), evilUdpAddr)
199
200 // Build a router so we don't have to reason who gets which packet
201 r := router.NewR(t, myControl, theirControl, evilControl)
202 defer r.RenderFlow()
203
204 // Start the servers
205 myControl.Start()
206 theirControl.Start()
207 evilControl.Start()
208
209 t.Log("Start the handshake process, we will route until we see the evil tunnel closed")
210 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
211
212 h := &header.H{}
213 r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
214 err := h.Parse(p.Data)
215 if err != nil {
216 panic(err)
217 }
218
219 if h.Type == header.CloseTunnel && p.To == evilUdpAddr {
220 return router.RouteAndExit
221 }
222
223 return router.KeepRouting
224 })
225
226 t.Log("Evil tunnel is closed, inject the correct udp addr for them")
227 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
228 pendingHi := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), true)
229 assert.NotContains(t, pendingHi.RemoteAddrs, evilUdpAddr)
230
231 t.Log("Route until we see the cached packet")
232 r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
233 err := h.Parse(p.Data)
234 if err != nil {
235 panic(err)
236 }
237
238 if p.To == theirUdpAddr && h.Type == 1 {
239 return router.RouteAndExit
240 }
241
242 return router.KeepRouting
243 })
244
245 t.Log("My cached packet should be received by them")
246 myCachedPacket := theirControl.GetFromTun(true)
247 assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
248
249 t.Log("Test the tunnel with them")
250 assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
251 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
252
253 t.Log("Flush all packets from all controllers")
254 r.FlushAll()
255
256 t.Log("Ensure ensure I don't have any hostinfo artifacts from evil")
257 assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), true), "My pending hostmap should not contain evil")
258 assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), false), "My main hostmap should not contain evil")
259
260 r.RenderHostmaps("Final hostmaps", myControl, theirControl, evilControl)
261 t.Log("Success!")
262 myControl.Stop()
263 theirControl.Stop()
264 }
265
266 func TestWrongResponderHandshakeStaticHostMap(t *testing.T) {
267 t.Parallel()
268 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
269
270 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.99/24", nil)
271 evilControl, evilVpnIp, evilUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "evil", "10.128.0.2/24", nil)
272 o := m{
273 "static_host_map": m{
274 theirVpnIpNet[0].Addr().String(): []string{evilUdpAddr.String()},
275 },
276 }
277 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.100/24", o)
278
279 // Put the evil udp addr in for their vpn addr, this is a case of a remote at a static entry changing its vpn addr.
280 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), evilUdpAddr)
281
282 // Build a router so we don't have to reason who gets which packet
283 r := router.NewR(t, myControl, theirControl, evilControl)
284 defer r.RenderFlow()
285
286 // Start the servers
287 myControl.Start()
288 theirControl.Start()
289 evilControl.Start()
290
291 t.Log("Start the handshake process, we will route until we see the evil tunnel closed")
292 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
293
294 h := &header.H{}
295 r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
296 err := h.Parse(p.Data)
297 if err != nil {
298 panic(err)
299 }
300
301 if h.Type == header.CloseTunnel && p.To == evilUdpAddr {
302 return router.RouteAndExit
303 }
304
305 return router.KeepRouting
306 })
307
308 t.Log("Evil tunnel is closed, inject the correct udp addr for them")
309 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
310 pendingHi := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), true)
311 assert.NotContains(t, pendingHi.RemoteAddrs, evilUdpAddr)
312
313 t.Log("Route until we see the cached packet")
314 r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
315 err := h.Parse(p.Data)
316 if err != nil {
317 panic(err)
318 }
319
320 if p.To == theirUdpAddr && h.Type == 1 {
321 return router.RouteAndExit
322 }
323
324 return router.KeepRouting
325 })
326
327 t.Log("My cached packet should be received by them")
328 myCachedPacket := theirControl.GetFromTun(true)
329 assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
330
331 t.Log("Test the tunnel with them")
332 assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
333 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
334
335 t.Log("Flush all packets from all controllers")
336 r.FlushAll()
337
338 t.Log("Ensure ensure I don't have any hostinfo artifacts from evil")
339 assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), true), "My pending hostmap should not contain evil")
340 assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), false), "My main hostmap should not contain evil")
341 //NOTE: if evil lost the handshake race it may still have a tunnel since me would reject the handshake since the tunnel is complete
342
343 r.RenderHostmaps("Final hostmaps", myControl, theirControl, evilControl)
344 t.Log("Success!")
345 myControl.Stop()
346 theirControl.Stop()
347 }
348
349 func TestStage1Race(t *testing.T) {
350 t.Parallel()
351 // This tests ensures that two hosts handshaking with each other at the same time will allow traffic to flow
352 // But will eventually collapse down to a single tunnel
353
354 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
355 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
356 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
357
358 // Put their info in our lighthouse and vice versa
359 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
360 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
361
362 // Build a router so we don't have to reason who gets which packet
363 r := router.NewR(t, myControl, theirControl)
364 defer r.RenderFlow()
365
366 // Start the servers
367 myControl.Start()
368 theirControl.Start()
369
370 t.Log("Trigger a handshake to start on both me and them")
371 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
372 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
373
374 t.Log("Get both stage 1 handshake packets")
375 myHsForThem := myControl.GetFromUDP(true)
376 theirHsForMe := theirControl.GetFromUDP(true)
377
378 r.Log("Now inject both stage 1 handshake packets")
379 r.InjectUDPPacket(theirControl, myControl, theirHsForMe)
380 r.InjectUDPPacket(myControl, theirControl, myHsForThem)
381
382 r.Log("Route until they receive a message packet")
383 myCachedPacket := r.RouteForAllUntilTxTun(theirControl)
384 assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
385
386 r.Log("Their cached packet should be received by me")
387 theirCachedPacket := r.RouteForAllUntilTxTun(myControl)
388 assertUdpPacket(t, []byte("Hi from them"), theirCachedPacket, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
389
390 r.Log("Do a bidirectional tunnel test")
391 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
392
393 myHostmapHosts := myControl.ListHostmapHosts(false)
394 myHostmapIndexes := myControl.ListHostmapIndexes(false)
395 theirHostmapHosts := theirControl.ListHostmapHosts(false)
396 theirHostmapIndexes := theirControl.ListHostmapIndexes(false)
397
398 // We should have two tunnels on both sides
399 assert.Len(t, myHostmapHosts, 1)
400 assert.Len(t, theirHostmapHosts, 1)
401 assert.Len(t, myHostmapIndexes, 2)
402 assert.Len(t, theirHostmapIndexes, 2)
403
404 r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
405
406 r.Log("Spin until connection manager tears down a tunnel")
407
408 for myControl.GetHostmapIndexCount()+theirControl.GetHostmapIndexCount() > 2 {
409 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
410 t.Log("Connection manager hasn't ticked yet")
411 time.Sleep(time.Second)
412 }
413
414 myFinalHostmapHosts := myControl.ListHostmapHosts(false)
415 myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
416 theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
417 theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
418
419 // We should only have a single tunnel now on both sides
420 assert.Len(t, myFinalHostmapHosts, 1)
421 assert.Len(t, theirFinalHostmapHosts, 1)
422 assert.Len(t, myFinalHostmapIndexes, 1)
423 assert.Len(t, theirFinalHostmapIndexes, 1)
424
425 r.RenderHostmaps("Final hostmaps", myControl, theirControl)
426 myControl.Stop()
427 theirControl.Stop()
428 }
429
430 func TestUncleanShutdownRaceLoser(t *testing.T) {
431 t.Parallel()
432 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
433 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
434 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
435
436 // Teach my how to get to the relay and that their can be reached via the relay
437 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
438 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
439
440 // Build a router so we don't have to reason who gets which packet
441 r := router.NewR(t, myControl, theirControl)
442 defer r.RenderFlow()
443
444 // Start the servers
445 myControl.Start()
446 theirControl.Start()
447
448 r.Log("Trigger a handshake from me to them")
449 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
450
451 p := r.RouteForAllUntilTxTun(theirControl)
452 assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
453
454 r.Log("Nuke my hostmap")
455 myHostmap := myControl.GetHostmap()
456 myHostmap.Lock()
457 myHostmap.Hosts = map[netip.Addr]*nebula.HostInfo{}
458 myHostmap.Indexes = map[uint32]*nebula.HostInfo{}
459 myHostmap.RemoteIndexes = map[uint32]*nebula.HostInfo{}
460 myHostmap.Unlock()
461
462 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me again")))
463 p = r.RouteForAllUntilTxTun(theirControl)
464 assertUdpPacket(t, []byte("Hi from me again"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
465
466 r.Log("Assert the tunnel works")
467 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
468
469 r.Log("Wait for the dead index to go away")
470 start := theirControl.GetHostmapIndexCount()
471 for {
472 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
473 if theirControl.GetHostmapIndexCount() < start {
474 break
475 }
476 time.Sleep(time.Second)
477 }
478
479 r.RenderHostmaps("Final hostmaps", myControl, theirControl)
480 }
481
482 func TestUncleanShutdownRaceWinner(t *testing.T) {
483 t.Parallel()
484 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
485 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
486 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
487
488 // Teach my how to get to the relay and that their can be reached via the relay
489 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
490 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
491
492 // Build a router so we don't have to reason who gets which packet
493 r := router.NewR(t, myControl, theirControl)
494 defer r.RenderFlow()
495
496 // Start the servers
497 myControl.Start()
498 theirControl.Start()
499
500 r.Log("Trigger a handshake from me to them")
501 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
502
503 p := r.RouteForAllUntilTxTun(theirControl)
504 assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
505 r.RenderHostmaps("Final hostmaps", myControl, theirControl)
506
507 r.Log("Nuke my hostmap")
508 theirHostmap := theirControl.GetHostmap()
509 theirHostmap.Lock()
510 theirHostmap.Hosts = map[netip.Addr]*nebula.HostInfo{}
511 theirHostmap.Indexes = map[uint32]*nebula.HostInfo{}
512 theirHostmap.RemoteIndexes = map[uint32]*nebula.HostInfo{}
513 theirHostmap.Unlock()
514
515 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them again")))
516 p = r.RouteForAllUntilTxTun(myControl)
517 assertUdpPacket(t, []byte("Hi from them again"), p, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
518 r.RenderHostmaps("Derp hostmaps", myControl, theirControl)
519
520 r.Log("Assert the tunnel works")
521 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
522
523 r.Log("Wait for the dead index to go away")
524 start := myControl.GetHostmapIndexCount()
525 for {
526 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
527 if myControl.GetHostmapIndexCount() < start {
528 break
529 }
530 time.Sleep(time.Second)
531 }
532
533 r.RenderHostmaps("Final hostmaps", myControl, theirControl)
534 }
535
536 func TestRelays(t *testing.T) {
537 t.Parallel()
538 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
539 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
540 relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
541 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
542
543 // Teach my how to get to the relay and that their can be reached via the relay
544 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
545 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
546 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
547
548 // Build a router so we don't have to reason who gets which packet
549 r := router.NewR(t, myControl, relayControl, theirControl)
550 defer r.RenderFlow()
551
552 // Start the servers
553 myControl.Start()
554 relayControl.Start()
555 theirControl.Start()
556
557 t.Log("Trigger a handshake from me to them via the relay")
558 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
559
560 p := r.RouteForAllUntilTxTun(theirControl)
561 r.Log("Assert the tunnel works")
562 assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
563 r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
564 }
565
566 func TestRelaysDontCareAboutIps(t *testing.T) {
567 t.Parallel()
568 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
569 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
570 relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "2001::9999/24", m{"relay": m{"am_relay": true}})
571 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
572
573 // Teach my how to get to the relay and that their can be reached via the relay
574 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
575 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
576 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
577
578 // Build a router so we don't have to reason who gets which packet
579 r := router.NewR(t, myControl, relayControl, theirControl)
580 defer r.RenderFlow()
581
582 // Start the servers
583 myControl.Start()
584 relayControl.Start()
585 theirControl.Start()
586
587 t.Log("Trigger a handshake from me to them via the relay")
588 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
589
590 p := r.RouteForAllUntilTxTun(theirControl)
591 r.Log("Assert the tunnel works")
592 assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
593 r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
594 }
595
596 func TestReestablishRelays(t *testing.T) {
597 t.Parallel()
598 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
599 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
600 relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
601 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
602
603 // Teach my how to get to the relay and that their can be reached via the relay
604 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
605 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
606 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
607
608 // Build a router so we don't have to reason who gets which packet
609 r := router.NewR(t, myControl, relayControl, theirControl)
610 defer r.RenderFlow()
611
612 // Start the servers
613 myControl.Start()
614 relayControl.Start()
615 theirControl.Start()
616
617 t.Log("Trigger a handshake from me to them via the relay")
618 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
619
620 p := r.RouteForAllUntilTxTun(theirControl)
621 r.Log("Assert the tunnel works")
622 assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
623
624 t.Log("Ensure packet traversal from them to me via the relay")
625 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
626
627 p = r.RouteForAllUntilTxTun(myControl)
628 r.Log("Assert the tunnel works")
629 assertUdpPacket(t, []byte("Hi from them"), p, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
630
631 // If we break the relay's connection to 'them', 'me' needs to detect and recover the connection
632 r.Log("Close the tunnel")
633 relayControl.CloseTunnel(theirVpnIpNet[0].Addr(), true)
634
635 start := myControl.GetHostmapIndexCount()
636 curIndexes := myControl.GetHostmapIndexCount()
637 for curIndexes >= start {
638 curIndexes = myControl.GetHostmapIndexCount()
639 r.Logf("Wait for the dead index to go away:start=%v indexes, current=%v indexes", start, curIndexes)
640 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me should fail")))
641
642 r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
643 return router.RouteAndExit
644 })
645 time.Sleep(2 * time.Second)
646 }
647 r.Log("Dead index went away. Woot!")
648 r.RenderHostmaps("Me removed hostinfo", myControl, relayControl, theirControl)
649 // Next packet should re-establish a relayed connection and work just great.
650
651 t.Logf("Assert the tunnel...")
652 for {
653 t.Log("RouteForAllUntilTxTun")
654 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
655 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
656 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
657 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
658
659 p = r.RouteForAllUntilTxTun(theirControl)
660 r.Log("Assert the tunnel works")
661 packet := gopacket.NewPacket(p, layers.LayerTypeIPv4, gopacket.Lazy)
662 v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
663 if slices.Compare(v4.SrcIP, myVpnIpNet[0].Addr().AsSlice()) != 0 {
664 t.Logf("SrcIP is unexpected...this is not the packet I'm looking for. Keep looking")
665 continue
666 }
667 if slices.Compare(v4.DstIP, theirVpnIpNet[0].Addr().AsSlice()) != 0 {
668 t.Logf("DstIP is unexpected...this is not the packet I'm looking for. Keep looking")
669 continue
670 }
671
672 udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
673 if udp == nil {
674 t.Log("Not a UDP packet. This is not the packet I'm looking for. Keep looking")
675 continue
676 }
677 data := packet.ApplicationLayer()
678 if data == nil {
679 t.Log("No data found in packet. This is not the packet I'm looking for. Keep looking.")
680 continue
681 }
682 if string(data.Payload()) != "Hi from me" {
683 t.Logf("Unexpected payload: '%v', keep looking", string(data.Payload()))
684 continue
685 }
686 t.Log("I found my lost packet. I am so happy.")
687 break
688 }
689 t.Log("Assert the tunnel works the other way, too")
690 for {
691 t.Log("RouteForAllUntilTxTun")
692 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
693
694 p = r.RouteForAllUntilTxTun(myControl)
695 r.Log("Assert the tunnel works")
696 packet := gopacket.NewPacket(p, layers.LayerTypeIPv4, gopacket.Lazy)
697 v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
698 if slices.Compare(v4.DstIP, myVpnIpNet[0].Addr().AsSlice()) != 0 {
699 t.Logf("Dst is unexpected...this is not the packet I'm looking for. Keep looking")
700 continue
701 }
702 if slices.Compare(v4.SrcIP, theirVpnIpNet[0].Addr().AsSlice()) != 0 {
703 t.Logf("SrcIP is unexpected...this is not the packet I'm looking for. Keep looking")
704 continue
705 }
706
707 udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
708 if udp == nil {
709 t.Log("Not a UDP packet. This is not the packet I'm looking for. Keep looking")
710 continue
711 }
712 data := packet.ApplicationLayer()
713 if data == nil {
714 t.Log("No data found in packet. This is not the packet I'm looking for. Keep looking.")
715 continue
716 }
717 if string(data.Payload()) != "Hi from them" {
718 t.Logf("Unexpected payload: '%v', keep looking", string(data.Payload()))
719 continue
720 }
721 t.Log("I found my lost packet. I am so happy.")
722 break
723 }
724 r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
725
726 }
727
728 func TestRelayHandshakeOverDisestablishedEntry(t *testing.T) {
729 t.Parallel()
730 // If them tears down the tunnel while me keeps Established relay state, me's next
731 // handshake flows through the relay with no fresh CreateRelayRequest and lands on
732 // them's Disestablished terminal relay entry. them must re-establish that entry, or
733 // its first transmit deletes its only relay and the tunnel is born transmit-dead:
734 // them can receive but every send is silently dropped.
735 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
736 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
737 relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
738 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
739
740 // Teach my how to get to the relay and that their can be reached via the relay
741 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
742 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
743 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
744
745 // Build a router so we don't have to reason who gets which packet
746 r := router.NewR(t, myControl, relayControl, theirControl)
747 defer r.RenderFlow()
748
749 // Start the servers
750 myControl.Start()
751 relayControl.Start()
752 theirControl.Start()
753
754 t.Log("Trigger a handshake from me to them via the relay")
755 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
756
757 p := r.RouteForAllUntilTxTun(theirControl)
758 assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
759 oldIdx := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false).LocalIndex
760
761 t.Log("Close the tunnel on them only, marking their relay entry Disestablished")
762 theirControl.CloseTunnel(myVpnIpNet[0].Addr(), true)
763
764 t.Log("Re-handshake from me, riding the still-Established relay state")
765 myControl.ReHandshake(theirVpnIpNet[0].Addr())
766 for {
767 h := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
768 if h != nil && h.LocalIndex != oldIdx && h.RemoteIndex != 0 {
769 break
770 }
771 r.RouteForAllExitFunc(func(*udp.Packet, *nebula.Control) router.ExitType {
772 return router.RouteAndExit
773 })
774 }
775
776 hAtThem := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
777 require.NotNil(t, hAtThem, "them should have completed the relayed handshake")
778 require.Equal(t, []netip.Addr{relayVpnIpNet[0].Addr()}, hAtThem.CurrentRelaysToMe, "them should know a relay for the new tunnel")
779
780 t.Log("Send from them to me; their only relay entry must survive the transmit")
781 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
782 require.Never(t, func() bool {
783 h := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
784 return h == nil || len(h.CurrentRelaysToMe) == 0
785 }, time.Second, 10*time.Millisecond, "them deleted its only relay entry; the tunnel is permanently transmit-dead")
786
787 p = r.RouteForAllUntilTxTun(myControl)
788 assertUdpPacket(t, []byte("Hi from them"), p, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
789 r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
790 }
791
792 func TestStage1RaceRelays(t *testing.T) {
793 t.Parallel()
794 //NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
795 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
796 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
797 relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
798 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
799
800 // Teach my how to get to the relay and that their can be reached via the relay
801 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
802 theirControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
803
804 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
805 theirControl.InjectRelays(myVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
806
807 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
808 relayControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
809
810 // Build a router so we don't have to reason who gets which packet
811 r := router.NewR(t, myControl, relayControl, theirControl)
812 defer r.RenderFlow()
813
814 // Start the servers
815 myControl.Start()
816 relayControl.Start()
817 theirControl.Start()
818
819 r.Log("Get a tunnel between me and relay")
820 assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
821
822 r.Log("Get a tunnel between them and relay")
823 assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
824
825 r.Log("Trigger a handshake from both them and me via relay to them and me")
826 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
827 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
828
829 r.Log("Wait for a packet from them to me")
830 p := r.RouteForAllUntilTxTun(myControl)
831 _ = p
832
833 r.FlushAll()
834
835 myControl.Stop()
836 theirControl.Stop()
837 relayControl.Stop()
838 }
839
840 func TestStage1RaceRelays2(t *testing.T) {
841 t.Parallel()
842 //NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
843 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
844 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
845 relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
846 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
847
848 // Teach my how to get to the relay and that their can be reached via the relay
849 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
850 theirControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
851
852 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
853 theirControl.InjectRelays(myVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
854
855 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
856 relayControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
857
858 // Build a router so we don't have to reason who gets which packet
859 r := router.NewR(t, myControl, relayControl, theirControl)
860 defer r.RenderFlow()
861
862 // Start the servers
863 myControl.Start()
864 relayControl.Start()
865 theirControl.Start()
866
867 r.Log("Get a tunnel between me and relay")
868 assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
869
870 r.Log("Get a tunnel between them and relay")
871 assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
872
873 r.Log("Trigger a handshake from both them and me via relay to them and me")
874 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
875 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
876
877 //r.RouteUntilAfterMsgType(myControl, header.Control, header.MessageNone)
878 //r.RouteUntilAfterMsgType(theirControl, header.Control, header.MessageNone)
879
880 r.Log("Wait for a packet from them to me; myControl")
881 r.RouteForAllUntilTxTun(myControl)
882 r.Log("Wait for a packet from them to me; theirControl")
883 r.RouteForAllUntilTxTun(theirControl)
884
885 r.Log("Assert the tunnel works")
886 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
887
888 t.Log("Wait until we remove extra tunnels")
889 t.Logf("Waiting for hostinfos to be removed... myControl=%d theirControl=%d relayControl=%d",
890 myControl.GetHostmapIndexCount(),
891 theirControl.GetHostmapIndexCount(),
892 relayControl.GetHostmapIndexCount(),
893 )
894 hostInfos := myControl.GetHostmapIndexCount() + theirControl.GetHostmapIndexCount() + relayControl.GetHostmapIndexCount()
895 retries := 60
896 for hostInfos > 6 && retries > 0 {
897 hostInfos = myControl.GetHostmapIndexCount() + theirControl.GetHostmapIndexCount() + relayControl.GetHostmapIndexCount()
898 t.Logf("Waiting for hostinfos to be removed... myControl=%d theirControl=%d relayControl=%d",
899 myControl.GetHostmapIndexCount(),
900 theirControl.GetHostmapIndexCount(),
901 relayControl.GetHostmapIndexCount(),
902 )
903 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
904 t.Log("Connection manager hasn't ticked yet")
905 time.Sleep(time.Second)
906 retries--
907 }
908
909 r.Log("Assert the tunnel works")
910 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
911
912 myControl.Stop()
913 theirControl.Stop()
914 relayControl.Stop()
915 }
916
917 func TestRehandshakingRelays(t *testing.T) {
918 t.Parallel()
919 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
920 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
921 relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
922 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
923
924 // Teach my how to get to the relay and that their can be reached via the relay
925 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
926 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
927 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
928
929 // Build a router so we don't have to reason who gets which packet
930 r := router.NewR(t, myControl, relayControl, theirControl)
931 defer r.RenderFlow()
932
933 // Start the servers
934 myControl.Start()
935 relayControl.Start()
936 theirControl.Start()
937
938 t.Log("Trigger a handshake from me to them via the relay")
939 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
940
941 p := r.RouteForAllUntilTxTun(theirControl)
942 r.Log("Assert the tunnel works")
943 assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
944 r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
945
946 // When I update the certificate for the relay, both me and them will have 2 host infos for the relay,
947 // and the main host infos will not have any relay state to handle the me<->relay<->them tunnel.
948 r.Log("Renew relay certificate and spin until me and them sees it")
949 _, _, myNextPrivKey, myNextPEM := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "relay", time.Now(), time.Now().Add(5*time.Minute), relayVpnIpNet, nil, []string{"new group"})
950
951 caB, err := ca.MarshalPEM()
952 if err != nil {
953 panic(err)
954 }
955
956 relayConfig.Settings["pki"] = m{
957 "ca": string(caB),
958 "cert": string(myNextPEM),
959 "key": string(myNextPrivKey),
960 }
961 rc, err := yaml.Marshal(relayConfig.Settings)
962 require.NoError(t, err)
963 relayConfig.ReloadConfigString(string(rc))
964
965 for {
966 r.Log("Assert the tunnel works between myVpnIpNet and relayVpnIpNet")
967 assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
968 c := myControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
969 if len(c.Cert.Groups()) != 0 {
970 // We have a new certificate now
971 r.Log("Certificate between my and relay is updated!")
972 break
973 }
974
975 time.Sleep(time.Second)
976 }
977
978 for {
979 r.Log("Assert the tunnel works between theirVpnIpNet and relayVpnIpNet")
980 assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
981 c := theirControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
982 if len(c.Cert.Groups()) != 0 {
983 // We have a new certificate now
984 r.Log("Certificate between their and relay is updated!")
985 break
986 }
987
988 time.Sleep(time.Second)
989 }
990
991 r.Log("Assert the relay tunnel still works")
992 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
993 r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
994 // We should have two hostinfos on all sides
995 for myControl.GetHostmapIndexCount() != 2 {
996 t.Logf("Waiting for myControl hostinfos (%v != 2) to get cleaned up from lack of use...", myControl.GetHostmapIndexCount())
997 r.Log("Assert the relay tunnel still works")
998 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
999 r.Log("yupitdoes")
1000 time.Sleep(time.Second)
1001 }
1002 t.Logf("myControl hostinfos got cleaned up!")
1003 for theirControl.GetHostmapIndexCount() != 2 {
1004 t.Logf("Waiting for theirControl hostinfos (%v != 2) to get cleaned up from lack of use...", theirControl.GetHostmapIndexCount())
1005 r.Log("Assert the relay tunnel still works")
1006 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
1007 r.Log("yupitdoes")
1008 time.Sleep(time.Second)
1009 }
1010 t.Logf("theirControl hostinfos got cleaned up!")
1011 for relayControl.GetHostmapIndexCount() != 2 {
1012 t.Logf("Waiting for relayControl hostinfos (%v != 2) to get cleaned up from lack of use...", relayControl.GetHostmapIndexCount())
1013 r.Log("Assert the relay tunnel still works")
1014 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
1015 r.Log("yupitdoes")
1016 time.Sleep(time.Second)
1017 }
1018 t.Logf("relayControl hostinfos got cleaned up!")
1019 }
1020
1021 func TestRehandshakingRelaysPrimary(t *testing.T) {
1022 t.Parallel()
1023 // This test is the same as TestRehandshakingRelays but one of the terminal types is a primary swap winner
1024 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
1025 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.128/24", m{"relay": m{"use_relays": true}})
1026 relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.1/24", m{"relay": m{"am_relay": true}})
1027 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
1028
1029 // Teach my how to get to the relay and that their can be reached via the relay
1030 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
1031 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
1032 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
1033
1034 // Build a router so we don't have to reason who gets which packet
1035 r := router.NewR(t, myControl, relayControl, theirControl)
1036 defer r.RenderFlow()
1037
1038 // Start the servers
1039 myControl.Start()
1040 relayControl.Start()
1041 theirControl.Start()
1042
1043 t.Log("Trigger a handshake from me to them via the relay")
1044 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
1045
1046 p := r.RouteForAllUntilTxTun(theirControl)
1047 r.Log("Assert the tunnel works")
1048 assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
1049 r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
1050
1051 // When I update the certificate for the relay, both me and them will have 2 host infos for the relay,
1052 // and the main host infos will not have any relay state to handle the me<->relay<->them tunnel.
1053 r.Log("Renew relay certificate and spin until me and them sees it")
1054 _, _, myNextPrivKey, myNextPEM := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "relay", time.Now(), time.Now().Add(5*time.Minute), relayVpnIpNet, nil, []string{"new group"})
1055
1056 caB, err := ca.MarshalPEM()
1057 if err != nil {
1058 panic(err)
1059 }
1060
1061 relayConfig.Settings["pki"] = m{
1062 "ca": string(caB),
1063 "cert": string(myNextPEM),
1064 "key": string(myNextPrivKey),
1065 }
1066 rc, err := yaml.Marshal(relayConfig.Settings)
1067 require.NoError(t, err)
1068 relayConfig.ReloadConfigString(string(rc))
1069
1070 for {
1071 r.Log("Assert the tunnel works between myVpnIpNet and relayVpnIpNet")
1072 assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
1073 c := myControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
1074 if len(c.Cert.Groups()) != 0 {
1075 // We have a new certificate now
1076 r.Log("Certificate between my and relay is updated!")
1077 break
1078 }
1079
1080 time.Sleep(time.Second)
1081 }
1082
1083 for {
1084 r.Log("Assert the tunnel works between theirVpnIpNet and relayVpnIpNet")
1085 assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
1086 c := theirControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
1087 if len(c.Cert.Groups()) != 0 {
1088 // We have a new certificate now
1089 r.Log("Certificate between their and relay is updated!")
1090 break
1091 }
1092
1093 time.Sleep(time.Second)
1094 }
1095
1096 r.Log("Assert the relay tunnel still works")
1097 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
1098 r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
1099 // We should have two hostinfos on all sides
1100 for myControl.GetHostmapIndexCount() != 2 {
1101 t.Logf("Waiting for myControl hostinfos (%v != 2) to get cleaned up from lack of use...", myControl.GetHostmapIndexCount())
1102 r.Log("Assert the relay tunnel still works")
1103 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
1104 r.Log("yupitdoes")
1105 time.Sleep(time.Second)
1106 }
1107 t.Logf("myControl hostinfos got cleaned up!")
1108 for theirControl.GetHostmapIndexCount() != 2 {
1109 t.Logf("Waiting for theirControl hostinfos (%v != 2) to get cleaned up from lack of use...", theirControl.GetHostmapIndexCount())
1110 r.Log("Assert the relay tunnel still works")
1111 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
1112 r.Log("yupitdoes")
1113 time.Sleep(time.Second)
1114 }
1115 t.Logf("theirControl hostinfos got cleaned up!")
1116 for relayControl.GetHostmapIndexCount() != 2 {
1117 t.Logf("Waiting for relayControl hostinfos (%v != 2) to get cleaned up from lack of use...", relayControl.GetHostmapIndexCount())
1118 r.Log("Assert the relay tunnel still works")
1119 assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
1120 r.Log("yupitdoes")
1121 time.Sleep(time.Second)
1122 }
1123 t.Logf("relayControl hostinfos got cleaned up!")
1124 }
1125
1126 func TestRehandshaking(t *testing.T) {
1127 t.Parallel()
1128 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
1129 myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.2/24", nil)
1130 theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.1/24", nil)
1131
1132 // Put their info in our lighthouse and vice versa
1133 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
1134 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
1135
1136 // Build a router so we don't have to reason who gets which packet
1137 r := router.NewR(t, myControl, theirControl)
1138 defer r.RenderFlow()
1139
1140 // Start the servers
1141 myControl.Start()
1142 theirControl.Start()
1143
1144 t.Log("Stand up a tunnel between me and them")
1145 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1146
1147 r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
1148
1149 r.Log("Renew my certificate and spin until their sees it")
1150 _, _, myNextPrivKey, myNextPEM := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), myVpnIpNet, nil, []string{"new group"})
1151
1152 caB, err := ca.MarshalPEM()
1153 if err != nil {
1154 panic(err)
1155 }
1156
1157 myConfig.Settings["pki"] = m{
1158 "ca": string(caB),
1159 "cert": string(myNextPEM),
1160 "key": string(myNextPrivKey),
1161 }
1162 rc, err := yaml.Marshal(myConfig.Settings)
1163 require.NoError(t, err)
1164 myConfig.ReloadConfigString(string(rc))
1165
1166 for {
1167 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1168 c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
1169 if len(c.Cert.Groups()) != 0 {
1170 // We have a new certificate now
1171 break
1172 }
1173
1174 time.Sleep(time.Second)
1175 }
1176
1177 r.Log("Got the new cert")
1178 // Flip their firewall to only allowing the new group to catch the tunnels reverting incorrectly
1179 rc, err = yaml.Marshal(theirConfig.Settings)
1180 require.NoError(t, err)
1181 var theirNewConfig m
1182 require.NoError(t, yaml.Unmarshal(rc, &theirNewConfig))
1183 theirFirewall := theirNewConfig["firewall"].(map[string]any)
1184 theirFirewall["inbound"] = []m{{
1185 "proto": "any",
1186 "port": "any",
1187 "group": "new group",
1188 }}
1189 rc, err = yaml.Marshal(theirNewConfig)
1190 require.NoError(t, err)
1191 theirConfig.ReloadConfigString(string(rc))
1192
1193 r.Log("Spin until there is only 1 tunnel")
1194 for myControl.GetHostmapIndexCount()+theirControl.GetHostmapIndexCount() > 2 {
1195 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1196 t.Log("Connection manager hasn't ticked yet")
1197 time.Sleep(time.Second)
1198 }
1199
1200 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1201 myFinalHostmapHosts := myControl.ListHostmapHosts(false)
1202 myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
1203 theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
1204 theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
1205
1206 // Make sure the correct tunnel won
1207 c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
1208 assert.Contains(t, c.Cert.Groups(), "new group")
1209
1210 // We should only have a single tunnel now on both sides
1211 assert.Len(t, myFinalHostmapHosts, 1)
1212 assert.Len(t, theirFinalHostmapHosts, 1)
1213 assert.Len(t, myFinalHostmapIndexes, 1)
1214 assert.Len(t, theirFinalHostmapIndexes, 1)
1215
1216 r.RenderHostmaps("Final hostmaps", myControl, theirControl)
1217
1218 myControl.Stop()
1219 theirControl.Stop()
1220 }
1221
1222 func TestRehandshakingLoser(t *testing.T) {
1223 t.Parallel()
1224 // The purpose of this test is that the race loser renews their certificate and rehandshakes. The final tunnel
1225 // Should be the one with the new certificate
1226 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
1227 myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.2/24", nil)
1228 theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.1/24", nil)
1229
1230 // Put their info in our lighthouse and vice versa
1231 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
1232 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
1233
1234 // Build a router so we don't have to reason who gets which packet
1235 r := router.NewR(t, myControl, theirControl)
1236 defer r.RenderFlow()
1237
1238 // Start the servers
1239 myControl.Start()
1240 theirControl.Start()
1241
1242 t.Log("Stand up a tunnel between me and them")
1243 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1244
1245 myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
1246 theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
1247
1248 r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
1249
1250 r.Log("Renew their certificate and spin until mine sees it")
1251 _, _, theirNextPrivKey, theirNextPEM := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), theirVpnIpNet, nil, []string{"their new group"})
1252
1253 caB, err := ca.MarshalPEM()
1254 if err != nil {
1255 panic(err)
1256 }
1257
1258 theirConfig.Settings["pki"] = m{
1259 "ca": string(caB),
1260 "cert": string(theirNextPEM),
1261 "key": string(theirNextPrivKey),
1262 }
1263 rc, err := yaml.Marshal(theirConfig.Settings)
1264 require.NoError(t, err)
1265 theirConfig.ReloadConfigString(string(rc))
1266
1267 for {
1268 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1269 theirCertInMe := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
1270
1271 if slices.Contains(theirCertInMe.Cert.Groups(), "their new group") {
1272 break
1273 }
1274
1275 time.Sleep(time.Second)
1276 }
1277
1278 // Flip my firewall to only allowing the new group to catch the tunnels reverting incorrectly
1279 rc, err = yaml.Marshal(myConfig.Settings)
1280 require.NoError(t, err)
1281 var myNewConfig m
1282 require.NoError(t, yaml.Unmarshal(rc, &myNewConfig))
1283 theirFirewall := myNewConfig["firewall"].(map[string]any)
1284 theirFirewall["inbound"] = []m{{
1285 "proto": "any",
1286 "port": "any",
1287 "group": "their new group",
1288 }}
1289 rc, err = yaml.Marshal(myNewConfig)
1290 require.NoError(t, err)
1291 myConfig.ReloadConfigString(string(rc))
1292
1293 r.Log("Spin until there is only 1 tunnel")
1294 for myControl.GetHostmapIndexCount()+theirControl.GetHostmapIndexCount() > 2 {
1295 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1296 t.Log("Connection manager hasn't ticked yet")
1297 time.Sleep(time.Second)
1298 }
1299
1300 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1301 myFinalHostmapHosts := myControl.ListHostmapHosts(false)
1302 myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
1303 theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
1304 theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
1305
1306 // Make sure the correct tunnel won
1307 theirCertInMe := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
1308 assert.Contains(t, theirCertInMe.Cert.Groups(), "their new group")
1309
1310 // We should only have a single tunnel now on both sides
1311 assert.Len(t, myFinalHostmapHosts, 1)
1312 assert.Len(t, theirFinalHostmapHosts, 1)
1313 assert.Len(t, myFinalHostmapIndexes, 1)
1314 assert.Len(t, theirFinalHostmapIndexes, 1)
1315
1316 r.RenderHostmaps("Final hostmaps", myControl, theirControl)
1317 myControl.Stop()
1318 theirControl.Stop()
1319 }
1320
1321 func TestRaceRegression(t *testing.T) {
1322 t.Parallel()
1323 // This test forces stage 1, stage 2, stage 1 to be received by me from them
1324 // We had a bug where we were not finding the duplicate handshake and responding to the final stage 1 which
1325 // caused a cross-linked hostinfo
1326 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
1327 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
1328 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
1329
1330 // Put their info in our lighthouse
1331 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
1332 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
1333
1334 // Start the servers
1335 myControl.Start()
1336 theirControl.Start()
1337
1338 //them rx stage:1 initiatorIndex=642843150 responderIndex=0
1339 //me rx stage:1 initiatorIndex=120607833 responderIndex=0
1340 //them rx stage:1 initiatorIndex=642843150 responderIndex=0
1341 //me rx stage:2 initiatorIndex=642843150 responderIndex=3701775874
1342 //me rx stage:1 initiatorIndex=120607833 responderIndex=0
1343 //them rx stage:2 initiatorIndex=120607833 responderIndex=4209862089
1344
1345 t.Log("Start both handshakes")
1346 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
1347 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
1348
1349 t.Log("Get both stage 1")
1350 myStage1ForThem := myControl.GetFromUDP(true)
1351 theirStage1ForMe := theirControl.GetFromUDP(true)
1352
1353 t.Log("Inject them in a special way")
1354 theirControl.InjectUDPPacket(myStage1ForThem)
1355 myControl.InjectUDPPacket(theirStage1ForMe)
1356 theirControl.InjectUDPPacket(myStage1ForThem)
1357
1358 t.Log("Get both stage 2")
1359 myStage2ForThem := myControl.GetFromUDP(true)
1360 theirStage2ForMe := theirControl.GetFromUDP(true)
1361
1362 t.Log("Inject them in a special way again")
1363 myControl.InjectUDPPacket(theirStage2ForMe)
1364 myControl.InjectUDPPacket(theirStage1ForMe)
1365 theirControl.InjectUDPPacket(myStage2ForThem)
1366
1367 r := router.NewR(t, myControl, theirControl)
1368 defer r.RenderFlow()
1369
1370 t.Log("Flush the packets")
1371 r.RouteForAllUntilTxTun(myControl)
1372 r.RouteForAllUntilTxTun(theirControl)
1373 r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
1374
1375 t.Log("Make sure the tunnel still works")
1376 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1377
1378 myControl.Stop()
1379 theirControl.Stop()
1380 }
1381
1382 func TestV2NonPrimaryWithLighthouse(t *testing.T) {
1383 t.Parallel()
1384 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
1385 lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "10.128.0.1/24, ff::1/64", m{"lighthouse": m{"am_lighthouse": true}})
1386
1387 o := m{
1388 "static_host_map": m{
1389 lhVpnIpNet[1].Addr().String(): []string{lhUdpAddr.String()},
1390 },
1391 "lighthouse": m{
1392 "hosts": []string{lhVpnIpNet[1].Addr().String()},
1393 "local_allow_list": m{
1394 // Try and block our lighthouse updates from using the actual addresses assigned to this computer
1395 // If we start discovering addresses the test router doesn't know about then test traffic cant flow
1396 "10.0.0.0/24": true,
1397 "::/0": false,
1398 },
1399 },
1400 }
1401 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.2/24, ff::2/64", o)
1402 theirControl, theirVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.3/24, ff::3/64", o)
1403
1404 // Build a router so we don't have to reason who gets which packet
1405 r := router.NewR(t, lhControl, myControl, theirControl)
1406 defer r.RenderFlow()
1407
1408 // Start the servers
1409 lhControl.Start()
1410 myControl.Start()
1411 theirControl.Start()
1412
1413 t.Log("Stand up an ipv6 tunnel between me and them")
1414 assert.True(t, myVpnIpNet[1].Addr().Is6())
1415 assert.True(t, theirVpnIpNet[1].Addr().Is6())
1416 assertTunnel(t, myVpnIpNet[1].Addr(), theirVpnIpNet[1].Addr(), myControl, theirControl, r)
1417
1418 lhControl.Stop()
1419 myControl.Stop()
1420 theirControl.Stop()
1421 }
1422
1423 func TestV2NonPrimaryWithOffNetLighthouse(t *testing.T) {
1424 t.Parallel()
1425 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
1426 lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "2001::1/64", m{"lighthouse": m{"am_lighthouse": true}})
1427
1428 o := m{
1429 "static_host_map": m{
1430 lhVpnIpNet[0].Addr().String(): []string{lhUdpAddr.String()},
1431 },
1432 "lighthouse": m{
1433 "hosts": []string{lhVpnIpNet[0].Addr().String()},
1434 "local_allow_list": m{
1435 // Try and block our lighthouse updates from using the actual addresses assigned to this computer
1436 // If we start discovering addresses the test router doesn't know about then test traffic cant flow
1437 "10.0.0.0/24": true,
1438 "::/0": false,
1439 },
1440 },
1441 }
1442 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.2/24, ff::2/64", o)
1443 theirControl, theirVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.3/24, ff::3/64", o)
1444
1445 // Build a router so we don't have to reason who gets which packet
1446 r := router.NewR(t, lhControl, myControl, theirControl)
1447 defer r.RenderFlow()
1448
1449 // Start the servers
1450 lhControl.Start()
1451 myControl.Start()
1452 theirControl.Start()
1453
1454 t.Log("Stand up an ipv6 tunnel between me and them")
1455 assert.True(t, myVpnIpNet[1].Addr().Is6())
1456 assert.True(t, theirVpnIpNet[1].Addr().Is6())
1457 assertTunnel(t, myVpnIpNet[1].Addr(), theirVpnIpNet[1].Addr(), myControl, theirControl, r)
1458
1459 lhControl.Stop()
1460 myControl.Stop()
1461 theirControl.Stop()
1462 }
1463
1464 func TestLighthouseUpdateOnReload(t *testing.T) {
1465 t.Parallel()
1466 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
1467
1468 // Create the lighthouse
1469 lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh", "10.128.0.1/24", m{"lighthouse": m{"am_lighthouse": true}})
1470
1471 // Create a client with NO lighthouse configured and a long update interval.
1472 // The initial SendUpdate at startup will be a no-op since no lighthouses are known.
1473 myControl, myVpnIpNet, _, myConfig := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.2/24", m{
1474 "lighthouse": m{
1475 "interval": 600,
1476 "local_allow_list": m{
1477 "10.0.0.0/24": true,
1478 "::/0": false,
1479 },
1480 },
1481 })
1482
1483 r := router.NewR(t, lhControl, myControl)
1484 defer r.RenderFlow()
1485
1486 lhControl.Start()
1487 myControl.Start()
1488
1489 // Drain any startup packets (there should be none meaningful)
1490 r.FlushAll()
1491
1492 // Verify lighthouse has no knowledge of the client
1493 assert.Nil(t, lhControl.QueryLighthouse(myVpnIpNet[0].Addr()))
1494
1495 // Build a new config that adds the lighthouse
1496 newSettings := make(m)
1497 for k, v := range myConfig.Settings {
1498 newSettings[k] = v
1499 }
1500 newSettings["static_host_map"] = m{
1501 lhVpnIpNet[0].Addr().String(): []any{lhUdpAddr.String()},
1502 }
1503 newSettings["lighthouse"] = m{
1504 "hosts": []any{lhVpnIpNet[0].Addr().String()},
1505 "interval": 600,
1506 "local_allow_list": m{
1507 "10.0.0.0/24": true,
1508 "::/0": false,
1509 },
1510 }
1511 newCfg, err := yaml.Marshal(newSettings)
1512 require.NoError(t, err)
1513
1514 // Reload the config. The lighthouse.hosts change triggers TriggerUpdate,
1515 // which wakes the update worker. It calls SendUpdate, initiating a
1516 // handshake to the new lighthouse and caching the HostUpdateNotification.
1517 require.NoError(t, myConfig.ReloadConfigString(string(newCfg)))
1518
1519 // Route until the lighthouse receives the HostUpdateNotification.
1520 // This covers: handshake stage 1, stage 2, then the cached update.
1521 done := make(chan struct{})
1522 go func() {
1523 r.RouteForAllUntilAfterMsgTypeTo(lhControl, header.LightHouse, 0)
1524 close(done)
1525 }()
1526 select {
1527 case <-done:
1528 case <-time.After(5 * time.Second):
1529 t.Fatal("timed out waiting for lighthouse update after config reload")
1530 }
1531
1532 // Verify lighthouse now has the client's addresses
1533 assert.NotNil(t, lhControl.QueryLighthouse(myVpnIpNet[0].Addr()))
1534
1535 r.RenderHostmaps("Final hostmaps", lhControl, myControl)
1536 lhControl.Stop()
1537 myControl.Stop()
1538 }
1539
1540 func TestGoodHandshakeUnsafeDest(t *testing.T) {
1541 t.Parallel()
1542 unsafePrefix := "192.168.6.0/24"
1543 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
1544 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServerWithUdpAndUnsafeNetworks(cert.Version2, ca, caKey, "spooky", "10.128.0.2/24", netip.MustParseAddrPort("10.64.0.2:4242"), unsafePrefix, nil)
1545 route := m{"route": unsafePrefix, "via": theirVpnIpNet[0].Addr().String()}
1546 myCfg := m{
1547 "tun": m{
1548 "unsafe_routes": []m{route},
1549 },
1550 }
1551 myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.1/24", myCfg)
1552 t.Logf("my config %v", myConfig)
1553 // Put their info in our lighthouse
1554 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
1555
1556 spookyDest := netip.MustParseAddr("192.168.6.4")
1557
1558 // Start the servers
1559 myControl.Start()
1560 theirControl.Start()
1561
1562 t.Log("Send a udp packet through to begin standing up the tunnel, this should come out the other side")
1563 myControl.InjectTunPacket(BuildTunUDPPacket(spookyDest, 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
1564
1565 t.Log("Have them consume my stage 0 packet. They have a tunnel now")
1566 theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
1567
1568 t.Log("Get their stage 1 packet so that we can play with it")
1569 stage1Packet := theirControl.GetFromUDP(true)
1570
1571 t.Log("I consume a garbage packet with a proper nebula header for our tunnel")
1572 // this should log a statement and get ignored, allowing the real handshake packet to complete the tunnel
1573 badPacket := stage1Packet.Copy()
1574 badPacket.Data = badPacket.Data[:len(badPacket.Data)-header.Len]
1575 myControl.InjectUDPPacket(badPacket)
1576
1577 t.Log("Have me consume their real stage 1 packet. I have a tunnel now")
1578 myControl.InjectUDPPacket(stage1Packet)
1579
1580 t.Log("Wait until we see my cached packet come through")
1581 myControl.WaitForType(1, 0, theirControl)
1582
1583 t.Log("Make sure our host infos are correct")
1584 assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
1585
1586 t.Log("Get that cached packet and make sure it looks right")
1587 myCachedPacket := theirControl.GetFromTun(true)
1588 assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), spookyDest, 80, 80)
1589
1590 //reply
1591 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, spookyDest, 80, []byte("Hi from the spookyman")))
1592 //wait for reply
1593 theirControl.WaitForType(1, 0, myControl)
1594 theirCachedPacket := myControl.GetFromTun(true)
1595 assertUdpPacket(t, []byte("Hi from the spookyman"), theirCachedPacket, spookyDest, myVpnIpNet[0].Addr(), 80, 80)
1596
1597 t.Log("Do a bidirectional tunnel test")
1598 r := router.NewR(t, myControl, theirControl)
1599 defer r.RenderFlow()
1600 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
1601
1602 r.RenderHostmaps("Final hostmaps", myControl, theirControl)
1603 myControl.Stop()
1604 theirControl.Stop()
1605 }
1606
1607 func TestMultiVpnAddrDeletePrimaryKeepsSecondAddr(t *testing.T) {
1608 t.Parallel()
1609 // Regression for the hostmap multi-vpnAddr delete bug. A dual-stack (v4+v6) V2-cert peer that
1610 // handshakes twice at once ends up with two hostinfos linked in the shared next/prev chain, with the
1611 // primary owning both addresses. Deleting that primary (e.g. connection manager dropping it, a
1612 // CloseTunnel, a collision) must promote the surviving sibling for EVERY address. The pre-fix code
1613 // unlinked the chain once per address, so it promoted the sibling for the first address and orphaned
1614 // the second: the peer stayed reachable at its v4 addr but not its v6 addr despite a live tunnel.
1615
1616 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
1617 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24,fd00::1/64", nil)
1618 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.2/24,fd00::2/64", nil)
1619
1620 // This bug only exists for peers carrying more than one vpn address
1621 require.Len(t, theirVpnIpNet, 2)
1622 theirV4 := theirVpnIpNet[0].Addr()
1623 theirV6 := theirVpnIpNet[1].Addr()
1624
1625 // Put their info in our lighthouse and vice versa
1626 myControl.InjectLightHouseAddr(theirV4, theirUdpAddr)
1627 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
1628
1629 // Build a router so we don't have to reason who gets which packet
1630 r := router.NewR(t, myControl, theirControl)
1631 defer r.RenderFlow()
1632
1633 myControl.Start()
1634 theirControl.Start()
1635
1636 // Race a handshake so both of us build a hostinfo for the other, leaving my hostmap with a single
1637 // host (them) backed by two linked hostinfos, just like TestStage1Race.
1638 myControl.InjectTunPacket(BuildTunUDPPacket(theirV4, 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
1639 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirV4, 80, []byte("Hi from them")))
1640
1641 myHsForThem := myControl.GetFromUDP(true)
1642 theirHsForMe := theirControl.GetFromUDP(true)
1643
1644 r.InjectUDPPacket(theirControl, myControl, theirHsForMe)
1645 r.InjectUDPPacket(myControl, theirControl, myHsForThem)
1646
1647 r.RouteForAllUntilTxTun(theirControl)
1648 r.RouteForAllUntilTxTun(myControl)
1649
1650 r.RenderHostmaps("Racing hostmaps", myControl, theirControl)
1651
1652 // Two hostinfos for them means the shared next/prev chain has a sibling to promote. The Hosts map has
1653 // one entry per vpn address (two, for dual stack), so the index count is what tells us there are two
1654 // hostinfos.
1655 require.Len(t, myControl.ListHostmapIndexes(false), 2)
1656
1657 // The primary owns both of their addresses
1658 primaryV4 := myControl.GetHostInfoByVpnAddr(theirV4, false)
1659 primaryV6 := myControl.GetHostInfoByVpnAddr(theirV6, false)
1660 require.NotNil(t, primaryV4)
1661 require.NotNil(t, primaryV6)
1662 require.Equal(t, primaryV4.LocalIndex, primaryV6.LocalIndex, "both addrs should point at the same primary")
1663
1664 // Delete the primary tunnel. localOnly so we don't perturb their side, we only care about my hostmap.
1665 require.True(t, myControl.CloseTunnel(theirV4, true))
1666
1667 // The surviving sibling must still serve BOTH addresses.
1668 survivorV4 := myControl.GetHostInfoByVpnAddr(theirV4, false)
1669 survivorV6 := myControl.GetHostInfoByVpnAddr(theirV6, false)
1670 require.NotNil(t, survivorV4, "v4 addr should still resolve to the surviving tunnel")
1671 // Pre-fix this is nil: the second address was orphaned when the primary was deleted.
1672 require.NotNil(t, survivorV6, "v6 addr was orphaned after deleting the primary (multi-vpnAddr delete bug)")
1673 assert.Equal(t, survivorV4.LocalIndex, survivorV6.LocalIndex, "both addrs should promote to the same survivor")
1674 assert.NotEqual(t, primaryV4.LocalIndex, survivorV4.LocalIndex, "a different hostinfo should now be primary")
1675
1676 r.RenderHostmaps("Final hostmaps", myControl, theirControl)
1677
1678 myControl.Stop()
1679 theirControl.Stop()
1680 }