netmap.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/types/wgkey"
  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 tailcfg.NodeKey
  26. PrivateKey wgkey.Private
  27. Expiry time.Time
  28. // Name is the DNS name assigned to this node.
  29. Name string
  30. Addresses []netaddr.IPPrefix // same as tailcfg.Node.Addresses (IP addresses of this Node directly)
  31. LocalPort uint16 // used for debugging
  32. MachineStatus tailcfg.MachineStatus
  33. MachineKey key.MachinePublic
  34. Peers []*tailcfg.Node // sorted by Node.ID
  35. DNS tailcfg.DNSConfig
  36. Hostinfo tailcfg.Hostinfo
  37. PacketFilter []filter.Match
  38. // CollectServices reports whether this node's Tailnet has
  39. // requested that info about services be included in HostInfo.
  40. // If set, Hostinfo.ShieldsUp blocks services collection; that
  41. // takes precedence over this field.
  42. CollectServices bool
  43. // DERPMap is the last DERP server map received. It's reused
  44. // between updates and should not be modified.
  45. DERPMap *tailcfg.DERPMap
  46. // Debug knobs from control server for debug or feature gating.
  47. Debug *tailcfg.Debug
  48. // ACLs
  49. User tailcfg.UserID
  50. Domain string
  51. UserProfiles map[tailcfg.UserID]tailcfg.UserProfile
  52. }
  53. // MagicDNSSuffix returns the domain's MagicDNS suffix (even if
  54. // MagicDNS isn't necessarily in use).
  55. //
  56. // It will neither start nor end with a period.
  57. func (nm *NetworkMap) MagicDNSSuffix() string {
  58. name := strings.Trim(nm.Name, ".")
  59. if i := strings.Index(name, "."); i != -1 {
  60. name = name[i+1:]
  61. }
  62. return name
  63. }
  64. func (nm *NetworkMap) String() string {
  65. return nm.Concise()
  66. }
  67. func (nm *NetworkMap) Concise() string {
  68. buf := new(strings.Builder)
  69. nm.printConciseHeader(buf)
  70. for _, p := range nm.Peers {
  71. printPeerConcise(buf, p)
  72. }
  73. return buf.String()
  74. }
  75. func (nm *NetworkMap) VeryConcise() string {
  76. buf := new(strings.Builder)
  77. nm.printConciseHeader(buf)
  78. return buf.String()
  79. }
  80. // printConciseHeader prints a concise header line representing nm to buf.
  81. //
  82. // If this function is changed to access different fields of nm, keep
  83. // in equalConciseHeader in sync.
  84. func (nm *NetworkMap) printConciseHeader(buf *strings.Builder) {
  85. fmt.Fprintf(buf, "netmap: self: %v auth=%v",
  86. nm.NodeKey.ShortString(), nm.MachineStatus)
  87. login := nm.UserProfiles[nm.User].LoginName
  88. if login == "" {
  89. if nm.User.IsZero() {
  90. login = "?"
  91. } else {
  92. login = fmt.Sprint(nm.User)
  93. }
  94. }
  95. fmt.Fprintf(buf, " u=%s", login)
  96. if nm.LocalPort != 0 {
  97. fmt.Fprintf(buf, " port=%v", nm.LocalPort)
  98. }
  99. if nm.Debug != nil {
  100. j, _ := json.Marshal(nm.Debug)
  101. fmt.Fprintf(buf, " debug=%s", j)
  102. }
  103. fmt.Fprintf(buf, " %v", nm.Addresses)
  104. buf.WriteByte('\n')
  105. }
  106. // equalConciseHeader reports whether a and b are equal for the fields
  107. // used by printConciseHeader.
  108. func (a *NetworkMap) equalConciseHeader(b *NetworkMap) bool {
  109. if a.NodeKey != b.NodeKey ||
  110. a.MachineStatus != b.MachineStatus ||
  111. a.LocalPort != b.LocalPort ||
  112. a.User != b.User ||
  113. len(a.Addresses) != len(b.Addresses) {
  114. return false
  115. }
  116. for i, a := range a.Addresses {
  117. if b.Addresses[i] != a {
  118. return false
  119. }
  120. }
  121. return (a.Debug == nil && b.Debug == nil) || reflect.DeepEqual(a.Debug, b.Debug)
  122. }
  123. // printPeerConcise appends to buf a line representing the peer p.
  124. //
  125. // If this function is changed to access different fields of p, keep
  126. // in nodeConciseEqual in sync.
  127. func printPeerConcise(buf *strings.Builder, p *tailcfg.Node) {
  128. aip := make([]string, len(p.AllowedIPs))
  129. for i, a := range p.AllowedIPs {
  130. s := strings.TrimSuffix(fmt.Sprint(a), "/32")
  131. aip[i] = s
  132. }
  133. ep := make([]string, len(p.Endpoints))
  134. for i, e := range p.Endpoints {
  135. // Align vertically on the ':' between IP and port
  136. colon := strings.IndexByte(e, ':')
  137. spaces := 0
  138. for colon > 0 && len(e)+spaces-colon < 6 {
  139. spaces++
  140. colon--
  141. }
  142. ep[i] = fmt.Sprintf("%21v", e+strings.Repeat(" ", spaces))
  143. }
  144. derp := p.DERP
  145. const derpPrefix = "127.3.3.40:"
  146. if strings.HasPrefix(derp, derpPrefix) {
  147. derp = "D" + derp[len(derpPrefix):]
  148. }
  149. var discoShort string
  150. if !p.DiscoKey.IsZero() {
  151. discoShort = p.DiscoKey.ShortString() + " "
  152. }
  153. // Most of the time, aip is just one element, so format the
  154. // table to look good in that case. This will also make multi-
  155. // subnet nodes stand out visually.
  156. fmt.Fprintf(buf, " %v %s%-2v %-15v : %v\n",
  157. p.Key.ShortString(),
  158. discoShort,
  159. derp,
  160. strings.Join(aip, " "),
  161. strings.Join(ep, " "))
  162. }
  163. // nodeConciseEqual reports whether a and b are equal for the fields accessed by printPeerConcise.
  164. func nodeConciseEqual(a, b *tailcfg.Node) bool {
  165. return a.Key == b.Key &&
  166. a.DERP == b.DERP &&
  167. a.DiscoKey == b.DiscoKey &&
  168. eqCIDRsIgnoreNil(a.AllowedIPs, b.AllowedIPs) &&
  169. eqStringsIgnoreNil(a.Endpoints, b.Endpoints)
  170. }
  171. func (b *NetworkMap) ConciseDiffFrom(a *NetworkMap) string {
  172. var diff strings.Builder
  173. // See if header (non-peers, "bare") part of the network map changed.
  174. // If so, print its diff lines first.
  175. if !a.equalConciseHeader(b) {
  176. diff.WriteByte('-')
  177. a.printConciseHeader(&diff)
  178. diff.WriteByte('+')
  179. b.printConciseHeader(&diff)
  180. }
  181. aps, bps := a.Peers, b.Peers
  182. for len(aps) > 0 && len(bps) > 0 {
  183. pa, pb := aps[0], bps[0]
  184. switch {
  185. case pa.ID == pb.ID:
  186. if !nodeConciseEqual(pa, pb) {
  187. diff.WriteByte('-')
  188. printPeerConcise(&diff, pa)
  189. diff.WriteByte('+')
  190. printPeerConcise(&diff, pb)
  191. }
  192. aps, bps = aps[1:], bps[1:]
  193. case pa.ID > pb.ID:
  194. // New peer in b.
  195. diff.WriteByte('+')
  196. printPeerConcise(&diff, pb)
  197. bps = bps[1:]
  198. case pb.ID > pa.ID:
  199. // Deleted peer in b.
  200. diff.WriteByte('-')
  201. printPeerConcise(&diff, pa)
  202. aps = aps[1:]
  203. }
  204. }
  205. for _, pa := range aps {
  206. diff.WriteByte('-')
  207. printPeerConcise(&diff, pa)
  208. }
  209. for _, pb := range bps {
  210. diff.WriteByte('+')
  211. printPeerConcise(&diff, pb)
  212. }
  213. return diff.String()
  214. }
  215. func (nm *NetworkMap) JSON() string {
  216. b, err := json.MarshalIndent(*nm, "", " ")
  217. if err != nil {
  218. return fmt.Sprintf("[json error: %v]", err)
  219. }
  220. return string(b)
  221. }
  222. // WGConfigFlags is a bitmask of flags to control the behavior of the
  223. // wireguard configuration generation done by NetMap.WGCfg.
  224. type WGConfigFlags int
  225. const (
  226. AllowSingleHosts WGConfigFlags = 1 << iota
  227. AllowSubnetRoutes
  228. )
  229. // eqStringsIgnoreNil reports whether a and b have the same length and
  230. // contents, but ignore whether a or b are nil.
  231. func eqStringsIgnoreNil(a, b []string) bool {
  232. if len(a) != len(b) {
  233. return false
  234. }
  235. for i, v := range a {
  236. if v != b[i] {
  237. return false
  238. }
  239. }
  240. return true
  241. }
  242. // eqCIDRsIgnoreNil reports whether a and b have the same length and
  243. // contents, but ignore whether a or b are nil.
  244. func eqCIDRsIgnoreNil(a, b []netaddr.IPPrefix) bool {
  245. if len(a) != len(b) {
  246. return false
  247. }
  248. for i, v := range a {
  249. if v != b[i] {
  250. return false
  251. }
  252. }
  253. return true
  254. }