|
|
@ -11,7 +11,9 @@ import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"hash/fnv"
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
@ -28,9 +30,12 @@ import (
|
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
|
|
|
"tailscale.com/interfaces"
|
|
|
|
"tailscale.com/interfaces"
|
|
|
|
|
|
|
|
"tailscale.com/netcheck"
|
|
|
|
"tailscale.com/stun"
|
|
|
|
"tailscale.com/stun"
|
|
|
|
"tailscale.com/stunner"
|
|
|
|
"tailscale.com/stunner"
|
|
|
|
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
"tailscale.com/version"
|
|
|
|
"tailscale.com/version"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -66,6 +71,10 @@ type Conn struct {
|
|
|
|
// Its Loaded value is always non-nil.
|
|
|
|
// Its Loaded value is always non-nil.
|
|
|
|
stunReceiveFunc atomic.Value // of func(p []byte, fromAddr *net.UDPAddr)
|
|
|
|
stunReceiveFunc atomic.Value // of func(p []byte, fromAddr *net.UDPAddr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
netInfoMu sync.Mutex
|
|
|
|
|
|
|
|
netInfoFunc func(*tailcfg.NetInfo) // nil until set
|
|
|
|
|
|
|
|
netInfoLast *tailcfg.NetInfo
|
|
|
|
|
|
|
|
|
|
|
|
udpRecvCh chan udpReadResult
|
|
|
|
udpRecvCh chan udpReadResult
|
|
|
|
derpRecvCh chan derpReadResult
|
|
|
|
derpRecvCh chan derpReadResult
|
|
|
|
|
|
|
|
|
|
|
@ -204,15 +213,17 @@ func (c *Conn) epUpdate(ctx context.Context) {
|
|
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
defer close(lastDone)
|
|
|
|
defer close(lastDone)
|
|
|
|
nearestDerp, endpoints, err := c.determineEndpoints(epCtx)
|
|
|
|
|
|
|
|
|
|
|
|
c.updateNetInfo() // best effort
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
endpoints, err := c.determineEndpoints(epCtx)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
c.logf("magicsock.Conn: endpoint update failed: %v", err)
|
|
|
|
c.logf("magicsock.Conn: endpoint update failed: %v", err)
|
|
|
|
// TODO(crawshaw): are there any conditions under which
|
|
|
|
// TODO(crawshaw): are there any conditions under which
|
|
|
|
// we should trigger a retry based on the error here?
|
|
|
|
// we should trigger a retry based on the error here?
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
derpChanged := c.setNearestDerp(nearestDerp)
|
|
|
|
if stringsEqual(endpoints, lastEndpoints) {
|
|
|
|
if stringsEqual(endpoints, lastEndpoints) && !derpChanged {
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastEndpoints = endpoints
|
|
|
|
lastEndpoints = endpoints
|
|
|
@ -222,6 +233,98 @@ func (c *Conn) epUpdate(ctx context.Context) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Conn) updateNetInfo() {
|
|
|
|
|
|
|
|
logf := logger.WithPrefix(c.logf, "updateNetInfo: ")
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
report, err := netcheck.GetReport(ctx, logf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
logf("GetReport: %v", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ni := &tailcfg.NetInfo{
|
|
|
|
|
|
|
|
DERPLatency: map[string]float64{},
|
|
|
|
|
|
|
|
MappingVariesByDestIP: report.MappingVariesByDestIP,
|
|
|
|
|
|
|
|
HairPinning: report.HairPinning,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for server, d := range report.DERPLatency {
|
|
|
|
|
|
|
|
ni.DERPLatency[server] = d.Seconds()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ni.WorkingIPv6.Set(report.IPv6)
|
|
|
|
|
|
|
|
ni.WorkingUDP.Set(report.UDP)
|
|
|
|
|
|
|
|
ni.PreferredDERP = report.PreferredDERP
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ni.PreferredDERP == 0 {
|
|
|
|
|
|
|
|
// Perhaps UDP is blocked. Pick a deterministic but arbitrary
|
|
|
|
|
|
|
|
// one.
|
|
|
|
|
|
|
|
ni.PreferredDERP = c.pickDERPFallback()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
c.setNearestDerp(ni.PreferredDERP)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: set link type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.callNetInfoCallback(ni)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var processStartUnixNano = time.Now().UnixNano()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pickDERPFallback returns a non-zero but deterministic DERP node to
|
|
|
|
|
|
|
|
// connect to. This is only used if netcheck couldn't find the
|
|
|
|
|
|
|
|
// nearest one (for instance, if UDP is blocked and thus STUN latency
|
|
|
|
|
|
|
|
// checks aren't working).
|
|
|
|
|
|
|
|
func (c *Conn) pickDERPFallback() int {
|
|
|
|
|
|
|
|
c.derpMu.Lock()
|
|
|
|
|
|
|
|
defer c.derpMu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if c.myDerp != 0 {
|
|
|
|
|
|
|
|
// If we already had one in the past, stay on it.
|
|
|
|
|
|
|
|
return c.myDerp
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(derpNodeID) == 0 {
|
|
|
|
|
|
|
|
// No DERP nodes registered.
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
h := fnv.New64()
|
|
|
|
|
|
|
|
h.Write([]byte(fmt.Sprintf("%p/%d", c, processStartUnixNano))) // arbitrary
|
|
|
|
|
|
|
|
return derpNodeID[rand.New(rand.NewSource(int64(h.Sum64()))).Intn(len(derpNodeID))]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// callNetInfoCallback calls the NetInfo callback (if previously
|
|
|
|
|
|
|
|
// registered with SetNetInfoCallback) if ni has substantially changed
|
|
|
|
|
|
|
|
// since the last state.
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// callNetInfoCallback takes ownership of ni.
|
|
|
|
|
|
|
|
func (c *Conn) callNetInfoCallback(ni *tailcfg.NetInfo) {
|
|
|
|
|
|
|
|
c.netInfoMu.Lock()
|
|
|
|
|
|
|
|
defer c.netInfoMu.Unlock()
|
|
|
|
|
|
|
|
if ni.BasicallyEqual(c.netInfoLast) {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
c.netInfoLast = ni
|
|
|
|
|
|
|
|
if c.netInfoFunc != nil {
|
|
|
|
|
|
|
|
c.logf("netInfo update: %+v", ni)
|
|
|
|
|
|
|
|
go c.netInfoFunc(ni)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Conn) SetNetInfoCallback(fn func(*tailcfg.NetInfo)) {
|
|
|
|
|
|
|
|
if fn == nil {
|
|
|
|
|
|
|
|
panic("nil NetInfoCallback")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
c.netInfoMu.Lock()
|
|
|
|
|
|
|
|
last := c.netInfoLast
|
|
|
|
|
|
|
|
c.netInfoFunc = fn
|
|
|
|
|
|
|
|
c.netInfoMu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if last != nil {
|
|
|
|
|
|
|
|
fn(last)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Conn) setNearestDerp(derpNum int) (changed bool) {
|
|
|
|
func (c *Conn) setNearestDerp(derpNum int) (changed bool) {
|
|
|
|
c.derpMu.Lock()
|
|
|
|
c.derpMu.Lock()
|
|
|
|
defer c.derpMu.Unlock()
|
|
|
|
defer c.derpMu.Unlock()
|
|
|
@ -236,8 +339,7 @@ func (c *Conn) setNearestDerp(derpNum int) (changed bool) {
|
|
|
|
|
|
|
|
|
|
|
|
// determineEndpoints returns the machine's endpoint addresses. It
|
|
|
|
// determineEndpoints returns the machine's endpoint addresses. It
|
|
|
|
// does a STUN lookup to determine its public address.
|
|
|
|
// does a STUN lookup to determine its public address.
|
|
|
|
func (c *Conn) determineEndpoints(ctx context.Context) (nearestDerp int, ipPorts []string, err error) {
|
|
|
|
func (c *Conn) determineEndpoints(ctx context.Context) (ipPorts []string, err error) {
|
|
|
|
nearestDerp = derpNYC // for now
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
alreadyMu sync.Mutex
|
|
|
|
alreadyMu sync.Mutex
|
|
|
|
already = make(map[string]bool) // endpoint -> true
|
|
|
|
already = make(map[string]bool) // endpoint -> true
|
|
|
@ -265,7 +367,7 @@ func (c *Conn) determineEndpoints(ctx context.Context) (nearestDerp int, ipPorts
|
|
|
|
c.stunReceiveFunc.Store(s.Receive)
|
|
|
|
c.stunReceiveFunc.Store(s.Receive)
|
|
|
|
|
|
|
|
|
|
|
|
if err := s.Run(ctx); err != nil {
|
|
|
|
if err := s.Run(ctx); err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.ignoreSTUNPackets()
|
|
|
|
c.ignoreSTUNPackets()
|
|
|
@ -273,7 +375,7 @@ func (c *Conn) determineEndpoints(ctx context.Context) (nearestDerp int, ipPorts
|
|
|
|
if localAddr := c.pconn.LocalAddr(); localAddr.IP.IsUnspecified() {
|
|
|
|
if localAddr := c.pconn.LocalAddr(); localAddr.IP.IsUnspecified() {
|
|
|
|
ips, loopback, err := interfaces.LocalAddresses()
|
|
|
|
ips, loopback, err := interfaces.LocalAddresses()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reason := "localAddresses"
|
|
|
|
reason := "localAddresses"
|
|
|
|
if len(ips) == 0 {
|
|
|
|
if len(ips) == 0 {
|
|
|
@ -303,7 +405,7 @@ func (c *Conn) determineEndpoints(ctx context.Context) (nearestDerp int, ipPorts
|
|
|
|
// The STUN address(es) are always first so that legacy wireguard
|
|
|
|
// The STUN address(es) are always first so that legacy wireguard
|
|
|
|
// can use eps[0] as its only known endpoint address (although that's
|
|
|
|
// can use eps[0] as its only known endpoint address (although that's
|
|
|
|
// obviously non-ideal).
|
|
|
|
// obviously non-ideal).
|
|
|
|
return nearestDerp, eps, nil
|
|
|
|
return eps, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func stringsEqual(x, y []string) bool {
|
|
|
|
func stringsEqual(x, y []string) bool {
|
|
|
|