1
0

rebind_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //go:build e2e_testing
  2. package e2e
  3. import (
  4. "net/netip"
  5. "testing"
  6. "time"
  7. "github.com/slackhq/nebula"
  8. "github.com/slackhq/nebula/cert"
  9. "github.com/slackhq/nebula/cert_test"
  10. "github.com/slackhq/nebula/e2e/router"
  11. "github.com/slackhq/nebula/header"
  12. "github.com/slackhq/nebula/udp"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. // reportedAddrs is what the lighthouse would hand a peer asking where vpnAddr is.
  17. func reportedAddrs(t *testing.T, lh *nebula.Control, vpnAddr netip.Addr) []netip.AddrPort {
  18. t.Helper()
  19. cm := lh.QueryLighthouse(vpnAddr)
  20. if cm == nil {
  21. return nil
  22. }
  23. var out []netip.AddrPort
  24. for _, c := range *cm {
  25. out = append(out, c.Reported...)
  26. out = append(out, c.Learned...)
  27. }
  28. return out
  29. }
  30. // waitForLighthouseMsg routes until a lighthouse message lands on lh, or gives up. Reports whether one arrived.
  31. func waitForLighthouseMsg(t *testing.T, r *router.R, lh *nebula.Control, wait time.Duration) bool {
  32. t.Helper()
  33. h := &header.H{}
  34. return r.RouteForAllExitFuncOrTimeout(wait, func(p *udp.Packet, c *nebula.Control) router.ExitType {
  35. if c != lh {
  36. return router.KeepRouting
  37. }
  38. // Punches are a single byte and never parse, they are just not what we are after
  39. if err := h.Parse(p.Data); err != nil {
  40. return router.KeepRouting
  41. }
  42. if h.Type == header.LightHouse {
  43. return router.RouteAndExit
  44. }
  45. return router.KeepRouting
  46. })
  47. }
  48. // A laptop that changes networks has to tell the lighthouse promptly, otherwise the lighthouse keeps handing peers
  49. // the old address and their punches land nowhere. On a long lighthouse interval the only thing that closes that
  50. // window is the rebind, which on darwin the network change monitor drives. The e2e build compiles the monitor out,
  51. // so we call RebindUDPServer directly, which is the same thing the monitor does.
  52. func TestRebindSendsLighthouseUpdate(t *testing.T) {
  53. t.Parallel()
  54. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  55. lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh", "10.128.0.1/24", m{
  56. "lighthouse": m{"am_lighthouse": true},
  57. })
  58. // 600s interval, so nothing scheduled can send an update during this test. A rebind is the only thing that can.
  59. myControl, _, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.2/24", m{
  60. "lighthouse": m{
  61. "hosts": []any{lhVpnIpNet[0].Addr().String()},
  62. "interval": 600,
  63. },
  64. "static_host_map": m{
  65. lhVpnIpNet[0].Addr().String(): []any{lhUdpAddr.String()},
  66. },
  67. })
  68. r := router.NewR(t, lhControl, myControl)
  69. defer r.RenderFlow()
  70. lhControl.Start()
  71. myControl.Start()
  72. // Let the startup registration finish, then clear everything it left behind
  73. require.True(t, waitForLighthouseMsg(t, r, lhControl, time.Second*5), "expected an initial registration")
  74. r.RouteFor(time.Millisecond * 400)
  75. // Nothing should be talking to the lighthouse on its own now
  76. require.False(t, waitForLighthouseMsg(t, r, lhControl, time.Millisecond*200),
  77. "nothing should reach the lighthouse before the rebind")
  78. myControl.RebindUDPServer()
  79. assert.True(t, waitForLighthouseMsg(t, r, lhControl, time.Second*5),
  80. "a rebind should push an update to the lighthouse rather than waiting out the interval")
  81. lhControl.Stop()
  82. myControl.Stop()
  83. }
  84. // The other half of a rebind: every live tunnel requeries the lighthouse on its next send. That query is what makes
  85. // the lighthouse tell the peer to punch toward our new address, which is the part that actually revives a tunnel
  86. // whose remote NAT state died while we were on a different network.
  87. func TestRebindRequeriesPeersOnNextSend(t *testing.T) {
  88. t.Parallel()
  89. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  90. lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh", "10.128.0.1/24", m{
  91. "lighthouse": m{"am_lighthouse": true},
  92. })
  93. lhCfg := m{
  94. "lighthouse": m{
  95. "hosts": []any{lhVpnIpNet[0].Addr().String()},
  96. "interval": 600,
  97. // Without this the peers advertise this machine's real addresses and then try to punch at them,
  98. // which the router has no route for.
  99. "local_allow_list": m{
  100. "10.0.0.0/24": true,
  101. "::/0": false,
  102. },
  103. },
  104. "static_host_map": m{
  105. lhVpnIpNet[0].Addr().String(): []any{lhUdpAddr.String()},
  106. },
  107. }
  108. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.2/24", lhCfg)
  109. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.3/24", lhCfg)
  110. r := router.NewR(t, lhControl, myControl, theirControl)
  111. defer r.RenderFlow()
  112. lhControl.Start()
  113. myControl.Start()
  114. theirControl.Start()
  115. r.RouteFor(time.Millisecond * 500)
  116. // Point the peers at each other directly, this test is about the rebind and not about lighthouse discovery
  117. myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
  118. theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
  119. myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("initial")))
  120. r.RouteFor(time.Second)
  121. require.NotNil(t, myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false), "expected a tunnel to them")
  122. r.RouteFor(time.Millisecond * 300)
  123. // Assert on what the peer sees rather than on lighthouse traffic. A query for them makes the lighthouse send
  124. // them a punch notification, which is the whole point. Our own update to the lighthouse sends them nothing,
  125. // so this cannot be satisfied by the update the rebind itself pushes.
  126. myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("quiet")))
  127. require.False(t, waitForLighthouseMsg(t, r, theirControl, time.Millisecond*300),
  128. "an ordinary send should not requery the lighthouse")
  129. myControl.RebindUDPServer()
  130. r.RouteFor(time.Millisecond * 300) // let the update the rebind itself sends pass by
  131. myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("after rebind")))
  132. assert.True(t, waitForLighthouseMsg(t, r, theirControl, time.Second*5),
  133. "the first send after a rebind should requery the lighthouse, which then tells the peer to punch at us")
  134. lhControl.Stop()
  135. myControl.Stop()
  136. theirControl.Stop()
  137. }
  138. // The scenario this whole thing exists for: a laptop sleeps at the office and wakes up at home on a new address.
  139. // Until it tells the lighthouse, the lighthouse keeps handing peers the office address, so their punches land
  140. // nowhere and the tunnel stays dead. On a long interval the rebind is the only thing that closes that window.
  141. func TestRebindAdvertisesNewAddressAfterMove(t *testing.T) {
  142. t.Parallel()
  143. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  144. lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh", "10.128.0.1/24", m{
  145. "lighthouse": m{"am_lighthouse": true},
  146. })
  147. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.2/24", m{
  148. "lighthouse": m{
  149. "hosts": []any{lhVpnIpNet[0].Addr().String()},
  150. "interval": 600,
  151. },
  152. "static_host_map": m{
  153. lhVpnIpNet[0].Addr().String(): []any{lhUdpAddr.String()},
  154. },
  155. })
  156. // Advertise wherever we currently are rather than this machine's real NICs, read fresh each time so a move
  157. // is picked up.
  158. myControl.SetLocalAddrsFn(func(*nebula.LocalAllowList) []netip.Addr {
  159. return []netip.Addr{myControl.GetUDPAddr().Addr()}
  160. })
  161. r := router.NewR(t, lhControl, myControl)
  162. defer r.RenderFlow()
  163. lhControl.Start()
  164. myControl.Start()
  165. require.True(t, waitForLighthouseMsg(t, r, lhControl, time.Second*5), "expected an initial registration")
  166. r.RouteFor(time.Millisecond * 400)
  167. require.Contains(t, reportedAddrs(t, lhControl, myVpnIpNet[0].Addr()), myUdpAddr,
  168. "the lighthouse should know the address we started on")
  169. // Wake up somewhere else
  170. newAddr := netip.MustParseAddrPort("10.0.0.99:4242")
  171. myControl.SetUDPAddr(newAddr)
  172. r.AddRoute(newAddr.Addr(), newAddr.Port(), myControl)
  173. // Nothing has told the lighthouse, and with interval 600 nothing scheduled will
  174. r.RouteFor(time.Millisecond * 400)
  175. require.NotContains(t, reportedAddrs(t, lhControl, myVpnIpNet[0].Addr()), newAddr,
  176. "the lighthouse should still be handing out the old address before the rebind")
  177. myControl.RebindUDPServer()
  178. require.True(t, waitForLighthouseMsg(t, r, lhControl, time.Second*5), "expected an update after the rebind")
  179. r.RouteFor(time.Millisecond * 400)
  180. assert.Contains(t, reportedAddrs(t, lhControl, myVpnIpNet[0].Addr()), newAddr,
  181. "after the rebind the lighthouse should hand peers our new address")
  182. lhControl.Stop()
  183. myControl.Stop()
  184. }
  185. // A relayed send records traffic but must not consume the rebind epoch. If it does, the next direct send to the
  186. // relay host sees the epoch already current and never requeries, so the far side is never told to punch at our
  187. // new address. This pins the SendVia call site, which the unit tests cannot reach.
  188. func TestRebindRequeriesAfterRelayedSend(t *testing.T) {
  189. t.Parallel()
  190. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  191. // No lighthouse on purpose: it would hand out a direct address for them and nothing would relay.
  192. // Long connection manager timers so it never fires a direct test packet at the relay tunnel and bumps its
  193. // epoch mid-test, which is the only other thing that touches that tunnel and would flake the assertion below.
  194. myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.1/24",
  195. m{"relay": m{"use_relays": true}, "timers": m{"connection_alive_interval": 3600, "pending_deletion_interval": 3600}})
  196. relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
  197. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
  198. myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
  199. myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
  200. relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
  201. r := router.NewR(t, myControl, relayControl, theirControl)
  202. defer r.RenderFlow()
  203. myControl.Start()
  204. relayControl.Start()
  205. theirControl.Start()
  206. myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("establish")))
  207. r.RouteForAllUntilTxTun(theirControl)
  208. r.RouteFor(time.Millisecond * 500)
  209. hi := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
  210. require.NotNil(t, hi, "expected a tunnel to them")
  211. require.NotEmpty(t, hi.CurrentRelaysToMe, "them must be reachable only via the relay for this test to mean anything")
  212. // sendNoMetrics only reaches SendVia when there is no direct remote, so pin that too. Without this the test
  213. // keeps passing while quietly sending direct and never exercising the relay path.
  214. require.False(t, hi.CurrentRemote.IsValid(), "them must have no direct remote, otherwise SendVia is never called")
  215. before, ok := myControl.GetRebindEpochFor(relayVpnIpNet[0].Addr())
  216. require.True(t, ok, "expected a tunnel to the relay")
  217. myControl.RebindUDPServer()
  218. // Traffic to them goes through SendVia on the relay tunnel. That must record traffic without consuming the
  219. // relay tunnel's own epoch edge, which belongs to the direct path.
  220. myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("relayed")))
  221. r.RouteForAllUntilTxTun(theirControl)
  222. after, ok := myControl.GetRebindEpochFor(relayVpnIpNet[0].Addr())
  223. require.True(t, ok)
  224. assert.Equal(t, before, after,
  225. "a relayed send consumed the relay tunnel's rebind epoch, so the next direct send will not requery")
  226. myControl.Stop()
  227. relayControl.Stop()
  228. theirControl.Stop()
  229. }