handshakes_test.go 61 KB

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