handshakes_test.go 51 KB

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