mesh.go 24 KB

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