| 1 |
//go:build e2e_testing |
| 2 |
// +build e2e_testing |
| 3 |
|
| 4 |
package e2e |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"net/netip" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 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 |
"gopkg.in/yaml.v3" |
| 20 |
) |
| 21 |
|
| 22 |
func TestDropInactiveTunnels(t *testing.T) { |
| 23 |
t.Parallel() |
| 24 |
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides |
| 25 |
// under ideal conditions |
| 26 |
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 27 |
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "5s"}}) |
| 28 |
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "10m"}}) |
| 29 |
|
| 30 |
// Share our underlay information |
| 31 |
myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr) |
| 32 |
theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr) |
| 33 |
|
| 34 |
// Start the servers |
| 35 |
myControl.Start() |
| 36 |
theirControl.Start() |
| 37 |
|
| 38 |
r := router.NewR(t, myControl, theirControl) |
| 39 |
|
| 40 |
r.Log("Assert the tunnel between me and them works") |
| 41 |
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r) |
| 42 |
|
| 43 |
r.Log("Go inactive and wait for the tunnels to get dropped") |
| 44 |
waitStart := time.Now() |
| 45 |
for { |
| 46 |
myIndexes := myControl.GetHostmapIndexCount() |
| 47 |
theirIndexes := theirControl.GetHostmapIndexCount() |
| 48 |
if myIndexes == 0 && theirIndexes == 0 { |
| 49 |
break |
| 50 |
} |
| 51 |
|
| 52 |
since := time.Since(waitStart) |
| 53 |
r.Logf("my tunnels: %v; their tunnels: %v; duration: %v", myIndexes, theirIndexes, since) |
| 54 |
if since > time.Second*30 { |
| 55 |
t.Fatal("Tunnel should have been declared inactive after 5 seconds and before 30 seconds") |
| 56 |
} |
| 57 |
|
| 58 |
time.Sleep(1 * time.Second) |
| 59 |
r.FlushAll() |
| 60 |
} |
| 61 |
|
| 62 |
r.Logf("Inactive tunnels were dropped within %v", time.Since(waitStart)) |
| 63 |
myControl.Stop() |
| 64 |
theirControl.Stop() |
| 65 |
} |
| 66 |
|
| 67 |
func TestCertUpgrade(t *testing.T) { |
| 68 |
t.Parallel() |
| 69 |
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides |
| 70 |
// under ideal conditions |
| 71 |
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 72 |
caB, err := ca.MarshalPEM() |
| 73 |
if err != nil { |
| 74 |
panic(err) |
| 75 |
} |
| 76 |
ca2, _, caKey2, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 77 |
|
| 78 |
ca2B, err := ca2.MarshalPEM() |
| 79 |
if err != nil { |
| 80 |
panic(err) |
| 81 |
} |
| 82 |
caStr := fmt.Sprintf("%s\n%s", caB, ca2B) |
| 83 |
|
| 84 |
myCert, _, myPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.1/24")}, nil, []string{}) |
| 85 |
_, myCert2Pem := cert_test.NewTestCertDifferentVersion(myCert, cert.Version2, ca2, caKey2) |
| 86 |
|
| 87 |
theirCert, _, theirPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.2/24")}, nil, []string{}) |
| 88 |
theirCert2, _ := cert_test.NewTestCertDifferentVersion(theirCert, cert.Version2, ca2, caKey2) |
| 89 |
|
| 90 |
myControl, myVpnIpNet, myUdpAddr, myC := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{myCert}, myPrivKey, m{}) |
| 91 |
theirControl, theirVpnIpNet, theirUdpAddr, _ := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{theirCert, theirCert2}, theirPrivKey, m{}) |
| 92 |
|
| 93 |
// Share our underlay information |
| 94 |
myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr) |
| 95 |
theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr) |
| 96 |
|
| 97 |
// Start the servers |
| 98 |
myControl.Start() |
| 99 |
theirControl.Start() |
| 100 |
|
| 101 |
r := router.NewR(t, myControl, theirControl) |
| 102 |
defer r.RenderFlow() |
| 103 |
|
| 104 |
r.Log("Assert the tunnel between me and them works") |
| 105 |
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r) |
| 106 |
r.Log("yay") |
| 107 |
//todo ??? |
| 108 |
time.Sleep(1 * time.Second) |
| 109 |
r.FlushAll() |
| 110 |
|
| 111 |
mc := m{ |
| 112 |
"pki": m{ |
| 113 |
"ca": caStr, |
| 114 |
"cert": string(myCert2Pem), |
| 115 |
"key": string(myPrivKey), |
| 116 |
}, |
| 117 |
//"tun": m{"disabled": true}, |
| 118 |
"firewall": myC.Settings["firewall"], |
| 119 |
//"handshakes": m{ |
| 120 |
// "try_interval": "1s", |
| 121 |
//}, |
| 122 |
"listen": myC.Settings["listen"], |
| 123 |
"logging": myC.Settings["logging"], |
| 124 |
"timers": myC.Settings["timers"], |
| 125 |
} |
| 126 |
|
| 127 |
cb, err := yaml.Marshal(mc) |
| 128 |
if err != nil { |
| 129 |
panic(err) |
| 130 |
} |
| 131 |
|
| 132 |
r.Logf("reload new v2-only config") |
| 133 |
err = myC.ReloadConfigString(string(cb)) |
| 134 |
assert.NoError(t, err) |
| 135 |
r.Log("yay, spin until their sees it") |
| 136 |
waitStart := time.Now() |
| 137 |
for { |
| 138 |
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r) |
| 139 |
c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false) |
| 140 |
if c == nil { |
| 141 |
r.Log("nil") |
| 142 |
} else { |
| 143 |
version := c.Cert.Version() |
| 144 |
r.Logf("version %d", version) |
| 145 |
if version == cert.Version2 { |
| 146 |
break |
| 147 |
} |
| 148 |
} |
| 149 |
since := time.Since(waitStart) |
| 150 |
if since > time.Second*10 { |
| 151 |
t.Fatal("Cert should be new by now") |
| 152 |
} |
| 153 |
time.Sleep(time.Second) |
| 154 |
} |
| 155 |
|
| 156 |
r.RenderHostmaps("Final hostmaps", myControl, theirControl) |
| 157 |
|
| 158 |
myControl.Stop() |
| 159 |
theirControl.Stop() |
| 160 |
} |
| 161 |
|
| 162 |
func TestCertDowngrade(t *testing.T) { |
| 163 |
t.Parallel() |
| 164 |
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides |
| 165 |
// under ideal conditions |
| 166 |
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 167 |
caB, err := ca.MarshalPEM() |
| 168 |
if err != nil { |
| 169 |
panic(err) |
| 170 |
} |
| 171 |
ca2, _, caKey2, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 172 |
|
| 173 |
ca2B, err := ca2.MarshalPEM() |
| 174 |
if err != nil { |
| 175 |
panic(err) |
| 176 |
} |
| 177 |
caStr := fmt.Sprintf("%s\n%s", caB, ca2B) |
| 178 |
|
| 179 |
myCert, _, myPrivKey, myCertPem := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.1/24")}, nil, []string{}) |
| 180 |
myCert2, _ := cert_test.NewTestCertDifferentVersion(myCert, cert.Version2, ca2, caKey2) |
| 181 |
|
| 182 |
theirCert, _, theirPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.2/24")}, nil, []string{}) |
| 183 |
theirCert2, _ := cert_test.NewTestCertDifferentVersion(theirCert, cert.Version2, ca2, caKey2) |
| 184 |
|
| 185 |
myControl, myVpnIpNet, myUdpAddr, myC := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{myCert2}, myPrivKey, m{}) |
| 186 |
theirControl, theirVpnIpNet, theirUdpAddr, _ := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{theirCert, theirCert2}, theirPrivKey, m{}) |
| 187 |
|
| 188 |
// Share our underlay information |
| 189 |
myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr) |
| 190 |
theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr) |
| 191 |
|
| 192 |
// Start the servers |
| 193 |
myControl.Start() |
| 194 |
theirControl.Start() |
| 195 |
|
| 196 |
r := router.NewR(t, myControl, theirControl) |
| 197 |
defer r.RenderFlow() |
| 198 |
|
| 199 |
r.Log("Assert the tunnel between me and them works") |
| 200 |
//assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r) |
| 201 |
//r.Log("yay") |
| 202 |
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r) |
| 203 |
r.Log("yay") |
| 204 |
//todo ??? |
| 205 |
time.Sleep(1 * time.Second) |
| 206 |
r.FlushAll() |
| 207 |
|
| 208 |
mc := m{ |
| 209 |
"pki": m{ |
| 210 |
"ca": caStr, |
| 211 |
"cert": string(myCertPem), |
| 212 |
"key": string(myPrivKey), |
| 213 |
}, |
| 214 |
"firewall": myC.Settings["firewall"], |
| 215 |
"listen": myC.Settings["listen"], |
| 216 |
"logging": myC.Settings["logging"], |
| 217 |
"timers": myC.Settings["timers"], |
| 218 |
} |
| 219 |
|
| 220 |
cb, err := yaml.Marshal(mc) |
| 221 |
if err != nil { |
| 222 |
panic(err) |
| 223 |
} |
| 224 |
|
| 225 |
r.Logf("reload new v1-only config") |
| 226 |
err = myC.ReloadConfigString(string(cb)) |
| 227 |
assert.NoError(t, err) |
| 228 |
r.Log("yay, spin until their sees it") |
| 229 |
waitStart := time.Now() |
| 230 |
for { |
| 231 |
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r) |
| 232 |
c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false) |
| 233 |
c2 := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false) |
| 234 |
if c == nil || c2 == nil { |
| 235 |
r.Log("nil") |
| 236 |
} else { |
| 237 |
version := c.Cert.Version() |
| 238 |
theirVersion := c2.Cert.Version() |
| 239 |
r.Logf("version %d,%d", version, theirVersion) |
| 240 |
if version == cert.Version1 { |
| 241 |
break |
| 242 |
} |
| 243 |
} |
| 244 |
since := time.Since(waitStart) |
| 245 |
if since > time.Second*5 { |
| 246 |
r.Log("it is unusual that the cert is not new yet, but not a failure yet") |
| 247 |
} |
| 248 |
if since > time.Second*10 { |
| 249 |
r.Log("wtf") |
| 250 |
t.Fatal("Cert should be new by now") |
| 251 |
} |
| 252 |
time.Sleep(time.Second) |
| 253 |
} |
| 254 |
|
| 255 |
r.RenderHostmaps("Final hostmaps", myControl, theirControl) |
| 256 |
|
| 257 |
myControl.Stop() |
| 258 |
theirControl.Stop() |
| 259 |
} |
| 260 |
|
| 261 |
func TestCertMismatchCorrection(t *testing.T) { |
| 262 |
t.Parallel() |
| 263 |
// The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides |
| 264 |
// under ideal conditions |
| 265 |
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 266 |
ca2, _, caKey2, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 267 |
|
| 268 |
myCert, _, myPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.1/24")}, nil, []string{}) |
| 269 |
myCert2, _ := cert_test.NewTestCertDifferentVersion(myCert, cert.Version2, ca2, caKey2) |
| 270 |
|
| 271 |
theirCert, _, theirPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.2/24")}, nil, []string{}) |
| 272 |
theirCert2, _ := cert_test.NewTestCertDifferentVersion(theirCert, cert.Version2, ca2, caKey2) |
| 273 |
|
| 274 |
myControl, myVpnIpNet, myUdpAddr, _ := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{myCert2}, myPrivKey, m{}) |
| 275 |
theirControl, theirVpnIpNet, theirUdpAddr, _ := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{theirCert, theirCert2}, theirPrivKey, m{}) |
| 276 |
|
| 277 |
// Share our underlay information |
| 278 |
myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr) |
| 279 |
theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr) |
| 280 |
|
| 281 |
// Start the servers |
| 282 |
myControl.Start() |
| 283 |
theirControl.Start() |
| 284 |
|
| 285 |
r := router.NewR(t, myControl, theirControl) |
| 286 |
defer r.RenderFlow() |
| 287 |
|
| 288 |
r.Log("Assert the tunnel between me and them works") |
| 289 |
//assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r) |
| 290 |
//r.Log("yay") |
| 291 |
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r) |
| 292 |
r.Log("yay") |
| 293 |
//todo ??? |
| 294 |
time.Sleep(1 * time.Second) |
| 295 |
r.FlushAll() |
| 296 |
|
| 297 |
waitStart := time.Now() |
| 298 |
for { |
| 299 |
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r) |
| 300 |
c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false) |
| 301 |
c2 := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false) |
| 302 |
if c == nil || c2 == nil { |
| 303 |
r.Log("nil") |
| 304 |
} else { |
| 305 |
version := c.Cert.Version() |
| 306 |
theirVersion := c2.Cert.Version() |
| 307 |
r.Logf("version %d,%d", version, theirVersion) |
| 308 |
if version == theirVersion { |
| 309 |
break |
| 310 |
} |
| 311 |
} |
| 312 |
since := time.Since(waitStart) |
| 313 |
if since > time.Second*5 { |
| 314 |
r.Log("wtf") |
| 315 |
} |
| 316 |
if since > time.Second*10 { |
| 317 |
r.Log("wtf") |
| 318 |
t.Fatal("Cert should be new by now") |
| 319 |
} |
| 320 |
time.Sleep(time.Second) |
| 321 |
} |
| 322 |
|
| 323 |
r.RenderHostmaps("Final hostmaps", myControl, theirControl) |
| 324 |
|
| 325 |
myControl.Stop() |
| 326 |
theirControl.Stop() |
| 327 |
} |
| 328 |
|
| 329 |
func TestCrossStackRelaysWork(t *testing.T) { |
| 330 |
t.Parallel() |
| 331 |
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 332 |
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24,fc00::1/64", m{"relay": m{"use_relays": true}}) |
| 333 |
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "10.128.0.128/24,fc00::128/64", m{"relay": m{"am_relay": true}}) |
| 334 |
theirUdp := netip.MustParseAddrPort("10.0.0.2:4242") |
| 335 |
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServerWithUdp(cert.Version2, ca, caKey, "them ", "fc00::2/64", theirUdp, m{"relay": m{"use_relays": true}}) |
| 336 |
|
| 337 |
//myVpnV4 := myVpnIpNet[0] |
| 338 |
myVpnV6 := myVpnIpNet[1] |
| 339 |
relayVpnV4 := relayVpnIpNet[0] |
| 340 |
relayVpnV6 := relayVpnIpNet[1] |
| 341 |
theirVpnV6 := theirVpnIpNet[0] |
| 342 |
|
| 343 |
// Teach my how to get to the relay and that their can be reached via the relay |
| 344 |
myControl.InjectLightHouseAddr(relayVpnV4.Addr(), relayUdpAddr) |
| 345 |
myControl.InjectLightHouseAddr(relayVpnV6.Addr(), relayUdpAddr) |
| 346 |
myControl.InjectRelays(theirVpnV6.Addr(), []netip.Addr{relayVpnV6.Addr()}) |
| 347 |
relayControl.InjectLightHouseAddr(theirVpnV6.Addr(), theirUdpAddr) |
| 348 |
|
| 349 |
// Build a router so we don't have to reason who gets which packet |
| 350 |
r := router.NewR(t, myControl, relayControl, theirControl) |
| 351 |
defer r.RenderFlow() |
| 352 |
|
| 353 |
// Start the servers |
| 354 |
myControl.Start() |
| 355 |
relayControl.Start() |
| 356 |
theirControl.Start() |
| 357 |
|
| 358 |
t.Log("Trigger a handshake from me to them via the relay") |
| 359 |
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnV6.Addr(), 80, myVpnV6.Addr(), 80, []byte("Hi from me"))) |
| 360 |
|
| 361 |
p := r.RouteForAllUntilTxTun(theirControl) |
| 362 |
r.Log("Assert the tunnel works") |
| 363 |
assertUdpPacket(t, []byte("Hi from me"), p, myVpnV6.Addr(), theirVpnV6.Addr(), 80, 80) |
| 364 |
|
| 365 |
t.Log("reply?") |
| 366 |
theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnV6.Addr(), 80, theirVpnV6.Addr(), 80, []byte("Hi from them"))) |
| 367 |
p = r.RouteForAllUntilTxTun(myControl) |
| 368 |
assertUdpPacket(t, []byte("Hi from them"), p, theirVpnV6.Addr(), myVpnV6.Addr(), 80, 80) |
| 369 |
|
| 370 |
r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl) |
| 371 |
//t.Log("finish up") |
| 372 |
//myControl.Stop() |
| 373 |
//theirControl.Stop() |
| 374 |
//relayControl.Stop() |
| 375 |
} |
| 376 |
|
| 377 |
// TestRelayReplayProtection asserts that a relay (forwarding-type) node rejects |
| 378 |
// replayed relay frames. A captured relay frame, re-injected with the same |
| 379 |
// message counter, must be dropped by the replay window rather than re-forwarded |
| 380 |
// to the relay target. Before the fix, handleOutsideRelayPacket authenticated the |
| 381 |
// frame but never advanced the replay window, so every replay was re-forwarded. |
| 382 |
func TestRelayReplayProtection(t *testing.T) { |
| 383 |
t.Parallel() |
| 384 |
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 385 |
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24,fc00::1/64", m{"relay": m{"use_relays": true}}) |
| 386 |
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "10.128.0.128/24,fc00::128/64", m{"relay": m{"am_relay": true}}) |
| 387 |
theirUdp := netip.MustParseAddrPort("10.0.0.2:4242") |
| 388 |
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServerWithUdp(cert.Version2, ca, caKey, "them ", "fc00::2/64", theirUdp, m{"relay": m{"use_relays": true}}) |
| 389 |
|
| 390 |
myVpnV6 := myVpnIpNet[1] |
| 391 |
relayVpnV4 := relayVpnIpNet[0] |
| 392 |
relayVpnV6 := relayVpnIpNet[1] |
| 393 |
theirVpnV6 := theirVpnIpNet[0] |
| 394 |
|
| 395 |
// Teach me how to reach the relay and that them is reachable via the relay |
| 396 |
myControl.InjectLightHouseAddr(relayVpnV4.Addr(), relayUdpAddr) |
| 397 |
myControl.InjectLightHouseAddr(relayVpnV6.Addr(), relayUdpAddr) |
| 398 |
myControl.InjectRelays(theirVpnV6.Addr(), []netip.Addr{relayVpnV6.Addr()}) |
| 399 |
relayControl.InjectLightHouseAddr(theirVpnV6.Addr(), theirUdpAddr) |
| 400 |
|
| 401 |
r := router.NewR(t, myControl, relayControl, theirControl) |
| 402 |
defer r.RenderFlow() |
| 403 |
|
| 404 |
myControl.Start() |
| 405 |
relayControl.Start() |
| 406 |
theirControl.Start() |
| 407 |
|
| 408 |
// Establish the relayed tunnel in both directions so all handshakes complete. |
| 409 |
t.Log("Establish the relayed tunnel") |
| 410 |
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnV6.Addr(), 80, myVpnV6.Addr(), 80, []byte("Hi from me"))) |
| 411 |
p := r.RouteForAllUntilTxTun(theirControl) |
| 412 |
assertUdpPacket(t, []byte("Hi from me"), p, myVpnV6.Addr(), theirVpnV6.Addr(), 80, 80) |
| 413 |
theirControl.InjectTunPacket(BuildTunUDPPacket(myVpnV6.Addr(), 80, theirVpnV6.Addr(), 80, []byte("Hi from them"))) |
| 414 |
p = r.RouteForAllUntilTxTun(myControl) |
| 415 |
assertUdpPacket(t, []byte("Hi from them"), p, theirVpnV6.Addr(), myVpnV6.Addr(), 80, 80) |
| 416 |
|
| 417 |
// Drain anything still queued on me's UDP tx so the next packet we pull is the |
| 418 |
// relay frame we are about to generate. |
| 419 |
for myControl.GetFromUDP(false) != nil { |
| 420 |
} |
| 421 |
|
| 422 |
// Capture a single legitimate relay frame that me transmits toward the relay. |
| 423 |
t.Log("Capture a relay frame from me -> relay") |
| 424 |
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnV6.Addr(), 80, myVpnV6.Addr(), 80, []byte("replay me"))) |
| 425 |
relayFrame := myControl.GetFromUDP(true) |
| 426 |
require.Equal(t, relayUdpAddr, relayFrame.To, "captured frame should be addressed to the relay") |
| 427 |
var fh header.H |
| 428 |
require.NoError(t, fh.Parse(relayFrame.Data)) |
| 429 |
require.Equal(t, header.Message, fh.Type) |
| 430 |
require.Equal(t, header.MessageRelay, fh.Subtype) |
| 431 |
|
| 432 |
// drainForwards counts relay frames the relay forwards toward them within the |
| 433 |
// settle window. We match on destination + (Message, MessageRelay) so the |
| 434 |
// relay's own direct traffic to them can't be miscounted. |
| 435 |
drainForwards := func(settle time.Duration) int { |
| 436 |
ch := relayControl.GetUDPTxChan() |
| 437 |
count := 0 |
| 438 |
for { |
| 439 |
select { |
| 440 |
case pkt := <-ch: |
| 441 |
var ph header.H |
| 442 |
if pkt.To == theirUdpAddr && ph.Parse(pkt.Data) == nil && |
| 443 |
ph.Type == header.Message && ph.Subtype == header.MessageRelay { |
| 444 |
count++ |
| 445 |
} |
| 446 |
pkt.Release() |
| 447 |
case <-time.After(settle): |
| 448 |
return count |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
// First delivery of the captured frame: the relay should forward it once. |
| 454 |
t.Log("Deliver the captured frame once; relay forwards it to them") |
| 455 |
relayControl.InjectUDPPacket(relayFrame) |
| 456 |
require.Equal(t, 1, drainForwards(200*time.Millisecond), "relay should forward the first, legitimate copy") |
| 457 |
|
| 458 |
// Replay the exact same frame several times. A correct replay window rejects |
| 459 |
// these duplicates so the relay forwards none of them. |
| 460 |
t.Log("Replay the captured frame; relay must drop the duplicates") |
| 461 |
const replays = 3 |
| 462 |
for i := 0; i < replays; i++ { |
| 463 |
relayControl.InjectUDPPacket(relayFrame) |
| 464 |
} |
| 465 |
forwarded := drainForwards(200 * time.Millisecond) |
| 466 |
assert.Equal(t, 0, forwarded, "relay re-forwarded %d/%d replayed relay frames; replay protection is ineffective on relay tunnels", forwarded, replays) |
| 467 |
|
| 468 |
r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl) |
| 469 |
} |
| 470 |
|
| 471 |
func TestCloseTunnelAuthenticated(t *testing.T) { |
| 472 |
t.Parallel() |
| 473 |
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{}) |
| 474 |
myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "5s"}}) |
| 475 |
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "10m"}}) |
| 476 |
|
| 477 |
// Share our underlay information |
| 478 |
myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr) |
| 479 |
theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr) |
| 480 |
|
| 481 |
// Start the servers |
| 482 |
myControl.Start() |
| 483 |
theirControl.Start() |
| 484 |
|
| 485 |
r := router.NewR(t, myControl, theirControl) |
| 486 |
|
| 487 |
r.Log("Assert the tunnel between me and them works") |
| 488 |
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r) |
| 489 |
|
| 490 |
r.Log("Close the tunnel") |
| 491 |
myControl.CloseTunnel(theirVpnIpNet[0].Addr(), false) |
| 492 |
r.FlushAll() |
| 493 |
|
| 494 |
waitStart := time.Now() |
| 495 |
for { |
| 496 |
myIndexes := myControl.GetHostmapIndexCount() |
| 497 |
theirIndexes := theirControl.GetHostmapIndexCount() |
| 498 |
if myIndexes == 0 && theirIndexes == 0 { |
| 499 |
break |
| 500 |
} |
| 501 |
|
| 502 |
since := time.Since(waitStart) |
| 503 |
r.Logf("my tunnels: %v; their tunnels: %v; duration: %v", myIndexes, theirIndexes, since) |
| 504 |
if since > time.Second*6 { |
| 505 |
t.Fatal("Tunnel should have been declared inactive after 2 seconds and before 6 seconds") |
| 506 |
} |
| 507 |
|
| 508 |
time.Sleep(1 * time.Second) |
| 509 |
//r.FlushAll() |
| 510 |
} |
| 511 |
|
| 512 |
r.Logf("Happy path success, tunnels were dropped within %v", time.Since(waitStart)) |
| 513 |
|
| 514 |
myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr) |
| 515 |
theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr) |
| 516 |
r.Log("Assert another tunnel between me and them works") |
| 517 |
assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r) |
| 518 |
hi := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false) |
| 519 |
if hi == nil { |
| 520 |
t.Fatal("There is no hostinfo for this tunnel") |
| 521 |
} |
| 522 |
myHi := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false) |
| 523 |
if myHi == nil { |
| 524 |
t.Fatal("There is no hostinfo for my tunnel") |
| 525 |
} |
| 526 |
r.Log("It does") |
| 527 |
|
| 528 |
buf := make([]byte, 1024) |
| 529 |
hdr := header.H{ |
| 530 |
Version: 1, |
| 531 |
Type: header.CloseTunnel, |
| 532 |
Subtype: 0, |
| 533 |
Reserved: 0, |
| 534 |
RemoteIndex: hi.RemoteIndex, |
| 535 |
MessageCounter: 5, |
| 536 |
} |
| 537 |
out, err := hdr.Encode(buf) |
| 538 |
if err != nil { |
| 539 |
t.Fatal(err) |
| 540 |
} |
| 541 |
|
| 542 |
pkt := &udp.Packet{ |
| 543 |
To: hi.CurrentRemote, |
| 544 |
From: myHi.CurrentRemote, |
| 545 |
Data: out, |
| 546 |
} |
| 547 |
r.InjectUDPPacket(myControl, theirControl, pkt) |
| 548 |
r.Log("Injected bogus close tunnel. Let's see!") |
| 549 |
waitStart = time.Now() |
| 550 |
for { |
| 551 |
myIndexes := myControl.GetHostmapIndexCount() |
| 552 |
theirIndexes := theirControl.GetHostmapIndexCount() |
| 553 |
if myIndexes == 0 { |
| 554 |
t.Fatal("myIndexes should not be 0") |
| 555 |
} |
| 556 |
if theirIndexes == 0 { |
| 557 |
t.Fatal("theirIndexes should not be 0, they should have rejected this bogus packet") |
| 558 |
} |
| 559 |
|
| 560 |
since := time.Since(waitStart) |
| 561 |
r.Logf("my tunnels: %v; their tunnels: %v; duration: %v", myIndexes, theirIndexes, since) |
| 562 |
if since > time.Second*4 { |
| 563 |
t.Log("The tunnel would have been gone by now") |
| 564 |
break |
| 565 |
} |
| 566 |
|
| 567 |
time.Sleep(1 * time.Second) |
| 568 |
r.FlushAll() |
| 569 |
} |
| 570 |
|
| 571 |
myControl.Stop() |
| 572 |
theirControl.Stop() |
| 573 |
} |