handshakes_test.go 59 KB

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