02_rule_order 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. Testing that `config rule` rules are rendered before `config forwarding` ones
  2. and that rules are rendered in the order they're declared.
  3. -- Testcase --
  4. {%
  5. include("./root/usr/share/firewall4/main.uc", {
  6. TRACE_CALLS: "stderr",
  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. "name": "lan",
  24. "network": "lan",
  25. "auto_helper": 0
  26. },
  27. {
  28. "name": "wan",
  29. "network": "wan",
  30. "auto_helper": 0
  31. }
  32. ],
  33. "forwarding": [
  34. {
  35. "src": "lan",
  36. "dest": "wan"
  37. }
  38. ],
  39. "rule": [
  40. {
  41. "name": "Deny rule #1",
  42. "proto": "any",
  43. "src": "lan",
  44. "dest": "wan",
  45. "src_ip": [ "192.168.1.2" ],
  46. "target": "drop"
  47. },
  48. {
  49. "name": "Deny rule #2",
  50. "proto": "icmp",
  51. "src": "lan",
  52. "dest": "wan",
  53. "src_ip": [ "192.168.1.3" ],
  54. "target": "drop"
  55. }
  56. ]
  57. }
  58. -- End --
  59. -- Expect stdout --
  60. table inet fw4
  61. flush table inet fw4
  62. table inet fw4 {
  63. #
  64. # Defines
  65. #
  66. define lan_devices = { "br-lan" }
  67. define lan_subnets = { 10.0.0.0/24, 192.168.26.0/24, 2001:db8:1000::/60, fd63:e2f:f706::/60 }
  68. define wan_devices = { "pppoe-wan" }
  69. define wan_subnets = { 10.11.12.0/24 }
  70. #
  71. # User includes
  72. #
  73. include "/etc/nftables.d/*.nft"
  74. #
  75. # Filter rules
  76. #
  77. chain input {
  78. type filter hook input priority filter; policy drop;
  79. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  80. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  81. iifname "br-lan" jump input_lan comment "!fw4: Handle lan IPv4/IPv6 input traffic"
  82. iifname "pppoe-wan" jump input_wan comment "!fw4: Handle wan 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 "br-lan" jump forward_lan comment "!fw4: Handle lan IPv4/IPv6 forward traffic"
  88. iifname "pppoe-wan" jump forward_wan comment "!fw4: Handle wan IPv4/IPv6 forward traffic"
  89. }
  90. chain output {
  91. type filter hook output priority filter; policy drop;
  92. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  93. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  94. oifname "br-lan" jump output_lan comment "!fw4: Handle lan IPv4/IPv6 output traffic"
  95. oifname "pppoe-wan" jump output_wan comment "!fw4: Handle wan IPv4/IPv6 output traffic"
  96. }
  97. chain prerouting {
  98. type filter hook prerouting priority filter; policy accept;
  99. }
  100. chain handle_reject {
  101. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  102. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  103. }
  104. chain input_lan {
  105. jump drop_from_lan
  106. }
  107. chain output_lan {
  108. jump drop_to_lan
  109. }
  110. chain forward_lan {
  111. ip saddr 192.168.1.2 counter jump drop_to_wan comment "!fw4: Deny rule #1"
  112. meta l4proto icmp ip saddr 192.168.1.3 counter jump drop_to_wan comment "!fw4: Deny rule #2"
  113. jump accept_to_wan comment "!fw4: Accept lan to wan forwarding"
  114. jump drop_to_lan
  115. }
  116. chain drop_from_lan {
  117. iifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  118. }
  119. chain drop_to_lan {
  120. oifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  121. }
  122. chain input_wan {
  123. jump drop_from_wan
  124. }
  125. chain output_wan {
  126. jump drop_to_wan
  127. }
  128. chain forward_wan {
  129. jump drop_to_wan
  130. }
  131. chain accept_to_wan {
  132. oifname "pppoe-wan" counter accept comment "!fw4: accept wan IPv4/IPv6 traffic"
  133. }
  134. chain drop_from_wan {
  135. iifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
  136. }
  137. chain drop_to_wan {
  138. oifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
  139. }
  140. #
  141. # NAT rules
  142. #
  143. chain dstnat {
  144. type nat hook prerouting priority dstnat; policy accept;
  145. }
  146. chain srcnat {
  147. type nat hook postrouting priority srcnat; policy accept;
  148. }
  149. #
  150. # Raw rules (notrack)
  151. #
  152. chain raw_prerouting {
  153. type filter hook prerouting priority raw; policy accept;
  154. }
  155. chain raw_output {
  156. type filter hook output priority raw; policy accept;
  157. }
  158. #
  159. # Mangle rules
  160. #
  161. chain mangle_prerouting {
  162. type filter hook prerouting priority mangle; policy accept;
  163. }
  164. chain mangle_postrouting {
  165. type filter hook postrouting priority mangle; policy accept;
  166. }
  167. chain mangle_input {
  168. type filter hook input priority mangle; policy accept;
  169. }
  170. chain mangle_output {
  171. type route hook output priority mangle; policy accept;
  172. }
  173. chain mangle_forward {
  174. type filter hook forward priority mangle; policy accept;
  175. }
  176. }
  177. -- End --
  178. -- Expect stderr --
  179. [call] ctx.call object <network.interface> method <dump> args <null>
  180. [call] ctx.call object <service> method <get_data> args <{ "type": "firewall" }>
  181. [call] fs.open path </proc/version> mode <r>
  182. [call] fs.glob pattern </usr/share/nftables.d/ruleset-pre/*.nft>
  183. [call] fs.glob pattern </usr/share/nftables.d/ruleset-post/*.nft>
  184. [call] fs.glob pattern </usr/share/nftables.d/table-pre/*.nft>
  185. [call] fs.glob pattern </usr/share/nftables.d/table-post/*.nft>
  186. [call] fs.lsdir path </usr/share/nftables.d/chain-pre>
  187. [call] fs.lsdir path </usr/share/nftables.d/chain-post>
  188. [call] fs.popen cmdline </usr/sbin/nft --terse --json list flowtables inet> mode <r>
  189. -- End --