Debian Codegraph @

pkg/ip/ipmasq_iptables_linux.go

raw symbols SCIP
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 ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/
16
17 import (
18 "errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/"
19 "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/"
20 "netscip-go gomod github.com/golang/go/src go1.24.2 net/"
21 "stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/"
22
23 "github.com/coreos/go-iptables/iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/"
24
25 "github.com/containernetworking/cni/pkg/typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/"
26 "github.com/containernetworking/plugins/pkg/utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/"
27 )
28
29 // setupIPMasqIPTables is the iptables-based implementation of SetupIPMasqForNetworks
30 func setupIPMasqIPTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/setupIPMasqIPTables().(ipns []*netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#, network, _, containerID string) error {
31 // Note: for historical reasons, the iptables implementation ignores ifname.
32 chain := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.FormatChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/FormatChainName().(network, containerID)
33 comment := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.FormatCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/FormatComment().(network, containerID)
34 for _, ip := range ipns {
35 if err := SetupIPMasqscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/SetupIPMasq().(ip, chain, comment); err != nil {
36 return err
37 }
38 }
39 return nil
40 }
41
42 // SetupIPMasq installs iptables rules to masquerade traffic
43 // coming from ip of ipn and going outside of ipn.
44 // Deprecated: This function only supports iptables. Use SetupIPMasqForNetworks, which
45 // supports both iptables and nftables.
46 func SetupIPMasqscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/SetupIPMasq().(ipn *netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#, chain string, comment string) error {
47 isV6 := ipn.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() == nil
48
49 var ipt *iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.IPTablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#
50 var err error
51 var multicastNet string
52
53 if isV6 {
54 ipt, err = iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.NewWithProtocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/NewWithProtocol().(iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.ProtocolIPv6scip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/ProtocolIPv6.)
55 multicastNet = "ff00::/8"
56 } else {
57 ipt, err = iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.NewWithProtocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/NewWithProtocol().(iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.ProtocolIPv4scip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/ProtocolIPv4.)
58 multicastNet = "224.0.0.0/4"
59 }
60 if err != nil {
61 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to locate iptables: %v", err)
62 }
63
64 // Create chain if doesn't exist
65 exists := false
66 chains, err := ipt.ListChainsscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#ListChains().("nat")
67 if err != nil {
68 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to list chains: %v", err)
69 }
70 for _, ch := range chains {
71 if ch == chain {
72 exists = true
73 break
74 }
75 }
76 if !exists {
77 if err = ipt.NewChainscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#NewChain().("nat", chain); err != nil {
78 return err
79 }
80 }
81
82 // Packets to this network should not be touched
83 if err := ipt.AppendUniquescip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#AppendUnique().("nat", chain, "-d", ipn.Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#String().(), "-j", "ACCEPT", "-m", "comment", "--comment", comment); err != nil {
84 return err
85 }
86
87 // Don't masquerade multicast - pods should be able to talk to other pods
88 // on the local network via multicast.
89 if err := ipt.AppendUniquescip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#AppendUnique().("nat", chain, "!", "-d", multicastNet, "-j", "MASQUERADE", "-m", "comment", "--comment", comment); err != nil {
90 return err
91 }
92
93 // Packets from the specific IP of this network will hit the chain
94 return ipt.AppendUniquescip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#AppendUnique().("nat", "POSTROUTING", "-s", ipn.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().(), "-j", chain, "-m", "comment", "--comment", comment)
95 }
96
97 // teardownIPMasqIPTables is the iptables-based implementation of TeardownIPMasqForNetworks
98 func teardownIPMasqIPTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/teardownIPMasqIPTables().(ipns []*netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#, network, _, containerID string) error {
99 // Note: for historical reasons, the iptables implementation ignores ifname.
100 chain := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.FormatChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/FormatChainName().(network, containerID)
101 comment := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.FormatCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/FormatComment().(network, containerID)
102
103 var errs []string
104 for _, ipn := range ipns {
105 err := TeardownIPMasqscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/TeardownIPMasq().(ipn, chain, comment)
106 if err != nil {
107 errs = append(errs, err.Error())
108 }
109 }
110
111 if errs == nil {
112 return nil
113 }
114 return errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/.Newscip-go gomod github.com/golang/go/src go1.24.2 errors/New().(stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.2 strings/Join().(errs, "\n"))
115 }
116
117 // TeardownIPMasq undoes the effects of SetupIPMasq.
118 // Deprecated: This function only supports iptables. Use TeardownIPMasqForNetworks, which
119 // supports both iptables and nftables.
120 func TeardownIPMasqscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/TeardownIPMasq().(ipn *netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#, chain string, comment string) error {
121 isV6 := ipn.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() == nil
122
123 var ipt *iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.IPTablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#
124 var err error
125
126 if isV6 {
127 ipt, err = iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.NewWithProtocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/NewWithProtocol().(iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.ProtocolIPv6scip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/ProtocolIPv6.)
128 } else {
129 ipt, err = iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.NewWithProtocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/NewWithProtocol().(iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.ProtocolIPv4scip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/ProtocolIPv4.)
130 }
131 if err != nil {
132 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to locate iptables: %v", err)
133 }
134
135 err = ipt.Deletescip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Delete().("nat", "POSTROUTING", "-s", ipn.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().(), "-j", chain, "-m", "comment", "--comment", comment)
136 if err != nil && !isNotExistscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/isNotExist().(err) {
137 return err
138 }
139
140 // for downward compatibility
141 err = ipt.Deletescip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Delete().("nat", "POSTROUTING", "-s", ipn.Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#String().(), "-j", chain, "-m", "comment", "--comment", comment)
142 if err != nil && !isNotExistscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/isNotExist().(err) {
143 return err
144 }
145
146 err = ipt.ClearChainscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#ClearChain().("nat", chain)
147 if err != nil && !isNotExistscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/isNotExist().(err) {
148 return err
149 }
150
151 err = ipt.DeleteChainscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#DeleteChain().("nat", chain)
152 if err != nil && !isNotExistscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/isNotExist().(err) {
153 return err
154 }
155
156 return nil
157 }
158
159 // gcIPMasqIPTables is the iptables-based implementation of GCIPMasqForNetwork
160 func gcIPMasqIPTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/gcIPMasqIPTables().(_ string, _ []typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.GCAttachmentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/GCAttachment#) error {
161 // FIXME: The iptables implementation does not support GC.
162 //
163 // (In theory, it _could_ backward-compatibly support it, by adding a no-op rule
164 // with a comment indicating the network to each chain it creates, so that it
165 // could later figure out which chains corresponded to which networks; older
166 // implementations would ignore the extra rule but would still correctly delete
167 // the chain on teardown (because they ClearChain() before doing DeleteChain()).
168
169 return nil
170 }
171
172 // isNotExist returnst true if the error is from iptables indicating
173 // that the target does not exist.
174 func isNotExistscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/isNotExist().(err error) bool {
175 e, ok := err.(*iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Errorscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Error#)
176 if !ok {
177 return false
178 }
179 return e.IsNotExistscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Error#IsNotExist().()
180 }