06_family_selections 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. Test that the zone family is honoured regardless of whether subnets are
  2. specified or not.
  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. "helper" : [
  18. {
  19. "description" : "An example IPv4-only conntrack helper",
  20. "family" : "ipv4",
  21. "module" : "nf_conntrack_dummy",
  22. "name" : "test",
  23. "port" : 1234,
  24. "proto" : "tcp"
  25. }
  26. ]
  27. }
  28. -- End --
  29. -- File uci/firewall.json --
  30. {
  31. "zone": [
  32. {
  33. ".description": "Family any with IPv4 subnet should emit only IPv4 rules",
  34. "name": "test1",
  35. "family": "any",
  36. "subnet": [ "10.0.0.0/8" ],
  37. "auto_helper": 0
  38. },
  39. {
  40. ".description": "Family any with IPv6 subnet should emit only IPv6 rules",
  41. "name": "test2",
  42. "family": "any",
  43. "subnet": [ "2001:db8:1234::1/64" ],
  44. "auto_helper": 0
  45. },
  46. {
  47. ".description": "Family IPv6 with IPv6 subnet should emit only IPv6 rules",
  48. "name": "test3",
  49. "family": "ipv6",
  50. "subnet": [ "2001:db8:1234::1/64" ],
  51. "auto_helper": 0
  52. },
  53. {
  54. ".description": "Family IPv6 with IPv4 subnet should emit no rules",
  55. "name": "test4",
  56. "family": "ipv6",
  57. "subnet": [ "2001:db8:1234::1/64" ],
  58. "auto_helper": 0
  59. },
  60. {
  61. ".description": "Family IPv6 with no subnets should emit only IPv6 rules",
  62. "name": "test5",
  63. "family": "ipv6",
  64. "device": [ "eth0" ],
  65. "auto_helper": 0
  66. },
  67. {
  68. ".description": "Family restrictions of associated ct helpers should not influence zone family selection",
  69. "name": "test6",
  70. "family": "any",
  71. "device": [ "br-lan" ],
  72. "helper": [ "test" ]
  73. }
  74. ]
  75. }
  76. -- End --
  77. -- Expect stdout --
  78. table inet fw4
  79. flush table inet fw4
  80. table inet fw4 {
  81. #
  82. # CT helper definitions
  83. #
  84. ct helper test {
  85. type "test" protocol tcp;
  86. }
  87. #
  88. # Defines
  89. #
  90. define test1_devices = { }
  91. define test1_subnets = { 10.0.0.0/8 }
  92. define test2_devices = { }
  93. define test2_subnets = { 2001:db8:1234::/64 }
  94. define test3_devices = { }
  95. define test3_subnets = { 2001:db8:1234::/64 }
  96. define test4_devices = { }
  97. define test4_subnets = { 2001:db8:1234::/64 }
  98. define test5_devices = { "eth0" }
  99. define test5_subnets = { }
  100. define test6_devices = { "br-lan" }
  101. define test6_subnets = { }
  102. #
  103. # User includes
  104. #
  105. include "/etc/nftables.d/*.nft"
  106. #
  107. # Filter rules
  108. #
  109. chain input {
  110. type filter hook input priority filter; policy drop;
  111. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  112. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  113. meta nfproto ipv4 ip saddr 10.0.0.0/8 jump input_test1 comment "!fw4: Handle test1 IPv4 input traffic"
  114. meta nfproto ipv6 ip6 saddr 2001:db8:1234::/64 jump input_test2 comment "!fw4: Handle test2 IPv6 input traffic"
  115. meta nfproto ipv6 ip6 saddr 2001:db8:1234::/64 jump input_test3 comment "!fw4: Handle test3 IPv6 input traffic"
  116. meta nfproto ipv6 ip6 saddr 2001:db8:1234::/64 jump input_test4 comment "!fw4: Handle test4 IPv6 input traffic"
  117. meta nfproto ipv6 iifname "eth0" jump input_test5 comment "!fw4: Handle test5 IPv6 input traffic"
  118. iifname "br-lan" jump input_test6 comment "!fw4: Handle test6 IPv4/IPv6 input traffic"
  119. }
  120. chain forward {
  121. type filter hook forward priority filter; policy drop;
  122. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  123. meta nfproto ipv4 ip saddr 10.0.0.0/8 jump forward_test1 comment "!fw4: Handle test1 IPv4 forward traffic"
  124. meta nfproto ipv6 ip6 saddr 2001:db8:1234::/64 jump forward_test2 comment "!fw4: Handle test2 IPv6 forward traffic"
  125. meta nfproto ipv6 ip6 saddr 2001:db8:1234::/64 jump forward_test3 comment "!fw4: Handle test3 IPv6 forward traffic"
  126. meta nfproto ipv6 ip6 saddr 2001:db8:1234::/64 jump forward_test4 comment "!fw4: Handle test4 IPv6 forward traffic"
  127. meta nfproto ipv6 iifname "eth0" jump forward_test5 comment "!fw4: Handle test5 IPv6 forward traffic"
  128. iifname "br-lan" jump forward_test6 comment "!fw4: Handle test6 IPv4/IPv6 forward traffic"
  129. }
  130. chain output {
  131. type filter hook output priority filter; policy drop;
  132. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  133. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  134. meta nfproto ipv4 ip daddr 10.0.0.0/8 jump output_test1 comment "!fw4: Handle test1 IPv4 output traffic"
  135. meta nfproto ipv6 ip6 daddr 2001:db8:1234::/64 jump output_test2 comment "!fw4: Handle test2 IPv6 output traffic"
  136. meta nfproto ipv6 ip6 daddr 2001:db8:1234::/64 jump output_test3 comment "!fw4: Handle test3 IPv6 output traffic"
  137. meta nfproto ipv6 ip6 daddr 2001:db8:1234::/64 jump output_test4 comment "!fw4: Handle test4 IPv6 output traffic"
  138. meta nfproto ipv6 oifname "eth0" jump output_test5 comment "!fw4: Handle test5 IPv6 output traffic"
  139. oifname "br-lan" jump output_test6 comment "!fw4: Handle test6 IPv4/IPv6 output traffic"
  140. }
  141. chain prerouting {
  142. type filter hook prerouting priority filter; policy accept;
  143. iifname "br-lan" jump helper_test6 comment "!fw4: Handle test6 IPv4/IPv6 helper assignment"
  144. }
  145. chain handle_reject {
  146. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  147. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  148. }
  149. chain input_test1 {
  150. jump drop_from_test1
  151. }
  152. chain output_test1 {
  153. jump drop_to_test1
  154. }
  155. chain forward_test1 {
  156. jump drop_to_test1
  157. }
  158. chain drop_from_test1 {
  159. meta nfproto ipv4 ip saddr 10.0.0.0/8 counter drop comment "!fw4: drop test1 IPv4 traffic"
  160. }
  161. chain drop_to_test1 {
  162. meta nfproto ipv4 ip daddr 10.0.0.0/8 counter drop comment "!fw4: drop test1 IPv4 traffic"
  163. }
  164. chain input_test2 {
  165. jump drop_from_test2
  166. }
  167. chain output_test2 {
  168. jump drop_to_test2
  169. }
  170. chain forward_test2 {
  171. jump drop_to_test2
  172. }
  173. chain drop_from_test2 {
  174. meta nfproto ipv6 ip6 saddr 2001:db8:1234::/64 counter drop comment "!fw4: drop test2 IPv6 traffic"
  175. }
  176. chain drop_to_test2 {
  177. meta nfproto ipv6 ip6 daddr 2001:db8:1234::/64 counter drop comment "!fw4: drop test2 IPv6 traffic"
  178. }
  179. chain input_test3 {
  180. jump drop_from_test3
  181. }
  182. chain output_test3 {
  183. jump drop_to_test3
  184. }
  185. chain forward_test3 {
  186. jump drop_to_test3
  187. }
  188. chain drop_from_test3 {
  189. meta nfproto ipv6 ip6 saddr 2001:db8:1234::/64 counter drop comment "!fw4: drop test3 IPv6 traffic"
  190. }
  191. chain drop_to_test3 {
  192. meta nfproto ipv6 ip6 daddr 2001:db8:1234::/64 counter drop comment "!fw4: drop test3 IPv6 traffic"
  193. }
  194. chain input_test4 {
  195. jump drop_from_test4
  196. }
  197. chain output_test4 {
  198. jump drop_to_test4
  199. }
  200. chain forward_test4 {
  201. jump drop_to_test4
  202. }
  203. chain drop_from_test4 {
  204. meta nfproto ipv6 ip6 saddr 2001:db8:1234::/64 counter drop comment "!fw4: drop test4 IPv6 traffic"
  205. }
  206. chain drop_to_test4 {
  207. meta nfproto ipv6 ip6 daddr 2001:db8:1234::/64 counter drop comment "!fw4: drop test4 IPv6 traffic"
  208. }
  209. chain input_test5 {
  210. jump drop_from_test5
  211. }
  212. chain output_test5 {
  213. jump drop_to_test5
  214. }
  215. chain forward_test5 {
  216. jump drop_to_test5
  217. }
  218. chain drop_from_test5 {
  219. meta nfproto ipv6 iifname "eth0" counter drop comment "!fw4: drop test5 IPv6 traffic"
  220. }
  221. chain drop_to_test5 {
  222. meta nfproto ipv6 oifname "eth0" counter drop comment "!fw4: drop test5 IPv6 traffic"
  223. }
  224. chain input_test6 {
  225. jump drop_from_test6
  226. }
  227. chain output_test6 {
  228. jump drop_to_test6
  229. }
  230. chain forward_test6 {
  231. jump drop_to_test6
  232. }
  233. chain helper_test6 {
  234. meta nfproto ipv4 meta l4proto tcp tcp dport 1234 ct helper set "test" comment "!fw4: An example IPv4-only conntrack helper"
  235. }
  236. chain drop_from_test6 {
  237. iifname "br-lan" counter drop comment "!fw4: drop test6 IPv4/IPv6 traffic"
  238. }
  239. chain drop_to_test6 {
  240. oifname "br-lan" counter drop comment "!fw4: drop test6 IPv4/IPv6 traffic"
  241. }
  242. #
  243. # NAT rules
  244. #
  245. chain dstnat {
  246. type nat hook prerouting priority dstnat; policy accept;
  247. }
  248. chain srcnat {
  249. type nat hook postrouting priority srcnat; policy accept;
  250. }
  251. #
  252. # Raw rules (notrack)
  253. #
  254. chain raw_prerouting {
  255. type filter hook prerouting priority raw; policy accept;
  256. }
  257. chain raw_output {
  258. type filter hook output priority raw; policy accept;
  259. }
  260. #
  261. # Mangle rules
  262. #
  263. chain mangle_prerouting {
  264. type filter hook prerouting priority mangle; policy accept;
  265. }
  266. chain mangle_postrouting {
  267. type filter hook postrouting priority mangle; policy accept;
  268. }
  269. chain mangle_input {
  270. type filter hook input priority mangle; policy accept;
  271. }
  272. chain mangle_output {
  273. type route hook output priority mangle; policy accept;
  274. }
  275. chain mangle_forward {
  276. type filter hook forward priority mangle; policy accept;
  277. }
  278. }
  279. -- End --