2
0

08_log_limit 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. Test that configured zone log limits are honored in emitted log 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/firewall.json --
  15. {
  16. "zone": [
  17. {
  18. ".description": "test zone with log_limit",
  19. "name": "lan",
  20. "network": "lan",
  21. "auto_helper": 0,
  22. "log": 3,
  23. "log_limit": "1/min"
  24. },
  25. {
  26. ".description": "test zone with MASQ and log_limit",
  27. "name": "wan",
  28. "network": "wan",
  29. "auto_helper": 0,
  30. "family": "ipv4",
  31. "masq": 1,
  32. "log": 3,
  33. "log_limit": "2/min"
  34. },
  35. {
  36. ".description": "test zone with log_limit and no log",
  37. "name": "guest",
  38. "network": "guest",
  39. "auto_helper": 0,
  40. "log_limit": "3/min"
  41. },
  42. {
  43. ".description": "test zone with log and no limit, should produce multi target rules",
  44. "name": "wan6",
  45. "network": "wan6",
  46. "auto_helper": 0,
  47. "family": "ipv6",
  48. "log": 1
  49. }
  50. ],
  51. "forwarding": [
  52. {
  53. "src": "lan",
  54. "dest": "wan"
  55. }
  56. ],
  57. "rule": [
  58. {
  59. ".description": "src lan log",
  60. "proto": "tcp",
  61. "src": "lan",
  62. "dest_port": 1001,
  63. "log": 1
  64. },
  65. {
  66. ".description": "src lan no log",
  67. "proto": "tcp",
  68. "src": "lan",
  69. "dest_port": 1002,
  70. "log": 0
  71. },
  72. {
  73. ".description": "dest lan log",
  74. "proto": "tcp",
  75. "dest": "lan",
  76. "dest_port": 1003,
  77. "log": 1
  78. },
  79. {
  80. ".description": "dest lan no log",
  81. "proto": "tcp",
  82. "dest": "lan",
  83. "dest_port": 1004,
  84. "log": 0
  85. },
  86. {
  87. ".description": "Source any, dest lan, log",
  88. "proto": "tcp",
  89. "src": "*",
  90. "dest": "lan",
  91. "dest_port": 1005,
  92. "log": 1
  93. },
  94. {
  95. ".description": "Source any, dest lan, no log",
  96. "proto": "tcp",
  97. "src": "*",
  98. "dest": "lan",
  99. "dest_port": 1006,
  100. "log": 0
  101. },
  102. {
  103. ".description": "src any log",
  104. "proto": "tcp",
  105. "src": "*",
  106. "dest_port": 1007,
  107. "log": 1
  108. },
  109. {
  110. ".description": "src any no log",
  111. "proto": "tcp",
  112. "src": "*",
  113. "dest_port": 1008,
  114. "log": 0
  115. },
  116. {
  117. "name": "Deny guest with no log",
  118. "proto": "icmp",
  119. "dest": "guest",
  120. "target": "drop"
  121. },
  122. {
  123. "name": "Deny guest with log",
  124. "proto": "icmp",
  125. "dest": "guest",
  126. "target": "drop",
  127. "log": 1
  128. },
  129. {
  130. "name": "Deny rule #1",
  131. "proto": "any",
  132. "src": "lan",
  133. "dest": "wan",
  134. "src_ip": [ "192.168.1.2" ],
  135. "target": "drop"
  136. },
  137. {
  138. "name": "Deny rule #2",
  139. "proto": "icmp",
  140. "src": "lan",
  141. "dest": "wan",
  142. "src_ip": [ "192.168.1.3" ],
  143. "target": "drop"
  144. },
  145. {
  146. ".description": "src any log",
  147. "proto": "tcp",
  148. "src": "*",
  149. "dest_port": 1009,
  150. "log": 1,
  151. "log_limit": "5/min"
  152. }
  153. ],
  154. "redirect": [
  155. {
  156. "proto": "tcp",
  157. "src": "wan",
  158. "dest": "lan",
  159. "dest_ip": "10.0.0.2",
  160. "dest_port": "22",
  161. "log": "1"
  162. },
  163. {
  164. "proto": "tcp",
  165. "src": "wan",
  166. "dest": "lan",
  167. "dest_ip": "10.0.0.2",
  168. "dest_port": "23",
  169. "log": "1",
  170. "log_limit": "4/min"
  171. }
  172. ]
  173. }
  174. -- End --
  175. -- File uci/helpers.json --
  176. {}
  177. -- End --
  178. -- Expect stdout --
  179. table inet fw4
  180. flush table inet fw4
  181. table inet fw4 {
  182. #
  183. # Defines
  184. #
  185. define lan_devices = { "br-lan" }
  186. define lan_subnets = { 10.0.0.0/24, 192.168.26.0/24, 2001:db8:1000::/60, fd63:e2f:f706::/60 }
  187. define wan_devices = { "pppoe-wan" }
  188. define wan_subnets = { 10.11.12.0/24 }
  189. define guest_devices = { "br-guest" }
  190. define guest_subnets = { 10.1.0.0/24, 192.168.27.0/24, 2001:db8:1000::/60, fd63:e2f:f706::/60 }
  191. define wan6_devices = { "pppoe-wan" }
  192. define wan6_subnets = { 2001:db8:54:321::/64 }
  193. #
  194. # Limits
  195. #
  196. limit lan.log_limit {
  197. comment "lan log limit"
  198. rate 1/minute
  199. }
  200. limit wan.log_limit {
  201. comment "wan log limit"
  202. rate 2/minute
  203. }
  204. limit guest.log_limit {
  205. comment "guest log limit"
  206. rate 3/minute
  207. }
  208. #
  209. # User includes
  210. #
  211. include "/etc/nftables.d/*.nft"
  212. #
  213. # Filter rules
  214. #
  215. chain input {
  216. type filter hook input priority filter; policy drop;
  217. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  218. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  219. tcp dport 1007 counter log prefix "@rule[6]: " comment "!fw4: @rule[6]"
  220. tcp dport 1008 counter comment "!fw4: @rule[7]"
  221. tcp dport 1009 limit rate 5/minute log prefix "@rule[12]: "
  222. tcp dport 1009 counter comment "!fw4: @rule[12]"
  223. iifname "br-lan" jump input_lan comment "!fw4: Handle lan IPv4/IPv6 input traffic"
  224. meta nfproto ipv4 iifname "pppoe-wan" jump input_wan comment "!fw4: Handle wan IPv4 input traffic"
  225. iifname "br-guest" jump input_guest comment "!fw4: Handle guest IPv4/IPv6 input traffic"
  226. meta nfproto ipv6 iifname "pppoe-wan" jump input_wan6 comment "!fw4: Handle wan6 IPv6 input traffic"
  227. }
  228. chain forward {
  229. type filter hook forward priority filter; policy drop;
  230. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  231. tcp dport 1005 limit name "lan.log_limit" log prefix "@rule[4]: "
  232. tcp dport 1005 counter comment "!fw4: @rule[4]"
  233. tcp dport 1006 counter comment "!fw4: @rule[5]"
  234. iifname "br-lan" jump forward_lan comment "!fw4: Handle lan IPv4/IPv6 forward traffic"
  235. meta nfproto ipv4 iifname "pppoe-wan" jump forward_wan comment "!fw4: Handle wan IPv4 forward traffic"
  236. iifname "br-guest" jump forward_guest comment "!fw4: Handle guest IPv4/IPv6 forward traffic"
  237. meta nfproto ipv6 iifname "pppoe-wan" jump forward_wan6 comment "!fw4: Handle wan6 IPv6 forward traffic"
  238. }
  239. chain output {
  240. type filter hook output priority filter; policy drop;
  241. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  242. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  243. oifname "br-lan" jump output_lan comment "!fw4: Handle lan IPv4/IPv6 output traffic"
  244. meta nfproto ipv4 oifname "pppoe-wan" jump output_wan comment "!fw4: Handle wan IPv4 output traffic"
  245. oifname "br-guest" jump output_guest comment "!fw4: Handle guest IPv4/IPv6 output traffic"
  246. meta nfproto ipv6 oifname "pppoe-wan" jump output_wan6 comment "!fw4: Handle wan6 IPv6 output traffic"
  247. }
  248. chain prerouting {
  249. type filter hook prerouting priority filter; policy accept;
  250. }
  251. chain handle_reject {
  252. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  253. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  254. }
  255. chain input_lan {
  256. tcp dport 1001 limit name "lan.log_limit" log prefix "@rule[0]: "
  257. tcp dport 1001 counter comment "!fw4: @rule[0]"
  258. tcp dport 1002 counter comment "!fw4: @rule[1]"
  259. ct status dnat accept comment "!fw4: Accept port redirections"
  260. jump drop_from_lan
  261. }
  262. chain output_lan {
  263. tcp dport 1003 limit name "lan.log_limit" log prefix "@rule[2]: "
  264. tcp dport 1003 counter comment "!fw4: @rule[2]"
  265. tcp dport 1004 counter comment "!fw4: @rule[3]"
  266. jump drop_to_lan
  267. }
  268. chain forward_lan {
  269. ip saddr 192.168.1.2 counter jump drop_to_wan comment "!fw4: Deny rule #1"
  270. meta l4proto icmp ip saddr 192.168.1.3 counter jump drop_to_wan comment "!fw4: Deny rule #2"
  271. meta nfproto ipv4 jump accept_to_wan comment "!fw4: Accept lan to wan IPv4 forwarding"
  272. ct status dnat accept comment "!fw4: Accept port forwards"
  273. jump drop_to_lan
  274. limit name "lan.log_limit" log prefix "drop lan forward: "
  275. }
  276. chain accept_to_lan {
  277. oifname "br-lan" counter accept comment "!fw4: accept lan IPv4/IPv6 traffic"
  278. }
  279. chain drop_from_lan {
  280. iifname "br-lan" limit name "lan.log_limit" log prefix "drop lan in: "
  281. iifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  282. }
  283. chain drop_to_lan {
  284. oifname "br-lan" limit name "lan.log_limit" log prefix "drop lan out: "
  285. oifname "br-lan" counter drop comment "!fw4: drop lan IPv4/IPv6 traffic"
  286. }
  287. chain input_wan {
  288. ct status dnat accept comment "!fw4: Accept port redirections"
  289. jump drop_from_wan
  290. }
  291. chain output_wan {
  292. jump drop_to_wan
  293. }
  294. chain forward_wan {
  295. ct status dnat accept comment "!fw4: Accept port forwards"
  296. jump drop_to_wan
  297. limit name "wan.log_limit" log prefix "drop wan forward: "
  298. }
  299. chain accept_to_wan {
  300. meta nfproto ipv4 oifname "pppoe-wan" ct state invalid limit name "wan.log_limit" log prefix "drop wan invalid ct state: "
  301. meta nfproto ipv4 oifname "pppoe-wan" ct state invalid counter drop comment "!fw4: Prevent NAT leakage"
  302. meta nfproto ipv4 oifname "pppoe-wan" counter accept comment "!fw4: accept wan IPv4 traffic"
  303. }
  304. chain drop_from_wan {
  305. meta nfproto ipv4 iifname "pppoe-wan" limit name "wan.log_limit" log prefix "drop wan in: "
  306. meta nfproto ipv4 iifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4 traffic"
  307. }
  308. chain drop_to_wan {
  309. meta nfproto ipv4 oifname "pppoe-wan" limit name "wan.log_limit" log prefix "drop wan out: "
  310. meta nfproto ipv4 oifname "pppoe-wan" counter drop comment "!fw4: drop wan IPv4 traffic"
  311. }
  312. chain input_guest {
  313. jump drop_from_guest
  314. }
  315. chain output_guest {
  316. meta l4proto { "icmp", "ipv6-icmp" } counter jump drop_to_guest comment "!fw4: Deny guest with no log"
  317. meta l4proto { "icmp", "ipv6-icmp" } limit name "guest.log_limit" log prefix "Deny guest with log: "
  318. meta l4proto { "icmp", "ipv6-icmp" } counter jump drop_to_guest comment "!fw4: Deny guest with log"
  319. jump drop_to_guest
  320. }
  321. chain forward_guest {
  322. jump drop_to_guest
  323. }
  324. chain drop_from_guest {
  325. iifname "br-guest" counter drop comment "!fw4: drop guest IPv4/IPv6 traffic"
  326. }
  327. chain drop_to_guest {
  328. oifname "br-guest" counter drop comment "!fw4: drop guest IPv4/IPv6 traffic"
  329. }
  330. chain input_wan6 {
  331. jump drop_from_wan6
  332. }
  333. chain output_wan6 {
  334. jump drop_to_wan6
  335. }
  336. chain forward_wan6 {
  337. jump drop_to_wan6
  338. log prefix "drop wan6 forward: "
  339. }
  340. chain drop_from_wan6 {
  341. meta nfproto ipv6 iifname "pppoe-wan" counter log prefix "drop wan6 in: " drop comment "!fw4: drop wan6 IPv6 traffic"
  342. }
  343. chain drop_to_wan6 {
  344. meta nfproto ipv6 oifname "pppoe-wan" counter log prefix "drop wan6 out: " drop comment "!fw4: drop wan6 IPv6 traffic"
  345. }
  346. #
  347. # NAT rules
  348. #
  349. chain dstnat {
  350. type nat hook prerouting priority dstnat; policy accept;
  351. iifname "br-lan" jump dstnat_lan comment "!fw4: Handle lan IPv4/IPv6 dstnat traffic"
  352. meta nfproto ipv4 iifname "pppoe-wan" jump dstnat_wan comment "!fw4: Handle wan IPv4 dstnat traffic"
  353. }
  354. chain srcnat {
  355. type nat hook postrouting priority srcnat; policy accept;
  356. oifname "br-lan" jump srcnat_lan comment "!fw4: Handle lan IPv4/IPv6 srcnat traffic"
  357. meta nfproto ipv4 oifname "pppoe-wan" jump srcnat_wan comment "!fw4: Handle wan IPv4 srcnat traffic"
  358. }
  359. chain dstnat_lan {
  360. ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 10.11.12.194 dnat 10.0.0.2:22 comment "!fw4: @redirect[0] (reflection)"
  361. ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 10.11.12.194 dnat 10.0.0.2:23 comment "!fw4: @redirect[1] (reflection)"
  362. }
  363. chain srcnat_lan {
  364. ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 10.0.0.2 tcp dport 22 snat 10.0.0.1 comment "!fw4: @redirect[0] (reflection)"
  365. ip saddr { 10.0.0.0/24, 192.168.26.0/24 } ip daddr 10.0.0.2 tcp dport 23 snat 10.0.0.1 comment "!fw4: @redirect[1] (reflection)"
  366. }
  367. chain dstnat_wan {
  368. meta nfproto ipv4 limit name "wan.log_limit" log prefix "@redirect[0]: "
  369. meta nfproto ipv4 counter dnat 10.0.0.2:22 comment "!fw4: @redirect[0]"
  370. meta nfproto ipv4 limit rate 4/minute log prefix "@redirect[1]: "
  371. meta nfproto ipv4 counter dnat 10.0.0.2:23 comment "!fw4: @redirect[1]"
  372. }
  373. chain srcnat_wan {
  374. meta nfproto ipv4 masquerade comment "!fw4: Masquerade IPv4 wan traffic"
  375. }
  376. #
  377. # Raw rules (notrack)
  378. #
  379. chain raw_prerouting {
  380. type filter hook prerouting priority raw; policy accept;
  381. }
  382. chain raw_output {
  383. type filter hook output priority raw; policy accept;
  384. }
  385. #
  386. # Mangle rules
  387. #
  388. chain mangle_prerouting {
  389. type filter hook prerouting priority mangle; policy accept;
  390. }
  391. chain mangle_postrouting {
  392. type filter hook postrouting priority mangle; policy accept;
  393. }
  394. chain mangle_input {
  395. type filter hook input priority mangle; policy accept;
  396. }
  397. chain mangle_output {
  398. type route hook output priority mangle; policy accept;
  399. }
  400. chain mangle_forward {
  401. type filter hook forward priority mangle; policy accept;
  402. }
  403. }
  404. -- End --