routes.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. Src: t.privateIP.IP,
  73. Protocol: unix.RTPROT_STATIC,
  74. Table: kiloTableIndex,
  75. })
  76. rules = append(rules, defaultRule(&netlink.Rule{
  77. Src: t.subnet,
  78. Dst: oneAddressCIDR(segment.privateIPs[i]),
  79. Table: kiloTableIndex,
  80. }))
  81. }
  82. }
  83. }
  84. continue
  85. }
  86. for i := range segment.cidrs {
  87. // Add routes to the Pod CIDRs of nodes in other segments.
  88. routes = append(routes, encapsulateRoute(&netlink.Route{
  89. Dst: segment.cidrs[i],
  90. Flags: int(netlink.FLAG_ONLINK),
  91. Gw: gw,
  92. LinkIndex: privIface,
  93. Protocol: unix.RTPROT_STATIC,
  94. }, enc.Strategy(), t.privateIP, tunlIface))
  95. }
  96. for i := range segment.privateIPs {
  97. // Add routes to the private IPs of nodes in other segments.
  98. routes = append(routes, encapsulateRoute(&netlink.Route{
  99. Dst: oneAddressCIDR(segment.privateIPs[i]),
  100. Flags: int(netlink.FLAG_ONLINK),
  101. Gw: gw,
  102. LinkIndex: privIface,
  103. Protocol: unix.RTPROT_STATIC,
  104. }, enc.Strategy(), t.privateIP, tunlIface))
  105. }
  106. // For segments / locations other than the location of this instance of kg,
  107. // we need to set routes for allowed location IPs over the leader in the current location.
  108. for i := range segment.allowedLocationIPs {
  109. routes = append(routes, encapsulateRoute(&netlink.Route{
  110. Dst: &segment.allowedLocationIPs[i],
  111. Flags: int(netlink.FLAG_ONLINK),
  112. Gw: gw,
  113. LinkIndex: privIface,
  114. Protocol: unix.RTPROT_STATIC,
  115. }, enc.Strategy(), t.privateIP, tunlIface))
  116. }
  117. }
  118. // Add routes for the allowed IPs of peers.
  119. for _, peer := range t.peers {
  120. for i := range peer.AllowedIPs {
  121. routes = append(routes, encapsulateRoute(&netlink.Route{
  122. Dst: &peer.AllowedIPs[i],
  123. Flags: int(netlink.FLAG_ONLINK),
  124. Gw: gw,
  125. LinkIndex: privIface,
  126. Protocol: unix.RTPROT_STATIC,
  127. }, enc.Strategy(), t.privateIP, tunlIface))
  128. }
  129. }
  130. return routes, rules
  131. }
  132. // Compute the preferred source address for routes through the WireGuard interface.
  133. // Without this, the kernel picks the WireGuard overlay IP (e.g. 100.66.0.x) as the
  134. // source, which can cause issues in environments like Azure SDN where the overlay
  135. // IP is unknown to the network fabric and reply packets cannot be routed back.
  136. var src net.IP
  137. if t.privateIP != nil {
  138. src = t.privateIP.IP
  139. }
  140. for _, segment := range t.segments {
  141. // Add routes for the current segment if local is true.
  142. if segment.location == t.location {
  143. // If the local node does not have a private IP address,
  144. // then skip adding routes, because the node is in its own location.
  145. if local && t.privateIP != nil {
  146. for i := range segment.cidrs {
  147. // Don't add routes for the local node.
  148. if segment.privateIPs[i].Equal(t.privateIP.IP) {
  149. continue
  150. }
  151. routes = append(routes, encapsulateRoute(&netlink.Route{
  152. Dst: segment.cidrs[i],
  153. Flags: int(netlink.FLAG_ONLINK),
  154. Gw: segment.privateIPs[i],
  155. LinkIndex: privIface,
  156. Protocol: unix.RTPROT_STATIC,
  157. }, enc.Strategy(), t.privateIP, tunlIface))
  158. // Encapsulate packets from the host's Pod subnet headed
  159. // to private IPs.
  160. if enc.Strategy() == encapsulation.Always || (enc.Strategy() == encapsulation.CrossSubnet && !t.privateIP.Contains(segment.privateIPs[i])) {
  161. routes = append(routes, &netlink.Route{
  162. Dst: oneAddressCIDR(segment.privateIPs[i]),
  163. Flags: int(netlink.FLAG_ONLINK),
  164. Gw: segment.privateIPs[i],
  165. LinkIndex: tunlIface,
  166. Src: t.privateIP.IP,
  167. Protocol: unix.RTPROT_STATIC,
  168. Table: kiloTableIndex,
  169. })
  170. rules = append(rules, defaultRule(&netlink.Rule{
  171. Src: t.subnet,
  172. Dst: oneAddressCIDR(segment.privateIPs[i]),
  173. Table: kiloTableIndex,
  174. }))
  175. // Also encapsulate packets from the Kilo interface
  176. // headed to private IPs.
  177. rules = append(rules, defaultRule(&netlink.Rule{
  178. Dst: oneAddressCIDR(segment.privateIPs[i]),
  179. Table: kiloTableIndex,
  180. IifName: kiloIfaceName,
  181. }))
  182. }
  183. }
  184. }
  185. // Continuing here prevents leaders form adding routes via WireGuard to
  186. // nodes in their own location.
  187. continue
  188. }
  189. for i := range segment.cidrs {
  190. // Add routes to the Pod CIDRs of nodes in other segments.
  191. routes = append(routes, &netlink.Route{
  192. Dst: segment.cidrs[i],
  193. Flags: int(netlink.FLAG_ONLINK),
  194. Gw: segment.wireGuardIP,
  195. LinkIndex: kiloIface,
  196. Src: src,
  197. Protocol: unix.RTPROT_STATIC,
  198. })
  199. // Don't add routes through Kilo if the private IP
  200. // equals the external IP. This means that the node
  201. // is only accessible through an external IP and we
  202. // cannot encapsulate traffic to an IP through the IP.
  203. if segment.privateIPs == nil || segment.privateIPs[i].Equal(t.updateEndpoint(segment.endpoint, segment.key, &segment.persistentKeepalive).IP()) {
  204. continue
  205. }
  206. // Add routes to the private IPs of nodes in other segments.
  207. // Number of CIDRs and private IPs always match so
  208. // we can reuse the loop.
  209. routes = append(routes, &netlink.Route{
  210. Dst: oneAddressCIDR(segment.privateIPs[i]),
  211. Flags: int(netlink.FLAG_ONLINK),
  212. Gw: segment.wireGuardIP,
  213. LinkIndex: kiloIface,
  214. Src: src,
  215. Protocol: unix.RTPROT_STATIC,
  216. })
  217. }
  218. // For segments / locations other than the location of this instance of kg,
  219. // we need to set routes for allowed location IPs over the wg interface.
  220. for i := range segment.allowedLocationIPs {
  221. routes = append(routes, &netlink.Route{
  222. Dst: &segment.allowedLocationIPs[i],
  223. Flags: int(netlink.FLAG_ONLINK),
  224. Gw: segment.wireGuardIP,
  225. LinkIndex: kiloIface,
  226. Src: src,
  227. Protocol: unix.RTPROT_STATIC,
  228. })
  229. }
  230. }
  231. // Add routes for the allowed IPs of peers.
  232. for _, peer := range t.peers {
  233. for i := range peer.AllowedIPs {
  234. routes = append(routes, &netlink.Route{
  235. Dst: &peer.AllowedIPs[i],
  236. LinkIndex: kiloIface,
  237. Src: src,
  238. Protocol: unix.RTPROT_STATIC,
  239. })
  240. }
  241. }
  242. return routes, rules
  243. }
  244. // PeerRoutes generates a slice of routes and rules for a given peer in the Topology.
  245. func (t *Topology) PeerRoutes(name string, kiloIface int, additionalAllowedIPs []net.IPNet) ([]*netlink.Route, []*netlink.Rule) {
  246. var routes []*netlink.Route
  247. var rules []*netlink.Rule
  248. for _, segment := range t.segments {
  249. for i := range segment.cidrs {
  250. // Add routes to the Pod CIDRs of nodes in other segments.
  251. routes = append(routes, &netlink.Route{
  252. Dst: segment.cidrs[i],
  253. Flags: int(netlink.FLAG_ONLINK),
  254. Gw: segment.wireGuardIP,
  255. LinkIndex: kiloIface,
  256. Protocol: unix.RTPROT_STATIC,
  257. })
  258. }
  259. for i := range segment.privateIPs {
  260. // Add routes to the private IPs of nodes in other segments.
  261. routes = append(routes, &netlink.Route{
  262. Dst: oneAddressCIDR(segment.privateIPs[i]),
  263. Flags: int(netlink.FLAG_ONLINK),
  264. Gw: segment.wireGuardIP,
  265. LinkIndex: kiloIface,
  266. Protocol: unix.RTPROT_STATIC,
  267. })
  268. }
  269. // Add routes for the allowed location IPs of all segments.
  270. for i := range segment.allowedLocationIPs {
  271. routes = append(routes, &netlink.Route{
  272. Dst: &segment.allowedLocationIPs[i],
  273. Flags: int(netlink.FLAG_ONLINK),
  274. Gw: segment.wireGuardIP,
  275. LinkIndex: kiloIface,
  276. Protocol: unix.RTPROT_STATIC,
  277. })
  278. }
  279. routes = append(routes, &netlink.Route{
  280. Dst: oneAddressCIDR(segment.wireGuardIP),
  281. LinkIndex: kiloIface,
  282. Protocol: unix.RTPROT_STATIC,
  283. })
  284. }
  285. // Add routes for the allowed IPs of peers.
  286. for _, peer := range t.peers {
  287. // Don't add routes to ourselves.
  288. if peer.Name == name {
  289. continue
  290. }
  291. for i := range peer.AllowedIPs {
  292. routes = append(routes, &netlink.Route{
  293. Dst: &peer.AllowedIPs[i],
  294. LinkIndex: kiloIface,
  295. Protocol: unix.RTPROT_STATIC,
  296. })
  297. }
  298. }
  299. for i := range additionalAllowedIPs {
  300. routes = append(routes, &netlink.Route{
  301. Dst: &additionalAllowedIPs[i],
  302. Flags: int(netlink.FLAG_ONLINK),
  303. Gw: t.segments[0].wireGuardIP,
  304. LinkIndex: kiloIface,
  305. Protocol: unix.RTPROT_STATIC,
  306. })
  307. }
  308. return routes, rules
  309. }
  310. func encapsulateRoute(route *netlink.Route, encapsulate encapsulation.Strategy, subnet *net.IPNet, tunlIface int) *netlink.Route {
  311. if encapsulate == encapsulation.Always || (encapsulate == encapsulation.CrossSubnet && subnet != nil && !subnet.Contains(route.Gw)) {
  312. route.LinkIndex = tunlIface
  313. if subnet != nil && route.Src == nil {
  314. route.Src = subnet.IP
  315. }
  316. }
  317. return route
  318. }
  319. // Rules returns the iptables rules required by the local node.
  320. func (t *Topology) Rules(cni, iptablesForwardRule bool) iptables.RuleSet {
  321. rules := iptables.RuleSet{}
  322. rules.AddToAppend(iptables.NewIPv4Chain("nat", "KILO-NAT"))
  323. rules.AddToAppend(iptables.NewIPv6Chain("nat", "KILO-NAT"))
  324. if cni {
  325. 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"))
  326. // Some linux distros or docker will set forward DROP in the filter table.
  327. // To still be able to have pod to pod communication we need to ALLOW packets from and to pod CIDRs within a location.
  328. // Leader nodes will forward packets from all nodes within a location because they act as a gateway for them.
  329. // Non leader nodes only need to allow packages from and to their own pod CIDR.
  330. if iptablesForwardRule && t.leader {
  331. for _, s := range t.segments {
  332. if s.location == t.location {
  333. // Make sure packets to and from pod cidrs are not dropped in the forward chain.
  334. for _, c := range s.cidrs {
  335. 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"))
  336. 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"))
  337. }
  338. // Make sure packets to and from allowed location IPs are not dropped in the forward chain.
  339. for _, c := range s.allowedLocationIPs {
  340. 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"))
  341. 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"))
  342. }
  343. // Make sure packets to and from private IPs are not dropped in the forward chain.
  344. for _, c := range s.privateIPs {
  345. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(c), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets from private IPs", "-s", oneAddressCIDR(c).String(), "-j", "ACCEPT"))
  346. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(c), "filter", "FORWARD", "-m", "comment", "--comment", "Kilo: forward packets to private IPs", "-d", oneAddressCIDR(c).String(), "-j", "ACCEPT"))
  347. }
  348. }
  349. }
  350. } else if iptablesForwardRule {
  351. 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"))
  352. 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"))
  353. }
  354. }
  355. for _, s := range t.segments {
  356. 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"))
  357. for _, aip := range s.allowedIPs {
  358. 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"))
  359. }
  360. // Make sure packets to allowed location IPs go through the KILO-NAT chain, so they can be MASQUERADEd,
  361. // Otherwise packets to these destinations will reach the destination, but never find their way back.
  362. // We only want to NAT in locations of the corresponding allowed location IPs.
  363. if t.location == s.location {
  364. for _, alip := range s.allowedLocationIPs {
  365. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(alip.IP), "nat", "POSTROUTING", "-d", alip.String(), "-m", "comment", "--comment", "Kilo: jump to NAT chain", "-j", "KILO-NAT"))
  366. }
  367. }
  368. }
  369. for _, p := range t.peers {
  370. for _, aip := range p.AllowedIPs {
  371. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(aip.IP), "nat", "POSTROUTING", "-s", aip.String(), "-m", "comment", "--comment", "Kilo: jump to NAT chain", "-j", "KILO-NAT"))
  372. 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"))
  373. }
  374. }
  375. for _, s := range t.serviceCIDRs {
  376. 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"))
  377. }
  378. rules.AddToAppend(iptables.NewIPv4Rule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"))
  379. rules.AddToAppend(iptables.NewIPv6Rule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"))
  380. return rules
  381. }
  382. func defaultRule(rule *netlink.Rule) *netlink.Rule {
  383. base := netlink.NewRule()
  384. base.Src = rule.Src
  385. base.Dst = rule.Dst
  386. base.IifName = rule.IifName
  387. base.Table = rule.Table
  388. return base
  389. }