iptables_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. 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. if !iptable.Exists(filterChain.Table, filterChain.Name, rule2...) {
  114. t.Fatal("rule2 does not exist")
  115. }
  116. }
  117. func TestPrerouting(t *testing.T) {
  118. iptable, natChain, _ := createNewChain(t)
  119. args := []string{
  120. "-i", "lo",
  121. "-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{
  137. "-o", "lo",
  138. "-d", "192.168.1.1"}
  139. err := natChain.Output(Insert, args...)
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. if !iptable.Exists(natChain.Table, "OUTPUT", args...) {
  144. t.Fatal("rule does not exist")
  145. }
  146. delRule := append([]string{"-D", "OUTPUT", "-t",
  147. string(natChain.Table)}, 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{"-t", string(filterChain.Table),
  188. string(Delete), "FORWARD",
  189. "-o", bridgeName,
  190. "-j", filterChain.Name}
  191. if _, err := iptable.Raw(link...); err != nil {
  192. t.Fatal(err)
  193. }
  194. filterChain.Remove()
  195. err := iptable.RemoveExistingChain(chainName, Nat)
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. rules, err = exec.Command("iptables-save").Output()
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. if strings.Contains(string(rules), chainName) {
  204. t.Fatalf("Removing chain failed. %s found in iptables-save", chainName)
  205. }
  206. }
  207. func TestExistsRaw(t *testing.T) {
  208. testChain1 := "ABCD"
  209. testChain2 := "EFGH"
  210. iptable := GetIptable(IPv4)
  211. _, err := iptable.NewChain(testChain1, Filter, false)
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. defer func() {
  216. iptable.RemoveExistingChain(testChain1, Filter)
  217. }()
  218. _, err = iptable.NewChain(testChain2, Filter, false)
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. defer func() {
  223. iptable.RemoveExistingChain(testChain2, Filter)
  224. }()
  225. // Test detection over full and truncated rule string
  226. input := []struct{ rule []string }{
  227. {[]string{"-s", "172.8.9.9/32", "-j", "ACCEPT"}},
  228. {[]string{"-d", "172.8.9.0/24", "-j", "DROP"}},
  229. {[]string{"-s", "172.0.3.0/24", "-d", "172.17.0.0/24", "-p", "tcp", "-m", "tcp", "--dport", "80", "-j", testChain2}},
  230. {[]string{"-j", "RETURN"}},
  231. }
  232. for i, r := range input {
  233. ruleAdd := append([]string{"-t", string(Filter), "-A", testChain1}, r.rule...)
  234. err = iptable.RawCombinedOutput(ruleAdd...)
  235. if err != nil {
  236. t.Fatalf("i=%d, err: %v", i, err)
  237. }
  238. if !iptable.existsRaw(Filter, testChain1, r.rule...) {
  239. t.Fatalf("Failed to detect rule. i=%d", i)
  240. }
  241. // Truncate the rule
  242. trg := r.rule[len(r.rule)-1]
  243. trg = trg[:len(trg)-2]
  244. r.rule[len(r.rule)-1] = trg
  245. if iptable.existsRaw(Filter, testChain1, r.rule...) {
  246. t.Fatalf("Invalid detection. i=%d", i)
  247. }
  248. }
  249. }
  250. func TestGetVersion(t *testing.T) {
  251. mj, mn, mc := parseVersionNumbers("iptables v1.4.19.1-alpha")
  252. if mj != 1 || mn != 4 || mc != 19 {
  253. t.Fatal("Failed to parse version numbers")
  254. }
  255. }
  256. func TestSupportsCOption(t *testing.T) {
  257. input := []struct {
  258. mj int
  259. mn int
  260. mc int
  261. ok bool
  262. }{
  263. {1, 4, 11, true},
  264. {1, 4, 12, true},
  265. {1, 5, 0, true},
  266. {0, 4, 11, false},
  267. {0, 5, 12, false},
  268. {1, 3, 12, false},
  269. {1, 4, 10, false},
  270. }
  271. for ind, inp := range input {
  272. if inp.ok != supportsCOption(inp.mj, inp.mn, inp.mc) {
  273. t.Fatalf("Incorrect check: %d", ind)
  274. }
  275. }
  276. }