ebtables.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ebtables
  14. import (
  15. "fmt"
  16. utilexec "k8s.io/kubernetes/pkg/util/exec"
  17. "regexp"
  18. "strings"
  19. "sync"
  20. )
  21. const (
  22. cmdebtables = "ebtables"
  23. // Flag to show full mac in output. The default representation omits leading zeroes.
  24. fullMac = "--Lmac2"
  25. )
  26. type RulePosition string
  27. const (
  28. Prepend RulePosition = "-I"
  29. Append RulePosition = "-A"
  30. )
  31. type Table string
  32. const (
  33. TableNAT Table = "nat"
  34. TableFilter Table = "filter"
  35. )
  36. type Chain string
  37. const (
  38. ChainPostrouting Chain = "POSTROUTING"
  39. ChainPrerouting Chain = "PREROUTING"
  40. ChainOutput Chain = "OUTPUT"
  41. ChainInput Chain = "INPUT"
  42. )
  43. type operation string
  44. const (
  45. opCreateChain operation = "-N"
  46. opFlushChain operation = "-F"
  47. opDeleteChain operation = "-X"
  48. opListChain operation = "-L"
  49. opAppendRule operation = "-A"
  50. opPrependRule operation = "-I"
  51. opDeleteRule operation = "-D"
  52. )
  53. // An injectable interface for running ebtables commands. Implementations must be goroutine-safe.
  54. type Interface interface {
  55. // GetVersion returns the "X.Y.Z" semver string for ebtables.
  56. GetVersion() (string, error)
  57. // EnsureRule checks if the specified rule is present and, if not, creates it. If the rule existed, return true.
  58. // WARNING: ebtables does not provide check operation like iptables do. Hence we have to do a string match of args.
  59. // Input args must follow the format and sequence of ebtables list output. Otherwise, EnsureRule will always create
  60. // new rules and causing duplicates.
  61. EnsureRule(position RulePosition, table Table, chain Chain, args ...string) (bool, error)
  62. // EnsureChain checks if the specified chain is present and, if not, creates it. If the rule existed, return true.
  63. EnsureChain(table Table, chain Chain) (bool, error)
  64. // DeleteChain deletes the specified chain. If the chain did not exist, return error.
  65. DeleteChain(table Table, chain Chain) error
  66. // FlushChain flush the specified chain. If the chain did not exist, return error.
  67. FlushChain(table Table, chain Chain) error
  68. }
  69. // runner implements Interface in terms of exec("ebtables").
  70. type runner struct {
  71. mu sync.Mutex
  72. exec utilexec.Interface
  73. }
  74. // New returns a new Interface which will exec ebtables.
  75. func New(exec utilexec.Interface) Interface {
  76. runner := &runner{
  77. exec: exec,
  78. }
  79. return runner
  80. }
  81. func makeFullArgs(table Table, op operation, chain Chain, args ...string) []string {
  82. return append([]string{"-t", string(table), string(op), string(chain)}, args...)
  83. }
  84. // getEbtablesVersionString runs "ebtables --version" to get the version string
  85. // in the form "X.X.X"
  86. func getEbtablesVersionString(exec utilexec.Interface) (string, error) {
  87. // this doesn't access mutable state so we don't need to use the interface / runner
  88. bytes, err := exec.Command(cmdebtables, "--version").CombinedOutput()
  89. if err != nil {
  90. return "", err
  91. }
  92. versionMatcher := regexp.MustCompile("v([0-9]+\\.[0-9]+\\.[0-9]+)")
  93. match := versionMatcher.FindStringSubmatch(string(bytes))
  94. if match == nil {
  95. return "", fmt.Errorf("no ebtables version found in string: %s", bytes)
  96. }
  97. return match[1], nil
  98. }
  99. func (runner *runner) GetVersion() (string, error) {
  100. return getEbtablesVersionString(runner.exec)
  101. }
  102. func (runner *runner) EnsureRule(position RulePosition, table Table, chain Chain, args ...string) (bool, error) {
  103. exist := true
  104. fullArgs := makeFullArgs(table, opListChain, chain, fullMac)
  105. out, err := runner.exec.Command(cmdebtables, fullArgs...).CombinedOutput()
  106. if err != nil {
  107. exist = false
  108. } else {
  109. exist = checkIfRuleExists(string(out), args...)
  110. }
  111. if !exist {
  112. fullArgs = makeFullArgs(table, operation(position), chain, args...)
  113. out, err := runner.exec.Command(cmdebtables, fullArgs...).CombinedOutput()
  114. if err != nil {
  115. return exist, fmt.Errorf("Failed to ensure rule: %v, output: %v", err, string(out))
  116. }
  117. }
  118. return exist, nil
  119. }
  120. func (runner *runner) EnsureChain(table Table, chain Chain) (bool, error) {
  121. exist := true
  122. args := makeFullArgs(table, opListChain, chain)
  123. _, err := runner.exec.Command(cmdebtables, args...).CombinedOutput()
  124. if err != nil {
  125. exist = false
  126. }
  127. if !exist {
  128. args = makeFullArgs(table, opCreateChain, chain)
  129. out, err := runner.exec.Command(cmdebtables, args...).CombinedOutput()
  130. if err != nil {
  131. return exist, fmt.Errorf("Failed to ensure %v chain: %v, output: %v", chain, err, string(out))
  132. }
  133. }
  134. return exist, nil
  135. }
  136. // checkIfRuleExists takes the output of ebtables list chain and checks if the input rules exists
  137. // WARNING: checkIfRuleExists expects the input args matches the format and sequence of ebtables list output
  138. func checkIfRuleExists(listChainOutput string, args ...string) bool {
  139. rule := strings.Join(args, " ")
  140. for _, line := range strings.Split(listChainOutput, "\n") {
  141. if strings.TrimSpace(line) == rule {
  142. return true
  143. }
  144. }
  145. return false
  146. }
  147. func (runner *runner) DeleteChain(table Table, chain Chain) error {
  148. fullArgs := makeFullArgs(table, opDeleteChain, chain)
  149. out, err := runner.exec.Command(cmdebtables, fullArgs...).CombinedOutput()
  150. if err != nil {
  151. return fmt.Errorf("Failed to delete %v chain %v: %v, output: %v", string(table), string(chain), err, string(out))
  152. }
  153. return nil
  154. }
  155. func (runner *runner) FlushChain(table Table, chain Chain) error {
  156. fullArgs := makeFullArgs(table, opFlushChain, chain)
  157. out, err := runner.exec.Command(cmdebtables, fullArgs...).CombinedOutput()
  158. if err != nil {
  159. return fmt.Errorf("Failed to flush %v chain %v: %v, output: %v", string(table), string(chain), err, string(out))
  160. }
  161. return nil
  162. }