2
0

10_notrack 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. Ensure that NOTRACK rules end up in the appropriate chains, depending on
  2. the src and dest options.
  3. -- Testcase --
  4. {%
  5. include("./root/usr/share/firewall4/main.uc", {
  6. getenv: function(varname) {
  7. switch (varname) {
  8. case 'ACTION':
  9. return 'print';
  10. }
  11. }
  12. })
  13. %}
  14. -- End --
  15. -- File uci/helpers.json --
  16. {}
  17. -- End --
  18. -- File fs/open~_sys_class_net_eth0_flags.txt --
  19. 0x1103
  20. -- End --
  21. -- File fs/open~_sys_class_net_lo_flags.txt --
  22. 0x9
  23. -- End --
  24. -- File uci/firewall.json --
  25. {
  26. "zone": [
  27. {
  28. "name": "zone1",
  29. "device": [ "eth0" ],
  30. "auto_helper": 0
  31. },
  32. {
  33. "name": "zone2",
  34. "device": [ "lo" ],
  35. "auto_helper": 0
  36. },
  37. {
  38. "name": "zone3",
  39. "subnet": [ "127.0.0.1/8", "::1/128" ],
  40. "auto_helper": 0
  41. }
  42. ],
  43. "rule": [
  44. {
  45. ".description": "An ordinary notrack rule should end up in the raw_prerouting chain",
  46. "name": "Notrack rule #1",
  47. "src": "zone1",
  48. "target": "NOTRACK"
  49. },
  50. {
  51. ".description": "A notrack rule with loopback source device should end up in the raw_output chain",
  52. "name": "Notrack rule #2",
  53. "src": "zone2",
  54. "target": "NOTRACK"
  55. },
  56. {
  57. ".description": "A notrack rule with loopback source address should end up in the raw_output chain",
  58. "name": "Notrack rule #3",
  59. "src": "zone3",
  60. "target": "NOTRACK"
  61. }
  62. ]
  63. }
  64. -- End --
  65. -- Expect stdout --
  66. table inet fw4
  67. flush table inet fw4
  68. table inet fw4 {
  69. #
  70. # Defines
  71. #
  72. define zone1_devices = { "eth0" }
  73. define zone1_subnets = { }
  74. define zone2_devices = { "lo" }
  75. define zone2_subnets = { }
  76. define zone3_devices = { }
  77. define zone3_subnets = { 127.0.0.0/8, ::1 }
  78. #
  79. # User includes
  80. #
  81. include "/etc/nftables.d/*.nft"
  82. #
  83. # Filter rules
  84. #
  85. chain input {
  86. type filter hook input priority filter; policy drop;
  87. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  88. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  89. iifname "eth0" jump input_zone1 comment "!fw4: Handle zone1 IPv4/IPv6 input traffic"
  90. iifname "lo" jump input_zone2 comment "!fw4: Handle zone2 IPv4/IPv6 input traffic"
  91. meta nfproto ipv4 ip saddr 127.0.0.0/8 jump input_zone3 comment "!fw4: Handle zone3 IPv4 input traffic"
  92. meta nfproto ipv6 ip6 saddr ::1 jump input_zone3 comment "!fw4: Handle zone3 IPv6 input traffic"
  93. }
  94. chain forward {
  95. type filter hook forward priority filter; policy drop;
  96. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  97. iifname "eth0" jump forward_zone1 comment "!fw4: Handle zone1 IPv4/IPv6 forward traffic"
  98. iifname "lo" jump forward_zone2 comment "!fw4: Handle zone2 IPv4/IPv6 forward traffic"
  99. meta nfproto ipv4 ip saddr 127.0.0.0/8 jump forward_zone3 comment "!fw4: Handle zone3 IPv4 forward traffic"
  100. meta nfproto ipv6 ip6 saddr ::1 jump forward_zone3 comment "!fw4: Handle zone3 IPv6 forward traffic"
  101. }
  102. chain output {
  103. type filter hook output priority filter; policy drop;
  104. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  105. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  106. oifname "eth0" jump output_zone1 comment "!fw4: Handle zone1 IPv4/IPv6 output traffic"
  107. oifname "lo" jump output_zone2 comment "!fw4: Handle zone2 IPv4/IPv6 output traffic"
  108. meta nfproto ipv4 ip daddr 127.0.0.0/8 jump output_zone3 comment "!fw4: Handle zone3 IPv4 output traffic"
  109. meta nfproto ipv6 ip6 daddr ::1 jump output_zone3 comment "!fw4: Handle zone3 IPv6 output traffic"
  110. }
  111. chain prerouting {
  112. type filter hook prerouting priority filter; policy accept;
  113. }
  114. chain handle_reject {
  115. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  116. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  117. }
  118. chain input_zone1 {
  119. jump drop_from_zone1
  120. }
  121. chain output_zone1 {
  122. jump drop_to_zone1
  123. }
  124. chain forward_zone1 {
  125. jump drop_to_zone1
  126. }
  127. chain drop_from_zone1 {
  128. iifname "eth0" counter drop comment "!fw4: drop zone1 IPv4/IPv6 traffic"
  129. }
  130. chain drop_to_zone1 {
  131. oifname "eth0" counter drop comment "!fw4: drop zone1 IPv4/IPv6 traffic"
  132. }
  133. chain input_zone2 {
  134. jump drop_from_zone2
  135. }
  136. chain output_zone2 {
  137. jump drop_to_zone2
  138. }
  139. chain forward_zone2 {
  140. jump drop_to_zone2
  141. }
  142. chain drop_from_zone2 {
  143. iifname "lo" counter drop comment "!fw4: drop zone2 IPv4/IPv6 traffic"
  144. }
  145. chain drop_to_zone2 {
  146. oifname "lo" counter drop comment "!fw4: drop zone2 IPv4/IPv6 traffic"
  147. }
  148. chain input_zone3 {
  149. jump drop_from_zone3
  150. }
  151. chain output_zone3 {
  152. jump drop_to_zone3
  153. }
  154. chain forward_zone3 {
  155. jump drop_to_zone3
  156. }
  157. chain drop_from_zone3 {
  158. meta nfproto ipv4 ip saddr 127.0.0.0/8 counter drop comment "!fw4: drop zone3 IPv4 traffic"
  159. meta nfproto ipv6 ip6 saddr ::1 counter drop comment "!fw4: drop zone3 IPv6 traffic"
  160. }
  161. chain drop_to_zone3 {
  162. meta nfproto ipv4 ip daddr 127.0.0.0/8 counter drop comment "!fw4: drop zone3 IPv4 traffic"
  163. meta nfproto ipv6 ip6 daddr ::1 counter drop comment "!fw4: drop zone3 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. iifname "eth0" jump notrack_zone1 comment "!fw4: Handle zone1 IPv4/IPv6 notrack traffic"
  180. }
  181. chain raw_output {
  182. type filter hook output priority raw; policy accept;
  183. iifname "lo" jump notrack_zone2 comment "!fw4: Handle zone2 IPv4/IPv6 notrack traffic"
  184. meta nfproto ipv4 ip saddr 127.0.0.0/8 jump notrack_zone3 comment "!fw4: Handle zone3 IPv4 notrack traffic"
  185. meta nfproto ipv6 ip6 saddr ::1 jump notrack_zone3 comment "!fw4: Handle zone3 IPv6 notrack traffic"
  186. }
  187. chain notrack_zone1 {
  188. meta l4proto tcp counter notrack comment "!fw4: Notrack rule #1"
  189. meta l4proto udp counter notrack comment "!fw4: Notrack rule #1"
  190. }
  191. chain notrack_zone2 {
  192. meta l4proto tcp counter notrack comment "!fw4: Notrack rule #2"
  193. meta l4proto udp counter notrack comment "!fw4: Notrack rule #2"
  194. }
  195. chain notrack_zone3 {
  196. meta l4proto tcp counter notrack comment "!fw4: Notrack rule #3"
  197. meta l4proto udp counter notrack comment "!fw4: Notrack rule #3"
  198. }
  199. #
  200. # Mangle rules
  201. #
  202. chain mangle_prerouting {
  203. type filter hook prerouting priority mangle; policy accept;
  204. }
  205. chain mangle_postrouting {
  206. type filter hook postrouting priority mangle; policy accept;
  207. }
  208. chain mangle_input {
  209. type filter hook input priority mangle; policy accept;
  210. }
  211. chain mangle_output {
  212. type route hook output priority mangle; policy accept;
  213. }
  214. chain mangle_forward {
  215. type filter hook forward priority mangle; policy accept;
  216. }
  217. }
  218. -- End --