iptables_test.go 6.5 KB

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