05_subnet_mask_matches 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. Test that non-contiguous subnet masks are properly handled. Such masks need
  2. to be translated into bitwise expressions which may not appear as part of
  3. sets, so various permutations of rules need to be emitted.
  4. -- Testcase --
  5. {%
  6. include("./root/usr/share/firewall4/main.uc", {
  7. getenv: function(varname) {
  8. switch (varname) {
  9. case 'ACTION':
  10. return 'print';
  11. }
  12. }
  13. })
  14. %}
  15. -- End --
  16. -- File uci/helpers.json --
  17. {}
  18. -- End --
  19. -- File uci/firewall.json --
  20. {
  21. "zone": [
  22. {
  23. ".description": "IP addrs with non-contiguous masks should be translated to bitwise comparisons",
  24. "name": "test1",
  25. "subnet": [
  26. "::1/::ffff",
  27. "!::2/::ffff"
  28. ]
  29. },
  30. {
  31. ".description": "IP addrs with non-contiguous masks should not be part of sets",
  32. "name": "test2",
  33. "subnet": [
  34. "::1/::ffff",
  35. "::2/::ffff",
  36. "::3/128",
  37. "::4/128",
  38. "!::5/::ffff",
  39. "!::6/::ffff",
  40. "!::7/128",
  41. "!::8/128"
  42. ]
  43. }
  44. ]
  45. }
  46. -- End --
  47. -- Expect stdout --
  48. table inet fw4
  49. flush table inet fw4
  50. table inet fw4 {
  51. #
  52. # Defines
  53. #
  54. define test1_devices = { }
  55. define test1_subnets = { }
  56. define test2_devices = { }
  57. define test2_subnets = { ::3, ::4 }
  58. #
  59. # User includes
  60. #
  61. include "/etc/nftables.d/*.nft"
  62. #
  63. # Filter rules
  64. #
  65. chain input {
  66. type filter hook input priority filter; policy drop;
  67. iifname "lo" accept comment "!fw4: Accept traffic from loopback"
  68. ct state established,related accept comment "!fw4: Allow inbound established and related flows"
  69. meta nfproto ipv6 ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::2 jump input_test1 comment "!fw4: Handle test1 IPv6 input traffic"
  70. meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump input_test2 comment "!fw4: Handle test2 IPv6 input traffic"
  71. meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::2 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump input_test2 comment "!fw4: Handle test2 IPv6 input traffic"
  72. meta nfproto ipv6 ip6 saddr { ::3, ::4 } ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump input_test2 comment "!fw4: Handle test2 IPv6 input traffic"
  73. }
  74. chain forward {
  75. type filter hook forward priority filter; policy drop;
  76. ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
  77. meta nfproto ipv6 ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::2 jump forward_test1 comment "!fw4: Handle test1 IPv6 forward traffic"
  78. meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump forward_test2 comment "!fw4: Handle test2 IPv6 forward traffic"
  79. meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::2 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump forward_test2 comment "!fw4: Handle test2 IPv6 forward traffic"
  80. meta nfproto ipv6 ip6 saddr { ::3, ::4 } ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump forward_test2 comment "!fw4: Handle test2 IPv6 forward traffic"
  81. }
  82. chain output {
  83. type filter hook output priority filter; policy drop;
  84. oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
  85. ct state established,related accept comment "!fw4: Allow outbound established and related flows"
  86. meta nfproto ipv6 ip6 daddr & ::ffff == ::1 ip6 daddr & ::ffff != ::2 jump output_test1 comment "!fw4: Handle test1 IPv6 output traffic"
  87. meta nfproto ipv6 ip6 daddr != { ::7, ::8 } ip6 daddr & ::ffff == ::1 ip6 daddr & ::ffff != ::5 ip6 daddr & ::ffff != ::6 jump output_test2 comment "!fw4: Handle test2 IPv6 output traffic"
  88. meta nfproto ipv6 ip6 daddr != { ::7, ::8 } ip6 daddr & ::ffff == ::2 ip6 daddr & ::ffff != ::5 ip6 daddr & ::ffff != ::6 jump output_test2 comment "!fw4: Handle test2 IPv6 output traffic"
  89. meta nfproto ipv6 ip6 daddr { ::3, ::4 } ip6 daddr != { ::7, ::8 } ip6 daddr & ::ffff != ::5 ip6 daddr & ::ffff != ::6 jump output_test2 comment "!fw4: Handle test2 IPv6 output traffic"
  90. }
  91. chain prerouting {
  92. type filter hook prerouting priority filter; policy accept;
  93. meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump helper_test2 comment "!fw4: Handle test2 IPv6 helper assignment"
  94. meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::2 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump helper_test2 comment "!fw4: Handle test2 IPv6 helper assignment"
  95. meta nfproto ipv6 ip6 saddr { ::3, ::4 } ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump helper_test2 comment "!fw4: Handle test2 IPv6 helper assignment"
  96. }
  97. chain handle_reject {
  98. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  99. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  100. }
  101. chain input_test1 {
  102. jump drop_from_test1
  103. }
  104. chain output_test1 {
  105. jump drop_to_test1
  106. }
  107. chain forward_test1 {
  108. jump drop_to_test1
  109. }
  110. chain helper_test1 {
  111. }
  112. chain drop_from_test1 {
  113. meta nfproto ipv6 ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::2 counter drop comment "!fw4: drop test1 IPv6 traffic"
  114. }
  115. chain drop_to_test1 {
  116. meta nfproto ipv6 ip6 daddr & ::ffff == ::1 ip6 daddr & ::ffff != ::2 counter drop comment "!fw4: drop test1 IPv6 traffic"
  117. }
  118. chain input_test2 {
  119. jump drop_from_test2
  120. }
  121. chain output_test2 {
  122. jump drop_to_test2
  123. }
  124. chain forward_test2 {
  125. jump drop_to_test2
  126. }
  127. chain helper_test2 {
  128. }
  129. chain drop_from_test2 {
  130. meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 counter drop comment "!fw4: drop test2 IPv6 traffic"
  131. meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::2 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 counter drop comment "!fw4: drop test2 IPv6 traffic"
  132. meta nfproto ipv6 ip6 saddr { ::3, ::4 } ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 counter drop comment "!fw4: drop test2 IPv6 traffic"
  133. }
  134. chain drop_to_test2 {
  135. meta nfproto ipv6 ip6 daddr != { ::7, ::8 } ip6 daddr & ::ffff == ::1 ip6 daddr & ::ffff != ::5 ip6 daddr & ::ffff != ::6 counter drop comment "!fw4: drop test2 IPv6 traffic"
  136. meta nfproto ipv6 ip6 daddr != { ::7, ::8 } ip6 daddr & ::ffff == ::2 ip6 daddr & ::ffff != ::5 ip6 daddr & ::ffff != ::6 counter drop comment "!fw4: drop test2 IPv6 traffic"
  137. meta nfproto ipv6 ip6 daddr { ::3, ::4 } ip6 daddr != { ::7, ::8 } ip6 daddr & ::ffff != ::5 ip6 daddr & ::ffff != ::6 counter drop comment "!fw4: drop test2 IPv6 traffic"
  138. }
  139. #
  140. # NAT rules
  141. #
  142. chain dstnat {
  143. type nat hook prerouting priority dstnat; policy accept;
  144. }
  145. chain srcnat {
  146. type nat hook postrouting priority srcnat; policy accept;
  147. }
  148. #
  149. # Raw rules (notrack)
  150. #
  151. chain raw_prerouting {
  152. type filter hook prerouting priority raw; policy accept;
  153. }
  154. chain raw_output {
  155. type filter hook output priority raw; policy accept;
  156. }
  157. #
  158. # Mangle rules
  159. #
  160. chain mangle_prerouting {
  161. type filter hook prerouting priority mangle; policy accept;
  162. }
  163. chain mangle_postrouting {
  164. type filter hook postrouting priority mangle; policy accept;
  165. }
  166. chain mangle_input {
  167. type filter hook input priority mangle; policy accept;
  168. }
  169. chain mangle_output {
  170. type route hook output priority mangle; policy accept;
  171. }
  172. chain mangle_forward {
  173. type filter hook forward priority mangle; policy accept;
  174. }
  175. }
  176. -- End --