2
0

01_nft_includes 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. Testing the correct placement of potential include positions.
  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_eth0_flags.txt --
  18. 0x1103
  19. -- End --
  20. -- File fs/open~_usr_share_nftables_d_include-ruleset-start_nft.txt --
  21. # dummy
  22. -- End --
  23. -- File fs/open~_usr_share_nftables_d_include-table-start_nft.txt --
  24. # dummy
  25. -- End --
  26. -- File fs/open~_usr_share_nftables_d_include-chain-start-forward_nft.txt --
  27. # dummy
  28. -- End --
  29. -- File fs/open~_usr_share_nftables_d_include-chain-end-forward_nft.txt --
  30. # dummy
  31. -- End --
  32. -- File fs/open~_usr_share_nftables_d_include-table-end-1_nft.txt --
  33. # dummy
  34. -- End --
  35. -- File fs/open~_usr_share_nftables_d_include-table-end-2_nft.txt --
  36. # dummy
  37. -- End --
  38. -- File fs/open~_usr_share_nftables_d_include-ruleset-end_nft.txt --
  39. # dummy
  40. -- End --
  41. -- File uci/firewall.json --
  42. {
  43. "zone": [
  44. {
  45. "name": "test",
  46. "device": [ "eth0" ],
  47. "auto_helper": 0
  48. }
  49. ],
  50. "include": [
  51. {
  52. ".description": "Position 'table-pre' (or 'table-prepend') will place an include before the first chain",
  53. "path": "/usr/share/nftables.d/include-table-start.nft",
  54. "type": "nftables",
  55. "position": "table-pre"
  56. },
  57. {
  58. ".description": "Position defaults to 'table-append', means after the last chain in the table scope",
  59. "path": "/usr/share/nftables.d/include-table-end-1.nft",
  60. "type": "nftables"
  61. },
  62. {
  63. ".description": "Position 'table-post' (or 'table-postpend') may be used as alias for 'table-append'",
  64. "path": "/usr/share/nftables.d/include-table-end-2.nft",
  65. "type": "nftables",
  66. "position": "table-post"
  67. },
  68. {
  69. ".description": "Position 'ruleset-pre' (or 'ruleset-prepend') will place an include before the table declaration",
  70. "path": "/usr/share/nftables.d/include-ruleset-start.nft",
  71. "type": "nftables",
  72. "position": "ruleset-pre"
  73. },
  74. {
  75. ".description": "Position 'ruleset-post' (or 'ruleset-append') will place an include after the table declaration",
  76. "path": "/usr/share/nftables.d/include-ruleset-end.nft",
  77. "type": "nftables",
  78. "position": "ruleset-post"
  79. },
  80. {
  81. ".description": "Position 'chain-pre' (or 'chain-prepend') will place an include at the top of a specified chain",
  82. "path": "/usr/share/nftables.d/include-chain-start-forward.nft",
  83. "type": "nftables",
  84. "position": "chain-pre",
  85. "chain": "forward"
  86. },
  87. {
  88. ".description": "Position 'chain-post' (or 'chain-append') will place an include at the bottom of a specified chain",
  89. "path": "/usr/share/nftables.d/include-chain-end-forward.nft",
  90. "type": "nftables",
  91. "position": "chain-post",
  92. "chain": "forward"
  93. },
  94. {
  95. ".description": "Position 'chain-pre' or 'chain-post' without chain option will yield and error",
  96. "path": "/usr/share/nftables.d/include-chain-end-forward.nft",
  97. "type": "nftables",
  98. "position": "chain-post"
  99. },
  100. ]
  101. }
  102. -- End --
  103. -- Expect stderr --
  104. [!] Section @include[7] must specify 'chain' for position chain-append, ignoring section
  105. -- End --
  106. -- Expect stdout --
  107. table inet fw4
  108. flush table inet fw4
  109. include "/usr/share/nftables.d/include-ruleset-start.nft"
  110. table inet fw4 {
  111. #
  112. # Defines
  113. #
  114. define test_devices = { "eth0" }
  115. define test_subnets = { }
  116. #
  117. # User includes
  118. #
  119. include "/etc/nftables.d/*.nft"
  120. include "/usr/share/nftables.d/include-table-start.nft"
  121. #
  122. # Filter rules
  123. #
  124. chain input {
  125. type filter hook input priority filter; policy drop;
  126. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  127. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  128. iifname "eth0" jump input_test comment "!fw4: Handle test IPv4/IPv6 input traffic"
  129. }
  130. chain forward {
  131. type filter hook forward priority filter; policy drop;
  132. include "/usr/share/nftables.d/include-chain-start-forward.nft"
  133. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  134. iifname "eth0" jump forward_test comment "!fw4: Handle test IPv4/IPv6 forward traffic"
  135. include "/usr/share/nftables.d/include-chain-end-forward.nft"
  136. }
  137. chain output {
  138. type filter hook output priority filter; policy drop;
  139. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  140. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  141. oifname "eth0" jump output_test comment "!fw4: Handle test IPv4/IPv6 output traffic"
  142. }
  143. chain prerouting {
  144. type filter hook prerouting priority filter; policy accept;
  145. }
  146. chain handle_reject {
  147. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  148. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  149. }
  150. chain input_test {
  151. jump drop_from_test
  152. }
  153. chain output_test {
  154. jump drop_to_test
  155. }
  156. chain forward_test {
  157. jump drop_to_test
  158. }
  159. chain drop_from_test {
  160. iifname "eth0" counter drop comment "!fw4: drop test IPv4/IPv6 traffic"
  161. }
  162. chain drop_to_test {
  163. oifname "eth0" counter drop comment "!fw4: drop test IPv4/IPv6 traffic"
  164. }
  165. #
  166. # NAT rules
  167. #
  168. chain dstnat {
  169. type nat hook prerouting priority dstnat; policy accept;
  170. }
  171. chain srcnat {
  172. type nat hook postrouting priority srcnat; policy accept;
  173. }
  174. #
  175. # Raw rules (notrack)
  176. #
  177. chain raw_prerouting {
  178. type filter hook prerouting priority raw; policy accept;
  179. }
  180. chain raw_output {
  181. type filter hook output priority raw; policy accept;
  182. }
  183. #
  184. # Mangle rules
  185. #
  186. chain mangle_prerouting {
  187. type filter hook prerouting priority mangle; policy accept;
  188. }
  189. chain mangle_postrouting {
  190. type filter hook postrouting priority mangle; policy accept;
  191. }
  192. chain mangle_input {
  193. type filter hook input priority mangle; policy accept;
  194. }
  195. chain mangle_output {
  196. type route hook output priority mangle; policy accept;
  197. }
  198. chain mangle_forward {
  199. type filter hook forward priority mangle; policy accept;
  200. }
  201. include "/usr/share/nftables.d/include-table-end-1.nft"
  202. include "/usr/share/nftables.d/include-table-end-2.nft"
  203. }
  204. include "/usr/share/nftables.d/include-ruleset-end.nft"
  205. -- End --