05_mangle 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. Ensure that DSCP and MARK target rules end up in the appropriate chains,
  2. depending on the src and dest options.
  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. -- End --
  18. -- File fs/open~_sys_class_net_eth0_flags.txt --
  19. 0x1103
  20. -- End --
  21. -- File fs/open~_sys_class_net_eth1_flags.txt --
  22. 0x1103
  23. -- End --
  24. -- File fs/open~_sys_class_net_eth2_flags.txt --
  25. 0x1103
  26. -- End --
  27. -- File fs/open~_sys_class_net_eth3_flags.txt --
  28. 0x1103
  29. -- End --
  30. -- File uci/firewall.json --
  31. {
  32. "zone": [
  33. {
  34. "name": "lan",
  35. "device": [ "eth0", "eth1" ]
  36. },
  37. {
  38. "name": "wan",
  39. "device": [ "eth2", "eth3" ]
  40. }
  41. ],
  42. "rule": [
  43. {
  44. ".description": "Source '*' and destination '*' should result in a forward rule",
  45. "name": "Mangle rule #1",
  46. "src": "*",
  47. "dest": "*",
  48. "target": "DSCP",
  49. "set_dscp": "1"
  50. },
  51. {
  52. ".description": "Source zone and destination zone should result in a forward rule",
  53. "name": "Mangle rule #2",
  54. "src": "lan",
  55. "dest": "wan",
  56. "target": "DSCP",
  57. "set_dscp": "1"
  58. },
  59. {
  60. ".description": "Any source zone and specific destination zone should result in a postrouting rule",
  61. "name": "Mangle rule #3",
  62. "src": "*",
  63. "dest": "wan",
  64. "target": "DSCP",
  65. "set_dscp": "1"
  66. },
  67. {
  68. ".description": "Specific source zone and any destination zone should result in a prerouting rule",
  69. "name": "Mangle rule #4",
  70. "src": "lan",
  71. "dest": "*",
  72. "target": "DSCP",
  73. "set_dscp": "1"
  74. },
  75. {
  76. ".description": "Specific source zone and no destination zone should result in an input rule",
  77. "name": "Mangle rule #5",
  78. "src": "lan",
  79. "target": "DSCP",
  80. "set_dscp": "1"
  81. },
  82. {
  83. ".description": "Any source zone and no destination zone should result in an input rule",
  84. "name": "Mangle rule #6",
  85. "src": "*",
  86. "target": "DSCP",
  87. "set_dscp": "1"
  88. },
  89. {
  90. ".description": "No source zone and no destination zone should result in an output rule",
  91. "name": "Mangle rule #7",
  92. "target": "DSCP",
  93. "set_dscp": "1"
  94. },
  95. {
  96. ".description": "No source zone and any destination zone should result in an output rule",
  97. "name": "Mangle rule #8",
  98. "dest": "*",
  99. "target": "DSCP",
  100. "set_dscp": "1"
  101. },
  102. {
  103. ".description": "No source zone and specific destination zone should result in an output rule",
  104. "name": "Mangle rule #9",
  105. "dest": "wan",
  106. "target": "DSCP",
  107. "set_dscp": "1"
  108. },
  109. {
  110. ".description": "Option device with no direction should override inbound ifname match",
  111. "name": "Mangle rule #10",
  112. "src": "*",
  113. "dest": "wan",
  114. "target": "DSCP",
  115. "set_dscp": "1",
  116. "device": "eth4"
  117. },
  118. {
  119. ".description": "Option device with direction 'in' should override inbound ifname match",
  120. "name": "Mangle rule #11",
  121. "src": "*",
  122. "dest": "wan",
  123. "target": "DSCP",
  124. "set_dscp": "1",
  125. "device": "eth4",
  126. "direction": "in"
  127. },
  128. {
  129. ".description": "Option device with direction 'out' should override outbound ifname match",
  130. "name": "Mangle rule #12",
  131. "src": "*",
  132. "dest": "wan",
  133. "target": "DSCP",
  134. "set_dscp": "1",
  135. "device": "eth5",
  136. "direction": "out"
  137. }
  138. ]
  139. }
  140. -- End --
  141. -- Expect stdout --
  142. table inet fw4
  143. flush table inet fw4
  144. table inet fw4 {
  145. #
  146. # Defines
  147. #
  148. define lan_devices = { "eth0", "eth1" }
  149. define lan_subnets = { }
  150. define wan_devices = { "eth2", "eth3" }
  151. define wan_subnets = { }
  152. #
  153. # User includes
  154. #
  155. include "/etc/nftables.d/*.nft"
  156. #
  157. # Filter rules
  158. #
  159. chain input {
  160. type filter hook input priority filter; policy drop;
  161. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  162. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  163. iifname { "eth0", "eth1" } jump input_lan comment "!fw4: Handle lan IPv4/IPv6 input traffic"
  164. iifname { "eth2", "eth3" } jump input_wan comment "!fw4: Handle wan IPv4/IPv6 input traffic"
  165. }
  166. chain forward {
  167. type filter hook forward priority filter; policy drop;
  168. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  169. iifname { "eth0", "eth1" } jump forward_lan comment "!fw4: Handle lan IPv4/IPv6 forward traffic"
  170. iifname { "eth2", "eth3" } jump forward_wan comment "!fw4: Handle wan IPv4/IPv6 forward traffic"
  171. }
  172. chain output {
  173. type filter hook output priority filter; policy drop;
  174. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  175. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  176. oifname { "eth0", "eth1" } jump output_lan comment "!fw4: Handle lan IPv4/IPv6 output traffic"
  177. oifname { "eth2", "eth3" } jump output_wan comment "!fw4: Handle wan IPv4/IPv6 output traffic"
  178. }
  179. chain prerouting {
  180. type filter hook prerouting priority filter; policy accept;
  181. iifname { "eth0", "eth1" } jump helper_lan comment "!fw4: Handle lan IPv4/IPv6 helper assignment"
  182. iifname { "eth2", "eth3" } jump helper_wan comment "!fw4: Handle wan IPv4/IPv6 helper assignment"
  183. }
  184. chain handle_reject {
  185. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  186. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  187. }
  188. chain input_lan {
  189. jump drop_from_lan
  190. }
  191. chain output_lan {
  192. jump drop_to_lan
  193. }
  194. chain forward_lan {
  195. jump drop_to_lan
  196. }
  197. chain helper_lan {
  198. }
  199. chain drop_from_lan {
  200. iifname { "eth0", "eth1" } counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  201. }
  202. chain drop_to_lan {
  203. oifname { "eth0", "eth1" } counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  204. }
  205. chain input_wan {
  206. jump drop_from_wan
  207. }
  208. chain output_wan {
  209. jump drop_to_wan
  210. }
  211. chain forward_wan {
  212. jump drop_to_wan
  213. }
  214. chain helper_wan {
  215. }
  216. chain drop_from_wan {
  217. iifname { "eth2", "eth3" } counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
  218. }
  219. chain drop_to_wan {
  220. oifname { "eth2", "eth3" } counter drop comment "!fw4: drop wan IPv4/IPv6 traffic"
  221. }
  222. #
  223. # NAT rules
  224. #
  225. chain dstnat {
  226. type nat hook prerouting priority dstnat; policy accept;
  227. }
  228. chain srcnat {
  229. type nat hook postrouting priority srcnat; policy accept;
  230. }
  231. #
  232. # Raw rules (notrack)
  233. #
  234. chain raw_prerouting {
  235. type filter hook prerouting priority raw; policy accept;
  236. }
  237. chain raw_output {
  238. type filter hook output priority raw; policy accept;
  239. }
  240. #
  241. # Mangle rules
  242. #
  243. chain mangle_prerouting {
  244. type filter hook prerouting priority mangle; policy accept;
  245. meta nfproto ipv4 meta l4proto tcp iifname { "eth0", "eth1" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #4"
  246. meta nfproto ipv6 meta l4proto tcp iifname { "eth0", "eth1" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #4"
  247. meta nfproto ipv4 meta l4proto udp iifname { "eth0", "eth1" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #4"
  248. meta nfproto ipv6 meta l4proto udp iifname { "eth0", "eth1" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #4"
  249. }
  250. chain mangle_postrouting {
  251. type filter hook postrouting priority mangle; policy accept;
  252. meta nfproto ipv4 meta l4proto tcp oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #3"
  253. meta nfproto ipv6 meta l4proto tcp oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #3"
  254. meta nfproto ipv4 meta l4proto udp oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #3"
  255. meta nfproto ipv6 meta l4proto udp oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #3"
  256. meta nfproto ipv4 meta l4proto tcp iifname "eth4" oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #10"
  257. meta nfproto ipv6 meta l4proto tcp iifname "eth4" oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #10"
  258. meta nfproto ipv4 meta l4proto udp iifname "eth4" oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #10"
  259. meta nfproto ipv6 meta l4proto udp iifname "eth4" oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #10"
  260. meta nfproto ipv4 meta l4proto tcp iifname "eth4" oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #11"
  261. meta nfproto ipv6 meta l4proto tcp iifname "eth4" oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #11"
  262. meta nfproto ipv4 meta l4proto udp iifname "eth4" oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #11"
  263. meta nfproto ipv6 meta l4proto udp iifname "eth4" oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #11"
  264. meta nfproto ipv4 meta l4proto tcp oifname "eth5" counter ip dscp set 0x1 comment "!fw4: Mangle rule #12"
  265. meta nfproto ipv6 meta l4proto tcp oifname "eth5" counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #12"
  266. meta nfproto ipv4 meta l4proto udp oifname "eth5" counter ip dscp set 0x1 comment "!fw4: Mangle rule #12"
  267. meta nfproto ipv6 meta l4proto udp oifname "eth5" counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #12"
  268. }
  269. chain mangle_input {
  270. type filter hook input priority mangle; policy accept;
  271. meta nfproto ipv4 meta l4proto tcp iifname { "eth0", "eth1" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #5"
  272. meta nfproto ipv6 meta l4proto tcp iifname { "eth0", "eth1" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #5"
  273. meta nfproto ipv4 meta l4proto udp iifname { "eth0", "eth1" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #5"
  274. meta nfproto ipv6 meta l4proto udp iifname { "eth0", "eth1" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #5"
  275. meta nfproto ipv4 meta l4proto tcp counter ip dscp set 0x1 comment "!fw4: Mangle rule #6"
  276. meta nfproto ipv6 meta l4proto tcp counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #6"
  277. meta nfproto ipv4 meta l4proto udp counter ip dscp set 0x1 comment "!fw4: Mangle rule #6"
  278. meta nfproto ipv6 meta l4proto udp counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #6"
  279. }
  280. chain mangle_output {
  281. type route hook output priority mangle; policy accept;
  282. meta nfproto ipv4 meta l4proto tcp counter ip dscp set 0x1 comment "!fw4: Mangle rule #7"
  283. meta nfproto ipv6 meta l4proto tcp counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #7"
  284. meta nfproto ipv4 meta l4proto udp counter ip dscp set 0x1 comment "!fw4: Mangle rule #7"
  285. meta nfproto ipv6 meta l4proto udp counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #7"
  286. meta nfproto ipv4 meta l4proto tcp counter ip dscp set 0x1 comment "!fw4: Mangle rule #8"
  287. meta nfproto ipv6 meta l4proto tcp counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #8"
  288. meta nfproto ipv4 meta l4proto udp counter ip dscp set 0x1 comment "!fw4: Mangle rule #8"
  289. meta nfproto ipv6 meta l4proto udp counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #8"
  290. meta nfproto ipv4 meta l4proto tcp oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #9"
  291. meta nfproto ipv6 meta l4proto tcp oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #9"
  292. meta nfproto ipv4 meta l4proto udp oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #9"
  293. meta nfproto ipv6 meta l4proto udp oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #9"
  294. }
  295. chain mangle_forward {
  296. type filter hook forward priority mangle; policy accept;
  297. meta nfproto ipv4 meta l4proto tcp counter ip dscp set 0x1 comment "!fw4: Mangle rule #1"
  298. meta nfproto ipv6 meta l4proto tcp counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #1"
  299. meta nfproto ipv4 meta l4proto udp counter ip dscp set 0x1 comment "!fw4: Mangle rule #1"
  300. meta nfproto ipv6 meta l4proto udp counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #1"
  301. meta nfproto ipv4 meta l4proto tcp iifname { "eth0", "eth1" } oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #2"
  302. meta nfproto ipv6 meta l4proto tcp iifname { "eth0", "eth1" } oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #2"
  303. meta nfproto ipv4 meta l4proto udp iifname { "eth0", "eth1" } oifname { "eth2", "eth3" } counter ip dscp set 0x1 comment "!fw4: Mangle rule #2"
  304. meta nfproto ipv6 meta l4proto udp iifname { "eth0", "eth1" } oifname { "eth2", "eth3" } counter ip6 dscp set 0x1 comment "!fw4: Mangle rule #2"
  305. }
  306. }
  307. -- End --