netmap.go 8.0 KB

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