mesh.go 24 KB

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