mesh.go 24 KB

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