iptables.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright 2019 the Kilo authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package iptables
  15. import (
  16. "fmt"
  17. "net"
  18. "strings"
  19. "sync"
  20. "time"
  21. "github.com/coreos/go-iptables/iptables"
  22. )
  23. type iptablesClient interface {
  24. AppendUnique(string, string, ...string) error
  25. Delete(string, string, ...string) error
  26. Exists(string, string, ...string) (bool, error)
  27. ClearChain(string, string) error
  28. DeleteChain(string, string) error
  29. NewChain(string, string) error
  30. }
  31. // rule represents an iptables rule.
  32. type rule struct {
  33. table string
  34. chain string
  35. spec []string
  36. client iptablesClient
  37. }
  38. func (r *rule) Add() error {
  39. if err := r.client.AppendUnique(r.table, r.chain, r.spec...); err != nil {
  40. return fmt.Errorf("failed to add iptables rule: %v", err)
  41. }
  42. return nil
  43. }
  44. func (r *rule) Delete() error {
  45. // Ignore the returned error as an error likely means
  46. // that the rule doesn't exist, which is fine.
  47. r.client.Delete(r.table, r.chain, r.spec...)
  48. return nil
  49. }
  50. func (r *rule) Exists() (bool, error) {
  51. return r.client.Exists(r.table, r.chain, r.spec...)
  52. }
  53. func (r *rule) String() string {
  54. if r == nil {
  55. return ""
  56. }
  57. return fmt.Sprintf("%s_%s_%s", r.table, r.chain, strings.Join(r.spec, "_"))
  58. }
  59. // chain represents an iptables chain.
  60. type chain struct {
  61. table string
  62. chain string
  63. client iptablesClient
  64. }
  65. func (c *chain) Add() error {
  66. if err := c.client.ClearChain(c.table, c.chain); err != nil {
  67. return fmt.Errorf("failed to add iptables chain: %v", err)
  68. }
  69. return nil
  70. }
  71. func (c *chain) Delete() error {
  72. // The chain must be empty before it can be deleted.
  73. if err := c.client.ClearChain(c.table, c.chain); err != nil {
  74. return fmt.Errorf("failed to clear iptables chain: %v", err)
  75. }
  76. // Ignore the returned error as an error likely means
  77. // that the chain doesn't exist, which is fine.
  78. c.client.DeleteChain(c.table, c.chain)
  79. return nil
  80. }
  81. func (c *chain) Exists() (bool, error) {
  82. // The code for "chain already exists".
  83. existsErr := 1
  84. err := c.client.NewChain(c.table, c.chain)
  85. se, ok := err.(statusExiter)
  86. switch {
  87. case err == nil:
  88. // If there was no error adding a new chain, then it did not exist.
  89. // Delete it and return false.
  90. c.client.DeleteChain(c.table, c.chain)
  91. return false, nil
  92. case ok && se.ExitStatus() == existsErr:
  93. return true, nil
  94. default:
  95. return false, err
  96. }
  97. }
  98. func (c *chain) String() string {
  99. if c == nil {
  100. return ""
  101. }
  102. return fmt.Sprintf("%s_%s", c.table, c.chain)
  103. }
  104. // Rule is an interface for interacting with iptables objects.
  105. type Rule interface {
  106. Add() error
  107. Delete() error
  108. Exists() (bool, error)
  109. String() string
  110. }
  111. // Controller is able to reconcile a given set of iptables rules.
  112. type Controller struct {
  113. client iptablesClient
  114. errors chan error
  115. rules map[string]Rule
  116. mu sync.Mutex
  117. subscribed bool
  118. }
  119. // New generates a new iptables rules controller.
  120. // It expects an IP address length to determine
  121. // whether to operate in IPv4 or IPv6 mode.
  122. func New(ipLength int) (*Controller, error) {
  123. p := iptables.ProtocolIPv4
  124. if ipLength == net.IPv6len {
  125. p = iptables.ProtocolIPv6
  126. }
  127. client, err := iptables.NewWithProtocol(p)
  128. if err != nil {
  129. return nil, fmt.Errorf("failed to create iptables client: %v", err)
  130. }
  131. return &Controller{
  132. client: client,
  133. errors: make(chan error),
  134. rules: make(map[string]Rule),
  135. }, nil
  136. }
  137. // Run watches for changes to iptables rules and reconciles
  138. // the rules against the desired state.
  139. func (c *Controller) Run(stop <-chan struct{}) (<-chan error, error) {
  140. c.mu.Lock()
  141. if c.subscribed {
  142. c.mu.Unlock()
  143. return c.errors, nil
  144. }
  145. // Ensure a given instance only subscribes once.
  146. c.subscribed = true
  147. c.mu.Unlock()
  148. go func() {
  149. defer close(c.errors)
  150. for {
  151. select {
  152. case <-time.After(5 * time.Second):
  153. case <-stop:
  154. return
  155. }
  156. c.mu.Lock()
  157. for _, r := range c.rules {
  158. ok, err := r.Exists()
  159. if err != nil {
  160. nonBlockingSend(c.errors, fmt.Errorf("failed to check if rule exists: %v", err))
  161. }
  162. if !ok {
  163. if err := r.Add(); err != nil {
  164. nonBlockingSend(c.errors, fmt.Errorf("failed to add rule: %v", err))
  165. }
  166. }
  167. }
  168. c.mu.Unlock()
  169. }
  170. }()
  171. return c.errors, nil
  172. }
  173. // Set idempotently overwrites any iptables rules previously defined
  174. // for the controller with the given set of rules.
  175. func (c *Controller) Set(rules []Rule) error {
  176. r := make(map[string]struct{})
  177. for i := range rules {
  178. if rules[i] == nil {
  179. continue
  180. }
  181. switch v := rules[i].(type) {
  182. case *rule:
  183. v.client = c.client
  184. case *chain:
  185. v.client = c.client
  186. }
  187. r[rules[i].String()] = struct{}{}
  188. }
  189. c.mu.Lock()
  190. defer c.mu.Unlock()
  191. for k, rule := range c.rules {
  192. if _, ok := r[k]; !ok {
  193. if err := rule.Delete(); err != nil {
  194. return fmt.Errorf("failed to delete rule: %v", err)
  195. }
  196. delete(c.rules, k)
  197. }
  198. }
  199. // Iterate over the slice rather than the map
  200. // to ensure the rules are added in order.
  201. for _, rule := range rules {
  202. if _, ok := c.rules[rule.String()]; !ok {
  203. if err := rule.Add(); err != nil {
  204. return fmt.Errorf("failed to add rule: %v", err)
  205. }
  206. c.rules[rule.String()] = rule
  207. }
  208. }
  209. return nil
  210. }
  211. // CleanUp will clean up any rules created by the controller.
  212. func (c *Controller) CleanUp() error {
  213. c.mu.Lock()
  214. defer c.mu.Unlock()
  215. for k, rule := range c.rules {
  216. if err := rule.Delete(); err != nil {
  217. return fmt.Errorf("failed to delete rule: %v", err)
  218. }
  219. delete(c.rules, k)
  220. }
  221. return nil
  222. }
  223. // IPIPRules returns a set of iptables rules that are necessary
  224. // when traffic between nodes must be encapsulated with IPIP.
  225. func IPIPRules(nodes []*net.IPNet) []Rule {
  226. var rules []Rule
  227. rules = append(rules, &chain{"filter", "KILO-IPIP", nil})
  228. rules = append(rules, &rule{"filter", "INPUT", []string{"-m", "comment", "--comment", "Kilo: jump to IPIP chain", "-p", "4", "-j", "KILO-IPIP"}, nil})
  229. for _, n := range nodes {
  230. // Accept encapsulated traffic from peers.
  231. rules = append(rules, &rule{"filter", "KILO-IPIP", []string{"-m", "comment", "--comment", "Kilo: allow IPIP traffic", "-s", n.IP.String(), "-j", "ACCEPT"}, nil})
  232. }
  233. // Drop all other IPIP traffic.
  234. rules = append(rules, &rule{"filter", "INPUT", []string{"-m", "comment", "--comment", "Kilo: reject other IPIP traffic", "-p", "4", "-j", "DROP"}, nil})
  235. return rules
  236. }
  237. // ForwardRules returns a set of iptables rules that are necessary
  238. // when traffic must be forwarded for the overlay.
  239. func ForwardRules(subnets ...*net.IPNet) []Rule {
  240. var rules []Rule
  241. for _, subnet := range subnets {
  242. s := subnet.String()
  243. rules = append(rules, []Rule{
  244. // Forward traffic to and from the overlay.
  245. &rule{"filter", "FORWARD", []string{"-s", s, "-j", "ACCEPT"}, nil},
  246. &rule{"filter", "FORWARD", []string{"-d", s, "-j", "ACCEPT"}, nil},
  247. }...)
  248. }
  249. return rules
  250. }
  251. // MasqueradeRules returns a set of iptables rules that are necessary
  252. // to NAT traffic from the local Pod subnet to the Internet and out of the Kilo interface.
  253. func MasqueradeRules(kilo, private, localPodSubnet *net.IPNet, remotePodSubnet, peers []*net.IPNet) []Rule {
  254. var rules []Rule
  255. rules = append(rules, &chain{"nat", "KILO-NAT", nil})
  256. // NAT packets from Kilo interface.
  257. rules = append(rules, &rule{"mangle", "PREROUTING", []string{"-m", "comment", "--comment", "Kilo: jump to mark chain", "-i", "kilo+", "-j", "MARK", "--set-xmark", "0x1107/0x1107"}, nil})
  258. rules = append(rules, &rule{"nat", "POSTROUTING", []string{"-m", "comment", "--comment", "Kilo: NAT packets from Kilo interface", "-m", "mark", "--mark", "0x1107/0x1107", "-j", "KILO-NAT"}, nil})
  259. // NAT packets from pod subnet.
  260. rules = append(rules, &rule{"nat", "POSTROUTING", []string{"-m", "comment", "--comment", "Kilo: jump to NAT chain", "-s", localPodSubnet.String(), "-j", "KILO-NAT"}, nil})
  261. rules = append(rules, &rule{"nat", "KILO-NAT", []string{"-m", "comment", "--comment", "Kilo: do not NAT packets destined for the local Pod subnet", "-d", localPodSubnet.String(), "-j", "RETURN"}, nil})
  262. rules = append(rules, &rule{"nat", "KILO-NAT", []string{"-m", "comment", "--comment", "Kilo: do not NAT packets destined for the Kilo subnet", "-d", kilo.String(), "-j", "RETURN"}, nil})
  263. rules = append(rules, &rule{"nat", "KILO-NAT", []string{"-m", "comment", "--comment", "Kilo: do not NAT packets destined for the local private IP", "-d", private.String(), "-j", "RETURN"}, nil})
  264. for _, r := range remotePodSubnet {
  265. rules = append(rules, &rule{"nat", "KILO-NAT", []string{"-m", "comment", "--comment", "Kilo: do not NAT packets from local pod subnet to remote pod subnets", "-s", localPodSubnet.String(), "-d", r.String(), "-j", "RETURN"}, nil})
  266. }
  267. for _, p := range peers {
  268. rules = append(rules, &rule{"nat", "KILO-NAT", []string{"-m", "comment", "--comment", "Kilo: do not NAT packets from local pod subnet to peers", "-s", localPodSubnet.String(), "-d", p.String(), "-j", "RETURN"}, nil})
  269. }
  270. rules = append(rules, &rule{"nat", "KILO-NAT", []string{"-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"}, nil})
  271. return rules
  272. }
  273. func nonBlockingSend(errors chan<- error, err error) {
  274. select {
  275. case errors <- err:
  276. default:
  277. }
  278. }