handshakes_test.go 63 KB

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