handshakes_test.go 64 KB

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