iptables_runner_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. )
  11. func TestAddAndDeleteChains(t *testing.T) {
  12. iptr := NewFakeIPTablesRunner()
  13. err := iptr.AddChains()
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. // Check that the chains were created.
  18. tsChains := []struct{ table, chain string }{ // table/chain
  19. {"filter", "ts-input"},
  20. {"filter", "ts-forward"},
  21. {"nat", "ts-postrouting"},
  22. }
  23. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  24. for _, tc := range tsChains {
  25. // Exists returns error if the chain doesn't exist.
  26. if _, err := proto.Exists(tc.table, tc.chain); err != nil {
  27. t.Errorf("chain %s/%s doesn't exist", tc.table, tc.chain)
  28. }
  29. }
  30. }
  31. err = iptr.DelChains()
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. // Check that the chains were deleted.
  36. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  37. for _, tc := range tsChains {
  38. if _, err = proto.Exists(tc.table, tc.chain); err == nil {
  39. t.Errorf("chain %s/%s still exists", tc.table, tc.chain)
  40. }
  41. }
  42. }
  43. }
  44. func TestAddAndDeleteHooks(t *testing.T) {
  45. iptr := NewFakeIPTablesRunner()
  46. // don't need to test what happens if the chains don't exist, because
  47. // this is handled by fake iptables, in realife iptables would return error.
  48. if err := iptr.AddChains(); err != nil {
  49. t.Fatal(err)
  50. }
  51. defer iptr.DelChains()
  52. if err := iptr.AddHooks(); err != nil {
  53. t.Fatal(err)
  54. }
  55. // Check that the rules were created.
  56. tsRules := []fakeRule{ // table/chain/rule
  57. {"filter", "INPUT", []string{"-j", "ts-input"}},
  58. {"filter", "FORWARD", []string{"-j", "ts-forward"}},
  59. {"nat", "POSTROUTING", []string{"-j", "ts-postrouting"}},
  60. }
  61. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  62. for _, tr := range tsRules {
  63. if exists, err := proto.Exists(tr.table, tr.chain, tr.args...); err != nil {
  64. t.Fatal(err)
  65. } else if !exists {
  66. t.Errorf("rule %s/%s/%s doesn't exist", tr.table, tr.chain, strings.Join(tr.args, " "))
  67. }
  68. // check if the rule is at front of the chain
  69. if proto.(*fakeIPTables).n[tr.table+"/"+tr.chain][0] != strings.Join(tr.args, " ") {
  70. t.Errorf("v4 rule %s/%s/%s is not at the top", tr.table, tr.chain, strings.Join(tr.args, " "))
  71. }
  72. }
  73. }
  74. if err := iptr.DelHooks(t.Logf); err != nil {
  75. t.Fatal(err)
  76. }
  77. // Check that the rules were deleted.
  78. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  79. for _, tr := range tsRules {
  80. if exists, err := proto.Exists(tr.table, tr.chain, tr.args...); err != nil {
  81. t.Fatal(err)
  82. } else if exists {
  83. t.Errorf("rule %s/%s/%s still exists", tr.table, tr.chain, strings.Join(tr.args, " "))
  84. }
  85. }
  86. }
  87. if err := iptr.AddHooks(); err != nil {
  88. t.Fatal(err)
  89. }
  90. }
  91. func TestAddAndDeleteBase(t *testing.T) {
  92. iptr := NewFakeIPTablesRunner()
  93. tunname := "tun0"
  94. if err := iptr.AddChains(); err != nil {
  95. t.Fatal(err)
  96. }
  97. if err := iptr.AddBase(tunname); err != nil {
  98. t.Fatal(err)
  99. }
  100. // Check that the rules were created.
  101. tsRulesV4 := []fakeRule{ // table/chain/rule
  102. {"filter", "ts-input", []string{"!", "-i", tunname, "-s", tsaddr.ChromeOSVMRange().String(), "-j", "RETURN"}},
  103. {"filter", "ts-input", []string{"!", "-i", tunname, "-s", tsaddr.CGNATRange().String(), "-j", "DROP"}},
  104. {"filter", "ts-forward", []string{"-o", tunname, "-s", tsaddr.CGNATRange().String(), "-j", "DROP"}},
  105. }
  106. tsRulesCommon := []fakeRule{ // table/chain/rule
  107. {"filter", "ts-input", []string{"-i", tunname, "-j", "ACCEPT"}},
  108. {"filter", "ts-forward", []string{"-i", tunname, "-j", "MARK", "--set-mark", TailscaleSubnetRouteMark + "/" + TailscaleFwmarkMask}},
  109. {"filter", "ts-forward", []string{"-m", "mark", "--mark", TailscaleSubnetRouteMark + "/" + TailscaleFwmarkMask, "-j", "ACCEPT"}},
  110. {"filter", "ts-forward", []string{"-o", tunname, "-j", "ACCEPT"}},
  111. }
  112. // check that the rules were created for ipt4
  113. for _, tr := range append(tsRulesV4, tsRulesCommon...) {
  114. if exists, err := iptr.ipt4.Exists(tr.table, tr.chain, tr.args...); err != nil {
  115. t.Fatal(err)
  116. } else if !exists {
  117. t.Errorf("rule %s/%s/%s doesn't exist", tr.table, tr.chain, strings.Join(tr.args, " "))
  118. }
  119. }
  120. // check that the rules were created for ipt6
  121. for _, tr := range tsRulesCommon {
  122. if exists, err := iptr.ipt6.Exists(tr.table, tr.chain, tr.args...); err != nil {
  123. t.Fatal(err)
  124. } else if !exists {
  125. t.Errorf("rule %s/%s/%s doesn't exist", tr.table, tr.chain, strings.Join(tr.args, " "))
  126. }
  127. }
  128. if err := iptr.DelBase(); err != nil {
  129. t.Fatal(err)
  130. }
  131. // Check that the rules were deleted.
  132. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  133. for _, tr := range append(tsRulesV4, tsRulesCommon...) {
  134. if exists, err := proto.Exists(tr.table, tr.chain, tr.args...); err != nil {
  135. t.Fatal(err)
  136. } else if exists {
  137. t.Errorf("rule %s/%s/%s still exists", tr.table, tr.chain, strings.Join(tr.args, " "))
  138. }
  139. }
  140. }
  141. if err := iptr.DelChains(); err != nil {
  142. t.Fatal(err)
  143. }
  144. }
  145. func TestAddAndDelLoopbackRule(t *testing.T) {
  146. iptr := NewFakeIPTablesRunner()
  147. // We don't need to test for malformed addresses, AddLoopbackRule
  148. // takes in a netip.Addr, which is already valid.
  149. fakeAddrV4 := netip.MustParseAddr("192.168.0.2")
  150. fakeAddrV6 := netip.MustParseAddr("2001:db8::2")
  151. if err := iptr.AddChains(); err != nil {
  152. t.Fatal(err)
  153. }
  154. if err := iptr.AddLoopbackRule(fakeAddrV4); err != nil {
  155. t.Fatal(err)
  156. }
  157. if err := iptr.AddLoopbackRule(fakeAddrV6); err != nil {
  158. t.Fatal(err)
  159. }
  160. // Check that the rules were created.
  161. tsRulesV4 := fakeRule{ // table/chain/rule
  162. "filter", "ts-input", []string{"-i", "lo", "-s", fakeAddrV4.String(), "-j", "ACCEPT"}}
  163. tsRulesV6 := fakeRule{ // table/chain/rule
  164. "filter", "ts-input", []string{"-i", "lo", "-s", fakeAddrV6.String(), "-j", "ACCEPT"}}
  165. // check that the rules were created for ipt4 and ipt6
  166. if exist, err := iptr.ipt4.Exists(tsRulesV4.table, tsRulesV4.chain, tsRulesV4.args...); err != nil {
  167. t.Fatal(err)
  168. } else if !exist {
  169. t.Errorf("rule %s/%s/%s doesn't exist", tsRulesV4.table, tsRulesV4.chain, strings.Join(tsRulesV4.args, " "))
  170. }
  171. if exist, err := iptr.ipt6.Exists(tsRulesV6.table, tsRulesV6.chain, tsRulesV6.args...); err != nil {
  172. t.Fatal(err)
  173. } else if !exist {
  174. t.Errorf("rule %s/%s/%s doesn't exist", tsRulesV6.table, tsRulesV6.chain, strings.Join(tsRulesV6.args, " "))
  175. }
  176. // check that the rule is at the top
  177. chain := "filter/ts-input"
  178. if iptr.ipt4.(*fakeIPTables).n[chain][0] != strings.Join(tsRulesV4.args, " ") {
  179. t.Errorf("v4 rule %s/%s/%s is not at the top", tsRulesV4.table, tsRulesV4.chain, strings.Join(tsRulesV4.args, " "))
  180. }
  181. if iptr.ipt6.(*fakeIPTables).n[chain][0] != strings.Join(tsRulesV6.args, " ") {
  182. t.Errorf("v6 rule %s/%s/%s is not at the top", tsRulesV6.table, tsRulesV6.chain, strings.Join(tsRulesV6.args, " "))
  183. }
  184. // delete the rules
  185. if err := iptr.DelLoopbackRule(fakeAddrV4); err != nil {
  186. t.Fatal(err)
  187. }
  188. if err := iptr.DelLoopbackRule(fakeAddrV6); err != nil {
  189. t.Fatal(err)
  190. }
  191. // Check that the rules were deleted.
  192. if exist, err := iptr.ipt4.Exists(tsRulesV4.table, tsRulesV4.chain, tsRulesV4.args...); err != nil {
  193. t.Fatal(err)
  194. } else if exist {
  195. t.Errorf("rule %s/%s/%s still exists", tsRulesV4.table, tsRulesV4.chain, strings.Join(tsRulesV4.args, " "))
  196. }
  197. if exist, err := iptr.ipt6.Exists(tsRulesV6.table, tsRulesV6.chain, tsRulesV6.args...); err != nil {
  198. t.Fatal(err)
  199. } else if exist {
  200. t.Errorf("rule %s/%s/%s still exists", tsRulesV6.table, tsRulesV6.chain, strings.Join(tsRulesV6.args, " "))
  201. }
  202. if err := iptr.DelChains(); err != nil {
  203. t.Fatal(err)
  204. }
  205. }
  206. func TestAddAndDelSNATRule(t *testing.T) {
  207. iptr := NewFakeIPTablesRunner()
  208. if err := iptr.AddChains(); err != nil {
  209. t.Fatal(err)
  210. }
  211. rule := fakeRule{ // table/chain/rule
  212. "nat", "ts-postrouting", []string{"-m", "mark", "--mark", TailscaleSubnetRouteMark + "/" + TailscaleFwmarkMask, "-j", "MASQUERADE"},
  213. }
  214. // Add SNAT rule
  215. if err := iptr.AddSNATRule(); err != nil {
  216. t.Fatal(err)
  217. }
  218. // Check that the rule was created for ipt4 and ipt6
  219. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  220. if exist, err := proto.Exists(rule.table, rule.chain, rule.args...); err != nil {
  221. t.Fatal(err)
  222. } else if !exist {
  223. t.Errorf("rule %s/%s/%s doesn't exist", rule.table, rule.chain, strings.Join(rule.args, " "))
  224. }
  225. }
  226. // Delete SNAT rule
  227. if err := iptr.DelSNATRule(); err != nil {
  228. t.Fatal(err)
  229. }
  230. // Check that the rule was deleted for ipt4 and ipt6
  231. for _, proto := range []iptablesInterface{iptr.ipt4, iptr.ipt6} {
  232. if exist, err := proto.Exists(rule.table, rule.chain, rule.args...); err != nil {
  233. t.Fatal(err)
  234. } else if exist {
  235. t.Errorf("rule %s/%s/%s still exists", rule.table, rule.chain, strings.Join(rule.args, " "))
  236. }
  237. }
  238. if err := iptr.DelChains(); err != nil {
  239. t.Fatal(err)
  240. }
  241. }