setup_ip_tables.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package bridge
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/docker/libnetwork/netutils"
  6. "github.com/docker/libnetwork/pkg/iptables"
  7. )
  8. // DockerChain: DOCKER iptable chain name
  9. const (
  10. DockerChain = "DOCKER"
  11. )
  12. func setupIPTables(config *NetworkConfiguration, i *bridgeInterface) error {
  13. // Sanity check.
  14. if config.EnableIPTables == false {
  15. return ipTableCfgError(config.BridgeName)
  16. }
  17. addrv4, _, err := netutils.GetIfaceAddr(config.BridgeName)
  18. if err != nil {
  19. return fmt.Errorf("Failed to setup IP tables, cannot acquire Interface address: %s", err.Error())
  20. }
  21. if err = setupIPTablesInternal(config.BridgeName, addrv4, config.EnableICC, config.EnableIPMasquerade, true); err != nil {
  22. return fmt.Errorf("Failed to Setup IP tables: %s", err.Error())
  23. }
  24. _, err = iptables.NewChain(DockerChain, config.BridgeName, iptables.Nat)
  25. if err != nil {
  26. return fmt.Errorf("Failed to create NAT chain: %s", err.Error())
  27. }
  28. chain, err := iptables.NewChain(DockerChain, config.BridgeName, iptables.Filter)
  29. if err != nil {
  30. return fmt.Errorf("Failed to create FILTER chain: %s", err.Error())
  31. }
  32. portMapper.SetIptablesChain(chain)
  33. return nil
  34. }
  35. type iptRule struct {
  36. table iptables.Table
  37. chain string
  38. preArgs []string
  39. args []string
  40. }
  41. func setupIPTablesInternal(bridgeIface string, addr net.Addr, icc, ipmasq, enable bool) error {
  42. var (
  43. address = addr.String()
  44. natRule = iptRule{table: iptables.Nat, chain: "POSTROUTING", preArgs: []string{"-t", "nat"}, args: []string{"-s", address, "!", "-o", bridgeIface, "-j", "MASQUERADE"}}
  45. outRule = iptRule{table: iptables.Filter, chain: "FORWARD", args: []string{"-i", bridgeIface, "!", "-o", bridgeIface, "-j", "ACCEPT"}}
  46. inRule = iptRule{table: iptables.Filter, chain: "FORWARD", args: []string{"-o", bridgeIface, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}}
  47. )
  48. // Set NAT.
  49. if ipmasq {
  50. if err := programChainRule(natRule, "NAT", enable); err != nil {
  51. return err
  52. }
  53. }
  54. // Set Inter Container Communication.
  55. if err := setIcc(bridgeIface, icc, enable); err != nil {
  56. return err
  57. }
  58. // Set Accept on all non-intercontainer outgoing packets.
  59. if err := programChainRule(outRule, "ACCEPT NON_ICC OUTGOING", enable); err != nil {
  60. return err
  61. }
  62. // Set Accept on incoming packets for existing connections.
  63. if err := programChainRule(inRule, "ACCEPT INCOMING", enable); err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. func programChainRule(rule iptRule, ruleDescr string, insert bool) error {
  69. var (
  70. prefix []string
  71. operation string
  72. condition bool
  73. doesExist = iptables.Exists(rule.table, rule.chain, rule.args...)
  74. )
  75. if insert {
  76. condition = !doesExist
  77. prefix = []string{"-I", rule.chain}
  78. operation = "enable"
  79. } else {
  80. condition = doesExist
  81. prefix = []string{"-D", rule.chain}
  82. operation = "disable"
  83. }
  84. if rule.preArgs != nil {
  85. prefix = append(rule.preArgs, prefix...)
  86. }
  87. if condition {
  88. if output, err := iptables.Raw(append(prefix, rule.args...)...); err != nil {
  89. return fmt.Errorf("Unable to %s %s rule: %s", operation, ruleDescr, err.Error())
  90. } else if len(output) != 0 {
  91. return &iptables.ChainError{Chain: rule.chain, Output: output}
  92. }
  93. }
  94. return nil
  95. }
  96. func setIcc(bridgeIface string, iccEnable, insert bool) error {
  97. var (
  98. table = iptables.Filter
  99. chain = "FORWARD"
  100. args = []string{"-i", bridgeIface, "-o", bridgeIface, "-j"}
  101. acceptArgs = append(args, "ACCEPT")
  102. dropArgs = append(args, "DROP")
  103. )
  104. if insert {
  105. if !iccEnable {
  106. iptables.Raw(append([]string{"-D", chain}, acceptArgs...)...)
  107. if !iptables.Exists(table, chain, dropArgs...) {
  108. if output, err := iptables.Raw(append([]string{"-A", chain}, dropArgs...)...); err != nil {
  109. return fmt.Errorf("Unable to prevent intercontainer communication: %s", err.Error())
  110. } else if len(output) != 0 {
  111. return fmt.Errorf("Error disabling intercontainer communication: %s", output)
  112. }
  113. }
  114. } else {
  115. iptables.Raw(append([]string{"-D", chain}, dropArgs...)...)
  116. if !iptables.Exists(table, chain, acceptArgs...) {
  117. if output, err := iptables.Raw(append([]string{"-A", chain}, acceptArgs...)...); err != nil {
  118. return fmt.Errorf("Unable to allow intercontainer communication: %s", err.Error())
  119. } else if len(output) != 0 {
  120. return fmt.Errorf("Error enabling intercontainer communication: %s", output)
  121. }
  122. }
  123. }
  124. } else {
  125. // Remove any ICC rule.
  126. if !iccEnable {
  127. if iptables.Exists(table, chain, dropArgs...) {
  128. iptables.Raw(append([]string{"-D", chain}, dropArgs...)...)
  129. }
  130. } else {
  131. if iptables.Exists(table, chain, acceptArgs...) {
  132. iptables.Raw(append([]string{"-D", chain}, acceptArgs...)...)
  133. }
  134. }
  135. }
  136. return nil
  137. }