2020-02-12 20:53:55 +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 monitor provides facilities for monitoring network
|
2020-07-07 00:36:57 +01:00
|
|
|
// interface and route changes. It primarily exists to know when
|
|
|
|
// portable devices move between different networks.
|
2020-02-12 20:53:55 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
2021-03-05 18:19:27 +00:00
|
|
|
"encoding/json"
|
2021-03-03 05:45:40 +00:00
|
|
|
"errors"
|
2021-03-25 17:23:13 +00:00
|
|
|
"runtime"
|
2020-02-17 17:00:38 +00:00
|
|
|
"sync"
|
2020-02-12 20:53:55 +00:00
|
|
|
"time"
|
|
|
|
|
2021-03-15 20:58:10 +00:00
|
|
|
"inet.af/netaddr"
|
2021-03-01 15:55:30 +00:00
|
|
|
"tailscale.com/net/interfaces"
|
2020-02-15 03:23:16 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-02-12 20:53:55 +00:00
|
|
|
)
|
|
|
|
|
2021-03-25 17:23:13 +00:00
|
|
|
// pollWallTimeInterval is how often we check the time to check
|
|
|
|
// for big jumps in wall (non-monotonic) time as a backup mechanism
|
|
|
|
// to get notified of a sleeping device waking back up.
|
|
|
|
// Usually there are also minor network change events on wake that let
|
|
|
|
// us check the wall time sooner than this.
|
|
|
|
const pollWallTimeInterval = 15 * time.Second
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
// message represents a message returned from an osMon.
|
2020-07-07 00:36:57 +01:00
|
|
|
type message interface {
|
|
|
|
// Ignore is whether we should ignore this message.
|
|
|
|
ignore() bool
|
|
|
|
}
|
2020-02-12 20:53:55 +00:00
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
// osMon is the interface that each operating system-specific
|
|
|
|
// implementation of the link monitor must implement.
|
|
|
|
type osMon interface {
|
2020-02-12 20:53:55 +00:00
|
|
|
Close() error
|
2020-02-17 17:00:38 +00:00
|
|
|
|
|
|
|
// Receive returns a new network interface change message. It
|
|
|
|
// should block until there's either something to return, or
|
|
|
|
// until the osMon is closed. After a Close, the returned
|
|
|
|
// error is ignored.
|
|
|
|
Receive() (message, error)
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 20:56:03 +00:00
|
|
|
// ChangeFunc is a callback function that's called when the network
|
|
|
|
// changed. The changed parameter is whether the network changed
|
|
|
|
// enough for interfaces.State to have changed since the last
|
|
|
|
// callback.
|
|
|
|
type ChangeFunc func(changed bool, state *interfaces.State)
|
2020-02-12 20:53:55 +00:00
|
|
|
|
2021-02-28 03:33:21 +00:00
|
|
|
// An allocated callbackHandle's address is the Mon.cbs map key.
|
|
|
|
type callbackHandle byte
|
|
|
|
|
2020-02-12 20:53:55 +00:00
|
|
|
// Mon represents a monitoring instance.
|
|
|
|
type Mon struct {
|
|
|
|
logf logger.Logf
|
2020-02-17 17:00:38 +00:00
|
|
|
om osMon // nil means not supported on this platform
|
2020-02-12 20:53:55 +00:00
|
|
|
change chan struct{}
|
2021-03-25 17:23:13 +00:00
|
|
|
stop chan struct{} // closed on Stop
|
2021-02-28 03:33:21 +00:00
|
|
|
|
2021-03-25 17:23:13 +00:00
|
|
|
mu sync.Mutex // guards all following fields
|
|
|
|
cbs map[*callbackHandle]ChangeFunc
|
2021-07-20 22:10:52 +01:00
|
|
|
ruleDelCB map[*callbackHandle]RuleDeleteCallback
|
2021-03-25 17:23:13 +00:00
|
|
|
ifState *interfaces.State
|
|
|
|
gwValid bool // whether gw and gwSelfIP are valid
|
|
|
|
gw netaddr.IP // our gateway's IP
|
|
|
|
gwSelfIP netaddr.IP // our own IP address (that corresponds to gw)
|
2020-02-17 17:00:38 +00:00
|
|
|
started bool
|
2021-03-25 17:23:13 +00:00
|
|
|
closed bool
|
2020-02-17 17:00:38 +00:00
|
|
|
goroutines sync.WaitGroup
|
2021-03-25 17:23:13 +00:00
|
|
|
wallTimer *time.Timer // nil until Started; re-armed AfterFunc per tick
|
|
|
|
lastWall time.Time
|
|
|
|
timeJumped bool // whether we need to send a changed=true after a big time jump
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 03:33:21 +00:00
|
|
|
// New instantiates and starts a monitoring instance.
|
2020-02-17 17:00:38 +00:00
|
|
|
// The returned monitor is inactive until it's started by the Start method.
|
2021-02-28 03:33:21 +00:00
|
|
|
// Use RegisterChangeCallback to get notified of network changes.
|
|
|
|
func New(logf logger.Logf) (*Mon, error) {
|
2020-07-07 00:36:57 +01:00
|
|
|
logf = logger.WithPrefix(logf, "monitor: ")
|
2021-03-01 20:56:03 +00:00
|
|
|
m := &Mon{
|
2021-03-25 17:23:13 +00:00
|
|
|
logf: logf,
|
|
|
|
cbs: map[*callbackHandle]ChangeFunc{},
|
|
|
|
change: make(chan struct{}, 1),
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
lastWall: wallTime(),
|
2021-03-01 20:56:03 +00:00
|
|
|
}
|
|
|
|
st, err := m.interfaceStateUncached()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m.ifState = st
|
2021-03-03 05:45:40 +00:00
|
|
|
|
|
|
|
m.om, err = newOSMon(logf, m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if m.om == nil {
|
|
|
|
return nil, errors.New("newOSMon returned nil, nil")
|
|
|
|
}
|
|
|
|
|
2021-03-01 20:56:03 +00:00
|
|
|
return m, nil
|
2020-02-17 17:00:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 17:51:12 +00:00
|
|
|
// InterfaceState returns the latest snapshot of the machine's network
|
|
|
|
// interfaces.
|
|
|
|
//
|
|
|
|
// The returned value is owned by Mon; it must not be modified.
|
2021-03-01 20:56:03 +00:00
|
|
|
func (m *Mon) InterfaceState() *interfaces.State {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
return m.ifState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mon) interfaceStateUncached() (*interfaces.State, error) {
|
2021-03-26 16:09:12 +00:00
|
|
|
return interfaces.GetState()
|
2021-03-01 15:55:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 20:58:10 +00:00
|
|
|
// GatewayAndSelfIP returns the current network's default gateway, and
|
|
|
|
// the machine's default IP for that gateway.
|
|
|
|
//
|
|
|
|
// It's the same as interfaces.LikelyHomeRouterIP, but it caches the
|
|
|
|
// result until the monitor detects a network change.
|
|
|
|
func (m *Mon) GatewayAndSelfIP() (gw, myIP netaddr.IP, ok bool) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if m.gwValid {
|
|
|
|
return m.gw, m.gwSelfIP, true
|
|
|
|
}
|
|
|
|
gw, myIP, ok = interfaces.LikelyHomeRouterIP()
|
|
|
|
if ok {
|
|
|
|
m.gw, m.gwSelfIP, m.gwValid = gw, myIP, true
|
|
|
|
}
|
|
|
|
return gw, myIP, ok
|
|
|
|
}
|
|
|
|
|
2021-02-28 03:33:21 +00:00
|
|
|
// RegisterChangeCallback adds callback to the set of parties to be
|
|
|
|
// notified (in their own goroutine) when the network state changes.
|
|
|
|
// To remove this callback, call unregister (or close the monitor).
|
|
|
|
func (m *Mon) RegisterChangeCallback(callback ChangeFunc) (unregister func()) {
|
|
|
|
handle := new(callbackHandle)
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.cbs[handle] = callback
|
|
|
|
return func() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
delete(m.cbs, handle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 22:10:52 +01:00
|
|
|
// RuleDeleteCallback is a callback when a Linux IP policy routing
|
|
|
|
// rule is deleted. The table is the table number (52, 253, 354) and
|
|
|
|
// priority is the priority order number (for Tailscale rules
|
|
|
|
// currently: 5210, 5230, 5250, 5270)
|
|
|
|
type RuleDeleteCallback func(table uint8, priority uint32)
|
|
|
|
|
|
|
|
// RegisterRuleDeleteCallback adds callback to the set of parties to be
|
|
|
|
// notified (in their own goroutine) when a Linux ip rule is deleted.
|
|
|
|
// To remove this callback, call unregister (or close the monitor).
|
|
|
|
func (m *Mon) RegisterRuleDeleteCallback(callback RuleDeleteCallback) (unregister func()) {
|
|
|
|
handle := new(callbackHandle)
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if m.ruleDelCB == nil {
|
|
|
|
m.ruleDelCB = map[*callbackHandle]RuleDeleteCallback{}
|
|
|
|
}
|
|
|
|
m.ruleDelCB[handle] = callback
|
|
|
|
return func() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
delete(m.ruleDelCB, handle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
// Start starts the monitor.
|
|
|
|
// A monitor can only be started & closed once.
|
|
|
|
func (m *Mon) Start() {
|
2021-03-25 17:23:13 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if m.started || m.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.started = true
|
|
|
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "ios", "android":
|
|
|
|
// For battery reasons, and because these platforms
|
|
|
|
// don't really sleep in the same way, don't poll
|
|
|
|
// for the wall time to detect for wake-for-sleep
|
|
|
|
// walltime jumps.
|
|
|
|
default:
|
|
|
|
m.wallTimer = time.AfterFunc(pollWallTimeInterval, m.pollWallTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.om == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.goroutines.Add(2)
|
|
|
|
go m.pump()
|
|
|
|
go m.debounce()
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
// Close closes the monitor.
|
2020-02-12 20:53:55 +00:00
|
|
|
func (m *Mon) Close() error {
|
2021-03-25 17:23:13 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
if m.closed {
|
|
|
|
m.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
m.closed = true
|
2020-02-12 20:53:55 +00:00
|
|
|
close(m.stop)
|
2021-03-25 17:23:13 +00:00
|
|
|
|
|
|
|
if m.wallTimer != nil {
|
|
|
|
m.wallTimer.Stop()
|
|
|
|
}
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
var err error
|
|
|
|
if m.om != nil {
|
|
|
|
err = m.om.Close()
|
|
|
|
}
|
2021-03-25 17:23:13 +00:00
|
|
|
|
|
|
|
started := m.started
|
|
|
|
m.mu.Unlock()
|
|
|
|
|
|
|
|
if started {
|
2020-02-17 17:00:38 +00:00
|
|
|
m.goroutines.Wait()
|
|
|
|
}
|
|
|
|
return err
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 04:58:09 +00:00
|
|
|
// InjectEvent forces the monitor to pretend there was a network
|
|
|
|
// change and re-check the state of the network. Any registered
|
|
|
|
// ChangeFunc callbacks will be called within the event coalescing
|
|
|
|
// period (under a fraction of a second).
|
|
|
|
func (m *Mon) InjectEvent() {
|
|
|
|
select {
|
|
|
|
case m.change <- struct{}{}:
|
|
|
|
default:
|
|
|
|
// Another change signal is already
|
|
|
|
// buffered. Debounce will wake up soon
|
|
|
|
// enough.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 18:29:49 +00:00
|
|
|
func (m *Mon) stopped() bool {
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 20:53:55 +00:00
|
|
|
// pump continuously retrieves messages from the connection, notifying
|
|
|
|
// the change channel of changes, and stopping when a stop is issued.
|
|
|
|
func (m *Mon) pump() {
|
2020-02-17 17:00:38 +00:00
|
|
|
defer m.goroutines.Done()
|
2021-03-02 18:29:49 +00:00
|
|
|
for !m.stopped() {
|
2020-07-07 00:36:57 +01:00
|
|
|
msg, err := m.om.Receive()
|
2020-02-12 20:53:55 +00:00
|
|
|
if err != nil {
|
2021-03-02 18:29:49 +00:00
|
|
|
if m.stopped() {
|
2020-02-12 20:53:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Keep retrying while we're not closed.
|
2020-08-25 05:23:52 +01:00
|
|
|
m.logf("error from link monitor: %v", err)
|
2020-02-12 20:53:55 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2021-07-20 22:10:52 +01:00
|
|
|
if rdm, ok := msg.(ipRuleDeletedMessage); ok {
|
|
|
|
m.notifyRuleDeleted(rdm)
|
|
|
|
continue
|
|
|
|
}
|
2020-07-07 00:36:57 +01:00
|
|
|
if msg.ignore() {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-04 04:58:09 +00:00
|
|
|
m.InjectEvent()
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 22:10:52 +01:00
|
|
|
func (m *Mon) notifyRuleDeleted(rdm ipRuleDeletedMessage) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
for _, cb := range m.ruleDelCB {
|
|
|
|
go cb(rdm.table, rdm.priority)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 20:53:55 +00:00
|
|
|
// debounce calls the callback function with a delay between events
|
|
|
|
// and exits when a stop is issued.
|
|
|
|
func (m *Mon) debounce() {
|
2020-02-17 17:00:38 +00:00
|
|
|
defer m.goroutines.Done()
|
2020-02-12 20:53:55 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
case <-m.change:
|
|
|
|
}
|
|
|
|
|
2021-03-01 20:56:03 +00:00
|
|
|
if curState, err := m.interfaceStateUncached(); err != nil {
|
|
|
|
m.logf("interfaces.State: %v", err)
|
|
|
|
} else {
|
|
|
|
m.mu.Lock()
|
2021-03-25 17:23:13 +00:00
|
|
|
|
|
|
|
// See if we have a queued or new time jump signal.
|
|
|
|
m.checkWallTimeAdvanceLocked()
|
|
|
|
timeJumped := m.timeJumped
|
|
|
|
if timeJumped {
|
|
|
|
m.logf("time jumped (probably wake from sleep); synthesizing major change event")
|
|
|
|
}
|
|
|
|
|
2021-03-05 18:19:27 +00:00
|
|
|
oldState := m.ifState
|
2021-11-20 01:04:35 +00:00
|
|
|
ifChanged := !curState.EqualFiltered(oldState, interfaces.UseInterestingInterfaces, interfaces.UseInterestingIPs)
|
2021-03-25 17:23:13 +00:00
|
|
|
if ifChanged {
|
2021-03-15 20:58:10 +00:00
|
|
|
m.gwValid = false
|
2021-03-01 20:56:03 +00:00
|
|
|
m.ifState = curState
|
2021-03-05 18:19:27 +00:00
|
|
|
|
|
|
|
if s1, s2 := oldState.String(), curState.String(); s1 == s2 {
|
|
|
|
m.logf("[unexpected] network state changed, but stringification didn't: %v\nold: %s\nnew: %s\n", s1,
|
|
|
|
jsonSummary(oldState), jsonSummary(curState))
|
|
|
|
}
|
2021-03-01 20:56:03 +00:00
|
|
|
}
|
2021-03-25 17:23:13 +00:00
|
|
|
changed := ifChanged || timeJumped
|
|
|
|
if changed {
|
|
|
|
m.timeJumped = false
|
|
|
|
}
|
2021-03-01 20:56:03 +00:00
|
|
|
for _, cb := range m.cbs {
|
|
|
|
go cb(changed, m.ifState)
|
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
2021-02-28 03:33:21 +00:00
|
|
|
}
|
2020-02-12 20:53:55 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
2021-03-02 18:29:49 +00:00
|
|
|
case <-time.After(250 * time.Millisecond):
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 18:19:27 +00:00
|
|
|
|
|
|
|
func jsonSummary(x interface{}) interface{} {
|
|
|
|
j, err := json.Marshal(x)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return j
|
|
|
|
}
|
2021-03-25 17:23:13 +00:00
|
|
|
|
|
|
|
func wallTime() time.Time {
|
|
|
|
// From time package's docs: "The canonical way to strip a
|
|
|
|
// monotonic clock reading is to use t = t.Round(0)."
|
|
|
|
return time.Now().Round(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mon) pollWallTime() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if m.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.checkWallTimeAdvanceLocked()
|
|
|
|
if m.timeJumped {
|
|
|
|
m.InjectEvent()
|
|
|
|
}
|
|
|
|
m.wallTimer.Reset(pollWallTimeInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkWallTimeAdvanceLocked updates m.timeJumped, if wall time jumped
|
|
|
|
// more than 150% of pollWallTimeInterval, indicating we probably just
|
|
|
|
// came out of sleep.
|
|
|
|
func (m *Mon) checkWallTimeAdvanceLocked() {
|
|
|
|
now := wallTime()
|
|
|
|
if now.Sub(m.lastWall) > pollWallTimeInterval*3/2 {
|
|
|
|
m.timeJumped = true
|
|
|
|
}
|
|
|
|
m.lastWall = now
|
|
|
|
}
|
2021-07-20 22:10:52 +01:00
|
|
|
|
|
|
|
type ipRuleDeletedMessage struct {
|
|
|
|
table uint8
|
|
|
|
priority uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ipRuleDeletedMessage) ignore() bool { return true }
|