ipmasq.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 flannel 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 network
  15. import (
  16. "fmt"
  17. "strings"
  18. log "github.com/golang/glog"
  19. "github.com/coreos/flannel/pkg/ip"
  20. "github.com/coreos/flannel/subnet"
  21. "github.com/coreos/go-iptables/iptables"
  22. "time"
  23. )
  24. type IPTablesRules interface {
  25. AppendUnique(table string, chain string, rulespec ...string) error
  26. Delete(table string, chain string, rulespec ...string) error
  27. Exists(table string, chain string, rulespec ...string) (bool, error)
  28. }
  29. func rules(ipn ip.IP4Net, lease *subnet.Lease) [][]string {
  30. n := ipn.String()
  31. sn := lease.Subnet.String()
  32. return [][]string{
  33. // This rule makes sure we don't NAT traffic within overlay network (e.g. coming out of docker0)
  34. {"-s", n, "-d", n, "-j", "RETURN"},
  35. // NAT if it's not multicast traffic
  36. {"-s", n, "!", "-d", "224.0.0.0/4", "-j", "MASQUERADE"},
  37. // Prevent performing Masquerade on external traffic which arrives from a Node that owns the container/pod IP address
  38. {"!", "-s", n, "-d", sn, "-j", "RETURN"},
  39. // Masquerade anything headed towards flannel from the host
  40. {"!", "-s", n, "-d", n, "-j", "MASQUERADE"},
  41. }
  42. }
  43. func ipMasqRulesExist(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) (bool, error) {
  44. for _, rule := range rules(ipn, lease) {
  45. exists, err := ipt.Exists("nat", "POSTROUTING", rule...)
  46. if err != nil {
  47. // this shouldn't ever happen
  48. return false, fmt.Errorf("failed to check rule existence: %v", err)
  49. }
  50. if !exists {
  51. return false, nil
  52. }
  53. }
  54. return true, nil
  55. }
  56. func SetupAndEnsureIPMasq(network ip.IP4Net, lease *subnet.Lease) {
  57. ipt, err := iptables.New()
  58. if err != nil {
  59. // if we can't find iptables, give up and return
  60. log.Errorf("Failed to setup IP Masquerade. IPTables was not found: %v", err)
  61. return
  62. }
  63. defer func() {
  64. teardownIPMasq(ipt, network, lease)
  65. }()
  66. for {
  67. // Ensure that all the rules exist every 5 seconds
  68. if err := ensureIPMasq(ipt, network, lease); err != nil {
  69. log.Errorf("Failed to ensure IP Masquerade: %v", err)
  70. }
  71. time.Sleep(5 * time.Second)
  72. }
  73. }
  74. func ensureIPMasq(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) error {
  75. exists, err := ipMasqRulesExist(ipt, ipn, lease)
  76. if err != nil {
  77. return fmt.Errorf("Error checking rule existence: %v", err)
  78. }
  79. if exists {
  80. // if all the rules already exist, no need to do anything
  81. return nil
  82. }
  83. // Otherwise, teardown all the rules and set them up again
  84. // We do this because the order of the rules is important
  85. log.Info("Some iptables rules are missing; deleting and recreating rules")
  86. teardownIPMasq(ipt, ipn, lease)
  87. if err = setupIPMasq(ipt, ipn, lease); err != nil {
  88. return fmt.Errorf("Error setting up rules: %v", err)
  89. }
  90. return nil
  91. }
  92. func setupIPMasq(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) error {
  93. for _, rule := range rules(ipn, lease) {
  94. log.Info("Adding iptables rule: ", strings.Join(rule, " "))
  95. err := ipt.AppendUnique("nat", "POSTROUTING", rule...)
  96. if err != nil {
  97. return fmt.Errorf("failed to insert IP masquerade rule: %v", err)
  98. }
  99. }
  100. return nil
  101. }
  102. func teardownIPMasq(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) {
  103. for _, rule := range rules(ipn, lease) {
  104. log.Info("Deleting iptables rule: ", strings.Join(rule, " "))
  105. // We ignore errors here because if there's an error it's almost certainly because the rule
  106. // doesn't exist, which is fine (we don't need to delete rules that don't exist)
  107. ipt.Delete("nat", "POSTROUTING", rule...)
  108. }
  109. }