handshakes_test.go 51 KB

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