2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-04-19 02:49:25 +00:00
|
|
|
|
|
|
|
package controlclient
|
|
|
|
|
|
|
|
import (
|
2023-08-21 02:52:52 +00:00
|
|
|
"context"
|
2022-07-20 20:31:20 +00:00
|
|
|
"fmt"
|
2021-04-19 02:49:25 +00:00
|
|
|
"log"
|
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"
|
2021-04-19 02:49:25 +00:00
|
|
|
"sort"
|
|
|
|
|
2022-01-24 18:52:57 +00:00
|
|
|
"tailscale.com/envknob"
|
2021-04-19 02:49:25 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2023-08-21 02:52:52 +00:00
|
|
|
"tailscale.com/tstime"
|
2021-09-03 20:17:46 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-04-19 02:49:25 +00:00
|
|
|
"tailscale.com/types/logger"
|
|
|
|
"tailscale.com/types/netmap"
|
2023-01-03 23:39:32 +00:00
|
|
|
"tailscale.com/types/views"
|
2023-08-21 02:52:52 +00:00
|
|
|
"tailscale.com/util/cmpx"
|
2021-04-19 02:49:25 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
|
|
|
)
|
|
|
|
|
|
|
|
// mapSession holds the state over a long-polled "map" request to the
|
|
|
|
// control plane.
|
|
|
|
//
|
|
|
|
// It accepts incremental tailcfg.MapResponse values to
|
|
|
|
// netMapForResponse and returns fully inflated NetworkMaps, filling
|
|
|
|
// in the omitted data implicit from prior MapResponse values from
|
|
|
|
// within the same session (the same long-poll HTTP response to the
|
|
|
|
// one MapRequest).
|
|
|
|
type mapSession struct {
|
|
|
|
// Immutable fields.
|
2023-08-21 02:52:52 +00:00
|
|
|
nu NetmapUpdater // called on changes (in addition to the optional hooks below)
|
2023-08-19 16:42:34 +00:00
|
|
|
privateNodeKey key.NodePrivate
|
|
|
|
logf logger.Logf
|
|
|
|
vlogf logger.Logf
|
|
|
|
machinePubKey key.MachinePublic
|
2023-08-21 02:52:52 +00:00
|
|
|
altClock tstime.Clock // if nil, regular time is used
|
|
|
|
cancel context.CancelFunc // always non-nil, shuts down caller's base long poll context
|
|
|
|
watchdogReset chan struct{} // send to request that the long poll activity watchdog timeout be reset
|
|
|
|
|
|
|
|
// sessionAliveCtx is a Background-based context that's alive for the
|
|
|
|
// duration of the mapSession that we own the lifetime of. It's closed by
|
|
|
|
// sessionAliveCtxClose.
|
|
|
|
sessionAliveCtx context.Context
|
|
|
|
sessionAliveCtxClose context.CancelFunc // closes sessionAliveCtx
|
|
|
|
|
|
|
|
// Optional hooks, set once before use.
|
|
|
|
|
|
|
|
// onDebug specifies what to do with a *tailcfg.Debug message.
|
|
|
|
// If the watchdogReset chan is nil, it's not used. Otherwise it can be sent to
|
|
|
|
// to request that the long poll activity watchdog timeout be reset.
|
|
|
|
onDebug func(_ context.Context, _ *tailcfg.Debug, watchdogReset chan<- struct{}) error
|
|
|
|
|
|
|
|
// onConciseNetMapSummary, if non-nil, is called with the Netmap.VeryConcise summary
|
|
|
|
// whenever a map response is received.
|
|
|
|
onConciseNetMapSummary func(string)
|
|
|
|
|
|
|
|
// onSelfNodeChanged is called before the NetmapUpdater if the self node was
|
|
|
|
// changed.
|
|
|
|
onSelfNodeChanged func(*netmap.NetworkMap)
|
2021-04-19 02:49:25 +00:00
|
|
|
|
2022-09-25 18:29:55 +00:00
|
|
|
// Fields storing state over the course of multiple MapResponses.
|
2021-04-19 17:56:48 +00:00
|
|
|
lastNode *tailcfg.Node
|
2021-04-19 02:49:25 +00:00
|
|
|
lastDNSConfig *tailcfg.DNSConfig
|
|
|
|
lastDERPMap *tailcfg.DERPMap
|
|
|
|
lastUserProfile map[tailcfg.UserID]tailcfg.UserProfile
|
2023-01-03 23:39:32 +00:00
|
|
|
lastPacketFilterRules views.Slice[tailcfg.FilterRule]
|
2021-04-19 02:49:25 +00:00
|
|
|
lastParsedPacketFilter []filter.Match
|
2022-02-10 22:12:34 +00:00
|
|
|
lastSSHPolicy *tailcfg.SSHPolicy
|
2021-04-19 02:49:25 +00:00
|
|
|
collectServices bool
|
|
|
|
previousPeers []*tailcfg.Node // for delta-purposes
|
2021-04-19 03:48:24 +00:00
|
|
|
lastDomain string
|
2022-10-06 23:19:38 +00:00
|
|
|
lastDomainAuditLogID string
|
2021-09-18 19:59:55 +00:00
|
|
|
lastHealth []string
|
2022-03-21 21:10:25 +00:00
|
|
|
lastPopBrowserURL string
|
2022-08-09 17:16:34 +00:00
|
|
|
stickyDebug tailcfg.Debug // accumulated opt.Bool values
|
2022-09-06 23:34:16 +00:00
|
|
|
lastTKAInfo *tailcfg.TKAInfo
|
2023-08-21 02:52:52 +00:00
|
|
|
lastNetmapSummary string // from NetworkMap.VeryConcise
|
2021-04-19 02:49:25 +00:00
|
|
|
|
|
|
|
// netMapBuilding is non-nil during a netmapForResponse call,
|
|
|
|
// containing the value to be returned, once fully populated.
|
|
|
|
netMapBuilding *netmap.NetworkMap
|
|
|
|
}
|
|
|
|
|
2023-08-21 02:52:52 +00:00
|
|
|
// newMapSession returns a mostly unconfigured new mapSession.
|
|
|
|
//
|
|
|
|
// Modify its optional fields on the returned value before use.
|
|
|
|
//
|
|
|
|
// It must have its Close method called to release resources.
|
|
|
|
func newMapSession(privateNodeKey key.NodePrivate, nu NetmapUpdater) *mapSession {
|
2021-04-19 02:49:25 +00:00
|
|
|
ms := &mapSession{
|
2023-08-21 02:52:52 +00:00
|
|
|
nu: nu,
|
2023-01-11 20:59:08 +00:00
|
|
|
privateNodeKey: privateNodeKey,
|
|
|
|
lastDNSConfig: new(tailcfg.DNSConfig),
|
|
|
|
lastUserProfile: map[tailcfg.UserID]tailcfg.UserProfile{},
|
2023-08-21 02:52:52 +00:00
|
|
|
watchdogReset: make(chan struct{}),
|
|
|
|
|
|
|
|
// Non-nil no-op defaults, to be optionally overridden by the caller.
|
|
|
|
logf: logger.Discard,
|
|
|
|
vlogf: logger.Discard,
|
|
|
|
cancel: func() {},
|
|
|
|
onDebug: func(context.Context, *tailcfg.Debug, chan<- struct{}) error { return nil },
|
|
|
|
onConciseNetMapSummary: func(string) {},
|
|
|
|
onSelfNodeChanged: func(*netmap.NetworkMap) {},
|
2021-04-19 02:49:25 +00:00
|
|
|
}
|
2023-08-21 02:52:52 +00:00
|
|
|
ms.sessionAliveCtx, ms.sessionAliveCtxClose = context.WithCancel(context.Background())
|
2021-04-19 02:49:25 +00:00
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
2023-08-21 02:52:52 +00:00
|
|
|
func (ms *mapSession) clock() tstime.Clock {
|
|
|
|
return cmpx.Or[tstime.Clock](ms.altClock, tstime.StdClock{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartWatchdog starts the session's watchdog timer.
|
|
|
|
// If there's no activity in too long, it tears down the connection.
|
|
|
|
// Call Close to release these resources.
|
|
|
|
func (ms *mapSession) StartWatchdog() {
|
|
|
|
timer, timedOutChan := ms.clock().NewTimer(watchdogTimeout)
|
|
|
|
go func() {
|
|
|
|
defer timer.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ms.sessionAliveCtx.Done():
|
|
|
|
ms.vlogf("netmap: ending timeout goroutine")
|
|
|
|
return
|
|
|
|
case <-timedOutChan:
|
|
|
|
ms.logf("map response long-poll timed out!")
|
|
|
|
ms.cancel()
|
|
|
|
return
|
|
|
|
case <-ms.watchdogReset:
|
|
|
|
if !timer.Stop() {
|
|
|
|
select {
|
|
|
|
case <-timedOutChan:
|
|
|
|
case <-ms.sessionAliveCtx.Done():
|
|
|
|
ms.vlogf("netmap: ending timeout goroutine")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ms.vlogf("netmap: reset timeout timer")
|
|
|
|
timer.Reset(watchdogTimeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *mapSession) Close() {
|
|
|
|
ms.sessionAliveCtxClose()
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleNonKeepAliveMapResponse handles a non-KeepAlive MapResponse (full or
|
|
|
|
// incremental).
|
|
|
|
//
|
|
|
|
// All fields that are valid on a KeepAlive MapResponse have already been
|
|
|
|
// handled.
|
|
|
|
//
|
|
|
|
// TODO(bradfitz): make this handle all fields later. For now (2023-08-20) this
|
|
|
|
// is [re]factoring progress enough.
|
|
|
|
func (ms *mapSession) HandleNonKeepAliveMapResponse(ctx context.Context, resp *tailcfg.MapResponse) error {
|
|
|
|
if debug := resp.Debug; debug != nil {
|
|
|
|
if err := ms.onDebug(ctx, debug, ms.watchdogReset); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if DevKnob.StripEndpoints() {
|
|
|
|
for _, p := range resp.Peers {
|
|
|
|
p.Endpoints = nil
|
|
|
|
}
|
|
|
|
for _, p := range resp.PeersChanged {
|
|
|
|
p.Endpoints = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For responses that mutate the self node, check for updated nodeAttrs.
|
|
|
|
if resp.Node != nil {
|
|
|
|
if DevKnob.StripCaps() {
|
|
|
|
resp.Node.Capabilities = nil
|
|
|
|
}
|
|
|
|
setControlKnobsFromNodeAttrs(resp.Node.Capabilities)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call Node.InitDisplayNames on any changed nodes.
|
|
|
|
initDisplayNames(cmpx.Or(resp.Node, ms.lastNode).View(), resp)
|
|
|
|
|
|
|
|
nm := ms.netmapForResponse(resp)
|
|
|
|
|
|
|
|
ms.lastNetmapSummary = nm.VeryConcise()
|
|
|
|
ms.onConciseNetMapSummary(ms.lastNetmapSummary)
|
|
|
|
|
|
|
|
// If the self node changed, we might need to update persist.
|
|
|
|
if resp.Node != nil {
|
|
|
|
ms.onSelfNodeChanged(nm)
|
|
|
|
}
|
|
|
|
|
|
|
|
ms.nu.UpdateFullNetmap(nm)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:49:25 +00:00
|
|
|
func (ms *mapSession) addUserProfile(userID tailcfg.UserID) {
|
2023-08-20 22:17:25 +00:00
|
|
|
if userID == 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-04-19 02:49:25 +00:00
|
|
|
nm := ms.netMapBuilding
|
|
|
|
if _, dup := nm.UserProfiles[userID]; dup {
|
|
|
|
// Already populated it from a previous peer.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if up, ok := ms.lastUserProfile[userID]; ok {
|
|
|
|
nm.UserProfiles[userID] = up
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// netmapForResponse returns a fully populated NetworkMap from a full
|
|
|
|
// or incremental MapResponse within the session, filling in omitted
|
|
|
|
// information from prior MapResponse values.
|
|
|
|
func (ms *mapSession) netmapForResponse(resp *tailcfg.MapResponse) *netmap.NetworkMap {
|
|
|
|
undeltaPeers(resp, ms.previousPeers)
|
|
|
|
|
|
|
|
ms.previousPeers = cloneNodes(resp.Peers) // defensive/lazy clone, since this escapes to who knows where
|
|
|
|
for _, up := range resp.UserProfiles {
|
|
|
|
ms.lastUserProfile[up.ID] = up
|
|
|
|
}
|
|
|
|
|
2023-07-12 18:45:46 +00:00
|
|
|
if dm := resp.DERPMap; dm != nil {
|
2021-04-19 02:49:25 +00:00
|
|
|
ms.vlogf("netmap: new map contains DERP map")
|
2023-07-12 18:45:46 +00:00
|
|
|
|
|
|
|
// Zero-valued fields in a DERPMap mean that we're not changing
|
|
|
|
// anything and are using the previous value(s).
|
|
|
|
if ldm := ms.lastDERPMap; ldm != nil {
|
|
|
|
if dm.Regions == nil {
|
|
|
|
dm.Regions = ldm.Regions
|
|
|
|
dm.OmitDefaultRegions = ldm.OmitDefaultRegions
|
|
|
|
}
|
|
|
|
if dm.HomeParams == nil {
|
|
|
|
dm.HomeParams = ldm.HomeParams
|
|
|
|
} else if oldhh := ldm.HomeParams; oldhh != nil {
|
|
|
|
// Propagate sub-fields of HomeParams
|
|
|
|
hh := dm.HomeParams
|
|
|
|
if hh.RegionScore == nil {
|
|
|
|
hh.RegionScore = oldhh.RegionScore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ms.lastDERPMap = dm
|
2021-04-19 02:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pf := resp.PacketFilter; pf != nil {
|
|
|
|
var err error
|
2023-01-03 23:39:32 +00:00
|
|
|
ms.lastPacketFilterRules = views.SliceOf(pf)
|
2021-04-19 02:49:25 +00:00
|
|
|
ms.lastParsedPacketFilter, err = filter.MatchesFromFilterRules(pf)
|
|
|
|
if err != nil {
|
|
|
|
ms.logf("parsePacketFilter: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c := resp.DNSConfig; c != nil {
|
|
|
|
ms.lastDNSConfig = c
|
|
|
|
}
|
2022-02-10 22:12:34 +00:00
|
|
|
if p := resp.SSHPolicy; p != nil {
|
|
|
|
ms.lastSSHPolicy = p
|
|
|
|
}
|
2021-04-19 02:49:25 +00:00
|
|
|
|
|
|
|
if v, ok := resp.CollectServices.Get(); ok {
|
|
|
|
ms.collectServices = v
|
|
|
|
}
|
2021-04-19 03:48:24 +00:00
|
|
|
if resp.Domain != "" {
|
|
|
|
ms.lastDomain = resp.Domain
|
|
|
|
}
|
2022-10-06 23:19:38 +00:00
|
|
|
if resp.DomainDataPlaneAuditLogID != "" {
|
|
|
|
ms.lastDomainAuditLogID = resp.DomainDataPlaneAuditLogID
|
|
|
|
}
|
2021-09-18 19:59:55 +00:00
|
|
|
if resp.Health != nil {
|
|
|
|
ms.lastHealth = resp.Health
|
|
|
|
}
|
2022-09-06 23:34:16 +00:00
|
|
|
if resp.TKAInfo != nil {
|
|
|
|
ms.lastTKAInfo = resp.TKAInfo
|
|
|
|
}
|
2021-04-19 02:49:25 +00:00
|
|
|
|
2023-08-18 14:57:44 +00:00
|
|
|
// TODO(bradfitz): now that this is a view, remove some of the defensive
|
|
|
|
// cloning elsewhere in mapSession.
|
|
|
|
peerViews := make([]tailcfg.NodeView, len(resp.Peers))
|
|
|
|
for i, n := range resp.Peers {
|
|
|
|
peerViews[i] = n.View()
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:49:25 +00:00
|
|
|
nm := &netmap.NetworkMap{
|
2023-01-03 23:39:32 +00:00
|
|
|
NodeKey: ms.privateNodeKey.Public(),
|
|
|
|
PrivateKey: ms.privateNodeKey,
|
|
|
|
MachineKey: ms.machinePubKey,
|
2023-08-18 14:57:44 +00:00
|
|
|
Peers: peerViews,
|
2023-01-03 23:39:32 +00:00
|
|
|
UserProfiles: make(map[tailcfg.UserID]tailcfg.UserProfile),
|
|
|
|
Domain: ms.lastDomain,
|
|
|
|
DomainAuditLogID: ms.lastDomainAuditLogID,
|
|
|
|
DNS: *ms.lastDNSConfig,
|
|
|
|
PacketFilter: ms.lastParsedPacketFilter,
|
|
|
|
PacketFilterRules: ms.lastPacketFilterRules,
|
|
|
|
SSHPolicy: ms.lastSSHPolicy,
|
|
|
|
CollectServices: ms.collectServices,
|
|
|
|
DERPMap: ms.lastDERPMap,
|
|
|
|
ControlHealth: ms.lastHealth,
|
|
|
|
TKAEnabled: ms.lastTKAInfo != nil && !ms.lastTKAInfo.Disabled,
|
2021-04-19 02:49:25 +00:00
|
|
|
}
|
|
|
|
ms.netMapBuilding = nm
|
|
|
|
|
2022-09-06 23:34:16 +00:00
|
|
|
if ms.lastTKAInfo != nil && ms.lastTKAInfo.Head != "" {
|
|
|
|
if err := nm.TKAHead.UnmarshalText([]byte(ms.lastTKAInfo.Head)); err != nil {
|
|
|
|
ms.logf("error unmarshalling TKAHead: %v", err)
|
|
|
|
nm.TKAEnabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:56:48 +00:00
|
|
|
if resp.Node != nil {
|
|
|
|
ms.lastNode = resp.Node
|
|
|
|
}
|
|
|
|
if node := ms.lastNode.Clone(); node != nil {
|
|
|
|
nm.SelfNode = node
|
|
|
|
nm.Expiry = node.KeyExpiry
|
|
|
|
nm.Name = node.Name
|
2021-06-28 22:25:58 +00:00
|
|
|
nm.Addresses = filterSelfAddresses(node.Addresses)
|
2022-02-15 16:19:44 +00:00
|
|
|
if node.Hostinfo.Valid() {
|
|
|
|
nm.Hostinfo = *node.Hostinfo.AsStruct()
|
|
|
|
}
|
2021-04-19 17:56:48 +00:00
|
|
|
if node.MachineAuthorized {
|
|
|
|
nm.MachineStatus = tailcfg.MachineAuthorized
|
|
|
|
} else {
|
|
|
|
nm.MachineStatus = tailcfg.MachineUnauthorized
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 22:17:25 +00:00
|
|
|
ms.addUserProfile(nm.User())
|
2021-04-19 02:49:25 +00:00
|
|
|
for _, peer := range resp.Peers {
|
2023-08-20 22:17:25 +00:00
|
|
|
ms.addUserProfile(peer.Sharer)
|
2021-04-19 02:49:25 +00:00
|
|
|
ms.addUserProfile(peer.User)
|
|
|
|
}
|
2022-09-14 19:49:39 +00:00
|
|
|
if DevKnob.ForceProxyDNS() {
|
2021-04-19 02:49:25 +00:00
|
|
|
nm.DNS.Proxied = true
|
|
|
|
}
|
|
|
|
ms.netMapBuilding = nil
|
|
|
|
return nm
|
|
|
|
}
|
|
|
|
|
|
|
|
// undeltaPeers updates mapRes.Peers to be complete based on the
|
|
|
|
// provided previous peer list and the PeersRemoved and PeersChanged
|
|
|
|
// fields in mapRes, as well as the PeerSeenChange and OnlineChange
|
|
|
|
// maps.
|
|
|
|
//
|
|
|
|
// It then also nils out the delta fields.
|
|
|
|
func undeltaPeers(mapRes *tailcfg.MapResponse, prev []*tailcfg.Node) {
|
|
|
|
if len(mapRes.Peers) > 0 {
|
|
|
|
// Not delta encoded.
|
|
|
|
if !nodesSorted(mapRes.Peers) {
|
|
|
|
log.Printf("netmap: undeltaPeers: MapResponse.Peers not sorted; sorting")
|
|
|
|
sortNodes(mapRes.Peers)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var removed map[tailcfg.NodeID]bool
|
|
|
|
if pr := mapRes.PeersRemoved; len(pr) > 0 {
|
|
|
|
removed = make(map[tailcfg.NodeID]bool, len(pr))
|
|
|
|
for _, id := range pr {
|
|
|
|
removed[id] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changed := mapRes.PeersChanged
|
|
|
|
|
|
|
|
if !nodesSorted(changed) {
|
|
|
|
log.Printf("netmap: undeltaPeers: MapResponse.PeersChanged not sorted; sorting")
|
|
|
|
sortNodes(changed)
|
|
|
|
}
|
|
|
|
if !nodesSorted(prev) {
|
|
|
|
// Internal error (unrelated to the network) if we get here.
|
|
|
|
log.Printf("netmap: undeltaPeers: [unexpected] prev not sorted; sorting")
|
|
|
|
sortNodes(prev)
|
|
|
|
}
|
|
|
|
|
|
|
|
newFull := prev
|
|
|
|
if len(removed) > 0 || len(changed) > 0 {
|
|
|
|
newFull = make([]*tailcfg.Node, 0, len(prev)-len(removed))
|
|
|
|
for len(prev) > 0 && len(changed) > 0 {
|
|
|
|
pID := prev[0].ID
|
|
|
|
cID := changed[0].ID
|
|
|
|
if removed[pID] {
|
|
|
|
prev = prev[1:]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case pID < cID:
|
|
|
|
newFull = append(newFull, prev[0])
|
|
|
|
prev = prev[1:]
|
|
|
|
case pID == cID:
|
|
|
|
newFull = append(newFull, changed[0])
|
|
|
|
prev, changed = prev[1:], changed[1:]
|
|
|
|
case cID < pID:
|
|
|
|
newFull = append(newFull, changed[0])
|
|
|
|
changed = changed[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newFull = append(newFull, changed...)
|
|
|
|
for _, n := range prev {
|
|
|
|
if !removed[n.ID] {
|
|
|
|
newFull = append(newFull, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sortNodes(newFull)
|
|
|
|
}
|
|
|
|
|
2022-07-20 20:31:20 +00:00
|
|
|
if len(mapRes.PeerSeenChange) != 0 || len(mapRes.OnlineChange) != 0 || len(mapRes.PeersChangedPatch) != 0 {
|
2021-04-19 02:49:25 +00:00
|
|
|
peerByID := make(map[tailcfg.NodeID]*tailcfg.Node, len(newFull))
|
|
|
|
for _, n := range newFull {
|
|
|
|
peerByID[n.ID] = n
|
|
|
|
}
|
2023-08-04 23:29:44 +00:00
|
|
|
now := clock.Now()
|
2021-04-19 02:49:25 +00:00
|
|
|
for nodeID, seen := range mapRes.PeerSeenChange {
|
|
|
|
if n, ok := peerByID[nodeID]; ok {
|
|
|
|
if seen {
|
|
|
|
n.LastSeen = &now
|
|
|
|
} else {
|
|
|
|
n.LastSeen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for nodeID, online := range mapRes.OnlineChange {
|
|
|
|
if n, ok := peerByID[nodeID]; ok {
|
|
|
|
online := online
|
|
|
|
n.Online = &online
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 20:31:20 +00:00
|
|
|
for _, ec := range mapRes.PeersChangedPatch {
|
|
|
|
if n, ok := peerByID[ec.NodeID]; ok {
|
|
|
|
if ec.DERPRegion != 0 {
|
|
|
|
n.DERP = fmt.Sprintf("%s:%v", tailcfg.DerpMagicIP, ec.DERPRegion)
|
|
|
|
}
|
2023-01-19 20:40:58 +00:00
|
|
|
if ec.Cap != 0 {
|
|
|
|
n.Cap = ec.Cap
|
|
|
|
}
|
2022-07-20 20:31:20 +00:00
|
|
|
if ec.Endpoints != nil {
|
|
|
|
n.Endpoints = ec.Endpoints
|
|
|
|
}
|
2022-08-03 03:42:13 +00:00
|
|
|
if ec.Key != nil {
|
|
|
|
n.Key = *ec.Key
|
|
|
|
}
|
|
|
|
if ec.DiscoKey != nil {
|
|
|
|
n.DiscoKey = *ec.DiscoKey
|
|
|
|
}
|
|
|
|
if v := ec.Online; v != nil {
|
|
|
|
n.Online = ptrCopy(v)
|
|
|
|
}
|
|
|
|
if v := ec.LastSeen; v != nil {
|
|
|
|
n.LastSeen = ptrCopy(v)
|
|
|
|
}
|
|
|
|
if v := ec.KeyExpiry; v != nil {
|
|
|
|
n.KeyExpiry = *v
|
|
|
|
}
|
|
|
|
if v := ec.Capabilities; v != nil {
|
|
|
|
n.Capabilities = *v
|
|
|
|
}
|
2022-08-12 19:34:25 +00:00
|
|
|
if v := ec.KeySignature; v != nil {
|
|
|
|
n.KeySignature = v
|
|
|
|
}
|
2022-07-20 20:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-19 02:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mapRes.Peers = newFull
|
|
|
|
mapRes.PeersChanged = nil
|
|
|
|
mapRes.PeersRemoved = nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 03:42:13 +00:00
|
|
|
// ptrCopy returns a pointer to a newly allocated shallow copy of *v.
|
|
|
|
func ptrCopy[T any](v *T) *T {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := new(T)
|
|
|
|
*ret = *v
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:49:25 +00:00
|
|
|
func nodesSorted(v []*tailcfg.Node) bool {
|
|
|
|
for i, n := range v {
|
|
|
|
if i > 0 && n.ID <= v[i-1].ID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortNodes(v []*tailcfg.Node) {
|
|
|
|
sort.Slice(v, func(i, j int) bool { return v[i].ID < v[j].ID })
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneNodes(v1 []*tailcfg.Node) []*tailcfg.Node {
|
|
|
|
if v1 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
v2 := make([]*tailcfg.Node, len(v1))
|
|
|
|
for i, n := range v1 {
|
|
|
|
v2[i] = n.Clone()
|
|
|
|
}
|
|
|
|
return v2
|
|
|
|
}
|
2021-06-28 22:25:58 +00:00
|
|
|
|
2022-09-14 19:49:39 +00:00
|
|
|
var debugSelfIPv6Only = envknob.RegisterBool("TS_DEBUG_SELF_V6_ONLY")
|
2021-06-28 22:25:58 +00:00
|
|
|
|
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
|
|
|
func filterSelfAddresses(in []netip.Prefix) (ret []netip.Prefix) {
|
2021-06-28 22:25:58 +00:00
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
return in
|
2022-09-14 19:49:39 +00:00
|
|
|
case debugSelfIPv6Only():
|
2021-06-28 22:25:58 +00:00
|
|
|
for _, a := range in {
|
2022-07-25 03:08:42 +00:00
|
|
|
if a.Addr().Is6() {
|
2021-06-28 22:25:58 +00:00
|
|
|
ret = append(ret, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
}
|