Chapter 6. Prioritizing rich rules
Rich rules provide a more advanced and flexible way to define firewall rules. Rich rules are particularly useful where services, ports, and so on are not enough to express complex firewall rules.
Concepts behind rich rules:
- granularity and flexibility
- You can define detailed conditions for network traffic based on more specific criteria.
- rule structure
A rich rule consists of a family (IPv4 or IPv6), followed by conditions and actions.
rule family="ipv4|ipv6" [conditions] [actions]
rule family="ipv4|ipv6" [conditions] [actions]
Copy to Clipboard Copied! - conditions
- They allow rich rules to apply only when certain criteria are met.
- actions
- You can define what happens to network traffic that matches the conditions.
- combining multiple conditions
- You can create more specific and complex filtering.
- hierarchical control and reusability
- You can combine rich rules with other firewall mechanisms such as zones or services.
By default, rich rules are organized based on their rule action. For example, deny
rules have precedence over allow
rules. The priority
parameter in rich rules provides administrators fine-grained control over rich rules and their execution order. When using the priority
parameter, rules are sorted first by their priority values in ascending order. When more rules have the same priority
, their order is determined by the rule action, and if the action is also the same, the order may be undefined.
6.1. How the priority parameter organizes rules into different chains
You can set the priority
parameter in a rich rule to any number between -32768
and 32767
, and lower numerical values have higher precedence.
The firewalld
service organizes rules based on their priority value into different chains:
-
Priority lower than 0: the rule is redirected into a chain with the
_pre
suffix. -
Priority higher than 0: the rule is redirected into a chain with the
_post
suffix. -
Priority equals 0: based on the action, the rule is redirected into a chain with the
_log
,_deny
, or_allow
the action.
Inside these sub-chains, firewalld
sorts the rules based on their priority value.
For more information see, the manual page `firewalld.richlanguage(5) on your system.
6.2. Setting the priority of a rich rule
The following is an example of how to create a rich rule that uses the priority
parameter to log all traffic that is not allowed or denied by other rules. You can use this rule to flag unexpected traffic.
Procedure
Add a rich rule with a very low precedence to log all traffic that has not been matched by other rules:
firewall-cmd --add-rich-rule='rule priority=32767 log prefix="UNEXPECTED: " limit value="5/m"'
# firewall-cmd --add-rich-rule='rule priority=32767 log prefix="UNEXPECTED: " limit value="5/m"'
Copy to Clipboard Copied! The command additionally limits the number of log entries to
5
per minute. For more details, see the `firewalld.richlanguage(5) manual page on your system.
Verification
Display the
nftables
rule that the command in the previous step created:nft list chain inet firewalld filter_IN_public_post
# nft list chain inet firewalld filter_IN_public_post table inet firewalld { chain filter_IN_public_post { log prefix "UNEXPECTED: " limit rate 5/minute } }
Copy to Clipboard Copied!