mesh.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. // Copyright 2019 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build linux
  15. package mesh
  16. import (
  17. "bytes"
  18. "fmt"
  19. "io/ioutil"
  20. "net"
  21. "os"
  22. "sync"
  23. "time"
  24. "github.com/go-kit/kit/log"
  25. "github.com/go-kit/kit/log/level"
  26. "github.com/prometheus/client_golang/prometheus"
  27. "github.com/vishvananda/netlink"
  28. "github.com/squat/kilo/pkg/encapsulation"
  29. "github.com/squat/kilo/pkg/iproute"
  30. "github.com/squat/kilo/pkg/iptables"
  31. "github.com/squat/kilo/pkg/route"
  32. "github.com/squat/kilo/pkg/wireguard"
  33. )
  34. const (
  35. // kiloPath is the directory where Kilo stores its configuration.
  36. kiloPath = "/var/lib/kilo"
  37. // privateKeyPath is the filepath where the WireGuard private key is stored.
  38. privateKeyPath = kiloPath + "/key"
  39. // confPath is the filepath where the WireGuard configuration is stored.
  40. confPath = kiloPath + "/conf"
  41. )
  42. // Mesh is able to create Kilo network meshes.
  43. type Mesh struct {
  44. Backend
  45. cleanUpIface bool
  46. cni bool
  47. cniPath string
  48. enc encapsulation.Encapsulator
  49. externalIP *net.IPNet
  50. granularity Granularity
  51. hostname string
  52. internalIP *net.IPNet
  53. ipTables *iptables.Controller
  54. kiloIface int
  55. key []byte
  56. local bool
  57. port uint32
  58. priv []byte
  59. privIface int
  60. pub []byte
  61. resyncPeriod time.Duration
  62. stop chan struct{}
  63. subnet *net.IPNet
  64. table *route.Table
  65. wireGuardIP *net.IPNet
  66. // nodes and peers are mutable fields in the struct
  67. // and need to be guarded.
  68. nodes map[string]*Node
  69. peers map[string]*Peer
  70. mu sync.Mutex
  71. errorCounter *prometheus.CounterVec
  72. leaderGuage prometheus.Gauge
  73. nodesGuage prometheus.Gauge
  74. peersGuage prometheus.Gauge
  75. reconcileCounter prometheus.Counter
  76. logger log.Logger
  77. }
  78. // New returns a new Mesh instance.
  79. func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularity, hostname string, port uint32, subnet *net.IPNet, local, cni bool, cniPath, iface string, cleanUpIface bool, createIface bool, mtu uint, resyncPeriod time.Duration, prioritisePrivateAddr bool, logger log.Logger) (*Mesh, error) {
  80. if err := os.MkdirAll(kiloPath, 0700); err != nil {
  81. return nil, fmt.Errorf("failed to create directory to store configuration: %v", err)
  82. }
  83. private, err := ioutil.ReadFile(privateKeyPath)
  84. private = bytes.Trim(private, "\n")
  85. if err != nil {
  86. level.Warn(logger).Log("msg", "no private key found on disk; generating one now")
  87. if private, err = wireguard.GenKey(); err != nil {
  88. return nil, err
  89. }
  90. }
  91. public, err := wireguard.PubKey(private)
  92. if err != nil {
  93. return nil, err
  94. }
  95. if err := ioutil.WriteFile(privateKeyPath, private, 0600); err != nil {
  96. return nil, fmt.Errorf("failed to write private key to disk: %v", err)
  97. }
  98. cniIndex, err := cniDeviceIndex()
  99. if err != nil {
  100. return nil, fmt.Errorf("failed to query netlink for CNI device: %v", err)
  101. }
  102. var kiloIface int
  103. if createIface {
  104. kiloIface, _, err = wireguard.New(iface, mtu)
  105. if err != nil {
  106. return nil, fmt.Errorf("failed to create WireGuard interface: %v", err)
  107. }
  108. } else {
  109. link, err := netlink.LinkByName(iface)
  110. if err != nil {
  111. return nil, fmt.Errorf("failed to get interface index: %v", err)
  112. }
  113. kiloIface = link.Attrs().Index
  114. }
  115. privateIP, publicIP, err := getIP(hostname, kiloIface, enc.Index(), cniIndex)
  116. if err != nil {
  117. return nil, fmt.Errorf("failed to find public IP: %v", err)
  118. }
  119. var privIface int
  120. if privateIP != nil {
  121. ifaces, err := interfacesForIP(privateIP)
  122. if err != nil {
  123. return nil, fmt.Errorf("failed to find interface for private IP: %v", err)
  124. }
  125. privIface = ifaces[0].Index
  126. if enc.Strategy() != encapsulation.Never {
  127. if err := enc.Init(privIface); err != nil {
  128. return nil, fmt.Errorf("failed to initialize encapsulator: %v", err)
  129. }
  130. }
  131. level.Debug(logger).Log("msg", fmt.Sprintf("using %s as the private IP address", privateIP.String()))
  132. } else {
  133. enc = encapsulation.Noop(enc.Strategy())
  134. level.Debug(logger).Log("msg", "running without a private IP address")
  135. }
  136. var externalIP *net.IPNet
  137. if prioritisePrivateAddr && privateIP != nil {
  138. externalIP = privateIP
  139. } else {
  140. externalIP = publicIP
  141. }
  142. level.Debug(logger).Log("msg", fmt.Sprintf("using %s as the public IP address", publicIP.String()))
  143. ipTables, err := iptables.New(iptables.WithLogger(log.With(logger, "component", "iptables")), iptables.WithResyncPeriod(resyncPeriod))
  144. if err != nil {
  145. return nil, fmt.Errorf("failed to IP tables controller: %v", err)
  146. }
  147. return &Mesh{
  148. Backend: backend,
  149. cleanUpIface: cleanUpIface,
  150. cni: cni,
  151. cniPath: cniPath,
  152. enc: enc,
  153. externalIP: externalIP,
  154. granularity: granularity,
  155. hostname: hostname,
  156. internalIP: privateIP,
  157. ipTables: ipTables,
  158. kiloIface: kiloIface,
  159. nodes: make(map[string]*Node),
  160. peers: make(map[string]*Peer),
  161. port: port,
  162. priv: private,
  163. privIface: privIface,
  164. pub: public,
  165. resyncPeriod: resyncPeriod,
  166. local: local,
  167. stop: make(chan struct{}),
  168. subnet: subnet,
  169. table: route.NewTable(),
  170. errorCounter: prometheus.NewCounterVec(prometheus.CounterOpts{
  171. Name: "kilo_errors_total",
  172. Help: "Number of errors that occurred while administering the mesh.",
  173. }, []string{"event"}),
  174. leaderGuage: prometheus.NewGauge(prometheus.GaugeOpts{
  175. Name: "kilo_leader",
  176. Help: "Leadership status of the node.",
  177. }),
  178. nodesGuage: prometheus.NewGauge(prometheus.GaugeOpts{
  179. Name: "kilo_nodes",
  180. Help: "Number of nodes in the mesh.",
  181. }),
  182. peersGuage: prometheus.NewGauge(prometheus.GaugeOpts{
  183. Name: "kilo_peers",
  184. Help: "Number of peers in the mesh.",
  185. }),
  186. reconcileCounter: prometheus.NewCounter(prometheus.CounterOpts{
  187. Name: "kilo_reconciles_total",
  188. Help: "Number of reconciliation attempts.",
  189. }),
  190. logger: logger,
  191. }, nil
  192. }
  193. // Run starts the mesh.
  194. func (m *Mesh) Run() error {
  195. if err := m.Nodes().Init(m.stop); err != nil {
  196. return fmt.Errorf("failed to initialize node backend: %v", err)
  197. }
  198. // Try to set the CNI config quickly.
  199. if m.cni {
  200. if n, err := m.Nodes().Get(m.hostname); err == nil {
  201. m.nodes[m.hostname] = n
  202. m.updateCNIConfig()
  203. } else {
  204. level.Warn(m.logger).Log("error", fmt.Errorf("failed to get node %q: %v", m.hostname, err))
  205. }
  206. }
  207. if err := m.Peers().Init(m.stop); err != nil {
  208. return fmt.Errorf("failed to initialize peer backend: %v", err)
  209. }
  210. ipTablesErrors, err := m.ipTables.Run(m.stop)
  211. if err != nil {
  212. return fmt.Errorf("failed to watch for IP tables updates: %v", err)
  213. }
  214. routeErrors, err := m.table.Run(m.stop)
  215. if err != nil {
  216. return fmt.Errorf("failed to watch for route table updates: %v", err)
  217. }
  218. go func() {
  219. for {
  220. var err error
  221. select {
  222. case err = <-ipTablesErrors:
  223. case err = <-routeErrors:
  224. case <-m.stop:
  225. return
  226. }
  227. if err != nil {
  228. level.Error(m.logger).Log("error", err)
  229. m.errorCounter.WithLabelValues("run").Inc()
  230. }
  231. }
  232. }()
  233. defer m.cleanUp()
  234. resync := time.NewTimer(m.resyncPeriod)
  235. checkIn := time.NewTimer(checkInPeriod)
  236. nw := m.Nodes().Watch()
  237. pw := m.Peers().Watch()
  238. var ne *NodeEvent
  239. var pe *PeerEvent
  240. for {
  241. select {
  242. case ne = <-nw:
  243. m.syncNodes(ne)
  244. case pe = <-pw:
  245. m.syncPeers(pe)
  246. case <-checkIn.C:
  247. m.checkIn()
  248. checkIn.Reset(checkInPeriod)
  249. case <-resync.C:
  250. if m.cni {
  251. m.updateCNIConfig()
  252. }
  253. m.applyTopology()
  254. resync.Reset(m.resyncPeriod)
  255. case <-m.stop:
  256. return nil
  257. }
  258. }
  259. }
  260. func (m *Mesh) syncNodes(e *NodeEvent) {
  261. logger := log.With(m.logger, "event", e.Type)
  262. level.Debug(logger).Log("msg", "syncing nodes", "event", e.Type)
  263. if isSelf(m.hostname, e.Node) {
  264. level.Debug(logger).Log("msg", "processing local node", "node", e.Node)
  265. m.handleLocal(e.Node)
  266. return
  267. }
  268. var diff bool
  269. m.mu.Lock()
  270. if !e.Node.Ready() {
  271. // Trace non ready nodes with their presence in the mesh.
  272. _, ok := m.nodes[e.Node.Name]
  273. level.Debug(logger).Log("msg", "received non ready node", "node", e.Node, "in-mesh", ok)
  274. }
  275. switch e.Type {
  276. case AddEvent:
  277. fallthrough
  278. case UpdateEvent:
  279. if !nodesAreEqual(m.nodes[e.Node.Name], e.Node) {
  280. diff = true
  281. }
  282. // Even if the nodes are the same,
  283. // overwrite the old node to update the timestamp.
  284. m.nodes[e.Node.Name] = e.Node
  285. case DeleteEvent:
  286. delete(m.nodes, e.Node.Name)
  287. diff = true
  288. }
  289. m.mu.Unlock()
  290. if diff {
  291. level.Info(logger).Log("node", e.Node)
  292. m.applyTopology()
  293. }
  294. }
  295. func (m *Mesh) syncPeers(e *PeerEvent) {
  296. logger := log.With(m.logger, "event", e.Type)
  297. level.Debug(logger).Log("msg", "syncing peers", "event", e.Type)
  298. var diff bool
  299. m.mu.Lock()
  300. // Peers are indexed by public key.
  301. key := string(e.Peer.PublicKey)
  302. if !e.Peer.Ready() {
  303. // Trace non ready peer with their presence in the mesh.
  304. _, ok := m.peers[key]
  305. level.Debug(logger).Log("msg", "received non ready peer", "peer", e.Peer, "in-mesh", ok)
  306. }
  307. switch e.Type {
  308. case AddEvent:
  309. fallthrough
  310. case UpdateEvent:
  311. if e.Old != nil && key != string(e.Old.PublicKey) {
  312. delete(m.peers, string(e.Old.PublicKey))
  313. diff = true
  314. }
  315. if !peersAreEqual(m.peers[key], e.Peer) {
  316. m.peers[key] = e.Peer
  317. diff = true
  318. }
  319. case DeleteEvent:
  320. delete(m.peers, key)
  321. diff = true
  322. }
  323. m.mu.Unlock()
  324. if diff {
  325. level.Info(logger).Log("peer", e.Peer)
  326. m.applyTopology()
  327. }
  328. }
  329. // checkIn will try to update the local node's LastSeen timestamp
  330. // in the backend.
  331. func (m *Mesh) checkIn() {
  332. m.mu.Lock()
  333. defer m.mu.Unlock()
  334. n := m.nodes[m.hostname]
  335. if n == nil {
  336. level.Debug(m.logger).Log("msg", "no local node found in backend")
  337. return
  338. }
  339. oldTime := n.LastSeen
  340. n.LastSeen = time.Now().Unix()
  341. if err := m.Nodes().Set(m.hostname, n); err != nil {
  342. level.Error(m.logger).Log("error", fmt.Sprintf("failed to set local node: %v", err), "node", n)
  343. m.errorCounter.WithLabelValues("checkin").Inc()
  344. // Revert time.
  345. n.LastSeen = oldTime
  346. return
  347. }
  348. level.Debug(m.logger).Log("msg", "successfully checked in local node in backend")
  349. }
  350. func (m *Mesh) handleLocal(n *Node) {
  351. // Allow the IPs to be overridden.
  352. if n.Endpoint == nil || (n.Endpoint.DNS == "" && n.Endpoint.IP == nil) {
  353. n.Endpoint = &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: m.externalIP.IP}, Port: m.port}
  354. }
  355. if n.InternalIP == nil && !n.NoInternalIP {
  356. n.InternalIP = m.internalIP
  357. }
  358. // Compare the given node to the calculated local node.
  359. // Take leader, location, and subnet from the argument, as these
  360. // are not determined by kilo.
  361. local := &Node{
  362. Endpoint: n.Endpoint,
  363. Key: m.pub,
  364. NoInternalIP: n.NoInternalIP,
  365. InternalIP: n.InternalIP,
  366. LastSeen: time.Now().Unix(),
  367. Leader: n.Leader,
  368. Location: n.Location,
  369. Name: m.hostname,
  370. PersistentKeepalive: n.PersistentKeepalive,
  371. Subnet: n.Subnet,
  372. WireGuardIP: m.wireGuardIP,
  373. DiscoveredEndpoints: n.DiscoveredEndpoints,
  374. AllowedLocationIPs: n.AllowedLocationIPs,
  375. Granularity: m.granularity,
  376. }
  377. if !nodesAreEqual(n, local) {
  378. level.Debug(m.logger).Log("msg", "local node differs from backend")
  379. if err := m.Nodes().Set(m.hostname, local); err != nil {
  380. level.Error(m.logger).Log("error", fmt.Sprintf("failed to set local node: %v", err), "node", local)
  381. m.errorCounter.WithLabelValues("local").Inc()
  382. return
  383. }
  384. level.Debug(m.logger).Log("msg", "successfully reconciled local node against backend")
  385. }
  386. m.mu.Lock()
  387. n = m.nodes[m.hostname]
  388. if n == nil {
  389. n = &Node{}
  390. }
  391. m.mu.Unlock()
  392. if !nodesAreEqual(n, local) {
  393. m.mu.Lock()
  394. m.nodes[local.Name] = local
  395. m.mu.Unlock()
  396. m.applyTopology()
  397. }
  398. }
  399. func (m *Mesh) applyTopology() {
  400. m.reconcileCounter.Inc()
  401. m.mu.Lock()
  402. defer m.mu.Unlock()
  403. // If we can't resolve an endpoint, then fail and retry later.
  404. if err := m.resolveEndpoints(); err != nil {
  405. level.Error(m.logger).Log("error", err)
  406. m.errorCounter.WithLabelValues("apply").Inc()
  407. return
  408. }
  409. // Ensure only ready nodes are considered.
  410. nodes := make(map[string]*Node)
  411. var readyNodes float64
  412. for k := range m.nodes {
  413. m.nodes[k].Granularity = m.granularity
  414. if !m.nodes[k].Ready() {
  415. continue
  416. }
  417. // Make it point to the node without copy.
  418. nodes[k] = m.nodes[k]
  419. readyNodes++
  420. }
  421. // Ensure only ready nodes are considered.
  422. peers := make(map[string]*Peer)
  423. var readyPeers float64
  424. for k := range m.peers {
  425. if !m.peers[k].Ready() {
  426. continue
  427. }
  428. // Make it point the peer without copy.
  429. peers[k] = m.peers[k]
  430. readyPeers++
  431. }
  432. m.nodesGuage.Set(readyNodes)
  433. m.peersGuage.Set(readyPeers)
  434. // We cannot do anything with the topology until the local node is available.
  435. if nodes[m.hostname] == nil {
  436. return
  437. }
  438. // Find the Kilo interface name.
  439. link, err := linkByIndex(m.kiloIface)
  440. if err != nil {
  441. level.Error(m.logger).Log("error", err)
  442. m.errorCounter.WithLabelValues("apply").Inc()
  443. return
  444. }
  445. // Find the old configuration.
  446. oldConfDump, err := wireguard.ShowDump(link.Attrs().Name)
  447. if err != nil {
  448. level.Error(m.logger).Log("error", err)
  449. m.errorCounter.WithLabelValues("apply").Inc()
  450. return
  451. }
  452. oldConf, err := wireguard.ParseDump(oldConfDump)
  453. if err != nil {
  454. level.Error(m.logger).Log("error", err)
  455. m.errorCounter.WithLabelValues("apply").Inc()
  456. return
  457. }
  458. natEndpoints := discoverNATEndpoints(nodes, peers, oldConf, m.logger)
  459. nodes[m.hostname].DiscoveredEndpoints = natEndpoints
  460. t, err := NewTopology(nodes, peers, m.granularity, m.hostname, nodes[m.hostname].Endpoint.Port, m.priv, m.subnet, nodes[m.hostname].PersistentKeepalive, m.logger)
  461. if err != nil {
  462. level.Error(m.logger).Log("error", err)
  463. m.errorCounter.WithLabelValues("apply").Inc()
  464. return
  465. }
  466. // Update the node's WireGuard IP.
  467. if t.leader {
  468. m.wireGuardIP = t.wireGuardCIDR
  469. } else {
  470. m.wireGuardIP = nil
  471. }
  472. conf := t.Conf()
  473. buf, err := conf.Bytes()
  474. if err != nil {
  475. level.Error(m.logger).Log("error", err)
  476. m.errorCounter.WithLabelValues("apply").Inc()
  477. return
  478. }
  479. if err := ioutil.WriteFile(confPath, buf, 0600); err != nil {
  480. level.Error(m.logger).Log("error", err)
  481. m.errorCounter.WithLabelValues("apply").Inc()
  482. return
  483. }
  484. ipRules := t.Rules(m.cni)
  485. // If we are handling local routes, ensure the local
  486. // tunnel has an IP address and IPIP traffic is allowed.
  487. if m.enc.Strategy() != encapsulation.Never && m.local {
  488. var cidrs []*net.IPNet
  489. for _, s := range t.segments {
  490. // If the location prefix is not logicalLocation, but nodeLocation,
  491. // we don't need to set any extra rules for encapsulation anyways
  492. // because traffic will go over WireGuard.
  493. if s.location == logicalLocationPrefix+nodes[m.hostname].Location {
  494. for i := range s.privateIPs {
  495. cidrs = append(cidrs, oneAddressCIDR(s.privateIPs[i]))
  496. }
  497. break
  498. }
  499. }
  500. ipRules = append(ipRules, m.enc.Rules(cidrs)...)
  501. // If we are handling local routes, ensure the local
  502. // tunnel has an IP address.
  503. if err := m.enc.Set(oneAddressCIDR(newAllocator(*nodes[m.hostname].Subnet).next().IP)); err != nil {
  504. level.Error(m.logger).Log("error", err)
  505. m.errorCounter.WithLabelValues("apply").Inc()
  506. return
  507. }
  508. }
  509. if err := m.ipTables.Set(ipRules); err != nil {
  510. level.Error(m.logger).Log("error", err)
  511. m.errorCounter.WithLabelValues("apply").Inc()
  512. return
  513. }
  514. if t.leader {
  515. m.leaderGuage.Set(1)
  516. if err := iproute.SetAddress(m.kiloIface, t.wireGuardCIDR); err != nil {
  517. level.Error(m.logger).Log("error", err)
  518. m.errorCounter.WithLabelValues("apply").Inc()
  519. return
  520. }
  521. // Setting the WireGuard configuration interrupts existing connections
  522. // so only set the configuration if it has changed.
  523. equal := conf.Equal(oldConf)
  524. if !equal {
  525. level.Info(m.logger).Log("msg", "WireGuard configurations are different")
  526. if err := wireguard.SetConf(link.Attrs().Name, confPath); err != nil {
  527. level.Error(m.logger).Log("error", err)
  528. m.errorCounter.WithLabelValues("apply").Inc()
  529. return
  530. }
  531. }
  532. if err := iproute.Set(m.kiloIface, true); err != nil {
  533. level.Error(m.logger).Log("error", err)
  534. m.errorCounter.WithLabelValues("apply").Inc()
  535. return
  536. }
  537. } else {
  538. m.leaderGuage.Set(0)
  539. level.Debug(m.logger).Log("msg", "local node is not the leader")
  540. if err := iproute.Set(m.kiloIface, false); err != nil {
  541. level.Error(m.logger).Log("error", err)
  542. m.errorCounter.WithLabelValues("apply").Inc()
  543. return
  544. }
  545. }
  546. // We need to add routes last since they may depend
  547. // on the WireGuard interface.
  548. routes, rules := t.Routes(link.Attrs().Name, m.kiloIface, m.privIface, m.enc.Index(), m.local, m.enc)
  549. if err := m.table.Set(routes, rules); err != nil {
  550. level.Error(m.logger).Log("error", err)
  551. m.errorCounter.WithLabelValues("apply").Inc()
  552. }
  553. }
  554. // RegisterMetrics registers Prometheus metrics on the given Prometheus
  555. // registerer.
  556. func (m *Mesh) RegisterMetrics(r prometheus.Registerer) {
  557. r.MustRegister(
  558. m.errorCounter,
  559. m.leaderGuage,
  560. m.nodesGuage,
  561. m.peersGuage,
  562. m.reconcileCounter,
  563. )
  564. }
  565. // Stop stops the mesh.
  566. func (m *Mesh) Stop() {
  567. close(m.stop)
  568. }
  569. func (m *Mesh) cleanUp() {
  570. if err := m.ipTables.CleanUp(); err != nil {
  571. level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up IP tables: %v", err))
  572. m.errorCounter.WithLabelValues("cleanUp").Inc()
  573. }
  574. if err := m.table.CleanUp(); err != nil {
  575. level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up routes: %v", err))
  576. m.errorCounter.WithLabelValues("cleanUp").Inc()
  577. }
  578. if err := os.Remove(confPath); err != nil {
  579. level.Error(m.logger).Log("error", fmt.Sprintf("failed to delete configuration file: %v", err))
  580. m.errorCounter.WithLabelValues("cleanUp").Inc()
  581. }
  582. if m.cleanUpIface {
  583. if err := iproute.RemoveInterface(m.kiloIface); err != nil {
  584. level.Error(m.logger).Log("error", fmt.Sprintf("failed to remove WireGuard interface: %v", err))
  585. m.errorCounter.WithLabelValues("cleanUp").Inc()
  586. }
  587. }
  588. if err := m.Nodes().CleanUp(m.hostname); err != nil {
  589. level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up node backend: %v", err))
  590. m.errorCounter.WithLabelValues("cleanUp").Inc()
  591. }
  592. if err := m.Peers().CleanUp(m.hostname); err != nil {
  593. level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up peer backend: %v", err))
  594. m.errorCounter.WithLabelValues("cleanUp").Inc()
  595. }
  596. if err := m.enc.CleanUp(); err != nil {
  597. level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up encapsulator: %v", err))
  598. m.errorCounter.WithLabelValues("cleanUp").Inc()
  599. }
  600. }
  601. func (m *Mesh) resolveEndpoints() error {
  602. for k := range m.nodes {
  603. // Skip unready nodes, since they will not be used
  604. // in the topology anyways.
  605. if !m.nodes[k].Ready() {
  606. continue
  607. }
  608. // If the node is ready, then the endpoint is not nil
  609. // but it may not have a DNS name.
  610. if m.nodes[k].Endpoint.DNS == "" {
  611. continue
  612. }
  613. if err := resolveEndpoint(m.nodes[k].Endpoint); err != nil {
  614. return err
  615. }
  616. }
  617. for k := range m.peers {
  618. // Skip unready peers, since they will not be used
  619. // in the topology anyways.
  620. if !m.peers[k].Ready() {
  621. continue
  622. }
  623. // Peers may have nil endpoints.
  624. if m.peers[k].Endpoint == nil || m.peers[k].Endpoint.DNS == "" {
  625. continue
  626. }
  627. if err := resolveEndpoint(m.peers[k].Endpoint); err != nil {
  628. return err
  629. }
  630. }
  631. return nil
  632. }
  633. func resolveEndpoint(endpoint *wireguard.Endpoint) error {
  634. ips, err := net.LookupIP(endpoint.DNS)
  635. if err != nil {
  636. return fmt.Errorf("failed to look up DNS name %q: %v", endpoint.DNS, err)
  637. }
  638. nets := make([]*net.IPNet, len(ips), len(ips))
  639. for i := range ips {
  640. nets[i] = oneAddressCIDR(ips[i])
  641. }
  642. sortIPs(nets)
  643. if len(nets) == 0 {
  644. return fmt.Errorf("did not find any addresses for DNS name %q", endpoint.DNS)
  645. }
  646. endpoint.IP = nets[0].IP
  647. return nil
  648. }
  649. func isSelf(hostname string, node *Node) bool {
  650. return node != nil && node.Name == hostname
  651. }
  652. func nodesAreEqual(a, b *Node) bool {
  653. if (a != nil) != (b != nil) {
  654. return false
  655. }
  656. if a == b {
  657. return true
  658. }
  659. // Check the DNS name first since this package
  660. // is doing the DNS resolution.
  661. if !a.Endpoint.Equal(b.Endpoint, true) {
  662. return false
  663. }
  664. // Ignore LastSeen when comparing equality we want to check if the nodes are
  665. // equivalent. However, we do want to check if LastSeen has transitioned
  666. // between valid and invalid.
  667. return string(a.Key) == string(b.Key) && ipNetsEqual(a.WireGuardIP, b.WireGuardIP) && ipNetsEqual(a.InternalIP, b.InternalIP) && a.Leader == b.Leader && a.Location == b.Location && a.Name == b.Name && subnetsEqual(a.Subnet, b.Subnet) && a.Ready() == b.Ready() && a.PersistentKeepalive == b.PersistentKeepalive && discoveredEndpointsAreEqual(a.DiscoveredEndpoints, b.DiscoveredEndpoints) && ipNetSlicesEqual(a.AllowedLocationIPs, b.AllowedLocationIPs) && a.Granularity == b.Granularity
  668. }
  669. func peersAreEqual(a, b *Peer) bool {
  670. if !(a != nil) == (b != nil) {
  671. return false
  672. }
  673. if a == b {
  674. return true
  675. }
  676. // Check the DNS name first since this package
  677. // is doing the DNS resolution.
  678. if !a.Endpoint.Equal(b.Endpoint, true) {
  679. return false
  680. }
  681. if len(a.AllowedIPs) != len(b.AllowedIPs) {
  682. return false
  683. }
  684. for i := range a.AllowedIPs {
  685. if !ipNetsEqual(a.AllowedIPs[i], b.AllowedIPs[i]) {
  686. return false
  687. }
  688. }
  689. return string(a.PublicKey) == string(b.PublicKey) && string(a.PresharedKey) == string(b.PresharedKey) && a.PersistentKeepalive == b.PersistentKeepalive
  690. }
  691. func ipNetsEqual(a, b *net.IPNet) bool {
  692. if a == nil && b == nil {
  693. return true
  694. }
  695. if (a != nil) != (b != nil) {
  696. return false
  697. }
  698. if a.Mask.String() != b.Mask.String() {
  699. return false
  700. }
  701. return a.IP.Equal(b.IP)
  702. }
  703. func ipNetSlicesEqual(a, b []*net.IPNet) bool {
  704. if len(a) != len(b) {
  705. return false
  706. }
  707. for i := range a {
  708. if !ipNetsEqual(a[i], b[i]) {
  709. return false
  710. }
  711. }
  712. return true
  713. }
  714. func subnetsEqual(a, b *net.IPNet) bool {
  715. if a == nil && b == nil {
  716. return true
  717. }
  718. if (a != nil) != (b != nil) {
  719. return false
  720. }
  721. if a.Mask.String() != b.Mask.String() {
  722. return false
  723. }
  724. if !a.Contains(b.IP) {
  725. return false
  726. }
  727. if !b.Contains(a.IP) {
  728. return false
  729. }
  730. return true
  731. }
  732. func discoveredEndpointsAreEqual(a, b map[string]*wireguard.Endpoint) bool {
  733. if a == nil && b == nil {
  734. return true
  735. }
  736. if (a != nil) != (b != nil) {
  737. return false
  738. }
  739. if len(a) != len(b) {
  740. return false
  741. }
  742. for k := range a {
  743. if !a[k].Equal(b[k], false) {
  744. return false
  745. }
  746. }
  747. return true
  748. }
  749. func linkByIndex(index int) (netlink.Link, error) {
  750. link, err := netlink.LinkByIndex(index)
  751. if err != nil {
  752. return nil, fmt.Errorf("failed to get interface: %v", err)
  753. }
  754. return link, nil
  755. }
  756. // discoverNATEndpoints uses the node's WireGuard configuration to returns a list of the most recently discovered endpoints for all nodes and peers behind NAT so that they can roam.
  757. func discoverNATEndpoints(nodes map[string]*Node, peers map[string]*Peer, conf *wireguard.Conf, logger log.Logger) map[string]*wireguard.Endpoint {
  758. natEndpoints := make(map[string]*wireguard.Endpoint)
  759. keys := make(map[string]*wireguard.Peer)
  760. for i := range conf.Peers {
  761. keys[string(conf.Peers[i].PublicKey)] = conf.Peers[i]
  762. }
  763. for _, n := range nodes {
  764. if peer, ok := keys[string(n.Key)]; ok && n.PersistentKeepalive > 0 {
  765. level.Debug(logger).Log("msg", "WireGuard Update NAT Endpoint", "node", n.Name, "endpoint", peer.Endpoint, "former-endpoint", n.Endpoint, "same", n.Endpoint.Equal(peer.Endpoint, false), "latest-handshake", peer.LatestHandshake)
  766. if (peer.LatestHandshake != time.Time{}) {
  767. natEndpoints[string(n.Key)] = peer.Endpoint
  768. }
  769. }
  770. }
  771. for _, p := range peers {
  772. if peer, ok := keys[string(p.PublicKey)]; ok && p.PersistentKeepalive > 0 {
  773. if (peer.LatestHandshake != time.Time{}) {
  774. natEndpoints[string(p.PublicKey)] = peer.Endpoint
  775. }
  776. }
  777. }
  778. level.Debug(logger).Log("msg", "Discovered WireGuard NAT Endpoints", "DiscoveredEndpoints", natEndpoints)
  779. return natEndpoints
  780. }