04_wildcard_devices 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. Test that wildcard devices are properly handled.
  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 fs/open~_sys_class_net_never_flags.txt --
  18. 0x0
  19. -- End --
  20. -- File fs/open~_sys_class_net_test_flags.txt --
  21. 0x0
  22. -- End --
  23. -- File fs/open~_sys_class_net_foo_flags.txt --
  24. 0x0
  25. -- End --
  26. -- File fs/open~_sys_class_net_bar_flags.txt --
  27. 0x0
  28. -- End --
  29. -- File fs/open~_sys_class_net_baz_flags.txt --
  30. 0x0
  31. -- End --
  32. -- File fs/open~_sys_class_net_qrx_flags.txt --
  33. 0x0
  34. -- End --
  35. -- File fs/open~_sys_class_net_test1_flags.txt --
  36. 0x1103
  37. -- End --
  38. -- File fs/open~_sys_class_net_test2_flags.txt --
  39. 0x1103
  40. -- End --
  41. -- File uci/firewall.json --
  42. {
  43. "zone": [
  44. {
  45. ".description": "A '+' device match should translate to no ifname match at all",
  46. "name": "test1",
  47. "device": [ "+" ]
  48. },
  49. {
  50. ".description": "An inverted '+' device match should result in a match that always fails",
  51. "name": "test2",
  52. "device": [ "!+" ]
  53. },
  54. {
  55. ".description": "A 'name+' device match should translate to an nft wildcard pattern",
  56. "name": "test3",
  57. "device": [ "test+" ]
  58. },
  59. {
  60. ".description": "Wildcard matches must not be grouped into sets",
  61. "name": "test4",
  62. "device": [ "foo+", "bar+", "test1", "test2" ]
  63. },
  64. {
  65. ".description": "Multiple inverted wildcard matches may be grouped into one rule",
  66. "name": "test5",
  67. "device": [ "foo+", "bar+", "!baz+", "!qrx+", "test1", "test2", "!test3", "!test4" ]
  68. }
  69. ]
  70. }
  71. -- End --
  72. -- Expect stdout --
  73. table inet fw4
  74. flush table inet fw4
  75. table inet fw4 {
  76. #
  77. # Defines
  78. #
  79. define test1_devices = { "+" }
  80. define test1_subnets = { }
  81. define test2_devices = { "/never/" }
  82. define test2_subnets = { }
  83. define test3_devices = { "test*" }
  84. define test3_subnets = { }
  85. define test4_devices = { "foo*", "bar*", "test1", "test2" }
  86. define test4_subnets = { }
  87. define test5_devices = { "foo*", "bar*", "test1", "test2" }
  88. define test5_subnets = { }
  89. #
  90. # User includes
  91. #
  92. include "/etc/nftables.d/*.nft"
  93. #
  94. # Filter rules
  95. #
  96. chain input {
  97. type filter hook input priority filter; policy drop;
  98. iifname "lo" accept comment "!fw4: Accept traffic from loopback"
  99. ct state established,related accept comment "!fw4: Allow inbound established and related flows"
  100. jump input_test1 comment "!fw4: Handle test1 IPv4/IPv6 input traffic"
  101. iifname "/never/" jump input_test2 comment "!fw4: Handle test2 IPv4/IPv6 input traffic"
  102. iifname "test*" jump input_test3 comment "!fw4: Handle test3 IPv4/IPv6 input traffic"
  103. iifname "foo*" jump input_test4 comment "!fw4: Handle test4 IPv4/IPv6 input traffic"
  104. iifname "bar*" jump input_test4 comment "!fw4: Handle test4 IPv4/IPv6 input traffic"
  105. iifname { "test1", "test2" } jump input_test4 comment "!fw4: Handle test4 IPv4/IPv6 input traffic"
  106. iifname "foo*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump input_test5 comment "!fw4: Handle test5 IPv4/IPv6 input traffic"
  107. iifname "bar*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump input_test5 comment "!fw4: Handle test5 IPv4/IPv6 input traffic"
  108. iifname { "test1", "test2" } iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump input_test5 comment "!fw4: Handle test5 IPv4/IPv6 input traffic"
  109. }
  110. chain forward {
  111. type filter hook forward priority filter; policy drop;
  112. ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
  113. jump forward_test1 comment "!fw4: Handle test1 IPv4/IPv6 forward traffic"
  114. iifname "/never/" jump forward_test2 comment "!fw4: Handle test2 IPv4/IPv6 forward traffic"
  115. iifname "test*" jump forward_test3 comment "!fw4: Handle test3 IPv4/IPv6 forward traffic"
  116. iifname "foo*" jump forward_test4 comment "!fw4: Handle test4 IPv4/IPv6 forward traffic"
  117. iifname "bar*" jump forward_test4 comment "!fw4: Handle test4 IPv4/IPv6 forward traffic"
  118. iifname { "test1", "test2" } jump forward_test4 comment "!fw4: Handle test4 IPv4/IPv6 forward traffic"
  119. iifname "foo*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump forward_test5 comment "!fw4: Handle test5 IPv4/IPv6 forward traffic"
  120. iifname "bar*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump forward_test5 comment "!fw4: Handle test5 IPv4/IPv6 forward traffic"
  121. iifname { "test1", "test2" } iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump forward_test5 comment "!fw4: Handle test5 IPv4/IPv6 forward traffic"
  122. }
  123. chain output {
  124. type filter hook output priority filter; policy drop;
  125. oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
  126. ct state established,related accept comment "!fw4: Allow outbound established and related flows"
  127. jump output_test1 comment "!fw4: Handle test1 IPv4/IPv6 output traffic"
  128. oifname "/never/" jump output_test2 comment "!fw4: Handle test2 IPv4/IPv6 output traffic"
  129. oifname "test*" jump output_test3 comment "!fw4: Handle test3 IPv4/IPv6 output traffic"
  130. oifname "foo*" jump output_test4 comment "!fw4: Handle test4 IPv4/IPv6 output traffic"
  131. oifname "bar*" jump output_test4 comment "!fw4: Handle test4 IPv4/IPv6 output traffic"
  132. oifname { "test1", "test2" } jump output_test4 comment "!fw4: Handle test4 IPv4/IPv6 output traffic"
  133. oifname "foo*" oifname != { "test3", "test4" } oifname != "baz*" oifname != "qrx*" jump output_test5 comment "!fw4: Handle test5 IPv4/IPv6 output traffic"
  134. oifname "bar*" oifname != { "test3", "test4" } oifname != "baz*" oifname != "qrx*" jump output_test5 comment "!fw4: Handle test5 IPv4/IPv6 output traffic"
  135. oifname { "test1", "test2" } oifname != { "test3", "test4" } oifname != "baz*" oifname != "qrx*" jump output_test5 comment "!fw4: Handle test5 IPv4/IPv6 output traffic"
  136. }
  137. chain prerouting {
  138. type filter hook prerouting priority filter; policy accept;
  139. iifname "/never/" jump helper_test2 comment "!fw4: Handle test2 IPv4/IPv6 helper assignment"
  140. iifname "test*" jump helper_test3 comment "!fw4: Handle test3 IPv4/IPv6 helper assignment"
  141. iifname "foo*" jump helper_test4 comment "!fw4: Handle test4 IPv4/IPv6 helper assignment"
  142. iifname "bar*" jump helper_test4 comment "!fw4: Handle test4 IPv4/IPv6 helper assignment"
  143. iifname { "test1", "test2" } jump helper_test4 comment "!fw4: Handle test4 IPv4/IPv6 helper assignment"
  144. iifname "foo*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump helper_test5 comment "!fw4: Handle test5 IPv4/IPv6 helper assignment"
  145. iifname "bar*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump helper_test5 comment "!fw4: Handle test5 IPv4/IPv6 helper assignment"
  146. iifname { "test1", "test2" } iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump helper_test5 comment "!fw4: Handle test5 IPv4/IPv6 helper assignment"
  147. }
  148. chain handle_reject {
  149. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  150. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  151. }
  152. chain input_test1 {
  153. jump drop_from_test1
  154. }
  155. chain output_test1 {
  156. jump drop_to_test1
  157. }
  158. chain forward_test1 {
  159. jump drop_to_test1
  160. }
  161. chain helper_test1 {
  162. }
  163. chain drop_from_test1 {
  164. counter drop comment "!fw4: drop test1 IPv4/IPv6 traffic"
  165. }
  166. chain drop_to_test1 {
  167. counter drop comment "!fw4: drop test1 IPv4/IPv6 traffic"
  168. }
  169. chain input_test2 {
  170. jump drop_from_test2
  171. }
  172. chain output_test2 {
  173. jump drop_to_test2
  174. }
  175. chain forward_test2 {
  176. jump drop_to_test2
  177. }
  178. chain helper_test2 {
  179. }
  180. chain drop_from_test2 {
  181. iifname "/never/" counter drop comment "!fw4: drop test2 IPv4/IPv6 traffic"
  182. }
  183. chain drop_to_test2 {
  184. oifname "/never/" counter drop comment "!fw4: drop test2 IPv4/IPv6 traffic"
  185. }
  186. chain input_test3 {
  187. jump drop_from_test3
  188. }
  189. chain output_test3 {
  190. jump drop_to_test3
  191. }
  192. chain forward_test3 {
  193. jump drop_to_test3
  194. }
  195. chain helper_test3 {
  196. }
  197. chain drop_from_test3 {
  198. iifname "test*" counter drop comment "!fw4: drop test3 IPv4/IPv6 traffic"
  199. }
  200. chain drop_to_test3 {
  201. oifname "test*" counter drop comment "!fw4: drop test3 IPv4/IPv6 traffic"
  202. }
  203. chain input_test4 {
  204. jump drop_from_test4
  205. }
  206. chain output_test4 {
  207. jump drop_to_test4
  208. }
  209. chain forward_test4 {
  210. jump drop_to_test4
  211. }
  212. chain helper_test4 {
  213. }
  214. chain drop_from_test4 {
  215. iifname "foo*" counter drop comment "!fw4: drop test4 IPv4/IPv6 traffic"
  216. iifname "bar*" counter drop comment "!fw4: drop test4 IPv4/IPv6 traffic"
  217. iifname { "test1", "test2" } counter drop comment "!fw4: drop test4 IPv4/IPv6 traffic"
  218. }
  219. chain drop_to_test4 {
  220. oifname "foo*" counter drop comment "!fw4: drop test4 IPv4/IPv6 traffic"
  221. oifname "bar*" counter drop comment "!fw4: drop test4 IPv4/IPv6 traffic"
  222. oifname { "test1", "test2" } counter drop comment "!fw4: drop test4 IPv4/IPv6 traffic"
  223. }
  224. chain input_test5 {
  225. jump drop_from_test5
  226. }
  227. chain output_test5 {
  228. jump drop_to_test5
  229. }
  230. chain forward_test5 {
  231. jump drop_to_test5
  232. }
  233. chain helper_test5 {
  234. }
  235. chain drop_from_test5 {
  236. iifname "foo*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" counter drop comment "!fw4: drop test5 IPv4/IPv6 traffic"
  237. iifname "bar*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" counter drop comment "!fw4: drop test5 IPv4/IPv6 traffic"
  238. iifname { "test1", "test2" } iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" counter drop comment "!fw4: drop test5 IPv4/IPv6 traffic"
  239. }
  240. chain drop_to_test5 {
  241. oifname "foo*" oifname != { "test3", "test4" } oifname != "baz*" oifname != "qrx*" counter drop comment "!fw4: drop test5 IPv4/IPv6 traffic"
  242. oifname "bar*" oifname != { "test3", "test4" } oifname != "baz*" oifname != "qrx*" counter drop comment "!fw4: drop test5 IPv4/IPv6 traffic"
  243. oifname { "test1", "test2" } oifname != { "test3", "test4" } oifname != "baz*" oifname != "qrx*" counter drop comment "!fw4: drop test5 IPv4/IPv6 traffic"
  244. }
  245. #
  246. # NAT rules
  247. #
  248. chain dstnat {
  249. type nat hook prerouting priority dstnat; policy accept;
  250. }
  251. chain srcnat {
  252. type nat hook postrouting priority srcnat; policy accept;
  253. }
  254. #
  255. # Raw rules (notrack)
  256. #
  257. chain raw_prerouting {
  258. type filter hook prerouting priority raw; policy accept;
  259. }
  260. chain raw_output {
  261. type filter hook output priority raw; policy accept;
  262. }
  263. #
  264. # Mangle rules
  265. #
  266. chain mangle_prerouting {
  267. type filter hook prerouting priority mangle; policy accept;
  268. }
  269. chain mangle_postrouting {
  270. type filter hook postrouting priority mangle; policy accept;
  271. }
  272. chain mangle_input {
  273. type filter hook input priority mangle; policy accept;
  274. }
  275. chain mangle_output {
  276. type route hook output priority mangle; policy accept;
  277. }
  278. chain mangle_forward {
  279. type filter hook forward priority mangle; policy accept;
  280. }
  281. }
  282. -- End --