Chapter 10. Getting started with nftables
If your scenario does not fall under typical packet-filtering cases covered by firewalld
, or you want to have complete control of rules, you can use the nftables
framework.
10.1. What is nftables
The nftables
framework classifies packets, and it is the successor to the iptables
, ip6tables
, arptables
, ebtables
, and ipset
utilities. It offers numerous improvements in convenience, features, and performance over previous packet-filtering tools, most notably:
- Built-in lookup tables instead of linear processing
-
A single framework for both the
IPv4
andIPv6
protocols - Updating the kernel rule set in place through transactions instead of fetching, updating, and storing the entire rule set
-
Support for debugging and tracing in the rule set (
nftrace
) and monitoring trace events (in thenft
tool) - More consistent and compact syntax, no protocol-specific extensions
- A Netlink API for third-party applications
The nftables
framework uses tables to store chains. The chains contain individual rules for performing actions. The nft
utility replaces all tools from the previous packet-filtering frameworks. You can use the libnftables
library for low-level interaction with nftables
Netlink API through the libnftnl
library.
To display the effect of rule set changes, use the nft list ruleset
command. To clear the kernel rule set, use the nft flush ruleset
command. Note that this may also affect the rule set installed by the iptables-nft
command, as it utilizes the same kernel infrastructure.
10.2. When to use firewalld or nftables
On Red Hat Enterprise Linux, you can use the following packet-filtering utilities depending on your scenario:
-
firewalld
: Thefirewalld
utility simplifies firewall configuration for common use cases. -
nftables
: Use thenftables
utility to set up complex and performance-critical firewalls, such as for a whole network.
To prevent the different firewall-related services (firewalld
or nftables
) from influencing each other, run only one of them on a RHEL host, and disable the other service.
10.3. Concepts in the nftables framework
Compared to the iptables
framework, nftables
offers a more modern, efficient, and flexible alternative. The nftables
framework provides advanced capabilities and improvements over iptables
, which simplify rule management and enhance performance. This makes nftables
a modern alternative for complex and high-performance networking environments.
- Tables and namespaces
-
In
nftables
, tables represent organizational units or namespaces that group together related firewall chains, sets, flowtables, and other objects. Innftables
, tables provide a more flexible way to structure firewall rules and related components. While iniptables
, the tables were more rigidly defined with specific purposes. - Table families
-
Each table in
nftables
is associated with a specific family (ip
,ip6
,inet
,arp
,bridge
, ornetdev
). This association determines which packets the table can process. For example, a table in theip
family handles only IPv4 packets. On the other hand,inet
is a special case of table family. It offers a unified approach across protocols, because it can process both IPv4 and IPv6 packets. Another case of a special table family isnetdev
, because it is used for rules that apply directly to network devices, enabling filtering at the device level. - Base chains
Base chains in
nftables
are highly configurable entry-points in the packet processing pipeline that enable users to specify the following:- Type of chain, for example, "filter"
- The hook point in the packet processing path, for example, "input", "output", "forward"
- Priority of the chain
This flexibility enables precise control over when and how the rules are applied to packets as they pass through the network stack. A special case of a chain is the
route
chain, which is used to influence the routing decisions made by the kernel, based on packet headers.- Virtual machine for rule processing
The
nftables
framework uses an internal virtual machine to process rules. This virtual machine executes instructions that are similar to assembly language operations (loading data into registers, performing comparisons, and so on). Such a mechanism allows for highly flexible and efficient rule processing.Enhancements in
nftables
can be introduced as new instructions for that virtual machine. This typically requires a new kernel module and updates to thelibnftnl
library and thenft
command-line utility.Alternatively, you can introduce new features by combining existing instructions in innovative ways without a need for kernel modifications. The syntax of
nftables
rules reflects the flexibility of the underlying virtual machine. For example, the rulemeta mark set tcp dport map { 22: 1, 80: 2 }
sets a packet’s firewall mark to 1 if the TCP destination port is 22, and to 2 if the port is 80. This demonstrates how complex logic can be expressed concisely.- Complex filtering and verdict maps
The
nftables
framework integrates and extends the functionality of theipset
utility, which is used iniptables
for bulk matching on IP addresses, ports, other data types and, most importantly, combinations thereof. This integration makes it easier to manage large and dynamic sets of data directly withinnftables
. Next,nftables
natively supports matching packets based on multiple values or ranges for any data type, which enhances its capability to handle complex filtering requirements. Withnftables
you can manipulate any field within a packet.In
nftables
, sets can be either named or anonymous. The named sets can be referenced by multiple rules and modified dynamically. The anonymous sets are defined inline within a rule and are immutable. Sets can contain elements that are combinations of different types, for example IP address and port number pairs. This feature provides greater flexibility in matching complex criteria. To manage sets, the kernel can select the most appropriate backend based on the specific requirements (performance, memory efficiency, and others). Sets can also function as maps with key-value pairs. The value part can be used as data points (values to write into packet headers), or as verdicts or chains to jump to. This enables complex and dynamic rule behaviors, known as "verdict maps".- Flexible rule format
The structure of
nftables
rules is straightforward. The conditions and actions are applied sequentially from left to right. This intuitive format simplifies rule creating and troubleshooting.Conditions in a rule are logically connected (with the AND operator) together, which means that all conditions must be evaluated as "true" for the rule to match. If any condition fails, the evaluation moves to the next rule.
Actions in
nftables
can be final, such asdrop
oraccept
, which stop further rule processing for the packet. Non-terminal actions, such ascounter log meta mark set 0x3
, perform specific tasks (counting packets, logging, setting a mark, and others), but allow subsequent rules to be evaluated.