2021-01-12 03:07:08 +00:00
|
|
|
// Copyright (c) 2021 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 wgengine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-03-16 20:58:56 +00:00
|
|
|
"runtime"
|
2021-01-12 03:07:08 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-02-04 21:12:42 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2021-01-12 03:07:08 +00:00
|
|
|
"tailscale.com/net/flowtrack"
|
|
|
|
"tailscale.com/net/packet"
|
2021-03-16 20:58:56 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2021-03-27 05:14:08 +00:00
|
|
|
"tailscale.com/net/tstun"
|
2021-03-21 04:45:47 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2021-01-12 03:07:08 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
|
|
|
)
|
|
|
|
|
|
|
|
const tcpTimeoutBeforeDebug = 5 * time.Second
|
|
|
|
|
|
|
|
// debugConnectFailures reports whether the local node should track
|
|
|
|
// outgoing TCP connections and log which ones fail and why.
|
|
|
|
func debugConnectFailures() bool {
|
|
|
|
s := os.Getenv("TS_DEBUG_CONNECT_FAILURES")
|
|
|
|
if s == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
v, _ := strconv.ParseBool(s)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
type pendingOpenFlow struct {
|
|
|
|
timer *time.Timer // until giving up on the flow
|
2021-02-02 23:05:51 +00:00
|
|
|
|
|
|
|
// guarded by userspaceEngine.mu:
|
|
|
|
|
|
|
|
// problem is non-zero if we got a MaybeBroken (non-terminal)
|
|
|
|
// TSMP "reject" header.
|
|
|
|
problem packet.TailscaleRejectReason
|
2021-01-12 03:07:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 20:03:41 +00:00
|
|
|
func (e *userspaceEngine) removeFlow(f flowtrack.Tuple) (removed bool) {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
of, ok := e.pendOpen[f]
|
|
|
|
if !ok {
|
|
|
|
// Not a tracked flow (likely already removed)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
of.timer.Stop()
|
|
|
|
delete(e.pendOpen, f)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-02 23:05:51 +00:00
|
|
|
func (e *userspaceEngine) noteFlowProblemFromPeer(f flowtrack.Tuple, problem packet.TailscaleRejectReason) {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
of, ok := e.pendOpen[f]
|
|
|
|
if !ok {
|
|
|
|
// Not a tracked flow (likely already removed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
of.problem = problem
|
|
|
|
}
|
|
|
|
|
2021-01-12 03:07:08 +00:00
|
|
|
func (e *userspaceEngine) trackOpenPreFilterIn(pp *packet.Parsed, t *tstun.TUN) (res filter.Response) {
|
|
|
|
res = filter.Accept // always
|
|
|
|
|
2021-03-21 04:45:47 +00:00
|
|
|
if pp.IPProto == ipproto.TSMP {
|
2021-01-12 20:03:41 +00:00
|
|
|
res = filter.DropSilently
|
|
|
|
rh, ok := pp.AsTailscaleRejectedHeader()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-02-02 23:05:51 +00:00
|
|
|
if rh.MaybeBroken {
|
|
|
|
e.noteFlowProblemFromPeer(rh.Flow(), rh.Reason)
|
|
|
|
} else if f := rh.Flow(); e.removeFlow(f) {
|
2021-01-12 20:03:41 +00:00
|
|
|
e.logf("open-conn-track: flow %v %v > %v rejected due to %v", rh.Proto, rh.Src, rh.Dst, rh.Reason)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-12 03:07:08 +00:00
|
|
|
if pp.IPVersion == 0 ||
|
2021-03-21 04:45:47 +00:00
|
|
|
pp.IPProto != ipproto.TCP ||
|
2021-01-12 03:07:08 +00:00
|
|
|
pp.TCPFlags&(packet.TCPSyn|packet.TCPRst) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-12 20:03:41 +00:00
|
|
|
// Either a SYN or a RST came back. Remove it in either case.
|
2021-01-12 03:07:08 +00:00
|
|
|
|
2021-03-20 04:05:51 +00:00
|
|
|
f := flowtrack.Tuple{Proto: pp.IPProto, Dst: pp.Src, Src: pp.Dst} // src/dst reversed
|
2021-01-12 20:03:41 +00:00
|
|
|
removed := e.removeFlow(f)
|
|
|
|
if removed && pp.TCPFlags&packet.TCPRst != 0 {
|
|
|
|
e.logf("open-conn-track: flow TCP %v got RST by peer", f)
|
2021-01-12 03:07:08 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *userspaceEngine) trackOpenPostFilterOut(pp *packet.Parsed, t *tstun.TUN) (res filter.Response) {
|
|
|
|
res = filter.Accept // always
|
|
|
|
|
|
|
|
if pp.IPVersion == 0 ||
|
2021-03-21 04:45:47 +00:00
|
|
|
pp.IPProto != ipproto.TCP ||
|
2021-01-12 03:07:08 +00:00
|
|
|
pp.TCPFlags&packet.TCPSyn == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-20 04:05:51 +00:00
|
|
|
flow := flowtrack.Tuple{Proto: pp.IPProto, Src: pp.Src, Dst: pp.Dst}
|
2021-03-16 20:58:56 +00:00
|
|
|
|
|
|
|
// iOS likes to probe Apple IPs on all interfaces to check for connectivity.
|
|
|
|
// Don't start timers tracking those. They won't succeed anyway. Avoids log spam
|
|
|
|
// like:
|
|
|
|
// open-conn-track: timeout opening (100.115.73.60:52501 => 17.125.252.5:443); no associated peer node
|
|
|
|
if runtime.GOOS == "ios" && flow.Dst.Port == 443 && !tsaddr.IsTailscaleIP(flow.Dst.IP) {
|
2021-03-23 04:25:43 +00:00
|
|
|
if _, err := e.peerForIP(flow.Dst.IP); err != nil {
|
2021-03-16 20:58:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 03:07:08 +00:00
|
|
|
timer := time.AfterFunc(tcpTimeoutBeforeDebug, func() {
|
|
|
|
e.onOpenTimeout(flow)
|
|
|
|
})
|
|
|
|
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
if e.pendOpen == nil {
|
|
|
|
e.pendOpen = make(map[flowtrack.Tuple]*pendingOpenFlow)
|
|
|
|
}
|
|
|
|
if _, dup := e.pendOpen[flow]; dup {
|
|
|
|
// Duplicates are expected when the OS retransmits. Ignore.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e.pendOpen[flow] = &pendingOpenFlow{timer: timer}
|
|
|
|
|
|
|
|
return filter.Accept
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *userspaceEngine) onOpenTimeout(flow flowtrack.Tuple) {
|
|
|
|
e.mu.Lock()
|
2021-02-02 23:05:51 +00:00
|
|
|
of, ok := e.pendOpen[flow]
|
|
|
|
if !ok {
|
2021-01-12 03:07:08 +00:00
|
|
|
// Not a tracked flow, or already handled & deleted.
|
|
|
|
e.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(e.pendOpen, flow)
|
2021-02-04 19:18:32 +00:00
|
|
|
problem := of.problem
|
2021-01-12 03:07:08 +00:00
|
|
|
e.mu.Unlock()
|
|
|
|
|
2021-02-04 19:18:32 +00:00
|
|
|
if !problem.IsZero() {
|
|
|
|
e.logf("open-conn-track: timeout opening %v; peer reported problem: %v", flow, problem)
|
2021-02-02 23:05:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 03:07:08 +00:00
|
|
|
// Diagnose why it might've timed out.
|
2021-03-23 04:25:43 +00:00
|
|
|
n, err := e.peerForIP(flow.Dst.IP)
|
|
|
|
if err != nil {
|
|
|
|
e.logf("open-conn-track: timeout opening %v; peerForIP: %v", flow, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n == nil {
|
2021-01-12 03:07:08 +00:00
|
|
|
e.logf("open-conn-track: timeout opening %v; no associated peer node", flow)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n.DiscoKey.IsZero() {
|
|
|
|
e.logf("open-conn-track: timeout opening %v; peer node %v running pre-0.100", flow, n.Key.ShortString())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n.DERP == "" {
|
|
|
|
e.logf("open-conn-track: timeout opening %v; peer node %v not connected to any DERP relay", flow, n.Key.ShortString())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var lastSeen time.Time
|
|
|
|
if n.LastSeen != nil {
|
|
|
|
lastSeen = *n.LastSeen
|
|
|
|
}
|
|
|
|
|
2021-02-04 21:12:42 +00:00
|
|
|
var ps *ipnstate.PeerStatusLite
|
2021-01-12 03:07:08 +00:00
|
|
|
if st, err := e.getStatus(); err == nil {
|
|
|
|
for _, v := range st.Peers {
|
|
|
|
if v.NodeKey == n.Key {
|
|
|
|
v := v // copy
|
|
|
|
ps = &v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
e.logf("open-conn-track: timeout opening %v to node %v; failed to get engine status: %v", flow, n.Key.ShortString(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ps == nil {
|
|
|
|
e.logf("open-conn-track: timeout opening %v; target node %v in netmap but unknown to wireguard", flow, n.Key.ShortString())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bradfitz): figure out what PeerStatus.LastHandshake
|
|
|
|
// is. It appears to be the last time we sent a handshake,
|
|
|
|
// which isn't what I expected. I thought it was when a
|
|
|
|
// handshake completed, which is what I want.
|
|
|
|
_ = ps.LastHandshake
|
|
|
|
|
|
|
|
e.logf("open-conn-track: timeout opening %v to node %v; lastSeen=%v, lastRecv=%v",
|
|
|
|
flow, n.Key.ShortString(),
|
|
|
|
agoOrNever(lastSeen), agoOrNever(e.magicConn.LastRecvActivityOfDisco(n.DiscoKey)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func agoOrNever(t time.Time) string {
|
|
|
|
if t.IsZero() {
|
|
|
|
return "never"
|
|
|
|
}
|
|
|
|
return time.Since(t).Round(time.Second).String()
|
|
|
|
}
|