handshakes_test.go 68 KB

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