2
0

06_subnet_mask_matches 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. Test that non-contiguous subnet masks are properly handled in rule source
  2. or destination IP expressions. Such masks need to be translated into
  3. bitwise expressions which may not appear as part of sets, so various
  4. permutations of rules need to be emitted.
  5. -- Testcase --
  6. {%
  7. include("./root/usr/share/firewall4/main.uc", {
  8. getenv: function(varname) {
  9. switch (varname) {
  10. case 'ACTION':
  11. return 'print';
  12. }
  13. }
  14. })
  15. %}
  16. -- End --
  17. -- File uci/helpers.json --
  18. {}
  19. -- End --
  20. -- File uci/firewall.json --
  21. {
  22. "zone": [
  23. {
  24. "name": "wan",
  25. "network": "wan6",
  26. "masq6": 1
  27. },
  28. {
  29. "name": "lan",
  30. "network": "lan",
  31. "auto_helper": 0
  32. },
  33. {
  34. "name": "guest",
  35. "network": "guest",
  36. "auto_helper": 0
  37. }
  38. ],
  39. "rule": [
  40. {
  41. ".description": "Ensure that IPs with non-contiguous masks are properly translated",
  42. "proto": "all",
  43. "name": "Mask rule #1",
  44. "src_ip": "::1/::ffff",
  45. "dest_ip": "!::2/::ffff"
  46. },
  47. {
  48. ".description": "Ensure that combinations of multiple masked and not masked IPs yield the proper rule permutations",
  49. "proto": "all",
  50. "name": "Mask rule #2",
  51. "src_ip": [
  52. "::1/::ffff",
  53. "::2/::ffff",
  54. "::3/128",
  55. "::4/128",
  56. "!::5/::ffff",
  57. "!::6/::ffff",
  58. "!::7/128",
  59. "!::8/128"
  60. ],
  61. "dest_ip": [
  62. "::9/::ffff",
  63. "::10/::ffff",
  64. "::11/128",
  65. "::12/128",
  66. "!::13/::ffff",
  67. "!::14/::ffff",
  68. "!::15/128",
  69. "!::16/128"
  70. ]
  71. },
  72. {
  73. ".description": "Ensure that CIDRs with negative bitcount are properly translated",
  74. "proto": "all",
  75. "name": "Mask rule #3",
  76. "src_ip": "::1/-64",
  77. "dest_ip": "!::2/-64"
  78. }
  79. ],
  80. "redirect": [
  81. {
  82. ".description": "Ensure that masked IPs are properly handled in reflection rules",
  83. "proto": "all",
  84. "name": "Mask rule #3",
  85. "src": "wan",
  86. "dest": "lan",
  87. "src_ip": "::1/::ffff",
  88. "src_dip": "::9/::ffff",
  89. "dest_ip": "::99",
  90. "dest_port": "22",
  91. "target": "DNAT",
  92. "reflection_zone": [ "lan", "guest" ]
  93. }
  94. ]
  95. }
  96. -- End --
  97. -- Expect stdout --
  98. table inet fw4
  99. flush table inet fw4
  100. table inet fw4 {
  101. #
  102. # Defines
  103. #
  104. define wan_devices = { "pppoe-wan" }
  105. define wan_subnets = { 2001:db8:54:321::/64 }
  106. define lan_devices = { "br-lan" }
  107. define lan_subnets = { 10.0.0.0/24, 192.168.26.0/24, 2001:db8:1000::/60, fd63:e2f:f706::/60 }
  108. define guest_devices = { "br-guest" }
  109. define guest_subnets = { 10.1.0.0/24, 192.168.27.0/24, 2001:db8:1000::/60, fd63:e2f:f706::/60 }
  110. #
  111. # User includes
  112. #
  113. include "/etc/nftables.d/*.nft"
  114. #
  115. # Filter rules
  116. #
  117. chain input {
  118. type filter hook input priority filter; policy drop;
  119. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  120. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  121. iifname "pppoe-wan" jump input_wan comment "!fw4: Handle wan IPv4/IPv6 input traffic"
  122. iifname "br-lan" jump input_lan comment "!fw4: Handle lan IPv4/IPv6 input traffic"
  123. iifname "br-guest" jump input_guest comment "!fw4: Handle guest IPv4/IPv6 input traffic"
  124. }
  125. chain forward {
  126. type filter hook forward priority filter; policy drop;
  127. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  128. iifname "pppoe-wan" jump forward_wan comment "!fw4: Handle wan IPv4/IPv6 forward traffic"
  129. iifname "br-lan" jump forward_lan comment "!fw4: Handle lan IPv4/IPv6 forward traffic"
  130. iifname "br-guest" jump forward_guest comment "!fw4: Handle guest IPv4/IPv6 forward traffic"
  131. }
  132. chain output {
  133. type filter hook output priority filter; policy drop;
  134. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  135. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  136. ip6 saddr & ::ffff == ::1 ip6 daddr & ::ffff != ::2 counter comment "!fw4: Mask rule #1"
  137. ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 ip6 daddr != { ::15, ::16 } ip6 daddr & ::ffff == ::9 ip6 daddr & ::ffff != ::13 ip6 daddr & ::ffff != ::14 counter comment "!fw4: Mask rule #2"
  138. ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 ip6 daddr != { ::15, ::16 } ip6 daddr & ::ffff == ::10 ip6 daddr & ::ffff != ::13 ip6 daddr & ::ffff != ::14 counter comment "!fw4: Mask rule #2"
  139. ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 ip6 daddr { ::11, ::12 } ip6 daddr != { ::15, ::16 } ip6 daddr & ::ffff != ::13 ip6 daddr & ::ffff != ::14 counter comment "!fw4: Mask rule #2"
  140. ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::2 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 ip6 daddr != { ::15, ::16 } ip6 daddr & ::ffff == ::9 ip6 daddr & ::ffff != ::13 ip6 daddr & ::ffff != ::14 counter comment "!fw4: Mask rule #2"
  141. ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::2 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 ip6 daddr != { ::15, ::16 } ip6 daddr & ::ffff == ::10 ip6 daddr & ::ffff != ::13 ip6 daddr & ::ffff != ::14 counter comment "!fw4: Mask rule #2"
  142. ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::2 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 ip6 daddr { ::11, ::12 } ip6 daddr != { ::15, ::16 } ip6 daddr & ::ffff != ::13 ip6 daddr & ::ffff != ::14 counter comment "!fw4: Mask rule #2"
  143. ip6 saddr { ::3, ::4 } ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 ip6 daddr != { ::15, ::16 } ip6 daddr & ::ffff == ::9 ip6 daddr & ::ffff != ::13 ip6 daddr & ::ffff != ::14 counter comment "!fw4: Mask rule #2"
  144. ip6 saddr { ::3, ::4 } ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 ip6 daddr != { ::15, ::16 } ip6 daddr & ::ffff == ::10 ip6 daddr & ::ffff != ::13 ip6 daddr & ::ffff != ::14 counter comment "!fw4: Mask rule #2"
  145. ip6 saddr { ::3, ::4 } ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 ip6 daddr { ::11, ::12 } ip6 daddr != { ::15, ::16 } ip6 daddr & ::ffff != ::13 ip6 daddr & ::ffff != ::14 counter comment "!fw4: Mask rule #2"
  146. ip6 saddr & ::ffff:ffff:ffff:ffff == ::1 ip6 daddr & ::ffff:ffff:ffff:ffff != ::2 counter comment "!fw4: Mask rule #3"
  147. oifname "pppoe-wan" jump output_wan comment "!fw4: Handle wan IPv4/IPv6 output traffic"
  148. oifname "br-lan" jump output_lan comment "!fw4: Handle lan IPv4/IPv6 output traffic"
  149. oifname "br-guest" jump output_guest comment "!fw4: Handle guest IPv4/IPv6 output traffic"
  150. }
  151. chain prerouting {
  152. type filter hook prerouting priority filter; policy accept;
  153. }
  154. chain handle_reject {
  155. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  156. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  157. }
  158. chain input_wan {
  159. ct status dnat accept comment "!fw4: Accept port redirections"
  160. jump drop_from_wan
  161. }
  162. chain output_wan {
  163. jump drop_to_wan
  164. }
  165. chain forward_wan {
  166. ct status dnat accept comment "!fw4: Accept port forwards"
  167. jump drop_to_wan
  168. }
  169. chain drop_from_wan {
  170. iifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
  171. }
  172. chain drop_to_wan {
  173. oifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
  174. }
  175. chain input_lan {
  176. ct status dnat accept comment "!fw4: Accept port redirections"
  177. jump drop_from_lan
  178. }
  179. chain output_lan {
  180. jump drop_to_lan
  181. }
  182. chain forward_lan {
  183. ct status dnat accept comment "!fw4: Accept port forwards"
  184. jump drop_to_lan
  185. }
  186. chain drop_from_lan {
  187. iifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  188. }
  189. chain drop_to_lan {
  190. oifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  191. }
  192. chain input_guest {
  193. ct status dnat accept comment "!fw4: Accept port redirections"
  194. jump drop_from_guest
  195. }
  196. chain output_guest {
  197. jump drop_to_guest
  198. }
  199. chain forward_guest {
  200. ct status dnat accept comment "!fw4: Accept port forwards"
  201. jump drop_to_guest
  202. }
  203. chain drop_from_guest {
  204. iifname "br-guest" counter drop comment "!fw4: drop guest IPv4/IPv6 traffic"
  205. }
  206. chain drop_to_guest {
  207. oifname "br-guest" counter drop comment "!fw4: drop guest IPv4/IPv6 traffic"
  208. }
  209. #
  210. # NAT rules
  211. #
  212. chain dstnat {
  213. type nat hook prerouting priority dstnat; policy accept;
  214. iifname "pppoe-wan" jump dstnat_wan comment "!fw4: Handle wan IPv4/IPv6 dstnat traffic"
  215. iifname "br-lan" jump dstnat_lan comment "!fw4: Handle lan IPv4/IPv6 dstnat traffic"
  216. iifname "br-guest" jump dstnat_guest comment "!fw4: Handle guest IPv4/IPv6 dstnat traffic"
  217. }
  218. chain srcnat {
  219. type nat hook postrouting priority srcnat; policy accept;
  220. oifname "pppoe-wan" jump srcnat_wan comment "!fw4: Handle wan IPv4/IPv6 srcnat traffic"
  221. oifname "br-lan" jump srcnat_lan comment "!fw4: Handle lan IPv4/IPv6 srcnat traffic"
  222. oifname "br-guest" jump srcnat_guest comment "!fw4: Handle guest IPv4/IPv6 srcnat traffic"
  223. }
  224. chain dstnat_wan {
  225. ip6 saddr & ::ffff == ::1 ip6 daddr & ::ffff == ::9 counter dnat ::99 comment "!fw4: Mask rule #3"
  226. }
  227. chain srcnat_wan {
  228. meta nfproto ipv6 masquerade comment "!fw4: Masquerade IPv6 wan traffic"
  229. }
  230. chain dstnat_lan {
  231. ip6 saddr { 2001:db8:1000::/60, fd63:e2f:f706::/60 } ip6 daddr & ::ffff == ::9 dnat ::99 comment "!fw4: Mask rule #3 (reflection)"
  232. }
  233. chain srcnat_lan {
  234. ip6 saddr { 2001:db8:1000::/60, fd63:e2f:f706::/60 } ip6 daddr ::99 snat 2001:db8:1000:1::1 comment "!fw4: Mask rule #3 (reflection)"
  235. }
  236. chain dstnat_guest {
  237. ip6 saddr { 2001:db8:1000::/60, fd63:e2f:f706::/60 } ip6 daddr & ::ffff == ::9 dnat ::99 comment "!fw4: Mask rule #3 (reflection)"
  238. }
  239. chain srcnat_guest {
  240. ip6 saddr { 2001:db8:1000::/60, fd63:e2f:f706::/60 } ip6 daddr ::99 snat 2001:db8:1000:2::1 comment "!fw4: Mask rule #3 (reflection)"
  241. }
  242. #
  243. # Raw rules (notrack)
  244. #
  245. chain raw_prerouting {
  246. type filter hook prerouting priority raw; policy accept;
  247. }
  248. chain raw_output {
  249. type filter hook output priority raw; policy accept;
  250. }
  251. #
  252. # Mangle rules
  253. #
  254. chain mangle_prerouting {
  255. type filter hook prerouting priority mangle; policy accept;
  256. }
  257. chain mangle_postrouting {
  258. type filter hook postrouting priority mangle; policy accept;
  259. }
  260. chain mangle_input {
  261. type filter hook input priority mangle; policy accept;
  262. }
  263. chain mangle_output {
  264. type route hook output priority mangle; policy accept;
  265. }
  266. chain mangle_forward {
  267. type filter hook forward priority mangle; policy accept;
  268. }
  269. }
  270. -- End --