| 1 | // Copyright 2016 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 | // This is a "meta-plugin". It reads in its own netconf, it does not create |
| 16 | // any network interface but just changes the network sysctl. |
| 17 | |
| 18 | package mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/ |
| 19 | |
| 20 | import ( |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 22 | "netscip-go gomod github.com/golang/go/src go1.24.2 net/" |
| 23 | |
| 24 | "github.com/coreos/go-iptables/iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/" |
| 25 | |
| 26 | currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/ "github.com/containernetworking/cni/pkg/types/100scip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/" |
| 27 | "github.com/containernetworking/plugins/pkg/utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/" |
| 28 | ) |
| 29 | |
| 30 | func getPrivChainRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/getPrivChainRules().(ip string) [][]string { |
| 31 | var rules [][]string |
| 32 | rules = append(rules, []string{"-d", ip, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}) |
| 33 | rules = append(rules, []string{"-s", ip, "-j", "ACCEPT"}) |
| 34 | return rules |
| 35 | } |
| 36 | |
| 37 | func generateFilterRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/generateFilterRule().(privChainName string) []string { |
| 38 | return []string{"-m", "comment", "--comment", "CNI firewall plugin rules", "-j", privChainName} |
| 39 | } |
| 40 | |
| 41 | func generateAdminRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/generateAdminRule().(adminChainName string) []string { |
| 42 | return []string{"-m", "comment", "--comment", "CNI firewall plugin admin overrides", "-j", adminChainName} |
| 43 | } |
| 44 | |
| 45 | func cleanupRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/cleanupRules().(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#, privChainName string, rules [][]string) { |
| 46 | for _, rule := range rules { |
| 47 | ipt.Deletescip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Delete().("filter", privChainName, rule...) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func ensureFirstChainRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/ensureFirstChainRule().(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#, chain string, rule []string) error { |
| 52 | exists, err := ipt.Existsscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Exists().("filter", chain, rule...) |
| 53 | if !exists && err == nil { |
| 54 | err = ipt.Insertscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Insert().("filter", chain, 1, rule...) |
| 55 | } |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | func (ib *iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#) setupChainsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#setupChains().(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#) error { |
| 60 | privRule := generateFilterRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/generateFilterRule().(ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName.) |
| 61 | adminRule := generateAdminRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/generateAdminRule().(ib.adminChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#adminChainName.) |
| 62 | |
| 63 | // Ensure our private chains exist |
| 64 | if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.EnsureChainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/EnsureChain().(ipt, "filter", ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName.); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.EnsureChainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/EnsureChain().(ipt, "filter", ib.adminChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#adminChainName.); err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | // Ensure our filter rule exists in the forward chain |
| 72 | if err := ensureFirstChainRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/ensureFirstChainRule().(ipt, "FORWARD", privRule); err != nil { |
| 73 | return err |
| 74 | } |
| 75 | |
| 76 | // Ensure our admin override chain rule exists in our private chain |
| 77 | return ensureFirstChainRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/ensureFirstChainRule().(ipt, ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName., adminRule) |
| 78 | } |
| 79 | |
| 80 | func protoForIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/protoForIP().(ip 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#) iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol# { |
| 81 | if ip.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 { |
| 82 | return 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. |
| 83 | } |
| 84 | return 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. |
| 85 | } |
| 86 | |
| 87 | func (ib *iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#) addRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#addRules().(_ *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#, result *currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#, 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#, proto iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol#) error { |
| 88 | rules := make([][]string, 0) |
| 89 | for _, ip := range result.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs. { |
| 90 | if protoForIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/protoForIP().(ip.Addressscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Address.) == proto { |
| 91 | rules = append(rules, getPrivChainRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/getPrivChainRules().(ipStringscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/ipString().(ip.Addressscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Address.))...) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if len(rules) > 0 { |
| 96 | if err := ib.setupChainsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#setupChains().(ipt); err != nil { |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | // Clean up on any errors |
| 101 | var err error |
| 102 | defer func() { |
| 103 | if err != nil { |
| 104 | cleanupRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/cleanupRules().(ipt, ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName., rules) |
| 105 | } |
| 106 | }() |
| 107 | |
| 108 | for _, rule := range rules { |
| 109 | err = ipt.AppendUniquescip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#AppendUnique().("filter", ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName., rule...) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | func (ib *iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#) delRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#delRules().(_ *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#, result *currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#, 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#, proto iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol#) { |
| 120 | rules := make([][]string, 0) |
| 121 | for _, ip := range result.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs. { |
| 122 | if protoForIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/protoForIP().(ip.Addressscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Address.) == proto { |
| 123 | rules = append(rules, getPrivChainRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/getPrivChainRules().(ipStringscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/ipString().(ip.Addressscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Address.))...) |
| 124 | } |
| 125 | } |
| 126 | if len(rules) > 0 { |
| 127 | cleanupRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/cleanupRules().(ipt, ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName., rules) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func (ib *iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#) checkRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#checkRules().(_ *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#, result *currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#, 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#, proto iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol#) error { |
| 132 | rules := make([][]string, 0) |
| 133 | for _, ip := range result.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs. { |
| 134 | if protoForIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/protoForIP().(ip.Addressscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Address.) == proto { |
| 135 | rules = append(rules, getPrivChainRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/getPrivChainRules().(ipStringscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/ipString().(ip.Addressscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Address.))...) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if len(rules) == 0 { |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | // Ensure our private chains exist |
| 144 | if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.EnsureChainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/EnsureChain().(ipt, "filter", ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName.); err != nil { |
| 145 | return err |
| 146 | } |
| 147 | if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.EnsureChainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/EnsureChain().(ipt, "filter", ib.adminChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#adminChainName.); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | |
| 151 | // Ensure our filter rule exists in the forward chain |
| 152 | privRule := generateFilterRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/generateFilterRule().(ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName.) |
| 153 | privExists, err := ipt.Existsscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Exists().("filter", "FORWARD", privRule...) |
| 154 | if err != nil { |
| 155 | return err |
| 156 | } |
| 157 | if !privExists { |
| 158 | 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().("expected %v rule %v not found", "FORWARD", privRule) |
| 159 | } |
| 160 | |
| 161 | // Ensure our admin override chain rule exists in our private chain |
| 162 | adminRule := generateAdminRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/generateAdminRule().(ib.adminChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#adminChainName.) |
| 163 | adminExists, err := ipt.Existsscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Exists().("filter", ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName., adminRule...) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | if !adminExists { |
| 168 | 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().("expected %v rule %v not found", ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName., adminRule) |
| 169 | } |
| 170 | |
| 171 | // ensure rules for this IP address exist |
| 172 | for _, rule := range rules { |
| 173 | // Ensure our rule exists in our private chain |
| 174 | exists, err := ipt.Existsscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Exists().("filter", ib.privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName., rule...) |
| 175 | if err != nil { |
| 176 | return err |
| 177 | } |
| 178 | if !exists { |
| 179 | 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().("expected rule %v not found", rule) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | func findProtosscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/findProtos().(conf *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#) []iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol# { |
| 187 | protos := []iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol#{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., 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.} |
| 188 | if conf.PrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#PrevResult. != nil { |
| 189 | // If PrevResult is given, scan all IP addresses to figure out |
| 190 | // which IP versions to use |
| 191 | protos = []iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol#{} |
| 192 | result, _ := currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.NewResultFromResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/NewResultFromResult().(conf.PrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#PrevResult.) |
| 193 | for _, addr := range result.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs. { |
| 194 | if addr.Addressscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Address..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 { |
| 195 | protos = append(protos, 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.) |
| 196 | } else { |
| 197 | protos = append(protos, 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.) |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | return protos |
| 202 | } |
| 203 | |
| 204 | type iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend# struct { |
| 205 | protosscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#protos. map[iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol#]*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# |
| 206 | privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName. string |
| 207 | adminChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#adminChainName. string |
| 208 | } |
| 209 | |
| 210 | // iptablesBackend implements the FirewallBackend interface |
| 211 | var _ FirewallBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallBackend# = &iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#{} |
| 212 | |
| 213 | func newIptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/newIptablesBackend().(conf *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#) (FirewallBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallBackend#, error) { |
| 214 | adminChainName := conf.IptablesAdminChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#IptablesAdminChainName. |
| 215 | if adminChainName == "" { |
| 216 | adminChainName = "CNI-ADMIN" |
| 217 | } |
| 218 | |
| 219 | backend := &iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#{ |
| 220 | privChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#privChainName.: "CNI-FORWARD", |
| 221 | adminChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#adminChainName.: adminChainName, |
| 222 | protosscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#protos.: make(map[iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol#]*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#), |
| 223 | } |
| 224 | |
| 225 | for _, proto := range []iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.Protocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Protocol#{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., 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.} { |
| 226 | 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().(proto) |
| 227 | if err != nil { |
| 228 | return nil, 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().("could not initialize iptables protocol %v: %v", proto, err) |
| 229 | } |
| 230 | backend.protosscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#protos.[proto] = ipt |
| 231 | } |
| 232 | |
| 233 | return backend, nil |
| 234 | } |
| 235 | |
| 236 | func (ib *iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#) Addscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#Add().(conf *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#, result *currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#) error { |
| 237 | for proto, ipt := range ib.protosscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#protos. { |
| 238 | if err := ib.addRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#addRules().(conf, result, ipt, proto); err != nil { |
| 239 | return err |
| 240 | } |
| 241 | } |
| 242 | return nil |
| 243 | } |
| 244 | |
| 245 | func (ib *iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#) Delscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#Del().(conf *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#, result *currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#) error { |
| 246 | for proto, ipt := range ib.protosscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#protos. { |
| 247 | ib.delRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#delRules().(conf, result, ipt, proto) |
| 248 | } |
| 249 | return nil |
| 250 | } |
| 251 | |
| 252 | func (ib *iptablesBackendscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#) Checkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#Check().(conf *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#, result *currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#) error { |
| 253 | for proto, ipt := range ib.protosscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#protos. { |
| 254 | if err := ib.checkRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/iptablesBackend#checkRules().(conf, result, ipt, proto); err != nil { |
| 255 | return err |
| 256 | } |
| 257 | } |
| 258 | return nil |
| 259 | } |