handshakes_test.go 63 KB

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