netmap.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 controlclient
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net"
  9. "reflect"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/tailscale/wireguard-go/wgcfg"
  14. "tailscale.com/tailcfg"
  15. "tailscale.com/types/logger"
  16. "tailscale.com/wgengine/filter"
  17. )
  18. type NetworkMap struct {
  19. // Core networking
  20. NodeKey tailcfg.NodeKey
  21. PrivateKey wgcfg.PrivateKey
  22. Expiry time.Time
  23. // Name is the DNS name assigned to this node.
  24. Name string
  25. Addresses []wgcfg.CIDR
  26. LocalPort uint16 // used for debugging
  27. MachineStatus tailcfg.MachineStatus
  28. MachineKey tailcfg.MachineKey
  29. Peers []*tailcfg.Node // sorted by Node.ID
  30. DNS tailcfg.DNSConfig
  31. Hostinfo tailcfg.Hostinfo
  32. PacketFilter []filter.Match
  33. // DERPMap is the last DERP server map received. It's reused
  34. // between updates and should not be modified.
  35. DERPMap *tailcfg.DERPMap
  36. // Debug knobs from control server for debug or feature gating.
  37. Debug *tailcfg.Debug
  38. // ACLs
  39. User tailcfg.UserID
  40. Domain string
  41. // TODO(crawshaw): reduce UserProfiles to []tailcfg.UserProfile?
  42. // There are lots of ways to slice this data, leave it up to users.
  43. UserProfiles map[tailcfg.UserID]tailcfg.UserProfile
  44. // TODO(crawshaw): Groups []tailcfg.Group
  45. // TODO(crawshaw): Capabilities []tailcfg.Capability
  46. }
  47. func (nm NetworkMap) String() string {
  48. return nm.Concise()
  49. }
  50. func (nm *NetworkMap) Concise() string {
  51. buf := new(strings.Builder)
  52. nm.printConciseHeader(buf)
  53. for _, p := range nm.Peers {
  54. printPeerConcise(buf, p)
  55. }
  56. return buf.String()
  57. }
  58. // printConciseHeader prints a concise header line representing nm to buf.
  59. //
  60. // If this function is changed to access different fields of nm, keep
  61. // in equalConciseHeader in sync.
  62. func (nm *NetworkMap) printConciseHeader(buf *strings.Builder) {
  63. fmt.Fprintf(buf, "netmap: self: %v auth=%v",
  64. nm.NodeKey.ShortString(), nm.MachineStatus)
  65. login := nm.UserProfiles[nm.User].LoginName
  66. if login == "" {
  67. if nm.User.IsZero() {
  68. login = "?"
  69. } else {
  70. login = fmt.Sprint(nm.User)
  71. }
  72. }
  73. fmt.Fprintf(buf, " u=%s", login)
  74. if nm.LocalPort != 0 {
  75. fmt.Fprintf(buf, " port=%v", nm.LocalPort)
  76. }
  77. if nm.Debug != nil {
  78. j, _ := json.Marshal(nm.Debug)
  79. fmt.Fprintf(buf, " debug=%s", j)
  80. }
  81. fmt.Fprintf(buf, " %v", nm.Addresses)
  82. buf.WriteByte('\n')
  83. }
  84. // equalConciseHeader reports whether a and b are equal for the fields
  85. // used by printConciseHeader.
  86. func (a *NetworkMap) equalConciseHeader(b *NetworkMap) bool {
  87. if a.NodeKey != b.NodeKey ||
  88. a.MachineStatus != b.MachineStatus ||
  89. a.LocalPort != b.LocalPort ||
  90. a.User != b.User ||
  91. len(a.Addresses) != len(b.Addresses) {
  92. return false
  93. }
  94. for i, a := range a.Addresses {
  95. if b.Addresses[i] != a {
  96. return false
  97. }
  98. }
  99. return (a.Debug == nil && b.Debug == nil) || reflect.DeepEqual(a.Debug, b.Debug)
  100. }
  101. // printPeerConcise appends to buf a line repsenting the peer p.
  102. //
  103. // If this function is changed to access different fields of p, keep
  104. // in nodeConciseEqual in sync.
  105. func printPeerConcise(buf *strings.Builder, p *tailcfg.Node) {
  106. aip := make([]string, len(p.AllowedIPs))
  107. for i, a := range p.AllowedIPs {
  108. s := strings.TrimSuffix(fmt.Sprint(a), "/32")
  109. aip[i] = s
  110. }
  111. ep := make([]string, len(p.Endpoints))
  112. for i, e := range p.Endpoints {
  113. // Align vertically on the ':' between IP and port
  114. colon := strings.IndexByte(e, ':')
  115. spaces := 0
  116. for colon > 0 && len(e)+spaces-colon < 6 {
  117. spaces++
  118. colon--
  119. }
  120. ep[i] = fmt.Sprintf("%21v", e+strings.Repeat(" ", spaces))
  121. }
  122. derp := p.DERP
  123. const derpPrefix = "127.3.3.40:"
  124. if strings.HasPrefix(derp, derpPrefix) {
  125. derp = "D" + derp[len(derpPrefix):]
  126. }
  127. var discoShort string
  128. if !p.DiscoKey.IsZero() {
  129. discoShort = p.DiscoKey.ShortString() + " "
  130. }
  131. // Most of the time, aip is just one element, so format the
  132. // table to look good in that case. This will also make multi-
  133. // subnet nodes stand out visually.
  134. fmt.Fprintf(buf, " %v %s%-2v %-15v : %v\n",
  135. p.Key.ShortString(),
  136. discoShort,
  137. derp,
  138. strings.Join(aip, " "),
  139. strings.Join(ep, " "))
  140. }
  141. // nodeConciseEqual reports whether a and b are equal for the fields accessed by printPeerConcise.
  142. func nodeConciseEqual(a, b *tailcfg.Node) bool {
  143. return a.Key == b.Key &&
  144. a.DERP == b.DERP &&
  145. a.DiscoKey == b.DiscoKey &&
  146. eqCIDRsIgnoreNil(a.AllowedIPs, b.AllowedIPs) &&
  147. eqStringsIgnoreNil(a.Endpoints, b.Endpoints)
  148. }
  149. func (b *NetworkMap) ConciseDiffFrom(a *NetworkMap) string {
  150. var diff strings.Builder
  151. // See if header (non-peers, "bare") part of the network map changed.
  152. // If so, print its diff lines first.
  153. if !a.equalConciseHeader(b) {
  154. diff.WriteByte('-')
  155. a.printConciseHeader(&diff)
  156. diff.WriteByte('+')
  157. b.printConciseHeader(&diff)
  158. }
  159. aps, bps := a.Peers, b.Peers
  160. for len(aps) > 0 && len(bps) > 0 {
  161. pa, pb := aps[0], bps[0]
  162. switch {
  163. case pa.ID == pb.ID:
  164. if !nodeConciseEqual(pa, pb) {
  165. diff.WriteByte('-')
  166. printPeerConcise(&diff, pa)
  167. diff.WriteByte('+')
  168. printPeerConcise(&diff, pb)
  169. }
  170. aps, bps = aps[1:], bps[1:]
  171. case pa.ID > pb.ID:
  172. // New peer in b.
  173. diff.WriteByte('+')
  174. printPeerConcise(&diff, pb)
  175. bps = bps[1:]
  176. case pb.ID > pa.ID:
  177. // Deleted peer in b.
  178. diff.WriteByte('-')
  179. printPeerConcise(&diff, pa)
  180. aps = aps[1:]
  181. }
  182. }
  183. for _, pa := range aps {
  184. diff.WriteByte('-')
  185. printPeerConcise(&diff, pa)
  186. }
  187. for _, pb := range bps {
  188. diff.WriteByte('+')
  189. printPeerConcise(&diff, pb)
  190. }
  191. return diff.String()
  192. }
  193. func (nm *NetworkMap) JSON() string {
  194. b, err := json.MarshalIndent(*nm, "", " ")
  195. if err != nil {
  196. return fmt.Sprintf("[json error: %v]", err)
  197. }
  198. return string(b)
  199. }
  200. // WGConfigFlags is a bitmask of flags to control the behavior of the
  201. // wireguard configuration generation done by NetMap.WGCfg.
  202. type WGConfigFlags int
  203. const (
  204. AllowSingleHosts WGConfigFlags = 1 << iota
  205. AllowSubnetRoutes
  206. AllowDefaultRoute
  207. )
  208. // EndpointDiscoSuffix is appended to the hex representation of a peer's discovery key
  209. // and is then the sole wireguard endpoint for peers with a non-zero discovery key.
  210. // This form is then recognize by magicsock's CreateEndpoint.
  211. const EndpointDiscoSuffix = ".disco.tailscale:12345"
  212. // WGCfg returns the NetworkMaps's Wireguard configuration.
  213. func (nm *NetworkMap) WGCfg(logf logger.Logf, flags WGConfigFlags) (*wgcfg.Config, error) {
  214. cfg := &wgcfg.Config{
  215. Name: "tailscale",
  216. PrivateKey: nm.PrivateKey,
  217. Addresses: nm.Addresses,
  218. ListenPort: nm.LocalPort,
  219. Peers: make([]wgcfg.Peer, 0, len(nm.Peers)),
  220. }
  221. for _, peer := range nm.Peers {
  222. if Debug.OnlyDisco && peer.DiscoKey.IsZero() {
  223. continue
  224. }
  225. if (flags&AllowSingleHosts) == 0 && len(peer.AllowedIPs) < 2 {
  226. logf("wgcfg: %v skipping a single-host peer.", peer.Key.ShortString())
  227. continue
  228. }
  229. cfg.Peers = append(cfg.Peers, wgcfg.Peer{
  230. PublicKey: wgcfg.Key(peer.Key),
  231. })
  232. cpeer := &cfg.Peers[len(cfg.Peers)-1]
  233. if peer.KeepAlive {
  234. cpeer.PersistentKeepalive = 25 // seconds
  235. }
  236. if !peer.DiscoKey.IsZero() {
  237. if err := appendEndpoint(cpeer, fmt.Sprintf("%x%s", peer.DiscoKey[:], EndpointDiscoSuffix)); err != nil {
  238. return nil, err
  239. }
  240. cpeer.Endpoints = []wgcfg.Endpoint{{Host: fmt.Sprintf("%x.disco.tailscale", peer.DiscoKey[:]), Port: 12345}}
  241. } else {
  242. if err := appendEndpoint(cpeer, peer.DERP); err != nil {
  243. return nil, err
  244. }
  245. for _, ep := range peer.Endpoints {
  246. if err := appendEndpoint(cpeer, ep); err != nil {
  247. return nil, err
  248. }
  249. }
  250. }
  251. for _, allowedIP := range peer.AllowedIPs {
  252. if allowedIP.Mask == 0 {
  253. if (flags & AllowDefaultRoute) == 0 {
  254. logf("wgcfg: %v skipping default route", peer.Key.ShortString())
  255. continue
  256. }
  257. } else if cidrIsSubnet(peer, allowedIP) {
  258. if (flags & AllowSubnetRoutes) == 0 {
  259. logf("wgcfg: %v skipping subnet route", peer.Key.ShortString())
  260. continue
  261. }
  262. }
  263. cpeer.AllowedIPs = append(cpeer.AllowedIPs, allowedIP)
  264. }
  265. }
  266. return cfg, nil
  267. }
  268. // cidrIsSubnet reports whether cidr is a non-default-route subnet
  269. // exported by node that is not one of its own self addresses.
  270. func cidrIsSubnet(node *tailcfg.Node, cidr wgcfg.CIDR) bool {
  271. if cidr.Mask == 0 {
  272. return false
  273. }
  274. if cidr.Mask < 32 {
  275. // Fast path for IPv4, to avoid loop below.
  276. //
  277. // TODO: if cidr.IP is an IPv6 address, we could do "< 128"
  278. // to avoid the range over node.Addresses. Or we could
  279. // just remove this fast path and unconditionally do the range
  280. // loop.
  281. return true
  282. }
  283. for _, selfCIDR := range node.Addresses {
  284. if cidr == selfCIDR {
  285. return false
  286. }
  287. }
  288. return true
  289. }
  290. func appendEndpoint(peer *wgcfg.Peer, epStr string) error {
  291. if epStr == "" {
  292. return nil
  293. }
  294. host, port, err := net.SplitHostPort(epStr)
  295. if err != nil {
  296. return fmt.Errorf("malformed endpoint %q for peer %v", epStr, peer.PublicKey.ShortString())
  297. }
  298. port16, err := strconv.ParseUint(port, 10, 16)
  299. if err != nil {
  300. return fmt.Errorf("invalid port in endpoint %q for peer %v", epStr, peer.PublicKey.ShortString())
  301. }
  302. peer.Endpoints = append(peer.Endpoints, wgcfg.Endpoint{Host: host, Port: uint16(port16)})
  303. return nil
  304. }
  305. // eqStringsIgnoreNil reports whether a and b have the same length and
  306. // contents, but ignore whether a or b are nil.
  307. func eqStringsIgnoreNil(a, b []string) bool {
  308. if len(a) != len(b) {
  309. return false
  310. }
  311. for i, v := range a {
  312. if v != b[i] {
  313. return false
  314. }
  315. }
  316. return true
  317. }
  318. // eqCIDRsIgnoreNil reports whether a and b have the same length and
  319. // contents, but ignore whether a or b are nil.
  320. func eqCIDRsIgnoreNil(a, b []wgcfg.CIDR) bool {
  321. if len(a) != len(b) {
  322. return false
  323. }
  324. for i, v := range a {
  325. if v != b[i] {
  326. return false
  327. }
  328. }
  329. return true
  330. }