iptables_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //go:build linux
  2. package iptables
  3. import (
  4. "net"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "golang.org/x/sync/errgroup"
  10. )
  11. const (
  12. chainName = "DOCKEREST"
  13. bridgeName = "lo"
  14. )
  15. func createNewChain(t *testing.T) (*IPTable, *ChainInfo, *ChainInfo) {
  16. t.Helper()
  17. iptable := GetIptable(IPv4)
  18. natChain, err := iptable.NewChain(chainName, Nat, false)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. err = iptable.ProgramChain(natChain, bridgeName, false, true)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. filterChain, err := iptable.NewChain(chainName, Filter, false)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. err = iptable.ProgramChain(filterChain, bridgeName, false, true)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. return iptable, natChain, filterChain
  35. }
  36. func TestNewChain(t *testing.T) {
  37. createNewChain(t)
  38. }
  39. func TestForward(t *testing.T) {
  40. iptable, natChain, filterChain := createNewChain(t)
  41. ip := net.ParseIP("192.168.1.1")
  42. port := 1234
  43. dstAddr := "172.17.0.1"
  44. dstPort := 4321
  45. proto := "tcp"
  46. err := natChain.Forward(Insert, ip, port, proto, dstAddr, dstPort, bridgeName)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. dnatRule := []string{
  51. "-d", ip.String(),
  52. "-p", proto,
  53. "--dport", strconv.Itoa(port),
  54. "-j", "DNAT",
  55. "--to-destination", dstAddr + ":" + strconv.Itoa(dstPort),
  56. "!", "-i", bridgeName,
  57. }
  58. if !iptable.Exists(natChain.Table, natChain.Name, dnatRule...) {
  59. t.Fatal("DNAT rule does not exist")
  60. }
  61. filterRule := []string{
  62. "!", "-i", bridgeName,
  63. "-o", bridgeName,
  64. "-d", dstAddr,
  65. "-p", proto,
  66. "--dport", strconv.Itoa(dstPort),
  67. "-j", "ACCEPT",
  68. }
  69. if !iptable.Exists(filterChain.Table, filterChain.Name, filterRule...) {
  70. t.Fatal("filter rule does not exist")
  71. }
  72. masqRule := []string{
  73. "-d", dstAddr,
  74. "-s", dstAddr,
  75. "-p", proto,
  76. "--dport", strconv.Itoa(dstPort),
  77. "-j", "MASQUERADE",
  78. }
  79. if !iptable.Exists(natChain.Table, "POSTROUTING", masqRule...) {
  80. t.Fatal("MASQUERADE rule does not exist")
  81. }
  82. }
  83. func TestLink(t *testing.T) {
  84. iptable, _, filterChain := createNewChain(t)
  85. ip1 := net.ParseIP("192.168.1.1")
  86. ip2 := net.ParseIP("192.168.1.2")
  87. port := 1234
  88. proto := "tcp"
  89. err := filterChain.Link(Append, ip1, ip2, port, proto, bridgeName)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. rule1 := []string{
  94. "-i", bridgeName,
  95. "-o", bridgeName,
  96. "-p", proto,
  97. "-s", ip1.String(),
  98. "-d", ip2.String(),
  99. "--dport", strconv.Itoa(port),
  100. "-j", "ACCEPT",
  101. }
  102. if !iptable.Exists(filterChain.Table, filterChain.Name, rule1...) {
  103. t.Fatal("rule1 does not exist")
  104. }
  105. rule2 := []string{
  106. "-i", bridgeName,
  107. "-o", bridgeName,
  108. "-p", proto,
  109. "-s", ip2.String(),
  110. "-d", ip1.String(),
  111. "--sport", strconv.Itoa(port),
  112. "-j", "ACCEPT",
  113. }
  114. if !iptable.Exists(filterChain.Table, filterChain.Name, rule2...) {
  115. t.Fatal("rule2 does not exist")
  116. }
  117. }
  118. func TestPrerouting(t *testing.T) {
  119. iptable, natChain, _ := createNewChain(t)
  120. args := []string{"-i", "lo", "-d", "192.168.1.1"}
  121. err := natChain.Prerouting(Insert, args...)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. if !iptable.Exists(natChain.Table, "PREROUTING", args...) {
  126. t.Fatal("rule does not exist")
  127. }
  128. delRule := append([]string{"-D", "PREROUTING", "-t", string(Nat)}, args...)
  129. if _, err = iptable.Raw(delRule...); err != nil {
  130. t.Fatal(err)
  131. }
  132. }
  133. func TestOutput(t *testing.T) {
  134. iptable, natChain, _ := createNewChain(t)
  135. args := []string{"-o", "lo", "-d", "192.168.1.1"}
  136. err := natChain.Output(Insert, args...)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. if !iptable.Exists(natChain.Table, "OUTPUT", args...) {
  141. t.Fatal("rule does not exist")
  142. }
  143. delRule := append([]string{
  144. "-D", "OUTPUT", "-t",
  145. string(natChain.Table),
  146. }, args...)
  147. if _, err = iptable.Raw(delRule...); err != nil {
  148. t.Fatal(err)
  149. }
  150. }
  151. func TestConcurrencyWithWait(t *testing.T) {
  152. RunConcurrencyTest(t, true)
  153. }
  154. func TestConcurrencyNoWait(t *testing.T) {
  155. RunConcurrencyTest(t, false)
  156. }
  157. // Runs 10 concurrent rule additions. This will fail if iptables
  158. // is actually invoked simultaneously without --wait.
  159. // Note that if iptables does not support the xtable lock on this
  160. // system, then allowXlock has no effect -- it will always be off.
  161. func RunConcurrencyTest(t *testing.T, allowXlock bool) {
  162. _, natChain, _ := createNewChain(t)
  163. if !allowXlock && supportsXlock {
  164. supportsXlock = false
  165. defer func() { supportsXlock = true }()
  166. }
  167. ip := net.ParseIP("192.168.1.1")
  168. port := 1234
  169. dstAddr := "172.17.0.1"
  170. dstPort := 4321
  171. proto := "tcp"
  172. group := new(errgroup.Group)
  173. for i := 0; i < 10; i++ {
  174. group.Go(func() error {
  175. return natChain.Forward(Append, ip, port, proto, dstAddr, dstPort, "lo")
  176. })
  177. }
  178. if err := group.Wait(); err != nil {
  179. t.Fatal(err)
  180. }
  181. }
  182. func TestCleanup(t *testing.T) {
  183. iptable, _, filterChain := createNewChain(t)
  184. var rules []byte
  185. // Cleanup filter/FORWARD first otherwise output of iptables-save is dirty
  186. link := []string{
  187. "-t", string(filterChain.Table),
  188. string(Delete), "FORWARD",
  189. "-o", bridgeName,
  190. "-j", filterChain.Name,
  191. }
  192. if _, err := iptable.Raw(link...); err != nil {
  193. t.Fatal(err)
  194. }
  195. filterChain.Remove()
  196. err := iptable.RemoveExistingChain(chainName, Nat)
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. rules, err = exec.Command("iptables-save").Output()
  201. if err != nil {
  202. t.Fatal(err)
  203. }
  204. if strings.Contains(string(rules), chainName) {
  205. t.Fatalf("Removing chain failed. %s found in iptables-save", chainName)
  206. }
  207. }
  208. func TestExistsRaw(t *testing.T) {
  209. const testChain1 = "ABCD"
  210. const testChain2 = "EFGH"
  211. iptable := GetIptable(IPv4)
  212. _, err := iptable.NewChain(testChain1, Filter, false)
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. defer func() {
  217. iptable.RemoveExistingChain(testChain1, Filter)
  218. }()
  219. _, err = iptable.NewChain(testChain2, Filter, false)
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. defer func() {
  224. iptable.RemoveExistingChain(testChain2, Filter)
  225. }()
  226. // Test detection over full and truncated rule string
  227. input := []struct{ rule []string }{
  228. {[]string{"-s", "172.8.9.9/32", "-j", "ACCEPT"}},
  229. {[]string{"-d", "172.8.9.0/24", "-j", "DROP"}},
  230. {[]string{"-s", "172.0.3.0/24", "-d", "172.17.0.0/24", "-p", "tcp", "-m", "tcp", "--dport", "80", "-j", testChain2}},
  231. {[]string{"-j", "RETURN"}},
  232. }
  233. for i, r := range input {
  234. ruleAdd := append([]string{"-t", string(Filter), "-A", testChain1}, r.rule...)
  235. err = iptable.RawCombinedOutput(ruleAdd...)
  236. if err != nil {
  237. t.Fatalf("i=%d, err: %v", i, err)
  238. }
  239. if !iptable.exists(true, Filter, testChain1, r.rule...) {
  240. t.Fatalf("Failed to detect rule. i=%d", i)
  241. }
  242. // Truncate the rule
  243. trg := r.rule[len(r.rule)-1]
  244. trg = trg[:len(trg)-2]
  245. r.rule[len(r.rule)-1] = trg
  246. if iptable.exists(true, Filter, testChain1, r.rule...) {
  247. t.Fatalf("Invalid detection. i=%d", i)
  248. }
  249. }
  250. }