Debian Codegraph @

pkg/utils/iptables.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 utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/
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
21 "github.com/coreos/go-iptables/iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/"
22 )
23
24 const statusChainExistsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/statusChainExists. = 1
25
26 // EnsureChain idempotently creates the iptables chain. It does not
27 // return an error if the chain already exists.
28 func EnsureChainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/EnsureChain().(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) error {
29 if ipt == nil {
30 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().("failed to ensure iptable chain: IPTables was nil")
31 }
32 exists, err := ipt.ChainExistsscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#ChainExists().(table, chain)
33 if err != nil {
34 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 check iptables chain existence: %v", err)
35 }
36 if !exists {
37 err = ipt.NewChainscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#NewChain().(table, chain)
38 if err != nil {
39 eerr, eok := 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#)
40 if eok && eerr.ExitStatusscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Error#ExitStatus().() != statusChainExistsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/statusChainExists. {
41 return err
42 }
43 }
44 }
45 return nil
46 }
47
48 // DeleteRule idempotently delete the iptables rule in the specified table/chain.
49 // It does not return an error if the referring chain doesn't exist
50 func DeleteRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/DeleteRule().(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, rulespec ...string) error {
51 if ipt == nil {
52 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().("failed to ensure iptable chain: IPTables was nil")
53 }
54 if err := ipt.Deletescip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Delete().(table, chain, rulespec...); err != nil {
55 eerr, eok := 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#)
56 switch {
57 case eok && eerr.IsNotExistscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Error#IsNotExist().():
58 // swallow here, the chain was already deleted
59 return nil
60 case eok && eerr.ExitStatusscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Error#ExitStatus().() == 2:
61 // swallow here, invalid command line parameter because the referring rule is missing
62 return nil
63 default:
64 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 delete referring rule %s %s: %v", table, chain, err)
65 }
66 }
67 return nil
68 }
69
70 // DeleteChain idempotently deletes the specified table/chain.
71 // It does not return an errors if the chain does not exist
72 func DeleteChainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/DeleteChain().(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) error {
73 if ipt == nil {
74 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().("failed to ensure iptable chain: IPTables was nil")
75 }
76
77 err := ipt.DeleteChainscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#DeleteChain().(table, chain)
78 eerr, eok := 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#)
79 switch {
80 case eok && eerr.IsNotExistscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Error#IsNotExist().():
81 // swallow here, the chain was already deleted
82 return nil
83 default:
84 return err
85 }
86 }
87
88 // ClearChain idempotently clear the iptables rules in the specified table/chain.
89 // If the chain does not exist, a new one will be created
90 func ClearChainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/ClearChain().(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) error {
91 if ipt == nil {
92 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().("failed to ensure iptable chain: IPTables was nil")
93 }
94 err := ipt.ClearChainscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#ClearChain().(table, chain)
95 eerr, eok := 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#)
96 switch {
97 case eok && eerr.IsNotExistscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/Error#IsNotExist().():
98 // swallow here, the chain was already deleted
99 return EnsureChainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/EnsureChain().(ipt, table, chain)
100 default:
101 return err
102 }
103 }
104
105 // InsertUnique will add a rule to a chain if it does not already exist.
106 // By default the rule is appended, unless prepend is true.
107 func InsertUniquescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/InsertUnique().(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, prepend bool, rule []string) error {
108 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...)
109 if err != nil {
110 return err
111 }
112 if exists {
113 return nil
114 }
115
116 if prepend {
117 return ipt.Insertscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Insert().(table, chain, 1, rule...)
118 }
119 return ipt.Appendscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#Append().(table, chain, rule...)
120 }