| 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 utils |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
| 20 | |
| 21 | "github.com/coreos/go-iptables/iptables" |
| 22 | ) |
| 23 | |
| 24 | const statusChainExists = 1 |
| 25 | |
| 26 | // EnsureChain idempotently creates the iptables chain. It does not |
| 27 | // return an error if the chain already exists. |
| 28 | func EnsureChain(ipt *iptables.IPTables, table, chain string) error { |
| 29 | if ipt == nil { |
| 30 | return errors.New("failed to ensure iptable chain: IPTables was nil") |
| 31 | } |
| 32 | exists, err := ipt.ChainExists(table, chain) |
| 33 | if err != nil { |
| 34 | return fmt.Errorf("failed to check iptables chain existence: %v", err) |
| 35 | } |
| 36 | if !exists { |
| 37 | err = ipt.NewChain(table, chain) |
| 38 | if err != nil { |
| 39 | eerr, eok := err.(*iptables.Error) |
| 40 | if eok && eerr.ExitStatus() != 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 DeleteRule(ipt *iptables.IPTables, table, chain string, rulespec ...string) error { |
| 51 | if ipt == nil { |
| 52 | return errors.New("failed to ensure iptable chain: IPTables was nil") |
| 53 | } |
| 54 | if err := ipt.Delete(table, chain, rulespec...); err != nil { |
| 55 | eerr, eok := err.(*iptables.Error) |
| 56 | switch { |
| 57 | case eok && eerr.IsNotExist(): |
| 58 | // swallow here, the chain was already deleted |
| 59 | return nil |
| 60 | case eok && eerr.ExitStatus() == 2: |
| 61 | // swallow here, invalid command line parameter because the referring rule is missing |
| 62 | return nil |
| 63 | default: |
| 64 | return 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 DeleteChain(ipt *iptables.IPTables, table, chain string) error { |
| 73 | if ipt == nil { |
| 74 | return errors.New("failed to ensure iptable chain: IPTables was nil") |
| 75 | } |
| 76 | |
| 77 | err := ipt.DeleteChain(table, chain) |
| 78 | eerr, eok := err.(*iptables.Error) |
| 79 | switch { |
| 80 | case eok && eerr.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 ClearChain(ipt *iptables.IPTables, table, chain string) error { |
| 91 | if ipt == nil { |
| 92 | return errors.New("failed to ensure iptable chain: IPTables was nil") |
| 93 | } |
| 94 | err := ipt.ClearChain(table, chain) |
| 95 | eerr, eok := err.(*iptables.Error) |
| 96 | switch { |
| 97 | case eok && eerr.IsNotExist(): |
| 98 | // swallow here, the chain was already deleted |
| 99 | return 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 InsertUnique(ipt *iptables.IPTables, table, chain string, prepend bool, rule []string) error { |
| 108 | exists, err := ipt.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.Insert(table, chain, 1, rule...) |
| 118 | } |
| 119 | return ipt.Append(table, chain, rule...) |
| 120 | } |