Debian Codegraph @

e2e/handshake_manager_test.go

raw
1 //go:build e2e_testing
2 // +build e2e_testing
3
4 package e2e
5
6 import (
7 "net/netip"
8 "testing"
9 "time"
10
11 "github.com/slackhq/nebula"
12 "github.com/slackhq/nebula/cert"
13 "github.com/slackhq/nebula/cert_test"
14 "github.com/slackhq/nebula/e2e/router"
15 "github.com/slackhq/nebula/header"
16 "github.com/slackhq/nebula/udp"
17 "github.com/stretchr/testify/assert"
18 )
19
20 // makeHandshakePacket creates a handshake packet with the given parameters.
21 func makeHandshakePacket(from, to netip.AddrPort, subtype header.MessageSubType, remoteIndex uint32, counter uint64) *udp.Packet {
22 data := make([]byte, 200)
23 header.Encode(data, header.Version, header.Handshake, subtype, remoteIndex, counter)
24 for i := header.Len; i < len(data); i++ {
25 data[i] = byte(i)
26 }
27 return &udp.Packet{To: to, From: from, Data: data}
28 }
29
30 func TestHandshakeRetransmitDuplicate(t *testing.T) {
31 t.Parallel()
32 // Verify the responder correctly handles receiving the same msg1 multiple times
33 // (retransmission). The duplicate goes through CheckAndComplete -> ErrAlreadySeen
34 // and the cached response is resent.
35
36 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
37 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
38 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
39
40 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
41 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
42
43 myControl.Start()
44 theirControl.Start()
45
46 r := router.NewR(t, myControl, theirControl)
47 defer r.RenderFlow()
48
49 t.Log("Trigger handshake from me to them")
50 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi")))
51
52 t.Log("Grab my msg1")
53 msg1 := myControl.GetFromUDP(true)
54
55 t.Log("Inject msg1 into them, first time")
56 theirControl.InjectUDPPacket(msg1)
57 _ = theirControl.GetFromUDP(true)
58
59 t.Log("Inject the SAME msg1 again, tests ErrAlreadySeen path")
60 theirControl.InjectUDPPacket(msg1)
61 resp2 := theirControl.GetFromUDP(true)
62 assert.NotNil(t, resp2, "should get cached response on duplicate msg1")
63
64 t.Log("Complete handshake with cached response")
65 myControl.InjectUDPPacket(resp2)
66 myControl.WaitForType(1, 0, theirControl)
67
68 t.Log("Drain cached packet and verify tunnel works")
69 cachedPacket := theirControl.GetFromTun(true)
70 assertUdpPacket(t, []byte("Hi"), cachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
71 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
72
73 t.Log("Verify only one tunnel exists on each side")
74 assert.Len(t, myControl.ListHostmapHosts(false), 1)
75 assert.Len(t, theirControl.ListHostmapHosts(false), 1)
76
77 myControl.Stop()
78 theirControl.Stop()
79 }
80
81 func TestHandshakeTruncatedPacketRecovery(t *testing.T) {
82 t.Parallel()
83 // Verify that a truncated handshake packet is ignored and the real
84 // packet can still complete the handshake.
85
86 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
87 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
88 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
89
90 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
91 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
92
93 myControl.Start()
94 theirControl.Start()
95
96 r := router.NewR(t, myControl, theirControl)
97 defer r.RenderFlow()
98
99 t.Log("Trigger handshake")
100 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi")))
101
102 t.Log("Get msg1 and deliver to responder")
103 msg1 := myControl.GetFromUDP(true)
104 theirControl.InjectUDPPacket(msg1)
105
106 t.Log("Get the real response")
107 realResp := theirControl.GetFromUDP(true)
108
109 t.Log("Truncate the response and inject, should be ignored")
110 truncResp := realResp.Copy()
111 truncResp.Data = truncResp.Data[:header.Len]
112 myControl.InjectUDPPacket(truncResp)
113
114 t.Log("Verify pending handshake survived the truncated packet")
115 assert.NotEmpty(t, myControl.ListHostmapHosts(true), "pending handshake should still exist")
116
117 t.Log("Inject real response, should complete handshake")
118 myControl.InjectUDPPacket(realResp)
119 myControl.WaitForType(1, 0, theirControl)
120
121 t.Log("Drain and verify tunnel")
122 cachedPacket := theirControl.GetFromTun(true)
123 assertUdpPacket(t, []byte("Hi"), cachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
124 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
125
126 myControl.Stop()
127 theirControl.Stop()
128 }
129
130 func TestHandshakeOrphanedMsg2Dropped(t *testing.T) {
131 t.Parallel()
132 // A msg2 arriving with no matching pending index should be silently dropped
133 // with no response sent and no state changes.
134
135 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
136 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
137 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
138
139 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
140 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
141
142 myControl.Start()
143 theirControl.Start()
144
145 r := router.NewR(t, myControl, theirControl)
146 defer r.RenderFlow()
147
148 t.Log("Complete a normal handshake")
149 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi")))
150 r.RouteForAllUntilTxTun(theirControl)
151 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
152
153 t.Log("Record hostmap state")
154 myIndexes := len(myControl.ListHostmapIndexes(false))
155
156 t.Log("Inject a fake msg2 with unknown RemoteIndex")
157 myControl.InjectUDPPacket(makeHandshakePacket(theirUdpAddr, myUdpAddr, header.HandshakeIXPSK0, 0xDEADBEEF, 2))
158
159 t.Log("Verify no new indexes created")
160 assert.Equal(t, myIndexes, len(myControl.ListHostmapIndexes(false)))
161
162 t.Log("Verify no UDP response was sent")
163 time.Sleep(100 * time.Millisecond)
164 assert.Nil(t, myControl.GetFromUDP(false), "should not send a response to orphaned msg2")
165
166 t.Log("Verify existing tunnel still works")
167 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
168
169 myControl.Stop()
170 theirControl.Stop()
171 }
172
173 func TestHandshakeUnknownMessageCounter(t *testing.T) {
174 t.Parallel()
175 // A handshake packet with an unexpected message counter should be silently
176 // dropped with no side effects and no UDP response.
177
178 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
179 myControl, _, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
180 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
181
182 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
183
184 myControl.Start()
185 theirControl.Start()
186
187 t.Log("Inject handshake with MessageCounter=3")
188 myControl.InjectUDPPacket(makeHandshakePacket(theirUdpAddr, myUdpAddr, header.HandshakeIXPSK0, 0, 3))
189
190 t.Log("Inject handshake with MessageCounter=99")
191 myControl.InjectUDPPacket(makeHandshakePacket(theirUdpAddr, myUdpAddr, header.HandshakeIXPSK0, 0, 99))
192
193 t.Log("Verify no tunnels or pending handshakes")
194 assert.Empty(t, myControl.ListHostmapHosts(false))
195 assert.Empty(t, myControl.ListHostmapHosts(true))
196
197 t.Log("Verify no UDP response was sent")
198 time.Sleep(100 * time.Millisecond)
199 assert.Nil(t, myControl.GetFromUDP(false))
200
201 myControl.Stop()
202 theirControl.Stop()
203 }
204
205 func TestHandshakeUnknownSubtype(t *testing.T) {
206 t.Parallel()
207 // A handshake packet with an unknown subtype should be silently dropped.
208
209 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
210 myControl, _, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
211 theirControl, _, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
212
213 myControl.Start()
214 theirControl.Start()
215
216 t.Log("Inject handshake with unknown subtype 99")
217 myControl.InjectUDPPacket(makeHandshakePacket(theirUdpAddr, myUdpAddr, header.MessageSubType(99), 0, 1))
218
219 t.Log("Verify no tunnels or pending handshakes")
220 assert.Empty(t, myControl.ListHostmapHosts(false))
221 assert.Empty(t, myControl.ListHostmapHosts(true))
222
223 t.Log("Verify no UDP response was sent")
224 time.Sleep(100 * time.Millisecond)
225 assert.Nil(t, myControl.GetFromUDP(false))
226
227 myControl.Stop()
228 theirControl.Stop()
229 }
230
231 func TestHandshakeLateResponse(t *testing.T) {
232 t.Parallel()
233 // After a handshake times out, a late response should be silently ignored
234 // with no new tunnels created.
235
236 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
237 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", m{
238 "handshakes": m{
239 "try_interval": "200ms",
240 "retries": 2,
241 },
242 })
243 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
244
245 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
246
247 myControl.Start()
248 theirControl.Start()
249
250 t.Log("Trigger handshake from me")
251 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi")))
252
253 t.Log("Grab msg1 but don't deliver")
254 msg1 := myControl.GetFromUDP(true)
255
256 t.Log("Wait for handshake to time out")
257 for i := 0; i < 5; i++ {
258 time.Sleep(300 * time.Millisecond)
259 myControl.GetFromUDP(false)
260 }
261
262 t.Log("Confirm no pending handshakes remain")
263 assert.Empty(t, myControl.ListHostmapHosts(true))
264
265 t.Log("Deliver old msg1 to them, they create a tunnel")
266 theirControl.InjectUDPPacket(msg1)
267 resp := theirControl.GetFromUDP(true)
268 assert.NotNil(t, resp)
269
270 t.Log("Inject late response into me, should be ignored")
271 myControl.InjectUDPPacket(resp)
272
273 t.Log("No tunnel should exist on my side")
274 assert.Empty(t, myControl.ListHostmapHosts(false))
275 assert.Empty(t, myControl.ListHostmapHosts(true))
276
277 myControl.Stop()
278 theirControl.Stop()
279 }
280
281 func TestHandshakeSelfConnectionRejected(t *testing.T) {
282 t.Parallel()
283 // Verify that a node rejects a handshake containing its own VPN IP in the
284 // peer cert. We do this by sending the initiator's own msg1 back to itself.
285
286 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
287 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
288
289 // Need a lighthouse entry to trigger a handshake
290 myControl.InjectLightHouseAddr(netip.MustParseAddr("10.128.0.2"), netip.MustParseAddrPort("10.0.0.2:4242"))
291
292 myControl.Start()
293
294 t.Log("Trigger handshake from me")
295 myControl.InjectTunPacket(BuildTunUDPPacket(netip.MustParseAddr("10.128.0.2"), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi")))
296 msg1 := myControl.GetFromUDP(true)
297
298 t.Log("Drain any handshake retransmits before injecting")
299 time.Sleep(100 * time.Millisecond)
300 for myControl.GetFromUDP(false) != nil {
301 }
302
303 t.Log("Feed my own msg1 back to me as if it came from someone else")
304 selfMsg := msg1.Copy()
305 selfMsg.From = netip.MustParseAddrPort("10.0.0.99:4242")
306 selfMsg.To = myUdpAddr
307 myControl.InjectUDPPacket(selfMsg)
308
309 t.Log("Verify no response was sent (self-connection rejected)")
310 time.Sleep(100 * time.Millisecond)
311 // Drain any further retransmits from the original handshake, then check
312 // that none of them are a handshake response (MessageCounter=2)
313 h := &header.H{}
314 for {
315 p := myControl.GetFromUDP(false)
316 if p == nil {
317 break
318 }
319 _ = h.Parse(p.Data)
320 assert.NotEqual(t, uint64(2), h.MessageCounter,
321 "should not send a stage 2 response to self-connection")
322 }
323
324 t.Log("Verify no tunnel to myself was created")
325 assert.Nil(t, myControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false))
326
327 myControl.Stop()
328 }
329
330 func TestHandshakeMessageCounter0Dropped(t *testing.T) {
331 t.Parallel()
332 // MessageCounter=0 is not a valid handshake message and should be dropped.
333
334 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
335 myControl, _, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
336 _, _, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
337
338 myControl.Start()
339
340 t.Log("Inject handshake with MessageCounter=0")
341 myControl.InjectUDPPacket(makeHandshakePacket(theirUdpAddr, myUdpAddr, header.HandshakeIXPSK0, 0, 0))
342
343 time.Sleep(100 * time.Millisecond)
344 assert.Empty(t, myControl.ListHostmapHosts(false))
345 assert.Empty(t, myControl.ListHostmapHosts(true))
346 assert.Nil(t, myControl.GetFromUDP(false))
347
348 myControl.Stop()
349 }
350
351 func TestHandshakeRemoteAllowList(t *testing.T) {
352 t.Parallel()
353 // Verify that a handshake from a blocked underlay IP is dropped with no
354 // response and no state changes. Then verify the same packet from an
355 // allowed IP succeeds.
356
357 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
358 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", m{
359 "lighthouse": m{
360 "remote_allow_list": m{
361 "10.0.0.0/8": true,
362 "0.0.0.0/0": false,
363 },
364 },
365 })
366 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
367
368 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
369 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
370
371 myControl.Start()
372 theirControl.Start()
373
374 r := router.NewR(t, myControl, theirControl)
375 defer r.RenderFlow()
376
377 t.Log("Trigger handshake from them")
378 theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi")))
379 msg1 := theirControl.GetFromUDP(true)
380
381 t.Log("Rewrite the source to a blocked IP and inject")
382 blockedMsg := msg1.Copy()
383 blockedMsg.From = netip.MustParseAddrPort("192.168.1.1:4242")
384 myControl.InjectUDPPacket(blockedMsg)
385
386 t.Log("Verify no tunnel, no pending, no response from blocked source")
387 time.Sleep(100 * time.Millisecond)
388 assert.Empty(t, myControl.ListHostmapHosts(false))
389 assert.Empty(t, myControl.ListHostmapHosts(true))
390 assert.Nil(t, myControl.GetFromUDP(false), "should not respond to blocked source")
391
392 t.Log("Now inject the real packet from the allowed source")
393 myControl.InjectUDPPacket(msg1)
394
395 t.Log("Verify handshake completes from allowed source")
396 resp := myControl.GetFromUDP(true)
397 assert.NotNil(t, resp)
398 theirControl.InjectUDPPacket(resp)
399 theirControl.WaitForType(1, 0, myControl)
400
401 t.Log("Drain cached packet and verify tunnel works")
402 cachedPacket := myControl.GetFromTun(true)
403 assertUdpPacket(t, []byte("Hi"), cachedPacket, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
404 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
405
406 myControl.Stop()
407 theirControl.Stop()
408 }
409
410 func TestHandshakeAlreadySeenPreferredRemote(t *testing.T) {
411 t.Parallel()
412 // When a duplicate msg1 arrives via ErrAlreadySeen, verify the tunnel
413 // remains functional and hostmap index count is stable.
414
415 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
416 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
417 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
418
419 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
420 theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
421
422 myControl.Start()
423 theirControl.Start()
424
425 r := router.NewR(t, myControl, theirControl)
426 defer r.RenderFlow()
427
428 t.Log("Complete a normal handshake via the router")
429 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi")))
430 r.RouteForAllUntilTxTun(theirControl)
431 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
432
433 t.Log("Record hostmap state")
434 theirIndexes := len(theirControl.ListHostmapIndexes(false))
435 hi := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
436 assert.NotNil(t, hi)
437 originalRemote := hi.CurrentRemote
438
439 t.Log("Re-trigger traffic to cause a new handshake attempt (ErrAlreadySeen)")
440 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("roam")))
441 r.RouteForAllUntilTxTun(theirControl)
442
443 t.Log("Verify tunnel still works")
444 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
445
446 t.Log("Verify remote is still valid and index count is stable")
447 hi2 := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
448 assert.NotNil(t, hi2)
449 assert.Equal(t, originalRemote, hi2.CurrentRemote)
450 assert.Equal(t, theirIndexes, len(theirControl.ListHostmapIndexes(false)),
451 "no extra indexes should be created from ErrAlreadySeen")
452
453 myControl.Stop()
454 theirControl.Stop()
455 }
456
457 func TestHandshakeWrongResponderPacketStore(t *testing.T) {
458 t.Parallel()
459 // Verify that when the wrong host responds, the cached packets are
460 // transferred to the new handshake, the evil tunnel is closed, evil's
461 // address is blocked, and the correct tunnel is eventually established.
462
463 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
464 myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.100/24", nil)
465 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.99/24", nil)
466 evilControl, evilVpnIpNet, evilUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "evil", "10.128.0.2/24", nil)
467
468 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), evilUdpAddr)
469
470 r := router.NewR(t, myControl, theirControl, evilControl)
471 defer r.RenderFlow()
472
473 myControl.Start()
474 theirControl.Start()
475 evilControl.Start()
476
477 t.Log("Send multiple packets to them (cached during handshake)")
478 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("packet1")))
479 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("packet2")))
480
481 t.Log("Route until evil tunnel is closed")
482 h := &header.H{}
483 r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
484 if err := h.Parse(p.Data); err != nil {
485 panic(err)
486 }
487 if h.Type == header.CloseTunnel && p.To == evilUdpAddr {
488 return router.RouteAndExit
489 }
490 return router.KeepRouting
491 })
492
493 t.Log("Verify evil's address is blocked in the new pending handshake")
494 pendingHI := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), true)
495 if pendingHI != nil {
496 assert.NotContains(t, pendingHI.RemoteAddrs, evilUdpAddr,
497 "evil's address should be blocked")
498 }
499
500 t.Log("Inject correct lighthouse addr for them")
501 myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
502
503 t.Log("Route until cached packets arrive at the real them")
504 p := r.RouteForAllUntilTxTun(theirControl)
505 assert.NotNil(t, p, "a cached packet should be delivered to the correct host")
506
507 t.Log("Verify the correct host has a tunnel")
508 assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
509
510 t.Log("Verify no hostinfo artifacts from evil remain")
511 assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIpNet[0].Addr(), true),
512 "no pending hostinfo for evil")
513 assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIpNet[0].Addr(), false),
514 "no main hostinfo for evil")
515
516 myControl.Stop()
517 theirControl.Stop()
518 evilControl.Stop()
519 }
520
521 func TestHandshakeRelayComplete(t *testing.T) {
522 t.Parallel()
523 // Verify that a relay handshake completes correctly and relay state is
524 // properly maintained on all three nodes.
525
526 ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
527 myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
528 relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
529 theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
530
531 myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
532 myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
533 relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
534
535 r := router.NewR(t, myControl, relayControl, theirControl)
536 defer r.RenderFlow()
537
538 myControl.Start()
539 relayControl.Start()
540 theirControl.Start()
541
542 t.Log("Trigger handshake via relay")
543 myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi via relay")))
544
545 p := r.RouteForAllUntilTxTun(theirControl)
546 assertUdpPacket(t, []byte("Hi via relay"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
547
548 t.Log("Verify bidirectional tunnel via relay")
549 assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
550
551 t.Log("Verify relay state on my side shows relay-to-me")
552 myHI := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
553 assert.NotNil(t, myHI)
554 assert.NotEmpty(t, myHI.CurrentRelaysToMe, "should have relay-to-me for them")
555
556 t.Log("Verify relay state on their side shows relay-to-me")
557 theirHI := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
558 assert.NotNil(t, theirHI)
559 assert.NotEmpty(t, theirHI.CurrentRelaysToMe, "should have relay-to-me for me")
560
561 t.Log("Verify relay node shows through-me relays")
562 relayHI := relayControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
563 assert.NotNil(t, relayHI)
564
565 myControl.Stop()
566 relayControl.Stop()
567 theirControl.Stop()
568 }
569
570 // NOTE: Relay V1 cert + IPv6 rejection is not tested here because
571 // BuildTunUDPPacket from a V4 node to a V6 address panics in the test
572 // framework. The check is in handshake_manager.go handleOutbound relay
573 // logic (lines ~304-313): if the relay host has a V1 cert and either
574 // address is IPv6, the relay is skipped.
575
576 // NOTE: Relay reestablishment (Disestablished state transition) is covered
577 // by the existing TestReestablishRelays in handshakes_test.go.