iptables.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. "k8s.io/apimachinery/pkg/util/sets"
  23. utilversion "k8s.io/apimachinery/pkg/util/version"
  24. utilwait "k8s.io/apimachinery/pkg/util/wait"
  25. "k8s.io/klog"
  26. utilexec "k8s.io/utils/exec"
  27. utiltrace "k8s.io/utils/trace"
  28. )
  29. // RulePosition holds the -I/-A flags for iptable
  30. type RulePosition string
  31. const (
  32. // Prepend is the insert flag for iptable
  33. Prepend RulePosition = "-I"
  34. // Append is the append flag for iptable
  35. Append RulePosition = "-A"
  36. )
  37. // Interface is an injectable interface for running iptables commands. Implementations must be goroutine-safe.
  38. type Interface interface {
  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. // Monitor detects when the given iptables tables have been flushed by an external
  62. // tool (e.g. a firewall reload) by creating canary chains and polling to see if
  63. // they have been deleted. (Specifically, it polls tables[0] every interval until
  64. // the canary has been deleted from there, then waits a short additional time for
  65. // the canaries to be deleted from the remaining tables as well. You can optimize
  66. // the polling by listing a relatively empty table in tables[0]). When a flush is
  67. // detected, this calls the reloadFunc so the caller can reload their own iptables
  68. // rules. If it is unable to create the canary chains (either initially or after
  69. // a reload) it will log an error and stop monitoring.
  70. // (This function should be called from a goroutine.)
  71. Monitor(canary Chain, tables []Table, reloadFunc func(), interval time.Duration, stopCh <-chan struct{})
  72. // HasRandomFully reveals whether `-j MASQUERADE` takes the
  73. // `--random-fully` option. This is helpful to work around a
  74. // Linux kernel bug that sometimes causes multiple flows to get
  75. // mapped to the same IP:PORT and consequently some suffer packet
  76. // drops.
  77. HasRandomFully() bool
  78. }
  79. // Protocol defines the ip protocol either ipv4 or ipv6
  80. type Protocol byte
  81. const (
  82. // ProtocolIpv4 represents ipv4 protocol in iptables
  83. ProtocolIpv4 Protocol = iota + 1
  84. // ProtocolIpv6 represents ipv6 protocol in iptables
  85. ProtocolIpv6
  86. )
  87. // Table represents different iptable like filter,nat, mangle and raw
  88. type Table string
  89. const (
  90. // TableNAT represents the built-in nat table
  91. TableNAT Table = "nat"
  92. // TableFilter represents the built-in filter table
  93. TableFilter Table = "filter"
  94. // TableMangle represents the built-in mangle table
  95. TableMangle Table = "mangle"
  96. )
  97. // Chain represents the different rules
  98. type Chain string
  99. const (
  100. // ChainPostrouting used for source NAT in nat table
  101. ChainPostrouting Chain = "POSTROUTING"
  102. // ChainPrerouting used for DNAT (destination NAT) in nat table
  103. ChainPrerouting Chain = "PREROUTING"
  104. // ChainOutput used for the packets going out from local
  105. ChainOutput Chain = "OUTPUT"
  106. // ChainInput used for incoming packets
  107. ChainInput Chain = "INPUT"
  108. // ChainForward used for the packets for another NIC
  109. ChainForward Chain = "FORWARD"
  110. )
  111. const (
  112. cmdIPTablesSave string = "iptables-save"
  113. cmdIPTablesRestore string = "iptables-restore"
  114. cmdIPTables string = "iptables"
  115. cmdIP6TablesRestore string = "ip6tables-restore"
  116. cmdIP6TablesSave string = "ip6tables-save"
  117. cmdIP6Tables string = "ip6tables"
  118. )
  119. // RestoreCountersFlag is an option flag for Restore
  120. type RestoreCountersFlag bool
  121. // RestoreCounters a boolean true constant for the option flag RestoreCountersFlag
  122. const RestoreCounters RestoreCountersFlag = true
  123. // NoRestoreCounters a boolean false constant for the option flag RestoreCountersFlag
  124. const NoRestoreCounters RestoreCountersFlag = false
  125. // FlushFlag an option flag for Flush
  126. type FlushFlag bool
  127. // FlushTables a boolean true constant for option flag FlushFlag
  128. const FlushTables FlushFlag = true
  129. // NoFlushTables a boolean false constant for option flag FlushFlag
  130. const NoFlushTables FlushFlag = false
  131. // MinCheckVersion minimum version to be checked
  132. // Versions of iptables less than this do not support the -C / --check flag
  133. // (test whether a rule exists).
  134. var MinCheckVersion = utilversion.MustParseGeneric("1.4.11")
  135. // RandomFullyMinVersion is the minimum version from which the --random-fully flag is supported,
  136. // used for port mapping to be fully randomized
  137. var RandomFullyMinVersion = utilversion.MustParseGeneric("1.6.2")
  138. // WaitMinVersion a minimum iptables versions supporting the -w and -w<seconds> flags
  139. var WaitMinVersion = utilversion.MustParseGeneric("1.4.20")
  140. // WaitIntervalMinVersion a minimum iptables versions supporting the wait interval useconds
  141. var WaitIntervalMinVersion = utilversion.MustParseGeneric("1.6.1")
  142. // WaitSecondsMinVersion a minimum iptables versions supporting the wait seconds
  143. var WaitSecondsMinVersion = utilversion.MustParseGeneric("1.4.22")
  144. // WaitRestoreMinVersion a minimum iptables versions supporting the wait restore seconds
  145. var WaitRestoreMinVersion = utilversion.MustParseGeneric("1.6.2")
  146. // WaitString a constant for specifying the wait flag
  147. const WaitString = "-w"
  148. // WaitSecondsValue a constant for specifying the default wait seconds
  149. const WaitSecondsValue = "5"
  150. // WaitIntervalString a constant for specifying the wait interval flag
  151. const WaitIntervalString = "-W"
  152. // WaitIntervalUsecondsValue a constant for specifying the default wait interval useconds
  153. const WaitIntervalUsecondsValue = "100000"
  154. // LockfilePath16x is the iptables lock file acquired by any process that's making any change in the iptable rule
  155. const LockfilePath16x = "/run/xtables.lock"
  156. // runner implements Interface in terms of exec("iptables").
  157. type runner struct {
  158. mu sync.Mutex
  159. exec utilexec.Interface
  160. protocol Protocol
  161. hasCheck bool
  162. hasRandomFully bool
  163. waitFlag []string
  164. restoreWaitFlag []string
  165. lockfilePath string
  166. }
  167. // newInternal returns a new Interface which will exec iptables, and allows the
  168. // caller to change the iptables-restore lockfile path
  169. func newInternal(exec utilexec.Interface, protocol Protocol, lockfilePath string) Interface {
  170. version, err := getIPTablesVersion(exec, protocol)
  171. if err != nil {
  172. klog.Warningf("Error checking iptables version, assuming version at least %s: %v", MinCheckVersion, err)
  173. version = MinCheckVersion
  174. }
  175. if lockfilePath == "" {
  176. lockfilePath = LockfilePath16x
  177. }
  178. runner := &runner{
  179. exec: exec,
  180. protocol: protocol,
  181. hasCheck: version.AtLeast(MinCheckVersion),
  182. hasRandomFully: version.AtLeast(RandomFullyMinVersion),
  183. waitFlag: getIPTablesWaitFlag(version),
  184. restoreWaitFlag: getIPTablesRestoreWaitFlag(version, exec, protocol),
  185. lockfilePath: lockfilePath,
  186. }
  187. return runner
  188. }
  189. // New returns a new Interface which will exec iptables.
  190. func New(exec utilexec.Interface, protocol Protocol) Interface {
  191. return newInternal(exec, protocol, "")
  192. }
  193. // EnsureChain is part of Interface.
  194. func (runner *runner) EnsureChain(table Table, chain Chain) (bool, error) {
  195. fullArgs := makeFullArgs(table, chain)
  196. runner.mu.Lock()
  197. defer runner.mu.Unlock()
  198. out, err := runner.run(opCreateChain, fullArgs)
  199. if err != nil {
  200. if ee, ok := err.(utilexec.ExitError); ok {
  201. if ee.Exited() && ee.ExitStatus() == 1 {
  202. return true, nil
  203. }
  204. }
  205. return false, fmt.Errorf("error creating chain %q: %v: %s", chain, err, out)
  206. }
  207. return false, nil
  208. }
  209. // FlushChain is part of Interface.
  210. func (runner *runner) FlushChain(table Table, chain Chain) error {
  211. fullArgs := makeFullArgs(table, chain)
  212. runner.mu.Lock()
  213. defer runner.mu.Unlock()
  214. out, err := runner.run(opFlushChain, fullArgs)
  215. if err != nil {
  216. return fmt.Errorf("error flushing chain %q: %v: %s", chain, err, out)
  217. }
  218. return nil
  219. }
  220. // DeleteChain is part of Interface.
  221. func (runner *runner) DeleteChain(table Table, chain Chain) error {
  222. fullArgs := makeFullArgs(table, chain)
  223. runner.mu.Lock()
  224. defer runner.mu.Unlock()
  225. // TODO: we could call iptables -S first, ignore the output and check for non-zero return (more like DeleteRule)
  226. out, err := runner.run(opDeleteChain, fullArgs)
  227. if err != nil {
  228. return fmt.Errorf("error deleting chain %q: %v: %s", chain, err, out)
  229. }
  230. return nil
  231. }
  232. // EnsureRule is part of Interface.
  233. func (runner *runner) EnsureRule(position RulePosition, table Table, chain Chain, args ...string) (bool, error) {
  234. fullArgs := makeFullArgs(table, chain, args...)
  235. runner.mu.Lock()
  236. defer runner.mu.Unlock()
  237. exists, err := runner.checkRule(table, chain, args...)
  238. if err != nil {
  239. return false, err
  240. }
  241. if exists {
  242. return true, nil
  243. }
  244. out, err := runner.run(operation(position), fullArgs)
  245. if err != nil {
  246. return false, fmt.Errorf("error appending rule: %v: %s", err, out)
  247. }
  248. return false, nil
  249. }
  250. // DeleteRule is part of Interface.
  251. func (runner *runner) DeleteRule(table Table, chain Chain, args ...string) error {
  252. fullArgs := makeFullArgs(table, chain, args...)
  253. runner.mu.Lock()
  254. defer runner.mu.Unlock()
  255. exists, err := runner.checkRule(table, chain, args...)
  256. if err != nil {
  257. return err
  258. }
  259. if !exists {
  260. return nil
  261. }
  262. out, err := runner.run(opDeleteRule, fullArgs)
  263. if err != nil {
  264. return fmt.Errorf("error deleting rule: %v: %s", err, out)
  265. }
  266. return nil
  267. }
  268. func (runner *runner) IsIpv6() bool {
  269. return runner.protocol == ProtocolIpv6
  270. }
  271. // SaveInto is part of Interface.
  272. func (runner *runner) SaveInto(table Table, buffer *bytes.Buffer) error {
  273. runner.mu.Lock()
  274. defer runner.mu.Unlock()
  275. trace := utiltrace.New("iptables save")
  276. defer trace.LogIfLong(2 * time.Second)
  277. // run and return
  278. iptablesSaveCmd := iptablesSaveCommand(runner.protocol)
  279. args := []string{"-t", string(table)}
  280. klog.V(4).Infof("running %s %v", iptablesSaveCmd, args)
  281. cmd := runner.exec.Command(iptablesSaveCmd, args...)
  282. cmd.SetStdout(buffer)
  283. stderrBuffer := bytes.NewBuffer(nil)
  284. cmd.SetStderr(stderrBuffer)
  285. err := cmd.Run()
  286. if err != nil {
  287. stderrBuffer.WriteTo(buffer) // ignore error, since we need to return the original error
  288. }
  289. return err
  290. }
  291. // Restore is part of Interface.
  292. func (runner *runner) Restore(table Table, data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
  293. // setup args
  294. args := []string{"-T", string(table)}
  295. return runner.restoreInternal(args, data, flush, counters)
  296. }
  297. // RestoreAll is part of Interface.
  298. func (runner *runner) RestoreAll(data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
  299. // setup args
  300. args := make([]string, 0)
  301. return runner.restoreInternal(args, data, flush, counters)
  302. }
  303. type iptablesLocker interface {
  304. Close() error
  305. }
  306. // restoreInternal is the shared part of Restore/RestoreAll
  307. func (runner *runner) restoreInternal(args []string, data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
  308. runner.mu.Lock()
  309. defer runner.mu.Unlock()
  310. trace := utiltrace.New("iptables restore")
  311. defer trace.LogIfLong(2 * time.Second)
  312. if !flush {
  313. args = append(args, "--noflush")
  314. }
  315. if counters {
  316. args = append(args, "--counters")
  317. }
  318. // Grab the iptables lock to prevent iptables-restore and iptables
  319. // from stepping on each other. iptables-restore 1.6.2 will have
  320. // a --wait option like iptables itself, but that's not widely deployed.
  321. if len(runner.restoreWaitFlag) == 0 {
  322. locker, err := grabIptablesLocks(runner.lockfilePath)
  323. if err != nil {
  324. return err
  325. }
  326. trace.Step("Locks grabbed")
  327. defer func(locker iptablesLocker) {
  328. if err := locker.Close(); err != nil {
  329. klog.Errorf("Failed to close iptables locks: %v", err)
  330. }
  331. }(locker)
  332. }
  333. // run the command and return the output or an error including the output and error
  334. fullArgs := append(runner.restoreWaitFlag, args...)
  335. iptablesRestoreCmd := iptablesRestoreCommand(runner.protocol)
  336. klog.V(4).Infof("running %s %v", iptablesRestoreCmd, fullArgs)
  337. cmd := runner.exec.Command(iptablesRestoreCmd, fullArgs...)
  338. cmd.SetStdin(bytes.NewBuffer(data))
  339. b, err := cmd.CombinedOutput()
  340. if err != nil {
  341. return fmt.Errorf("%v (%s)", err, b)
  342. }
  343. return nil
  344. }
  345. func iptablesSaveCommand(protocol Protocol) string {
  346. if protocol == ProtocolIpv6 {
  347. return cmdIP6TablesSave
  348. }
  349. return cmdIPTablesSave
  350. }
  351. func iptablesRestoreCommand(protocol Protocol) string {
  352. if protocol == ProtocolIpv6 {
  353. return cmdIP6TablesRestore
  354. }
  355. return cmdIPTablesRestore
  356. }
  357. func iptablesCommand(protocol Protocol) string {
  358. if protocol == ProtocolIpv6 {
  359. return cmdIP6Tables
  360. }
  361. return cmdIPTables
  362. }
  363. func (runner *runner) run(op operation, args []string) ([]byte, error) {
  364. return runner.runContext(context.TODO(), op, args)
  365. }
  366. func (runner *runner) runContext(ctx context.Context, op operation, args []string) ([]byte, error) {
  367. iptablesCmd := iptablesCommand(runner.protocol)
  368. fullArgs := append(runner.waitFlag, string(op))
  369. fullArgs = append(fullArgs, args...)
  370. klog.V(5).Infof("running iptables: %s %v", iptablesCmd, fullArgs)
  371. if ctx == nil {
  372. return runner.exec.Command(iptablesCmd, fullArgs...).CombinedOutput()
  373. }
  374. return runner.exec.CommandContext(ctx, iptablesCmd, fullArgs...).CombinedOutput()
  375. // Don't log err here - callers might not think it is an error.
  376. }
  377. // Returns (bool, nil) if it was able to check the existence of the rule, or
  378. // (<undefined>, error) if the process of checking failed.
  379. func (runner *runner) checkRule(table Table, chain Chain, args ...string) (bool, error) {
  380. if runner.hasCheck {
  381. return runner.checkRuleUsingCheck(makeFullArgs(table, chain, args...))
  382. }
  383. return runner.checkRuleWithoutCheck(table, chain, args...)
  384. }
  385. var hexnumRE = regexp.MustCompile("0x0+([0-9])")
  386. func trimhex(s string) string {
  387. return hexnumRE.ReplaceAllString(s, "0x$1")
  388. }
  389. // Executes the rule check without using the "-C" flag, instead parsing iptables-save.
  390. // Present for compatibility with <1.4.11 versions of iptables. This is full
  391. // of hack and half-measures. We should nix this ASAP.
  392. func (runner *runner) checkRuleWithoutCheck(table Table, chain Chain, args ...string) (bool, error) {
  393. iptablesSaveCmd := iptablesSaveCommand(runner.protocol)
  394. klog.V(1).Infof("running %s -t %s", iptablesSaveCmd, string(table))
  395. out, err := runner.exec.Command(iptablesSaveCmd, "-t", string(table)).CombinedOutput()
  396. if err != nil {
  397. return false, fmt.Errorf("error checking rule: %v", err)
  398. }
  399. // Sadly, iptables has inconsistent quoting rules for comments. Just remove all quotes.
  400. // Also, quoted multi-word comments (which are counted as a single arg)
  401. // will be unpacked into multiple args,
  402. // in order to compare against iptables-save output (which will be split at whitespace boundary)
  403. // e.g. a single arg('"this must be before the NodePort rules"') will be unquoted and unpacked into 7 args.
  404. var argsCopy []string
  405. for i := range args {
  406. tmpField := strings.Trim(args[i], "\"")
  407. tmpField = trimhex(tmpField)
  408. argsCopy = append(argsCopy, strings.Fields(tmpField)...)
  409. }
  410. argset := sets.NewString(argsCopy...)
  411. for _, line := range strings.Split(string(out), "\n") {
  412. var fields = strings.Fields(line)
  413. // Check that this is a rule for the correct chain, and that it has
  414. // the correct number of argument (+2 for "-A <chain name>")
  415. if !strings.HasPrefix(line, fmt.Sprintf("-A %s", string(chain))) || len(fields) != len(argsCopy)+2 {
  416. continue
  417. }
  418. // Sadly, iptables has inconsistent quoting rules for comments.
  419. // Just remove all quotes.
  420. for i := range fields {
  421. fields[i] = strings.Trim(fields[i], "\"")
  422. fields[i] = trimhex(fields[i])
  423. }
  424. // TODO: This misses reorderings e.g. "-x foo ! -y bar" will match "! -x foo -y bar"
  425. if sets.NewString(fields...).IsSuperset(argset) {
  426. return true, nil
  427. }
  428. klog.V(5).Infof("DBG: fields is not a superset of args: fields=%v args=%v", fields, args)
  429. }
  430. return false, nil
  431. }
  432. // Executes the rule check using the "-C" flag
  433. func (runner *runner) checkRuleUsingCheck(args []string) (bool, error) {
  434. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
  435. defer cancel()
  436. out, err := runner.runContext(ctx, opCheckRule, args)
  437. if ctx.Err() == context.DeadlineExceeded {
  438. return false, fmt.Errorf("timed out while checking rules")
  439. }
  440. if err == nil {
  441. return true, nil
  442. }
  443. if ee, ok := err.(utilexec.ExitError); ok {
  444. // iptables uses exit(1) to indicate a failure of the operation,
  445. // as compared to a malformed commandline, for example.
  446. if ee.Exited() && ee.ExitStatus() == 1 {
  447. return false, nil
  448. }
  449. }
  450. return false, fmt.Errorf("error checking rule: %v: %s", err, out)
  451. }
  452. const (
  453. // Max time we wait for an iptables flush to complete after we notice it has started
  454. iptablesFlushTimeout = 5 * time.Second
  455. // How often we poll while waiting for an iptables flush to complete
  456. iptablesFlushPollTime = 100 * time.Millisecond
  457. )
  458. // Monitor is part of Interface
  459. func (runner *runner) Monitor(canary Chain, tables []Table, reloadFunc func(), interval time.Duration, stopCh <-chan struct{}) {
  460. for {
  461. _ = utilwait.PollImmediateUntil(interval, func() (bool, error) {
  462. for _, table := range tables {
  463. if _, err := runner.EnsureChain(table, canary); err != nil {
  464. klog.Warningf("Could not set up iptables canary %s/%s: %v", string(table), string(canary), err)
  465. return false, nil
  466. }
  467. }
  468. return true, nil
  469. }, stopCh)
  470. // Poll until stopCh is closed or iptables is flushed
  471. err := utilwait.PollUntil(interval, func() (bool, error) {
  472. if exists, err := runner.chainExists(tables[0], canary); exists {
  473. return false, nil
  474. } else if isResourceError(err) {
  475. klog.Warningf("Could not check for iptables canary %s/%s: %v", string(tables[0]), string(canary), err)
  476. return false, nil
  477. }
  478. klog.V(2).Infof("iptables canary %s/%s deleted", string(tables[0]), string(canary))
  479. // Wait for the other canaries to be deleted too before returning
  480. // so we don't start reloading too soon.
  481. err := utilwait.PollImmediate(iptablesFlushPollTime, iptablesFlushTimeout, func() (bool, error) {
  482. for i := 1; i < len(tables); i++ {
  483. if exists, err := runner.chainExists(tables[i], canary); exists || isResourceError(err) {
  484. return false, nil
  485. }
  486. }
  487. return true, nil
  488. })
  489. if err != nil {
  490. klog.Warning("Inconsistent iptables state detected.")
  491. }
  492. return true, nil
  493. }, stopCh)
  494. if err != nil {
  495. // stopCh was closed
  496. for _, table := range tables {
  497. _ = runner.DeleteChain(table, canary)
  498. }
  499. return
  500. }
  501. klog.V(2).Infof("Reloading after iptables flush")
  502. reloadFunc()
  503. }
  504. }
  505. // chainExists is used internally by Monitor; none of the public Interface methods can be
  506. // used to distinguish "chain exists" from "chain does not exist" with no side effects
  507. func (runner *runner) chainExists(table Table, chain Chain) (bool, error) {
  508. fullArgs := makeFullArgs(table, chain)
  509. runner.mu.Lock()
  510. defer runner.mu.Unlock()
  511. _, err := runner.run(opListChain, fullArgs)
  512. return err == nil, err
  513. }
  514. type operation string
  515. const (
  516. opCreateChain operation = "-N"
  517. opFlushChain operation = "-F"
  518. opDeleteChain operation = "-X"
  519. opListChain operation = "-L"
  520. opAppendRule operation = "-A"
  521. opCheckRule operation = "-C"
  522. opDeleteRule operation = "-D"
  523. )
  524. func makeFullArgs(table Table, chain Chain, args ...string) []string {
  525. return append([]string{string(chain), "-t", string(table)}, args...)
  526. }
  527. const iptablesVersionPattern = `v([0-9]+(\.[0-9]+)+)`
  528. // getIPTablesVersion runs "iptables --version" and parses the returned version
  529. func getIPTablesVersion(exec utilexec.Interface, protocol Protocol) (*utilversion.Version, error) {
  530. // this doesn't access mutable state so we don't need to use the interface / runner
  531. iptablesCmd := iptablesCommand(protocol)
  532. bytes, err := exec.Command(iptablesCmd, "--version").CombinedOutput()
  533. if err != nil {
  534. return nil, err
  535. }
  536. versionMatcher := regexp.MustCompile(iptablesVersionPattern)
  537. match := versionMatcher.FindStringSubmatch(string(bytes))
  538. if match == nil {
  539. return nil, fmt.Errorf("no iptables version found in string: %s", bytes)
  540. }
  541. version, err := utilversion.ParseGeneric(match[1])
  542. if err != nil {
  543. return nil, fmt.Errorf("iptables version %q is not a valid version string: %v", match[1], err)
  544. }
  545. return version, nil
  546. }
  547. // Checks if iptables version has a "wait" flag
  548. func getIPTablesWaitFlag(version *utilversion.Version) []string {
  549. switch {
  550. case version.AtLeast(WaitIntervalMinVersion):
  551. return []string{WaitString, WaitSecondsValue, WaitIntervalString, WaitIntervalUsecondsValue}
  552. case version.AtLeast(WaitSecondsMinVersion):
  553. return []string{WaitString, WaitSecondsValue}
  554. case version.AtLeast(WaitMinVersion):
  555. return []string{WaitString}
  556. default:
  557. return nil
  558. }
  559. }
  560. // Checks if iptables-restore has a "wait" flag
  561. func getIPTablesRestoreWaitFlag(version *utilversion.Version, exec utilexec.Interface, protocol Protocol) []string {
  562. if version.AtLeast(WaitRestoreMinVersion) {
  563. return []string{WaitString, WaitSecondsValue, WaitIntervalString, WaitIntervalUsecondsValue}
  564. }
  565. // Older versions may have backported features; if iptables-restore supports
  566. // --version, assume it also supports --wait
  567. vstring, err := getIPTablesRestoreVersionString(exec, protocol)
  568. if err != nil || vstring == "" {
  569. klog.V(3).Infof("couldn't get iptables-restore version; assuming it doesn't support --wait")
  570. return nil
  571. }
  572. if _, err := utilversion.ParseGeneric(vstring); err != nil {
  573. klog.V(3).Infof("couldn't parse iptables-restore version; assuming it doesn't support --wait")
  574. return nil
  575. }
  576. return []string{WaitString}
  577. }
  578. // getIPTablesRestoreVersionString runs "iptables-restore --version" to get the version string
  579. // in the form "X.X.X"
  580. func getIPTablesRestoreVersionString(exec utilexec.Interface, protocol Protocol) (string, error) {
  581. // this doesn't access mutable state so we don't need to use the interface / runner
  582. // iptables-restore hasn't always had --version, and worse complains
  583. // about unrecognized commands but doesn't exit when it gets them.
  584. // Work around that by setting stdin to nothing so it exits immediately.
  585. iptablesRestoreCmd := iptablesRestoreCommand(protocol)
  586. cmd := exec.Command(iptablesRestoreCmd, "--version")
  587. cmd.SetStdin(bytes.NewReader([]byte{}))
  588. bytes, err := cmd.CombinedOutput()
  589. if err != nil {
  590. return "", err
  591. }
  592. versionMatcher := regexp.MustCompile(iptablesVersionPattern)
  593. match := versionMatcher.FindStringSubmatch(string(bytes))
  594. if match == nil {
  595. return "", fmt.Errorf("no iptables version found in string: %s", bytes)
  596. }
  597. return match[1], nil
  598. }
  599. func (runner *runner) HasRandomFully() bool {
  600. return runner.hasRandomFully
  601. }
  602. var iptablesNotFoundStrings = []string{
  603. // iptables-legacy [-A|-I] BAD-CHAIN [...]
  604. // iptables-legacy [-C|-D] GOOD-CHAIN [...non-matching rule...]
  605. // iptables-legacy [-X|-F|-Z] BAD-CHAIN
  606. // iptables-nft -X BAD-CHAIN
  607. // NB: iptables-nft [-F|-Z] BAD-CHAIN exits with no error
  608. "No chain/target/match by that name",
  609. // iptables-legacy [...] -j BAD-CHAIN
  610. // iptables-nft-1.8.0 [-A|-I] BAD-CHAIN [...]
  611. // iptables-nft-1.8.0 [-A|-I] GOOD-CHAIN -j BAD-CHAIN
  612. // NB: also matches some other things like "-m BAD-MODULE"
  613. "No such file or directory",
  614. // iptables-legacy [-C|-D] BAD-CHAIN [...]
  615. // iptables-nft [-C|-D] GOOD-CHAIN [...non-matching rule...]
  616. "does a matching rule exist",
  617. // iptables-nft-1.8.2 [-A|-C|-D|-I] BAD-CHAIN [...]
  618. // iptables-nft-1.8.2 [...] -j BAD-CHAIN
  619. "does not exist",
  620. }
  621. // IsNotFoundError returns true if the error indicates "not found". It parses
  622. // the error string looking for known values, which is imperfect; beware using
  623. // this function for anything beyond deciding between logging or ignoring an
  624. // error.
  625. func IsNotFoundError(err error) bool {
  626. es := err.Error()
  627. for _, str := range iptablesNotFoundStrings {
  628. if strings.Contains(es, str) {
  629. return true
  630. }
  631. }
  632. return false
  633. }
  634. const iptablesStatusResourceProblem = 4
  635. // isResourceError returns true if the error indicates that iptables ran into a "resource
  636. // problem" and was unable to attempt the request. In particular, this will be true if it
  637. // times out trying to get the iptables lock.
  638. func isResourceError(err error) bool {
  639. if ee, isExitError := err.(utilexec.ExitError); isExitError {
  640. return ee.ExitStatus() == iptablesStatusResourceProblem
  641. }
  642. return false
  643. }