01_policies 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. Testing that zone policies are properly mapped to chains.
  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 fs/open~_sys_class_net_zone3_flags.txt --
  24. 0x1103
  25. -- End --
  26. -- File uci/firewall.json --
  27. {
  28. "zone": [
  29. {
  30. ".description": "Zone accept policies should map to accept rules from/to covered interfaces",
  31. "name": "test1",
  32. "input": "ACCEPT",
  33. "output": "ACCEPT",
  34. "forward": "ACCEPT",
  35. "device": "zone1"
  36. },
  37. {
  38. ".description": "Zone drop policies should map to drop rules from/to covered interfaces",
  39. "name": "test2",
  40. "input": "DROP",
  41. "output": "DROP",
  42. "forward": "DROP",
  43. "device": "zone2"
  44. },
  45. {
  46. ".description": "Zone reject policies should map to reject rules from/to covered interfaces",
  47. "name": "test3",
  48. "input": "REJECT",
  49. "output": "REJECT",
  50. "forward": "REJECT",
  51. "device": "zone3"
  52. }
  53. ]
  54. }
  55. -- End --
  56. -- Expect stdout --
  57. table inet fw4
  58. flush table inet fw4
  59. table inet fw4 {
  60. #
  61. # Defines
  62. #
  63. define test1_devices = { "zone1" }
  64. define test1_subnets = { }
  65. define test2_devices = { "zone2" }
  66. define test2_subnets = { }
  67. define test3_devices = { "zone3" }
  68. define test3_subnets = { }
  69. #
  70. # User includes
  71. #
  72. include "/etc/nftables.d/*.nft"
  73. #
  74. # Filter rules
  75. #
  76. chain input {
  77. type filter hook input priority filter; policy drop;
  78. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  79. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  80. iifname "zone1" jump input_test1 comment "!fw4: Handle test1 IPv4/IPv6 input traffic"
  81. iifname "zone2" jump input_test2 comment "!fw4: Handle test2 IPv4/IPv6 input traffic"
  82. iifname "zone3" jump input_test3 comment "!fw4: Handle test3 IPv4/IPv6 input traffic"
  83. }
  84. chain forward {
  85. type filter hook forward priority filter; policy drop;
  86. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  87. iifname "zone1" jump forward_test1 comment "!fw4: Handle test1 IPv4/IPv6 forward traffic"
  88. iifname "zone2" jump forward_test2 comment "!fw4: Handle test2 IPv4/IPv6 forward traffic"
  89. iifname "zone3" jump forward_test3 comment "!fw4: Handle test3 IPv4/IPv6 forward traffic"
  90. }
  91. chain output {
  92. type filter hook output priority filter; policy drop;
  93. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  94. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  95. oifname "zone1" jump output_test1 comment "!fw4: Handle test1 IPv4/IPv6 output traffic"
  96. oifname "zone2" jump output_test2 comment "!fw4: Handle test2 IPv4/IPv6 output traffic"
  97. oifname "zone3" jump output_test3 comment "!fw4: Handle test3 IPv4/IPv6 output traffic"
  98. }
  99. chain prerouting {
  100. type filter hook prerouting priority filter; policy accept;
  101. iifname "zone1" jump helper_test1 comment "!fw4: Handle test1 IPv4/IPv6 helper assignment"
  102. iifname "zone2" jump helper_test2 comment "!fw4: Handle test2 IPv4/IPv6 helper assignment"
  103. iifname "zone3" jump helper_test3 comment "!fw4: Handle test3 IPv4/IPv6 helper assignment"
  104. }
  105. chain handle_reject {
  106. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  107. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  108. }
  109. chain input_test1 {
  110. jump accept_from_test1
  111. }
  112. chain output_test1 {
  113. jump accept_to_test1
  114. }
  115. chain forward_test1 {
  116. jump accept_to_test1
  117. }
  118. chain helper_test1 {
  119. }
  120. chain accept_from_test1 {
  121. iifname "zone1" counter accept comment "!fw4: accept test1 IPv4/IPv6 traffic"
  122. }
  123. chain accept_to_test1 {
  124. oifname "zone1" counter accept comment "!fw4: accept test1 IPv4/IPv6 traffic"
  125. }
  126. chain input_test2 {
  127. jump drop_from_test2
  128. }
  129. chain output_test2 {
  130. jump drop_to_test2
  131. }
  132. chain forward_test2 {
  133. jump drop_to_test2
  134. }
  135. chain helper_test2 {
  136. }
  137. chain drop_from_test2 {
  138. iifname "zone2" counter drop comment "!fw4: drop test2 IPv4/IPv6 traffic"
  139. }
  140. chain drop_to_test2 {
  141. oifname "zone2" counter drop comment "!fw4: drop test2 IPv4/IPv6 traffic"
  142. }
  143. chain input_test3 {
  144. jump reject_from_test3
  145. }
  146. chain output_test3 {
  147. jump reject_to_test3
  148. }
  149. chain forward_test3 {
  150. jump reject_to_test3
  151. }
  152. chain helper_test3 {
  153. }
  154. chain reject_from_test3 {
  155. iifname "zone3" counter jump handle_reject comment "!fw4: reject test3 IPv4/IPv6 traffic"
  156. }
  157. chain reject_to_test3 {
  158. oifname "zone3" counter jump handle_reject comment "!fw4: reject test3 IPv4/IPv6 traffic"
  159. }
  160. #
  161. # NAT rules
  162. #
  163. chain dstnat {
  164. type nat hook prerouting priority dstnat; policy accept;
  165. }
  166. chain srcnat {
  167. type nat hook postrouting priority srcnat; policy accept;
  168. }
  169. #
  170. # Raw rules (notrack)
  171. #
  172. chain raw_prerouting {
  173. type filter hook prerouting priority raw; policy accept;
  174. }
  175. chain raw_output {
  176. type filter hook output priority raw; policy accept;
  177. }
  178. #
  179. # Mangle rules
  180. #
  181. chain mangle_prerouting {
  182. type filter hook prerouting priority mangle; policy accept;
  183. }
  184. chain mangle_postrouting {
  185. type filter hook postrouting priority mangle; policy accept;
  186. }
  187. chain mangle_input {
  188. type filter hook input priority mangle; policy accept;
  189. }
  190. chain mangle_output {
  191. type route hook output priority mangle; policy accept;
  192. }
  193. chain mangle_forward {
  194. type filter hook forward priority mangle; policy accept;
  195. }
  196. }
  197. -- End --