iptables.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. "fmt"
  17. "regexp"
  18. "strings"
  19. "sync"
  20. "github.com/coreos/go-semver/semver"
  21. godbus "github.com/godbus/dbus"
  22. "github.com/golang/glog"
  23. utildbus "k8s.io/kubernetes/pkg/util/dbus"
  24. utilexec "k8s.io/kubernetes/pkg/util/exec"
  25. "k8s.io/kubernetes/pkg/util/sets"
  26. )
  27. type RulePosition string
  28. const (
  29. Prepend RulePosition = "-I"
  30. Append RulePosition = "-A"
  31. )
  32. // An injectable interface for running iptables commands. Implementations must be goroutine-safe.
  33. type Interface interface {
  34. // GetVersion returns the "X.Y.Z" semver string for iptables.
  35. GetVersion() (string, error)
  36. // EnsureChain checks if the specified chain exists and, if not, creates it. If the chain existed, return true.
  37. EnsureChain(table Table, chain Chain) (bool, error)
  38. // FlushChain clears the specified chain. If the chain did not exist, return error.
  39. FlushChain(table Table, chain Chain) error
  40. // DeleteChain deletes the specified chain. If the chain did not exist, return error.
  41. DeleteChain(table Table, chain Chain) error
  42. // EnsureRule checks if the specified rule is present and, if not, creates it. If the rule existed, return true.
  43. EnsureRule(position RulePosition, table Table, chain Chain, args ...string) (bool, error)
  44. // DeleteRule checks if the specified rule is present and, if so, deletes it.
  45. DeleteRule(table Table, chain Chain, args ...string) error
  46. // IsIpv6 returns true if this is managing ipv6 tables
  47. IsIpv6() bool
  48. // TODO: (BenTheElder) Unit-Test Save/SaveAll, Restore/RestoreAll
  49. // Save calls `iptables-save` for table.
  50. Save(table Table) ([]byte, error)
  51. // SaveAll calls `iptables-save`.
  52. SaveAll() ([]byte, 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 Save()
  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. )
  76. type Chain string
  77. const (
  78. ChainPostrouting Chain = "POSTROUTING"
  79. ChainPrerouting Chain = "PREROUTING"
  80. ChainOutput Chain = "OUTPUT"
  81. ChainInput Chain = "INPUT"
  82. )
  83. const (
  84. cmdIptablesSave string = "iptables-save"
  85. cmdIptablesRestore string = "iptables-restore"
  86. cmdIptables string = "iptables"
  87. cmdIp6tables string = "ip6tables"
  88. )
  89. // Option flag for Restore
  90. type RestoreCountersFlag bool
  91. const RestoreCounters RestoreCountersFlag = true
  92. const NoRestoreCounters RestoreCountersFlag = false
  93. // Option flag for Flush
  94. type FlushFlag bool
  95. const FlushTables FlushFlag = true
  96. const NoFlushTables FlushFlag = false
  97. // Versions of iptables less than this do not support the -C / --check flag
  98. // (test whether a rule exists).
  99. const MinCheckVersion = "1.4.11"
  100. // Minimum iptables versions supporting the -w and -w2 flags
  101. const MinWaitVersion = "1.4.20"
  102. const MinWait2Version = "1.4.22"
  103. // runner implements Interface in terms of exec("iptables").
  104. type runner struct {
  105. mu sync.Mutex
  106. exec utilexec.Interface
  107. dbus utildbus.Interface
  108. protocol Protocol
  109. hasCheck bool
  110. waitFlag []string
  111. reloadFuncs []func()
  112. signal chan *godbus.Signal
  113. }
  114. // New returns a new Interface which will exec iptables.
  115. func New(exec utilexec.Interface, dbus utildbus.Interface, protocol Protocol) Interface {
  116. vstring, err := getIptablesVersionString(exec)
  117. if err != nil {
  118. glog.Warningf("Error checking iptables version, assuming version at least %s: %v", MinCheckVersion, err)
  119. vstring = MinCheckVersion
  120. }
  121. runner := &runner{
  122. exec: exec,
  123. dbus: dbus,
  124. protocol: protocol,
  125. hasCheck: getIptablesHasCheckCommand(vstring),
  126. waitFlag: getIptablesWaitFlag(vstring),
  127. }
  128. runner.connectToFirewallD()
  129. return runner
  130. }
  131. // Destroy is part of Interface.
  132. func (runner *runner) Destroy() {
  133. if runner.signal != nil {
  134. runner.signal <- nil
  135. }
  136. }
  137. const (
  138. firewalldName = "org.fedoraproject.FirewallD1"
  139. firewalldPath = "/org/fedoraproject/FirewallD1"
  140. firewalldInterface = "org.fedoraproject.FirewallD1"
  141. )
  142. // Connects to D-Bus and listens for FirewallD start/restart. (On non-FirewallD-using
  143. // systems, this is effectively a no-op; we listen for the signals, but they will never be
  144. // emitted, so reload() will never be called.)
  145. func (runner *runner) connectToFirewallD() {
  146. bus, err := runner.dbus.SystemBus()
  147. if err != nil {
  148. glog.V(1).Infof("Could not connect to D-Bus system bus: %s", err)
  149. return
  150. }
  151. rule := fmt.Sprintf("type='signal',sender='%s',path='%s',interface='%s',member='Reloaded'", firewalldName, firewalldPath, firewalldInterface)
  152. bus.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
  153. rule = fmt.Sprintf("type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged',path='/org/freedesktop/DBus',sender='org.freedesktop.DBus',arg0='%s'", firewalldName)
  154. bus.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, rule)
  155. runner.signal = make(chan *godbus.Signal, 10)
  156. bus.Signal(runner.signal)
  157. go runner.dbusSignalHandler(bus)
  158. }
  159. // GetVersion returns the version string.
  160. func (runner *runner) GetVersion() (string, error) {
  161. return getIptablesVersionString(runner.exec)
  162. }
  163. // EnsureChain is part of Interface.
  164. func (runner *runner) EnsureChain(table Table, chain Chain) (bool, error) {
  165. fullArgs := makeFullArgs(table, chain)
  166. runner.mu.Lock()
  167. defer runner.mu.Unlock()
  168. out, err := runner.run(opCreateChain, fullArgs)
  169. if err != nil {
  170. if ee, ok := err.(utilexec.ExitError); ok {
  171. if ee.Exited() && ee.ExitStatus() == 1 {
  172. return true, nil
  173. }
  174. }
  175. return false, fmt.Errorf("error creating chain %q: %v: %s", chain, err, out)
  176. }
  177. return false, nil
  178. }
  179. // FlushChain is part of Interface.
  180. func (runner *runner) FlushChain(table Table, chain Chain) error {
  181. fullArgs := makeFullArgs(table, chain)
  182. runner.mu.Lock()
  183. defer runner.mu.Unlock()
  184. out, err := runner.run(opFlushChain, fullArgs)
  185. if err != nil {
  186. return fmt.Errorf("error flushing chain %q: %v: %s", chain, err, out)
  187. }
  188. return nil
  189. }
  190. // DeleteChain is part of Interface.
  191. func (runner *runner) DeleteChain(table Table, chain Chain) error {
  192. fullArgs := makeFullArgs(table, chain)
  193. runner.mu.Lock()
  194. defer runner.mu.Unlock()
  195. // TODO: we could call iptables -S first, ignore the output and check for non-zero return (more like DeleteRule)
  196. out, err := runner.run(opDeleteChain, fullArgs)
  197. if err != nil {
  198. return fmt.Errorf("error deleting chain %q: %v: %s", chain, err, out)
  199. }
  200. return nil
  201. }
  202. // EnsureRule is part of Interface.
  203. func (runner *runner) EnsureRule(position RulePosition, table Table, chain Chain, args ...string) (bool, error) {
  204. fullArgs := makeFullArgs(table, chain, args...)
  205. runner.mu.Lock()
  206. defer runner.mu.Unlock()
  207. exists, err := runner.checkRule(table, chain, args...)
  208. if err != nil {
  209. return false, err
  210. }
  211. if exists {
  212. return true, nil
  213. }
  214. out, err := runner.run(operation(position), fullArgs)
  215. if err != nil {
  216. return false, fmt.Errorf("error appending rule: %v: %s", err, out)
  217. }
  218. return false, nil
  219. }
  220. // DeleteRule is part of Interface.
  221. func (runner *runner) DeleteRule(table Table, chain Chain, args ...string) error {
  222. fullArgs := makeFullArgs(table, chain, args...)
  223. runner.mu.Lock()
  224. defer runner.mu.Unlock()
  225. exists, err := runner.checkRule(table, chain, args...)
  226. if err != nil {
  227. return err
  228. }
  229. if !exists {
  230. return nil
  231. }
  232. out, err := runner.run(opDeleteRule, fullArgs)
  233. if err != nil {
  234. return fmt.Errorf("error deleting rule: %v: %s", err, out)
  235. }
  236. return nil
  237. }
  238. func (runner *runner) IsIpv6() bool {
  239. return runner.protocol == ProtocolIpv6
  240. }
  241. // Save is part of Interface.
  242. func (runner *runner) Save(table Table) ([]byte, error) {
  243. runner.mu.Lock()
  244. defer runner.mu.Unlock()
  245. // run and return
  246. args := []string{"-t", string(table)}
  247. glog.V(4).Infof("running iptables-save %v", args)
  248. return runner.exec.Command(cmdIptablesSave, args...).CombinedOutput()
  249. }
  250. // SaveAll is part of Interface.
  251. func (runner *runner) SaveAll() ([]byte, error) {
  252. runner.mu.Lock()
  253. defer runner.mu.Unlock()
  254. // run and return
  255. glog.V(4).Infof("running iptables-save")
  256. return runner.exec.Command(cmdIptablesSave, []string{}...).CombinedOutput()
  257. }
  258. // Restore is part of Interface.
  259. func (runner *runner) Restore(table Table, data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
  260. // setup args
  261. args := []string{"-T", string(table)}
  262. return runner.restoreInternal(args, data, flush, counters)
  263. }
  264. // RestoreAll is part of Interface.
  265. func (runner *runner) RestoreAll(data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
  266. // setup args
  267. args := make([]string, 0)
  268. return runner.restoreInternal(args, data, flush, counters)
  269. }
  270. // restoreInternal is the shared part of Restore/RestoreAll
  271. func (runner *runner) restoreInternal(args []string, data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
  272. runner.mu.Lock()
  273. defer runner.mu.Unlock()
  274. if !flush {
  275. args = append(args, "--noflush")
  276. }
  277. if counters {
  278. args = append(args, "--counters")
  279. }
  280. // run the command and return the output or an error including the output and error
  281. glog.V(4).Infof("running iptables-restore %v", args)
  282. cmd := runner.exec.Command(cmdIptablesRestore, args...)
  283. cmd.SetStdin(bytes.NewBuffer(data))
  284. b, err := cmd.CombinedOutput()
  285. if err != nil {
  286. return fmt.Errorf("%v (%s)", err, b)
  287. }
  288. return nil
  289. }
  290. func (runner *runner) iptablesCommand() string {
  291. if runner.IsIpv6() {
  292. return cmdIp6tables
  293. } else {
  294. return cmdIptables
  295. }
  296. }
  297. func (runner *runner) run(op operation, args []string) ([]byte, error) {
  298. iptablesCmd := runner.iptablesCommand()
  299. fullArgs := append(runner.waitFlag, string(op))
  300. fullArgs = append(fullArgs, args...)
  301. glog.V(4).Infof("running iptables %s %v", string(op), args)
  302. return runner.exec.Command(iptablesCmd, fullArgs...).CombinedOutput()
  303. // Don't log err here - callers might not think it is an error.
  304. }
  305. // Returns (bool, nil) if it was able to check the existence of the rule, or
  306. // (<undefined>, error) if the process of checking failed.
  307. func (runner *runner) checkRule(table Table, chain Chain, args ...string) (bool, error) {
  308. if runner.hasCheck {
  309. return runner.checkRuleUsingCheck(makeFullArgs(table, chain, args...))
  310. } else {
  311. return runner.checkRuleWithoutCheck(table, chain, args...)
  312. }
  313. }
  314. // Executes the rule check without using the "-C" flag, instead parsing iptables-save.
  315. // Present for compatibility with <1.4.11 versions of iptables. This is full
  316. // of hack and half-measures. We should nix this ASAP.
  317. func (runner *runner) checkRuleWithoutCheck(table Table, chain Chain, args ...string) (bool, error) {
  318. glog.V(1).Infof("running iptables-save -t %s", string(table))
  319. out, err := runner.exec.Command(cmdIptablesSave, "-t", string(table)).CombinedOutput()
  320. if err != nil {
  321. return false, fmt.Errorf("error checking rule: %v", err)
  322. }
  323. // Sadly, iptables has inconsistent quoting rules for comments. Just remove all quotes.
  324. // Also, quoted multi-word comments (which are counted as a single arg)
  325. // will be unpacked into multiple args,
  326. // in order to compare against iptables-save output (which will be split at whitespace boundary)
  327. // e.g. a single arg('"this must be before the NodePort rules"') will be unquoted and unpacked into 7 args.
  328. var argsCopy []string
  329. for i := range args {
  330. tmpField := strings.Trim(args[i], "\"")
  331. argsCopy = append(argsCopy, strings.Fields(tmpField)...)
  332. }
  333. argset := sets.NewString(argsCopy...)
  334. for _, line := range strings.Split(string(out), "\n") {
  335. var fields = strings.Fields(line)
  336. // Check that this is a rule for the correct chain, and that it has
  337. // the correct number of argument (+2 for "-A <chain name>")
  338. if !strings.HasPrefix(line, fmt.Sprintf("-A %s", string(chain))) || len(fields) != len(argsCopy)+2 {
  339. continue
  340. }
  341. // Sadly, iptables has inconsistent quoting rules for comments.
  342. // Just remove all quotes.
  343. for i := range fields {
  344. fields[i] = strings.Trim(fields[i], "\"")
  345. }
  346. // TODO: This misses reorderings e.g. "-x foo ! -y bar" will match "! -x foo -y bar"
  347. if sets.NewString(fields...).IsSuperset(argset) {
  348. return true, nil
  349. }
  350. glog.V(5).Infof("DBG: fields is not a superset of args: fields=%v args=%v", fields, args)
  351. }
  352. return false, nil
  353. }
  354. // Executes the rule check using the "-C" flag
  355. func (runner *runner) checkRuleUsingCheck(args []string) (bool, error) {
  356. out, err := runner.run(opCheckRule, args)
  357. if err == nil {
  358. return true, nil
  359. }
  360. if ee, ok := err.(utilexec.ExitError); ok {
  361. // iptables uses exit(1) to indicate a failure of the operation,
  362. // as compared to a malformed commandline, for example.
  363. if ee.Exited() && ee.ExitStatus() == 1 {
  364. return false, nil
  365. }
  366. }
  367. return false, fmt.Errorf("error checking rule: %v: %s", err, out)
  368. }
  369. type operation string
  370. const (
  371. opCreateChain operation = "-N"
  372. opFlushChain operation = "-F"
  373. opDeleteChain operation = "-X"
  374. opAppendRule operation = "-A"
  375. opCheckRule operation = "-C"
  376. opDeleteRule operation = "-D"
  377. )
  378. func makeFullArgs(table Table, chain Chain, args ...string) []string {
  379. return append([]string{string(chain), "-t", string(table)}, args...)
  380. }
  381. // Checks if iptables has the "-C" flag
  382. func getIptablesHasCheckCommand(vstring string) bool {
  383. minVersion, err := semver.NewVersion(MinCheckVersion)
  384. if err != nil {
  385. glog.Errorf("MinCheckVersion (%s) is not a valid version string: %v", MinCheckVersion, err)
  386. return true
  387. }
  388. version, err := semver.NewVersion(vstring)
  389. if err != nil {
  390. glog.Errorf("vstring (%s) is not a valid version string: %v", vstring, err)
  391. return true
  392. }
  393. if version.LessThan(*minVersion) {
  394. return false
  395. }
  396. return true
  397. }
  398. // Checks if iptables version has a "wait" flag
  399. func getIptablesWaitFlag(vstring string) []string {
  400. version, err := semver.NewVersion(vstring)
  401. if err != nil {
  402. glog.Errorf("vstring (%s) is not a valid version string: %v", vstring, err)
  403. return nil
  404. }
  405. minVersion, err := semver.NewVersion(MinWaitVersion)
  406. if err != nil {
  407. glog.Errorf("MinWaitVersion (%s) is not a valid version string: %v", MinWaitVersion, err)
  408. return nil
  409. }
  410. if version.LessThan(*minVersion) {
  411. return nil
  412. }
  413. minVersion, err = semver.NewVersion(MinWait2Version)
  414. if err != nil {
  415. glog.Errorf("MinWait2Version (%s) is not a valid version string: %v", MinWait2Version, err)
  416. return nil
  417. }
  418. if version.LessThan(*minVersion) {
  419. return []string{"-w"}
  420. } else {
  421. return []string{"-w2"}
  422. }
  423. }
  424. // getIptablesVersionString runs "iptables --version" to get the version string
  425. // in the form "X.X.X"
  426. func getIptablesVersionString(exec utilexec.Interface) (string, error) {
  427. // this doesn't access mutable state so we don't need to use the interface / runner
  428. bytes, err := exec.Command(cmdIptables, "--version").CombinedOutput()
  429. if err != nil {
  430. return "", err
  431. }
  432. versionMatcher := regexp.MustCompile("v([0-9]+\\.[0-9]+\\.[0-9]+)")
  433. match := versionMatcher.FindStringSubmatch(string(bytes))
  434. if match == nil {
  435. return "", fmt.Errorf("no iptables version found in string: %s", bytes)
  436. }
  437. return match[1], nil
  438. }
  439. // goroutine to listen for D-Bus signals
  440. func (runner *runner) dbusSignalHandler(bus utildbus.Connection) {
  441. firewalld := bus.Object(firewalldName, firewalldPath)
  442. for s := range runner.signal {
  443. if s == nil {
  444. // Unregister
  445. bus.Signal(runner.signal)
  446. return
  447. }
  448. switch s.Name {
  449. case "org.freedesktop.DBus.NameOwnerChanged":
  450. name := s.Body[0].(string)
  451. new_owner := s.Body[2].(string)
  452. if name != firewalldName || len(new_owner) == 0 {
  453. continue
  454. }
  455. // FirewallD startup (specifically the part where it deletes
  456. // all existing iptables rules) may not yet be complete when
  457. // we get this signal, so make a dummy request to it to
  458. // synchronize.
  459. firewalld.Call(firewalldInterface+".getDefaultZone", 0)
  460. runner.reload()
  461. case firewalldInterface + ".Reloaded":
  462. runner.reload()
  463. }
  464. }
  465. }
  466. // AddReloadFunc is part of Interface
  467. func (runner *runner) AddReloadFunc(reloadFunc func()) {
  468. runner.reloadFuncs = append(runner.reloadFuncs, reloadFunc)
  469. }
  470. // runs all reload funcs to re-sync iptables rules
  471. func (runner *runner) reload() {
  472. glog.V(1).Infof("reloading iptables rules")
  473. for _, f := range runner.reloadFuncs {
  474. f()
  475. }
  476. }
  477. // IsNotFoundError returns true if the error indicates "not found". It parses
  478. // the error string looking for known values, which is imperfect but works in
  479. // practice.
  480. func IsNotFoundError(err error) bool {
  481. es := err.Error()
  482. if strings.Contains(es, "No such file or directory") {
  483. return true
  484. }
  485. if strings.Contains(es, "No chain/target/match by that name") {
  486. return true
  487. }
  488. return false
  489. }