07_redirect 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. Test various address selection rules in redirect rules.
  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 uci/firewall.json --
  18. {
  19. "zone": [
  20. {
  21. "name": "wan",
  22. "network": [ "wan", "wan6" ],
  23. "masq": 1,
  24. "masq6": 1
  25. },
  26. {
  27. "name": "lan",
  28. "network": "lan",
  29. "auto_helper": 0
  30. },
  31. {
  32. "name": "noaddr",
  33. "network": [ "noaddr" ],
  34. "masq": 1,
  35. "masq6": 1
  36. }
  37. ],
  38. "redirect": [
  39. {
  40. ".description": "Ensure unspecified family with no src, dest or rewrite address is treated as IPv4 only",
  41. "name": "Redirect test #1",
  42. "src": "lan",
  43. "dest": "wan",
  44. "proto": "udp",
  45. "src_dport": "53",
  46. "dest_port": "53",
  47. "target": "dnat"
  48. },
  49. {
  50. ".description": "Ensure that explicit family any with no src, dest or rewrite address is treated as IPv4/IPv6",
  51. "name": "Redirect test #2",
  52. "family": "any",
  53. "src": "lan",
  54. "dest": "wan",
  55. "proto": "udp",
  56. "src_dport": "53",
  57. "dest_port": "53",
  58. "target": "dnat"
  59. },
  60. {
  61. ".description": "Ensure that a DNAT without explicit dest zone infers the zone from the rewrite address",
  62. "name": "Redirect test #3",
  63. "src": "wan",
  64. "proto": "tcp",
  65. "src_dport": "22",
  66. "dest_ip": "192.168.26.100"
  67. },
  68. {
  69. ".description": "Ensure that external reflection source uses the src zone addr as reflection source IP",
  70. "name": "Redirect test #4",
  71. "src": "wan",
  72. "proto": "tcp",
  73. "src_dport": "23",
  74. "dest_ip": "192.168.26.100",
  75. "reflection_src": "external"
  76. },
  77. {
  78. ".description": "Ensure that reflection is disabled if external address cannot be determined",
  79. "name": "Redirect test #5",
  80. "src": "noaddr",
  81. "dest": "lan",
  82. "proto": "tcp",
  83. "src_dport": "24",
  84. "dest_ip": "192.168.26.100"
  85. },
  86. {
  87. ".description": "Ensure that the rewrite IPv6 address is using bracket notation when a port is specified",
  88. "name": "Redirect test #6",
  89. "family": "ipv6",
  90. "src": "wan",
  91. "dest": "lan",
  92. "proto": "tcp",
  93. "src_dport": "25",
  94. "dest_ip": "2001:db8:1000:1::1234",
  95. "dest_port": "25",
  96. "target": "dnat"
  97. },
  98. {
  99. ".description": "Ensure that family restricted redirect rules work properly",
  100. "name": "Redirect test #7",
  101. "family": "ipv4",
  102. "src": "wan",
  103. "dest": "lan",
  104. "proto": "tcp",
  105. "src_dport": "26",
  106. "dest_port": "26",
  107. "target": "dnat"
  108. },
  109. {
  110. ".description": "Ensure that family restricted redirect rules work properly",
  111. "name": "Redirect test #8",
  112. "family": "ipv6",
  113. "src": "wan",
  114. "dest": "lan",
  115. "proto": "tcp",
  116. "src_dport": "27",
  117. "dest_port": "27",
  118. "target": "dnat"
  119. }
  120. ]
  121. }
  122. -- End --
  123. -- Expect stderr --
  124. [!] Section @redirect[2] (Redirect test #3) does not specify a destination, assuming 'lan'
  125. [!] Section @redirect[3] (Redirect test #4) does not specify a destination, assuming 'lan'
  126. [!] Section @redirect[4] (Redirect test #5) external address range cannot be determined, disabling reflection
  127. -- End --
  128. -- Expect stdout --
  129. table inet fw4
  130. flush table inet fw4
  131. table inet fw4 {
  132. #
  133. # Defines
  134. #
  135. define wan_devices = { "pppoe-wan" }
  136. define wan_subnets = { 10.11.12.0/24, 2001:db8:54:321::/64 }
  137. define lan_devices = { "br-lan" }
  138. define lan_subnets = { 10.0.0.0/24, 192.168.26.0/24, 2001:db8:1000::/60, fd63:e2f:f706::/60 }
  139. define noaddr_devices = { "wwan0" }
  140. define noaddr_subnets = { }
  141. #
  142. # User includes
  143. #
  144. include "/etc/nftables.d/*.nft"
  145. #
  146. # Filter rules
  147. #
  148. chain input {
  149. type filter hook input priority filter; policy drop;
  150. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  151. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  152. iifname "pppoe-wan" jump input_wan comment "!fw4: Handle wan IPv4/IPv6 input traffic"
  153. iifname "br-lan" jump input_lan comment "!fw4: Handle lan IPv4/IPv6 input traffic"
  154. iifname "wwan0" jump input_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 input traffic"
  155. }
  156. chain forward {
  157. type filter hook forward priority filter; policy drop;
  158. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  159. iifname "pppoe-wan" jump forward_wan comment "!fw4: Handle wan IPv4/IPv6 forward traffic"
  160. iifname "br-lan" jump forward_lan comment "!fw4: Handle lan IPv4/IPv6 forward traffic"
  161. iifname "wwan0" jump forward_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 forward traffic"
  162. }
  163. chain output {
  164. type filter hook output priority filter; policy drop;
  165. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  166. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  167. oifname "pppoe-wan" jump output_wan comment "!fw4: Handle wan IPv4/IPv6 output traffic"
  168. oifname "br-lan" jump output_lan comment "!fw4: Handle lan IPv4/IPv6 output traffic"
  169. oifname "wwan0" jump output_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 output traffic"
  170. }
  171. chain prerouting {
  172. type filter hook prerouting priority filter; policy accept;
  173. }
  174. chain handle_reject {
  175. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  176. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  177. }
  178. chain input_wan {
  179. ct status dnat accept comment "!fw4: Accept port redirections"
  180. jump drop_from_wan
  181. }
  182. chain output_wan {
  183. jump drop_to_wan
  184. }
  185. chain forward_wan {
  186. ct status dnat accept comment "!fw4: Accept port forwards"
  187. jump drop_to_wan
  188. }
  189. chain drop_from_wan {
  190. iifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
  191. }
  192. chain drop_to_wan {
  193. oifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
  194. }
  195. chain input_lan {
  196. ct status dnat accept comment "!fw4: Accept port redirections"
  197. jump drop_from_lan
  198. }
  199. chain output_lan {
  200. jump drop_to_lan
  201. }
  202. chain forward_lan {
  203. ct status dnat accept comment "!fw4: Accept port forwards"
  204. jump drop_to_lan
  205. }
  206. chain accept_to_lan {
  207. oifname "br-lan" counter accept comment "!fw4: accept lan IPv4/IPv6 traffic"
  208. }
  209. chain drop_from_lan {
  210. iifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  211. }
  212. chain drop_to_lan {
  213. oifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  214. }
  215. chain input_noaddr {
  216. ct status dnat accept comment "!fw4: Accept port redirections"
  217. jump drop_from_noaddr
  218. }
  219. chain output_noaddr {
  220. jump drop_to_noaddr
  221. }
  222. chain forward_noaddr {
  223. ct status dnat accept comment "!fw4: Accept port forwards"
  224. jump drop_to_noaddr
  225. }
  226. chain drop_from_noaddr {
  227. iifname "wwan0" counter drop comment "!fw4: drop noaddr IPv4/IPv6 traffic"
  228. }
  229. chain drop_to_noaddr {
  230. oifname "wwan0" counter drop comment "!fw4: drop noaddr IPv4/IPv6 traffic"
  231. }
  232. #
  233. # NAT rules
  234. #
  235. chain dstnat {
  236. type nat hook prerouting priority dstnat; policy accept;
  237. iifname "pppoe-wan" jump dstnat_wan comment "!fw4: Handle wan IPv4/IPv6 dstnat traffic"
  238. iifname "br-lan" jump dstnat_lan comment "!fw4: Handle lan IPv4/IPv6 dstnat traffic"
  239. iifname "wwan0" jump dstnat_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 dstnat traffic"
  240. }
  241. chain srcnat {
  242. type nat hook postrouting priority srcnat; policy accept;
  243. oifname "pppoe-wan" jump srcnat_wan comment "!fw4: Handle wan IPv4/IPv6 srcnat traffic"
  244. oifname "br-lan" jump srcnat_lan comment "!fw4: Handle lan IPv4/IPv6 srcnat traffic"
  245. oifname "wwan0" jump srcnat_noaddr comment "!fw4: Handle noaddr IPv4/IPv6 srcnat traffic"
  246. }
  247. chain dstnat_wan {
  248. meta nfproto ipv4 tcp dport 22 counter dnat 192.168.26.100:22 comment "!fw4: Redirect test #3"
  249. meta nfproto ipv4 tcp dport 23 counter dnat 192.168.26.100:23 comment "!fw4: Redirect test #4"
  250. meta nfproto ipv6 tcp dport 25 counter dnat [2001:db8:1000:1::1234]:25 comment "!fw4: Redirect test #6"
  251. meta nfproto ipv4 tcp dport 26 counter redirect to 26 comment "!fw4: Redirect test #7"
  252. meta nfproto ipv6 tcp dport 27 counter redirect to 27 comment "!fw4: Redirect test #8"
  253. }
  254. chain srcnat_wan {
  255. meta nfproto ipv4 masquerade comment "!fw4: Masquerade IPv4 wan traffic"
  256. meta nfproto ipv6 masquerade comment "!fw4: Masquerade IPv6 wan traffic"
  257. }
  258. chain dstnat_lan {
  259. meta nfproto ipv4 udp dport 53 counter redirect to 53 comment "!fw4: Redirect test #1"
  260. udp dport 53 counter redirect to 53 comment "!fw4: Redirect test #2"
  261. ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 10.11.12.194 tcp dport 22 dnat 192.168.26.100:22 comment "!fw4: Redirect test #3 (reflection)"
  262. ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 10.11.12.194 tcp dport 23 dnat 192.168.26.100:23 comment "!fw4: Redirect test #4 (reflection)"
  263. ip6 saddr { 2001:db8:1000::/60, fd63:e2f:f706::/60 } ip6 daddr 2001:db8:54:321::2 tcp dport 25 dnat [2001:db8:1000:1::1234]:25 comment "!fw4: Redirect test #6 (reflection)"
  264. }
  265. chain srcnat_lan {
  266. ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 192.168.26.100 tcp dport 22 snat 192.168.26.1 comment "!fw4: Redirect test #3 (reflection)"
  267. ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 192.168.26.100 tcp dport 23 snat 10.11.12.194 comment "!fw4: Redirect test #4 (reflection)"
  268. ip6 saddr { 2001:db8:1000::/60, fd63:e2f:f706::/60 } ip6 daddr 2001:db8:1000:1::1234 tcp dport 25 snat 2001:db8:1000:1::1 comment "!fw4: Redirect test #6 (reflection)"
  269. }
  270. chain dstnat_noaddr {
  271. meta nfproto ipv4 tcp dport 24 counter dnat 192.168.26.100:24 comment "!fw4: Redirect test #5"
  272. }
  273. chain srcnat_noaddr {
  274. meta nfproto ipv4 masquerade comment "!fw4: Masquerade IPv4 noaddr traffic"
  275. meta nfproto ipv6 masquerade comment "!fw4: Masquerade IPv6 noaddr traffic"
  276. }
  277. #
  278. # Raw rules (notrack)
  279. #
  280. chain raw_prerouting {
  281. type filter hook prerouting priority raw; policy accept;
  282. }
  283. chain raw_output {
  284. type filter hook output priority raw; policy accept;
  285. }
  286. #
  287. # Mangle rules
  288. #
  289. chain mangle_prerouting {
  290. type filter hook prerouting priority mangle; policy accept;
  291. }
  292. chain mangle_postrouting {
  293. type filter hook postrouting priority mangle; policy accept;
  294. }
  295. chain mangle_input {
  296. type filter hook input priority mangle; policy accept;
  297. }
  298. chain mangle_output {
  299. type route hook output priority mangle; policy accept;
  300. }
  301. chain mangle_forward {
  302. type filter hook forward priority mangle; policy accept;
  303. }
  304. }
  305. -- End --