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