mesh.go 26 KB

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