01_ruleset 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. Testing the ruleset rendered from the default firewall configuration.
  2. -- Testcase --
  3. {%
  4. include("./root/usr/share/firewall4/main.uc", {
  5. TRACE_CALLS: "stderr",
  6. getenv: function(varname) {
  7. switch (varname) {
  8. case 'ACTION':
  9. return 'print';
  10. }
  11. }
  12. })
  13. %}
  14. -- End --
  15. -- Expect stdout --
  16. table inet fw4
  17. flush table inet fw4
  18. table inet fw4 {
  19. #
  20. # Flowtable
  21. #
  22. flowtable ft {
  23. hook ingress priority 0;
  24. devices = { "eth0", "eth1" };
  25. counter;
  26. flags offload;
  27. }
  28. #
  29. # CT helper definitions
  30. #
  31. ct helper amanda {
  32. type "amanda" protocol udp;
  33. }
  34. ct helper ftp {
  35. type "ftp" protocol tcp;
  36. }
  37. ct helper RAS {
  38. type "RAS" protocol udp;
  39. }
  40. ct helper Q.931 {
  41. type "Q.931" protocol tcp;
  42. }
  43. ct helper irc {
  44. type "irc" protocol tcp;
  45. }
  46. ct helper netbios-ns {
  47. type "netbios-ns" protocol udp;
  48. }
  49. ct helper pptp {
  50. type "pptp" protocol tcp;
  51. }
  52. ct helper sane {
  53. type "sane" protocol tcp;
  54. }
  55. ct helper sip {
  56. type "sip" protocol udp;
  57. }
  58. ct helper snmp {
  59. type "snmp" protocol udp;
  60. }
  61. ct helper tftp {
  62. type "tftp" protocol udp;
  63. }
  64. ct helper rtsp {
  65. type "rtsp" protocol tcp;
  66. }
  67. #
  68. # Defines
  69. #
  70. define lan_devices = { "br-lan" }
  71. define lan_subnets = { 10.0.0.0/24, 192.168.26.0/24, 2001:db8:1000::/60, fd63:e2f:f706::/60 }
  72. define wan_devices = { "pppoe-wan" }
  73. define wan_subnets = { 10.11.12.0/24, 2001:db8:54:321::/64 }
  74. #
  75. # User includes
  76. #
  77. include "/etc/nftables.d/*.nft"
  78. #
  79. # Filter rules
  80. #
  81. chain input {
  82. type filter hook input priority filter; policy drop;
  83. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  84. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  85. tcp flags & (fin | syn | rst | ack) == syn jump syn_flood comment "!fw4: Rate limit TCP syn packets"
  86. iifname "br-lan" jump input_lan comment "!fw4: Handle lan IPv4/IPv6 input traffic"
  87. iifname "pppoe-wan" jump input_wan comment "!fw4: Handle wan IPv4/IPv6 input traffic"
  88. jump handle_reject
  89. }
  90. chain forward {
  91. type filter hook forward priority filter; policy drop;
  92. meta l4proto { tcp, udp } flow offload @ft;
  93. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  94. iifname "br-lan" jump forward_lan comment "!fw4: Handle lan IPv4/IPv6 forward traffic"
  95. iifname "pppoe-wan" jump forward_wan comment "!fw4: Handle wan IPv4/IPv6 forward traffic"
  96. jump handle_reject
  97. }
  98. chain output {
  99. type filter hook output priority filter; policy accept;
  100. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  101. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  102. meta l4proto tcp counter comment "!fw4: Test-Deprecated-Rule-Option"
  103. oifname "br-lan" jump output_lan comment "!fw4: Handle lan IPv4/IPv6 output traffic"
  104. oifname "pppoe-wan" jump output_wan comment "!fw4: Handle wan IPv4/IPv6 output traffic"
  105. }
  106. chain prerouting {
  107. type filter hook prerouting priority filter; policy accept;
  108. iifname "br-lan" jump helper_lan comment "!fw4: Handle lan IPv4/IPv6 helper assignment"
  109. }
  110. chain handle_reject {
  111. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  112. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  113. }
  114. chain syn_flood {
  115. limit rate 25/second burst 50 packets return comment "!fw4: Accept SYN packets below rate-limit"
  116. drop comment "!fw4: Drop excess packets"
  117. }
  118. chain input_lan {
  119. jump accept_from_lan
  120. }
  121. chain output_lan {
  122. jump accept_to_lan
  123. }
  124. chain forward_lan {
  125. jump accept_to_wan comment "!fw4: Accept lan to wan forwarding"
  126. jump accept_to_lan
  127. }
  128. chain helper_lan {
  129. meta l4proto udp udp dport 10080 ct helper set "amanda" comment "!fw4: Amanda backup and archiving proto"
  130. meta l4proto tcp tcp dport 21 ct helper set "ftp" comment "!fw4: FTP passive connection tracking"
  131. meta l4proto udp udp dport 1719 ct helper set "RAS" comment "!fw4: RAS proto tracking"
  132. meta l4proto tcp tcp dport 1720 ct helper set "Q.931" comment "!fw4: Q.931 proto tracking"
  133. meta nfproto ipv4 meta l4proto tcp tcp dport 6667 ct helper set "irc" comment "!fw4: IRC DCC connection tracking"
  134. meta nfproto ipv4 meta l4proto udp udp dport 137 ct helper set "netbios-ns" comment "!fw4: NetBIOS name service broadcast tracking"
  135. meta nfproto ipv4 meta l4proto tcp tcp dport 1723 ct helper set "pptp" comment "!fw4: PPTP VPN connection tracking"
  136. meta l4proto tcp tcp dport 6566 ct helper set "sane" comment "!fw4: SANE scanner connection tracking"
  137. meta l4proto udp udp dport 5060 ct helper set "sip" comment "!fw4: SIP VoIP connection tracking"
  138. meta nfproto ipv4 meta l4proto udp udp dport 161 ct helper set "snmp" comment "!fw4: SNMP monitoring connection tracking"
  139. meta l4proto udp udp dport 69 ct helper set "tftp" comment "!fw4: TFTP connection tracking"
  140. meta nfproto ipv4 meta l4proto tcp tcp dport 554 ct helper set "rtsp" comment "!fw4: RTSP connection tracking"
  141. }
  142. chain accept_from_lan {
  143. iifname "br-lan" counter accept comment "!fw4: accept lan IPv4/IPv6 traffic"
  144. }
  145. chain accept_to_lan {
  146. oifname "br-lan" counter accept comment "!fw4: accept lan IPv4/IPv6 traffic"
  147. }
  148. chain input_wan {
  149. meta nfproto ipv4 udp dport 68 counter accept comment "!fw4: Allow-DHCP-Renew"
  150. meta nfproto ipv4 icmp type 8 counter accept comment "!fw4: Allow-Ping"
  151. meta nfproto ipv4 meta l4proto igmp counter accept comment "!fw4: Allow-IGMP"
  152. ip6 saddr fc00::/6 ip6 daddr fc00::/6 udp dport 546 counter accept comment "!fw4: Allow-DHCPv6"
  153. ip6 saddr fe80::/10 icmpv6 type . icmpv6 code { 130 . 0, 131 . 0, 132 . 0, 143 . 0 } counter accept comment "!fw4: Allow-MLD"
  154. meta nfproto ipv6 icmpv6 type { 128, 129, 1, 3, 133, 134 } limit rate 1000/second counter accept comment "!fw4: Allow-ICMPv6-Input"
  155. meta nfproto ipv6 icmpv6 type . icmpv6 code { 2 . 0, 4 . 0, 4 . 1, 135 . 0, 136 . 0 } limit rate 1000/second counter accept comment "!fw4: Allow-ICMPv6-Input"
  156. jump reject_from_wan
  157. }
  158. chain output_wan {
  159. jump accept_to_wan
  160. }
  161. chain forward_wan {
  162. meta nfproto ipv6 icmpv6 type { 128, 129, 1, 3 } limit rate 1000/second counter accept comment "!fw4: Allow-ICMPv6-Forward"
  163. meta nfproto ipv6 icmpv6 type . icmpv6 code { 2 . 0, 4 . 0, 4 . 1 } limit rate 1000/second counter accept comment "!fw4: Allow-ICMPv6-Forward"
  164. meta l4proto esp counter jump accept_to_lan comment "!fw4: Allow-IPSec-ESP"
  165. udp dport 500 counter jump accept_to_lan comment "!fw4: Allow-ISAKMP"
  166. jump reject_to_wan
  167. }
  168. chain accept_to_wan {
  169. meta nfproto ipv4 oifname "pppoe-wan" ct state invalid counter drop comment "!fw4: Prevent NAT leakage"
  170. oifname "pppoe-wan" counter accept comment "!fw4: accept wan IPv4/IPv6 traffic"
  171. }
  172. chain reject_from_wan {
  173. iifname "pppoe-wan" counter jump handle_reject comment "!fw4: reject wan IPv4/IPv6 traffic"
  174. }
  175. chain reject_to_wan {
  176. oifname "pppoe-wan" counter jump handle_reject comment "!fw4: reject wan IPv4/IPv6 traffic"
  177. }
  178. #
  179. # NAT rules
  180. #
  181. chain dstnat {
  182. type nat hook prerouting priority dstnat; policy accept;
  183. }
  184. chain srcnat {
  185. type nat hook postrouting priority srcnat; policy accept;
  186. oifname "pppoe-wan" jump srcnat_wan comment "!fw4: Handle wan IPv4/IPv6 srcnat traffic"
  187. }
  188. chain srcnat_wan {
  189. meta nfproto ipv4 masquerade comment "!fw4: Masquerade IPv4 wan traffic"
  190. }
  191. #
  192. # Raw rules (notrack)
  193. #
  194. chain raw_prerouting {
  195. type filter hook prerouting priority raw; policy accept;
  196. }
  197. chain raw_output {
  198. type filter hook output priority raw; policy accept;
  199. }
  200. #
  201. # Mangle rules
  202. #
  203. chain mangle_prerouting {
  204. type filter hook prerouting priority mangle; policy accept;
  205. }
  206. chain mangle_postrouting {
  207. type filter hook postrouting priority mangle; policy accept;
  208. oifname "pppoe-wan" tcp flags syn / syn,fin,rst tcp option maxseg size set rt mtu comment "!fw4: Zone wan IPv4/IPv6 egress MTU fixing"
  209. }
  210. chain mangle_input {
  211. type filter hook input priority mangle; policy accept;
  212. }
  213. chain mangle_output {
  214. type route hook output priority mangle; policy accept;
  215. }
  216. chain mangle_forward {
  217. type filter hook forward priority mangle; policy accept;
  218. iifname "pppoe-wan" tcp flags syn / syn,fin,rst tcp option maxseg size set rt mtu comment "!fw4: Zone wan IPv4/IPv6 ingress MTU fixing"
  219. }
  220. }
  221. -- End --
  222. -- Expect stderr --
  223. [call] ctx.call object <network.interface> method <dump> args <null>
  224. [call] ctx.call object <service> method <get_data> args <{ "type": "firewall" }>
  225. [call] fs.open path </proc/version> mode <r>
  226. [call] fs.stat path </sys/module/nf_conntrack_amanda>
  227. [call] fs.stat path </sys/module/nf_conntrack_ftp>
  228. [call] fs.stat path </sys/module/nf_conntrack_h323>
  229. [call] fs.stat path </sys/module/nf_conntrack_h323>
  230. [call] fs.stat path </sys/module/nf_conntrack_irc>
  231. [call] fs.stat path </sys/module/nf_conntrack_netbios_ns>
  232. [call] fs.stat path </sys/module/nf_conntrack_pptp>
  233. [call] fs.stat path </sys/module/nf_conntrack_sane>
  234. [call] fs.stat path </sys/module/nf_conntrack_sip>
  235. [call] fs.stat path </sys/module/nf_conntrack_snmp>
  236. [call] fs.stat path </sys/module/nf_conntrack_tftp>
  237. [call] fs.stat path </sys/module/nf_conntrack_rtsp>
  238. [!] Section @defaults[0] specifies unknown option 'unknown_defaults_option'
  239. [!] Section @rule[9] (Test-Deprecated-Rule-Option) option '_name' is deprecated by fw4
  240. [!] Section @rule[9] (Test-Deprecated-Rule-Option) specifies unknown option 'unknown_rule_option'
  241. [call] fs.glob pattern </usr/share/nftables.d/ruleset-pre/*.nft>
  242. [call] fs.glob pattern </usr/share/nftables.d/ruleset-post/*.nft>
  243. [call] fs.glob pattern </usr/share/nftables.d/table-pre/*.nft>
  244. [call] fs.glob pattern </usr/share/nftables.d/table-post/*.nft>
  245. [call] fs.lsdir path </usr/share/nftables.d/chain-pre>
  246. [call] fs.lsdir path </usr/share/nftables.d/chain-post>
  247. [call] ctx.call object <network.device> method <status> args <null>
  248. [call] fs.opendir path </sys/class/net/br-lan>
  249. [call] fs.opendir path </sys/class/net/eth0>
  250. [call] fs.opendir path </sys/class/net/eth1>
  251. [call] system command </usr/sbin/nft -c '
  252. add table inet fw4-hw-offload-test;
  253. add flowtable inet fw4-hw-offload-test ft {
  254. hook ingress priority 0;
  255. devices = { "eth0", "eth1" };
  256. flags offload;
  257. }
  258. ' 2>/dev/null> timeout <null>
  259. [call] fs.popen cmdline </usr/sbin/nft --terse --json list flowtables inet> mode <r>
  260. [call] fs.readfile path </sys/class/net/br-lan/flags> limit <null>
  261. [call] fs.readfile path </sys/class/net/br-lan/flags> limit <null>
  262. -- End --