2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package magicsock
|
|
|
|
|
|
|
|
import (
|
2020-03-03 15:39:40 +00:00
|
|
|
"bytes"
|
2020-07-25 00:32:18 +00:00
|
|
|
"context"
|
2020-03-03 21:50:47 +00:00
|
|
|
crand "crypto/rand"
|
|
|
|
"crypto/tls"
|
2021-02-10 22:47:26 +00:00
|
|
|
"errors"
|
2020-02-05 22:16:58 +00:00
|
|
|
"fmt"
|
2020-07-04 05:26:53 +00:00
|
|
|
"io/ioutil"
|
2020-02-05 22:16:58 +00:00
|
|
|
"net"
|
2020-03-03 21:50:47 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-03-06 21:35:59 +00:00
|
|
|
"os"
|
2021-02-07 05:27:02 +00:00
|
|
|
"runtime"
|
2020-08-06 17:23:16 +00:00
|
|
|
"strconv"
|
2020-02-05 22:16:58 +00:00
|
|
|
"strings"
|
2020-05-14 03:44:58 +00:00
|
|
|
"sync"
|
2020-02-05 22:16:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-08-06 21:57:03 +00:00
|
|
|
"unsafe"
|
2020-03-03 11:51:31 +00:00
|
|
|
|
2020-06-26 21:38:53 +00:00
|
|
|
"golang.org/x/crypto/nacl/box"
|
2021-05-25 19:42:22 +00:00
|
|
|
"golang.zx2c4.com/wireguard/device"
|
|
|
|
"golang.zx2c4.com/wireguard/tun/tuntest"
|
2020-06-30 19:22:42 +00:00
|
|
|
"inet.af/netaddr"
|
2020-03-03 21:50:47 +00:00
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
2020-07-25 00:32:18 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2020-05-25 16:15:50 +00:00
|
|
|
"tailscale.com/net/stun/stuntest"
|
2021-03-27 05:14:08 +00:00
|
|
|
"tailscale.com/net/tstun"
|
2020-05-17 16:51:38 +00:00
|
|
|
"tailscale.com/tailcfg"
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
"tailscale.com/tstest"
|
2020-07-10 21:26:04 +00:00
|
|
|
"tailscale.com/tstest/natlab"
|
2020-03-03 21:50:47 +00:00
|
|
|
"tailscale.com/types/key"
|
2020-03-07 01:50:36 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2020-07-10 21:26:04 +00:00
|
|
|
"tailscale.com/types/nettype"
|
2020-12-30 01:22:56 +00:00
|
|
|
"tailscale.com/types/wgkey"
|
2021-02-10 19:49:30 +00:00
|
|
|
"tailscale.com/util/cibuild"
|
2021-09-15 23:43:44 +00:00
|
|
|
"tailscale.com/util/racebuild"
|
2020-05-13 13:16:17 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
2021-01-29 20:16:36 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
2021-02-05 20:44:43 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg/nmcfg"
|
2021-01-21 20:33:54 +00:00
|
|
|
"tailscale.com/wgengine/wglog"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-10-28 15:23:12 +00:00
|
|
|
func init() {
|
|
|
|
os.Setenv("IN_TS_TEST", "1")
|
|
|
|
}
|
|
|
|
|
2020-05-14 17:01:48 +00:00
|
|
|
// WaitReady waits until the magicsock is entirely initialized and connected
|
|
|
|
// to its home DERP server. This is normally not necessary, since magicsock
|
|
|
|
// is intended to be entirely asynchronous, but it helps eliminate race
|
|
|
|
// conditions in tests. In particular, you can't expect two test magicsocks
|
|
|
|
// to be able to connect to each other through a test DERP unless they are
|
|
|
|
// both fully initialized before you try.
|
2020-12-03 04:12:14 +00:00
|
|
|
func (c *Conn) WaitReady(t testing.TB) {
|
2020-05-14 17:01:48 +00:00
|
|
|
t.Helper()
|
|
|
|
timer := time.NewTimer(10 * time.Second)
|
|
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
|
|
case <-c.derpStarted:
|
|
|
|
return
|
|
|
|
case <-c.connCtx.Done():
|
|
|
|
t.Fatalf("magicsock.Conn closed while waiting for readiness")
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatalf("timeout waiting for readiness")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:19:20 +00:00
|
|
|
func runDERPAndStun(t *testing.T, logf logger.Logf, l nettype.PacketListener, stunIP netaddr.IP) (derpMap *tailcfg.DERPMap, cleanup func()) {
|
|
|
|
var serverPrivateKey key.Private
|
|
|
|
if _, err := crand.Read(serverPrivateKey[:]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
d := derp.NewServer(serverPrivateKey, logf)
|
|
|
|
|
|
|
|
httpsrv := httptest.NewUnstartedServer(derphttp.Handler(d))
|
|
|
|
httpsrv.Config.ErrorLog = logger.StdLogger(logf)
|
|
|
|
httpsrv.Config.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
|
|
|
|
httpsrv.StartTLS()
|
|
|
|
|
|
|
|
stunAddr, stunCleanup := stuntest.ServeWithPacketListener(t, l)
|
|
|
|
|
|
|
|
m := &tailcfg.DERPMap{
|
|
|
|
Regions: map[int]*tailcfg.DERPRegion{
|
2021-04-01 16:54:54 +00:00
|
|
|
1: {
|
2020-07-24 21:19:20 +00:00
|
|
|
RegionID: 1,
|
|
|
|
RegionCode: "test",
|
|
|
|
Nodes: []*tailcfg.DERPNode{
|
|
|
|
{
|
2021-07-09 18:16:43 +00:00
|
|
|
Name: "t1",
|
|
|
|
RegionID: 1,
|
|
|
|
HostName: "test-node.unused",
|
|
|
|
IPv4: "127.0.0.1",
|
|
|
|
IPv6: "none",
|
|
|
|
STUNPort: stunAddr.Port,
|
|
|
|
DERPPort: httpsrv.Listener.Addr().(*net.TCPAddr).Port,
|
|
|
|
InsecureForTests: true,
|
|
|
|
STUNTestIP: stunIP.String(),
|
2020-07-24 21:19:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() {
|
|
|
|
httpsrv.CloseClientConnections()
|
|
|
|
httpsrv.Close()
|
|
|
|
d.Close()
|
|
|
|
stunCleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
// magicStack is a magicsock, plus all the stuff around it that's
|
|
|
|
// necessary to send and receive packets to test e2e wireguard
|
|
|
|
// happiness.
|
|
|
|
type magicStack struct {
|
2020-12-30 01:22:56 +00:00
|
|
|
privateKey wgkey.Private
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
epCh chan []tailcfg.Endpoint // endpoint updates produced by this peer
|
|
|
|
conn *Conn // the magicsock itself
|
|
|
|
tun *tuntest.ChannelTUN // TUN device to send/receive packets
|
|
|
|
tsTun *tstun.Wrapper // wrapped tun that implements filtering and wgengine hooks
|
|
|
|
dev *device.Device // the wireguard-go Device that connects the previous things
|
|
|
|
wgLogger *wglog.Logger // wireguard-go log wrapper
|
2020-07-24 21:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newMagicStack builds and initializes an idle magicsock and
|
|
|
|
// friends. You need to call conn.SetNetworkMap and dev.Reconfig
|
|
|
|
// before anything interesting happens.
|
2021-08-26 02:39:20 +00:00
|
|
|
func newMagicStack(t testing.TB, logf logger.Logf, l nettype.PacketListener, derpMap *tailcfg.DERPMap) *magicStack {
|
2020-07-24 21:19:20 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-12-30 01:22:56 +00:00
|
|
|
privateKey, err := wgkey.NewPrivate()
|
2020-07-24 21:19:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("generating private key: %v", err)
|
|
|
|
}
|
|
|
|
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
epCh := make(chan []tailcfg.Endpoint, 100) // arbitrary
|
2020-07-24 21:19:20 +00:00
|
|
|
conn, err := NewConn(Options{
|
2021-08-26 05:26:25 +00:00
|
|
|
Logf: logf,
|
|
|
|
TestOnlyPacketListener: l,
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
EndpointsFunc: func(eps []tailcfg.Endpoint) {
|
2020-07-24 21:19:20 +00:00
|
|
|
epCh <- eps
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("constructing magicsock: %v", err)
|
|
|
|
}
|
|
|
|
conn.SetDERPMap(derpMap)
|
|
|
|
if err := conn.SetPrivateKey(privateKey); err != nil {
|
|
|
|
t.Fatalf("setting private key in magicsock: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tun := tuntest.NewChannelTUN()
|
2021-03-27 06:13:20 +00:00
|
|
|
tsTun := tstun.Wrap(logf, tun.TUN())
|
2020-11-10 06:02:03 +00:00
|
|
|
tsTun.SetFilter(filter.NewAllowAllForTest(logf))
|
2020-07-24 21:19:20 +00:00
|
|
|
|
2021-01-21 20:33:54 +00:00
|
|
|
wgLogger := wglog.NewLogger(logf)
|
2021-05-11 22:24:37 +00:00
|
|
|
dev := device.NewDevice(tsTun, conn.Bind(), wgLogger.DeviceLogger)
|
2020-07-24 21:19:20 +00:00
|
|
|
dev.Up()
|
|
|
|
|
|
|
|
// Wait for magicsock to connect up to DERP.
|
|
|
|
conn.WaitReady(t)
|
|
|
|
|
|
|
|
// Wait for first endpoint update to be available
|
|
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
|
|
for len(epCh) == 0 && time.Now().Before(deadline) {
|
2020-07-28 17:04:09 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-07-24 21:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &magicStack{
|
|
|
|
privateKey: privateKey,
|
|
|
|
epCh: epCh,
|
|
|
|
conn: conn,
|
|
|
|
tun: tun,
|
|
|
|
tsTun: tsTun,
|
|
|
|
dev: dev,
|
2021-01-21 20:33:54 +00:00
|
|
|
wgLogger: wgLogger,
|
2020-07-24 21:19:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 20:33:54 +00:00
|
|
|
func (s *magicStack) Reconfig(cfg *wgcfg.Config) error {
|
|
|
|
s.wgLogger.SetPeers(cfg.Peers)
|
2021-01-29 20:16:36 +00:00
|
|
|
return wgcfg.ReconfigDevice(s.dev, cfg, s.conn.logf)
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 15:09:54 +00:00
|
|
|
func (s *magicStack) String() string {
|
|
|
|
pub := s.Public()
|
|
|
|
return pub.ShortString()
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:19:20 +00:00
|
|
|
func (s *magicStack) Close() {
|
|
|
|
s.dev.Close()
|
|
|
|
s.conn.Close()
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:09:54 +00:00
|
|
|
func (s *magicStack) Public() key.Public {
|
|
|
|
return key.Public(s.privateKey.Public())
|
|
|
|
}
|
|
|
|
|
2020-07-25 00:32:18 +00:00
|
|
|
func (s *magicStack) Status() *ipnstate.Status {
|
|
|
|
var sb ipnstate.StatusBuilder
|
|
|
|
s.conn.UpdateStatus(&sb)
|
|
|
|
return sb.Status()
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:25:25 +00:00
|
|
|
// IP returns the Tailscale IP address assigned to this magicStack.
|
|
|
|
//
|
|
|
|
// Something external needs to provide a NetworkMap and WireGuard
|
|
|
|
// configs to the magicStack in order for it to acquire an IP
|
|
|
|
// address. See meshStacks for one possible source of netmaps and IPs.
|
2021-03-29 20:50:44 +00:00
|
|
|
func (s *magicStack) IP() netaddr.IP {
|
2020-07-27 20:25:25 +00:00
|
|
|
for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) {
|
|
|
|
st := s.Status()
|
|
|
|
if len(st.TailscaleIPs) > 0 {
|
|
|
|
return st.TailscaleIPs[0]
|
|
|
|
}
|
2020-07-25 00:32:18 +00:00
|
|
|
}
|
2021-03-29 20:50:44 +00:00
|
|
|
panic("timed out waiting for magicstack to get an IP assigned")
|
2020-07-25 00:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// meshStacks monitors epCh on all given ms, and plumbs network maps
|
|
|
|
// and WireGuard configs into everyone to form a full mesh that has up
|
|
|
|
// to date endpoint info. Think of it as an extremely stripped down
|
|
|
|
// and purpose-built Tailscale control plane.
|
2021-08-26 02:39:20 +00:00
|
|
|
func meshStacks(logf logger.Logf, mutateNetmap func(idx int, nm *netmap.NetworkMap), ms ...*magicStack) (cleanup func()) {
|
2020-07-25 00:32:18 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// Serialize all reconfigurations globally, just to keep things
|
|
|
|
// simpler.
|
|
|
|
var (
|
|
|
|
mu sync.Mutex
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
eps = make([][]tailcfg.Endpoint, len(ms))
|
2020-07-25 00:32:18 +00:00
|
|
|
)
|
|
|
|
|
2021-02-05 23:44:46 +00:00
|
|
|
buildNetmapLocked := func(myIdx int) *netmap.NetworkMap {
|
2020-07-25 00:32:18 +00:00
|
|
|
me := ms[myIdx]
|
2021-02-05 23:44:46 +00:00
|
|
|
nm := &netmap.NetworkMap{
|
2020-07-25 00:32:18 +00:00
|
|
|
PrivateKey: me.privateKey,
|
|
|
|
NodeKey: tailcfg.NodeKey(me.privateKey.Public()),
|
2021-05-15 01:07:28 +00:00
|
|
|
Addresses: []netaddr.IPPrefix{netaddr.IPPrefixFrom(netaddr.IPv4(1, 0, 0, byte(myIdx+1)), 32)},
|
2020-07-25 00:32:18 +00:00
|
|
|
}
|
|
|
|
for i, peer := range ms {
|
|
|
|
if i == myIdx {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-15 01:07:28 +00:00
|
|
|
addrs := []netaddr.IPPrefix{netaddr.IPPrefixFrom(netaddr.IPv4(1, 0, 0, byte(i+1)), 32)}
|
2020-07-25 00:32:18 +00:00
|
|
|
peer := &tailcfg.Node{
|
|
|
|
ID: tailcfg.NodeID(i + 1),
|
|
|
|
Name: fmt.Sprintf("node%d", i+1),
|
|
|
|
Key: tailcfg.NodeKey(peer.privateKey.Public()),
|
|
|
|
DiscoKey: peer.conn.DiscoPublicKey(),
|
|
|
|
Addresses: addrs,
|
|
|
|
AllowedIPs: addrs,
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
Endpoints: epStrings(eps[i]),
|
2020-07-25 00:32:18 +00:00
|
|
|
DERP: "127.3.3.40:1",
|
|
|
|
}
|
|
|
|
nm.Peers = append(nm.Peers, peer)
|
|
|
|
}
|
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
if mutateNetmap != nil {
|
|
|
|
mutateNetmap(myIdx, nm)
|
|
|
|
}
|
2020-07-25 00:32:18 +00:00
|
|
|
return nm
|
|
|
|
}
|
|
|
|
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
updateEps := func(idx int, newEps []tailcfg.Endpoint) {
|
2020-07-25 00:32:18 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
eps[idx] = newEps
|
|
|
|
|
|
|
|
for i, m := range ms {
|
2021-02-05 23:44:46 +00:00
|
|
|
nm := buildNetmapLocked(i)
|
|
|
|
m.conn.SetNetworkMap(nm)
|
|
|
|
peerSet := make(map[key.Public]struct{}, len(nm.Peers))
|
|
|
|
for _, peer := range nm.Peers {
|
2020-07-25 00:32:18 +00:00
|
|
|
peerSet[key.Public(peer.Key)] = struct{}{}
|
|
|
|
}
|
|
|
|
m.conn.UpdatePeers(peerSet)
|
2021-02-25 04:05:23 +00:00
|
|
|
wg, err := nmcfg.WGCfg(nm, logf, netmap.AllowSingleHosts, "")
|
2020-07-25 00:32:18 +00:00
|
|
|
if err != nil {
|
|
|
|
// We're too far from the *testing.T to be graceful,
|
|
|
|
// blow up. Shouldn't happen anyway.
|
|
|
|
panic(fmt.Sprintf("failed to construct wgcfg from netmap: %v", err))
|
|
|
|
}
|
2021-01-21 20:33:54 +00:00
|
|
|
if err := m.Reconfig(wg); err != nil {
|
2021-08-30 22:32:06 +00:00
|
|
|
if ctx.Err() != nil || errors.Is(err, errConnClosed) {
|
2021-08-27 21:13:49 +00:00
|
|
|
// shutdown race, don't care.
|
|
|
|
return
|
|
|
|
}
|
2020-07-25 00:32:18 +00:00
|
|
|
panic(fmt.Sprintf("device reconfig failed: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(ms))
|
|
|
|
for i := range ms {
|
|
|
|
go func(myIdx int) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case eps := <-ms[myIdx].epCh:
|
|
|
|
logf("conn%d endpoints update", myIdx+1)
|
|
|
|
updateEps(myIdx, eps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:51:38 +00:00
|
|
|
func TestNewConn(t *testing.T) {
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
tstest.PanicOnLog()
|
2021-02-02 19:30:46 +00:00
|
|
|
tstest.ResourceCheck(t)
|
2020-03-03 21:50:47 +00:00
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
epCh := make(chan string, 16)
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
epFunc := func(endpoints []tailcfg.Endpoint) {
|
2020-02-05 22:16:58 +00:00
|
|
|
for _, ep := range endpoints {
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
epCh <- ep.Addr.String()
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:14:48 +00:00
|
|
|
stunAddr, stunCleanupFn := stuntest.Serve(t)
|
2020-03-03 11:51:31 +00:00
|
|
|
defer stunCleanupFn()
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
port := pickPort(t)
|
2020-05-17 16:51:38 +00:00
|
|
|
conn, err := NewConn(Options{
|
2021-08-26 02:39:20 +00:00
|
|
|
Port: port,
|
|
|
|
EndpointsFunc: epFunc,
|
|
|
|
Logf: t.Logf,
|
2020-02-05 22:16:58 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2020-05-17 16:51:38 +00:00
|
|
|
conn.SetDERPMap(stuntest.DERPMapOf(stunAddr.String()))
|
2020-12-30 01:22:56 +00:00
|
|
|
conn.SetPrivateKey(wgkey.Private(key.NewPrivate()))
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
go func() {
|
2020-02-18 16:57:11 +00:00
|
|
|
var pkt [64 << 10]byte
|
2020-02-05 22:16:58 +00:00
|
|
|
for {
|
2021-03-24 16:41:57 +00:00
|
|
|
_, _, err := conn.receiveIPv4(pkt[:])
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-03-06 20:37:19 +00:00
|
|
|
timeout := time.After(10 * time.Second)
|
2020-02-05 22:16:58 +00:00
|
|
|
var endpoints []string
|
|
|
|
suffix := fmt.Sprintf(":%d", port)
|
|
|
|
collectEndpoints:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ep := <-epCh:
|
|
|
|
endpoints = append(endpoints, ep)
|
|
|
|
if strings.HasSuffix(ep, suffix) {
|
|
|
|
break collectEndpoints
|
|
|
|
}
|
2020-03-06 20:37:19 +00:00
|
|
|
case <-timeout:
|
2020-02-05 22:16:58 +00:00
|
|
|
t.Fatalf("timeout with endpoints: %v", endpoints)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 04:12:14 +00:00
|
|
|
func pickPort(t testing.TB) uint16 {
|
2020-02-05 22:16:58 +00:00
|
|
|
t.Helper()
|
2020-10-28 15:23:12 +00:00
|
|
|
conn, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
return uint16(conn.LocalAddr().(*net.UDPAddr).Port)
|
|
|
|
}
|
2020-02-18 21:32:04 +00:00
|
|
|
|
2020-03-04 06:21:56 +00:00
|
|
|
func TestPickDERPFallback(t *testing.T) {
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
tstest.PanicOnLog()
|
2021-02-02 19:30:46 +00:00
|
|
|
tstest.ResourceCheck(t)
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
|
2020-05-17 16:51:38 +00:00
|
|
|
c := newConn()
|
2021-06-25 18:44:40 +00:00
|
|
|
dm := &tailcfg.DERPMap{
|
|
|
|
Regions: map[int]*tailcfg.DERPRegion{
|
|
|
|
1: &tailcfg.DERPRegion{},
|
|
|
|
2: &tailcfg.DERPRegion{},
|
|
|
|
3: &tailcfg.DERPRegion{},
|
|
|
|
4: &tailcfg.DERPRegion{},
|
|
|
|
5: &tailcfg.DERPRegion{},
|
|
|
|
6: &tailcfg.DERPRegion{},
|
|
|
|
7: &tailcfg.DERPRegion{},
|
|
|
|
8: &tailcfg.DERPRegion{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.derpMap = dm
|
2020-03-04 06:21:56 +00:00
|
|
|
a := c.pickDERPFallback()
|
|
|
|
if a == 0 {
|
|
|
|
t.Fatalf("pickDERPFallback returned 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that it's consistent.
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
b := c.pickDERPFallback()
|
|
|
|
if a != b {
|
|
|
|
t.Fatalf("got inconsistent %d vs %d values", a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that that the pointer value of c is blended in and
|
|
|
|
// distribution over nodes works.
|
|
|
|
got := map[int]int{}
|
|
|
|
for i := 0; i < 50; i++ {
|
2020-05-17 16:51:38 +00:00
|
|
|
c = newConn()
|
2021-06-25 18:44:40 +00:00
|
|
|
c.derpMap = dm
|
2020-03-04 06:21:56 +00:00
|
|
|
got[c.pickDERPFallback()]++
|
|
|
|
}
|
|
|
|
t.Logf("distribution: %v", got)
|
|
|
|
if len(got) < 2 {
|
|
|
|
t.Errorf("expected more than 1 node; got %v", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that stickiness works.
|
|
|
|
const someNode = 123456
|
|
|
|
c.myDerp = someNode
|
|
|
|
if got := c.pickDERPFallback(); got != someNode {
|
|
|
|
t.Errorf("not sticky: got %v; want %v", got, someNode)
|
|
|
|
}
|
2020-03-25 18:14:29 +00:00
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
// TODO: test that disco-based clients changing to a new DERP
|
|
|
|
// region causes this fallback to also move, once disco clients
|
|
|
|
// have fixed DERP fallback logic.
|
2020-03-03 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 01:50:36 +00:00
|
|
|
// TestDeviceStartStop exercises the startup and shutdown logic of
|
|
|
|
// wireguard-go, which is intimately intertwined with magicsock's own
|
|
|
|
// lifecycle. We seem to be good at generating deadlocks here, so if
|
|
|
|
// this test fails you should suspect a deadlock somewhere in startup
|
|
|
|
// or shutdown. It may be an infrequent flake, so run with
|
|
|
|
// -count=10000 to be sure.
|
|
|
|
func TestDeviceStartStop(t *testing.T) {
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
tstest.PanicOnLog()
|
2021-02-02 19:30:46 +00:00
|
|
|
tstest.ResourceCheck(t)
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
|
2020-05-17 16:51:38 +00:00
|
|
|
conn, err := NewConn(Options{
|
2021-08-26 02:39:20 +00:00
|
|
|
EndpointsFunc: func(eps []tailcfg.Endpoint) {},
|
|
|
|
Logf: t.Logf,
|
2020-03-07 01:50:36 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
tun := tuntest.NewChannelTUN()
|
2021-03-23 18:39:06 +00:00
|
|
|
wgLogger := wglog.NewLogger(t.Logf)
|
2021-05-11 22:24:37 +00:00
|
|
|
dev := device.NewDevice(tun.TUN(), conn.Bind(), wgLogger.DeviceLogger)
|
2020-03-07 01:50:36 +00:00
|
|
|
dev.Up()
|
|
|
|
dev.Close()
|
|
|
|
}
|
|
|
|
|
2021-01-11 01:22:11 +00:00
|
|
|
// Exercise a code path in sendDiscoMessage if the connection has been closed.
|
|
|
|
func TestConnClosed(t *testing.T) {
|
|
|
|
mstun := &natlab.Machine{Name: "stun"}
|
|
|
|
m1 := &natlab.Machine{Name: "m1"}
|
|
|
|
m2 := &natlab.Machine{Name: "m2"}
|
|
|
|
inet := natlab.NewInternet()
|
|
|
|
sif := mstun.Attach("eth0", inet)
|
|
|
|
m1if := m1.Attach("eth0", inet)
|
|
|
|
m2if := m2.Attach("eth0", inet)
|
|
|
|
|
|
|
|
d := &devices{
|
|
|
|
m1: m1,
|
|
|
|
m1IP: m1if.V4(),
|
|
|
|
m2: m2,
|
|
|
|
m2IP: m2if.V4(),
|
|
|
|
stun: mstun,
|
|
|
|
stunIP: sif.V4(),
|
|
|
|
}
|
|
|
|
|
2021-01-15 01:39:36 +00:00
|
|
|
logf, closeLogf := logger.LogfCloser(t.Logf)
|
|
|
|
defer closeLogf()
|
|
|
|
|
|
|
|
derpMap, cleanup := runDERPAndStun(t, logf, d.stun, d.stunIP)
|
2021-01-11 01:22:11 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
ms1 := newMagicStack(t, logger.WithPrefix(logf, "conn1: "), d.m1, derpMap)
|
2021-01-11 01:22:11 +00:00
|
|
|
defer ms1.Close()
|
2021-08-26 02:39:20 +00:00
|
|
|
ms2 := newMagicStack(t, logger.WithPrefix(logf, "conn2: "), d.m2, derpMap)
|
2021-01-11 01:22:11 +00:00
|
|
|
defer ms2.Close()
|
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
cleanup = meshStacks(t.Logf, nil, ms1, ms2)
|
2021-01-11 01:22:11 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2021-03-29 20:50:44 +00:00
|
|
|
pkt := tuntest.Ping(ms2.IP().IPAddr().IP, ms1.IP().IPAddr().IP)
|
2021-01-11 01:22:11 +00:00
|
|
|
|
|
|
|
if len(ms1.conn.activeDerp) == 0 {
|
|
|
|
t.Errorf("unexpected DERP empty got: %v want: >0", len(ms1.conn.activeDerp))
|
|
|
|
}
|
|
|
|
|
|
|
|
ms1.conn.Close()
|
|
|
|
ms2.conn.Close()
|
|
|
|
|
|
|
|
// This should hit a c.closed conditional in sendDiscoMessage() and return immediately.
|
|
|
|
ms1.tun.Outbound <- pkt
|
|
|
|
select {
|
|
|
|
case <-ms2.tun.Inbound:
|
|
|
|
t.Error("unexpected response with connection closed")
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ms1.conn.activeDerp) > 0 {
|
|
|
|
t.Errorf("unexpected DERP active got: %v want:0", len(ms1.conn.activeDerp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 03:44:58 +00:00
|
|
|
func makeNestable(t *testing.T) (logf logger.Logf, setT func(t *testing.T)) {
|
2020-06-22 08:54:59 +00:00
|
|
|
var mu sync.RWMutex
|
2020-05-14 03:44:58 +00:00
|
|
|
cur := t
|
|
|
|
|
|
|
|
setT = func(t *testing.T) {
|
|
|
|
mu.Lock()
|
|
|
|
cur = t
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
logf = func(s string, args ...interface{}) {
|
2020-06-22 08:54:59 +00:00
|
|
|
mu.RLock()
|
2020-05-14 03:44:58 +00:00
|
|
|
t := cur
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
t.Logf(s, args...)
|
2020-06-22 08:54:59 +00:00
|
|
|
mu.RUnlock()
|
2020-05-14 03:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return logf, setT
|
|
|
|
}
|
|
|
|
|
2021-08-26 06:33:46 +00:00
|
|
|
// localhostOnlyListener is a nettype.PacketListener that listens on
|
|
|
|
// localhost (127.0.0.1 or ::1, depending on the requested network)
|
|
|
|
// when asked to listen on the unspecified address.
|
|
|
|
//
|
|
|
|
// It's used in tests where we set up localhost-to-localhost
|
|
|
|
// communication, because if you listen on the unspecified address on
|
|
|
|
// macOS and Windows, you get an interactive firewall consent prompt
|
|
|
|
// to allow the binding, which breaks our CIs.
|
|
|
|
type localhostListener struct{}
|
|
|
|
|
|
|
|
func (localhostListener) ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error) {
|
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch network {
|
|
|
|
case "udp4":
|
|
|
|
switch host {
|
|
|
|
case "", "0.0.0.0":
|
|
|
|
host = "127.0.0.1"
|
|
|
|
case "127.0.0.1":
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("localhostListener cannot be asked to listen on %q", address)
|
|
|
|
}
|
|
|
|
case "udp6":
|
|
|
|
switch host {
|
|
|
|
case "", "::":
|
|
|
|
host = "::1"
|
|
|
|
case "::1":
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("localhostListener cannot be asked to listen on %q", address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var conf net.ListenConfig
|
|
|
|
return conf.ListenPacket(ctx, network, net.JoinHostPort(host, port))
|
|
|
|
}
|
|
|
|
|
2020-03-03 15:39:40 +00:00
|
|
|
func TestTwoDevicePing(t *testing.T) {
|
2021-08-26 06:33:46 +00:00
|
|
|
l, ip := localhostListener{}, netaddr.IPv4(127, 0, 0, 1)
|
2020-07-27 15:21:17 +00:00
|
|
|
n := &devices{
|
|
|
|
m1: l,
|
|
|
|
m1IP: ip,
|
|
|
|
m2: l,
|
|
|
|
m2IP: ip,
|
|
|
|
stun: l,
|
|
|
|
stunIP: ip,
|
|
|
|
}
|
|
|
|
testTwoDevicePing(t, n)
|
|
|
|
}
|
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
// Legacy clients appear to new code as peers that know about DERP and
|
|
|
|
// WireGuard, but don't have a disco key. Check that we can still
|
|
|
|
// communicate successfully with such peers.
|
|
|
|
func TestNoDiscoKey(t *testing.T) {
|
|
|
|
tstest.PanicOnLog()
|
|
|
|
tstest.ResourceCheck(t)
|
|
|
|
|
2021-08-26 06:33:46 +00:00
|
|
|
derpMap, cleanup := runDERPAndStun(t, t.Logf, localhostListener{}, netaddr.IPv4(127, 0, 0, 1))
|
2021-08-26 02:39:20 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2021-08-26 06:33:46 +00:00
|
|
|
m1 := newMagicStack(t, t.Logf, localhostListener{}, derpMap)
|
2021-08-26 02:39:20 +00:00
|
|
|
defer m1.Close()
|
2021-08-26 06:33:46 +00:00
|
|
|
m2 := newMagicStack(t, t.Logf, localhostListener{}, derpMap)
|
2021-08-26 02:39:20 +00:00
|
|
|
defer m2.Close()
|
|
|
|
|
|
|
|
removeDisco := func(idx int, nm *netmap.NetworkMap) {
|
|
|
|
for _, p := range nm.Peers {
|
|
|
|
p.DiscoKey = tailcfg.DiscoKey{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupMesh := meshStacks(t.Logf, removeDisco, m1, m2)
|
|
|
|
defer cleanupMesh()
|
|
|
|
|
|
|
|
// Wait for both peers to know about each other before we try to
|
|
|
|
// ping.
|
|
|
|
for {
|
|
|
|
if s1 := m1.Status(); len(s1.Peer) != 1 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s2 := m2.Status(); len(s2.Peer) != 1 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt := tuntest.Ping(m2.IP().IPAddr().IP, m1.IP().IPAddr().IP)
|
|
|
|
m1.tun.Outbound <- pkt
|
|
|
|
select {
|
|
|
|
case <-m2.tun.Inbound:
|
|
|
|
t.Logf("ping m1>m2 ok")
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatalf("timed out waiting for ping to transit")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:21:17 +00:00
|
|
|
func TestActiveDiscovery(t *testing.T) {
|
|
|
|
t.Run("simple_internet", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
mstun := &natlab.Machine{Name: "stun"}
|
|
|
|
m1 := &natlab.Machine{Name: "m1"}
|
|
|
|
m2 := &natlab.Machine{Name: "m2"}
|
|
|
|
inet := natlab.NewInternet()
|
|
|
|
sif := mstun.Attach("eth0", inet)
|
|
|
|
m1if := m1.Attach("eth0", inet)
|
|
|
|
m2if := m2.Attach("eth0", inet)
|
|
|
|
|
2020-07-11 06:48:08 +00:00
|
|
|
n := &devices{
|
2020-07-27 15:21:17 +00:00
|
|
|
m1: m1,
|
|
|
|
m1IP: m1if.V4(),
|
|
|
|
m2: m2,
|
|
|
|
m2IP: m2if.V4(),
|
|
|
|
stun: mstun,
|
|
|
|
stunIP: sif.V4(),
|
2020-07-11 06:48:08 +00:00
|
|
|
}
|
2020-07-27 15:21:17 +00:00
|
|
|
testActiveDiscovery(t, n)
|
2020-07-10 21:26:04 +00:00
|
|
|
})
|
2020-07-11 07:03:19 +00:00
|
|
|
|
2020-07-27 20:34:41 +00:00
|
|
|
t.Run("facing_easy_firewalls", func(t *testing.T) {
|
2020-07-27 15:21:17 +00:00
|
|
|
mstun := &natlab.Machine{Name: "stun"}
|
|
|
|
m1 := &natlab.Machine{
|
|
|
|
Name: "m1",
|
|
|
|
PacketHandler: &natlab.Firewall{},
|
|
|
|
}
|
|
|
|
m2 := &natlab.Machine{
|
|
|
|
Name: "m2",
|
|
|
|
PacketHandler: &natlab.Firewall{},
|
|
|
|
}
|
|
|
|
inet := natlab.NewInternet()
|
|
|
|
sif := mstun.Attach("eth0", inet)
|
|
|
|
m1if := m1.Attach("eth0", inet)
|
|
|
|
m2if := m2.Attach("eth0", inet)
|
|
|
|
|
|
|
|
n := &devices{
|
|
|
|
m1: m1,
|
|
|
|
m1IP: m1if.V4(),
|
|
|
|
m2: m2,
|
|
|
|
m2IP: m2if.V4(),
|
|
|
|
stun: mstun,
|
|
|
|
stunIP: sif.V4(),
|
|
|
|
}
|
|
|
|
testActiveDiscovery(t, n)
|
2020-07-10 21:26:04 +00:00
|
|
|
})
|
2020-07-27 15:21:17 +00:00
|
|
|
|
|
|
|
t.Run("facing_nats", func(t *testing.T) {
|
|
|
|
mstun := &natlab.Machine{Name: "stun"}
|
|
|
|
m1 := &natlab.Machine{
|
|
|
|
Name: "m1",
|
|
|
|
PacketHandler: &natlab.Firewall{},
|
|
|
|
}
|
|
|
|
nat1 := &natlab.Machine{
|
|
|
|
Name: "nat1",
|
|
|
|
}
|
|
|
|
m2 := &natlab.Machine{
|
|
|
|
Name: "m2",
|
|
|
|
PacketHandler: &natlab.Firewall{},
|
|
|
|
}
|
|
|
|
nat2 := &natlab.Machine{
|
|
|
|
Name: "nat2",
|
|
|
|
}
|
|
|
|
|
|
|
|
inet := natlab.NewInternet()
|
|
|
|
lan1 := &natlab.Network{
|
|
|
|
Name: "lan1",
|
2021-08-26 02:39:20 +00:00
|
|
|
Prefix4: netaddr.MustParseIPPrefix("192.168.0.0/24"),
|
2020-07-27 15:21:17 +00:00
|
|
|
}
|
|
|
|
lan2 := &natlab.Network{
|
|
|
|
Name: "lan2",
|
2021-08-26 02:39:20 +00:00
|
|
|
Prefix4: netaddr.MustParseIPPrefix("192.168.1.0/24"),
|
2020-07-27 15:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sif := mstun.Attach("eth0", inet)
|
|
|
|
nat1WAN := nat1.Attach("wan", inet)
|
|
|
|
nat1LAN := nat1.Attach("lan1", lan1)
|
|
|
|
nat2WAN := nat2.Attach("wan", inet)
|
|
|
|
nat2LAN := nat2.Attach("lan2", lan2)
|
|
|
|
m1if := m1.Attach("eth0", lan1)
|
|
|
|
m2if := m2.Attach("eth0", lan2)
|
|
|
|
lan1.SetDefaultGateway(nat1LAN)
|
|
|
|
lan2.SetDefaultGateway(nat2LAN)
|
|
|
|
|
|
|
|
nat1.PacketHandler = &natlab.SNAT44{
|
|
|
|
Machine: nat1,
|
|
|
|
ExternalInterface: nat1WAN,
|
|
|
|
Firewall: &natlab.Firewall{
|
|
|
|
TrustedInterface: nat1LAN,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nat2.PacketHandler = &natlab.SNAT44{
|
|
|
|
Machine: nat2,
|
|
|
|
ExternalInterface: nat2WAN,
|
|
|
|
Firewall: &natlab.Firewall{
|
|
|
|
TrustedInterface: nat2LAN,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
n := &devices{
|
|
|
|
m1: m1,
|
|
|
|
m1IP: m1if.V4(),
|
|
|
|
m2: m2,
|
|
|
|
m2IP: m2if.V4(),
|
|
|
|
stun: mstun,
|
|
|
|
stunIP: sif.V4(),
|
|
|
|
}
|
|
|
|
testActiveDiscovery(t, n)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-11 06:48:08 +00:00
|
|
|
type devices struct {
|
|
|
|
m1 nettype.PacketListener
|
|
|
|
m1IP netaddr.IP
|
|
|
|
|
|
|
|
m2 nettype.PacketListener
|
|
|
|
m2IP netaddr.IP
|
|
|
|
|
|
|
|
stun nettype.PacketListener
|
|
|
|
stunIP netaddr.IP
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:09:54 +00:00
|
|
|
// newPinger starts continuously sending test packets from srcM to
|
|
|
|
// dstM, until cleanup is invoked to stop it. Each ping has 1 second
|
|
|
|
// to transit the network. It is a test failure to lose a ping.
|
2020-07-27 20:25:25 +00:00
|
|
|
func newPinger(t *testing.T, logf logger.Logf, src, dst *magicStack) (cleanup func()) {
|
2020-07-27 15:09:54 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
done := make(chan struct{})
|
|
|
|
one := func() bool {
|
|
|
|
// TODO(danderson): requiring exactly zero packet loss
|
|
|
|
// will probably be too strict for some tests we'd like to
|
|
|
|
// run (e.g. discovery switching to a new path on
|
|
|
|
// failure). Figure out what kind of thing would be
|
|
|
|
// acceptable to test instead of "every ping must
|
|
|
|
// transit".
|
2021-03-29 20:50:44 +00:00
|
|
|
pkt := tuntest.Ping(dst.IP().IPAddr().IP, src.IP().IPAddr().IP)
|
2020-07-27 20:32:45 +00:00
|
|
|
select {
|
|
|
|
case src.tun.Outbound <- pkt:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return false
|
|
|
|
}
|
2020-07-27 15:09:54 +00:00
|
|
|
select {
|
2020-07-27 20:25:25 +00:00
|
|
|
case <-dst.tun.Inbound:
|
2020-07-27 15:09:54 +00:00
|
|
|
return true
|
2020-07-27 16:20:31 +00:00
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
// Very generous timeout here because depending on
|
|
|
|
// magicsock setup races, the first handshake might get
|
|
|
|
// eaten by the receiving end (if wireguard-go hasn't been
|
|
|
|
// configured quite yet), so we have to wait for at least
|
|
|
|
// the first retransmit from wireguard before we declare
|
|
|
|
// failure.
|
2020-07-27 15:09:54 +00:00
|
|
|
t.Errorf("timed out waiting for ping to transit")
|
|
|
|
return true
|
|
|
|
case <-ctx.Done():
|
2020-07-27 19:46:34 +00:00
|
|
|
// Try a little bit longer to consume the packet we're
|
|
|
|
// waiting for. This is to deal with shutdown races, where
|
|
|
|
// natlab may still be delivering a packet to us from a
|
|
|
|
// goroutine.
|
|
|
|
select {
|
2020-07-27 20:25:25 +00:00
|
|
|
case <-dst.tun.Inbound:
|
2020-07-27 19:46:34 +00:00
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
2020-07-27 15:09:54 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() {
|
|
|
|
cancel()
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronously transit one ping to get things started. This is
|
|
|
|
// nice because it means that newPinger returning means we've
|
|
|
|
// worked through initial connectivity.
|
|
|
|
if !one() {
|
|
|
|
cleanup()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2021-03-29 20:50:44 +00:00
|
|
|
logf("sending ping stream from %s (%s) to %s (%s)", src, src.IP(), dst, dst.IP())
|
2020-07-27 15:09:54 +00:00
|
|
|
defer close(done)
|
|
|
|
for one() {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return cleanup
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:33:09 +00:00
|
|
|
// testActiveDiscovery verifies that two magicStacks tied to the given
|
|
|
|
// devices can establish a direct p2p connection with each other. See
|
|
|
|
// TestActiveDiscovery for the various configurations of devices that
|
|
|
|
// get exercised.
|
2020-07-25 00:32:18 +00:00
|
|
|
func testActiveDiscovery(t *testing.T, d *devices) {
|
|
|
|
tstest.PanicOnLog()
|
2021-02-02 19:30:46 +00:00
|
|
|
tstest.ResourceCheck(t)
|
2020-07-25 00:32:18 +00:00
|
|
|
|
|
|
|
tlogf, setT := makeNestable(t)
|
|
|
|
setT(t)
|
|
|
|
|
|
|
|
start := time.Now()
|
2021-01-16 02:19:20 +00:00
|
|
|
wlogf := func(msg string, args ...interface{}) {
|
2020-09-11 23:26:05 +00:00
|
|
|
t.Helper()
|
|
|
|
msg = fmt.Sprintf("%s: %s", time.Since(start).Truncate(time.Microsecond), msg)
|
2020-07-25 00:32:18 +00:00
|
|
|
tlogf(msg, args...)
|
|
|
|
}
|
2021-01-16 02:19:20 +00:00
|
|
|
logf, closeLogf := logger.LogfCloser(wlogf)
|
|
|
|
defer closeLogf()
|
2020-07-25 00:32:18 +00:00
|
|
|
|
|
|
|
derpMap, cleanup := runDERPAndStun(t, logf, d.stun, d.stunIP)
|
|
|
|
defer cleanup()
|
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
m1 := newMagicStack(t, logger.WithPrefix(logf, "conn1: "), d.m1, derpMap)
|
2020-07-25 00:32:18 +00:00
|
|
|
defer m1.Close()
|
2021-08-26 02:39:20 +00:00
|
|
|
m2 := newMagicStack(t, logger.WithPrefix(logf, "conn2: "), d.m2, derpMap)
|
2020-07-25 00:32:18 +00:00
|
|
|
defer m2.Close()
|
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
cleanup = meshStacks(logf, nil, m1, m2)
|
2020-07-25 00:32:18 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2021-03-29 20:50:44 +00:00
|
|
|
m1IP := m1.IP()
|
|
|
|
m2IP := m2.IP()
|
2020-07-25 00:32:18 +00:00
|
|
|
logf("IPs: %s %s", m1IP, m2IP)
|
|
|
|
|
2020-07-27 20:25:25 +00:00
|
|
|
cleanup = newPinger(t, logf, m1, m2)
|
2020-07-27 15:09:54 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Everything is now up and running, active discovery should find
|
|
|
|
// a direct path between our peers. Wait for it to switch away
|
|
|
|
// from DERP.
|
|
|
|
|
|
|
|
mustDirect := func(m1, m2 *magicStack) {
|
2020-08-04 16:36:38 +00:00
|
|
|
lastLog := time.Now().Add(-time.Minute)
|
2020-09-12 00:20:52 +00:00
|
|
|
// See https://github.com/tailscale/tailscale/issues/654 for a discussion of this deadline.
|
|
|
|
for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) {
|
2020-07-27 15:09:54 +00:00
|
|
|
pst := m1.Status().Peer[m2.Public()]
|
|
|
|
if pst.CurAddr != "" {
|
|
|
|
logf("direct link %s->%s found with addr %s", m1, m2, pst.CurAddr)
|
|
|
|
return
|
|
|
|
}
|
2020-08-04 16:36:38 +00:00
|
|
|
if now := time.Now(); now.Sub(lastLog) > time.Second {
|
|
|
|
logf("no direct path %s->%s yet, addrs %v", m1, m2, pst.Addrs)
|
|
|
|
lastLog = now
|
|
|
|
}
|
2020-07-27 15:09:54 +00:00
|
|
|
}
|
|
|
|
t.Errorf("magicsock did not find a direct path from %s to %s", m1, m2)
|
2020-07-25 00:32:18 +00:00
|
|
|
}
|
2020-07-27 15:09:54 +00:00
|
|
|
|
|
|
|
mustDirect(m1, m2)
|
|
|
|
mustDirect(m2, m1)
|
|
|
|
|
|
|
|
logf("starting cleanup")
|
2020-07-25 00:32:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 06:48:08 +00:00
|
|
|
func testTwoDevicePing(t *testing.T, d *devices) {
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
tstest.PanicOnLog()
|
2021-02-02 19:30:46 +00:00
|
|
|
tstest.ResourceCheck(t)
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
|
|
|
|
// This gets reassigned inside every test, so that the connections
|
|
|
|
// all log using the "current" t.Logf function. Sigh.
|
2021-01-19 19:04:17 +00:00
|
|
|
nestedLogf, setT := makeNestable(t)
|
|
|
|
|
|
|
|
logf, closeLogf := logger.LogfCloser(nestedLogf)
|
|
|
|
defer closeLogf()
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
|
2020-07-24 21:19:20 +00:00
|
|
|
derpMap, cleanup := runDERPAndStun(t, logf, d.stun, d.stunIP)
|
|
|
|
defer cleanup()
|
2020-07-10 21:26:04 +00:00
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
m1 := newMagicStack(t, logf, d.m1, derpMap)
|
2020-07-24 21:19:20 +00:00
|
|
|
defer m1.Close()
|
2021-08-26 02:39:20 +00:00
|
|
|
m2 := newMagicStack(t, logf, d.m2, derpMap)
|
2020-07-24 21:19:20 +00:00
|
|
|
defer m2.Close()
|
2020-03-03 15:39:40 +00:00
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
cleanupMesh := meshStacks(logf, nil, m1, m2)
|
|
|
|
defer cleanupMesh()
|
|
|
|
|
|
|
|
// Wait for magicsock to be told about peers from meshStacks.
|
|
|
|
tstest.WaitFor(10*time.Second, func() error {
|
|
|
|
if p := m1.Status().Peer[key.Public(m2.privateKey.Public())]; p == nil || !p.InMagicSock {
|
|
|
|
return errors.New("m1 not ready")
|
|
|
|
}
|
|
|
|
if p := m2.Status().Peer[key.Public(m1.privateKey.Public())]; p == nil || !p.InMagicSock {
|
|
|
|
return errors.New("m2 not ready")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
m1cfg := &wgcfg.Config{
|
|
|
|
Name: "peer1",
|
|
|
|
PrivateKey: m1.privateKey,
|
|
|
|
Addresses: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.0.0.1/32")},
|
|
|
|
Peers: []wgcfg.Peer{
|
|
|
|
wgcfg.Peer{
|
|
|
|
PublicKey: m2.privateKey.Public(),
|
2021-09-01 05:37:23 +00:00
|
|
|
DiscoKey: m2.conn.DiscoPublicKey(),
|
2021-08-26 02:39:20 +00:00
|
|
|
AllowedIPs: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.0.0.2/32")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
m2cfg := &wgcfg.Config{
|
|
|
|
Name: "peer2",
|
|
|
|
PrivateKey: m2.privateKey,
|
|
|
|
Addresses: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.0.0.2/32")},
|
|
|
|
Peers: []wgcfg.Peer{
|
|
|
|
wgcfg.Peer{
|
|
|
|
PublicKey: m1.privateKey.Public(),
|
2021-09-01 05:37:23 +00:00
|
|
|
DiscoKey: m1.conn.DiscoPublicKey(),
|
2021-08-26 02:39:20 +00:00
|
|
|
AllowedIPs: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.0.0.1/32")},
|
|
|
|
},
|
|
|
|
},
|
2020-07-10 21:26:04 +00:00
|
|
|
}
|
2020-03-03 15:39:40 +00:00
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
if err := m1.Reconfig(m1cfg); err != nil {
|
2020-03-03 21:50:47 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-26 02:39:20 +00:00
|
|
|
if err := m2.Reconfig(m2cfg); err != nil {
|
2020-03-03 21:50:47 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-02-08 20:27:35 +00:00
|
|
|
// In the normal case, pings succeed immediately.
|
|
|
|
// However, in the case of a handshake race, we need to retry.
|
2021-02-10 19:49:30 +00:00
|
|
|
// With very bad luck, we can need to retry multiple times.
|
|
|
|
allowedRetries := 3
|
|
|
|
if cibuild.On() {
|
|
|
|
// Allow extra retries on small/flaky/loaded CI machines.
|
|
|
|
allowedRetries *= 2
|
|
|
|
}
|
|
|
|
// Retries take 5s each. Add 1s for some processing time.
|
|
|
|
pingTimeout := 5*time.Second*time.Duration(allowedRetries) + time.Second
|
2021-02-08 20:27:35 +00:00
|
|
|
|
2021-02-10 22:47:26 +00:00
|
|
|
// sendWithTimeout sends msg using send, checking that it is received unchanged from in.
|
|
|
|
// It resends once per second until the send succeeds, or pingTimeout time has elapsed.
|
|
|
|
sendWithTimeout := func(msg []byte, in chan []byte, send func()) error {
|
|
|
|
start := time.Now()
|
|
|
|
for time.Since(start) < pingTimeout {
|
|
|
|
send()
|
|
|
|
select {
|
|
|
|
case recv := <-in:
|
|
|
|
if !bytes.Equal(msg, recv) {
|
|
|
|
return errors.New("ping did not transit correctly")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
// try again
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("ping timed out")
|
|
|
|
}
|
|
|
|
|
2020-05-14 00:54:27 +00:00
|
|
|
ping1 := func(t *testing.T) {
|
2020-03-03 15:39:40 +00:00
|
|
|
msg2to1 := tuntest.Ping(net.ParseIP("1.0.0.1"), net.ParseIP("1.0.0.2"))
|
2021-02-10 22:47:26 +00:00
|
|
|
send := func() {
|
|
|
|
m2.tun.Outbound <- msg2to1
|
|
|
|
t.Log("ping1 sent")
|
|
|
|
}
|
|
|
|
in := m1.tun.Inbound
|
|
|
|
if err := sendWithTimeout(msg2to1, in, send); err != nil {
|
|
|
|
t.Error(err)
|
2020-03-03 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ping2 := func(t *testing.T) {
|
|
|
|
msg1to2 := tuntest.Ping(net.ParseIP("1.0.0.2"), net.ParseIP("1.0.0.1"))
|
2021-02-10 22:47:26 +00:00
|
|
|
send := func() {
|
|
|
|
m1.tun.Outbound <- msg1to2
|
|
|
|
t.Log("ping2 sent")
|
|
|
|
}
|
|
|
|
in := m2.tun.Inbound
|
|
|
|
if err := sendWithTimeout(msg1to2, in, send); err != nil {
|
|
|
|
t.Error(err)
|
2020-03-03 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 08:54:59 +00:00
|
|
|
outerT := t
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
t.Run("ping 1.0.0.1", func(t *testing.T) {
|
2020-05-14 03:44:58 +00:00
|
|
|
setT(t)
|
2020-06-22 08:54:59 +00:00
|
|
|
defer setT(outerT)
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
ping1(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ping 1.0.0.2", func(t *testing.T) {
|
2020-06-22 08:54:59 +00:00
|
|
|
setT(t)
|
|
|
|
defer setT(outerT)
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
ping2(t)
|
|
|
|
})
|
|
|
|
|
2020-03-03 15:39:40 +00:00
|
|
|
t.Run("ping 1.0.0.2 via SendPacket", func(t *testing.T) {
|
2020-05-14 03:44:58 +00:00
|
|
|
setT(t)
|
2020-06-22 08:54:59 +00:00
|
|
|
defer setT(outerT)
|
2020-03-03 15:39:40 +00:00
|
|
|
msg1to2 := tuntest.Ping(net.ParseIP("1.0.0.2"), net.ParseIP("1.0.0.1"))
|
2021-02-10 22:47:26 +00:00
|
|
|
send := func() {
|
|
|
|
if err := m1.tsTun.InjectOutbound(msg1to2); err != nil {
|
|
|
|
t.Fatal(err)
|
2020-03-03 15:39:40 +00:00
|
|
|
}
|
2021-02-10 22:47:26 +00:00
|
|
|
t.Log("SendPacket sent")
|
|
|
|
}
|
|
|
|
in := m2.tun.Inbound
|
|
|
|
if err := sendWithTimeout(msg1to2, in, send); err != nil {
|
|
|
|
t.Error(err)
|
2020-03-03 15:39:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no-op dev1 reconfig", func(t *testing.T) {
|
2020-05-14 03:44:58 +00:00
|
|
|
setT(t)
|
2020-06-22 08:54:59 +00:00
|
|
|
defer setT(outerT)
|
2021-08-26 02:39:20 +00:00
|
|
|
if err := m1.Reconfig(m1cfg); err != nil {
|
2020-03-03 15:39:40 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ping1(t)
|
|
|
|
ping2(t)
|
|
|
|
})
|
|
|
|
}
|
2020-03-09 16:13:28 +00:00
|
|
|
|
2020-06-26 21:38:53 +00:00
|
|
|
func TestDiscoMessage(t *testing.T) {
|
2020-06-29 21:26:25 +00:00
|
|
|
c := newConn()
|
|
|
|
c.logf = t.Logf
|
2020-07-30 20:48:32 +00:00
|
|
|
c.privateKey = key.NewPrivate()
|
2020-07-06 19:10:39 +00:00
|
|
|
|
|
|
|
peer1Pub := c.DiscoPublicKey()
|
|
|
|
peer1Priv := c.discoPrivate
|
2021-08-26 02:39:20 +00:00
|
|
|
n := &tailcfg.Node{
|
|
|
|
Key: tailcfg.NodeKey(key.NewPrivate().Public()),
|
|
|
|
DiscoKey: peer1Pub,
|
|
|
|
}
|
2021-08-26 05:20:31 +00:00
|
|
|
c.peerMap.upsertDiscoEndpoint(&endpoint{
|
2021-08-26 02:39:20 +00:00
|
|
|
publicKey: n.Key,
|
|
|
|
discoKey: n.DiscoKey,
|
|
|
|
})
|
2020-06-26 21:38:53 +00:00
|
|
|
|
|
|
|
const payload = "why hello"
|
|
|
|
|
|
|
|
var nonce [24]byte
|
|
|
|
crand.Read(nonce[:])
|
|
|
|
|
|
|
|
pkt := append([]byte("TS💬"), peer1Pub[:]...)
|
|
|
|
pkt = append(pkt, nonce[:]...)
|
|
|
|
|
|
|
|
pkt = box.Seal(pkt, []byte(payload), &nonce, c.discoPrivate.Public().B32(), peer1Priv.B32())
|
2020-06-30 20:25:13 +00:00
|
|
|
got := c.handleDiscoMessage(pkt, netaddr.IPPort{})
|
2020-06-26 21:38:53 +00:00
|
|
|
if !got {
|
|
|
|
t.Error("failed to open it")
|
|
|
|
}
|
|
|
|
}
|
2020-07-04 05:26:53 +00:00
|
|
|
|
2021-08-26 05:20:31 +00:00
|
|
|
// tests that having a endpoint.String prevents wireguard-go's
|
2020-07-04 05:26:53 +00:00
|
|
|
// log.Printf("%v") of its conn.Endpoint values from using reflect to
|
|
|
|
// walk into read mutex while they're being used and then causing data
|
|
|
|
// races.
|
|
|
|
func TestDiscoStringLogRace(t *testing.T) {
|
2021-08-26 05:20:31 +00:00
|
|
|
de := new(endpoint)
|
2020-07-04 05:26:53 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
fmt.Fprintf(ioutil.Discard, "%v", de)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
de.mu.Lock()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2020-08-06 17:23:16 +00:00
|
|
|
|
2021-01-18 16:39:52 +00:00
|
|
|
func Test32bitAlignment(t *testing.T) {
|
2021-09-01 02:06:04 +00:00
|
|
|
// Need an associated conn with non-nil noteRecvActivity to
|
|
|
|
// trigger interesting work on the atomics in endpoint.
|
|
|
|
called := 0
|
|
|
|
de := endpoint{
|
|
|
|
c: &Conn{
|
|
|
|
noteRecvActivity: func(tailcfg.NodeKey) { called++ },
|
|
|
|
},
|
|
|
|
}
|
2021-02-20 06:15:41 +00:00
|
|
|
|
2021-07-21 18:04:36 +00:00
|
|
|
if off := unsafe.Offsetof(de.lastRecv); off%8 != 0 {
|
2021-08-26 05:20:31 +00:00
|
|
|
t.Fatalf("endpoint.lastRecv is not 8-byte aligned")
|
2021-02-20 06:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 02:06:04 +00:00
|
|
|
de.noteRecvActivity() // verify this doesn't panic on 32-bit
|
|
|
|
if called != 1 {
|
|
|
|
t.Fatal("expected call to noteRecvActivity")
|
2020-08-06 21:57:03 +00:00
|
|
|
}
|
2021-09-01 02:06:04 +00:00
|
|
|
de.noteRecvActivity()
|
|
|
|
if called != 1 {
|
|
|
|
t.Error("expected no second call to noteRecvActivity")
|
2020-08-06 21:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-03 04:12:14 +00:00
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
// newTestConn returns a new Conn.
|
|
|
|
func newTestConn(t testing.TB) *Conn {
|
2021-02-07 05:27:02 +00:00
|
|
|
t.Helper()
|
|
|
|
port := pickPort(t)
|
2020-12-03 04:12:14 +00:00
|
|
|
conn, err := NewConn(Options{
|
2021-08-26 06:33:46 +00:00
|
|
|
Logf: t.Logf,
|
|
|
|
Port: port,
|
|
|
|
TestOnlyPacketListener: localhostListener{},
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
EndpointsFunc: func(eps []tailcfg.Endpoint) {
|
2021-02-07 05:27:02 +00:00
|
|
|
t.Logf("endpoints: %q", eps)
|
2020-12-03 04:12:14 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-02-07 05:27:02 +00:00
|
|
|
t.Fatal(err)
|
2020-12-03 04:12:14 +00:00
|
|
|
}
|
2021-02-07 05:27:02 +00:00
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// addTestEndpoint sets conn's network map to a single peer expected
|
|
|
|
// to receive packets from sendConn (or DERP), and returns that peer's
|
|
|
|
// nodekey and discokey.
|
2021-03-23 18:58:45 +00:00
|
|
|
func addTestEndpoint(tb testing.TB, conn *Conn, sendConn net.PacketConn) (tailcfg.NodeKey, tailcfg.DiscoKey) {
|
2021-01-15 20:50:33 +00:00
|
|
|
// Give conn just enough state that it'll recognize sendConn as a
|
|
|
|
// valid peer and not fall through to the legacy magicsock
|
|
|
|
// codepath.
|
|
|
|
discoKey := tailcfg.DiscoKey{31: 1}
|
2021-02-07 05:27:02 +00:00
|
|
|
nodeKey := tailcfg.NodeKey{0: 'N', 1: 'K'}
|
2021-02-05 23:44:46 +00:00
|
|
|
conn.SetNetworkMap(&netmap.NetworkMap{
|
2021-01-15 20:50:33 +00:00
|
|
|
Peers: []*tailcfg.Node{
|
|
|
|
{
|
2021-02-07 05:27:02 +00:00
|
|
|
Key: nodeKey,
|
2021-01-15 20:50:33 +00:00
|
|
|
DiscoKey: discoKey,
|
|
|
|
Endpoints: []string{sendConn.LocalAddr().String()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-02-05 21:36:05 +00:00
|
|
|
conn.SetPrivateKey(wgkey.Private{0: 1})
|
2021-09-01 05:37:23 +00:00
|
|
|
_, err := conn.ParseEndpoint(wgkey.Key(nodeKey).HexString())
|
2021-03-23 18:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
2021-01-15 20:50:33 +00:00
|
|
|
conn.addValidDiscoPathForTest(discoKey, netaddr.MustParseIPPort(sendConn.LocalAddr().String()))
|
2021-02-07 05:27:02 +00:00
|
|
|
return nodeKey, discoKey
|
|
|
|
}
|
|
|
|
|
2021-02-11 21:35:06 +00:00
|
|
|
func setUpReceiveFrom(tb testing.TB) (roundTrip func()) {
|
2021-08-31 03:43:18 +00:00
|
|
|
if b, ok := tb.(*testing.B); ok {
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
2021-08-26 02:39:20 +00:00
|
|
|
conn := newTestConn(tb)
|
2021-02-11 21:35:06 +00:00
|
|
|
tb.Cleanup(func() { conn.Close() })
|
2021-02-11 19:54:58 +00:00
|
|
|
conn.logf = logger.Discard
|
2021-02-07 05:27:02 +00:00
|
|
|
|
|
|
|
sendConn, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
2021-02-11 21:35:06 +00:00
|
|
|
tb.Fatal(err)
|
2021-02-07 05:27:02 +00:00
|
|
|
}
|
2021-02-11 21:35:06 +00:00
|
|
|
tb.Cleanup(func() { sendConn.Close() })
|
2021-02-07 05:27:02 +00:00
|
|
|
|
2021-03-23 18:58:45 +00:00
|
|
|
addTestEndpoint(tb, conn, sendConn)
|
2021-01-15 20:50:33 +00:00
|
|
|
|
2020-12-03 04:12:14 +00:00
|
|
|
var dstAddr net.Addr = conn.pconn4.LocalAddr()
|
|
|
|
sendBuf := make([]byte, 1<<10)
|
|
|
|
for i := range sendBuf {
|
|
|
|
sendBuf[i] = 'x'
|
|
|
|
}
|
|
|
|
buf := make([]byte, 2<<10)
|
2021-02-11 21:35:06 +00:00
|
|
|
return func() {
|
2020-12-03 04:12:14 +00:00
|
|
|
if _, err := sendConn.WriteTo(sendBuf, dstAddr); err != nil {
|
2021-02-11 21:35:06 +00:00
|
|
|
tb.Fatalf("WriteTo: %v", err)
|
2020-12-03 04:12:14 +00:00
|
|
|
}
|
2021-03-24 16:41:57 +00:00
|
|
|
n, ep, err := conn.receiveIPv4(buf)
|
2020-12-03 04:12:14 +00:00
|
|
|
if err != nil {
|
2021-02-11 21:35:06 +00:00
|
|
|
tb.Fatal(err)
|
2020-12-03 04:12:14 +00:00
|
|
|
}
|
|
|
|
_ = n
|
|
|
|
_ = ep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 21:35:06 +00:00
|
|
|
// goMajorVersion reports the major Go version and whether it is a Tailscale fork.
|
|
|
|
// If parsing fails, goMajorVersion returns 0, false.
|
|
|
|
func goMajorVersion(s string) (version int, isTS bool) {
|
|
|
|
if !strings.HasPrefix(s, "go1.") {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
mm := s[len("go1."):]
|
|
|
|
var major, rest string
|
|
|
|
for _, sep := range []string{".", "rc", "beta"} {
|
|
|
|
i := strings.Index(mm, sep)
|
|
|
|
if i > 0 {
|
|
|
|
major, rest = mm[:i], mm[i:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if major == "" {
|
|
|
|
major = mm
|
|
|
|
}
|
|
|
|
n, err := strconv.Atoi(major)
|
|
|
|
if err != nil {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return n, strings.Contains(rest, "ts")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoMajorVersion(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
version string
|
|
|
|
wantN int
|
|
|
|
wantTS bool
|
|
|
|
}{
|
|
|
|
{"go1.15.8", 15, false},
|
|
|
|
{"go1.16rc1", 16, false},
|
|
|
|
{"go1.16rc1", 16, false},
|
|
|
|
{"go1.15.5-ts3bd89195a3", 15, true},
|
|
|
|
{"go1.15", 15, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
n, ts := goMajorVersion(tt.version)
|
|
|
|
if tt.wantN != n || tt.wantTS != ts {
|
|
|
|
t.Errorf("goMajorVersion(%s) = %v, %v, want %v, %v", tt.version, n, ts, tt.wantN, tt.wantTS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReceiveFromAllocs(t *testing.T) {
|
2021-09-15 23:43:44 +00:00
|
|
|
if racebuild.On {
|
|
|
|
t.Skip("alloc tests are unreliable with -race")
|
|
|
|
}
|
2021-02-11 21:35:06 +00:00
|
|
|
// Go 1.16 and before: allow 3 allocs.
|
|
|
|
// Go Tailscale fork, Go 1.17+: only allow 2 allocs.
|
|
|
|
major, ts := goMajorVersion(runtime.Version())
|
|
|
|
maxAllocs := 3
|
|
|
|
if major >= 17 || ts {
|
|
|
|
maxAllocs = 2
|
|
|
|
}
|
|
|
|
t.Logf("allowing %d allocs for Go version %q", maxAllocs, runtime.Version())
|
|
|
|
roundTrip := setUpReceiveFrom(t)
|
2021-09-09 18:49:44 +00:00
|
|
|
avg := int(testing.AllocsPerRun(1000, roundTrip))
|
2021-02-11 21:35:06 +00:00
|
|
|
if avg > maxAllocs {
|
|
|
|
t.Fatalf("expected %d allocs in ReceiveFrom, got %v", maxAllocs, avg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkReceiveFrom(b *testing.B) {
|
|
|
|
roundTrip := setUpReceiveFrom(b)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
roundTrip()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 04:12:14 +00:00
|
|
|
func BenchmarkReceiveFrom_Native(b *testing.B) {
|
2021-08-31 03:43:18 +00:00
|
|
|
b.ReportAllocs()
|
2020-12-03 04:12:14 +00:00
|
|
|
recvConn, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer recvConn.Close()
|
|
|
|
recvConnUDP := recvConn.(*net.UDPConn)
|
|
|
|
|
|
|
|
sendConn, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer sendConn.Close()
|
|
|
|
|
|
|
|
var dstAddr net.Addr = recvConn.LocalAddr()
|
|
|
|
sendBuf := make([]byte, 1<<10)
|
|
|
|
for i := range sendBuf {
|
|
|
|
sendBuf[i] = 'x'
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 2<<10)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if _, err := sendConn.WriteTo(sendBuf, dstAddr); err != nil {
|
|
|
|
b.Fatalf("WriteTo: %v", err)
|
|
|
|
}
|
|
|
|
if _, _, err := recvConnUDP.ReadFromUDP(buf); err != nil {
|
|
|
|
b.Fatalf("ReadFromUDP: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 22:26:29 +00:00
|
|
|
|
|
|
|
// Test that a netmap update where node changes its node key but
|
|
|
|
// doesn't change its disco key doesn't result in a broken state.
|
|
|
|
//
|
|
|
|
// https://github.com/tailscale/tailscale/issues/1391
|
|
|
|
func TestSetNetworkMapChangingNodeKey(t *testing.T) {
|
2021-08-26 02:39:20 +00:00
|
|
|
conn := newTestConn(t)
|
2021-02-23 22:26:29 +00:00
|
|
|
t.Cleanup(func() { conn.Close() })
|
2021-09-08 02:27:19 +00:00
|
|
|
var buf tstest.MemLogger
|
2021-09-06 00:41:23 +00:00
|
|
|
conn.logf = buf.Logf
|
2021-02-23 22:26:29 +00:00
|
|
|
|
|
|
|
conn.SetPrivateKey(wgkey.Private{0: 1})
|
|
|
|
|
|
|
|
discoKey := tailcfg.DiscoKey{31: 1}
|
|
|
|
nodeKey1 := tailcfg.NodeKey{0: 'N', 1: 'K', 2: '1'}
|
|
|
|
nodeKey2 := tailcfg.NodeKey{0: 'N', 1: 'K', 2: '2'}
|
|
|
|
|
|
|
|
conn.SetNetworkMap(&netmap.NetworkMap{
|
|
|
|
Peers: []*tailcfg.Node{
|
|
|
|
{
|
|
|
|
Key: nodeKey1,
|
|
|
|
DiscoKey: discoKey,
|
|
|
|
Endpoints: []string{"192.168.1.2:345"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-09-01 05:37:23 +00:00
|
|
|
_, err := conn.ParseEndpoint(wgkey.Key(nodeKey1).HexString())
|
2021-02-23 22:26:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
conn.SetNetworkMap(&netmap.NetworkMap{
|
|
|
|
Peers: []*tailcfg.Node{
|
|
|
|
{
|
|
|
|
Key: nodeKey2,
|
|
|
|
DiscoKey: discoKey,
|
|
|
|
Endpoints: []string{"192.168.1.2:345"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-26 05:20:31 +00:00
|
|
|
de, ok := conn.peerMap.endpointForDiscoKey(discoKey)
|
2021-08-26 02:39:20 +00:00
|
|
|
if ok && de.publicKey != nodeKey2 {
|
2021-02-23 22:26:29 +00:00
|
|
|
t.Fatalf("discoEndpoint public key = %q; want %q", de.publicKey[:], nodeKey2[:])
|
|
|
|
}
|
|
|
|
|
2021-09-06 00:41:23 +00:00
|
|
|
log := buf.String()
|
2021-02-23 22:26:29 +00:00
|
|
|
wantSub := map[string]int{
|
2021-08-26 02:39:20 +00:00
|
|
|
"magicsock: got updated network map; 1 peers": 2,
|
2021-02-23 22:26:29 +00:00
|
|
|
"magicsock: disco key discokey:0000000000000000000000000000000000000000000000000000000000000001 changed from node key [TksxA] to [TksyA]": 1,
|
|
|
|
}
|
|
|
|
for sub, want := range wantSub {
|
|
|
|
got := strings.Count(log, sub)
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("in log, count of substring %q = %v; want %v", sub, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t.Failed() {
|
|
|
|
t.Logf("log output: %s", log)
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 23:48:49 +00:00
|
|
|
|
|
|
|
func TestRebindStress(t *testing.T) {
|
2021-08-26 02:39:20 +00:00
|
|
|
conn := newTestConn(t)
|
2021-03-08 23:48:49 +00:00
|
|
|
|
2021-09-08 02:27:19 +00:00
|
|
|
var buf tstest.MemLogger
|
2021-09-06 00:41:23 +00:00
|
|
|
conn.logf = buf.Logf
|
2021-03-08 23:48:49 +00:00
|
|
|
|
|
|
|
closed := false
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if !closed {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
buf := make([]byte, 1500)
|
|
|
|
for {
|
2021-03-24 16:41:57 +00:00
|
|
|
_, _, err := conn.receiveIPv4(buf)
|
2021-03-08 23:48:49 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
errc <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errc <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for i := 0; i < 2000; i++ {
|
|
|
|
conn.Rebind()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for i := 0; i < 2000; i++ {
|
|
|
|
conn.Rebind()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
if err := conn.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
closed = true
|
|
|
|
|
|
|
|
err := <-errc
|
|
|
|
if err != nil {
|
2021-09-06 00:41:23 +00:00
|
|
|
t.Fatalf("Got ReceiveIPv4 error: %v (is closed = %v). Log:\n%s", err, errors.Is(err, net.ErrClosed), buf.String())
|
2021-03-08 23:48:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-22 17:23:26 +00:00
|
|
|
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
func TestEndpointSetsEqual(t *testing.T) {
|
|
|
|
s := func(ports ...uint16) (ret []tailcfg.Endpoint) {
|
|
|
|
for _, port := range ports {
|
|
|
|
ret = append(ret, tailcfg.Endpoint{
|
2021-05-15 01:07:28 +00:00
|
|
|
Addr: netaddr.IPPortFrom(netaddr.IP{}, port),
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
})
|
2021-03-22 17:23:26 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tests := []struct {
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
a, b []tailcfg.Endpoint
|
2021-03-22 17:23:26 +00:00
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: s(1, 2, 3),
|
|
|
|
b: s(1, 2, 3),
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: s(1, 2),
|
|
|
|
b: s(2, 1),
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: s(1, 2),
|
|
|
|
b: s(2, 1, 1),
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: s(1, 2, 2),
|
|
|
|
b: s(2, 1),
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: s(1, 2, 2),
|
|
|
|
b: s(2, 1, 1),
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: s(1, 2, 2, 3),
|
|
|
|
b: s(2, 1, 1),
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: s(1, 2, 2),
|
|
|
|
b: s(2, 1, 1, 3),
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
if got := endpointSetsEqual(tt.a, tt.b); got != tt.want {
|
2021-03-22 17:23:26 +00:00
|
|
|
t.Errorf("%q vs %q = %v; want %v", tt.a, tt.b, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-03-23 17:07:34 +00:00
|
|
|
|
|
|
|
func TestBetterAddr(t *testing.T) {
|
|
|
|
const ms = time.Millisecond
|
|
|
|
al := func(ipps string, d time.Duration) addrLatency {
|
|
|
|
return addrLatency{netaddr.MustParseIPPort(ipps), d}
|
|
|
|
}
|
|
|
|
zero := addrLatency{}
|
|
|
|
tests := []struct {
|
|
|
|
a, b addrLatency
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{a: zero, b: zero, want: false},
|
|
|
|
{a: al("10.0.0.2:123", 5*ms), b: zero, want: true},
|
|
|
|
{a: zero, b: al("10.0.0.2:123", 5*ms), want: false},
|
|
|
|
{a: al("10.0.0.2:123", 5*ms), b: al("1.2.3.4:555", 6*ms), want: true},
|
|
|
|
{a: al("10.0.0.2:123", 5*ms), b: al("10.0.0.2:123", 10*ms), want: false}, // same IPPort
|
2021-03-23 17:17:19 +00:00
|
|
|
|
|
|
|
// Prefer IPv6 if roughly equivalent:
|
|
|
|
{
|
|
|
|
a: al("[2001::5]:123", 100*ms),
|
|
|
|
b: al("1.2.3.4:555", 91*ms),
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: al("1.2.3.4:555", 91*ms),
|
|
|
|
b: al("[2001::5]:123", 100*ms),
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// But not if IPv4 is much faster:
|
|
|
|
{
|
|
|
|
a: al("[2001::5]:123", 100*ms),
|
|
|
|
b: al("1.2.3.4:555", 30*ms),
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: al("1.2.3.4:555", 30*ms),
|
|
|
|
b: al("[2001::5]:123", 100*ms),
|
|
|
|
want: true,
|
|
|
|
},
|
2021-03-23 17:07:34 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
got := betterAddr(tt.a, tt.b)
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("betterAddr(%+v, %+v) = %v; want %v", tt.a, tt.b, got, tt.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gotBack := betterAddr(tt.b, tt.a)
|
|
|
|
if got && gotBack {
|
|
|
|
t.Errorf("betterAddr(%+v, %+v) and betterAddr(%+v, %+v) both unexpectedly true", tt.a, tt.b, tt.b, tt.a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
|
|
|
|
func epStrings(eps []tailcfg.Endpoint) (ret []string) {
|
|
|
|
for _, ep := range eps {
|
|
|
|
ret = append(ret, ep.Addr.String())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|