routes.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. //go:build linux
  15. // +build linux
  16. package mesh
  17. import (
  18. "net"
  19. "github.com/vishvananda/netlink"
  20. "golang.org/x/sys/unix"
  21. "github.com/squat/kilo/pkg/encapsulation"
  22. "github.com/squat/kilo/pkg/iptables"
  23. )
  24. const kiloTableIndex = 1107
  25. // Routes generates a slice of routes for a given Topology.
  26. func (t *Topology) Routes(kiloIfaceName string, kiloIface, privIface, tunlIface int, local bool, enc encapsulation.Encapsulator) ([]*netlink.Route, []*netlink.Rule) {
  27. var routes []*netlink.Route
  28. var rules []*netlink.Rule
  29. if !t.leader {
  30. // Find the GW for this segment.
  31. // This will be the an IP of the leader.
  32. // In an IPIP encapsulated mesh it is the leader's private IP.
  33. var gw net.IP
  34. for _, segment := range t.segments {
  35. if segment.location == t.location {
  36. gw = enc.Gw(t.updateEndpoint(segment.endpoint, segment.key, &segment.persistentKeepalive).IP(), segment.privateIPs[segment.leader], segment.cidrs[segment.leader])
  37. break
  38. }
  39. }
  40. for _, segment := range t.segments {
  41. // First, add a route to the WireGuard IP of the segment.
  42. routes = append(routes, encapsulateRoute(&netlink.Route{
  43. Dst: oneAddressCIDR(segment.wireGuardIP),
  44. Flags: int(netlink.FLAG_ONLINK),
  45. Gw: gw,
  46. LinkIndex: privIface,
  47. Protocol: unix.RTPROT_STATIC,
  48. }, enc.Strategy(), t.privateIP, tunlIface))
  49. // Add routes for the current segment if local is true.
  50. if segment.location == t.location {
  51. if local {
  52. for i := range segment.cidrs {
  53. // Don't add routes for the local node.
  54. if segment.privateIPs[i].Equal(t.privateIP.IP) {
  55. continue
  56. }
  57. routes = append(routes, encapsulateRoute(&netlink.Route{
  58. Dst: segment.cidrs[i],
  59. Flags: int(netlink.FLAG_ONLINK),
  60. Gw: segment.privateIPs[i],
  61. LinkIndex: privIface,
  62. Protocol: unix.RTPROT_STATIC,
  63. }, enc.Strategy(), t.privateIP, tunlIface))
  64. // Encapsulate packets from the host's Pod subnet headed
  65. // to private IPs.
  66. if enc.Strategy() == encapsulation.Always || (enc.Strategy() == encapsulation.CrossSubnet && !t.privateIP.Contains(segment.privateIPs[i])) {
  67. routes = append(routes, &netlink.Route{
  68. Dst: oneAddressCIDR(segment.privateIPs[i]),
  69. Flags: int(netlink.FLAG_ONLINK),
  70. Gw: segment.privateIPs[i],
  71. LinkIndex: tunlIface,
  72. Protocol: unix.RTPROT_STATIC,
  73. Table: kiloTableIndex,
  74. })
  75. rules = append(rules, defaultRule(&netlink.Rule{
  76. Src: t.subnet,
  77. Dst: oneAddressCIDR(segment.privateIPs[i]),
  78. Table: kiloTableIndex,
  79. }))
  80. }
  81. }
  82. }
  83. continue
  84. }
  85. for i := range segment.cidrs {
  86. // Add routes to the Pod CIDRs of nodes in other segments.
  87. routes = append(routes, encapsulateRoute(&netlink.Route{
  88. Dst: segment.cidrs[i],
  89. Flags: int(netlink.FLAG_ONLINK),
  90. Gw: gw,
  91. LinkIndex: privIface,
  92. Protocol: unix.RTPROT_STATIC,
  93. }, enc.Strategy(), t.privateIP, tunlIface))
  94. }
  95. for i := range segment.privateIPs {
  96. // Add routes to the private IPs of nodes in other segments.
  97. routes = append(routes, encapsulateRoute(&netlink.Route{
  98. Dst: oneAddressCIDR(segment.privateIPs[i]),
  99. Flags: int(netlink.FLAG_ONLINK),
  100. Gw: gw,
  101. LinkIndex: privIface,
  102. Protocol: unix.RTPROT_STATIC,
  103. }, enc.Strategy(), t.privateIP, tunlIface))
  104. }
  105. // For segments / locations other than the location of this instance of kg,
  106. // we need to set routes for allowed location IPs over the leader in the current location.
  107. for i := range segment.allowedLocationIPs {
  108. routes = append(routes, encapsulateRoute(&netlink.Route{
  109. Dst: &segment.allowedLocationIPs[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. // Add routes for the allowed IPs of peers.
  118. for _, peer := range t.peers {
  119. for i := range peer.AllowedIPs {
  120. routes = append(routes, encapsulateRoute(&netlink.Route{
  121. Dst: &peer.AllowedIPs[i],
  122. Flags: int(netlink.FLAG_ONLINK),
  123. Gw: gw,
  124. LinkIndex: privIface,
  125. Protocol: unix.RTPROT_STATIC,
  126. }, enc.Strategy(), t.privateIP, tunlIface))
  127. }
  128. }
  129. return routes, rules
  130. }
  131. for _, segment := range t.segments {
  132. // Add routes for the current segment if local is true.
  133. if segment.location == t.location {
  134. // If the local node does not have a private IP address,
  135. // then skip adding routes, because the node is in its own location.
  136. if local && t.privateIP != nil {
  137. for i := range segment.cidrs {
  138. // Don't add routes for the local node.
  139. if segment.privateIPs[i].Equal(t.privateIP.IP) {
  140. continue
  141. }
  142. routes = append(routes, encapsulateRoute(&netlink.Route{
  143. Dst: segment.cidrs[i],
  144. Flags: int(netlink.FLAG_ONLINK),
  145. Gw: segment.privateIPs[i],
  146. LinkIndex: privIface,
  147. Protocol: unix.RTPROT_STATIC,
  148. }, enc.Strategy(), t.privateIP, tunlIface))
  149. // Encapsulate packets from the host's Pod subnet headed
  150. // to private IPs.
  151. if enc.Strategy() == encapsulation.Always || (enc.Strategy() == encapsulation.CrossSubnet && !t.privateIP.Contains(segment.privateIPs[i])) {
  152. routes = append(routes, &netlink.Route{
  153. Dst: oneAddressCIDR(segment.privateIPs[i]),
  154. Flags: int(netlink.FLAG_ONLINK),
  155. Gw: segment.privateIPs[i],
  156. LinkIndex: tunlIface,
  157. Protocol: unix.RTPROT_STATIC,
  158. Table: kiloTableIndex,
  159. })
  160. rules = append(rules, defaultRule(&netlink.Rule{
  161. Src: t.subnet,
  162. Dst: oneAddressCIDR(segment.privateIPs[i]),
  163. Table: kiloTableIndex,
  164. }))
  165. // Also encapsulate packets from the Kilo interface
  166. // headed to private IPs.
  167. rules = append(rules, defaultRule(&netlink.Rule{
  168. Dst: oneAddressCIDR(segment.privateIPs[i]),
  169. Table: kiloTableIndex,
  170. IifName: kiloIfaceName,
  171. }))
  172. }
  173. }
  174. }
  175. // Continuing here prevents leaders form adding routes via WireGuard to
  176. // nodes in their own location.
  177. continue
  178. }
  179. for i := range segment.cidrs {
  180. // Add routes to the Pod CIDRs of nodes in other segments.
  181. routes = append(routes, &netlink.Route{
  182. Dst: segment.cidrs[i],
  183. Flags: int(netlink.FLAG_ONLINK),
  184. Gw: segment.wireGuardIP,
  185. LinkIndex: kiloIface,
  186. Protocol: unix.RTPROT_STATIC,
  187. })
  188. // Don't add routes through Kilo if the private IP
  189. // equals the external IP. This means that the node
  190. // is only accessible through an external IP and we
  191. // cannot encapsulate traffic to an IP through the IP.
  192. if segment.privateIPs == nil || segment.privateIPs[i].Equal(t.updateEndpoint(segment.endpoint, segment.key, &segment.persistentKeepalive).IP()) {
  193. continue
  194. }
  195. // Add routes to the private IPs of nodes in other segments.
  196. // Number of CIDRs and private IPs always match so
  197. // we can reuse the loop.
  198. routes = append(routes, &netlink.Route{
  199. Dst: oneAddressCIDR(segment.privateIPs[i]),
  200. Flags: int(netlink.FLAG_ONLINK),
  201. Gw: segment.wireGuardIP,
  202. LinkIndex: kiloIface,
  203. Protocol: unix.RTPROT_STATIC,
  204. })
  205. }
  206. // For segments / locations other than the location of this instance of kg,
  207. // we need to set routes for allowed location IPs over the wg interface.
  208. for i := range segment.allowedLocationIPs {
  209. routes = append(routes, &netlink.Route{
  210. Dst: &segment.allowedLocationIPs[i],
  211. Flags: int(netlink.FLAG_ONLINK),
  212. Gw: segment.wireGuardIP,
  213. LinkIndex: kiloIface,
  214. Protocol: unix.RTPROT_STATIC,
  215. })
  216. }
  217. }
  218. // Add routes for the allowed IPs of peers.
  219. for _, peer := range t.peers {
  220. for i := range peer.AllowedIPs {
  221. routes = append(routes, &netlink.Route{
  222. Dst: &peer.AllowedIPs[i],
  223. LinkIndex: kiloIface,
  224. Protocol: unix.RTPROT_STATIC,
  225. })
  226. }
  227. }
  228. return routes, rules
  229. }
  230. // PeerRoutes generates a slice of routes and rules for a given peer in the Topology.
  231. func (t *Topology) PeerRoutes(name string, kiloIface int, additionalAllowedIPs []net.IPNet) ([]*netlink.Route, []*netlink.Rule) {
  232. var routes []*netlink.Route
  233. var rules []*netlink.Rule
  234. for _, segment := range t.segments {
  235. for i := range segment.cidrs {
  236. // Add routes to the Pod CIDRs of nodes in other segments.
  237. routes = append(routes, &netlink.Route{
  238. Dst: segment.cidrs[i],
  239. Flags: int(netlink.FLAG_ONLINK),
  240. Gw: segment.wireGuardIP,
  241. LinkIndex: kiloIface,
  242. Protocol: unix.RTPROT_STATIC,
  243. })
  244. }
  245. for i := range segment.privateIPs {
  246. // Add routes to the private IPs of nodes in other segments.
  247. routes = append(routes, &netlink.Route{
  248. Dst: oneAddressCIDR(segment.privateIPs[i]),
  249. Flags: int(netlink.FLAG_ONLINK),
  250. Gw: segment.wireGuardIP,
  251. LinkIndex: kiloIface,
  252. Protocol: unix.RTPROT_STATIC,
  253. })
  254. }
  255. // Add routes for the allowed location IPs of all segments.
  256. for i := range segment.allowedLocationIPs {
  257. routes = append(routes, &netlink.Route{
  258. Dst: &segment.allowedLocationIPs[i],
  259. Flags: int(netlink.FLAG_ONLINK),
  260. Gw: segment.wireGuardIP,
  261. LinkIndex: kiloIface,
  262. Protocol: unix.RTPROT_STATIC,
  263. })
  264. }
  265. routes = append(routes, &netlink.Route{
  266. Dst: oneAddressCIDR(segment.wireGuardIP),
  267. LinkIndex: kiloIface,
  268. Protocol: unix.RTPROT_STATIC,
  269. })
  270. }
  271. // Add routes for the allowed IPs of peers.
  272. for _, peer := range t.peers {
  273. // Don't add routes to ourselves.
  274. if peer.Name == name {
  275. continue
  276. }
  277. for i := range peer.AllowedIPs {
  278. routes = append(routes, &netlink.Route{
  279. Dst: &peer.AllowedIPs[i],
  280. LinkIndex: kiloIface,
  281. Protocol: unix.RTPROT_STATIC,
  282. })
  283. }
  284. }
  285. for i := range additionalAllowedIPs {
  286. routes = append(routes, &netlink.Route{
  287. Dst: &additionalAllowedIPs[i],
  288. Flags: int(netlink.FLAG_ONLINK),
  289. Gw: t.segments[0].wireGuardIP,
  290. LinkIndex: kiloIface,
  291. Protocol: unix.RTPROT_STATIC,
  292. })
  293. }
  294. return routes, rules
  295. }
  296. func encapsulateRoute(route *netlink.Route, encapsulate encapsulation.Strategy, subnet *net.IPNet, tunlIface int) *netlink.Route {
  297. if encapsulate == encapsulation.Always || (encapsulate == encapsulation.CrossSubnet && !subnet.Contains(route.Gw)) {
  298. route.LinkIndex = tunlIface
  299. }
  300. return route
  301. }
  302. // Rules returns the iptables rules required by the local node.
  303. func (t *Topology) Rules(cni, iptablesForwardRule bool) iptables.RuleSet {
  304. rules := iptables.RuleSet{}
  305. rules.AddToAppend(iptables.NewIPv4Chain("nat", "KILO-NAT"))
  306. rules.AddToAppend(iptables.NewIPv6Chain("nat", "KILO-NAT"))
  307. if cni {
  308. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(t.subnet.IP), "nat", "POSTROUTING", "-s", t.subnet.String(), "-m", "comment", "--comment", "Kilo: jump to KILO-NAT chain", "-j", "KILO-NAT"))
  309. // Some linux distros or docker will set forward DROP in the filter table.
  310. // To still be able to have pod to pod communication we need to ALLOW packets from and to pod CIDRs within a location.
  311. // Leader nodes will forward packets from all nodes within a location because they act as a gateway for them.
  312. // Non leader nodes only need to allow packages from and to their own pod CIDR.
  313. if iptablesForwardRule && t.leader {
  314. for _, s := range t.segments {
  315. if s.location == t.location {
  316. // Make sure packets to and from pod cidrs are not dropped in the forward chain.
  317. for _, c := range s.cidrs {
  318. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(c.IP), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets from the pod subnet", "-s", c.String(), "-j", "ACCEPT"))
  319. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(c.IP), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets to the pod subnet", "-d", c.String(), "-j", "ACCEPT"))
  320. }
  321. // Make sure packets to and from allowed location IPs are not dropped in the forward chain.
  322. for _, c := range s.allowedLocationIPs {
  323. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(c.IP), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets from allowed location IPs", "-s", c.String(), "-j", "ACCEPT"))
  324. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(c.IP), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets to allowed location IPs", "-d", c.String(), "-j", "ACCEPT"))
  325. }
  326. // Make sure packets to and from private IPs are not dropped in the forward chain.
  327. for _, c := range s.privateIPs {
  328. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(c), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets from private IPs", "-s", oneAddressCIDR(c).String(), "-j", "ACCEPT"))
  329. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(c), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets to private IPs", "-d", oneAddressCIDR(c).String(), "-j", "ACCEPT"))
  330. }
  331. }
  332. }
  333. } else if iptablesForwardRule {
  334. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(t.subnet.IP), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets from the node's pod subnet", "-s", t.subnet.String(), "-j", "ACCEPT"))
  335. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(t.subnet.IP), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets to the node's pod subnet", "-d", t.subnet.String(), "-j", "ACCEPT"))
  336. }
  337. }
  338. for _, s := range t.segments {
  339. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(s.wireGuardIP), "nat", "KILO-NAT", "-d", oneAddressCIDR(s.wireGuardIP).String(), "-m", "comment", "--comment", "Kilo: do not NAT packets destined for WireGuared IPs", "-j", "RETURN"))
  340. for _, aip := range s.allowedIPs {
  341. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(aip.IP), "nat", "KILO-NAT", "-d", aip.String(), "-m", "comment", "--comment", "Kilo: do not NAT packets destined for known IPs", "-j", "RETURN"))
  342. }
  343. // Make sure packets to allowed location IPs go through the KILO-NAT chain, so they can be MASQUERADEd,
  344. // Otherwise packets to these destinations will reach the destination, but never find their way back.
  345. // We only want to NAT in locations of the corresponding allowed location IPs.
  346. if t.location == s.location {
  347. for _, alip := range s.allowedLocationIPs {
  348. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(alip.IP), "nat", "POSTROUTING", "-d", alip.String(), "-m", "comment", "--comment", "Kilo: jump to NAT chain", "-j", "KILO-NAT"))
  349. }
  350. }
  351. }
  352. for _, p := range t.peers {
  353. for _, aip := range p.AllowedIPs {
  354. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(aip.IP), "nat", "POSTROUTING", "-s", aip.String(), "-m", "comment", "--comment", "Kilo: jump to NAT chain", "-j", "KILO-NAT"))
  355. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(aip.IP), "nat", "KILO-NAT", "-d", aip.String(), "-m", "comment", "--comment", "Kilo: do not NAT packets destined for peers", "-j", "RETURN"))
  356. }
  357. }
  358. for _, s := range t.serviceCIDRs {
  359. rules.AddToAppend(iptables.NewRule(iptables.GetProtocol(s.IP), "nat", "KILO-NAT", "-d", s.String(), "-m", "comment", "--comment", "Kilo: do not NAT packets destined for service CIDRs", "-j", "RETURN"))
  360. }
  361. rules.AddToAppend(iptables.NewIPv4Rule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"))
  362. rules.AddToAppend(iptables.NewIPv6Rule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"))
  363. return rules
  364. }
  365. func defaultRule(rule *netlink.Rule) *netlink.Rule {
  366. base := netlink.NewRule()
  367. base.Src = rule.Src
  368. base.Dst = rule.Dst
  369. base.IifName = rule.IifName
  370. base.Table = rule.Table
  371. return base
  372. }