|
15 | 15 | package ip |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "errors" |
18 | 19 | "fmt" |
19 | 20 | "net" |
| 21 | + "strings" |
20 | 22 |
|
21 | | - "github.com/coreos/go-iptables/iptables" |
| 23 | + "github.com/containernetworking/cni/pkg/types" |
| 24 | + "github.com/containernetworking/plugins/pkg/utils" |
22 | 25 | ) |
23 | 26 |
|
24 | | -// SetupIPMasq installs iptables rules to masquerade traffic |
25 | | -// coming from ip of ipn and going outside of ipn |
26 | | -func SetupIPMasq(ipn *net.IPNet, chain string, comment string) error { |
27 | | - isV6 := ipn.IP.To4() == nil |
28 | | - |
29 | | - var ipt *iptables.IPTables |
30 | | - var err error |
31 | | - var multicastNet string |
32 | | - |
33 | | - if isV6 { |
34 | | - ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv6) |
35 | | - multicastNet = "ff00::/8" |
36 | | - } else { |
37 | | - ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4) |
38 | | - multicastNet = "224.0.0.0/4" |
39 | | - } |
40 | | - if err != nil { |
41 | | - return fmt.Errorf("failed to locate iptables: %v", err) |
42 | | - } |
43 | | - |
44 | | - // Create chain if doesn't exist |
45 | | - exists := false |
46 | | - chains, err := ipt.ListChains("nat") |
47 | | - if err != nil { |
48 | | - return fmt.Errorf("failed to list chains: %v", err) |
49 | | - } |
50 | | - for _, ch := range chains { |
51 | | - if ch == chain { |
52 | | - exists = true |
53 | | - break |
54 | | - } |
55 | | - } |
56 | | - if !exists { |
57 | | - if err = ipt.NewChain("nat", chain); err != nil { |
58 | | - return err |
| 27 | +// SetupIPMasqForNetwork installs rules to masquerade traffic coming from ip of ipn and |
| 28 | +// going outside of ipn, 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 SetupIPMasqForNetwork(backend *string, ipn *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" |
59 | 37 | } |
| 38 | + backend = &defaultBackend |
60 | 39 | } |
61 | 40 |
|
62 | | - // Packets to this network should not be touched |
63 | | - if err := ipt.AppendUnique("nat", chain, "-d", ipn.String(), "-j", "ACCEPT", "-m", "comment", "--comment", comment); err != nil { |
64 | | - return err |
| 41 | + switch *backend { |
| 42 | + case "iptables": |
| 43 | + return setupIPMasqIPTables(ipn, network, ifname, containerID) |
| 44 | + case "nftables": |
| 45 | + return setupIPMasqNFTables(ipn, network, ifname, containerID) |
| 46 | + default: |
| 47 | + return fmt.Errorf("unknown ipmasq backend %q", *backend) |
65 | 48 | } |
66 | | - |
67 | | - // Don't masquerade multicast - pods should be able to talk to other pods |
68 | | - // on the local network via multicast. |
69 | | - if err := ipt.AppendUnique("nat", chain, "!", "-d", multicastNet, "-j", "MASQUERADE", "-m", "comment", "--comment", comment); err != nil { |
70 | | - return err |
71 | | - } |
72 | | - |
73 | | - // Packets from the specific IP of this network will hit the chain |
74 | | - return ipt.AppendUnique("nat", "POSTROUTING", "-s", ipn.IP.String(), "-j", chain, "-m", "comment", "--comment", comment) |
75 | 49 | } |
76 | 50 |
|
77 | | -// TeardownIPMasq undoes the effects of SetupIPMasq |
78 | | -func TeardownIPMasq(ipn *net.IPNet, chain string, comment string) error { |
79 | | - isV6 := ipn.IP.To4() == nil |
| 51 | +// TeardownIPMasqForNetwork undoes the effects of SetupIPMasqForNetwork |
| 52 | +func TeardownIPMasqForNetwork(ipn *net.IPNet, network, ifname, containerID string) error { |
| 53 | + var errs []string |
80 | 54 |
|
81 | | - var ipt *iptables.IPTables |
82 | | - var err error |
| 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. |
83 | 57 |
|
84 | | - if isV6 { |
85 | | - ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv6) |
86 | | - } else { |
87 | | - ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4) |
88 | | - } |
89 | | - if err != nil { |
90 | | - return fmt.Errorf("failed to locate iptables: %v", err) |
| 58 | + err := teardownIPMasqIPTables(ipn, network, ifname, containerID) |
| 59 | + if err != nil && utils.SupportsIPTables() { |
| 60 | + errs = append(errs, err.Error()) |
91 | 61 | } |
92 | 62 |
|
93 | | - err = ipt.Delete("nat", "POSTROUTING", "-s", ipn.IP.String(), "-j", chain, "-m", "comment", "--comment", comment) |
94 | | - if err != nil && !isNotExist(err) { |
95 | | - return err |
| 63 | + err = teardownIPMasqNFTables(ipn, network, ifname, containerID) |
| 64 | + if err != nil && utils.SupportsNFTables() { |
| 65 | + errs = append(errs, err.Error()) |
96 | 66 | } |
97 | 67 |
|
98 | | - // for downward compatibility |
99 | | - err = ipt.Delete("nat", "POSTROUTING", "-s", ipn.String(), "-j", chain, "-m", "comment", "--comment", comment) |
100 | | - if err != nil && !isNotExist(err) { |
101 | | - return err |
| 68 | + if errs == nil { |
| 69 | + return nil |
102 | 70 | } |
| 71 | + return errors.New(strings.Join(errs, "\n")) |
| 72 | +} |
103 | 73 |
|
104 | | - err = ipt.ClearChain("nat", chain) |
105 | | - if err != nil && !isNotExist(err) { |
106 | | - return err |
107 | | - } |
| 74 | +// GCIPMasqForNetwork garbage collects stale IPMasq entries for network |
| 75 | +func GCIPMasqForNetwork(network string, attachments []types.GCAttachment) error { |
| 76 | + var errs []string |
108 | 77 |
|
109 | | - err = ipt.DeleteChain("nat", chain) |
110 | | - if err != nil && !isNotExist(err) { |
111 | | - return err |
| 78 | + err := gcIPMasqIPTables(network, attachments) |
| 79 | + if err != nil && utils.SupportsIPTables() { |
| 80 | + errs = append(errs, err.Error()) |
112 | 81 | } |
113 | 82 |
|
114 | | - return nil |
115 | | -} |
| 83 | + err = gcIPMasqNFTables(network, attachments) |
| 84 | + if err != nil && utils.SupportsNFTables() { |
| 85 | + errs = append(errs, err.Error()) |
| 86 | + } |
116 | 87 |
|
117 | | -// isNotExist returnst true if the error is from iptables indicating |
118 | | -// that the target does not exist. |
119 | | -func isNotExist(err error) bool { |
120 | | - e, ok := err.(*iptables.Error) |
121 | | - if !ok { |
122 | | - return false |
| 88 | + if errs == nil { |
| 89 | + return nil |
123 | 90 | } |
124 | | - return e.IsNotExist() |
| 91 | + return errors.New(strings.Join(errs, "\n")) |
125 | 92 | } |
0 commit comments