ipmasq.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2015 CNI 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 ip
  15. import (
  16. "fmt"
  17. "net"
  18. "github.com/coreos/go-iptables/iptables"
  19. )
  20. // SetupIPMasq installs iptables rules to masquerade traffic
  21. // coming from ipn and going outside of it
  22. func SetupIPMasq(ipn *net.IPNet, chain string, comment string) error {
  23. isV6 := ipn.IP.To4() == nil
  24. var ipt *iptables.IPTables
  25. var err error
  26. var multicastNet string
  27. if isV6 {
  28. ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv6)
  29. multicastNet = "ff00::/8"
  30. } else {
  31. ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4)
  32. multicastNet = "224.0.0.0/4"
  33. }
  34. if err != nil {
  35. return fmt.Errorf("failed to locate iptables: %v", err)
  36. }
  37. // Create chain if doesn't exist
  38. exists := false
  39. chains, err := ipt.ListChains("nat")
  40. if err != nil {
  41. return fmt.Errorf("failed to list chains: %v", err)
  42. }
  43. for _, ch := range chains {
  44. if ch == chain {
  45. exists = true
  46. break
  47. }
  48. }
  49. if !exists {
  50. if err = ipt.NewChain("nat", chain); err != nil {
  51. return err
  52. }
  53. }
  54. // Packets to this network should not be touched
  55. if err := ipt.AppendUnique("nat", chain, "-d", ipn.String(), "-j", "ACCEPT", "-m", "comment", "--comment", comment); err != nil {
  56. return err
  57. }
  58. // Don't masquerade multicast - pods should be able to talk to other pods
  59. // on the local network via multicast.
  60. if err := ipt.AppendUnique("nat", chain, "!", "-d", multicastNet, "-j", "MASQUERADE", "-m", "comment", "--comment", comment); err != nil {
  61. return err
  62. }
  63. return ipt.AppendUnique("nat", "POSTROUTING", "-s", ipn.String(), "-j", chain, "-m", "comment", "--comment", comment)
  64. }
  65. // TeardownIPMasq undoes the effects of SetupIPMasq
  66. func TeardownIPMasq(ipn *net.IPNet, chain string, comment string) error {
  67. ipt, err := iptables.New()
  68. if err != nil {
  69. return fmt.Errorf("failed to locate iptables: %v", err)
  70. }
  71. if err = ipt.Delete("nat", "POSTROUTING", "-s", ipn.String(), "-j", chain, "-m", "comment", "--comment", comment); err != nil {
  72. return err
  73. }
  74. if err = ipt.ClearChain("nat", chain); err != nil {
  75. return err
  76. }
  77. return ipt.DeleteChain("nat", chain)
  78. }