Debian Codegraph @

plugins/meta/portmap/chain.go

raw symbols SCIP
1 // Copyright 2017 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 mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/
16
17 import (
18 "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/"
19 "stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/"
20
21 "github.com/coreos/go-iptables/iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/"
22 "github.com/mattn/go-shellwordsscip-go gomod github.com/mattn/go-shellwords v1.0.12 `github.com/mattn/go-shellwords`/"
23
24 "github.com/containernetworking/plugins/pkg/utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/"
25 )
26
27 type chainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain# struct {
28 tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table. string
29 namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name. string
30 entryChainsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryChains. []string // the chains to add the entry rule
31
32 entryRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryRules. [][]string // the rules that "point" to this chain
33 rulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#rules. [][]string // the rules this chain contains
34
35 prependEntryscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#prependEntry. bool // whether or not the entry rules should be prepended
36 }
37
38 // setup idempotently creates the chain. It will not error if the chain exists.
39 func (c *chainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#) setupscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#setup().(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 {
40 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, c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name.)
41 if err != nil {
42 return err
43 }
44
45 // Add the rules to the chain
46 for _, rule := range c.rulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#rules. {
47 if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.InsertUniquescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/InsertUnique().(ipt, c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name., false, rule); err != nil {
48 return err
49 }
50 }
51
52 // Add the entry rules to the entry chains
53 for _, entryChain := range c.entryChainsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryChains. {
54 for _, rule := range c.entryRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryRules. {
55 r := []string{}
56 r = append(r, rule...)
57 r = append(r, "-j", c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name.)
58 if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.InsertUniquescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/InsertUnique().(ipt, c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., entryChain, c.prependEntryscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#prependEntry., r); err != nil {
59 return err
60 }
61 }
62 }
63
64 return nil
65 }
66
67 // teardown idempotently deletes a chain. It will not error if the chain doesn't exist.
68 // It will first delete all references to this chain in the entryChains.
69 func (c *chainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#) teardownscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#teardown().(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 {
70 // nothing to do if the custom chain doesn't exist to begin with
71 exists, err := ipt.ChainExistsscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#ChainExists().(c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name.)
72 if err == nil && !exists {
73 return nil
74 }
75 // delete references created by setup()
76 for _, entryChain := range c.entryChainsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryChains. {
77 for _, rule := range c.entryRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryRules. {
78 r := []string{}
79 r = append(r, rule...)
80 r = append(r, "-j", c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name.)
81
82 ipt.Deletescip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Delete().(c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., entryChain, r...)
83 }
84 }
85 // if chain deletion succeeds now, all references are gone
86 if err := ipt.ClearAndDeleteChainscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#ClearAndDeleteChain().(c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name.); err == nil {
87 return nil
88 }
89
90 // find references the hard way
91 for _, entryChain := range c.entryChainsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryChains. {
92 entryChainRules, err := ipt.Listscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#List().(c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., entryChain)
93 if err != nil || len(entryChainRules) < 1 {
94 // Swallow error here - probably the chain doesn't exist.
95 // If we miss something the deletion will fail
96 continue
97 }
98
99 for _, entryChainRule := range entryChainRules[1:] {
100 if stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.24.2 strings/HasSuffix().(entryChainRule, "-j "+c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name.) {
101 chainParts, err := shellwordsscip-go gomod github.com/mattn/go-shellwords v1.0.12 `github.com/mattn/go-shellwords`/.Parsescip-go gomod github.com/mattn/go-shellwords v1.0.12 `github.com/mattn/go-shellwords`/Parse().(entryChainRule)
102 if err != nil {
103 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().("error parsing iptables rule: %s: %v", entryChainRule, err)
104 }
105 chainParts = chainParts[2:] // List results always include an -A CHAINNAME
106
107 if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.DeleteRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/DeleteRule().(ipt, c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., entryChain, chainParts...); err != nil {
108 return err
109 }
110
111 }
112 }
113 }
114
115 return ipt.ClearAndDeleteChainscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#ClearAndDeleteChain().(c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name.)
116 }
117
118 // check the chain.
119 func (c *chainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#) checkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#check().(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 {
120 exists, err := ipt.ChainExistsscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#ChainExists().(c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name.)
121 if err != nil {
122 return err
123 }
124 if !exists {
125 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().("chain %s not found in iptables table %s", c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name., c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table.)
126 }
127
128 for i := len(c.rulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#rules.) - 1; i >= 0; i-- {
129 match := checkRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/checkRule().(ipt, c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name., c.rulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#rules.[i])
130 if !match {
131 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().("rule %s in chain %s not found in table %s", c.rulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#rules., c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name., c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table.)
132 }
133 }
134
135 for _, entryChain := range c.entryChainsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryChains. {
136 for i := len(c.entryRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryRules.) - 1; i >= 0; i-- {
137 r := []string{}
138 r = append(r, c.entryRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryRules.[i]...)
139 r = append(r, "-j", c.namescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#name.)
140 matchEntryChain := checkRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/checkRule().(ipt, c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table., entryChain, r)
141 if !matchEntryChain {
142 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().("rule %s in chain %s not found in table %s", c.entryRulesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#entryRules., entryChain, c.tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/chain#table.)
143 }
144 }
145 }
146
147 return nil
148 }
149
150 func checkRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/checkRule().(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#, table, chain string, rule []string) bool {
151 exists, err := ipt.Existsscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Exists().(table, chain, rule...)
152 if err != nil {
153 return false
154 }
155 return exists
156 }