Debian Codegraph @

e2e/rebind_test.go

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