2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-10-12 18:57:13 +00:00
|
|
|
|
|
|
|
// Package netlog provides a logger that monitors a TUN device and
|
|
|
|
// periodically records any traffic into a log stream.
|
|
|
|
package netlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/netip"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-04-26 17:12:46 +00:00
|
|
|
"tailscale.com/health"
|
2022-10-12 18:57:13 +00:00
|
|
|
"tailscale.com/logpolicy"
|
|
|
|
"tailscale.com/logtail"
|
2022-11-28 23:59:33 +00:00
|
|
|
"tailscale.com/net/connstats"
|
2023-04-17 23:01:41 +00:00
|
|
|
"tailscale.com/net/netmon"
|
2023-03-29 21:28:30 +00:00
|
|
|
"tailscale.com/net/sockstats"
|
2022-10-12 18:57:13 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2022-10-28 17:09:30 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2023-03-01 03:00:00 +00:00
|
|
|
"tailscale.com/types/logid"
|
2022-10-27 21:14:18 +00:00
|
|
|
"tailscale.com/types/netlogtype"
|
2022-12-16 18:14:00 +00:00
|
|
|
"tailscale.com/util/multierr"
|
2022-10-12 18:57:13 +00:00
|
|
|
"tailscale.com/wgengine/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
// pollPeriod specifies how often to poll for network traffic.
|
|
|
|
const pollPeriod = 5 * time.Second
|
|
|
|
|
2022-10-13 17:46:29 +00:00
|
|
|
// Device is an abstraction over a tunnel device or a magic socket.
|
2022-12-16 18:14:00 +00:00
|
|
|
// Both *tstun.Wrapper and *magicsock.Conn implement this interface.
|
2022-10-12 18:57:13 +00:00
|
|
|
type Device interface {
|
2022-11-28 23:59:33 +00:00
|
|
|
SetStatistics(*connstats.Statistics)
|
2022-10-12 18:57:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:46:29 +00:00
|
|
|
type noopDevice struct{}
|
|
|
|
|
2022-11-28 23:59:33 +00:00
|
|
|
func (noopDevice) SetStatistics(*connstats.Statistics) {}
|
2022-10-13 17:46:29 +00:00
|
|
|
|
2022-10-12 18:57:13 +00:00
|
|
|
// Logger logs statistics about every connection.
|
|
|
|
// At present, it only logs connections within a tailscale network.
|
|
|
|
// Exit node traffic is not logged for privacy reasons.
|
|
|
|
// The zero value is ready for use.
|
|
|
|
type Logger struct {
|
2022-12-16 18:14:00 +00:00
|
|
|
mu sync.Mutex // protects all fields below
|
2022-10-12 18:57:13 +00:00
|
|
|
|
2022-10-12 22:05:21 +00:00
|
|
|
logger *logtail.Logger
|
2022-12-16 18:14:00 +00:00
|
|
|
stats *connstats.Statistics
|
|
|
|
tun Device
|
|
|
|
sock Device
|
2022-10-12 18:57:13 +00:00
|
|
|
|
|
|
|
addrs map[netip.Addr]bool
|
|
|
|
prefixes map[netip.Prefix]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Running reports whether the logger is running.
|
|
|
|
func (nl *Logger) Running() bool {
|
|
|
|
nl.mu.Lock()
|
|
|
|
defer nl.mu.Unlock()
|
|
|
|
return nl.logger != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testClient *http.Client
|
|
|
|
|
|
|
|
// Startup starts an asynchronous network logger that monitors
|
2022-10-13 17:46:29 +00:00
|
|
|
// statistics for the provided tun and/or sock device.
|
|
|
|
//
|
|
|
|
// The tun Device captures packets within the tailscale network,
|
|
|
|
// where at least one address is a tailscale IP address.
|
|
|
|
// The source is always from the perspective of the current node.
|
|
|
|
// If one of the other endpoint is not a tailscale IP address,
|
|
|
|
// then it suggests the use of a subnet router or exit node.
|
|
|
|
// For example, when using a subnet router, the source address is
|
|
|
|
// the tailscale IP address of the current node, and
|
|
|
|
// the destination address is an IP address within the subnet range.
|
|
|
|
// In contrast, when acting as a subnet router, the source address is
|
|
|
|
// an IP address within the subnet range, and the destination is a
|
|
|
|
// tailscale IP address that initiated the subnet proxy connection.
|
|
|
|
// In this case, the node acting as a subnet router is acting on behalf
|
|
|
|
// of some remote endpoint within the subnet range.
|
|
|
|
// The tun is used to populate the VirtualTraffic, SubnetTraffic,
|
|
|
|
// and ExitTraffic fields in Message.
|
|
|
|
//
|
|
|
|
// The sock Device captures packets at the magicsock layer.
|
|
|
|
// The source is always a tailscale IP address and the destination
|
|
|
|
// is a non-tailscale IP address to contact for that particular tailscale node.
|
|
|
|
// The IP protocol and source port are always zero.
|
|
|
|
// The sock is used to populated the PhysicalTraffic field in Message.
|
2023-04-17 23:01:41 +00:00
|
|
|
// The netMon parameter is optional; if non-nil it's used to do faster interface lookups.
|
2024-05-02 17:55:05 +00:00
|
|
|
func (nl *Logger) Startup(nodeID tailcfg.StableNodeID, nodeLogID, domainLogID logid.PrivateID, tun, sock Device, netMon *netmon.Monitor, health *health.Tracker, logExitFlowEnabledEnabled bool) error {
|
2022-10-12 18:57:13 +00:00
|
|
|
nl.mu.Lock()
|
|
|
|
defer nl.mu.Unlock()
|
|
|
|
if nl.logger != nil {
|
2022-10-12 22:05:21 +00:00
|
|
|
return fmt.Errorf("network logger already running for %v", nl.logger.PrivateID().Public())
|
2022-10-12 18:57:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 18:14:00 +00:00
|
|
|
// Startup a log stream to Tailscale's logging service.
|
2023-07-13 15:54:29 +00:00
|
|
|
logf := log.Printf
|
2024-04-26 17:12:46 +00:00
|
|
|
httpc := &http.Client{Transport: logpolicy.NewLogtailTransport(logtail.DefaultHost, netMon, health, logf)}
|
2022-10-12 18:57:13 +00:00
|
|
|
if testClient != nil {
|
|
|
|
httpc = testClient
|
|
|
|
}
|
2022-12-16 18:14:00 +00:00
|
|
|
nl.logger = logtail.NewLogger(logtail.Config{
|
2022-10-12 18:57:13 +00:00
|
|
|
Collection: "tailtraffic.log.tailscale.io",
|
2022-10-28 17:09:30 +00:00
|
|
|
PrivateID: nodeLogID,
|
|
|
|
CopyPrivateID: domainLogID,
|
2022-10-12 18:57:13 +00:00
|
|
|
Stderr: io.Discard,
|
2024-03-21 19:20:38 +00:00
|
|
|
CompressLogs: true,
|
|
|
|
HTTPC: httpc,
|
2022-10-12 18:57:13 +00:00
|
|
|
// TODO(joetsai): Set Buffer? Use an in-memory buffer for now.
|
|
|
|
|
|
|
|
// Include process sequence numbers to identify missing samples.
|
|
|
|
IncludeProcID: true,
|
|
|
|
IncludeProcSequence: true,
|
2023-07-13 15:54:29 +00:00
|
|
|
}, logf)
|
2023-03-29 21:28:30 +00:00
|
|
|
nl.logger.SetSockstatsLabel(sockstats.LabelNetlogLogger)
|
2022-10-12 18:57:13 +00:00
|
|
|
|
2022-12-16 18:14:00 +00:00
|
|
|
// Startup a data structure to track per-connection statistics.
|
|
|
|
// There is a maximum size for individual log messages that logtail
|
|
|
|
// can upload to the Tailscale log service, so stay below this limit.
|
|
|
|
const maxLogSize = 256 << 10
|
|
|
|
const maxConns = (maxLogSize - netlogtype.MaxMessageJSONSize) / netlogtype.MaxConnectionCountsJSONSize
|
|
|
|
nl.stats = connstats.NewStatistics(pollPeriod, maxConns, func(start, end time.Time, virtual, physical map[netlogtype.Connection]netlogtype.Counts) {
|
|
|
|
nl.mu.Lock()
|
|
|
|
addrs := nl.addrs
|
|
|
|
prefixes := nl.prefixes
|
|
|
|
nl.mu.Unlock()
|
2024-05-02 17:55:05 +00:00
|
|
|
recordStatistics(nl.logger, nodeID, start, end, virtual, physical, addrs, prefixes, logExitFlowEnabledEnabled)
|
2022-12-16 18:14:00 +00:00
|
|
|
})
|
2022-10-13 17:46:29 +00:00
|
|
|
|
2022-12-16 18:14:00 +00:00
|
|
|
// Register the connection tracker into the TUN device.
|
|
|
|
if tun == nil {
|
|
|
|
tun = noopDevice{}
|
|
|
|
}
|
|
|
|
nl.tun = tun
|
|
|
|
nl.tun.SetStatistics(nl.stats)
|
2022-10-12 18:57:13 +00:00
|
|
|
|
2022-12-16 18:14:00 +00:00
|
|
|
// Register the connection tracker into magicsock.
|
|
|
|
if sock == nil {
|
|
|
|
sock = noopDevice{}
|
|
|
|
}
|
|
|
|
nl.sock = sock
|
|
|
|
nl.sock.SetStatistics(nl.stats)
|
2022-10-12 18:57:13 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-02 17:55:05 +00:00
|
|
|
func recordStatistics(logger *logtail.Logger, nodeID tailcfg.StableNodeID, start, end time.Time, connstats, sockStats map[netlogtype.Connection]netlogtype.Counts, addrs map[netip.Addr]bool, prefixes map[netip.Prefix]bool, logExitFlowEnabled bool) {
|
2022-10-28 17:09:30 +00:00
|
|
|
m := netlogtype.Message{NodeID: nodeID, Start: start.UTC(), End: end.UTC()}
|
2022-10-13 17:46:29 +00:00
|
|
|
|
2022-10-12 18:57:13 +00:00
|
|
|
classifyAddr := func(a netip.Addr) (isTailscale, withinRoute bool) {
|
|
|
|
// NOTE: There could be mis-classifications where an address is treated
|
|
|
|
// as a Tailscale IP address because the subnet range overlaps with
|
|
|
|
// the subnet range that Tailscale IP addresses are allocated from.
|
2022-10-13 17:46:29 +00:00
|
|
|
// This should never happen for IPv6, but could happen for IPv4.
|
2022-10-12 18:57:13 +00:00
|
|
|
withinRoute = addrs[a]
|
|
|
|
for p := range prefixes {
|
|
|
|
if p.Contains(a) && p.Bits() > 0 {
|
|
|
|
withinRoute = true
|
2022-11-02 21:25:31 +00:00
|
|
|
break
|
2022-10-12 18:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return withinRoute && tsaddr.IsTailscaleIP(a), withinRoute && !tsaddr.IsTailscaleIP(a)
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:25:31 +00:00
|
|
|
exitTraffic := make(map[netlogtype.Connection]netlogtype.Counts)
|
2022-11-28 23:59:33 +00:00
|
|
|
for conn, cnts := range connstats {
|
2022-10-12 18:57:13 +00:00
|
|
|
srcIsTailscaleIP, srcWithinSubnet := classifyAddr(conn.Src.Addr())
|
|
|
|
dstIsTailscaleIP, dstWithinSubnet := classifyAddr(conn.Dst.Addr())
|
|
|
|
switch {
|
|
|
|
case srcIsTailscaleIP && dstIsTailscaleIP:
|
2022-10-27 21:14:18 +00:00
|
|
|
m.VirtualTraffic = append(m.VirtualTraffic, netlogtype.ConnectionCounts{Connection: conn, Counts: cnts})
|
2022-10-12 18:57:13 +00:00
|
|
|
case srcWithinSubnet || dstWithinSubnet:
|
2022-10-27 21:14:18 +00:00
|
|
|
m.SubnetTraffic = append(m.SubnetTraffic, netlogtype.ConnectionCounts{Connection: conn, Counts: cnts})
|
2022-10-12 18:57:13 +00:00
|
|
|
default:
|
|
|
|
const anonymize = true
|
2024-05-02 17:55:05 +00:00
|
|
|
if anonymize && !logExitFlowEnabled {
|
2022-11-02 21:25:31 +00:00
|
|
|
// Only preserve the address if it is a Tailscale IP address.
|
|
|
|
srcOrig, dstOrig := conn.Src, conn.Dst
|
|
|
|
conn = netlogtype.Connection{} // scrub everything by default
|
|
|
|
if srcIsTailscaleIP {
|
|
|
|
conn.Src = netip.AddrPortFrom(srcOrig.Addr(), 0)
|
|
|
|
}
|
|
|
|
if dstIsTailscaleIP {
|
|
|
|
conn.Dst = netip.AddrPortFrom(dstOrig.Addr(), 0)
|
2022-10-12 18:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-02 21:25:31 +00:00
|
|
|
exitTraffic[conn] = exitTraffic[conn].Add(cnts)
|
2022-10-12 18:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-02 21:25:31 +00:00
|
|
|
for conn, cnts := range exitTraffic {
|
|
|
|
m.ExitTraffic = append(m.ExitTraffic, netlogtype.ConnectionCounts{Connection: conn, Counts: cnts})
|
|
|
|
}
|
2022-10-13 17:46:29 +00:00
|
|
|
for conn, cnts := range sockStats {
|
2022-10-27 21:14:18 +00:00
|
|
|
m.PhysicalTraffic = append(m.PhysicalTraffic, netlogtype.ConnectionCounts{Connection: conn, Counts: cnts})
|
2022-10-12 18:57:13 +00:00
|
|
|
}
|
2022-10-13 17:46:29 +00:00
|
|
|
|
|
|
|
if len(m.VirtualTraffic)+len(m.SubnetTraffic)+len(m.ExitTraffic)+len(m.PhysicalTraffic) > 0 {
|
|
|
|
if b, err := json.Marshal(m); err != nil {
|
|
|
|
logger.Logf("json.Marshal error: %v", err)
|
|
|
|
} else {
|
|
|
|
logger.Logf("%s", b)
|
|
|
|
}
|
2022-10-12 18:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeRouteMaps(cfg *router.Config) (addrs map[netip.Addr]bool, prefixes map[netip.Prefix]bool) {
|
|
|
|
addrs = make(map[netip.Addr]bool)
|
|
|
|
for _, p := range cfg.LocalAddrs {
|
|
|
|
if p.IsSingleIP() {
|
|
|
|
addrs[p.Addr()] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prefixes = make(map[netip.Prefix]bool)
|
|
|
|
insertPrefixes := func(rs []netip.Prefix) {
|
|
|
|
for _, p := range rs {
|
|
|
|
if p.IsSingleIP() {
|
|
|
|
addrs[p.Addr()] = true
|
|
|
|
} else {
|
|
|
|
prefixes[p] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insertPrefixes(cfg.Routes)
|
|
|
|
insertPrefixes(cfg.SubnetRoutes)
|
|
|
|
return addrs, prefixes
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReconfigRoutes configures the network logger with updated routes.
|
2022-10-13 17:46:29 +00:00
|
|
|
// The cfg is used to classify the types of connections captured by
|
|
|
|
// the tun Device passed to Startup.
|
2022-10-12 18:57:13 +00:00
|
|
|
func (nl *Logger) ReconfigRoutes(cfg *router.Config) {
|
|
|
|
nl.mu.Lock()
|
|
|
|
defer nl.mu.Unlock()
|
|
|
|
// TODO(joetsai): There is a race where deleted routes are not known at
|
|
|
|
// the time of extraction. We need to keep old routes around for a bit.
|
|
|
|
nl.addrs, nl.prefixes = makeRouteMaps(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown shuts down the network logger.
|
|
|
|
// This attempts to flush out all pending log messages.
|
2022-10-12 22:05:21 +00:00
|
|
|
// Even if an error is returned, the logger is still shut down.
|
2022-10-12 18:57:13 +00:00
|
|
|
func (nl *Logger) Shutdown(ctx context.Context) error {
|
|
|
|
nl.mu.Lock()
|
|
|
|
defer nl.mu.Unlock()
|
|
|
|
if nl.logger == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-16 18:14:00 +00:00
|
|
|
|
|
|
|
// Shutdown in reverse order of Startup.
|
|
|
|
// Do not hold lock while shutting down since this may flush one last time.
|
2022-10-12 18:57:13 +00:00
|
|
|
nl.mu.Unlock()
|
2022-12-16 18:14:00 +00:00
|
|
|
nl.sock.SetStatistics(nil)
|
|
|
|
nl.tun.SetStatistics(nil)
|
|
|
|
err1 := nl.stats.Shutdown(ctx)
|
|
|
|
err2 := nl.logger.Shutdown(ctx)
|
2022-10-12 18:57:13 +00:00
|
|
|
nl.mu.Lock()
|
|
|
|
|
2022-12-16 18:14:00 +00:00
|
|
|
// Purge state.
|
2022-10-12 18:57:13 +00:00
|
|
|
nl.logger = nil
|
2022-12-16 18:14:00 +00:00
|
|
|
nl.stats = nil
|
|
|
|
nl.tun = nil
|
|
|
|
nl.sock = nil
|
2022-10-12 18:57:13 +00:00
|
|
|
nl.addrs = nil
|
|
|
|
nl.prefixes = nil
|
2022-12-16 18:14:00 +00:00
|
|
|
|
|
|
|
return multierr.New(err1, err2)
|
2022-10-12 18:57:13 +00:00
|
|
|
}
|