mesh.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. "fmt"
  17. "io/ioutil"
  18. "net"
  19. "os"
  20. "sync"
  21. "time"
  22. "github.com/go-kit/kit/log"
  23. "github.com/go-kit/kit/log/level"
  24. "github.com/prometheus/client_golang/prometheus"
  25. "github.com/vishvananda/netlink"
  26. "github.com/squat/kilo/pkg/iproute"
  27. "github.com/squat/kilo/pkg/ipset"
  28. "github.com/squat/kilo/pkg/iptables"
  29. "github.com/squat/kilo/pkg/route"
  30. "github.com/squat/kilo/pkg/wireguard"
  31. )
  32. const resyncPeriod = 30 * time.Second
  33. const (
  34. // KiloPath is the directory where Kilo stores its configuration.
  35. KiloPath = "/var/lib/kilo"
  36. // PrivateKeyPath is the filepath where the WireGuard private key is stored.
  37. PrivateKeyPath = KiloPath + "/key"
  38. // ConfPath is the filepath where the WireGuard configuration is stored.
  39. ConfPath = KiloPath + "/conf"
  40. )
  41. // Granularity represents the abstraction level at which the network
  42. // should be meshed.
  43. type Granularity string
  44. // Encapsulate identifies what packets within a location should
  45. // be encapsulated.
  46. type Encapsulate string
  47. const (
  48. // DataCenterGranularity indicates that the network should create
  49. // a mesh between data-centers but not between nodes within a
  50. // single data-center.
  51. DataCenterGranularity Granularity = "data-center"
  52. // NodeGranularity indicates that the network should create
  53. // a mesh between every node.
  54. NodeGranularity Granularity = "node"
  55. // NeverEncapsulate indicates that no packets within a location
  56. // should be encapsulated.
  57. NeverEncapsulate Encapsulate = "never"
  58. // CrossSubnetEncapsulate indicates that only packets that
  59. // traverse subnets within a location should be encapsulated.
  60. CrossSubnetEncapsulate Encapsulate = "crosssubnet"
  61. // AlwaysEncapsulate indicates that all packets within a location
  62. // should be encapsulated.
  63. AlwaysEncapsulate Encapsulate = "always"
  64. )
  65. // Node represents a node in the network.
  66. type Node struct {
  67. ExternalIP *net.IPNet
  68. Key []byte
  69. InternalIP *net.IPNet
  70. // LastSeen is a Unix time for the last time
  71. // the node confirmed it was live.
  72. LastSeen int64
  73. // Leader is a suggestion to Kilo that
  74. // the node wants to lead its segment.
  75. Leader bool
  76. Location string
  77. Name string
  78. Subnet *net.IPNet
  79. }
  80. // Ready indicates whether or not the node is ready.
  81. func (n *Node) Ready() bool {
  82. return n != nil && n.ExternalIP != nil && n.Key != nil && n.InternalIP != nil && n.Subnet != nil && time.Now().Unix()-n.LastSeen < int64(resyncPeriod)*2/int64(time.Second)
  83. }
  84. // EventType describes what kind of an action an event represents.
  85. type EventType string
  86. const (
  87. // AddEvent represents an action where an item was added.
  88. AddEvent EventType = "add"
  89. // DeleteEvent represents an action where an item was removed.
  90. DeleteEvent EventType = "delete"
  91. // UpdateEvent represents an action where an item was updated.
  92. UpdateEvent EventType = "update"
  93. )
  94. // Event represents an update event concerning a node in the cluster.
  95. type Event struct {
  96. Type EventType
  97. Node *Node
  98. }
  99. // Backend can get nodes by name, init itself,
  100. // list the nodes that should be meshed,
  101. // set Kilo properties for a node,
  102. // clean up any changes applied to the backend,
  103. // and watch for changes to nodes.
  104. type Backend interface {
  105. CleanUp(string) error
  106. Get(string) (*Node, error)
  107. Init(<-chan struct{}) error
  108. List() ([]*Node, error)
  109. Set(string, *Node) error
  110. Watch() <-chan *Event
  111. }
  112. // Mesh is able to create Kilo network meshes.
  113. type Mesh struct {
  114. Backend
  115. encapsulate Encapsulate
  116. externalIP *net.IPNet
  117. granularity Granularity
  118. hostname string
  119. internalIP *net.IPNet
  120. ipset *ipset.Set
  121. ipTables *iptables.Controller
  122. kiloIface int
  123. key []byte
  124. local bool
  125. port int
  126. priv []byte
  127. privIface int
  128. pub []byte
  129. pubIface int
  130. stop chan struct{}
  131. subnet *net.IPNet
  132. table *route.Table
  133. tunlIface int
  134. // nodes is a mutable field in the struct
  135. // and needs to be guarded.
  136. nodes map[string]*Node
  137. mu sync.Mutex
  138. errorCounter *prometheus.CounterVec
  139. nodesGuage prometheus.Gauge
  140. reconcileCounter prometheus.Counter
  141. logger log.Logger
  142. }
  143. // New returns a new Mesh instance.
  144. func New(backend Backend, encapsulate Encapsulate, granularity Granularity, hostname string, port int, subnet *net.IPNet, local bool, logger log.Logger) (*Mesh, error) {
  145. if err := os.MkdirAll(KiloPath, 0700); err != nil {
  146. return nil, fmt.Errorf("failed to create directory to store configuration: %v", err)
  147. }
  148. private, err := ioutil.ReadFile(PrivateKeyPath)
  149. if err != nil {
  150. level.Warn(logger).Log("msg", "no private key found on disk; generating one now")
  151. if private, err = wireguard.GenKey(); err != nil {
  152. return nil, err
  153. }
  154. }
  155. public, err := wireguard.PubKey(private)
  156. if err != nil {
  157. return nil, err
  158. }
  159. if err := ioutil.WriteFile(PrivateKeyPath, private, 0600); err != nil {
  160. return nil, fmt.Errorf("failed to write private key to disk: %v", err)
  161. }
  162. privateIP, publicIP, err := getIP(hostname)
  163. if err != nil {
  164. return nil, fmt.Errorf("failed to find public IP: %v", err)
  165. }
  166. ifaces, err := interfacesForIP(privateIP)
  167. if err != nil {
  168. return nil, fmt.Errorf("failed to find interface for private IP: %v", err)
  169. }
  170. privIface := ifaces[0].Index
  171. ifaces, err = interfacesForIP(publicIP)
  172. if err != nil {
  173. return nil, fmt.Errorf("failed to find interface for public IP: %v", err)
  174. }
  175. pubIface := ifaces[0].Index
  176. kiloIface, err := wireguard.New("kilo")
  177. if err != nil {
  178. return nil, fmt.Errorf("failed to create WireGuard interface: %v", err)
  179. }
  180. var tunlIface int
  181. if encapsulate != NeverEncapsulate {
  182. if tunlIface, err = iproute.NewIPIP(privIface); err != nil {
  183. return nil, fmt.Errorf("failed to create tunnel interface: %v", err)
  184. }
  185. if err := iproute.Set(tunlIface, true); err != nil {
  186. return nil, fmt.Errorf("failed to set tunnel interface up: %v", err)
  187. }
  188. }
  189. level.Debug(logger).Log("msg", fmt.Sprintf("using %s as the private IP address", privateIP.String()))
  190. level.Debug(logger).Log("msg", fmt.Sprintf("using %s as the public IP address", publicIP.String()))
  191. ipTables, err := iptables.New(len(subnet.IP))
  192. if err != nil {
  193. return nil, fmt.Errorf("failed to IP tables controller: %v", err)
  194. }
  195. return &Mesh{
  196. Backend: backend,
  197. encapsulate: encapsulate,
  198. externalIP: publicIP,
  199. granularity: granularity,
  200. hostname: hostname,
  201. internalIP: privateIP,
  202. // This is a patch until Calico supports
  203. // other hosts adding IPIP iptables rules.
  204. ipset: ipset.New("cali40all-hosts-net"),
  205. ipTables: ipTables,
  206. kiloIface: kiloIface,
  207. nodes: make(map[string]*Node),
  208. port: port,
  209. priv: private,
  210. privIface: privIface,
  211. pub: public,
  212. pubIface: pubIface,
  213. local: local,
  214. stop: make(chan struct{}),
  215. subnet: subnet,
  216. table: route.NewTable(),
  217. tunlIface: tunlIface,
  218. errorCounter: prometheus.NewCounterVec(prometheus.CounterOpts{
  219. Name: "kilo_errors_total",
  220. Help: "Number of errors that occurred while administering the mesh.",
  221. }, []string{"event"}),
  222. nodesGuage: prometheus.NewGauge(prometheus.GaugeOpts{
  223. Name: "kilo_nodes",
  224. Help: "Number of in the mesh.",
  225. }),
  226. reconcileCounter: prometheus.NewCounter(prometheus.CounterOpts{
  227. Name: "kilo_reconciles_total",
  228. Help: "Number of reconciliation attempts.",
  229. }),
  230. logger: logger,
  231. }, nil
  232. }
  233. // Run starts the mesh.
  234. func (m *Mesh) Run() error {
  235. if err := m.Init(m.stop); err != nil {
  236. return fmt.Errorf("failed to initialize backend: %v", err)
  237. }
  238. ipsetErrors, err := m.ipset.Run(m.stop)
  239. if err != nil {
  240. return fmt.Errorf("failed to watch for ipset updates: %v", err)
  241. }
  242. ipTablesErrors, err := m.ipTables.Run(m.stop)
  243. if err != nil {
  244. return fmt.Errorf("failed to watch for IP tables updates: %v", err)
  245. }
  246. routeErrors, err := m.table.Run(m.stop)
  247. if err != nil {
  248. return fmt.Errorf("failed to watch for route table updates: %v", err)
  249. }
  250. go func() {
  251. for {
  252. var err error
  253. select {
  254. case err = <-ipsetErrors:
  255. case err = <-ipTablesErrors:
  256. case err = <-routeErrors:
  257. case <-m.stop:
  258. return
  259. }
  260. if err != nil {
  261. level.Error(m.logger).Log("error", err)
  262. m.errorCounter.WithLabelValues("run").Inc()
  263. }
  264. }
  265. }()
  266. defer m.cleanUp()
  267. t := time.NewTimer(resyncPeriod)
  268. w := m.Watch()
  269. for {
  270. var e *Event
  271. select {
  272. case e = <-w:
  273. m.sync(e)
  274. case <-t.C:
  275. m.checkIn()
  276. m.applyTopology()
  277. t.Reset(resyncPeriod)
  278. case <-m.stop:
  279. return nil
  280. }
  281. }
  282. }
  283. func (m *Mesh) sync(e *Event) {
  284. logger := log.With(m.logger, "event", e.Type)
  285. level.Debug(logger).Log("msg", "syncing", "event", e.Type)
  286. if isSelf(m.hostname, e.Node) {
  287. level.Debug(logger).Log("msg", "processing local node", "node", e.Node)
  288. m.handleLocal(e.Node)
  289. return
  290. }
  291. var diff bool
  292. m.mu.Lock()
  293. if !e.Node.Ready() {
  294. level.Debug(logger).Log("msg", "received incomplete node", "node", e.Node)
  295. // An existing node is no longer valid
  296. // so remove it from the mesh.
  297. if _, ok := m.nodes[e.Node.Name]; ok {
  298. level.Info(logger).Log("msg", "node is no longer in the mesh", "node", e.Node)
  299. delete(m.nodes, e.Node.Name)
  300. diff = true
  301. }
  302. } else {
  303. switch e.Type {
  304. case AddEvent:
  305. fallthrough
  306. case UpdateEvent:
  307. if !nodesAreEqual(m.nodes[e.Node.Name], e.Node) {
  308. m.nodes[e.Node.Name] = e.Node
  309. diff = true
  310. }
  311. case DeleteEvent:
  312. delete(m.nodes, e.Node.Name)
  313. diff = true
  314. }
  315. }
  316. m.mu.Unlock()
  317. if diff {
  318. level.Info(logger).Log("node", e.Node)
  319. m.applyTopology()
  320. }
  321. }
  322. // checkIn will try to update the local node's LastSeen timestamp
  323. // in the backend.
  324. func (m *Mesh) checkIn() {
  325. m.mu.Lock()
  326. n := m.nodes[m.hostname]
  327. m.mu.Unlock()
  328. if n == nil {
  329. level.Debug(m.logger).Log("msg", "no local node found in backend")
  330. return
  331. }
  332. n.LastSeen = time.Now().Unix()
  333. if err := m.Set(m.hostname, n); err != nil {
  334. level.Error(m.logger).Log("error", fmt.Sprintf("failed to set local node: %v", err), "node", n)
  335. m.errorCounter.WithLabelValues("checkin").Inc()
  336. return
  337. }
  338. level.Debug(m.logger).Log("msg", "successfully checked in local node in backend")
  339. }
  340. func (m *Mesh) handleLocal(n *Node) {
  341. // Allow the external IP to be overridden.
  342. if n.ExternalIP == nil {
  343. n.ExternalIP = m.externalIP
  344. }
  345. // Compare the given node to the calculated local node.
  346. // Take leader, location, and subnet from the argument, as these
  347. // are not determined by kilo.
  348. local := &Node{
  349. ExternalIP: n.ExternalIP,
  350. Key: m.pub,
  351. InternalIP: m.internalIP,
  352. LastSeen: time.Now().Unix(),
  353. Leader: n.Leader,
  354. Location: n.Location,
  355. Name: m.hostname,
  356. Subnet: n.Subnet,
  357. }
  358. if !nodesAreEqual(n, local) {
  359. level.Debug(m.logger).Log("msg", "local node differs from backend")
  360. if err := m.Set(m.hostname, local); err != nil {
  361. level.Error(m.logger).Log("error", fmt.Sprintf("failed to set local node: %v", err), "node", local)
  362. m.errorCounter.WithLabelValues("local").Inc()
  363. return
  364. }
  365. level.Debug(m.logger).Log("msg", "successfully reconciled local node against backend")
  366. }
  367. m.mu.Lock()
  368. n = m.nodes[m.hostname]
  369. if n == nil {
  370. n = &Node{}
  371. }
  372. m.mu.Unlock()
  373. if !nodesAreEqual(n, local) {
  374. m.mu.Lock()
  375. m.nodes[local.Name] = local
  376. m.mu.Unlock()
  377. m.applyTopology()
  378. }
  379. }
  380. func (m *Mesh) applyTopology() {
  381. m.reconcileCounter.Inc()
  382. m.mu.Lock()
  383. defer m.mu.Unlock()
  384. // Ensure all unready nodes are removed.
  385. var ready float64
  386. for n := range m.nodes {
  387. if !m.nodes[n].Ready() {
  388. delete(m.nodes, n)
  389. continue
  390. }
  391. ready++
  392. }
  393. m.nodesGuage.Set(ready)
  394. // We cannot do anything with the topology until the local node is available.
  395. if m.nodes[m.hostname] == nil {
  396. return
  397. }
  398. t, err := NewTopology(m.nodes, m.granularity, m.hostname, m.port, m.priv, m.subnet)
  399. if err != nil {
  400. level.Error(m.logger).Log("error", err)
  401. m.errorCounter.WithLabelValues("apply").Inc()
  402. return
  403. }
  404. conf, err := t.Conf()
  405. if err != nil {
  406. level.Error(m.logger).Log("error", err)
  407. m.errorCounter.WithLabelValues("apply").Inc()
  408. }
  409. if err := ioutil.WriteFile(ConfPath, conf, 0600); err != nil {
  410. level.Error(m.logger).Log("error", err)
  411. m.errorCounter.WithLabelValues("apply").Inc()
  412. return
  413. }
  414. var private *net.IPNet
  415. // If we are not encapsulating packets to the local private network,
  416. // then pass the private IP to add an exception to the NAT rule.
  417. if m.encapsulate != AlwaysEncapsulate {
  418. private = t.privateIP
  419. }
  420. rules := iptables.MasqueradeRules(private, m.nodes[m.hostname].Subnet, t.RemoteSubnets())
  421. rules = append(rules, iptables.ForwardRules(m.subnet)...)
  422. if err := m.ipTables.Set(rules); err != nil {
  423. level.Error(m.logger).Log("error", err)
  424. m.errorCounter.WithLabelValues("apply").Inc()
  425. return
  426. }
  427. if m.encapsulate != NeverEncapsulate {
  428. var peers []net.IP
  429. for _, s := range t.Segments {
  430. if s.Location == m.nodes[m.hostname].Location {
  431. peers = s.privateIPs
  432. break
  433. }
  434. }
  435. if err := m.ipset.Set(peers); err != nil {
  436. level.Error(m.logger).Log("error", err)
  437. m.errorCounter.WithLabelValues("apply").Inc()
  438. return
  439. }
  440. if m.local {
  441. if err := iproute.SetAddress(m.tunlIface, oneAddressCIDR(newAllocator(*m.nodes[m.hostname].Subnet).next().IP)); err != nil {
  442. level.Error(m.logger).Log("error", err)
  443. m.errorCounter.WithLabelValues("apply").Inc()
  444. return
  445. }
  446. }
  447. }
  448. if t.leader {
  449. if err := iproute.SetAddress(m.kiloIface, t.wireGuardCIDR); err != nil {
  450. level.Error(m.logger).Log("error", err)
  451. m.errorCounter.WithLabelValues("apply").Inc()
  452. return
  453. }
  454. link, err := linkByIndex(m.kiloIface)
  455. if err != nil {
  456. level.Error(m.logger).Log("error", err)
  457. m.errorCounter.WithLabelValues("apply").Inc()
  458. return
  459. }
  460. oldConf, err := wireguard.ShowConf(link.Attrs().Name)
  461. if err != nil {
  462. level.Error(m.logger).Log("error", err)
  463. m.errorCounter.WithLabelValues("apply").Inc()
  464. return
  465. }
  466. // Setting the WireGuard configuration interrupts existing connections
  467. // so only set the configuration if it has changed.
  468. equal, err := wireguard.CompareConf(conf, oldConf)
  469. if err != nil {
  470. level.Error(m.logger).Log("error", err)
  471. m.errorCounter.WithLabelValues("apply").Inc()
  472. // Don't return here, simply overwrite the old configuration.
  473. equal = false
  474. }
  475. if !equal {
  476. if err := wireguard.SetConf(link.Attrs().Name, ConfPath); err != nil {
  477. level.Error(m.logger).Log("error", err)
  478. m.errorCounter.WithLabelValues("apply").Inc()
  479. return
  480. }
  481. }
  482. if err := iproute.Set(m.kiloIface, true); err != nil {
  483. level.Error(m.logger).Log("error", err)
  484. m.errorCounter.WithLabelValues("apply").Inc()
  485. return
  486. }
  487. } else {
  488. level.Debug(m.logger).Log("msg", "local node is not the leader")
  489. if err := iproute.Set(m.kiloIface, false); err != nil {
  490. level.Error(m.logger).Log("error", err)
  491. m.errorCounter.WithLabelValues("apply").Inc()
  492. return
  493. }
  494. }
  495. // We need to add routes last since they may depend
  496. // on the WireGuard interface.
  497. routes := t.Routes(m.kiloIface, m.privIface, m.tunlIface, m.local, m.encapsulate)
  498. if err := m.table.Set(routes); err != nil {
  499. level.Error(m.logger).Log("error", err)
  500. m.errorCounter.WithLabelValues("apply").Inc()
  501. }
  502. }
  503. // RegisterMetrics registers Prometheus metrics on the given Prometheus
  504. // registerer.
  505. func (m *Mesh) RegisterMetrics(r prometheus.Registerer) {
  506. r.MustRegister(
  507. m.errorCounter,
  508. m.nodesGuage,
  509. m.reconcileCounter,
  510. )
  511. }
  512. // Stop stops the mesh.
  513. func (m *Mesh) Stop() {
  514. close(m.stop)
  515. }
  516. func (m *Mesh) cleanUp() {
  517. if err := m.ipTables.CleanUp(); err != nil {
  518. level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up IP tables: %v", err))
  519. m.errorCounter.WithLabelValues("cleanUp").Inc()
  520. }
  521. if err := m.table.CleanUp(); err != nil {
  522. level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up routes: %v", err))
  523. m.errorCounter.WithLabelValues("cleanUp").Inc()
  524. }
  525. if err := os.Remove(PrivateKeyPath); err != nil {
  526. level.Error(m.logger).Log("error", fmt.Sprintf("failed to delete private key: %v", err))
  527. m.errorCounter.WithLabelValues("cleanUp").Inc()
  528. }
  529. if err := os.Remove(ConfPath); err != nil {
  530. level.Error(m.logger).Log("error", fmt.Sprintf("failed to delete configuration file: %v", err))
  531. m.errorCounter.WithLabelValues("cleanUp").Inc()
  532. }
  533. if err := iproute.RemoveInterface(m.kiloIface); err != nil {
  534. level.Error(m.logger).Log("error", fmt.Sprintf("failed to remove wireguard interface: %v", err))
  535. m.errorCounter.WithLabelValues("cleanUp").Inc()
  536. }
  537. if err := m.CleanUp(m.hostname); err != nil {
  538. level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up backend: %v", err))
  539. m.errorCounter.WithLabelValues("cleanUp").Inc()
  540. }
  541. if err := m.ipset.CleanUp(); err != nil {
  542. level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up ipset: %v", err))
  543. m.errorCounter.WithLabelValues("cleanUp").Inc()
  544. }
  545. }
  546. func isSelf(hostname string, node *Node) bool {
  547. return node != nil && node.Name == hostname
  548. }
  549. func nodesAreEqual(a, b *Node) bool {
  550. if !(a != nil) == (b != nil) {
  551. return false
  552. }
  553. if a == b {
  554. return true
  555. }
  556. // Ignore LastSeen when comparing equality.
  557. return ipNetsEqual(a.ExternalIP, b.ExternalIP) && string(a.Key) == string(b.Key) && ipNetsEqual(a.InternalIP, b.InternalIP) && a.Leader == b.Leader && a.Location == b.Location && a.Name == b.Name && subnetsEqual(a.Subnet, b.Subnet)
  558. }
  559. func ipNetsEqual(a, b *net.IPNet) bool {
  560. if a == nil && b == nil {
  561. return true
  562. }
  563. if (a != nil) != (b != nil) {
  564. return false
  565. }
  566. if a.Mask.String() != b.Mask.String() {
  567. return false
  568. }
  569. return a.IP.Equal(b.IP)
  570. }
  571. func subnetsEqual(a, b *net.IPNet) bool {
  572. if a.Mask.String() != b.Mask.String() {
  573. return false
  574. }
  575. if !a.Contains(b.IP) {
  576. return false
  577. }
  578. if !b.Contains(a.IP) {
  579. return false
  580. }
  581. return true
  582. }
  583. func linkByIndex(index int) (netlink.Link, error) {
  584. link, err := netlink.LinkByIndex(index)
  585. if err != nil {
  586. return nil, fmt.Errorf("failed to get interface: %v", err)
  587. }
  588. return link, nil
  589. }