iptables_runner_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build linux
  4. package linuxfw
  5. import (
  6. "net/netip"
  7. "strings"
  8. "testing"
  9. "tailscale.com/net/tsaddr"
  10. "tailscale.com/tsconst"
  11. )
  12. var testIsNotExistErr = "exitcode:1"
  13. func init() {
  14. isNotExistError = func(e error) bool { return e.Error() == testIsNotExistErr }
  15. }
  16. func TestAddAndDeleteChains(t *testing.T) {
  17. iptr := newFakeIPTablesRunner()
  18. err := iptr.AddChains()
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. // Check that the chains were created.
  23. tsChains := []struct{ table, chain string }{ // table/chain
  24. {"filter", "ts-input"},
  25. {"filter", "ts-forward"},
  26. {"nat", "ts-postrouting"},
  27. }
  28. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  29. for _, tc := range tsChains {
  30. // Exists returns error if the chain doesn't exist.
  31. if _, err := proto.Exists(tc.table, tc.chain); err != nil {
  32. t.Errorf("chain %s/%s doesn't exist", tc.table, tc.chain)
  33. }
  34. }
  35. }
  36. err = iptr.DelChains()
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. // Check that the chains were deleted.
  41. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  42. for _, tc := range tsChains {
  43. if _, err = proto.Exists(tc.table, tc.chain); err == nil {
  44. t.Errorf("chain %s/%s still exists", tc.table, tc.chain)
  45. }
  46. }
  47. }
  48. }
  49. func TestAddAndDeleteHooks(t *testing.T) {
  50. iptr := newFakeIPTablesRunner()
  51. // don't need to test what happens if the chains don't exist, because
  52. // this is handled by fake iptables, in realife iptables would return error.
  53. if err := iptr.AddChains(); err != nil {
  54. t.Fatal(err)
  55. }
  56. defer iptr.DelChains()
  57. if err := iptr.AddHooks(); err != nil {
  58. t.Fatal(err)
  59. }
  60. // Check that the rules were created.
  61. tsRules := []fakeRule{ // table/chain/rule
  62. {"filter", "INPUT", []string{"-j", "ts-input"}},
  63. {"filter", "FORWARD", []string{"-j", "ts-forward"}},
  64. {"nat", "POSTROUTING", []string{"-j", "ts-postrouting"}},
  65. }
  66. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  67. for _, tr := range tsRules {
  68. if exists, err := proto.Exists(tr.table, tr.chain, tr.args...); err != nil {
  69. t.Fatal(err)
  70. } else if !exists {
  71. t.Errorf("rule %s/%s/%s doesn't exist", tr.table, tr.chain, strings.Join(tr.args, " "))
  72. }
  73. // check if the rule is at front of the chain
  74. if proto.(*fakeIPTables).n[tr.table+"/"+tr.chain][0] != strings.Join(tr.args, " ") {
  75. t.Errorf("v4 rule %s/%s/%s is not at the top", tr.table, tr.chain, strings.Join(tr.args, " "))
  76. }
  77. }
  78. }
  79. if err := iptr.DelHooks(t.Logf); err != nil {
  80. t.Fatal(err)
  81. }
  82. // Check that the rules were deleted.
  83. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  84. for _, tr := range tsRules {
  85. if exists, err := proto.Exists(tr.table, tr.chain, tr.args...); err != nil {
  86. t.Fatal(err)
  87. } else if exists {
  88. t.Errorf("rule %s/%s/%s still exists", tr.table, tr.chain, strings.Join(tr.args, " "))
  89. }
  90. }
  91. }
  92. if err := iptr.AddHooks(); err != nil {
  93. t.Fatal(err)
  94. }
  95. }
  96. func TestAddAndDeleteBase(t *testing.T) {
  97. iptr := newFakeIPTablesRunner()
  98. tunname := "tun0"
  99. if err := iptr.AddChains(); err != nil {
  100. t.Fatal(err)
  101. }
  102. if err := iptr.AddBase(tunname); err != nil {
  103. t.Fatal(err)
  104. }
  105. // Check that the rules were created.
  106. tsRulesV4 := []fakeRule{ // table/chain/rule
  107. {"filter", "ts-input", []string{"!", "-i", tunname, "-s", tsaddr.ChromeOSVMRange().String(), "-j", "RETURN"}},
  108. {"filter", "ts-input", []string{"!", "-i", tunname, "-s", tsaddr.CGNATRange().String(), "-j", "DROP"}},
  109. {"filter", "ts-forward", []string{"-o", tunname, "-s", tsaddr.CGNATRange().String(), "-j", "DROP"}},
  110. }
  111. tsRulesCommon := []fakeRule{ // table/chain/rule
  112. {"filter", "ts-input", []string{"-i", tunname, "-j", "ACCEPT"}},
  113. {"filter", "ts-forward", []string{"-i", tunname, "-j", "MARK", "--set-mark", tsconst.LinuxSubnetRouteMark + "/" + tsconst.LinuxFwmarkMask}},
  114. {"filter", "ts-forward", []string{"-m", "mark", "--mark", tsconst.LinuxSubnetRouteMark + "/" + tsconst.LinuxFwmarkMask, "-j", "ACCEPT"}},
  115. {"filter", "ts-forward", []string{"-o", tunname, "-j", "ACCEPT"}},
  116. }
  117. // check that the rules were created for ipt4
  118. for _, tr := range append(tsRulesV4, tsRulesCommon...) {
  119. if exists, err := iptr.ipt4.Exists(tr.table, tr.chain, tr.args...); err != nil {
  120. t.Fatal(err)
  121. } else if !exists {
  122. t.Errorf("rule %s/%s/%s doesn't exist", tr.table, tr.chain, strings.Join(tr.args, " "))
  123. }
  124. }
  125. // check that the rules were created for ipt6
  126. for _, tr := range tsRulesCommon {
  127. if exists, err := iptr.ipt6.Exists(tr.table, tr.chain, tr.args...); err != nil {
  128. t.Fatal(err)
  129. } else if !exists {
  130. t.Errorf("rule %s/%s/%s doesn't exist", tr.table, tr.chain, strings.Join(tr.args, " "))
  131. }
  132. }
  133. if err := iptr.DelBase(); err != nil {
  134. t.Fatal(err)
  135. }
  136. // Check that the rules were deleted.
  137. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  138. for _, tr := range append(tsRulesV4, tsRulesCommon...) {
  139. if exists, err := proto.Exists(tr.table, tr.chain, tr.args...); err != nil {
  140. t.Fatal(err)
  141. } else if exists {
  142. t.Errorf("rule %s/%s/%s still exists", tr.table, tr.chain, strings.Join(tr.args, " "))
  143. }
  144. }
  145. }
  146. if err := iptr.DelChains(); err != nil {
  147. t.Fatal(err)
  148. }
  149. }
  150. func TestAddAndDelLoopbackRule(t *testing.T) {
  151. iptr := newFakeIPTablesRunner()
  152. // We don't need to test for malformed addresses, AddLoopbackRule
  153. // takes in a netip.Addr, which is already valid.
  154. fakeAddrV4 := netip.MustParseAddr("192.168.0.2")
  155. fakeAddrV6 := netip.MustParseAddr("2001:db8::2")
  156. if err := iptr.AddChains(); err != nil {
  157. t.Fatal(err)
  158. }
  159. if err := iptr.AddLoopbackRule(fakeAddrV4); err != nil {
  160. t.Fatal(err)
  161. }
  162. if err := iptr.AddLoopbackRule(fakeAddrV6); err != nil {
  163. t.Fatal(err)
  164. }
  165. // Check that the rules were created.
  166. tsRulesV4 := fakeRule{ // table/chain/rule
  167. "filter", "ts-input", []string{"-i", "lo", "-s", fakeAddrV4.String(), "-j", "ACCEPT"}}
  168. tsRulesV6 := fakeRule{ // table/chain/rule
  169. "filter", "ts-input", []string{"-i", "lo", "-s", fakeAddrV6.String(), "-j", "ACCEPT"}}
  170. // check that the rules were created for ipt4 and ipt6
  171. if exist, err := iptr.ipt4.Exists(tsRulesV4.table, tsRulesV4.chain, tsRulesV4.args...); err != nil {
  172. t.Fatal(err)
  173. } else if !exist {
  174. t.Errorf("rule %s/%s/%s doesn't exist", tsRulesV4.table, tsRulesV4.chain, strings.Join(tsRulesV4.args, " "))
  175. }
  176. if exist, err := iptr.ipt6.Exists(tsRulesV6.table, tsRulesV6.chain, tsRulesV6.args...); err != nil {
  177. t.Fatal(err)
  178. } else if !exist {
  179. t.Errorf("rule %s/%s/%s doesn't exist", tsRulesV6.table, tsRulesV6.chain, strings.Join(tsRulesV6.args, " "))
  180. }
  181. // check that the rule is at the top
  182. chain := "filter/ts-input"
  183. if iptr.ipt4.(*fakeIPTables).n[chain][0] != strings.Join(tsRulesV4.args, " ") {
  184. t.Errorf("v4 rule %s/%s/%s is not at the top", tsRulesV4.table, tsRulesV4.chain, strings.Join(tsRulesV4.args, " "))
  185. }
  186. if iptr.ipt6.(*fakeIPTables).n[chain][0] != strings.Join(tsRulesV6.args, " ") {
  187. t.Errorf("v6 rule %s/%s/%s is not at the top", tsRulesV6.table, tsRulesV6.chain, strings.Join(tsRulesV6.args, " "))
  188. }
  189. // delete the rules
  190. if err := iptr.DelLoopbackRule(fakeAddrV4); err != nil {
  191. t.Fatal(err)
  192. }
  193. if err := iptr.DelLoopbackRule(fakeAddrV6); err != nil {
  194. t.Fatal(err)
  195. }
  196. // Check that the rules were deleted.
  197. if exist, err := iptr.ipt4.Exists(tsRulesV4.table, tsRulesV4.chain, tsRulesV4.args...); err != nil {
  198. t.Fatal(err)
  199. } else if exist {
  200. t.Errorf("rule %s/%s/%s still exists", tsRulesV4.table, tsRulesV4.chain, strings.Join(tsRulesV4.args, " "))
  201. }
  202. if exist, err := iptr.ipt6.Exists(tsRulesV6.table, tsRulesV6.chain, tsRulesV6.args...); err != nil {
  203. t.Fatal(err)
  204. } else if exist {
  205. t.Errorf("rule %s/%s/%s still exists", tsRulesV6.table, tsRulesV6.chain, strings.Join(tsRulesV6.args, " "))
  206. }
  207. if err := iptr.DelChains(); err != nil {
  208. t.Fatal(err)
  209. }
  210. }
  211. func TestAddAndDelSNATRule(t *testing.T) {
  212. iptr := newFakeIPTablesRunner()
  213. if err := iptr.AddChains(); err != nil {
  214. t.Fatal(err)
  215. }
  216. rule := fakeRule{ // table/chain/rule
  217. "nat", "ts-postrouting", []string{"-m", "mark", "--mark", tsconst.LinuxSubnetRouteMark + "/" + tsconst.LinuxFwmarkMask, "-j", "MASQUERADE"},
  218. }
  219. // Add SNAT rule
  220. if err := iptr.AddSNATRule(); err != nil {
  221. t.Fatal(err)
  222. }
  223. // Check that the rule was created for ipt4 and ipt6
  224. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  225. if exist, err := proto.Exists(rule.table, rule.chain, rule.args...); err != nil {
  226. t.Fatal(err)
  227. } else if !exist {
  228. t.Errorf("rule %s/%s/%s doesn't exist", rule.table, rule.chain, strings.Join(rule.args, " "))
  229. }
  230. }
  231. // Delete SNAT rule
  232. if err := iptr.DelSNATRule(); err != nil {
  233. t.Fatal(err)
  234. }
  235. // Check that the rule was deleted for ipt4 and ipt6
  236. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  237. if exist, err := proto.Exists(rule.table, rule.chain, rule.args...); err != nil {
  238. t.Fatal(err)
  239. } else if exist {
  240. t.Errorf("rule %s/%s/%s still exists", rule.table, rule.chain, strings.Join(rule.args, " "))
  241. }
  242. }
  243. if err := iptr.DelChains(); err != nil {
  244. t.Fatal(err)
  245. }
  246. }
  247. func TestEnsureSNATForDst_ipt(t *testing.T) {
  248. ip1, ip2, ip3 := netip.MustParseAddr("100.99.99.99"), netip.MustParseAddr("100.88.88.88"), netip.MustParseAddr("100.77.77.77")
  249. iptr := newFakeIPTablesRunner()
  250. // 1. A new rule gets added
  251. mustCreateSNATRule_ipt(t, iptr, ip1, ip2)
  252. checkSNATRule_ipt(t, iptr, ip1, ip2)
  253. checkSNATRuleCount(t, iptr, ip1, 1)
  254. // 2. Another call to EnsureSNATForDst with the same src and dst does not result in another rule being added.
  255. mustCreateSNATRule_ipt(t, iptr, ip1, ip2)
  256. checkSNATRule_ipt(t, iptr, ip1, ip2)
  257. checkSNATRuleCount(t, iptr, ip1, 1) // still just 1 rule
  258. // 3. Another call to EnsureSNATForDst with a different src and the same dst results in the earlier rule being
  259. // deleted.
  260. mustCreateSNATRule_ipt(t, iptr, ip3, ip2)
  261. checkSNATRule_ipt(t, iptr, ip3, ip2)
  262. checkSNATRuleCount(t, iptr, ip1, 1) // still just 1 rule
  263. // 4. Another call to EnsureSNATForDst with a different dst should not get the earlier rule deleted.
  264. mustCreateSNATRule_ipt(t, iptr, ip3, ip1)
  265. checkSNATRule_ipt(t, iptr, ip3, ip1)
  266. checkSNATRuleCount(t, iptr, ip1, 2) // now 2 rules
  267. // 5. A call to EnsureSNATForDst with a match dst and a match port should not get deleted by EnsureSNATForDst for the same dst.
  268. args := []string{"--destination", ip1.String(), "-j", "SNAT", "--to-source", "10.0.0.1"}
  269. if err := iptr.getIPTByAddr(ip1).Insert("nat", "POSTROUTING", 1, args...); err != nil {
  270. t.Fatalf("error adding SNAT rule: %v", err)
  271. }
  272. exists, err := iptr.getIPTByAddr(ip1).Exists("nat", "POSTROUTING", args...)
  273. if err != nil {
  274. t.Fatalf("error checking if rule exists: %v", err)
  275. }
  276. if !exists {
  277. t.Fatalf("SNAT rule for destination and port unexpectedly deleted")
  278. }
  279. mustCreateSNATRule_ipt(t, iptr, ip3, ip1)
  280. checkSNATRuleCount(t, iptr, ip1, 3) // now 3 rules
  281. }
  282. func mustCreateSNATRule_ipt(t *testing.T, iptr *iptablesRunner, src, dst netip.Addr) {
  283. t.Helper()
  284. if err := iptr.EnsureSNATForDst(src, dst); err != nil {
  285. t.Fatalf("error ensuring SNAT rule: %v", err)
  286. }
  287. }
  288. func checkSNATRule_ipt(t *testing.T, iptr *iptablesRunner, src, dst netip.Addr) {
  289. t.Helper()
  290. dstPrefix, err := dst.Prefix(32)
  291. if err != nil {
  292. t.Fatalf("error converting addr to prefix: %v", err)
  293. }
  294. exists, err := iptr.getIPTByAddr(src).Exists("nat", "POSTROUTING", "-d", dstPrefix.String(), "-j", "SNAT", "--to-source", src.String())
  295. if err != nil {
  296. t.Fatalf("error checking if rule exists: %v", err)
  297. }
  298. if !exists {
  299. t.Fatalf("SNAT rule for src %s dst %s should exist, but it does not", src, dst)
  300. }
  301. }
  302. func checkSNATRuleCount(t *testing.T, iptr *iptablesRunner, ip netip.Addr, wantsRules int) {
  303. t.Helper()
  304. rules, err := iptr.getIPTByAddr(ip).List("nat", "POSTROUTING")
  305. if err != nil {
  306. t.Fatalf("error listing rules: %v", err)
  307. }
  308. if len(rules) != wantsRules {
  309. t.Fatalf("wants %d rules, got %d", wantsRules, len(rules))
  310. }
  311. }