handshakes_test.go 71 KB

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