03_masq_src_dest_restrictions 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. Testing that zone masquerading restrictions source and destination restrictions are properly applied.
  2. -- Testcase --
  3. {%
  4. include("./root/usr/share/firewall4/main.uc", {
  5. getenv: function(varname) {
  6. switch (varname) {
  7. case 'ACTION':
  8. return 'print';
  9. }
  10. }
  11. })
  12. %}
  13. -- End --
  14. -- File uci/helpers.json --
  15. {}
  16. -- End --
  17. -- File fs/open~_sys_class_net_zone1_flags.txt --
  18. 0x1103
  19. -- End --
  20. -- File fs/open~_sys_class_net_zone2_flags.txt --
  21. 0x1103
  22. -- End --
  23. -- File uci/firewall.json --
  24. {
  25. "zone": [
  26. {
  27. ".description": "Positive and negative entries should be handled properly and IPv6 addresses should be filtered out for IPv4 masquerading",
  28. "name": "test1",
  29. "input": "ACCEPT",
  30. "output": "ACCEPT",
  31. "forward": "ACCEPT",
  32. "device": "zone1",
  33. "masq": "1",
  34. "masq_src": [
  35. "10.1.0.0/24",
  36. "10.1.1.1",
  37. "!10.1.0.1",
  38. "!10.1.0.2",
  39. "2001:db8:0:1::/64",
  40. "2001:db8:0:2::/64",
  41. "!2001:db8:0:1::1",
  42. "!2001:db8:0:1::2"
  43. ],
  44. "masq_dest": [
  45. "10.2.0.0/24",
  46. "10.2.1.1",
  47. "!10.2.0.1",
  48. "!10.2.0.2",
  49. "2001:db8:1:1::/64",
  50. "2001:db8:1:2::/64",
  51. "!2001:db8:1:1::1",
  52. "!2001:db8:1:1::2"
  53. ]
  54. },
  55. {
  56. ".description": "Positive and negative entries should be handled properly and IPv4 addresses should be filtered out for IPv6 masquerading",
  57. "name": "test2",
  58. "input": "DROP",
  59. "output": "DROP",
  60. "forward": "DROP",
  61. "device": "zone2",
  62. "masq6": "1",
  63. "masq_src": [
  64. "10.1.0.0/24",
  65. "10.1.1.1",
  66. "!10.1.0.1",
  67. "!10.1.0.2",
  68. "2001:db8:0:1::/64",
  69. "2001:db8:0:2::/64",
  70. "!2001:db8:0:1::1",
  71. "!2001:db8:0:1::2"
  72. ],
  73. "masq_dest": [
  74. "10.2.0.0/24",
  75. "10.2.1.1",
  76. "!10.2.0.1",
  77. "!10.2.0.2",
  78. "2001:db8:1:1::/64",
  79. "2001:db8:1:2::/64",
  80. "!2001:db8:1:1::1",
  81. "!2001:db8:1:1::2"
  82. ]
  83. }
  84. ]
  85. }
  86. -- End --
  87. -- Expect stdout --
  88. table inet fw4
  89. flush table inet fw4
  90. table inet fw4 {
  91. #
  92. # Defines
  93. #
  94. define test1_devices = { "zone1" }
  95. define test1_subnets = { }
  96. define test2_devices = { "zone2" }
  97. define test2_subnets = { }
  98. #
  99. # User includes
  100. #
  101. include "/etc/nftables.d/*.nft"
  102. #
  103. # Filter rules
  104. #
  105. chain input {
  106. type filter hook input priority filter; policy drop;
  107. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  108. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  109. iifname "zone1" jump input_test1 comment "!fw4: Handle test1 IPv4/IPv6 input traffic"
  110. iifname "zone2" jump input_test2 comment "!fw4: Handle test2 IPv4/IPv6 input traffic"
  111. }
  112. chain forward {
  113. type filter hook forward priority filter; policy drop;
  114. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  115. iifname "zone1" jump forward_test1 comment "!fw4: Handle test1 IPv4/IPv6 forward traffic"
  116. iifname "zone2" jump forward_test2 comment "!fw4: Handle test2 IPv4/IPv6 forward traffic"
  117. }
  118. chain output {
  119. type filter hook output priority filter; policy drop;
  120. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  121. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  122. oifname "zone1" jump output_test1 comment "!fw4: Handle test1 IPv4/IPv6 output traffic"
  123. oifname "zone2" jump output_test2 comment "!fw4: Handle test2 IPv4/IPv6 output traffic"
  124. }
  125. chain prerouting {
  126. type filter hook prerouting priority filter; policy accept;
  127. }
  128. chain handle_reject {
  129. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  130. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  131. }
  132. chain input_test1 {
  133. jump accept_from_test1
  134. }
  135. chain output_test1 {
  136. jump accept_to_test1
  137. }
  138. chain forward_test1 {
  139. jump accept_to_test1
  140. }
  141. chain accept_from_test1 {
  142. iifname "zone1" counter accept comment "!fw4: accept test1 IPv4/IPv6 traffic"
  143. }
  144. chain accept_to_test1 {
  145. meta nfproto ipv4 oifname "zone1" ct state invalid counter drop comment "!fw4: Prevent NAT leakage"
  146. oifname "zone1" counter accept comment "!fw4: accept test1 IPv4/IPv6 traffic"
  147. }
  148. chain input_test2 {
  149. jump drop_from_test2
  150. }
  151. chain output_test2 {
  152. jump drop_to_test2
  153. }
  154. chain forward_test2 {
  155. jump drop_to_test2
  156. }
  157. chain drop_from_test2 {
  158. iifname "zone2" counter drop comment "!fw4: drop test2 IPv4/IPv6 traffic"
  159. }
  160. chain drop_to_test2 {
  161. oifname "zone2" counter drop comment "!fw4: drop test2 IPv4/IPv6 traffic"
  162. }
  163. #
  164. # NAT rules
  165. #
  166. chain dstnat {
  167. type nat hook prerouting priority dstnat; policy accept;
  168. }
  169. chain srcnat {
  170. type nat hook postrouting priority srcnat; policy accept;
  171. oifname "zone1" jump srcnat_test1 comment "!fw4: Handle test1 IPv4/IPv6 srcnat traffic"
  172. oifname "zone2" jump srcnat_test2 comment "!fw4: Handle test2 IPv4/IPv6 srcnat traffic"
  173. }
  174. chain srcnat_test1 {
  175. meta nfproto ipv4 ip saddr { 10.1.0.0/24, 10.1.1.1 } ip saddr != { 10.1.0.1, 10.1.0.2 } ip daddr { 10.2.0.0/24, 10.2.1.1 } ip daddr != { 10.2.0.1, 10.2.0.2 } masquerade comment "!fw4: Masquerade IPv4 test1 traffic"
  176. }
  177. chain srcnat_test2 {
  178. meta nfproto ipv6 ip6 saddr { 2001:db8:0:1::/64, 2001:db8:0:2::/64 } ip6 saddr != { 2001:db8:0:1::1, 2001:db8:0:1::2 } ip6 daddr { 2001:db8:1:1::/64, 2001:db8:1:2::/64 } ip6 daddr != { 2001:db8:1:1::1, 2001:db8:1:1::2 } masquerade comment "!fw4: Masquerade IPv6 test2 traffic"
  179. }
  180. #
  181. # Raw rules (notrack)
  182. #
  183. chain raw_prerouting {
  184. type filter hook prerouting priority raw; policy accept;
  185. }
  186. chain raw_output {
  187. type filter hook output priority raw; policy accept;
  188. }
  189. #
  190. # Mangle rules
  191. #
  192. chain mangle_prerouting {
  193. type filter hook prerouting priority mangle; policy accept;
  194. }
  195. chain mangle_postrouting {
  196. type filter hook postrouting priority mangle; policy accept;
  197. }
  198. chain mangle_input {
  199. type filter hook input priority mangle; policy accept;
  200. }
  201. chain mangle_output {
  202. type route hook output priority mangle; policy accept;
  203. }
  204. chain mangle_forward {
  205. type filter hook forward priority mangle; policy accept;
  206. }
  207. }
  208. -- End --