| 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 main |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/coreos/go-iptables/iptables" |
| 22 | "github.com/mattn/go-shellwords" |
| 23 | |
| 24 | "github.com/containernetworking/plugins/pkg/utils" |
| 25 | ) |
| 26 | |
| 27 | type chain struct { |
| 28 | table string |
| 29 | name string |
| 30 | entryChains []string // the chains to add the entry rule |
| 31 | |
| 32 | entryRules [][]string // the rules that "point" to this chain |
| 33 | rules [][]string // the rules this chain contains |
| 34 | |
| 35 | 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 *chain) setup(ipt *iptables.IPTables) error { |
| 40 | err := utils.EnsureChain(ipt, c.table, c.name) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | |
| 45 | // Add the rules to the chain |
| 46 | for _, rule := range c.rules { |
| 47 | if err := utils.InsertUnique(ipt, c.table, c.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.entryChains { |
| 54 | for _, rule := range c.entryRules { |
| 55 | r := []string{} |
| 56 | r = append(r, rule...) |
| 57 | r = append(r, "-j", c.name) |
| 58 | if err := utils.InsertUnique(ipt, c.table, entryChain, c.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 *chain) teardown(ipt *iptables.IPTables) error { |
| 70 | // nothing to do if the custom chain doesn't exist to begin with |
| 71 | exists, err := ipt.ChainExists(c.table, c.name) |
| 72 | if err == nil && !exists { |
| 73 | return nil |
| 74 | } |
| 75 | // delete references created by setup() |
| 76 | for _, entryChain := range c.entryChains { |
| 77 | for _, rule := range c.entryRules { |
| 78 | r := []string{} |
| 79 | r = append(r, rule...) |
| 80 | r = append(r, "-j", c.name) |
| 81 | |
| 82 | ipt.Delete(c.table, entryChain, r...) |
| 83 | } |
| 84 | } |
| 85 | // if chain deletion succeeds now, all references are gone |
| 86 | if err := ipt.ClearAndDeleteChain(c.table, c.name); err == nil { |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | // find references the hard way |
| 91 | for _, entryChain := range c.entryChains { |
| 92 | entryChainRules, err := ipt.List(c.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 strings.HasSuffix(entryChainRule, "-j "+c.name) { |
| 101 | chainParts, err := shellwords.Parse(entryChainRule) |
| 102 | if err != nil { |
| 103 | return 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 := utils.DeleteRule(ipt, c.table, entryChain, chainParts...); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return ipt.ClearAndDeleteChain(c.table, c.name) |
| 116 | } |
| 117 | |
| 118 | // check the chain. |
| 119 | func (c *chain) check(ipt *iptables.IPTables) error { |
| 120 | exists, err := ipt.ChainExists(c.table, c.name) |
| 121 | if err != nil { |
| 122 | return err |
| 123 | } |
| 124 | if !exists { |
| 125 | return fmt.Errorf("chain %s not found in iptables table %s", c.name, c.table) |
| 126 | } |
| 127 | |
| 128 | for i := len(c.rules) - 1; i >= 0; i-- { |
| 129 | match := checkRule(ipt, c.table, c.name, c.rules[i]) |
| 130 | if !match { |
| 131 | return fmt.Errorf("rule %s in chain %s not found in table %s", c.rules, c.name, c.table) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | for _, entryChain := range c.entryChains { |
| 136 | for i := len(c.entryRules) - 1; i >= 0; i-- { |
| 137 | r := []string{} |
| 138 | r = append(r, c.entryRules[i]...) |
| 139 | r = append(r, "-j", c.name) |
| 140 | matchEntryChain := checkRule(ipt, c.table, entryChain, r) |
| 141 | if !matchEntryChain { |
| 142 | return fmt.Errorf("rule %s in chain %s not found in table %s", c.entryRules, entryChain, c.table) |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return nil |
| 148 | } |
| 149 | |
| 150 | func checkRule(ipt *iptables.IPTables, table, chain string, rule []string) bool { |
| 151 | exists, err := ipt.Exists(table, chain, rule...) |
| 152 | if err != nil { |
| 153 | return false |
| 154 | } |
| 155 | return exists |
| 156 | } |