2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-08-12 21:03:34 +00:00
|
|
|
|
2023-04-18 21:26:58 +00:00
|
|
|
package netmon
|
2020-08-12 21:03:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-11-18 18:48:53 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-08-12 21:03:34 +00:00
|
|
|
"time"
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2021-11-18 18:48:53 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2020-08-12 21:03:34 +00:00
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-03-03 15:03:07 +00:00
|
|
|
errClosed = errors.New("closed")
|
2020-08-12 21:03:34 +00:00
|
|
|
)
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
type eventMessage struct {
|
|
|
|
eventType string
|
2020-08-25 04:23:52 +00:00
|
|
|
}
|
2020-08-12 21:03:34 +00:00
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
func (eventMessage) ignore() bool { return false }
|
2020-08-25 04:23:52 +00:00
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
type winMon struct {
|
|
|
|
logf logger.Logf
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2023-12-21 17:56:54 +00:00
|
|
|
isActive func() bool
|
2021-03-03 15:03:07 +00:00
|
|
|
messagec chan eventMessage
|
|
|
|
addressChangeCallback *winipcfg.UnicastAddressChangeCallback
|
|
|
|
routeChangeCallback *winipcfg.RouteChangeCallback
|
|
|
|
|
2021-11-18 18:48:53 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
lastLog time.Time // time we last logged about any windows change event
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
// noDeadlockTicker exists just to have something scheduled as
|
|
|
|
// far as the Go runtime is concerned. Otherwise "tailscaled
|
|
|
|
// debug --monitor" thinks it's deadlocked with nothing to do,
|
|
|
|
// as Go's runtime doesn't know about callbacks registered with
|
|
|
|
// Windows.
|
|
|
|
noDeadlockTicker *time.Ticker
|
2020-08-12 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 17:56:54 +00:00
|
|
|
func newOSMon(logf logger.Logf, pm *Monitor) (osMon, error) {
|
2021-03-03 15:03:07 +00:00
|
|
|
m := &winMon{
|
|
|
|
logf: logf,
|
2023-12-21 17:56:54 +00:00
|
|
|
isActive: pm.isActive,
|
2021-03-03 15:03:07 +00:00
|
|
|
messagec: make(chan eventMessage, 1),
|
|
|
|
noDeadlockTicker: time.NewTicker(5000 * time.Hour), // arbitrary
|
2020-11-18 20:32:38 +00:00
|
|
|
}
|
2022-12-13 18:14:38 +00:00
|
|
|
m.ctx, m.cancel = context.WithCancel(context.Background())
|
2020-11-18 20:32:38 +00:00
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
var err error
|
|
|
|
m.addressChangeCallback, err = winipcfg.RegisterUnicastAddressChangeCallback(m.unicastAddressChanged)
|
|
|
|
if err != nil {
|
|
|
|
m.logf("winipcfg.RegisterUnicastAddressChangeCallback error: %v", err)
|
2022-12-13 18:14:38 +00:00
|
|
|
m.cancel()
|
2021-03-03 15:03:07 +00:00
|
|
|
return nil, err
|
2020-08-25 04:23:52 +00:00
|
|
|
}
|
2020-08-12 21:03:34 +00:00
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
m.routeChangeCallback, err = winipcfg.RegisterRouteChangeCallback(m.routeChanged)
|
|
|
|
if err != nil {
|
|
|
|
m.addressChangeCallback.Unregister()
|
|
|
|
m.logf("winipcfg.RegisterRouteChangeCallback error: %v", err)
|
2022-12-13 18:14:38 +00:00
|
|
|
m.cancel()
|
2021-03-03 15:03:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-21 23:28:10 +00:00
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
return m, nil
|
2020-08-12 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 19:03:50 +00:00
|
|
|
func (m *winMon) IsInterestingInterface(iface string) bool { return true }
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
func (m *winMon) Close() (ret error) {
|
|
|
|
m.cancel()
|
|
|
|
m.noDeadlockTicker.Stop()
|
2020-08-12 21:03:34 +00:00
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
if m.addressChangeCallback != nil {
|
|
|
|
if err := m.addressChangeCallback.Unregister(); err != nil {
|
|
|
|
m.logf("addressChangeCallback.Unregister error: %v", err)
|
|
|
|
ret = err
|
|
|
|
} else {
|
|
|
|
m.addressChangeCallback = nil
|
2020-08-25 04:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
if m.routeChangeCallback != nil {
|
|
|
|
if err := m.routeChangeCallback.Unregister(); err != nil {
|
|
|
|
m.logf("routeChangeCallback.Unregister error: %v", err)
|
|
|
|
ret = err
|
|
|
|
} else {
|
|
|
|
m.routeChangeCallback = nil
|
2020-08-25 04:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-03 15:03:07 +00:00
|
|
|
|
|
|
|
return
|
2020-08-25 04:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
func (m *winMon) Receive() (message, error) {
|
2020-08-12 21:03:34 +00:00
|
|
|
if m.ctx.Err() != nil {
|
2021-03-03 15:03:07 +00:00
|
|
|
m.logf("Receive call on closed monitor")
|
2020-08-12 21:03:34 +00:00
|
|
|
return nil, errClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
t0 := time.Now()
|
2020-11-18 20:32:38 +00:00
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
select {
|
|
|
|
case msg := <-m.messagec:
|
2021-11-18 18:48:53 +00:00
|
|
|
now := time.Now()
|
|
|
|
m.mu.Lock()
|
|
|
|
sinceLast := now.Sub(m.lastLog)
|
|
|
|
m.lastLog = now
|
|
|
|
m.mu.Unlock()
|
|
|
|
// If it's either been awhile since we last logged
|
|
|
|
// anything, or if this some route/addr that's not
|
|
|
|
// about a Tailscale IP ("ts" prefix), then log. This
|
|
|
|
// is mainly limited to suppress the flood about our own
|
|
|
|
// route updates after connecting to a large tailnet
|
|
|
|
// and all the IPv4 /32 routes.
|
|
|
|
if sinceLast > 5*time.Second || !strings.HasPrefix(msg.eventType, "ts") {
|
|
|
|
m.logf("got windows change event after %v: evt=%s", time.Since(t0).Round(time.Millisecond), msg.eventType)
|
|
|
|
}
|
2021-03-03 15:03:07 +00:00
|
|
|
return msg, nil
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
return nil, errClosed
|
2020-08-25 04:23:52 +00:00
|
|
|
}
|
2020-08-12 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
// unicastAddressChanged is the callback we register with Windows to call when unicast address changes.
|
2021-11-18 18:48:53 +00:00
|
|
|
func (m *winMon) unicastAddressChanged(_ winipcfg.MibNotificationType, row *winipcfg.MibUnicastIPAddressRow) {
|
2023-12-21 17:56:54 +00:00
|
|
|
if !m.isActive() {
|
|
|
|
// Avoid starting a goroutine that sends events to messagec,
|
|
|
|
// or sending messages to messagec directly, if the monitor
|
|
|
|
// hasn't started and Receive is not yet reading from messagec.
|
|
|
|
//
|
|
|
|
// Doing so can lead to goroutine leaks or deadlocks, especially
|
|
|
|
// if the monitor is never started.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-18 18:48:53 +00:00
|
|
|
what := "addr"
|
2022-08-11 17:23:35 +00:00
|
|
|
if ip := row.Address.Addr(); ip.IsValid() && tsaddr.IsTailscaleIP(ip.Unmap()) {
|
2021-11-18 18:48:53 +00:00
|
|
|
what = "tsaddr"
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
// start a goroutine to finish our work, to return to Windows out of this callback
|
2021-11-18 18:48:53 +00:00
|
|
|
go m.somethingChanged(what)
|
2020-08-12 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
// routeChanged is the callback we register with Windows to call when route changes.
|
2021-11-18 18:48:53 +00:00
|
|
|
func (m *winMon) routeChanged(_ winipcfg.MibNotificationType, row *winipcfg.MibIPforwardRow2) {
|
2023-12-21 17:56:54 +00:00
|
|
|
if !m.isActive() {
|
|
|
|
// Avoid starting a goroutine that sends events to messagec,
|
|
|
|
// or sending messages to messagec directly, if the monitor
|
|
|
|
// hasn't started and Receive is not yet reading from messagec.
|
|
|
|
//
|
|
|
|
// Doing so can lead to goroutine leaks or deadlocks, especially
|
|
|
|
// if the monitor is never started.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-18 18:48:53 +00:00
|
|
|
what := "route"
|
2022-08-11 17:23:35 +00:00
|
|
|
ip := row.DestinationPrefix.Prefix().Addr().Unmap()
|
|
|
|
if ip.IsValid() && tsaddr.IsTailscaleIP(ip) {
|
2021-11-18 18:48:53 +00:00
|
|
|
what = "tsroute"
|
|
|
|
}
|
2021-03-03 15:03:07 +00:00
|
|
|
// start a goroutine to finish our work, to return to Windows out of this callback
|
2021-11-18 18:48:53 +00:00
|
|
|
go m.somethingChanged(what)
|
2020-08-12 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 15:03:07 +00:00
|
|
|
// somethingChanged gets called from OS callbacks whenever address or route changes.
|
|
|
|
func (m *winMon) somethingChanged(evt string) {
|
|
|
|
select {
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
return
|
|
|
|
case m.messagec <- eventMessage{eventType: evt}:
|
|
|
|
return
|
2020-08-12 21:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-22 01:40:03 +00:00
|
|
|
|
|
|
|
// isActive reports whether this monitor has been started and not yet closed.
|
|
|
|
func (m *Monitor) isActive() bool {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
return m.started && !m.closed
|
|
|
|
}
|