mesh.go 24 KB

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