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