netmap.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package netmap contains the netmap.NetworkMap type.
  4. package netmap
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/netip"
  9. "reflect"
  10. "strings"
  11. "time"
  12. "tailscale.com/tailcfg"
  13. "tailscale.com/tka"
  14. "tailscale.com/types/key"
  15. "tailscale.com/types/views"
  16. "tailscale.com/wgengine/filter"
  17. )
  18. // NetworkMap is the current state of the world.
  19. //
  20. // The fields should all be considered read-only. They might
  21. // alias parts of previous NetworkMap values.
  22. type NetworkMap struct {
  23. // Core networking
  24. SelfNode *tailcfg.Node
  25. NodeKey key.NodePublic
  26. PrivateKey key.NodePrivate
  27. Expiry time.Time
  28. // Name is the DNS name assigned to this node.
  29. Name string
  30. Addresses []netip.Prefix // same as tailcfg.Node.Addresses (IP addresses of this Node directly)
  31. MachineStatus tailcfg.MachineStatus
  32. MachineKey key.MachinePublic
  33. Peers []*tailcfg.Node // sorted by Node.ID
  34. DNS tailcfg.DNSConfig
  35. // TODO(maisem) : replace with View.
  36. Hostinfo tailcfg.Hostinfo
  37. PacketFilter []filter.Match
  38. PacketFilterRules views.Slice[tailcfg.FilterRule]
  39. SSHPolicy *tailcfg.SSHPolicy // or nil, if not enabled/allowed
  40. // CollectServices reports whether this node's Tailnet has
  41. // requested that info about services be included in HostInfo.
  42. // If set, Hostinfo.ShieldsUp blocks services collection; that
  43. // takes precedence over this field.
  44. CollectServices bool
  45. // DERPMap is the last DERP server map received. It's reused
  46. // between updates and should not be modified.
  47. DERPMap *tailcfg.DERPMap
  48. // Debug knobs from control server for debug or feature gating.
  49. Debug *tailcfg.Debug
  50. // ControlHealth are the list of health check problems for this
  51. // node from the perspective of the control plane.
  52. // If empty, there are no known problems from the control plane's
  53. // point of view, but the node might know about its own health
  54. // check problems.
  55. ControlHealth []string
  56. // TKAEnabled indicates whether the tailnet key authority should be
  57. // enabled, from the perspective of the control plane.
  58. TKAEnabled bool
  59. // TKAHead indicates the control plane's understanding of 'head' (the
  60. // hash of the latest update message to tick through TKA).
  61. TKAHead tka.AUMHash
  62. // ACLs
  63. User tailcfg.UserID
  64. // Domain is the current Tailnet name.
  65. Domain string
  66. // DomainAuditLogID is an audit log ID provided by control and
  67. // only populated if the domain opts into data-plane audit logging.
  68. // If this is empty, then data-plane audit logging is disabled.
  69. DomainAuditLogID string
  70. UserProfiles map[tailcfg.UserID]tailcfg.UserProfile
  71. }
  72. // AnyPeersAdvertiseRoutes reports whether any peer is advertising non-exit node routes.
  73. func (nm *NetworkMap) AnyPeersAdvertiseRoutes() bool {
  74. for _, p := range nm.Peers {
  75. if len(p.PrimaryRoutes) > 0 {
  76. return true
  77. }
  78. }
  79. return false
  80. }
  81. // PeerByTailscaleIP returns a peer's Node based on its Tailscale IP.
  82. //
  83. // If nm is nil or no peer is found, ok is false.
  84. func (nm *NetworkMap) PeerByTailscaleIP(ip netip.Addr) (peer *tailcfg.Node, ok bool) {
  85. // TODO(bradfitz):
  86. if nm == nil {
  87. return nil, false
  88. }
  89. for _, n := range nm.Peers {
  90. for _, a := range n.Addresses {
  91. if a.Addr() == ip {
  92. return n, true
  93. }
  94. }
  95. }
  96. return nil, false
  97. }
  98. // MagicDNSSuffix returns the domain's MagicDNS suffix (even if
  99. // MagicDNS isn't necessarily in use).
  100. //
  101. // It will neither start nor end with a period.
  102. func (nm *NetworkMap) MagicDNSSuffix() string {
  103. name := strings.Trim(nm.Name, ".")
  104. if _, rest, ok := strings.Cut(name, "."); ok {
  105. return rest
  106. }
  107. return name
  108. }
  109. // SelfCapabilities returns SelfNode.Capabilities if nm and nm.SelfNode are
  110. // non-nil. This is a method so we can use it in envknob/logknob without a
  111. // circular dependency.
  112. func (nm *NetworkMap) SelfCapabilities() []string {
  113. if nm == nil || nm.SelfNode == nil {
  114. return nil
  115. }
  116. return nm.SelfNode.Capabilities
  117. }
  118. func (nm *NetworkMap) String() string {
  119. return nm.Concise()
  120. }
  121. func (nm *NetworkMap) Concise() string {
  122. buf := new(strings.Builder)
  123. nm.printConciseHeader(buf)
  124. for _, p := range nm.Peers {
  125. printPeerConcise(buf, p)
  126. }
  127. return buf.String()
  128. }
  129. func (nm *NetworkMap) VeryConcise() string {
  130. buf := new(strings.Builder)
  131. nm.printConciseHeader(buf)
  132. return buf.String()
  133. }
  134. // PeerWithStableID finds and returns the peer associated to the inputted StableNodeID.
  135. func (nm *NetworkMap) PeerWithStableID(pid tailcfg.StableNodeID) (_ *tailcfg.Node, ok bool) {
  136. for _, p := range nm.Peers {
  137. if p.StableID == pid {
  138. return p, true
  139. }
  140. }
  141. return nil, false
  142. }
  143. // printConciseHeader prints a concise header line representing nm to buf.
  144. //
  145. // If this function is changed to access different fields of nm, keep
  146. // in equalConciseHeader in sync.
  147. func (nm *NetworkMap) printConciseHeader(buf *strings.Builder) {
  148. fmt.Fprintf(buf, "netmap: self: %v auth=%v",
  149. nm.NodeKey.ShortString(), nm.MachineStatus)
  150. login := nm.UserProfiles[nm.User].LoginName
  151. if login == "" {
  152. if nm.User.IsZero() {
  153. login = "?"
  154. } else {
  155. login = fmt.Sprint(nm.User)
  156. }
  157. }
  158. fmt.Fprintf(buf, " u=%s", login)
  159. if nm.Debug != nil {
  160. j, _ := json.Marshal(nm.Debug)
  161. fmt.Fprintf(buf, " debug=%s", j)
  162. }
  163. fmt.Fprintf(buf, " %v", nm.Addresses)
  164. buf.WriteByte('\n')
  165. }
  166. // equalConciseHeader reports whether a and b are equal for the fields
  167. // used by printConciseHeader.
  168. func (a *NetworkMap) equalConciseHeader(b *NetworkMap) bool {
  169. if a.NodeKey != b.NodeKey ||
  170. a.MachineStatus != b.MachineStatus ||
  171. a.User != b.User ||
  172. len(a.Addresses) != len(b.Addresses) {
  173. return false
  174. }
  175. for i, a := range a.Addresses {
  176. if b.Addresses[i] != a {
  177. return false
  178. }
  179. }
  180. return (a.Debug == nil && b.Debug == nil) || reflect.DeepEqual(a.Debug, b.Debug)
  181. }
  182. // printPeerConcise appends to buf a line representing the peer p.
  183. //
  184. // If this function is changed to access different fields of p, keep
  185. // in nodeConciseEqual in sync.
  186. func printPeerConcise(buf *strings.Builder, p *tailcfg.Node) {
  187. aip := make([]string, len(p.AllowedIPs))
  188. for i, a := range p.AllowedIPs {
  189. s := strings.TrimSuffix(fmt.Sprint(a), "/32")
  190. aip[i] = s
  191. }
  192. ep := make([]string, len(p.Endpoints))
  193. for i, e := range p.Endpoints {
  194. // Align vertically on the ':' between IP and port
  195. colon := strings.IndexByte(e, ':')
  196. spaces := 0
  197. for colon > 0 && len(e)+spaces-colon < 6 {
  198. spaces++
  199. colon--
  200. }
  201. ep[i] = fmt.Sprintf("%21v", e+strings.Repeat(" ", spaces))
  202. }
  203. derp := p.DERP
  204. const derpPrefix = "127.3.3.40:"
  205. if strings.HasPrefix(derp, derpPrefix) {
  206. derp = "D" + derp[len(derpPrefix):]
  207. }
  208. var discoShort string
  209. if !p.DiscoKey.IsZero() {
  210. discoShort = p.DiscoKey.ShortString() + " "
  211. }
  212. // Most of the time, aip is just one element, so format the
  213. // table to look good in that case. This will also make multi-
  214. // subnet nodes stand out visually.
  215. fmt.Fprintf(buf, " %v %s%-2v %-15v : %v\n",
  216. p.Key.ShortString(),
  217. discoShort,
  218. derp,
  219. strings.Join(aip, " "),
  220. strings.Join(ep, " "))
  221. }
  222. // nodeConciseEqual reports whether a and b are equal for the fields accessed by printPeerConcise.
  223. func nodeConciseEqual(a, b *tailcfg.Node) bool {
  224. return a.Key == b.Key &&
  225. a.DERP == b.DERP &&
  226. a.DiscoKey == b.DiscoKey &&
  227. eqCIDRsIgnoreNil(a.AllowedIPs, b.AllowedIPs) &&
  228. eqStringsIgnoreNil(a.Endpoints, b.Endpoints)
  229. }
  230. func (b *NetworkMap) ConciseDiffFrom(a *NetworkMap) string {
  231. var diff strings.Builder
  232. // See if header (non-peers, "bare") part of the network map changed.
  233. // If so, print its diff lines first.
  234. if !a.equalConciseHeader(b) {
  235. diff.WriteByte('-')
  236. a.printConciseHeader(&diff)
  237. diff.WriteByte('+')
  238. b.printConciseHeader(&diff)
  239. }
  240. aps, bps := a.Peers, b.Peers
  241. for len(aps) > 0 && len(bps) > 0 {
  242. pa, pb := aps[0], bps[0]
  243. switch {
  244. case pa.ID == pb.ID:
  245. if !nodeConciseEqual(pa, pb) {
  246. diff.WriteByte('-')
  247. printPeerConcise(&diff, pa)
  248. diff.WriteByte('+')
  249. printPeerConcise(&diff, pb)
  250. }
  251. aps, bps = aps[1:], bps[1:]
  252. case pa.ID > pb.ID:
  253. // New peer in b.
  254. diff.WriteByte('+')
  255. printPeerConcise(&diff, pb)
  256. bps = bps[1:]
  257. case pb.ID > pa.ID:
  258. // Deleted peer in b.
  259. diff.WriteByte('-')
  260. printPeerConcise(&diff, pa)
  261. aps = aps[1:]
  262. }
  263. }
  264. for _, pa := range aps {
  265. diff.WriteByte('-')
  266. printPeerConcise(&diff, pa)
  267. }
  268. for _, pb := range bps {
  269. diff.WriteByte('+')
  270. printPeerConcise(&diff, pb)
  271. }
  272. return diff.String()
  273. }
  274. func (nm *NetworkMap) JSON() string {
  275. b, err := json.MarshalIndent(*nm, "", " ")
  276. if err != nil {
  277. return fmt.Sprintf("[json error: %v]", err)
  278. }
  279. return string(b)
  280. }
  281. // WGConfigFlags is a bitmask of flags to control the behavior of the
  282. // wireguard configuration generation done by NetMap.WGCfg.
  283. type WGConfigFlags int
  284. const (
  285. AllowSingleHosts WGConfigFlags = 1 << iota
  286. AllowSubnetRoutes
  287. )
  288. // eqStringsIgnoreNil reports whether a and b have the same length and
  289. // contents, but ignore whether a or b are nil.
  290. func eqStringsIgnoreNil(a, b []string) bool {
  291. if len(a) != len(b) {
  292. return false
  293. }
  294. for i, v := range a {
  295. if v != b[i] {
  296. return false
  297. }
  298. }
  299. return true
  300. }
  301. // eqCIDRsIgnoreNil reports whether a and b have the same length and
  302. // contents, but ignore whether a or b are nil.
  303. func eqCIDRsIgnoreNil(a, b []netip.Prefix) bool {
  304. if len(a) != len(b) {
  305. return false
  306. }
  307. for i, v := range a {
  308. if v != b[i] {
  309. return false
  310. }
  311. }
  312. return true
  313. }