mesh.go 24 KB

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