2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
package wgengine
|
|
|
|
|
|
|
|
import (
|
2020-04-10 15:42:34 +00:00
|
|
|
"errors"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
"net/netip"
|
2022-03-27 03:12:12 +00:00
|
|
|
"time"
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-03-26 05:57:46 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2021-04-02 07:34:32 +00:00
|
|
|
"tailscale.com/net/dns"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-11-02 21:41:56 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2023-01-19 22:28:49 +00:00
|
|
|
"tailscale.com/wgengine/capture"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
2021-03-01 20:56:03 +00:00
|
|
|
"tailscale.com/wgengine/monitor"
|
2020-05-11 21:02:12 +00:00
|
|
|
"tailscale.com/wgengine/router"
|
2021-01-29 20:16:36 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// Status is the Engine status.
|
2020-03-26 05:57:46 +00:00
|
|
|
//
|
|
|
|
// TODO(bradfitz): remove this, subset of ipnstate? Need to migrate users.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Status struct {
|
2022-03-27 03:12:12 +00:00
|
|
|
AsOf time.Time // the time at which the status was calculated
|
2021-02-04 21:12:42 +00:00
|
|
|
Peers []ipnstate.PeerStatusLite
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
LocalAddrs []tailcfg.Endpoint // the set of possible endpoints for the magic conn
|
|
|
|
DERPs int // number of active DERP connections
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// StatusCallback is the type of status callbacks used by
|
|
|
|
// Engine.SetStatusCallback.
|
|
|
|
//
|
|
|
|
// Exactly one of Status or error is non-nil.
|
|
|
|
type StatusCallback func(*Status, error)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-03-04 06:21:56 +00:00
|
|
|
// NetInfoCallback is the type used by Engine.SetNetInfoCallback.
|
|
|
|
type NetInfoCallback func(*tailcfg.NetInfo)
|
|
|
|
|
2021-01-15 14:16:28 +00:00
|
|
|
// NetworkMapCallback is the type used by callbacks that hook
|
|
|
|
// into network map updates.
|
2021-02-05 23:44:46 +00:00
|
|
|
type NetworkMapCallback func(*netmap.NetworkMap)
|
2021-01-15 14:16:28 +00:00
|
|
|
|
|
|
|
// someHandle is allocated so its pointer address acts as a unique
|
|
|
|
// map key handle. (It needs to have non-zero size for Go to guarantee
|
|
|
|
// the pointer is unique.)
|
|
|
|
type someHandle struct{ _ byte }
|
|
|
|
|
2020-04-10 15:42:34 +00:00
|
|
|
// ErrNoChanges is returned by Engine.Reconfig if no changes were made.
|
|
|
|
var ErrNoChanges = errors.New("no changes made to Engine config")
|
|
|
|
|
2021-11-30 18:30:44 +00:00
|
|
|
// PeerForIP is the type returned by Engine.PeerForIP.
|
|
|
|
type PeerForIP struct {
|
|
|
|
// Node is the matched node. It's always non-nil when
|
|
|
|
// Engine.PeerForIP returns ok==true.
|
|
|
|
Node *tailcfg.Node
|
|
|
|
|
|
|
|
// IsSelf is whether the Node is the local process.
|
|
|
|
IsSelf bool
|
|
|
|
|
|
|
|
// Route is the route that matched the IP provided
|
|
|
|
// to Engine.PeerForIP.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Route netip.Prefix
|
2021-11-30 18:30:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// Engine is the Tailscale WireGuard engine interface.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Engine interface {
|
2020-02-11 23:21:24 +00:00
|
|
|
// Reconfig reconfigures WireGuard and makes sure it's running.
|
2020-02-05 22:16:58 +00:00
|
|
|
// This also handles setting up any kernel routes.
|
2020-02-11 23:21:24 +00:00
|
|
|
//
|
2020-06-24 21:10:42 +00:00
|
|
|
// This is called whenever tailcontrol (the control plane)
|
2020-02-11 23:21:24 +00:00
|
|
|
// sends an updated network map.
|
2020-04-10 15:42:34 +00:00
|
|
|
//
|
2021-06-22 20:13:59 +00:00
|
|
|
// The *tailcfg.Debug parameter can be nil.
|
|
|
|
//
|
2020-04-10 15:42:34 +00:00
|
|
|
// The returned error is ErrNoChanges if no changes were made.
|
2021-06-22 20:13:59 +00:00
|
|
|
Reconfig(*wgcfg.Config, *router.Config, *dns.Config, *tailcfg.Debug) error
|
2020-02-11 23:21:24 +00:00
|
|
|
|
2021-11-30 18:30:44 +00:00
|
|
|
// PeerForIP returns the node to which the provided IP routes,
|
2022-10-12 21:14:22 +00:00
|
|
|
// if any. If none is found, (nil, false) is returned.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
PeerForIP(netip.Addr) (_ PeerForIP, ok bool)
|
2021-11-30 18:30:44 +00:00
|
|
|
|
2020-03-25 15:40:36 +00:00
|
|
|
// GetFilter returns the current packet filter, if any.
|
2020-03-25 07:47:55 +00:00
|
|
|
GetFilter() *filter.Filter
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// SetFilter updates the packet filter.
|
|
|
|
SetFilter(*filter.Filter)
|
|
|
|
|
|
|
|
// SetStatusCallback sets the function to call when the
|
|
|
|
// WireGuard status changes.
|
|
|
|
SetStatusCallback(StatusCallback)
|
|
|
|
|
2021-03-01 20:56:03 +00:00
|
|
|
// GetLinkMonitor returns the link monitor.
|
|
|
|
GetLinkMonitor() *monitor.Mon
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// RequestStatus requests a WireGuard status update right
|
|
|
|
// away, sent to the callback registered via SetStatusCallback.
|
2020-02-05 22:16:58 +00:00
|
|
|
RequestStatus()
|
2020-02-11 23:21:24 +00:00
|
|
|
|
|
|
|
// Close shuts down this wireguard instance, remove any routes
|
|
|
|
// it added, etc. To bring it up again later, you'll need a
|
|
|
|
// new Engine.
|
2020-02-05 22:16:58 +00:00
|
|
|
Close()
|
2020-02-11 23:21:24 +00:00
|
|
|
|
|
|
|
// Wait waits until the Engine's Close method is called or the
|
|
|
|
// engine aborts with an error. You don't have to call this.
|
|
|
|
// TODO: return an error?
|
2020-02-05 22:16:58 +00:00
|
|
|
Wait()
|
2020-02-11 23:21:24 +00:00
|
|
|
|
2021-03-04 04:58:09 +00:00
|
|
|
// LinkChange informs the engine that the system network
|
|
|
|
// link has changed.
|
|
|
|
//
|
|
|
|
// The isExpensive parameter is not used.
|
|
|
|
//
|
|
|
|
// LinkChange should be called whenever something changed with
|
|
|
|
// the network, no matter how minor.
|
|
|
|
//
|
|
|
|
// Deprecated: don't use this method. It was removed shortly
|
|
|
|
// before the Tailscale 1.6 release when we remembered that
|
|
|
|
// Android doesn't use the Linux-based link monitor and has
|
|
|
|
// its own mechanism that uses LinkChange. Android is the only
|
|
|
|
// caller of this method now. Don't add more.
|
|
|
|
LinkChange(isExpensive bool)
|
|
|
|
|
2020-05-17 16:51:38 +00:00
|
|
|
// SetDERPMap controls which (if any) DERP servers are used.
|
|
|
|
// If nil, DERP is disabled. It starts disabled until a DERP map
|
|
|
|
// is configured.
|
|
|
|
SetDERPMap(*tailcfg.DERPMap)
|
2020-03-04 20:21:40 +00:00
|
|
|
|
2020-06-25 18:04:52 +00:00
|
|
|
// SetNetworkMap informs the engine of the latest network map
|
|
|
|
// from the server. The network map's DERPMap field should be
|
|
|
|
// ignored as as it might be disabled; get it from SetDERPMap
|
|
|
|
// instead.
|
|
|
|
// The network map should only be read from.
|
2021-02-05 23:44:46 +00:00
|
|
|
SetNetworkMap(*netmap.NetworkMap)
|
2020-06-25 18:04:52 +00:00
|
|
|
|
2021-01-15 14:16:28 +00:00
|
|
|
// AddNetworkMapCallback adds a function to a list of callbacks
|
|
|
|
// that are called when the network map updates. It returns a
|
|
|
|
// function that when called would remove the function from the
|
|
|
|
// list of callbacks.
|
|
|
|
AddNetworkMapCallback(NetworkMapCallback) (removeCallback func())
|
|
|
|
|
2020-03-04 06:21:56 +00:00
|
|
|
// SetNetInfoCallback sets the function to call when a
|
|
|
|
// new NetInfo summary is available.
|
|
|
|
SetNetInfoCallback(NetInfoCallback)
|
2020-03-26 05:57:46 +00:00
|
|
|
|
2020-07-06 19:10:39 +00:00
|
|
|
// DiscoPublicKey gets the public key used for path discovery
|
2020-06-19 19:06:49 +00:00
|
|
|
// messages.
|
2021-11-02 21:41:56 +00:00
|
|
|
DiscoPublicKey() key.DiscoPublic
|
2020-06-19 19:06:49 +00:00
|
|
|
|
2020-03-26 05:57:46 +00:00
|
|
|
// UpdateStatus populates the network state using the provided
|
|
|
|
// status builder.
|
|
|
|
UpdateStatus(*ipnstate.StatusBuilder)
|
2020-08-09 21:49:42 +00:00
|
|
|
|
2022-04-22 01:49:01 +00:00
|
|
|
// Ping is a request to start a ping with the peer handling the given IP and
|
|
|
|
// then call cb with its ping latency & method.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Ping(ip netip.Addr, pingType tailcfg.PingType, cb func(*ipnstate.PingResult))
|
2021-03-15 21:59:35 +00:00
|
|
|
|
|
|
|
// RegisterIPPortIdentity registers a given node (identified by its
|
|
|
|
// Tailscale IP) as temporarily having the given IP:port for whois lookups.
|
|
|
|
// The IP:port is generally a localhost IP and an ephemeral port, used
|
2021-11-30 18:30:44 +00:00
|
|
|
// while proxying connections to localhost when tailscaled is running
|
|
|
|
// in netstack mode.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
RegisterIPPortIdentity(netip.AddrPort, netip.Addr)
|
2021-03-15 21:59:35 +00:00
|
|
|
|
2021-11-30 18:30:44 +00:00
|
|
|
// UnregisterIPPortIdentity removes a temporary IP:port registration
|
|
|
|
// made previously by RegisterIPPortIdentity.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
UnregisterIPPortIdentity(netip.AddrPort)
|
2021-03-15 21:59:35 +00:00
|
|
|
|
|
|
|
// WhoIsIPPort looks up an IP:port in the temporary registrations,
|
|
|
|
// and returns a matching Tailscale IP, if it exists.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
WhoIsIPPort(netip.AddrPort) (netip.Addr, bool)
|
2023-01-19 22:28:49 +00:00
|
|
|
|
|
|
|
// InstallCaptureHook registers a function to be called to capture
|
|
|
|
// packets traversing the data path. The hook can be uninstalled by
|
|
|
|
// calling this function with a nil value.
|
2023-02-08 23:48:27 +00:00
|
|
|
InstallCaptureHook(capture.Callback)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|