iptables.go 4.4 KB

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