ipmasq_linux.go 2.8 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. "errors"
  17. "fmt"
  18. "net"
  19. "strings"
  20. "github.com/containernetworking/cni/pkg/types"
  21. "github.com/containernetworking/plugins/pkg/utils"
  22. )
  23. // SetupIPMasqForNetworks installs rules to masquerade traffic coming from ips of ipns and
  24. // going outside of ipns, using a chain name based on network, ifname, and containerID. The
  25. // backend can be either "iptables" or "nftables"; if it is nil, then a suitable default
  26. // implementation will be used.
  27. func SetupIPMasqForNetworks(backend *string, ipns []*net.IPNet, network, ifname, containerID string) error {
  28. if backend == nil {
  29. // Prefer iptables, unless only nftables is available
  30. defaultBackend := "iptables"
  31. if !utils.SupportsIPTables() && utils.SupportsNFTables() {
  32. defaultBackend = "nftables"
  33. }
  34. backend = &defaultBackend
  35. }
  36. switch *backend {
  37. case "iptables":
  38. return setupIPMasqIPTables(ipns, network, ifname, containerID)
  39. case "nftables":
  40. return setupIPMasqNFTables(ipns, network, ifname, containerID)
  41. default:
  42. return fmt.Errorf("unknown ipmasq backend %q", *backend)
  43. }
  44. }
  45. // TeardownIPMasqForNetworks undoes the effects of SetupIPMasqForNetworks
  46. func TeardownIPMasqForNetworks(ipns []*net.IPNet, network, ifname, containerID string) error {
  47. var errs []string
  48. // Do both the iptables and the nftables cleanup, since the pod may have been
  49. // created with a different version of this plugin or a different configuration.
  50. err := teardownIPMasqIPTables(ipns, network, ifname, containerID)
  51. if err != nil && utils.SupportsIPTables() {
  52. errs = append(errs, err.Error())
  53. }
  54. err = teardownIPMasqNFTables(ipns, network, ifname, containerID)
  55. if err != nil && utils.SupportsNFTables() {
  56. errs = append(errs, err.Error())
  57. }
  58. if errs == nil {
  59. return nil
  60. }
  61. return errors.New(strings.Join(errs, "\n"))
  62. }
  63. // GCIPMasqForNetwork garbage collects stale IPMasq entries for network
  64. func GCIPMasqForNetwork(network string, attachments []types.GCAttachment) error {
  65. var errs []string
  66. err := gcIPMasqIPTables(network, attachments)
  67. if err != nil && utils.SupportsIPTables() {
  68. errs = append(errs, err.Error())
  69. }
  70. err = gcIPMasqNFTables(network, attachments)
  71. if err != nil && utils.SupportsNFTables() {
  72. errs = append(errs, err.Error())
  73. }
  74. if errs == nil {
  75. return nil
  76. }
  77. return errors.New(strings.Join(errs, "\n"))
  78. }