| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679 |
- //go:build e2e_testing
- package e2e
- import (
- "net/netip"
- "slices"
- "testing"
- "time"
- "github.com/google/gopacket"
- "github.com/google/gopacket/layers"
- "github.com/slackhq/nebula"
- "github.com/slackhq/nebula/cert"
- "github.com/slackhq/nebula/cert_test"
- "github.com/slackhq/nebula/e2e/router"
- "github.com/slackhq/nebula/header"
- "github.com/slackhq/nebula/overlay"
- "github.com/slackhq/nebula/udp"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.yaml.in/yaml/v3"
- )
- func BenchmarkHotPath(b *testing.B) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Start the servers
- myControl.Start()
- theirControl.Start()
- r := router.NewR(b, myControl, theirControl)
- r.CancelFlowLogs()
- assertTunnel(b, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- // Pre-build the IP packet bytes once so the bench measures the data plane,
- // not gopacket SerializeLayers overhead.
- prebuilt := BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- // EnableFanIn switches the router to a 0-alloc routing path. Required
- // for hot-path benchmarks; would conflict with GetFromUDP-using tests.
- r.EnableFanIn()
- b.ResetTimer()
- for n := 0; n < b.N; n++ {
- myControl.InjectTunPacket(prebuilt)
- // Release the TUN-side bytes back to the harness freelist; the bench
- // just confirms a packet arrived, the contents aren't inspected.
- overlay.ReleaseTunBuf(r.RouteForAllUntilTxTun(theirControl))
- }
- myControl.Stop()
- theirControl.Stop()
- }
- func BenchmarkHotPathRelay(b *testing.B) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(b, myControl, relayControl, theirControl)
- r.CancelFlowLogs()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- assertTunnel(b, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- prebuilt := BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- r.EnableFanIn()
- b.ResetTimer()
- for n := 0; n < b.N; n++ {
- myControl.InjectTunPacket(prebuilt)
- overlay.ReleaseTunBuf(r.RouteForAllUntilTxTun(theirControl))
- }
- myControl.Stop()
- theirControl.Stop()
- relayControl.Stop()
- }
- func TestGoodHandshake(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Send a udp packet through to begin standing up the tunnel, this should come out the other side")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- t.Log("Have them consume my stage 0 packet. They have a tunnel now")
- theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
- t.Log("Get their stage 1 packet so that we can play with it")
- stage1Packet := theirControl.GetFromUDP(true)
- t.Log("I consume a garbage packet with a proper nebula header for our tunnel")
- // this should log a statement and get ignored, allowing the real handshake packet to complete the tunnel
- badPacket := stage1Packet.Copy()
- badPacket.Data = badPacket.Data[:len(badPacket.Data)-header.Len]
- myControl.InjectUDPPacket(badPacket)
- t.Log("Have me consume their real stage 1 packet. I have a tunnel now")
- myControl.InjectUDPPacket(stage1Packet)
- t.Log("Wait until we see my cached packet come through")
- myControl.WaitForType(1, 0, theirControl)
- t.Log("Make sure our host infos are correct")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- t.Log("Get that cached packet and make sure it looks right")
- myCachedPacket := theirControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- t.Log("Do a bidirectional tunnel test")
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestGoodHandshakeNoOverlap(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "2001::69/24", nil) //look ma, cross-stack!
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Start the servers
- myControl.Start()
- theirControl.Start()
- empty := []byte{}
- t.Log("do something to cause a handshake")
- myControl.GetF().SendMessageToVpnAddr(header.Test, header.MessageNone, theirVpnIpNet[0].Addr(), empty, empty, empty)
- t.Log("Have them consume my stage 0 packet. They have a tunnel now")
- theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
- t.Log("Get their stage 1 packet")
- stage1Packet := theirControl.GetFromUDP(true)
- t.Log("Have me consume their stage 1 packet. I have a tunnel now")
- myControl.InjectUDPPacket(stage1Packet)
- t.Log("Wait until we see a test packet come through to make sure we give the tunnel time to complete")
- myControl.WaitForType(header.Test, 0, theirControl)
- t.Log("Make sure our host infos are correct")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestWrongResponderHandshake(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.100/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.99/24", nil)
- evilControl, evilVpnIp, evilUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "evil", "10.128.0.2/24", nil)
- // Put the evil udp addr in for their vpn Ip, this is a case of being lied to by the lighthouse.
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), evilUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl, evilControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- evilControl.Start()
- t.Log("Start the handshake process, we will route until we see the evil tunnel closed")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- h := &header.H{}
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- err := h.Parse(p.Data)
- if err != nil {
- panic(err)
- }
- if h.Type == header.CloseTunnel && p.To == evilUdpAddr {
- return router.RouteAndExit
- }
- return router.KeepRouting
- })
- t.Log("Evil tunnel is closed, inject the correct udp addr for them")
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- pendingHi := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), true)
- assert.NotContains(t, pendingHi.RemoteAddrs, evilUdpAddr)
- t.Log("Route until we see the cached packet")
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- err := h.Parse(p.Data)
- if err != nil {
- panic(err)
- }
- if p.To == theirUdpAddr && h.Type == 1 {
- return router.RouteAndExit
- }
- return router.KeepRouting
- })
- t.Log("My cached packet should be received by them")
- myCachedPacket := theirControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- t.Log("Test the tunnel with them")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Flush all packets from all controllers")
- r.FlushAll()
- t.Log("Ensure ensure I don't have any hostinfo artifacts from evil")
- assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), true), "My pending hostmap should not contain evil")
- assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), false), "My main hostmap should not contain evil")
- r.RenderHostmaps("Final hostmaps", myControl, theirControl, evilControl)
- t.Log("Success!")
- myControl.Stop()
- theirControl.Stop()
- }
- func TestWrongResponderHandshakeStaticHostMap(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.99/24", nil)
- evilControl, evilVpnIp, evilUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "evil", "10.128.0.2/24", nil)
- o := m{
- "static_host_map": m{
- theirVpnIpNet[0].Addr().String(): []string{evilUdpAddr.String()},
- },
- }
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.100/24", o)
- // 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.
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), evilUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl, evilControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- evilControl.Start()
- t.Log("Start the handshake process, we will route until we see the evil tunnel closed")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- h := &header.H{}
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- err := h.Parse(p.Data)
- if err != nil {
- panic(err)
- }
- if h.Type == header.CloseTunnel && p.To == evilUdpAddr {
- return router.RouteAndExit
- }
- return router.KeepRouting
- })
- t.Log("Evil tunnel is closed, inject the correct udp addr for them")
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- pendingHi := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), true)
- assert.NotContains(t, pendingHi.RemoteAddrs, evilUdpAddr)
- t.Log("Route until we see the cached packet")
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- err := h.Parse(p.Data)
- if err != nil {
- panic(err)
- }
- if p.To == theirUdpAddr && h.Type == 1 {
- return router.RouteAndExit
- }
- return router.KeepRouting
- })
- t.Log("My cached packet should be received by them")
- myCachedPacket := theirControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- t.Log("Test the tunnel with them")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Flush all packets from all controllers")
- r.FlushAll()
- t.Log("Ensure ensure I don't have any hostinfo artifacts from evil")
- assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), true), "My pending hostmap should not contain evil")
- assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), false), "My main hostmap should not contain evil")
- //NOTE: if evil lost the handshake race it may still have a tunnel since me would reject the handshake since the tunnel is complete
- r.RenderHostmaps("Final hostmaps", myControl, theirControl, evilControl)
- t.Log("Success!")
- myControl.Stop()
- theirControl.Stop()
- }
- func TestStage1Race(t *testing.T) {
- t.Parallel()
- // This tests ensures that two hosts handshaking with each other at the same time will allow traffic to flow
- // But will eventually collapse down to a single tunnel
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Put their info in our lighthouse and vice versa
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake to start on both me and them")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
- t.Log("Get both stage 1 handshake packets")
- myHsForThem := myControl.GetFromUDP(true)
- theirHsForMe := theirControl.GetFromUDP(true)
- r.Log("Now inject both stage 1 handshake packets")
- r.InjectUDPPacket(theirControl, myControl, theirHsForMe)
- r.InjectUDPPacket(myControl, theirControl, myHsForThem)
- r.Log("Route until they receive a message packet")
- myCachedPacket := r.RouteForAllUntilTxTun(theirControl)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.Log("Their cached packet should be received by me")
- theirCachedPacket := r.RouteForAllUntilTxTun(myControl)
- assertUdpPacket(t, []byte("Hi from them"), theirCachedPacket, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
- r.Log("Do a bidirectional tunnel test")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myHostmapHosts := myControl.ListHostmapHosts(false)
- myHostmapIndexes := myControl.ListHostmapIndexes(false)
- theirHostmapHosts := theirControl.ListHostmapHosts(false)
- theirHostmapIndexes := theirControl.ListHostmapIndexes(false)
- // We should have two tunnels on both sides
- assert.Len(t, myHostmapHosts, 1)
- assert.Len(t, theirHostmapHosts, 1)
- assert.Len(t, myHostmapIndexes, 2)
- assert.Len(t, theirHostmapIndexes, 2)
- r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
- r.Log("Spin until connection manager tears down a tunnel")
- for myControl.GetHostmapIndexCount()+theirControl.GetHostmapIndexCount() > 2 {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Connection manager hasn't ticked yet")
- time.Sleep(time.Second)
- }
- myFinalHostmapHosts := myControl.ListHostmapHosts(false)
- myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
- theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
- theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
- // We should only have a single tunnel now on both sides
- assert.Len(t, myFinalHostmapHosts, 1)
- assert.Len(t, theirFinalHostmapHosts, 1)
- assert.Len(t, myFinalHostmapIndexes, 1)
- assert.Len(t, theirFinalHostmapIndexes, 1)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestUncleanShutdownRaceLoser(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- r.Log("Trigger a handshake from me to them")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- p := r.RouteForAllUntilTxTun(theirControl)
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.Log("Nuke my hostmap")
- myHostmap := myControl.GetHostmap()
- myHostmap.Lock()
- myHostmap.Hosts = map[netip.Addr]*nebula.HostInfo{}
- myHostmap.Indexes = map[uint32]*nebula.HostInfo{}
- myHostmap.RemoteIndexes = map[uint32]*nebula.HostInfo{}
- myHostmap.Unlock()
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me again")))
- p = r.RouteForAllUntilTxTun(theirControl)
- assertUdpPacket(t, []byte("Hi from me again"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.Log("Assert the tunnel works")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.Log("Wait for the dead index to go away")
- start := theirControl.GetHostmapIndexCount()
- for {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- if theirControl.GetHostmapIndexCount() < start {
- break
- }
- time.Sleep(time.Second)
- }
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- }
- func TestUncleanShutdownRaceWinner(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- r.Log("Trigger a handshake from me to them")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- p := r.RouteForAllUntilTxTun(theirControl)
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- r.Log("Nuke my hostmap")
- theirHostmap := theirControl.GetHostmap()
- theirHostmap.Lock()
- theirHostmap.Hosts = map[netip.Addr]*nebula.HostInfo{}
- theirHostmap.Indexes = map[uint32]*nebula.HostInfo{}
- theirHostmap.RemoteIndexes = map[uint32]*nebula.HostInfo{}
- theirHostmap.Unlock()
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them again")))
- p = r.RouteForAllUntilTxTun(myControl)
- assertUdpPacket(t, []byte("Hi from them again"), p, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("Derp hostmaps", myControl, theirControl)
- r.Log("Assert the tunnel works")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.Log("Wait for the dead index to go away")
- start := myControl.GetHostmapIndexCount()
- for {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- if myControl.GetHostmapIndexCount() < start {
- break
- }
- time.Sleep(time.Second)
- }
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- }
- func TestRelays(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
- }
- func TestRelaysDontCareAboutIps(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "2001::9999/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
- }
- func TestReestablishRelays(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- t.Log("Ensure packet traversal from them to me via the relay")
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
- p = r.RouteForAllUntilTxTun(myControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from them"), p, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
- // If we break the relay's connection to 'them', 'me' needs to detect and recover the connection
- r.Log("Close the tunnel")
- relayControl.CloseTunnel(theirVpnIpNet[0].Addr(), true)
- start := myControl.GetHostmapIndexCount()
- curIndexes := myControl.GetHostmapIndexCount()
- for curIndexes >= start {
- curIndexes = myControl.GetHostmapIndexCount()
- r.Logf("Wait for the dead index to go away:start=%v indexes, current=%v indexes", start, curIndexes)
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me should fail")))
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- return router.RouteAndExit
- })
- time.Sleep(2 * time.Second)
- }
- r.Log("Dead index went away. Woot!")
- r.RenderHostmaps("Me removed hostinfo", myControl, relayControl, theirControl)
- // Next packet should re-establish a relayed connection and work just great.
- t.Logf("Assert the tunnel...")
- for {
- t.Log("RouteForAllUntilTxTun")
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- p = r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- packet := gopacket.NewPacket(p, layers.LayerTypeIPv4, gopacket.Lazy)
- v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
- if slices.Compare(v4.SrcIP, myVpnIpNet[0].Addr().AsSlice()) != 0 {
- t.Logf("SrcIP is unexpected...this is not the packet I'm looking for. Keep looking")
- continue
- }
- if slices.Compare(v4.DstIP, theirVpnIpNet[0].Addr().AsSlice()) != 0 {
- t.Logf("DstIP is unexpected...this is not the packet I'm looking for. Keep looking")
- continue
- }
- udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
- if udp == nil {
- t.Log("Not a UDP packet. This is not the packet I'm looking for. Keep looking")
- continue
- }
- data := packet.ApplicationLayer()
- if data == nil {
- t.Log("No data found in packet. This is not the packet I'm looking for. Keep looking.")
- continue
- }
- if string(data.Payload()) != "Hi from me" {
- t.Logf("Unexpected payload: '%v', keep looking", string(data.Payload()))
- continue
- }
- t.Log("I found my lost packet. I am so happy.")
- break
- }
- t.Log("Assert the tunnel works the other way, too")
- for {
- t.Log("RouteForAllUntilTxTun")
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
- p = r.RouteForAllUntilTxTun(myControl)
- r.Log("Assert the tunnel works")
- packet := gopacket.NewPacket(p, layers.LayerTypeIPv4, gopacket.Lazy)
- v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
- if slices.Compare(v4.DstIP, myVpnIpNet[0].Addr().AsSlice()) != 0 {
- t.Logf("Dst is unexpected...this is not the packet I'm looking for. Keep looking")
- continue
- }
- if slices.Compare(v4.SrcIP, theirVpnIpNet[0].Addr().AsSlice()) != 0 {
- t.Logf("SrcIP is unexpected...this is not the packet I'm looking for. Keep looking")
- continue
- }
- udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
- if udp == nil {
- t.Log("Not a UDP packet. This is not the packet I'm looking for. Keep looking")
- continue
- }
- data := packet.ApplicationLayer()
- if data == nil {
- t.Log("No data found in packet. This is not the packet I'm looking for. Keep looking.")
- continue
- }
- if string(data.Payload()) != "Hi from them" {
- t.Logf("Unexpected payload: '%v', keep looking", string(data.Payload()))
- continue
- }
- t.Log("I found my lost packet. I am so happy.")
- break
- }
- r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
- }
- func TestRelayHandshakeOverDisestablishedEntry(t *testing.T) {
- t.Parallel()
- // If them tears down the tunnel while me keeps Established relay state, me's next
- // handshake flows through the relay with no fresh CreateRelayRequest and lands on
- // them's Disestablished terminal relay entry. them must re-establish that entry, or
- // its first transmit deletes its only relay and the tunnel is born transmit-dead:
- // them can receive but every send is silently dropped.
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- p := r.RouteForAllUntilTxTun(theirControl)
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- oldIdx := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false).LocalIndex
- t.Log("Close the tunnel on them only, marking their relay entry Disestablished")
- theirControl.CloseTunnel(myVpnIpNet[0].Addr(), true)
- t.Log("Re-handshake from me, riding the still-Established relay state")
- myControl.ReHandshake(theirVpnIpNet[0].Addr())
- for {
- h := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
- if h != nil && h.LocalIndex != oldIdx && h.RemoteIndex != 0 {
- break
- }
- r.RouteForAllExitFunc(func(*udp.Packet, *nebula.Control) router.ExitType {
- return router.RouteAndExit
- })
- }
- hAtThem := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
- require.NotNil(t, hAtThem, "them should have completed the relayed handshake")
- require.Equal(t, []netip.Addr{relayVpnIpNet[0].Addr()}, hAtThem.CurrentRelaysToMe, "them should know a relay for the new tunnel")
- t.Log("Send from them to me; their only relay entry must survive the transmit")
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
- require.Never(t, func() bool {
- h := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
- return h == nil || len(h.CurrentRelaysToMe) == 0
- }, time.Second, 10*time.Millisecond, "them deleted its only relay entry; the tunnel is permanently transmit-dead")
- p = r.RouteForAllUntilTxTun(myControl)
- assertUdpPacket(t, []byte("Hi from them"), p, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
- }
- func TestStage1RaceRelays(t *testing.T) {
- t.Parallel()
- //NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- theirControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- theirControl.InjectRelays(myVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- relayControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- r.Log("Get a tunnel between me and relay")
- assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
- r.Log("Get a tunnel between them and relay")
- assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
- r.Log("Trigger a handshake from both them and me via relay to them and me")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
- r.Log("Wait for a packet from them to me")
- p := r.RouteForAllUntilTxTun(myControl)
- _ = p
- r.FlushAll()
- myControl.Stop()
- theirControl.Stop()
- relayControl.Stop()
- }
- func TestStage1RaceRelays2(t *testing.T) {
- t.Parallel()
- //NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- theirControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- theirControl.InjectRelays(myVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- relayControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- r.Log("Get a tunnel between me and relay")
- assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
- r.Log("Get a tunnel between them and relay")
- assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
- r.Log("Trigger a handshake from both them and me via relay to them and me")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
- //r.RouteUntilAfterMsgType(myControl, header.Control, header.MessageNone)
- //r.RouteUntilAfterMsgType(theirControl, header.Control, header.MessageNone)
- r.Log("Wait for a packet from them to me; myControl")
- r.RouteForAllUntilTxTun(myControl)
- r.Log("Wait for a packet from them to me; theirControl")
- r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- t.Log("Wait until we remove extra tunnels")
- t.Logf("Waiting for hostinfos to be removed... myControl=%d theirControl=%d relayControl=%d",
- myControl.GetHostmapIndexCount(),
- theirControl.GetHostmapIndexCount(),
- relayControl.GetHostmapIndexCount(),
- )
- hostInfos := myControl.GetHostmapIndexCount() + theirControl.GetHostmapIndexCount() + relayControl.GetHostmapIndexCount()
- retries := 60
- for hostInfos > 6 && retries > 0 {
- hostInfos = myControl.GetHostmapIndexCount() + theirControl.GetHostmapIndexCount() + relayControl.GetHostmapIndexCount()
- t.Logf("Waiting for hostinfos to be removed... myControl=%d theirControl=%d relayControl=%d",
- myControl.GetHostmapIndexCount(),
- theirControl.GetHostmapIndexCount(),
- relayControl.GetHostmapIndexCount(),
- )
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Connection manager hasn't ticked yet")
- time.Sleep(time.Second)
- retries--
- }
- r.Log("Assert the tunnel works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- myControl.Stop()
- theirControl.Stop()
- relayControl.Stop()
- }
- func TestRehandshakingRelays(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
- // When I update the certificate for the relay, both me and them will have 2 host infos for the relay,
- // and the main host infos will not have any relay state to handle the me<->relay<->them tunnel.
- r.Log("Renew relay certificate and spin until me and them sees it")
- _, _, 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"})
- caB, err := ca.MarshalPEM()
- if err != nil {
- panic(err)
- }
- relayConfig.Settings["pki"] = m{
- "ca": string(caB),
- "cert": string(myNextPEM),
- "key": string(myNextPrivKey),
- }
- rc, err := yaml.Marshal(relayConfig.Settings)
- require.NoError(t, err)
- relayConfig.ReloadConfigString(string(rc))
- for {
- r.Log("Assert the tunnel works between myVpnIpNet and relayVpnIpNet")
- assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
- c := myControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- r.Log("Certificate between my and relay is updated!")
- break
- }
- time.Sleep(time.Second)
- }
- for {
- r.Log("Assert the tunnel works between theirVpnIpNet and relayVpnIpNet")
- assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
- c := theirControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- r.Log("Certificate between their and relay is updated!")
- break
- }
- time.Sleep(time.Second)
- }
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
- // We should have two hostinfos on all sides
- for myControl.GetHostmapIndexCount() != 2 {
- t.Logf("Waiting for myControl hostinfos (%v != 2) to get cleaned up from lack of use...", myControl.GetHostmapIndexCount())
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("myControl hostinfos got cleaned up!")
- for theirControl.GetHostmapIndexCount() != 2 {
- t.Logf("Waiting for theirControl hostinfos (%v != 2) to get cleaned up from lack of use...", theirControl.GetHostmapIndexCount())
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("theirControl hostinfos got cleaned up!")
- for relayControl.GetHostmapIndexCount() != 2 {
- t.Logf("Waiting for relayControl hostinfos (%v != 2) to get cleaned up from lack of use...", relayControl.GetHostmapIndexCount())
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("relayControl hostinfos got cleaned up!")
- }
- func TestRehandshakingRelaysPrimary(t *testing.T) {
- t.Parallel()
- // This test is the same as TestRehandshakingRelays but one of the terminal types is a primary swap winner
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.128/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.1/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
- // When I update the certificate for the relay, both me and them will have 2 host infos for the relay,
- // and the main host infos will not have any relay state to handle the me<->relay<->them tunnel.
- r.Log("Renew relay certificate and spin until me and them sees it")
- _, _, 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"})
- caB, err := ca.MarshalPEM()
- if err != nil {
- panic(err)
- }
- relayConfig.Settings["pki"] = m{
- "ca": string(caB),
- "cert": string(myNextPEM),
- "key": string(myNextPrivKey),
- }
- rc, err := yaml.Marshal(relayConfig.Settings)
- require.NoError(t, err)
- relayConfig.ReloadConfigString(string(rc))
- for {
- r.Log("Assert the tunnel works between myVpnIpNet and relayVpnIpNet")
- assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
- c := myControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- r.Log("Certificate between my and relay is updated!")
- break
- }
- time.Sleep(time.Second)
- }
- for {
- r.Log("Assert the tunnel works between theirVpnIpNet and relayVpnIpNet")
- assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
- c := theirControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- r.Log("Certificate between their and relay is updated!")
- break
- }
- time.Sleep(time.Second)
- }
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
- // We should have two hostinfos on all sides
- for myControl.GetHostmapIndexCount() != 2 {
- t.Logf("Waiting for myControl hostinfos (%v != 2) to get cleaned up from lack of use...", myControl.GetHostmapIndexCount())
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("myControl hostinfos got cleaned up!")
- for theirControl.GetHostmapIndexCount() != 2 {
- t.Logf("Waiting for theirControl hostinfos (%v != 2) to get cleaned up from lack of use...", theirControl.GetHostmapIndexCount())
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("theirControl hostinfos got cleaned up!")
- for relayControl.GetHostmapIndexCount() != 2 {
- t.Logf("Waiting for relayControl hostinfos (%v != 2) to get cleaned up from lack of use...", relayControl.GetHostmapIndexCount())
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("relayControl hostinfos got cleaned up!")
- }
- func TestRehandshaking(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.2/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.1/24", nil)
- // Put their info in our lighthouse and vice versa
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Stand up a tunnel between me and them")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
- r.Log("Renew my certificate and spin until their sees it")
- _, _, 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"})
- caB, err := ca.MarshalPEM()
- if err != nil {
- panic(err)
- }
- myConfig.Settings["pki"] = m{
- "ca": string(caB),
- "cert": string(myNextPEM),
- "key": string(myNextPrivKey),
- }
- rc, err := yaml.Marshal(myConfig.Settings)
- require.NoError(t, err)
- myConfig.ReloadConfigString(string(rc))
- for {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- break
- }
- time.Sleep(time.Second)
- }
- r.Log("Got the new cert")
- // Flip their firewall to only allowing the new group to catch the tunnels reverting incorrectly
- rc, err = yaml.Marshal(theirConfig.Settings)
- require.NoError(t, err)
- var theirNewConfig m
- require.NoError(t, yaml.Unmarshal(rc, &theirNewConfig))
- theirFirewall := theirNewConfig["firewall"].(map[string]any)
- theirFirewall["inbound"] = []m{{
- "proto": "any",
- "port": "any",
- "group": "new group",
- }}
- rc, err = yaml.Marshal(theirNewConfig)
- require.NoError(t, err)
- theirConfig.ReloadConfigString(string(rc))
- r.Log("Spin until there is only 1 tunnel")
- for myControl.GetHostmapIndexCount()+theirControl.GetHostmapIndexCount() > 2 {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Connection manager hasn't ticked yet")
- time.Sleep(time.Second)
- }
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myFinalHostmapHosts := myControl.ListHostmapHosts(false)
- myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
- theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
- theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
- // Make sure the correct tunnel won
- c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
- assert.Contains(t, c.Cert.Groups(), "new group")
- // We should only have a single tunnel now on both sides
- assert.Len(t, myFinalHostmapHosts, 1)
- assert.Len(t, theirFinalHostmapHosts, 1)
- assert.Len(t, myFinalHostmapIndexes, 1)
- assert.Len(t, theirFinalHostmapIndexes, 1)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestRehandshakingLoser(t *testing.T) {
- t.Parallel()
- // The purpose of this test is that the race loser renews their certificate and rehandshakes. The final tunnel
- // Should be the one with the new certificate
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.2/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.1/24", nil)
- // Put their info in our lighthouse and vice versa
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Stand up a tunnel between me and them")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
- theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
- r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
- r.Log("Renew their certificate and spin until mine sees it")
- _, _, 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"})
- caB, err := ca.MarshalPEM()
- if err != nil {
- panic(err)
- }
- theirConfig.Settings["pki"] = m{
- "ca": string(caB),
- "cert": string(theirNextPEM),
- "key": string(theirNextPrivKey),
- }
- rc, err := yaml.Marshal(theirConfig.Settings)
- require.NoError(t, err)
- theirConfig.ReloadConfigString(string(rc))
- for {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- theirCertInMe := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
- if slices.Contains(theirCertInMe.Cert.Groups(), "their new group") {
- break
- }
- time.Sleep(time.Second)
- }
- // Flip my firewall to only allowing the new group to catch the tunnels reverting incorrectly
- rc, err = yaml.Marshal(myConfig.Settings)
- require.NoError(t, err)
- var myNewConfig m
- require.NoError(t, yaml.Unmarshal(rc, &myNewConfig))
- theirFirewall := myNewConfig["firewall"].(map[string]any)
- theirFirewall["inbound"] = []m{{
- "proto": "any",
- "port": "any",
- "group": "their new group",
- }}
- rc, err = yaml.Marshal(myNewConfig)
- require.NoError(t, err)
- myConfig.ReloadConfigString(string(rc))
- r.Log("Spin until there is only 1 tunnel")
- for myControl.GetHostmapIndexCount()+theirControl.GetHostmapIndexCount() > 2 {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Connection manager hasn't ticked yet")
- time.Sleep(time.Second)
- }
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myFinalHostmapHosts := myControl.ListHostmapHosts(false)
- myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
- theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
- theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
- // Make sure the correct tunnel won
- theirCertInMe := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
- assert.Contains(t, theirCertInMe.Cert.Groups(), "their new group")
- // We should only have a single tunnel now on both sides
- assert.Len(t, myFinalHostmapHosts, 1)
- assert.Len(t, theirFinalHostmapHosts, 1)
- assert.Len(t, myFinalHostmapIndexes, 1)
- assert.Len(t, theirFinalHostmapIndexes, 1)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestRaceRegression(t *testing.T) {
- t.Parallel()
- // This test forces stage 1, stage 2, stage 1 to be received by me from them
- // We had a bug where we were not finding the duplicate handshake and responding to the final stage 1 which
- // caused a cross-linked hostinfo
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Start the servers
- myControl.Start()
- theirControl.Start()
- //them rx stage:1 initiatorIndex=642843150 responderIndex=0
- //me rx stage:1 initiatorIndex=120607833 responderIndex=0
- //them rx stage:1 initiatorIndex=642843150 responderIndex=0
- //me rx stage:2 initiatorIndex=642843150 responderIndex=3701775874
- //me rx stage:1 initiatorIndex=120607833 responderIndex=0
- //them rx stage:2 initiatorIndex=120607833 responderIndex=4209862089
- t.Log("Start both handshakes")
- myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them")))
- t.Log("Get both stage 1")
- myStage1ForThem := myControl.GetFromUDP(true)
- theirStage1ForMe := theirControl.GetFromUDP(true)
- t.Log("Inject them in a special way")
- theirControl.InjectUDPPacket(myStage1ForThem)
- myControl.InjectUDPPacket(theirStage1ForMe)
- theirControl.InjectUDPPacket(myStage1ForThem)
- t.Log("Get both stage 2")
- myStage2ForThem := myControl.GetFromUDP(true)
- theirStage2ForMe := theirControl.GetFromUDP(true)
- t.Log("Inject them in a special way again")
- myControl.InjectUDPPacket(theirStage2ForMe)
- myControl.InjectUDPPacket(theirStage1ForMe)
- theirControl.InjectUDPPacket(myStage2ForThem)
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- t.Log("Flush the packets")
- r.RouteForAllUntilTxTun(myControl)
- r.RouteForAllUntilTxTun(theirControl)
- r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
- t.Log("Make sure the tunnel still works")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestV2NonPrimaryWithLighthouse(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "10.128.0.1/24, ff::1/64", m{"lighthouse": m{"am_lighthouse": true}})
- o := m{
- "static_host_map": m{
- lhVpnIpNet[1].Addr().String(): []string{lhUdpAddr.String()},
- },
- "lighthouse": m{
- "hosts": []string{lhVpnIpNet[1].Addr().String()},
- "local_allow_list": m{
- // Try and block our lighthouse updates from using the actual addresses assigned to this computer
- // If we start discovering addresses the test router doesn't know about then test traffic cant flow
- "10.0.0.0/24": true,
- "::/0": false,
- },
- },
- }
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.2/24, ff::2/64", o)
- theirControl, theirVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.3/24, ff::3/64", o)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, lhControl, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- lhControl.Start()
- myControl.Start()
- theirControl.Start()
- t.Log("Stand up an ipv6 tunnel between me and them")
- assert.True(t, myVpnIpNet[1].Addr().Is6())
- assert.True(t, theirVpnIpNet[1].Addr().Is6())
- assertTunnel(t, myVpnIpNet[1].Addr(), theirVpnIpNet[1].Addr(), myControl, theirControl, r)
- lhControl.Stop()
- myControl.Stop()
- theirControl.Stop()
- }
- func TestV2NonPrimaryWithOffNetLighthouse(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "2001::1/64", m{"lighthouse": m{"am_lighthouse": true}})
- o := m{
- "static_host_map": m{
- lhVpnIpNet[0].Addr().String(): []string{lhUdpAddr.String()},
- },
- "lighthouse": m{
- "hosts": []string{lhVpnIpNet[0].Addr().String()},
- "local_allow_list": m{
- // Try and block our lighthouse updates from using the actual addresses assigned to this computer
- // If we start discovering addresses the test router doesn't know about then test traffic cant flow
- "10.0.0.0/24": true,
- "::/0": false,
- },
- },
- }
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.2/24, ff::2/64", o)
- theirControl, theirVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.3/24, ff::3/64", o)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, lhControl, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- lhControl.Start()
- myControl.Start()
- theirControl.Start()
- t.Log("Stand up an ipv6 tunnel between me and them")
- assert.True(t, myVpnIpNet[1].Addr().Is6())
- assert.True(t, theirVpnIpNet[1].Addr().Is6())
- assertTunnel(t, myVpnIpNet[1].Addr(), theirVpnIpNet[1].Addr(), myControl, theirControl, r)
- lhControl.Stop()
- myControl.Stop()
- theirControl.Stop()
- }
- func TestLighthouseUpdateOnReload(t *testing.T) {
- t.Parallel()
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- // Create the lighthouse
- lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh", "10.128.0.1/24", m{"lighthouse": m{"am_lighthouse": true}})
- // Create a client with NO lighthouse configured and a long update interval.
- // The initial SendUpdate at startup will be a no-op since no lighthouses are known.
- myControl, myVpnIpNet, _, myConfig := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.2/24", m{
- "lighthouse": m{
- "interval": 600,
- "local_allow_list": m{
- "10.0.0.0/24": true,
- "::/0": false,
- },
- },
- })
- r := router.NewR(t, lhControl, myControl)
- defer r.RenderFlow()
- lhControl.Start()
- myControl.Start()
- // Drain any startup packets (there should be none meaningful)
- r.FlushAll()
- // Verify lighthouse has no knowledge of the client
- assert.Nil(t, lhControl.QueryLighthouse(myVpnIpNet[0].Addr()))
- // Build a new config that adds the lighthouse
- newSettings := make(m)
- for k, v := range myConfig.Settings {
- newSettings[k] = v
- }
- newSettings["static_host_map"] = m{
- lhVpnIpNet[0].Addr().String(): []any{lhUdpAddr.String()},
- }
- newSettings["lighthouse"] = m{
- "hosts": []any{lhVpnIpNet[0].Addr().String()},
- "interval": 600,
- "local_allow_list": m{
- "10.0.0.0/24": true,
- "::/0": false,
- },
- }
- newCfg, err := yaml.Marshal(newSettings)
- require.NoError(t, err)
- // Reload the config. The lighthouse.hosts change triggers TriggerUpdate,
- // which wakes the update worker. It calls SendUpdate, initiating a
- // handshake to the new lighthouse and caching the HostUpdateNotification.
- require.NoError(t, myConfig.ReloadConfigString(string(newCfg)))
- // Route until the lighthouse receives the HostUpdateNotification.
- // This covers: handshake stage 1, stage 2, then the cached update.
- done := make(chan struct{})
- go func() {
- r.RouteForAllUntilAfterMsgTypeTo(lhControl, header.LightHouse, 0)
- close(done)
- }()
- select {
- case <-done:
- case <-time.After(5 * time.Second):
- t.Fatal("timed out waiting for lighthouse update after config reload")
- }
- // Verify lighthouse now has the client's addresses
- assert.NotNil(t, lhControl.QueryLighthouse(myVpnIpNet[0].Addr()))
- r.RenderHostmaps("Final hostmaps", lhControl, myControl)
- lhControl.Stop()
- myControl.Stop()
- }
- func TestGoodHandshakeUnsafeDest(t *testing.T) {
- t.Parallel()
- unsafePrefix := "192.168.6.0/24"
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServerWithUdpAndUnsafeNetworks(cert.Version2, ca, caKey, "spooky", "10.128.0.2/24", netip.MustParseAddrPort("10.64.0.2:4242"), unsafePrefix, nil)
- route := m{"route": unsafePrefix, "via": theirVpnIpNet[0].Addr().String()}
- myCfg := m{
- "tun": m{
- "unsafe_routes": []m{route},
- },
- }
- myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.1/24", myCfg)
- t.Logf("my config %v", myConfig)
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- spookyDest := netip.MustParseAddr("192.168.6.4")
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Send a udp packet through to begin standing up the tunnel, this should come out the other side")
- myControl.InjectTunPacket(BuildTunUDPPacket(spookyDest, 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- t.Log("Have them consume my stage 0 packet. They have a tunnel now")
- theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
- t.Log("Get their stage 1 packet so that we can play with it")
- stage1Packet := theirControl.GetFromUDP(true)
- t.Log("I consume a garbage packet with a proper nebula header for our tunnel")
- // this should log a statement and get ignored, allowing the real handshake packet to complete the tunnel
- badPacket := stage1Packet.Copy()
- badPacket.Data = badPacket.Data[:len(badPacket.Data)-header.Len]
- myControl.InjectUDPPacket(badPacket)
- t.Log("Have me consume their real stage 1 packet. I have a tunnel now")
- myControl.InjectUDPPacket(stage1Packet)
- t.Log("Wait until we see my cached packet come through")
- myControl.WaitForType(1, 0, theirControl)
- t.Log("Make sure our host infos are correct")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- t.Log("Get that cached packet and make sure it looks right")
- myCachedPacket := theirControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), spookyDest, 80, 80)
- //reply
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, spookyDest, 80, []byte("Hi from the spookyman")))
- //wait for reply
- theirControl.WaitForType(1, 0, myControl)
- theirCachedPacket := myControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from the spookyman"), theirCachedPacket, spookyDest, myVpnIpNet[0].Addr(), 80, 80)
- t.Log("Do a bidirectional tunnel test")
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestMultiVpnAddrDeletePrimaryKeepsSecondAddr(t *testing.T) {
- t.Parallel()
- // Regression for the hostmap multi-vpnAddr delete bug. A dual-stack (v4+v6) V2-cert peer that
- // handshakes twice at once ends up with two hostinfos linked in the shared next/prev chain, with the
- // primary owning both addresses. Deleting that primary (e.g. connection manager dropping it, a
- // CloseTunnel, a collision) must promote the surviving sibling for EVERY address. The pre-fix code
- // unlinked the chain once per address, so it promoted the sibling for the first address and orphaned
- // the second: the peer stayed reachable at its v4 addr but not its v6 addr despite a live tunnel.
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24,fd00::1/64", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.2/24,fd00::2/64", nil)
- // This bug only exists for peers carrying more than one vpn address
- require.Len(t, theirVpnIpNet, 2)
- theirV4 := theirVpnIpNet[0].Addr()
- theirV6 := theirVpnIpNet[1].Addr()
- // Put their info in our lighthouse and vice versa
- myControl.InjectLightHouseAddr(theirV4, theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- myControl.Start()
- theirControl.Start()
- // Race a handshake so both of us build a hostinfo for the other, leaving my hostmap with a single
- // host (them) backed by two linked hostinfos, just like TestStage1Race.
- myControl.InjectTunPacket(BuildTunUDPPacket(theirV4, 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
- theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirV4, 80, []byte("Hi from them")))
- myHsForThem := myControl.GetFromUDP(true)
- theirHsForMe := theirControl.GetFromUDP(true)
- r.InjectUDPPacket(theirControl, myControl, theirHsForMe)
- r.InjectUDPPacket(myControl, theirControl, myHsForThem)
- r.RouteForAllUntilTxTun(theirControl)
- r.RouteForAllUntilTxTun(myControl)
- r.RenderHostmaps("Racing hostmaps", myControl, theirControl)
- // Two hostinfos for them means the shared next/prev chain has a sibling to promote. The Hosts map has
- // one entry per vpn address (two, for dual stack), so the index count is what tells us there are two
- // hostinfos.
- require.Len(t, myControl.ListHostmapIndexes(false), 2)
- // The primary owns both of their addresses
- primaryV4 := myControl.GetHostInfoByVpnAddr(theirV4, false)
- primaryV6 := myControl.GetHostInfoByVpnAddr(theirV6, false)
- require.NotNil(t, primaryV4)
- require.NotNil(t, primaryV6)
- require.Equal(t, primaryV4.LocalIndex, primaryV6.LocalIndex, "both addrs should point at the same primary")
- // Delete the primary tunnel. localOnly so we don't perturb their side, we only care about my hostmap.
- require.True(t, myControl.CloseTunnel(theirV4, true))
- // The surviving sibling must still serve BOTH addresses.
- survivorV4 := myControl.GetHostInfoByVpnAddr(theirV4, false)
- survivorV6 := myControl.GetHostInfoByVpnAddr(theirV6, false)
- require.NotNil(t, survivorV4, "v4 addr should still resolve to the surviving tunnel")
- // Pre-fix this is nil: the second address was orphaned when the primary was deleted.
- require.NotNil(t, survivorV6, "v6 addr was orphaned after deleting the primary (multi-vpnAddr delete bug)")
- assert.Equal(t, survivorV4.LocalIndex, survivorV6.LocalIndex, "both addrs should promote to the same survivor")
- assert.NotEqual(t, primaryV4.LocalIndex, survivorV4.LocalIndex, "a different hostinfo should now be primary")
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
|