cilium.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 encapsulation
  15. import (
  16. "fmt"
  17. "net"
  18. "github.com/vishvananda/netlink"
  19. "github.com/squat/kilo/pkg/iproute"
  20. "github.com/squat/kilo/pkg/iptables"
  21. )
  22. const (
  23. ciliumHostIface = "cilium_host"
  24. // ciliumTunlIface is the kernel's default IPIP tunnel (tunl0) renamed
  25. // by Cilium when enable-ipip-termination is enabled.
  26. ciliumTunlIface = "cilium_tunl"
  27. )
  28. type cilium struct {
  29. iface int
  30. strategy Strategy
  31. ownsTunnel bool
  32. }
  33. // NewCilium returns an encapsulator that uses IPIP tunnels
  34. // routed through Cilium's VxLAN overlay.
  35. func NewCilium(strategy Strategy) Encapsulator {
  36. return &cilium{strategy: strategy}
  37. }
  38. // CleanUp will remove any created IPIP devices.
  39. // If the tunnel is owned by Cilium, skip removal.
  40. func (c *cilium) CleanUp() error {
  41. if !c.ownsTunnel {
  42. return nil
  43. }
  44. if err := iproute.DeleteAddresses(c.iface); err != nil {
  45. return err
  46. }
  47. return iproute.RemoveInterface(c.iface)
  48. }
  49. // Gw returns the correct gateway IP associated with the given node.
  50. // It returns the Cilium internal IP so that the IPIP outer packets are routed
  51. // through Cilium's VxLAN overlay rather than the host network.
  52. func (c *cilium) Gw(_, _, cniIP net.IP, subnet *net.IPNet) net.IP {
  53. if cniIP != nil {
  54. return cniIP
  55. }
  56. return subnet.IP
  57. }
  58. // CNICompatibilityIP returns the IP address of the cilium_host interface.
  59. // This IP is advertised to other nodes so they can route IPIP outer
  60. // packets through Cilium's overlay.
  61. func (c *cilium) CNICompatibilityIP() *net.IPNet {
  62. iface, err := net.InterfaceByName(ciliumHostIface)
  63. if err != nil {
  64. // cilium_host does not exist; Cilium may not be running.
  65. return nil
  66. }
  67. addrs, err := iface.Addrs()
  68. if err != nil {
  69. // Unable to list addresses; safe to skip since the
  70. // CNI compatibility IP is only used for optimization.
  71. return nil
  72. }
  73. for _, a := range addrs {
  74. // IPIP tunnels use IPv4 encapsulation, so only IPv4
  75. // addresses are usable as the outer header source/destination.
  76. if ipNet, ok := a.(*net.IPNet); ok && ipNet.IP.To4() != nil {
  77. return ipNet
  78. }
  79. }
  80. return nil
  81. }
  82. // Index returns the index of the IPIP tunnel interface.
  83. func (c *cilium) Index() int {
  84. return c.iface
  85. }
  86. // Init initializes the IPIP tunnel interface.
  87. // If Cilium is running with enable-ipip-termination, it renames the kernel's
  88. // tunl0 to cilium_tunl. In that case we reuse the existing cilium_tunl.
  89. // Otherwise we create the standard tunl0 ourselves.
  90. func (c *cilium) Init(base int) error {
  91. // If Cilium created cilium_tunl (enable-ipip-termination), reuse it.
  92. if link, err := netlink.LinkByName(ciliumTunlIface); err == nil {
  93. c.iface = link.Attrs().Index
  94. c.ownsTunnel = false
  95. // Ensure the interface is UP — Cilium may leave it DOWN.
  96. if link.Attrs().Flags&net.FlagUp == 0 {
  97. if err := iproute.Set(c.iface, true); err != nil {
  98. return fmt.Errorf("failed to set %s up: %v", ciliumTunlIface, err)
  99. }
  100. }
  101. return nil
  102. }
  103. // No cilium_tunl — create standard tunl0.
  104. iface, err := iproute.NewIPIP(base)
  105. if err != nil {
  106. return fmt.Errorf("failed to create tunnel interface: %v", err)
  107. }
  108. if err := iproute.Set(iface, true); err != nil {
  109. return fmt.Errorf("failed to set tunnel interface up: %v", err)
  110. }
  111. c.iface = iface
  112. c.ownsTunnel = true
  113. return nil
  114. }
  115. // Rules returns a set of iptables rules that are necessary
  116. // when traffic between nodes must be encapsulated.
  117. func (c *cilium) Rules(nodes []*net.IPNet) iptables.RuleSet {
  118. rules := iptables.RuleSet{}
  119. proto := ipipProtocolName()
  120. rules.AddToAppend(iptables.NewIPv4Chain("filter", "KILO-IPIP"))
  121. rules.AddToAppend(iptables.NewIPv6Chain("filter", "KILO-IPIP"))
  122. rules.AddToAppend(iptables.NewIPv4Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: jump to IPIP chain", "-j", "KILO-IPIP"))
  123. rules.AddToAppend(iptables.NewIPv6Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: jump to IPIP chain", "-j", "KILO-IPIP"))
  124. for _, n := range nodes {
  125. // Accept encapsulated traffic from peers.
  126. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(n.IP), "filter", "KILO-IPIP", "-s", n.String(), "-m", "comment", "--comment", "Kilo: allow IPIP traffic", "-j", "ACCEPT"))
  127. }
  128. // Drop all other IPIP traffic.
  129. rules.AddToAppend(iptables.NewIPv4Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: reject other IPIP traffic", "-j", "DROP"))
  130. rules.AddToAppend(iptables.NewIPv6Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: reject other IPIP traffic", "-j", "DROP"))
  131. return rules
  132. }
  133. // Set sets the IP address of the IPIP tunnel interface.
  134. func (c *cilium) Set(cidr *net.IPNet) error {
  135. return iproute.SetAddress(c.iface, cidr)
  136. }
  137. // Strategy returns the configured strategy for encapsulation.
  138. func (c *cilium) Strategy() Strategy {
  139. return c.strategy
  140. }