This topic is to share some basic understandings I have to be corrected if wrong and to also bring up some ideas with a goal of optimizing RouterOS firewall rules in production environments. So, I am going to focus on a snippet from this page -> https://help.mikrotik.com/docs/spaces/R ... d+Firewall, namely, the section as follows.
/ip firewall raw
add action=accept chain=icmp4 comment="defconf: echo reply" icmp-options=0:0 limit=5,10:packet protocol=icmp
add action=accept chain=icmp4 comment="defconf: net unreachable" icmp-options=3:0 protocol=icmp
add action=accept chain=icmp4 comment="defconf: host unreachable" icmp-options=3:1 protocol=icmp
add action=accept chain=icmp4 comment="defconf: protocol unreachable" icmp-options=3:2 protocol=icmp
add action=accept chain=icmp4 comment="defconf: port unreachable" icmp-options=3:3 protocol=icmp
add action=accept chain=icmp4 comment="defconf: fragmentation needed" icmp-options=3:4 protocol=icmp
add action=accept chain=icmp4 comment="defconf: echo" icmp-options=8:0 limit=5,10:packet protocol=icmp
add action=accept chain=icmp4 comment="defconf: time exceeded " icmp-options=11:0-255 protocol=icmp
add action=drop chain=icmp4 comment="defconf: drop other icmp" protocol=icmp
So, here goes, when this chain icmp4 is jumped to it starts at the topmost line testing packets and if it matches a rule is accepted and doesn't proceed to the next line leaving the chain but essentially leaves the chain going back to where it was jumped from. In this scenario that would mean once a packet matches a rule it would then go on to the line "add action=jump chain=prerouting comment="defconf: jump to TCP chain" jump-target=bad_tcp protocol=tcp" directly under the jump line of "add action=jump chain=prerouting comment="defconf: jump to ICMP chain" jump-target=icmp4 protocol=icmp". Correct? Or, if it doesn't match any particular action=accept rule it advances on to the next set of rules potentially not matching any of the action=accept rules and getting to the action=drop rule. Perhaps an add to a list which a function operates off sending abuse reports would be in order behind the scenes but I'll focus on these rules.
So, as I understand it, if a packet is going through these rules and it is an icmp packet type 8 code 0 it is going to be tested first by the prior 6 rules in the icmp4 chain, unless, logic behind the scenes to optimize rule processing is taking place. While I am not sure how much it would help in things like decreasing both latency and cpu processing with this set of rules, while they are nicely ordered with the lower icmp types first, and next, the lower icmp codes first, as follows (0,0; 3,0; 3,1; 3,2; 3,3; 3,4; 8,0; 11,0-255) which is helpful in organizing rules, I am assuming it likely with most production environments there would be more matching icmp echo and reply rules than the other rules so pushing them up to the top of the chain assuming the logic itself doesn't change in an optimized set of rules would be beneficial, correct?
So, this brings me to another question kind of related to how in compiling code human language is analyzed looking for patterns that would make the code faster without changing the logic before creating an executable. In practice is there already something going on in how RouterOS handles these rules where it detects things it can optimize without changing the logic or would it be potentially beneficial to work towards putting the most likeliest matches first, not just in this scenario with an icmp chain named icmp4, but other chains and also just in overall goals of writing firewall rules and even adapting when in production perhaps even when handling DDoS scenarios as things change, so that when packets traverse through testing of less likely to be matched rules are not done? I imagine a running firewall could do things like as traffic patterns change after an epoch of say 5 minutes has expired rearrange the order or processing rules where for that set of rules it doesn't affect the logic of traffic flow such that the most commonly matched rules are processed first.