routes.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // Add routes to the private IPs of nodes in other segments.
  94. // Number of CIDRs and private IPs always match so
  95. // we can reuse the loop.
  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 local {
  123. for i := range segment.cidrs {
  124. // Don't add routes for the local node.
  125. if segment.privateIPs[i].Equal(t.privateIP.IP) {
  126. continue
  127. }
  128. routes = append(routes, encapsulateRoute(&netlink.Route{
  129. Dst: segment.cidrs[i],
  130. Flags: int(netlink.FLAG_ONLINK),
  131. Gw: segment.privateIPs[i],
  132. LinkIndex: privIface,
  133. Protocol: unix.RTPROT_STATIC,
  134. }, enc.Strategy(), t.privateIP, tunlIface))
  135. // Encapsulate packets from the host's Pod subnet headed
  136. // to private IPs.
  137. if enc.Strategy() == encapsulation.Always || (enc.Strategy() == encapsulation.CrossSubnet && !t.privateIP.Contains(segment.privateIPs[i])) {
  138. routes = append(routes, &netlink.Route{
  139. Dst: oneAddressCIDR(segment.privateIPs[i]),
  140. Flags: int(netlink.FLAG_ONLINK),
  141. Gw: segment.privateIPs[i],
  142. LinkIndex: tunlIface,
  143. Protocol: unix.RTPROT_STATIC,
  144. Table: kiloTableIndex,
  145. })
  146. rules = append(rules, defaultRule(&netlink.Rule{
  147. Src: t.subnet,
  148. Dst: oneAddressCIDR(segment.privateIPs[i]),
  149. Table: kiloTableIndex,
  150. }))
  151. // Also encapsulate packets from the Kilo interface
  152. // headed to private IPs.
  153. rules = append(rules, defaultRule(&netlink.Rule{
  154. Dst: oneAddressCIDR(segment.privateIPs[i]),
  155. Table: kiloTableIndex,
  156. IifName: kiloIfaceName,
  157. }))
  158. }
  159. }
  160. }
  161. continue
  162. }
  163. for i := range segment.cidrs {
  164. // Add routes to the Pod CIDRs of nodes in other segments.
  165. routes = append(routes, &netlink.Route{
  166. Dst: segment.cidrs[i],
  167. Flags: int(netlink.FLAG_ONLINK),
  168. Gw: segment.wireGuardIP,
  169. LinkIndex: kiloIface,
  170. Protocol: unix.RTPROT_STATIC,
  171. })
  172. // Don't add routes through Kilo if the private IP
  173. // equals the external IP. This means that the node
  174. // is only accessible through an external IP and we
  175. // cannot encapsulate traffic to an IP through the IP.
  176. if segment.privateIPs[i].Equal(segment.endpoint.IP) {
  177. continue
  178. }
  179. // Add routes to the private IPs of nodes in other segments.
  180. // Number of CIDRs and private IPs always match so
  181. // we can reuse the loop.
  182. routes = append(routes, &netlink.Route{
  183. Dst: oneAddressCIDR(segment.privateIPs[i]),
  184. Flags: int(netlink.FLAG_ONLINK),
  185. Gw: segment.wireGuardIP,
  186. LinkIndex: kiloIface,
  187. Protocol: unix.RTPROT_STATIC,
  188. })
  189. }
  190. }
  191. // Add routes for the allowed IPs of peers.
  192. for _, peer := range t.peers {
  193. for i := range peer.AllowedIPs {
  194. routes = append(routes, &netlink.Route{
  195. Dst: peer.AllowedIPs[i],
  196. LinkIndex: kiloIface,
  197. Protocol: unix.RTPROT_STATIC,
  198. })
  199. }
  200. }
  201. return routes, rules
  202. }
  203. func encapsulateRoute(route *netlink.Route, encapsulate encapsulation.Strategy, subnet *net.IPNet, tunlIface int) *netlink.Route {
  204. if encapsulate == encapsulation.Always || (encapsulate == encapsulation.CrossSubnet && !subnet.Contains(route.Gw)) {
  205. route.LinkIndex = tunlIface
  206. }
  207. return route
  208. }
  209. // Rules returns the iptables rules required by the local node.
  210. func (t *Topology) Rules(cni bool) []iptables.Rule {
  211. var rules []iptables.Rule
  212. rules = append(rules, iptables.NewIPv4Chain("nat", "KILO-NAT"))
  213. rules = append(rules, iptables.NewIPv6Chain("nat", "KILO-NAT"))
  214. if cni {
  215. rules = append(rules, iptables.NewRule(iptables.GetProtocol(len(t.subnet.IP)), "nat", "POSTROUTING", "-m", "comment", "--comment", "Kilo: jump to NAT chain", "-s", t.subnet.String(), "-j", "KILO-NAT"))
  216. }
  217. for _, s := range t.segments {
  218. rules = append(rules, iptables.NewRule(iptables.GetProtocol(len(s.wireGuardIP)), "nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: do not NAT packets destined for WireGuared IPs", "-d", s.wireGuardIP.String(), "-j", "RETURN"))
  219. for _, aip := range s.allowedIPs {
  220. rules = append(rules, iptables.NewRule(iptables.GetProtocol(len(aip.IP)), "nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: do not NAT packets destined for known IPs", "-d", aip.String(), "-j", "RETURN"))
  221. }
  222. }
  223. for _, p := range t.peers {
  224. for _, aip := range p.AllowedIPs {
  225. rules = append(rules,
  226. iptables.NewRule(iptables.GetProtocol(len(aip.IP)), "nat", "POSTROUTING", "-m", "comment", "--comment", "Kilo: jump to NAT chain", "-s", aip.String(), "-j", "KILO-NAT"),
  227. iptables.NewRule(iptables.GetProtocol(len(aip.IP)), "nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: do not NAT packets destined for peers", "-d", aip.String(), "-j", "RETURN"),
  228. )
  229. }
  230. }
  231. rules = append(rules, iptables.NewIPv4Rule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"))
  232. rules = append(rules, iptables.NewIPv6Rule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"))
  233. return rules
  234. }
  235. func defaultRule(rule *netlink.Rule) *netlink.Rule {
  236. base := netlink.NewRule()
  237. base.Src = rule.Src
  238. base.Dst = rule.Dst
  239. base.IifName = rule.IifName
  240. base.Table = rule.Table
  241. return base
  242. }