routes.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. // +build linux
  15. package mesh
  16. import (
  17. "net"
  18. "github.com/vishvananda/netlink"
  19. "golang.org/x/sys/unix"
  20. "github.com/squat/kilo/pkg/encapsulation"
  21. "github.com/squat/kilo/pkg/iptables"
  22. )
  23. const kiloTableIndex = 1107
  24. // Routes generates a slice of routes for a given Topology.
  25. func (t *Topology) Routes(kiloIfaceName string, kiloIface, privIface, tunlIface int, local bool, enc encapsulation.Encapsulator) ([]*netlink.Route, []*netlink.Rule) {
  26. var routes []*netlink.Route
  27. var rules []*netlink.Rule
  28. if !t.leader {
  29. // Find the GW for this segment.
  30. // This will be the an IP of the leader.
  31. // In an IPIP encapsulated mesh it is the leader's private IP.
  32. var gw net.IP
  33. for _, segment := range t.segments {
  34. if segment.location == t.location {
  35. gw = enc.Gw(segment.endpoint.IP, segment.privateIPs[segment.leader], segment.cidrs[segment.leader])
  36. break
  37. }
  38. }
  39. for _, segment := range t.segments {
  40. // First, add a route to the WireGuard IP of the segment.
  41. routes = append(routes, encapsulateRoute(&netlink.Route{
  42. Dst: oneAddressCIDR(segment.wireGuardIP),
  43. Flags: int(netlink.FLAG_ONLINK),
  44. Gw: gw,
  45. LinkIndex: privIface,
  46. Protocol: unix.RTPROT_STATIC,
  47. }, enc.Strategy(), t.privateIP, tunlIface))
  48. // Add routes for the current segment if local is true.
  49. if segment.location == t.location {
  50. if local {
  51. for i := range segment.cidrs {
  52. // Don't add routes for the local node.
  53. if segment.privateIPs[i].Equal(t.privateIP.IP) {
  54. continue
  55. }
  56. routes = append(routes, encapsulateRoute(&netlink.Route{
  57. Dst: segment.cidrs[i],
  58. Flags: int(netlink.FLAG_ONLINK),
  59. Gw: segment.privateIPs[i],
  60. LinkIndex: privIface,
  61. Protocol: unix.RTPROT_STATIC,
  62. }, enc.Strategy(), t.privateIP, tunlIface))
  63. // Encapsulate packets from the host's Pod subnet headed
  64. // to private IPs.
  65. if enc.Strategy() == encapsulation.Always || (enc.Strategy() == encapsulation.CrossSubnet && !t.privateIP.Contains(segment.privateIPs[i])) {
  66. routes = append(routes, &netlink.Route{
  67. Dst: oneAddressCIDR(segment.privateIPs[i]),
  68. Flags: int(netlink.FLAG_ONLINK),
  69. Gw: segment.privateIPs[i],
  70. LinkIndex: tunlIface,
  71. Protocol: unix.RTPROT_STATIC,
  72. Table: kiloTableIndex,
  73. })
  74. rules = append(rules, defaultRule(&netlink.Rule{
  75. Src: t.subnet,
  76. Dst: oneAddressCIDR(segment.privateIPs[i]),
  77. Table: kiloTableIndex,
  78. }))
  79. }
  80. }
  81. }
  82. continue
  83. }
  84. for i := range segment.cidrs {
  85. // Add routes to the Pod CIDRs of nodes in other segments.
  86. routes = append(routes, encapsulateRoute(&netlink.Route{
  87. Dst: segment.cidrs[i],
  88. Flags: int(netlink.FLAG_ONLINK),
  89. Gw: gw,
  90. LinkIndex: privIface,
  91. Protocol: unix.RTPROT_STATIC,
  92. }, enc.Strategy(), t.privateIP, tunlIface))
  93. }
  94. for i := range segment.privateIPs {
  95. // Add routes to the private IPs of nodes in other segments.
  96. routes = append(routes, encapsulateRoute(&netlink.Route{
  97. Dst: oneAddressCIDR(segment.privateIPs[i]),
  98. Flags: int(netlink.FLAG_ONLINK),
  99. Gw: gw,
  100. LinkIndex: privIface,
  101. Protocol: unix.RTPROT_STATIC,
  102. }, enc.Strategy(), t.privateIP, tunlIface))
  103. }
  104. }
  105. // Add routes for the allowed IPs of peers.
  106. for _, peer := range t.peers {
  107. for i := range peer.AllowedIPs {
  108. routes = append(routes, encapsulateRoute(&netlink.Route{
  109. Dst: peer.AllowedIPs[i],
  110. Flags: int(netlink.FLAG_ONLINK),
  111. Gw: gw,
  112. LinkIndex: privIface,
  113. Protocol: unix.RTPROT_STATIC,
  114. }, enc.Strategy(), t.privateIP, tunlIface))
  115. }
  116. }
  117. return routes, rules
  118. }
  119. for _, segment := range t.segments {
  120. // Add routes for the current segment if local is true.
  121. if segment.location == t.location {
  122. // If the local node does not have a private IP address,
  123. // then skip adding routes, because the node is in its own location.
  124. if local && t.privateIP != nil {
  125. for i := range segment.cidrs {
  126. // Don't add routes for the local node.
  127. if segment.privateIPs[i].Equal(t.privateIP.IP) {
  128. continue
  129. }
  130. routes = append(routes, encapsulateRoute(&netlink.Route{
  131. Dst: segment.cidrs[i],
  132. Flags: int(netlink.FLAG_ONLINK),
  133. Gw: segment.privateIPs[i],
  134. LinkIndex: privIface,
  135. Protocol: unix.RTPROT_STATIC,
  136. }, enc.Strategy(), t.privateIP, tunlIface))
  137. // Encapsulate packets from the host's Pod subnet headed
  138. // to private IPs.
  139. if enc.Strategy() == encapsulation.Always || (enc.Strategy() == encapsulation.CrossSubnet && !t.privateIP.Contains(segment.privateIPs[i])) {
  140. routes = append(routes, &netlink.Route{
  141. Dst: oneAddressCIDR(segment.privateIPs[i]),
  142. Flags: int(netlink.FLAG_ONLINK),
  143. Gw: segment.privateIPs[i],
  144. LinkIndex: tunlIface,
  145. Protocol: unix.RTPROT_STATIC,
  146. Table: kiloTableIndex,
  147. })
  148. rules = append(rules, defaultRule(&netlink.Rule{
  149. Src: t.subnet,
  150. Dst: oneAddressCIDR(segment.privateIPs[i]),
  151. Table: kiloTableIndex,
  152. }))
  153. // Also encapsulate packets from the Kilo interface
  154. // headed to private IPs.
  155. rules = append(rules, defaultRule(&netlink.Rule{
  156. Dst: oneAddressCIDR(segment.privateIPs[i]),
  157. Table: kiloTableIndex,
  158. IifName: kiloIfaceName,
  159. }))
  160. }
  161. }
  162. }
  163. // Continuing here prevents leaders form adding routes via WireGuard to
  164. // nodes in their own location.
  165. continue
  166. }
  167. for i := range segment.cidrs {
  168. // Add routes to the Pod CIDRs of nodes in other segments.
  169. routes = append(routes, &netlink.Route{
  170. Dst: segment.cidrs[i],
  171. Flags: int(netlink.FLAG_ONLINK),
  172. Gw: segment.wireGuardIP,
  173. LinkIndex: kiloIface,
  174. Protocol: unix.RTPROT_STATIC,
  175. })
  176. // Don't add routes through Kilo if the private IP
  177. // equals the external IP. This means that the node
  178. // is only accessible through an external IP and we
  179. // cannot encapsulate traffic to an IP through the IP.
  180. if segment.privateIPs == nil || segment.privateIPs[i].Equal(segment.endpoint.IP) {
  181. continue
  182. }
  183. // Add routes to the private IPs of nodes in other segments.
  184. // Number of CIDRs and private IPs always match so
  185. // we can reuse the loop.
  186. routes = append(routes, &netlink.Route{
  187. Dst: oneAddressCIDR(segment.privateIPs[i]),
  188. Flags: int(netlink.FLAG_ONLINK),
  189. Gw: segment.wireGuardIP,
  190. LinkIndex: kiloIface,
  191. Protocol: unix.RTPROT_STATIC,
  192. })
  193. }
  194. }
  195. // Add routes for the allowed IPs of peers.
  196. for _, peer := range t.peers {
  197. for i := range peer.AllowedIPs {
  198. routes = append(routes, &netlink.Route{
  199. Dst: peer.AllowedIPs[i],
  200. LinkIndex: kiloIface,
  201. Protocol: unix.RTPROT_STATIC,
  202. })
  203. }
  204. }
  205. return routes, rules
  206. }
  207. func encapsulateRoute(route *netlink.Route, encapsulate encapsulation.Strategy, subnet *net.IPNet, tunlIface int) *netlink.Route {
  208. if encapsulate == encapsulation.Always || (encapsulate == encapsulation.CrossSubnet && !subnet.Contains(route.Gw)) {
  209. route.LinkIndex = tunlIface
  210. }
  211. return route
  212. }
  213. // Rules returns the iptables rules required by the local node.
  214. func (t *Topology) Rules(cni bool) []iptables.Rule {
  215. var rules []iptables.Rule
  216. rules = append(rules, iptables.NewIPv4Chain("nat", "KILO-NAT"))
  217. rules = append(rules, iptables.NewIPv6Chain("nat", "KILO-NAT"))
  218. if cni {
  219. rules = append(rules, iptables.NewRule(iptables.GetProtocol(len(t.subnet.IP)), "nat", "POSTROUTING", "-s", t.subnet.String(), "-m", "comment", "--comment", "Kilo: jump to KILO-NAT chain", "-j", "KILO-NAT"))
  220. }
  221. for _, s := range t.segments {
  222. rules = append(rules, iptables.NewRule(iptables.GetProtocol(len(s.wireGuardIP)), "nat", "KILO-NAT", "-d", oneAddressCIDR(s.wireGuardIP).String(), "-m", "comment", "--comment", "Kilo: do not NAT packets destined for WireGuared IPs", "-j", "RETURN"))
  223. for _, aip := range s.allowedIPs {
  224. rules = append(rules, iptables.NewRule(iptables.GetProtocol(len(aip.IP)), "nat", "KILO-NAT", "-d", aip.String(), "-m", "comment", "--comment", "Kilo: do not NAT packets destined for known IPs", "-j", "RETURN"))
  225. }
  226. }
  227. for _, p := range t.peers {
  228. for _, aip := range p.AllowedIPs {
  229. rules = append(rules,
  230. iptables.NewRule(iptables.GetProtocol(len(aip.IP)), "nat", "POSTROUTING", "-s", aip.String(), "-m", "comment", "--comment", "Kilo: jump to NAT chain", "-j", "KILO-NAT"),
  231. iptables.NewRule(iptables.GetProtocol(len(aip.IP)), "nat", "KILO-NAT", "-d", aip.String(), "-m", "comment", "--comment", "Kilo: do not NAT packets destined for peers", "-j", "RETURN"),
  232. )
  233. }
  234. }
  235. rules = append(rules, iptables.NewIPv4Rule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"))
  236. rules = append(rules, iptables.NewIPv6Rule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"))
  237. return rules
  238. }
  239. func defaultRule(rule *netlink.Rule) *netlink.Rule {
  240. base := netlink.NewRule()
  241. base.Src = rule.Src
  242. base.Dst = rule.Dst
  243. base.IifName = rule.IifName
  244. base.Table = rule.Table
  245. return base
  246. }