iptables.go 3.5 KB

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