iptables.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. Copyright 2014 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 iptables
  14. import (
  15. "bytes"
  16. "context"
  17. "fmt"
  18. "regexp"
  19. "strings"
  20. "sync"
  21. "time"
  22. godbus "github.com/godbus/dbus"
  23. "k8s.io/apimachinery/pkg/util/sets"
  24. utilversion "k8s.io/apimachinery/pkg/util/version"
  25. "k8s.io/klog"
  26. utildbus "k8s.io/kubernetes/pkg/util/dbus"
  27. utilexec "k8s.io/utils/exec"
  28. utiltrace "k8s.io/utils/trace"
  29. )
  30. type RulePosition string
  31. const (
  32. Prepend RulePosition = "-I"
  33. Append RulePosition = "-A"
  34. )
  35. // An injectable interface for running iptables commands. Implementations must be goroutine-safe.
  36. type Interface interface {
  37. // GetVersion returns the "X.Y.Z" version string for iptables.
  38. GetVersion() (string, error)
  39. // EnsureChain checks if the specified chain exists and, if not, creates it. If the chain existed, return true.
  40. EnsureChain(table Table, chain Chain) (bool, error)
  41. // FlushChain clears the specified chain. If the chain did not exist, return error.
  42. FlushChain(table Table, chain Chain) error
  43. // DeleteChain deletes the specified chain. If the chain did not exist, return error.
  44. DeleteChain(table Table, chain Chain) error
  45. // EnsureRule checks if the specified rule is present and, if not, creates it. If the rule existed, return true.
  46. EnsureRule(position RulePosition, table Table, chain Chain, args ...string) (bool, error)
  47. // DeleteRule checks if the specified rule is present and, if so, deletes it.
  48. DeleteRule(table Table, chain Chain, args ...string) error
  49. // IsIpv6 returns true if this is managing ipv6 tables
  50. IsIpv6() bool
  51. // SaveInto calls `iptables-save` for table and stores result in a given buffer.
  52. SaveInto(table Table, buffer *bytes.Buffer) error
  53. // Restore runs `iptables-restore` passing data through []byte.
  54. // table is the Table to restore
  55. // data should be formatted like the output of SaveInto()
  56. // flush sets the presence of the "--noflush" flag. see: FlushFlag
  57. // counters sets the "--counters" flag. see: RestoreCountersFlag
  58. Restore(table Table, data []byte, flush FlushFlag, counters RestoreCountersFlag) error
  59. // RestoreAll is the same as Restore except that no table is specified.
  60. RestoreAll(data []byte, flush FlushFlag, counters RestoreCountersFlag) error
  61. // AddReloadFunc adds a function to call on iptables reload
  62. AddReloadFunc(reloadFunc func())
  63. // Destroy cleans up resources used by the Interface
  64. Destroy()
  65. }
  66. type Protocol byte
  67. const (
  68. ProtocolIpv4 Protocol = iota + 1
  69. ProtocolIpv6
  70. )
  71. type Table string
  72. const (
  73. TableNAT Table = "nat"
  74. TableFilter Table = "filter"
  75. TableMangle Table = "mangle"
  76. )
  77. type Chain string
  78. const (
  79. ChainPostrouting Chain = "POSTROUTING"
  80. ChainPrerouting Chain = "PREROUTING"
  81. ChainOutput Chain = "OUTPUT"
  82. ChainInput Chain = "INPUT"
  83. ChainForward Chain = "FORWARD"
  84. )
  85. const (
  86. cmdIPTablesSave string = "iptables-save"
  87. cmdIPTablesRestore string = "iptables-restore"
  88. cmdIPTables string = "iptables"
  89. cmdIP6TablesRestore string = "ip6tables-restore"
  90. cmdIP6TablesSave string = "ip6tables-save"
  91. cmdIP6Tables string = "ip6tables"
  92. )
  93. // Option flag for Restore
  94. type RestoreCountersFlag bool
  95. const RestoreCounters RestoreCountersFlag = true
  96. const NoRestoreCounters RestoreCountersFlag = false
  97. // Option flag for Flush
  98. type FlushFlag bool
  99. const FlushTables FlushFlag = true
  100. const NoFlushTables FlushFlag = false
  101. // Versions of iptables less than this do not support the -C / --check flag
  102. // (test whether a rule exists).
  103. const MinCheckVersion = "1.4.11"
  104. // Minimum iptables versions supporting the -w and -w<seconds> flags
  105. const WaitMinVersion = "1.4.20"
  106. const WaitSecondsMinVersion = "1.4.22"
  107. const WaitString = "-w"
  108. const WaitSecondsValue = "5"
  109. const LockfilePath16x = "/run/xtables.lock"
  110. // runner implements Interface in terms of exec("iptables").
  111. type runner struct {
  112. mu sync.Mutex
  113. exec utilexec.Interface
  114. dbus utildbus.Interface
  115. protocol Protocol
  116. hasCheck bool
  117. hasListener bool
  118. waitFlag []string
  119. restoreWaitFlag []string
  120. lockfilePath string
  121. reloadFuncs []func()
  122. signal chan *godbus.Signal
  123. }
  124. // newInternal returns a new Interface which will exec iptables, and allows the
  125. // caller to change the iptables-restore lockfile path
  126. func newInternal(exec utilexec.Interface, dbus utildbus.Interface, protocol Protocol, lockfilePath string) Interface {
  127. vstring, err := getIPTablesVersionString(exec, protocol)
  128. if err != nil {
  129. klog.Warningf("Error checking iptables version, assuming version at least %s: %v", MinCheckVersion, err)
  130. vstring = MinCheckVersion
  131. }
  132. if lockfilePath == "" {
  133. lockfilePath = LockfilePath16x
  134. }
  135. runner := &runner{
  136. exec: exec,
  137. dbus: dbus,
  138. protocol: protocol,
  139. hasCheck: getIPTablesHasCheckCommand(vstring),
  140. hasListener: false,
  141. waitFlag: getIPTablesWaitFlag(vstring),
  142. restoreWaitFlag: getIPTablesRestoreWaitFlag(exec, protocol),
  143. lockfilePath: lockfilePath,
  144. }
  145. return runner
  146. }
  147. // New returns a new Interface which will exec iptables.
  148. func New(exec utilexec.Interface, dbus utildbus.Interface, protocol Protocol) Interface {
  149. return newInternal(exec, dbus, protocol, "")
  150. }
  151. // Destroy is part of Interface.
  152. func (runner *runner) Destroy() {
  153. if runner.signal != nil {
  154. runner.signal <- nil
  155. }
  156. }
  157. const (
  158. firewalldName = "org.fedoraproject.FirewallD1"
  159. firewalldPath = "/org/fedoraproject/FirewallD1"
  160. firewalldInterface = "org.fedoraproject.FirewallD1"
  161. )
  162. // Connects to D-Bus and listens for FirewallD start/restart. (On non-FirewallD-using
  163. // systems, this is effectively a no-op; we listen for the signals, but they will never be
  164. // emitted, so reload() will never be called.)
  165. func (runner *runner) connectToFirewallD() {
  166. bus, err := runner.dbus.SystemBus()
  167. if err != nil {
  168. klog.V(1).Infof("Could not connect to D-Bus system bus: %s", err)
  169. return
  170. }
  171. runner.hasListener = true
  172. rule := fmt.Sprintf("type='signal',sender='%s',path='%s',interface='%s',member='Reloaded'", firewalldName, firewalldPath, firewalldInterface)
  173. bus.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
  174. rule = fmt.Sprintf("type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged',path='/org/freedesktop/DBus',sender='org.freedesktop.DBus',arg0='%s'", firewalldName)
  175. bus.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
  176. runner.signal = make(chan *godbus.Signal, 10)
  177. bus.Signal(runner.signal)
  178. go runner.dbusSignalHandler(bus)
  179. }
  180. // GetVersion returns the version string.
  181. func (runner *runner) GetVersion() (string, error) {
  182. return getIPTablesVersionString(runner.exec, runner.protocol)
  183. }
  184. // EnsureChain is part of Interface.
  185. func (runner *runner) EnsureChain(table Table, chain Chain) (bool, error) {
  186. fullArgs := makeFullArgs(table, chain)
  187. runner.mu.Lock()
  188. defer runner.mu.Unlock()
  189. out, err := runner.run(opCreateChain, fullArgs)
  190. if err != nil {
  191. if ee, ok := err.(utilexec.ExitError); ok {
  192. if ee.Exited() && ee.ExitStatus() == 1 {
  193. return true, nil
  194. }
  195. }
  196. return false, fmt.Errorf("error creating chain %q: %v: %s", chain, err, out)
  197. }
  198. return false, nil
  199. }
  200. // FlushChain is part of Interface.
  201. func (runner *runner) FlushChain(table Table, chain Chain) error {
  202. fullArgs := makeFullArgs(table, chain)
  203. runner.mu.Lock()
  204. defer runner.mu.Unlock()
  205. out, err := runner.run(opFlushChain, fullArgs)
  206. if err != nil {
  207. return fmt.Errorf("error flushing chain %q: %v: %s", chain, err, out)
  208. }
  209. return nil
  210. }
  211. // DeleteChain is part of Interface.
  212. func (runner *runner) DeleteChain(table Table, chain Chain) error {
  213. fullArgs := makeFullArgs(table, chain)
  214. runner.mu.Lock()
  215. defer runner.mu.Unlock()
  216. // TODO: we could call iptables -S first, ignore the output and check for non-zero return (more like DeleteRule)
  217. out, err := runner.run(opDeleteChain, fullArgs)
  218. if err != nil {
  219. return fmt.Errorf("error deleting chain %q: %v: %s", chain, err, out)
  220. }
  221. return nil
  222. }
  223. // EnsureRule is part of Interface.
  224. func (runner *runner) EnsureRule(position RulePosition, table Table, chain Chain, args ...string) (bool, error) {
  225. fullArgs := makeFullArgs(table, chain, args...)
  226. runner.mu.Lock()
  227. defer runner.mu.Unlock()
  228. exists, err := runner.checkRule(table, chain, args...)
  229. if err != nil {
  230. return false, err
  231. }
  232. if exists {
  233. return true, nil
  234. }
  235. out, err := runner.run(operation(position), fullArgs)
  236. if err != nil {
  237. return false, fmt.Errorf("error appending rule: %v: %s", err, out)
  238. }
  239. return false, nil
  240. }
  241. // DeleteRule is part of Interface.
  242. func (runner *runner) DeleteRule(table Table, chain Chain, args ...string) error {
  243. fullArgs := makeFullArgs(table, chain, args...)
  244. runner.mu.Lock()
  245. defer runner.mu.Unlock()
  246. exists, err := runner.checkRule(table, chain, args...)
  247. if err != nil {
  248. return err
  249. }
  250. if !exists {
  251. return nil
  252. }
  253. out, err := runner.run(opDeleteRule, fullArgs)
  254. if err != nil {
  255. return fmt.Errorf("error deleting rule: %v: %s", err, out)
  256. }
  257. return nil
  258. }
  259. func (runner *runner) IsIpv6() bool {
  260. return runner.protocol == ProtocolIpv6
  261. }
  262. // SaveInto is part of Interface.
  263. func (runner *runner) SaveInto(table Table, buffer *bytes.Buffer) error {
  264. runner.mu.Lock()
  265. defer runner.mu.Unlock()
  266. trace := utiltrace.New("iptables save")
  267. defer trace.LogIfLong(2 * time.Second)
  268. // run and return
  269. iptablesSaveCmd := iptablesSaveCommand(runner.protocol)
  270. args := []string{"-t", string(table)}
  271. klog.V(4).Infof("running %s %v", iptablesSaveCmd, args)
  272. cmd := runner.exec.Command(iptablesSaveCmd, args...)
  273. cmd.SetStdout(buffer)
  274. stderrBuffer := bytes.NewBuffer(nil)
  275. cmd.SetStderr(stderrBuffer)
  276. err := cmd.Run()
  277. if err != nil {
  278. stderrBuffer.WriteTo(buffer) // ignore error, since we need to return the original error
  279. }
  280. return err
  281. }
  282. // Restore is part of Interface.
  283. func (runner *runner) Restore(table Table, data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
  284. // setup args
  285. args := []string{"-T", string(table)}
  286. return runner.restoreInternal(args, data, flush, counters)
  287. }
  288. // RestoreAll is part of Interface.
  289. func (runner *runner) RestoreAll(data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
  290. // setup args
  291. args := make([]string, 0)
  292. return runner.restoreInternal(args, data, flush, counters)
  293. }
  294. type iptablesLocker interface {
  295. Close() error
  296. }
  297. // restoreInternal is the shared part of Restore/RestoreAll
  298. func (runner *runner) restoreInternal(args []string, data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
  299. runner.mu.Lock()
  300. defer runner.mu.Unlock()
  301. trace := utiltrace.New("iptables restore")
  302. defer trace.LogIfLong(2 * time.Second)
  303. if !flush {
  304. args = append(args, "--noflush")
  305. }
  306. if counters {
  307. args = append(args, "--counters")
  308. }
  309. // Grab the iptables lock to prevent iptables-restore and iptables
  310. // from stepping on each other. iptables-restore 1.6.2 will have
  311. // a --wait option like iptables itself, but that's not widely deployed.
  312. if len(runner.restoreWaitFlag) == 0 {
  313. locker, err := grabIptablesLocks(runner.lockfilePath)
  314. if err != nil {
  315. return err
  316. }
  317. trace.Step("Locks grabbed")
  318. defer func(locker iptablesLocker) {
  319. if err := locker.Close(); err != nil {
  320. klog.Errorf("Failed to close iptables locks: %v", err)
  321. }
  322. }(locker)
  323. }
  324. // run the command and return the output or an error including the output and error
  325. fullArgs := append(runner.restoreWaitFlag, args...)
  326. iptablesRestoreCmd := iptablesRestoreCommand(runner.protocol)
  327. klog.V(4).Infof("running %s %v", iptablesRestoreCmd, fullArgs)
  328. cmd := runner.exec.Command(iptablesRestoreCmd, fullArgs...)
  329. cmd.SetStdin(bytes.NewBuffer(data))
  330. b, err := cmd.CombinedOutput()
  331. if err != nil {
  332. return fmt.Errorf("%v (%s)", err, b)
  333. }
  334. return nil
  335. }
  336. func iptablesSaveCommand(protocol Protocol) string {
  337. if protocol == ProtocolIpv6 {
  338. return cmdIP6TablesSave
  339. }
  340. return cmdIPTablesSave
  341. }
  342. func iptablesRestoreCommand(protocol Protocol) string {
  343. if protocol == ProtocolIpv6 {
  344. return cmdIP6TablesRestore
  345. }
  346. return cmdIPTablesRestore
  347. }
  348. func iptablesCommand(protocol Protocol) string {
  349. if protocol == ProtocolIpv6 {
  350. return cmdIP6Tables
  351. }
  352. return cmdIPTables
  353. }
  354. func (runner *runner) run(op operation, args []string) ([]byte, error) {
  355. return runner.runContext(nil, op, args)
  356. }
  357. func (runner *runner) runContext(ctx context.Context, op operation, args []string) ([]byte, error) {
  358. iptablesCmd := iptablesCommand(runner.protocol)
  359. fullArgs := append(runner.waitFlag, string(op))
  360. fullArgs = append(fullArgs, args...)
  361. klog.V(5).Infof("running iptables %s %v", string(op), args)
  362. if ctx == nil {
  363. return runner.exec.Command(iptablesCmd, fullArgs...).CombinedOutput()
  364. }
  365. return runner.exec.CommandContext(ctx, iptablesCmd, fullArgs...).CombinedOutput()
  366. // Don't log err here - callers might not think it is an error.
  367. }
  368. // Returns (bool, nil) if it was able to check the existence of the rule, or
  369. // (<undefined>, error) if the process of checking failed.
  370. func (runner *runner) checkRule(table Table, chain Chain, args ...string) (bool, error) {
  371. if runner.hasCheck {
  372. return runner.checkRuleUsingCheck(makeFullArgs(table, chain, args...))
  373. }
  374. return runner.checkRuleWithoutCheck(table, chain, args...)
  375. }
  376. var hexnumRE = regexp.MustCompile("0x0+([0-9])")
  377. func trimhex(s string) string {
  378. return hexnumRE.ReplaceAllString(s, "0x$1")
  379. }
  380. // Executes the rule check without using the "-C" flag, instead parsing iptables-save.
  381. // Present for compatibility with <1.4.11 versions of iptables. This is full
  382. // of hack and half-measures. We should nix this ASAP.
  383. func (runner *runner) checkRuleWithoutCheck(table Table, chain Chain, args ...string) (bool, error) {
  384. iptablesSaveCmd := iptablesSaveCommand(runner.protocol)
  385. klog.V(1).Infof("running %s -t %s", iptablesSaveCmd, string(table))
  386. out, err := runner.exec.Command(iptablesSaveCmd, "-t", string(table)).CombinedOutput()
  387. if err != nil {
  388. return false, fmt.Errorf("error checking rule: %v", err)
  389. }
  390. // Sadly, iptables has inconsistent quoting rules for comments. Just remove all quotes.
  391. // Also, quoted multi-word comments (which are counted as a single arg)
  392. // will be unpacked into multiple args,
  393. // in order to compare against iptables-save output (which will be split at whitespace boundary)
  394. // e.g. a single arg('"this must be before the NodePort rules"') will be unquoted and unpacked into 7 args.
  395. var argsCopy []string
  396. for i := range args {
  397. tmpField := strings.Trim(args[i], "\"")
  398. tmpField = trimhex(tmpField)
  399. argsCopy = append(argsCopy, strings.Fields(tmpField)...)
  400. }
  401. argset := sets.NewString(argsCopy...)
  402. for _, line := range strings.Split(string(out), "\n") {
  403. var fields = strings.Fields(line)
  404. // Check that this is a rule for the correct chain, and that it has
  405. // the correct number of argument (+2 for "-A <chain name>")
  406. if !strings.HasPrefix(line, fmt.Sprintf("-A %s", string(chain))) || len(fields) != len(argsCopy)+2 {
  407. continue
  408. }
  409. // Sadly, iptables has inconsistent quoting rules for comments.
  410. // Just remove all quotes.
  411. for i := range fields {
  412. fields[i] = strings.Trim(fields[i], "\"")
  413. fields[i] = trimhex(fields[i])
  414. }
  415. // TODO: This misses reorderings e.g. "-x foo ! -y bar" will match "! -x foo -y bar"
  416. if sets.NewString(fields...).IsSuperset(argset) {
  417. return true, nil
  418. }
  419. klog.V(5).Infof("DBG: fields is not a superset of args: fields=%v args=%v", fields, args)
  420. }
  421. return false, nil
  422. }
  423. // Executes the rule check using the "-C" flag
  424. func (runner *runner) checkRuleUsingCheck(args []string) (bool, error) {
  425. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
  426. defer cancel()
  427. out, err := runner.runContext(ctx, opCheckRule, args)
  428. if ctx.Err() == context.DeadlineExceeded {
  429. return false, fmt.Errorf("timed out while checking rules")
  430. }
  431. if err == nil {
  432. return true, nil
  433. }
  434. if ee, ok := err.(utilexec.ExitError); ok {
  435. // iptables uses exit(1) to indicate a failure of the operation,
  436. // as compared to a malformed commandline, for example.
  437. if ee.Exited() && ee.ExitStatus() == 1 {
  438. return false, nil
  439. }
  440. }
  441. return false, fmt.Errorf("error checking rule: %v: %s", err, out)
  442. }
  443. type operation string
  444. const (
  445. opCreateChain operation = "-N"
  446. opFlushChain operation = "-F"
  447. opDeleteChain operation = "-X"
  448. opAppendRule operation = "-A"
  449. opCheckRule operation = "-C"
  450. opDeleteRule operation = "-D"
  451. )
  452. func makeFullArgs(table Table, chain Chain, args ...string) []string {
  453. return append([]string{string(chain), "-t", string(table)}, args...)
  454. }
  455. // Checks if iptables has the "-C" flag
  456. func getIPTablesHasCheckCommand(vstring string) bool {
  457. minVersion, err := utilversion.ParseGeneric(MinCheckVersion)
  458. if err != nil {
  459. klog.Errorf("MinCheckVersion (%s) is not a valid version string: %v", MinCheckVersion, err)
  460. return true
  461. }
  462. version, err := utilversion.ParseGeneric(vstring)
  463. if err != nil {
  464. klog.Errorf("vstring (%s) is not a valid version string: %v", vstring, err)
  465. return true
  466. }
  467. return version.AtLeast(minVersion)
  468. }
  469. // Checks if iptables version has a "wait" flag
  470. func getIPTablesWaitFlag(vstring string) []string {
  471. version, err := utilversion.ParseGeneric(vstring)
  472. if err != nil {
  473. klog.Errorf("vstring (%s) is not a valid version string: %v", vstring, err)
  474. return nil
  475. }
  476. minVersion, err := utilversion.ParseGeneric(WaitMinVersion)
  477. if err != nil {
  478. klog.Errorf("WaitMinVersion (%s) is not a valid version string: %v", WaitMinVersion, err)
  479. return nil
  480. }
  481. if version.LessThan(minVersion) {
  482. return nil
  483. }
  484. minVersion, err = utilversion.ParseGeneric(WaitSecondsMinVersion)
  485. if err != nil {
  486. klog.Errorf("WaitSecondsMinVersion (%s) is not a valid version string: %v", WaitSecondsMinVersion, err)
  487. return nil
  488. }
  489. if version.LessThan(minVersion) {
  490. return []string{WaitString}
  491. }
  492. return []string{WaitString, WaitSecondsValue}
  493. }
  494. // getIPTablesVersionString runs "iptables --version" to get the version string
  495. // in the form "X.X.X"
  496. func getIPTablesVersionString(exec utilexec.Interface, protocol Protocol) (string, error) {
  497. // this doesn't access mutable state so we don't need to use the interface / runner
  498. iptablesCmd := iptablesCommand(protocol)
  499. bytes, err := exec.Command(iptablesCmd, "--version").CombinedOutput()
  500. if err != nil {
  501. return "", err
  502. }
  503. versionMatcher := regexp.MustCompile("v([0-9]+(\\.[0-9]+)+)")
  504. match := versionMatcher.FindStringSubmatch(string(bytes))
  505. if match == nil {
  506. return "", fmt.Errorf("no iptables version found in string: %s", bytes)
  507. }
  508. return match[1], nil
  509. }
  510. // Checks if iptables-restore has a "wait" flag
  511. // --wait support landed in v1.6.1+ right before --version support, so
  512. // any version of iptables-restore that supports --version will also
  513. // support --wait
  514. func getIPTablesRestoreWaitFlag(exec utilexec.Interface, protocol Protocol) []string {
  515. vstring, err := getIPTablesRestoreVersionString(exec, protocol)
  516. if err != nil || vstring == "" {
  517. klog.V(3).Infof("couldn't get iptables-restore version; assuming it doesn't support --wait")
  518. return nil
  519. }
  520. if _, err := utilversion.ParseGeneric(vstring); err != nil {
  521. klog.V(3).Infof("couldn't parse iptables-restore version; assuming it doesn't support --wait")
  522. return nil
  523. }
  524. return []string{WaitString, WaitSecondsValue}
  525. }
  526. // getIPTablesRestoreVersionString runs "iptables-restore --version" to get the version string
  527. // in the form "X.X.X"
  528. func getIPTablesRestoreVersionString(exec utilexec.Interface, protocol Protocol) (string, error) {
  529. // this doesn't access mutable state so we don't need to use the interface / runner
  530. // iptables-restore hasn't always had --version, and worse complains
  531. // about unrecognized commands but doesn't exit when it gets them.
  532. // Work around that by setting stdin to nothing so it exits immediately.
  533. iptablesRestoreCmd := iptablesRestoreCommand(protocol)
  534. cmd := exec.Command(iptablesRestoreCmd, "--version")
  535. cmd.SetStdin(bytes.NewReader([]byte{}))
  536. bytes, err := cmd.CombinedOutput()
  537. if err != nil {
  538. return "", err
  539. }
  540. versionMatcher := regexp.MustCompile("v([0-9]+(\\.[0-9]+)+)")
  541. match := versionMatcher.FindStringSubmatch(string(bytes))
  542. if match == nil {
  543. return "", fmt.Errorf("no iptables version found in string: %s", bytes)
  544. }
  545. return match[1], nil
  546. }
  547. // goroutine to listen for D-Bus signals
  548. func (runner *runner) dbusSignalHandler(bus utildbus.Connection) {
  549. firewalld := bus.Object(firewalldName, firewalldPath)
  550. for s := range runner.signal {
  551. if s == nil {
  552. // Unregister
  553. bus.Signal(runner.signal)
  554. return
  555. }
  556. switch s.Name {
  557. case "org.freedesktop.DBus.NameOwnerChanged":
  558. name := s.Body[0].(string)
  559. newOwner := s.Body[2].(string)
  560. if name != firewalldName || len(newOwner) == 0 {
  561. continue
  562. }
  563. // FirewallD startup (specifically the part where it deletes
  564. // all existing iptables rules) may not yet be complete when
  565. // we get this signal, so make a dummy request to it to
  566. // synchronize.
  567. firewalld.Call(firewalldInterface+".getDefaultZone", 0)
  568. runner.reload()
  569. case firewalldInterface + ".Reloaded":
  570. runner.reload()
  571. }
  572. }
  573. }
  574. // AddReloadFunc is part of Interface
  575. func (runner *runner) AddReloadFunc(reloadFunc func()) {
  576. runner.mu.Lock()
  577. defer runner.mu.Unlock()
  578. // We only need to listen to firewalld if there are Reload functions, so lazy
  579. // initialize the listener.
  580. if !runner.hasListener {
  581. runner.connectToFirewallD()
  582. }
  583. runner.reloadFuncs = append(runner.reloadFuncs, reloadFunc)
  584. }
  585. // runs all reload funcs to re-sync iptables rules
  586. func (runner *runner) reload() {
  587. klog.V(1).Infof("reloading iptables rules")
  588. for _, f := range runner.reloadFuncs {
  589. f()
  590. }
  591. }
  592. var iptablesNotFoundStrings = []string{
  593. // iptables-legacy [-A|-I] BAD-CHAIN [...]
  594. // iptables-legacy [-C|-D] GOOD-CHAIN [...non-matching rule...]
  595. // iptables-legacy [-X|-F|-Z] BAD-CHAIN
  596. // iptables-nft -X BAD-CHAIN
  597. // NB: iptables-nft [-F|-Z] BAD-CHAIN exits with no error
  598. "No chain/target/match by that name",
  599. // iptables-legacy [...] -j BAD-CHAIN
  600. // iptables-nft-1.8.0 [-A|-I] BAD-CHAIN [...]
  601. // iptables-nft-1.8.0 [-A|-I] GOOD-CHAIN -j BAD-CHAIN
  602. // NB: also matches some other things like "-m BAD-MODULE"
  603. "No such file or directory",
  604. // iptables-legacy [-C|-D] BAD-CHAIN [...]
  605. // iptables-nft [-C|-D] GOOD-CHAIN [...non-matching rule...]
  606. "does a matching rule exist",
  607. // iptables-nft-1.8.2 [-A|-C|-D|-I] BAD-CHAIN [...]
  608. // iptables-nft-1.8.2 [...] -j BAD-CHAIN
  609. "does not exist",
  610. }
  611. // IsNotFoundError returns true if the error indicates "not found". It parses
  612. // the error string looking for known values, which is imperfect; beware using
  613. // this function for anything beyond deciding between logging or ignoring an
  614. // error.
  615. func IsNotFoundError(err error) bool {
  616. es := err.Error()
  617. for _, str := range iptablesNotFoundStrings {
  618. if strings.Contains(es, str) {
  619. return true
  620. }
  621. }
  622. return false
  623. }