08_family_inheritance 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. Testing various option constraints.
  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. ".description": "A zone matching only IPv4 subnets is assumed to be an IPv4 only zone",
  22. "name": "ipv4only",
  23. "subnet": "192.168.1.0/24",
  24. "auto_helper": 0
  25. },
  26. {
  27. ".description": "A zone with conflicting family and subnet settings should be skipped",
  28. "name": "afconflict",
  29. "subnet": "10.0.0.0/8",
  30. "family": "IPv6",
  31. "auto_helper": 0
  32. }
  33. ],
  34. "ipset": [
  35. {
  36. "name": "ipv4set",
  37. "match": "src_ip",
  38. "entry": [
  39. "10.0.0.2",
  40. "10.0.0.3",
  41. "10.0.0.4"
  42. ]
  43. }
  44. ],
  45. "rule": [
  46. {
  47. ".description": "Rules referencing an IPv4 only zone should be restricted to IPv4 themselves",
  48. "src": "ipv4only",
  49. "proto": "tcp",
  50. "dest_port": "22",
  51. "name": "Rule #1",
  52. "target": "accept"
  53. },
  54. {
  55. ".description": "Rules whose family conflicts with their addresses should be skipped",
  56. "proto": "tcp",
  57. "src_ip": "10.0.0.1",
  58. "dest_port": "22",
  59. "name": "Rule #2",
  60. "target": "accept",
  61. "family": "IPv6"
  62. },
  63. {
  64. ".description": "Rules whose family conflicts with the zone family should be skipped",
  65. "src": "ipv4only",
  66. "proto": "tcp",
  67. "dest_port": "22",
  68. "name": "Rule #3",
  69. "target": "accept",
  70. "family": "IPv6"
  71. },
  72. {
  73. ".description": "Rules whose family conflicts with the referenced set family should be skipped",
  74. "src": "ipv4only",
  75. "proto": "tcp",
  76. "ipset": "ipv4set",
  77. "name": "Rule #4",
  78. "target": "accept",
  79. "family": "IPv6"
  80. }
  81. ],
  82. "redirect": [
  83. {
  84. ".description": "Redirects whose family conflicts with the referenced zone family should be skipped",
  85. "src": "ipv4only",
  86. "proto": "tcp",
  87. "src_dport": "22",
  88. "dest_ip": "fdca::1",
  89. "name": "Redirect #1",
  90. "target": "dnat"
  91. },
  92. ],
  93. "nat": [
  94. {
  95. ".description": "NAT rules whose family conflicts with the referenced zone family should be skipped",
  96. "name": "NAT #1",
  97. "family": "ipv6",
  98. "src": "ipv4only",
  99. "target": "masquerade"
  100. },
  101. {
  102. ".description": "NAT rules whose family conflicts with their addresses should be skipped",
  103. "name": "NAT #2",
  104. "family": "ipv4",
  105. "src": "*",
  106. "src_ip": "fc00::/7",
  107. "target": "masquerade"
  108. },
  109. {
  110. ".description": "NAT rules without any AF specific bits and unspecified family should default to IPv4 for backwards compatibility",
  111. "name": "NAT #3",
  112. "src": "*",
  113. "target": "masquerade"
  114. },
  115. {
  116. ".description": "NAT rules without explicit family but IPv6 specific bits should be IPv6",
  117. "name": "NAT #4",
  118. "src": "*",
  119. "src_ip": "fc00::/7",
  120. "target": "masquerade"
  121. },
  122. {
  123. ".description": "NAT rules with explicit family any should inherit zone restrictions",
  124. "name": "NAT #5",
  125. "src": "ipv4only",
  126. "target": "masquerade"
  127. },
  128. {
  129. ".description": "NAT rules without any AF specific bits but explicit family any should be IPv4/IPv6",
  130. "name": "NAT #6",
  131. "family": "any",
  132. "src": "*",
  133. "target": "masquerade"
  134. }
  135. ]
  136. }
  137. -- End --
  138. -- Expect stderr --
  139. [!] Section @zone[1] (afconflict) is restricted to IPv6 but referenced subnet list is IPv4 only, skipping
  140. [!] Section @rule[1] (Rule #2) is restricted to IPv6 but referenced source IP is IPv4 only, skipping
  141. [!] Section @rule[2] (Rule #3) is restricted to IPv6 but referenced source zone is IPv4 only, skipping
  142. [!] Section @rule[3] (Rule #4) is restricted to IPv6 but referenced set match is IPv4 only, skipping
  143. [!] Section @redirect[0] (Redirect #1) is restricted to IPv6 but referenced source zone is IPv4 only, skipping
  144. [!] Section @nat[0] (NAT #1) is restricted to IPv6 but referenced source zone is IPv4 only, skipping
  145. [!] Section @nat[1] (NAT #2) is restricted to IPv4 but referenced source IP is IPv6 only, skipping
  146. -- End --
  147. -- Expect stdout --
  148. table inet fw4
  149. flush table inet fw4
  150. table inet fw4 {
  151. #
  152. # Set definitions
  153. #
  154. set ipv4set {
  155. type ipv4_addr
  156. elements = {
  157. 10.0.0.2,
  158. 10.0.0.3,
  159. 10.0.0.4,
  160. }
  161. }
  162. #
  163. # Defines
  164. #
  165. define ipv4only_devices = { }
  166. define ipv4only_subnets = { 192.168.1.0/24 }
  167. #
  168. # User includes
  169. #
  170. include "/etc/nftables.d/*.nft"
  171. #
  172. # Filter rules
  173. #
  174. chain input {
  175. type filter hook input priority filter; policy drop;
  176. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  177. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  178. meta nfproto ipv4 ip saddr 192.168.1.0/24 jump input_ipv4only comment "!fw4: Handle ipv4only IPv4 input traffic"
  179. }
  180. chain forward {
  181. type filter hook forward priority filter; policy drop;
  182. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  183. meta nfproto ipv4 ip saddr 192.168.1.0/24 jump forward_ipv4only comment "!fw4: Handle ipv4only IPv4 forward traffic"
  184. }
  185. chain output {
  186. type filter hook output priority filter; policy drop;
  187. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  188. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  189. meta nfproto ipv4 ip daddr 192.168.1.0/24 jump output_ipv4only comment "!fw4: Handle ipv4only IPv4 output traffic"
  190. }
  191. chain prerouting {
  192. type filter hook prerouting priority filter; policy accept;
  193. }
  194. chain handle_reject {
  195. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  196. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  197. }
  198. chain input_ipv4only {
  199. meta nfproto ipv4 tcp dport 22 counter accept comment "!fw4: Rule #1"
  200. ct status dnat accept comment "!fw4: Accept port redirections"
  201. jump drop_from_ipv4only
  202. }
  203. chain output_ipv4only {
  204. jump drop_to_ipv4only
  205. }
  206. chain forward_ipv4only {
  207. ct status dnat accept comment "!fw4: Accept port forwards"
  208. jump drop_to_ipv4only
  209. }
  210. chain drop_from_ipv4only {
  211. meta nfproto ipv4 ip saddr 192.168.1.0/24 counter drop comment "!fw4: drop ipv4only IPv4 traffic"
  212. }
  213. chain drop_to_ipv4only {
  214. meta nfproto ipv4 ip daddr 192.168.1.0/24 counter drop comment "!fw4: drop ipv4only IPv4 traffic"
  215. }
  216. #
  217. # NAT rules
  218. #
  219. chain dstnat {
  220. type nat hook prerouting priority dstnat; policy accept;
  221. meta nfproto ipv4 ip saddr 192.168.1.0/24 jump dstnat_ipv4only comment "!fw4: Handle ipv4only IPv4 dstnat traffic"
  222. }
  223. chain srcnat {
  224. type nat hook postrouting priority srcnat; policy accept;
  225. meta nfproto ipv4 counter masquerade comment "!fw4: NAT #3"
  226. ip6 saddr fc00::/7 counter masquerade comment "!fw4: NAT #4"
  227. counter masquerade comment "!fw4: NAT #6"
  228. meta nfproto ipv4 ip daddr 192.168.1.0/24 jump srcnat_ipv4only comment "!fw4: Handle ipv4only IPv4 srcnat traffic"
  229. }
  230. chain dstnat_ipv4only {
  231. }
  232. chain srcnat_ipv4only {
  233. meta nfproto ipv4 counter masquerade comment "!fw4: NAT #5"
  234. }
  235. #
  236. # Raw rules (notrack)
  237. #
  238. chain raw_prerouting {
  239. type filter hook prerouting priority raw; policy accept;
  240. }
  241. chain raw_output {
  242. type filter hook output priority raw; policy accept;
  243. }
  244. #
  245. # Mangle rules
  246. #
  247. chain mangle_prerouting {
  248. type filter hook prerouting priority mangle; policy accept;
  249. }
  250. chain mangle_postrouting {
  251. type filter hook postrouting priority mangle; policy accept;
  252. }
  253. chain mangle_input {
  254. type filter hook input priority mangle; policy accept;
  255. }
  256. chain mangle_output {
  257. type route hook output priority mangle; policy accept;
  258. }
  259. chain mangle_forward {
  260. type filter hook forward priority mangle; policy accept;
  261. }
  262. }
  263. -- End --