setup_ip_tables.go 5.1 KB

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