Manual: nftables
NFT(8) NFT(8)
NAME
nft - Administration tool of the nftables framework for packet filtering and classification
SYNOPSIS
nft [ -nNscaeSupyjtT ] [ -I directory ] [ -f filename | -i | cmd ...]
nft -h
nft -v
DESCRIPTION
nft is the command line tool used to set up, maintain and inspect packet filtering and classification rules in the
Linux kernel, in the nftables framework. The Linux kernel subsystem is known as nf_tables, and βnfβ stands for
Netfilter.
OPTIONS
The command accepts several different options which are documented here in groups for better understanding of their
meaning. You can get information about options by running nft --help.
General options:
-h, --help
Show help message and all options.
-v, --version
Show version.
-V
Show long version information, including compile-time configuration.
Ruleset input handling options that specify to how to load rulesets:
-f, --file filename
Read input from filename. If filename is -, read from stdin. The directory path to this file is inserted at the
beginning the list of directories to be searched for included files (see -I/--includepath).
-D, --define name=value
Define a variable. You can only combine this option with -f.
-i, --interactive
Read input from an interactive readline CLI. You can use quit to exit, or use the EOF marker, normally this is
CTRL-D.
-I, --includepath directory
Add the directory directory to the list of directories to be searched for included files. This option may be
specified multiple times.
-c, --check
Check commands validity without actually applying the changes.
-o, --optimize
Optimize your ruleset. You can combine this option with -c to inspect the proposed optimizations.
Ruleset list output formatting that modify the output of the list ruleset command:
-a, --handle
Show object handles in output.
-s, --stateless
Omit stateful information of rules and stateful objects.
-t, --terse
Omit contents of sets from output.
-S, --service
Translate ports to service names as defined by /etc/services.
-N, --reversedns
Translate IP address to names via reverse DNS lookup. This may slow down your listing since it generates network
traffic.
-u, --guid
Translate numeric UID/GID to names as defined by /etc/passwd and /etc/group.
-n, --numeric
Print fully numerical output.
-y, --numeric-priority
Display base chain priority numerically.
-p, --numeric-protocol
Display layer 4 protocol numerically.
-T, --numeric-time
Show time, day and hour values in numeric format.
Command output formatting:
-e, --echo
When inserting items into the ruleset using add, insert or replace commands, print notifications just like nft
monitor.
-j, --json
Format output in JSON. See libnftables-json(5) for a schema description.
-d, --debug level
Enable debugging output. The debug level can be any of scanner, parser, eval, netlink, mnl, proto-ctx, segtree,
all. You can combine more than one by separating by the , symbol, for example -d eval,mnl.
INPUT FILE FORMATS
LEXICAL CONVENTIONS
Input is parsed line-wise. When the last character of a line, just before the newline character, is a non-quoted
backslash (\), the next line is treated as a continuation. Multiple commands on the same line can be separated using
a semicolon (;).
A hash sign (#) begins a comment. All following characters on the same line are ignored.
Identifiers begin with an alphabetic character (a-z,A-Z), followed by zero or more alphanumeric characters
(a-z,A-Z,0-9) and the characters slash (/), backslash (\), underscore (_) and dot (.). Identifiers using different
characters or clashing with a keyword need to be enclosed in double quotes (").
INCLUDE FILES
include filename
Other files can be included by using the include statement. The directories to be searched for include files can be
specified using the -I/--includepath option. You can override this behaviour either by prepending β./β to your path
to force inclusion of files located in the current working directory (i.e. relative path) or / for file location
expressed as an absolute path.
If -I/--includepath is not specified, then nft relies on the default directory that is specified at compile time. You
can retrieve this default directory via the -h/--help option.
Include statements support the usual shell wildcard symbols (,?,[]). Having no matches for an include statement is
not an error, if wildcard symbols are used in the include statement. This allows having potentially empty include
directories for statements like include "/etc/firewall/rules/". The wildcard matches are loaded in the collation
order of the C locale. Files beginning with dot (.) are not matched by include statements.
SYMBOLIC VARIABLES
define variable = expr
undefine variable
redefine variable = expr
$variable
Symbolic variables can be defined using the define statement. Variable references are expressions and can be used to
initialize other variables. The scope of a definition is the current block and all blocks contained within. Symbolic
variables can be undefined using the undefine statement, and modified using the redefine statement.
Using symbolic variables.
define int_if1 = eth0
define int_if2 = eth1
define int_ifs = { $int_if1, $int_if2 }
redefine int_if2 = wlan0
undefine int_if2
filter input iif $int_ifs accept
ADDRESS FAMILIES
Address families determine the type of packets which are processed. For each address family, the kernel contains so
called hooks at specific stages of the packet processing paths, which invoke nftables if rules for these hooks exist.
ip IPv4 address family.
ip6 IPv6 address family.
inet Internet (IPv4/IPv6) address family.
arp ARP address family, handling IPv4 ARP
packets.
bridge Bridge address family, handling packets
which traverse a bridge device.
netdev Netdev address family, handling packets
on ingress and egress.
All nftables objects exist in address family specific namespaces, therefore all identifiers include an address
family. If an identifier is specified without an address family, the ip family is used by default.
IPV4/IPV6/INET ADDRESS FAMILIES
The IPv4/IPv6/Inet address families handle IPv4, IPv6 or both types of packets. They contain five hooks at different
packet processing stages in the network stack.
Table 1. IPv4/IPv6/Inet address family hooks
βββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β Hook β Description β
βββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β prerouting β All packets entering the system are β
β β processed by the prerouting hook. It is β
β β invoked before the routing process and is β
β β used for early filtering or changing β
β β packet attributes that affect routing. β
βββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β input β Packets delivered to the local system are β
β β processed by the input hook. β
βββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β forward β Packets forwarded to a different host are β
β β processed by the forward hook. β
βββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β output β Packets sent by local processes are β
β β processed by the output hook. β
βββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β postrouting β All packets leaving the system are β
β β processed by the postrouting hook. β
βββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β ingress β All packets entering the system are β
β β processed by this hook. It is invoked β
β β before layer 3 protocol handlers, hence β
β β before the prerouting hook, and it can be β
β β used for filtering and policing. Ingress β
β β is only available for Inet family (since β
β β Linux kernel 5.10). β
βββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββ
ARP ADDRESS FAMILY
The ARP address family handles ARP packets received and sent by the system. It is commonly used to mangle ARP packets
for clustering.
Table 2. ARP address family hooks
ββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β Hook β Description β
ββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β input β Packets delivered to the local system are β
β β processed by the input hook. β
ββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β output β Packets send by the local system are β
β β processed by the output hook. β
ββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββ
BRIDGE ADDRESS FAMILY
The bridge address family handles Ethernet packets traversing bridge devices.
The list of supported hooks is identical to IPv4/IPv6/Inet address families above.
NETDEV ADDRESS FAMILY
The Netdev address family handles packets from the device ingress and egress path. This family allows you to filter
packets of any ethertype such as ARP, VLAN 802.1q, VLAN 802.1ad (Q-in-Q) as well as IPv4 and IPv6 packets.
Table 3. Netdev address family hooks
βββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β Hook β Description β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β ingress β All packets entering the system are β
β β processed by this hook. It is invoked β
β β after the network taps (ie. tcpdump), β
β β right after tc ingress and before layer 3 β
β β protocol handlers, it can be used for β
β β early filtering and policing. β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β egress β All packets leaving the system are β
β β processed by this hook. It is invoked β
β β after layer 3 protocol handlers and β
β β before tc egress. It can be used for late β
β β filtering and policing. β
βββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββ
Tunneled packets (such as vxlan) are processed by netdev family hooks both in decapsulated and encapsulated
(tunneled) form. So a packet can be filtered on the overlay network as well as on the underlying network.
Note that the order of netfilter and tc is mirrored on ingress versus egress. This ensures symmetry for NAT and other
packet mangling.
Ingress packets which are redirected out some other interface are only processed by netfilter on egress if they have
passed through netfilter ingress processing before. Thus, ingress packets which are redirected by tc are not
subjected to netfilter. But they are if they are redirected by netfilter on ingress. Conceptually, tc and netfilter
can be thought of as layers, with netfilter layered above tc: If the packet hasnβt been passed up from the tc layer
to the netfilter layer, itβs not subjected to netfilter on egress.
RULESET
{list | flush} ruleset [family]
The ruleset keyword is used to identify the whole set of tables, chains, etc. currently in place in kernel. The
following ruleset commands exist:
list Print the ruleset in human-readable
format.
flush Clear the whole ruleset. Note that,
unlike iptables, this will remove all
tables and whatever they contain,
effectively leading to an empty ruleset -
no packet filtering will happen anymore,
so the kernel accepts any valid packet it
receives.
It is possible to limit list and flush to a specific address family only. For a list of valid family names, see the
section called βADDRESS FAMILIESβ above.
By design, list ruleset command output may be used as input to nft -f. Effectively, this is the nft-equivalent of
iptables-save and iptables-restore.
TABLES
{add | create} table [family] table [{ [comment comment ;] [flags flags ;] }]
{delete | destroy | list | flush} table [family] table
list tables [family]
delete table [family] handle handle
destroy table [family] handle handle
Tables are containers for chains, sets and stateful objects. They are identified by their address family and their
name. The address family must be one of ip, ip6, inet, arp, bridge, netdev. The inet address family is a dummy family
which is used to create hybrid IPv4/IPv6 tables. The meta expression nfproto keyword can be used to test which family
(ipv4 or ipv6) context the packet is being processed in. When no address family is specified, ip is used by default.
The only difference between add and create is that the former will not return an error if the specified table already
exists while create will return an error.
Table 4. Table flags
βββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ
β Flag β Description β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β dormant β table is not evaluated any more (base β
β β chains are unregistered). β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β owner β table is owned by the creating process. β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β persist β table shall outlive the owning process. β
βββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ
Creating a table with flag owner excludes other processes from manipulating it or its contents. By default, it will
be removed when the process exits. Setting flag persist will prevent this and the resulting orphaned table will
accept a new owner, e.g. a restarting daemon maintaining the table.
Add, change, delete a table.
# start nft in interactive mode
nft --interactive
# create a new table.
create table inet mytable
# add a new base chain: get input packets
add chain inet mytable myin { type filter hook input priority filter; }
# add a single counter to the chain
add rule inet mytable myin counter
# disable the table temporarily -- rules are not evaluated anymore
add table inet mytable { flags dormant; }
# make table active again:
add table inet mytable
add Add a new table for the given family with
the given name.
delete Delete the specified table.
destroy Delete the specified table, it does not
fail if it does not exist.
list List all chains and rules of the
specified table.
flush Flush all chains and rules of the
specified table.
CHAINS
{add | create} chain [family] table chain [{ type type hook hook [DEVICE] priority priority ; [policy policy ;] [comment comment ;] }]
{delete | destroy | list | flush} chain [family] table chain
list chains [family]
delete chain [family] table handle handle
destroy chain [family] table handle handle
rename chain [family] table chain newname
DEVICE := {device DEVICE_NAME | devices = { DEVICE_LIST }}
DEVICE_LIST := DEVICE_NAME [, DEVICE_LIST]
DEVICE_NAME := string | string*
Chains are containers for rules. They exist in two kinds, base chains and regular chains. A base chain is an entry
point for packets from the networking stack, a regular chain may be used as jump target and is used for better rule
organization. Regular chains can be anonymous, see VERDICT STATEMENT examples for details.
add Add a new chain in the specified table.
When a hook and priority value are
specified, the chain is created as a base
chain and hooked up to the networking
stack.
create Similar to the add command, but returns
an error if the chain already exists.
delete Delete the specified chain. The chain
must not contain any rules or be used as
jump target.
destroy Delete the specified chain, it does not
fail if it does not exist. The chain must
not contain any rules or be used as jump
target.
rename Rename the specified chain.
list List all rules of the specified chain.
flush Flush all rules of the specified chain.
For base chains, type, hook and priority parameters are mandatory.
Table 5. Supported chain types
ββββββββββ¬ββββββββββββββββ¬ββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β Type β Families β Hooks β Description β
ββββββββββΌββββββββββββββββΌββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β filter β all β all β Standard chain type to β
β β β β use in doubt. β
ββββββββββΌββββββββββββββββΌββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β nat β ip, ip6, inet β prerouting, input, β Chains of this type β
β β β output, postrouting β perform Native Address β
β β β β Translation based on β
β β β β conntrack entries. Only β
β β β β the first packet of a β
β β β β connection actually β
β β β β traverses this chain - β
β β β β its rules usually define β
β β β β details of the created β
β β β β conntrack entry (NAT β
β β β β statements for instance). β
ββββββββββΌββββββββββββββββΌββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β route β ip, ip6, inet β output β If a packet has traversed β
β β β β a chain of this type and β
β β β β is about to be accepted, β
β β β β a new route lookup is β
β β β β performed if relevant β
β β β β parts of the IP header β
β β β β have changed. This allows β
β β β β one to e.g. implement β
β β β β policy routing selectors β
β β β β in nftables. β
ββββββββββ΄ββββββββββββββββ΄ββββββββββββββββββββββ΄ββββββββββββββββββββββββββββ
Apart from the special cases illustrated above (e.g. nat type not supporting forward hook or route type only
supporting output hook), there are three further quirks worth noticing:
β’ The netdev family supports merely two combinations, namely filter type with ingress hook and filter type with
egress hook. Base chains in this family also require the DEVICE parameter to be present since they exist per
interface only.
β’ The arp family supports only the input and output hooks, both in chains of type filter.
β’ The inet family also supports the ingress hook (since Linux kernel 5.10), to filter IPv4 and IPv6 packet at the
same location as the netdev ingress hook. This inet hook allows you to share sets and maps between the usual
prerouting, input, forward, output, postrouting and this ingress hook.
The device parameter accepts a network interface name as a string, and is required when adding a base chain that
filters traffic on the ingress or egress hooks. Any ingress or egress chains will only filter traffic from the
interface specified in the device parameter. The same base chain may be used for multiple devices by using the
devices parameter instead.
With newer kernels there is also basic support for wildcards in DEVICE_NAME by specifying an asterisk suffix. The
chain will apply to all interfaces matching the given prefix. Use the list hooks command to see the current status.
The priority parameter accepts a signed integer value or a standard priority name which specifies the order in which
chains with the same hook value are traversed (regardless of the table to which they belong). The ordering is
ascending: for each hook, chains with lower priority values are evaluated before those with higher ones. The ordering
of chains with identical priority is undefined.
With nat type chains, thereβs a lower excluding limit of -200 for priority values, because conntrack hooks at this
priority and NAT requires it.
Standard priority values can be replaced with easily memorizable names. Not all names make sense in every family with
every hook (see the compatibility matrices below) but their numerical value can still be used for prioritizing
chains.
These names and values are defined and made available based on what priorities are used by xtables when registering
their default chains.
Most of the families use the same values, but bridge uses different ones from the others. See the following tables
that describe the values and compatibility.
Table 6. Standard priority names, family and hook compatibility matrix
ββββββββββββ¬ββββββββ¬ββββββββββββββββββββββ¬ββββββββββββββ
β Name β Value β Families β Hooks β
ββββββββββββΌββββββββΌββββββββββββββββββββββΌββββββββββββββ€
β β β β β
β raw β -300 β ip, ip6, inet β all β
ββββββββββββΌββββββββΌββββββββββββββββββββββΌββββββββββββββ€
β β β β β
β mangle β -150 β ip, ip6, inet β all β
ββββββββββββΌββββββββΌββββββββββββββββββββββΌββββββββββββββ€
β β β β β
β dstnat β -100 β ip, ip6, inet β prerouting β
ββββββββββββΌββββββββΌββββββββββββββββββββββΌββββββββββββββ€
β β β β β
β filter β 0 β ip, ip6, inet, arp, β all β
β β β netdev β β
ββββββββββββΌββββββββΌββββββββββββββββββββββΌββββββββββββββ€
β β β β β
β security β 50 β ip, ip6, inet β all β
ββββββββββββΌββββββββΌββββββββββββββββββββββΌββββββββββββββ€
β β β β β
β srcnat β 100 β ip, ip6, inet β postrouting β
ββββββββββββ΄ββββββββ΄ββββββββββββββββββββββ΄ββββββββββββββ
Table 7. Standard priority names and hook compatibility for the bridge family
ββββββββββ¬ββββββββ¬ββββββββββββββ
β β β β
β Name β Value β Hooks β
ββββββββββΌββββββββΌββββββββββββββ€
β β β β
β dstnat β -300 β prerouting β
ββββββββββΌββββββββΌββββββββββββββ€
β β β β
β filter β -200 β all β
ββββββββββΌββββββββΌββββββββββββββ€
β β β β
β out β 100 β output β
ββββββββββΌββββββββΌββββββββββββββ€
β β β β
β srcnat β 300 β postrouting β
ββββββββββ΄ββββββββ΄ββββββββββββββ
Basic arithmetic expressions (addition and subtraction) can also be achieved with these standard names to ease
relative prioritizing, e.g. mangle - 5 stands for -155. Values will also be printed like this until the value is not
further than 10 from the standard value.
Base chains also allow one to set the chainβs policy, i.e. what happens to packets not explicitly accepted or refused
in contained rules. Supported policy values are accept (which is the default) or drop.
RULES
{add | insert} rule [family] table chain [handle handle | index index] statement ... [comment comment]
replace rule [family] table chain handle handle statement ... [comment comment]
{delete | reset} rule [family] table chain handle handle
destroy rule [family] table chain handle handle
reset rules [family] [table [chain]]
Rules are added to chains in the given table. If the family is not specified, the ip family is used. Rules are
constructed from two kinds of components according to a set of grammatical rules: expressions and statements.
The add and insert commands support an optional location specifier, which is either a handle or the index (starting
at zero) of an existing rule. Internally, rule locations are always identified by handle and the translation from
index happens in userspace. This has two potential implications in case a concurrent ruleset change happens after the
translation was done: The effective rule index might change if a rule was inserted or deleted before the referred
one. If the referred rule was deleted, the command is rejected by the kernel just as if an invalid handle was given.
A comment is a single word or a double-quoted (") multi-word string which can be used to make notes regarding the
actual rule. Note: If you use bash for adding rules, you have to escape the quotation marks, e.g. \"enable ssh for
servers\".
add Add a new rule described by the list of
statements. The rule is appended to the
given chain unless a location is
specified, in which case the rule is
inserted after the specified rule.
insert Same as add except the rule is inserted
at the beginning of the chain or before
the specified rule.
replace Similar to add, but the rule replaces the
specified rule.
delete Delete the specified rule.
destroy Delete the specified rule, it does not
fail if it does not exist.
reset Reset rule-contained state, e.g. counter
and quota statement values.
add a rule to ip table output chain.
nft add rule filter output ip daddr 192.168.0.0/24 accept # 'ip filter' is assumed
# same command, slightly more verbose
nft add rule ip filter output ip daddr 192.168.0.0/24 accept
delete rule from inet table.
# nft -a list ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy accept;
ct state established,related accept # handle 4
ip saddr 10.1.1.1 tcp dport ssh accept # handle 5
...
# delete the rule with handle 5
nft delete rule inet filter input handle 5
OVERALL EVALUATION OF THE RULESET
This is a summary of how the ruleset is evaluated.
β’ Even if a packet is accepted by the ruleset it may still get discarded by other means, for example Linux
generally ignores various ICMP types and there are sysctl options like net.ipv{4,6}.conf.*.forwarding or
net.ipv4.conf.*.rp_filter.
β’ Tables are merely a concept of nftables to structure the ruleset. They are not relevant to the evaluation of the
ruleset.
β’ Packets traverse the network stack and at various hooks (see the section called βADDRESS FAMILIESβ above for
lists of hooks per address family) theyβre evaluated by any base chains attached to these hooks.
β’ Base chains may call regular chains (via jump and goto). Regular chains can call other regular chains. Evaluation
continues in the called chain. Chains residing in a different table cannot be called.
β’ For each hook, the attached chains are evaluated in order of their priorities. Chains with lower priority values
are evaluated before those with higher ones. The order of chains with the same priority value is undefined.
β’ An accept verdict (including an implict one via the base chainβs policy) ends the evaluation of the current base
chain. It is not relevant if the accept verdict is issued in the base chain itself or a regular chain called from
the base chain. The packet advances to the next base chain. Thus a packet is ultimately accepted if and only if
no (matching) rule or base chain policy issues a drop verdict. All this applies to verdict-like statements that
imply accept, for example the NAT statements.
β’ A drop verdict (including an implict one via the base chainβs policy) immediately ends the evaluation of the
whole ruleset. No further chains of any hook are consulted. It is therefore not possible to have a drop verdict
changed to an accept in a later chain. This also applies to other terminal statements that imply drop, for
example reject. Thus, if any base chain uses drop as its policy, the same base chain (or a regular chain directly
or indirectly called by it) must contain at least one matching accept rule or the packet will be dropped.
β’ Given the semantics of accept/drop and only with respect to the utlimate decision of whether a packet is accepted
or dropped, the ordering of the various base chains per hook via their priorities matters only in so far, as any
of them modifies the packet or its meta data and that has an influence on the verdicts issued by the chains β
other than that, the ordering shouldnβt matter (except for performance and other side effects). It also means
that short-circuiting the ultimate decision is only possible via drop verdicts (respectively verdict-like
statements that imply drop, for example reject).
β’ A jump verdict causes the current position to be stored in the call stack of chains and evaluation to continue at
the beginning of the called regular chain. Called chains must be from the same table and cannot be base chains.
When the end of the called chain is reached, an implicit return verdict is issued. Other verdicts (respectively
verdict-like statements) are processed as described above and below.
β’ A goto verdict is equal to jump except that the current position is not stored in the call stack of chains.
β’ A return verdict ends the evaluation of the current chain, pops the most recently added position from the call
stack of chains and causes evaluation to continue after that position. When thereβs no position to pop (which is
the case when the current chain is either the base chain or a regular chain that was reached solely via goto
verdicts) it ends the evaluation of the current base chain (and any regular chains called from it) using the base
chainβs policy as implicit verdict.
β’ Examples for jump/goto/return:
β’ base {jump}β regular-1 {jump}β regular-2 At the end of regular-2 or when a return is issued in that, evaluation
continues after the jump position in regular-1. At the end of regular-1 or when a return is issued in that,
evaluation continues after the jump position in base.
β’ base {jump}β regular-1 {goto}β regular-2 At the end of regular-2 or when a return is issued in that, evaluation
continues after the jump position in base.
β’ base {jump}β regular-1 {jump}β regular-2 {goto}β regular-3 At the end of regular-3 or when a return is issued in
that, evaluation continues after the jump position in regular-1. At the end of regular-1 or when a return is
issued in that, evaluation continues after the jump position in base.
β’ base {jump}β regular-1 {goto}β regular-2 {goto}β regular-3 At the end of regular-3 or when a return is issued in
that, evaluation continues after the jump position in base.
β’ Verdicts (that is: accept, drop, jump, goto, return and continue) as well as statements that imply a verdict
(like reject or the NAT statements) also end the evaluation of any later statements in their respective rules
(respectively cause an error when loading such rules). For example in ... counter accept the counter statement is
processed, but in ... accept counter it is not. This does not apply to the comment statement, which is always
evaluated.
SETS
nftables offers two kinds of set concepts. Anonymous sets are sets that have no specific name. The set members are
enclosed in curly braces, with commas to separate elements when creating the rule the set is used in. Once that rule
is removed, the set is removed as well. They cannot be updated, i.e. once an anonymous set is declared it cannot be
changed anymore except by removing/altering the rule that uses the anonymous set.
Using anonymous sets to accept particular subnets and ports.
nft add rule filter input ip saddr { 10.0.0.0/8, 192.168.0.0/16 } tcp dport { 22, 443 } accept
Named sets are sets that need to be defined first before they can be referenced in rules. Unlike anonymous sets,
elements can be added to or removed from a named set at any time. Sets are referenced from rules using an @ prefixed
to the sets name.
Using named sets to accept addresses and ports.
nft add rule filter input ip saddr @allowed_hosts tcp dport @allowed_ports accept
The sets allowed_hosts and allowed_ports need to be created first. The next section describes nft set syntax in more
detail.
add set [family] table set { type type | typeof expression ; [flags flags ;] [timeout timeout ;] [gc-interval gc-interval ;] [elements = { element[, ...] } ;] [size size ;] [comment comment ;] [policy 'policy ;] [auto-merge ;] }
{delete | destroy | list | flush | reset } set [family] table set
list sets [family] [table]
delete set [family] table handle handle
{add | delete | destroy } element [family] table set { element[, ...] }
Sets are element containers of a user-defined data type, they are uniquely identified by a user-defined name and
attached to tables. Their behaviour can be tuned with the flags that can be specified at set creation time.
See Set and Map flags below for more details.
add Add a new set in the specified table. See
the Set specification table below for
more information about how to specify
properties of a set.
delete Delete the specified set.
destroy Delete the specified set, it does not
fail if it does not exist.
list Display the elements in the specified
set.
flush Remove all elements from the specified
set.
reset Reset state in all contained elements,
e.g. counter and quota statement values.
Table 8. Set specifications
βββββββββββββββ¬βββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β type β data type of set elements β string: ipv4_addr, ipv6_addr, β
β β β ether_addr, inet_proto, β
β β β inet_service, mark β
βββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β typeof β data type of set element β expression to derive the data β
β β β type from β
βββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β flags β set flags β string: constant, dynamic, β
β β β interval, timeout. Used to β
β β β describe the sets properties. β
βββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β timeout β time an element stays in the β string, decimal followed by β
β β set, mandatory if set is added β unit. Units are: d, h, m, s β
β β to from the packet path β β
β β (ruleset) β β
βββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β gc-interval β garbage collection interval, β string, decimal followed by β
β β only available when timeout or β unit. Units are: d, h, m, s β
β β flag timeout are active β β
βββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β elements β elements contained by the set β set data type β
βββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β size β maximum number of elements in β unsigned integer (64 bit) β
β β the set, mandatory if set is β β
β β added to from the packet path β β
β β (ruleset) β β
βββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β policy β set policy β string: performance [default], β
β β β memory β
βββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β auto-merge β automatic merge of β β
β β adjacent/overlapping set β β
β β elements (only for interval β β
β β sets) β β
βββββββββββββββ΄βββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ
The gc-interval doesnβt affect element timeout, but it does affect memory reclaim. A large set that rarely has
elements that time out can use a higher (less frequent) garbage collection to save cpu time, whereas sets that see
many updates with short-lived elements will benefit from a lower interval. Lower intervals ensure the set stays below
its maximum size. Internally, a timed-out entry stays around until it is removed by the garbage collector, which also
decrements the sets element count. This also means that it is possible to have a set that can not accept more
elements, even if all elements timed out, if the gc-interval is set too large.
The size defines the upper limit of the amount of elements that the set can support. Mandatory for sets that are
added to from the ruleset with the add and update keywords. Providing the size keyword for sets that are only added
to via nft add element allows for a more compact (memory conserving) set implementation selection, but it is not
required.
The optional policy keyword can be used to request a more memory-conserving set implementation.
auto-merge instructs the nftables frontend to merge adjacent and overlapping ranges. Example: When the set contains
range 1.2.3.1-1.2.3.4, then adding element 1.2.3.2 has no effect. Adding 1.2.3.5 changes the existing range to cover
1.2.3.1-1.2.3.5. Without this flag, 1.2.3.2 can not be added and 1.2.3.5 is inserted as a new entry.
Equality of a value with a set is given if the value matches exactly one value in the set (which for intervals means
that itβs contained in any of them). See the section called βBITMASK TYPEβ for the subtle differences between
syntactically similarly looking equiality checks of sets and bitmasks.
MAPS
add map [family] table map { type type | typeof expression [flags flags ;] [elements = { element[, ...] } ;] [size size ;] [comment comment ;] [policy 'policy ;] }
{delete | destroy | list | flush | reset } map [family] table map
list maps [family] [table]
Maps store data based on some specific key used as input. They are uniquely identified by a user-defined name and
attached to tables.
add Add a new map in the specified table.
delete Delete the specified map.
destroy Delete the specified map, it does not
fail if it does not exist.
list Display the elements in the specified
map.
flush Remove all elements from the specified
map.
reset Reset state in all contained elements,
e.g. counter and quota statement values.
Table 9. Map specifications
ββββββββββββ¬ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β type β data type of map elements β string: ipv4_addr, ipv6_addr, β
β β β ether_addr, inet_proto, β
β β β inet_service, mark, counter, β
β β β quota. Counter and quota canβt β
β β β be used as keys β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β typeof β data type of set element β expression to derive the data β
β β β type from β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β flags β map flags β string, same as set flags β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β elements β elements contained by the map β map data type β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β size β maximum number of elements in β unsigned integer (64 bit) β
β β the map β β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β policy β map policy β string: performance [default], β
β β β memory β
ββββββββββββ΄ββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ
Users can specify the properties/features that the set/map must support. This allows the kernel to pick an optimal
internal representation. If a required flag is missing, the ruleset might still work, as nftables will auto-enable
features if it can infer this from the ruleset. This may not work for all cases, however, so it is recommended to
specify all required features in the set/map definition manually. Also, some features are mutually exclusive. For
example, it is not possible for a set to support intervals and insertion from the packet path.
Table 10. Set and Map flags
ββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββ
β Flag β Description β
ββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββ€
β β β
β constant β Set contents will never change after β
β β creation β
ββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββ€
β β β
β dynamic β Set must support updates from the packet β
β β path with the add, update or delete β
β β keywords. β
ββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββ€
β β β
β interval β Set must be able to store intervals β
β β (ranges). Cannot be combined with the β
β β dynamic flag. β
ββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββ€
β β β
β timeout β Set must support element timeouts β
β β (auto-removal of elements once they β
β β expire). β
ββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββ
ELEMENTS
{add | create | delete | destroy | get | reset } element [family] table set { ELEMENT[, ...] }
ELEMENT := key_expression OPTIONS [: value_expression]
OPTIONS := [timeout TIMESPEC] [expires TIMESPEC] [comment string]
TIMESPEC := [numd][numh][numm][num[s]]
Element-related commands allow one to change contents of named sets and maps. key_expression is typically a value
matching the set type. value_expression is not allowed in sets but mandatory when adding to maps, where it matches
the data part in its type definition. When deleting from maps, it may be specified but is optional as key_expression
uniquely identifies the element.
create command is similar to add with the exception that none of the listed elements may already exist.
get command is useful to check if an element is contained in a set which may be non-trivial in very large and/or
interval sets. In the latter case, the containing interval is returned instead of just the element itself.
reset command resets state attached to the given element(s), e.g. counter and quota statement values.
Table 11. Element options
βββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ
β Option β Description β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββ€
β β β
β timeout β timeout value for sets/maps with flag β
β β timeout β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββ€
β β β
β expires β the time until given element expires, β
β β useful for ruleset replication only β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββ€
β β β
β comment β per element comment field β
βββββββββββ΄ββββββββββββββββββββββββββββββββββββββββ
FLOWTABLES
{add | create} flowtable [family] table flowtable { hook hook priority priority ; devices = { DEVICE_LIST } ; }
list flowtables [family] [table]
{delete | destroy | list} flowtable [family] table flowtable
delete flowtable [family] table handle handle
DEVICE_LIST := DEVICE_NAME [, DEVICE_LIST]
DEVICE_NAME := string | string*
Flowtables allow you to accelerate packet forwarding in software. Flowtables entries are represented through a tuple
that is composed of the input interface, source and destination address, source and destination port; and layer 3/4
protocols. Each entry also caches the destination interface and the gateway address - to update the destination
link-layer address - to forward packets. The ttl and hoplimit fields are also decremented. Hence, flowtables provides
an alternative path that allow packets to bypass the classic forwarding path. Flowtables reside in the ingress hook
that is located before the prerouting hook. You can select which flows you want to offload through the flow
expression from the forward chain. Flowtables are identified by their address family and their name. The address
family must be one of ip, ip6, or inet. The inet address family is a dummy family which is used to create hybrid
IPv4/IPv6 tables. When no address family is specified, ip is used by default.
The priority can be a signed integer or filter which stands for 0. Addition and subtraction can be used to set
relative priority, e.g. filter + 5 equals to 5.
With newer kernels there is basic support for wildcards in DEVICE_LIST by specifying an asterisk suffix. The
flowtable will apply to all interfaces matching the given prefix. Use the list hooks command to see the current
status.
add Add a new flowtable for the given family
with the given name.
delete Delete the specified flowtable.
destroy Delete the specified flowtable, it does
not fail if it does not exist.
list List all flowtables.
STATEFUL OBJECTS
{add | delete | destroy | list | reset} counter [family] table object
{add | delete | destroy | list | reset} quota [family] table object
{add | delete | destroy | list} limit [family] table object
delete counter [family] table handle handle
delete quota [family] table handle handle
delete limit [family] table handle handle
destroy counter [family] table handle handle
destroy quota [family] table handle handle
destroy limit [family] table handle handle
list { counters | limits | quotas } [family] [table]
reset { counters | quotas } [family] [table]
Stateful objects are attached to tables and are identified by a unique name. They group stateful information from
rules, to reference them in rules the keywords "type name" are used e.g. "counter name".
add Add a new stateful object in the
specified table.
delete Delete the specified object.
destroy Delete the specified object, it does not
fail if it does not exist.
list Display stateful information the object
holds.
reset List-and-reset stateful object.
CT HELPER
add ct helper [family] table name { type type protocol protocol ; [l3proto family ;] }
delete ct helper [family] table name
list ct helpers
Ct helper is used to define connection tracking helpers that can then be used in combination with the ct helper set
statement. type and protocol are mandatory, l3proto is derived from the table family by default, i.e. in the inet
table the kernel will try to load both the ipv4 and ipv6 helper backends, if they are supported by the kernel.
Table 12. conntrack helper specifications
ββββββββββββ¬βββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β β β β
β type β name of helper type β quoted string (e.g. "ftp") β
ββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β β β β
β protocol β layer 4 protocol of the helper β string (e.g. ip) β
ββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β β β β
β l3proto β layer 3 protocol of the helper β address family (e.g. ip) β
ββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββ€
β β β β
β comment β per ct helper comment field β string β
ββββββββββββ΄βββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββ
defining and assigning ftp helper.
Unlike iptables, helper assignment needs to be performed after the conntrack
lookup has completed, for example with the default 0 hook priority.
table inet myhelpers {
ct helper ftp-standard {
type "ftp" protocol tcp
}
chain prerouting {
type filter hook prerouting priority filter;
tcp dport 21 ct helper set "ftp-standard"
}
}
CT TIMEOUT
add ct timeout [family] table name { protocol protocol ; policy = { state: value [, ...] } ; [l3proto family ;] }
delete ct timeout [family] table name
list ct timeouts
Ct timeout is used to update connection tracking timeout values.Timeout policies are assigned with the ct timeout set
statement. protocol and policy are mandatory, l3proto is derived from the table family by default.
Table 13. conntrack timeout specifications
ββββββββββββ¬ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββ€
β β β β
β protocol β layer 4 protocol of the timeout β string (e.g. ip) β
β β object β β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββ€
β β β β
β state β connection state name β string (e.g. "established") β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββ€
β β β β
β value β timeout value for connection β unsigned integer β
β β state β β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββ€
β β β β
β l3proto β layer 3 protocol of the timeout β address family (e.g. ip) β
β β object β β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββ€
β β β β
β comment β per ct timeout comment field β string β
ββββββββββββ΄ββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββ
tcp connection state names that can have a specific timeout value are:
close, close_wait, established, fin_wait, last_ack, retrans, syn_recv, syn_sent, time_wait and unack.
You can use sysctl -a |grep net.netfilter.nf_conntrack_tcp_timeout_ to view and change the system-wide defaults. ct
timeout allows for flow-specific settings, without changing the global timeouts.
For example, tcp port 53 could have much lower settings than other traffic.
udp state names that can have a specific timeout value are replied and unreplied.
defining and assigning ct timeout policy.
table ip filter {
ct timeout customtimeout {
protocol tcp;
l3proto ip
policy = { established: 2m, close: 20s }
}
chain output {
type filter hook output priority filter; policy accept;
ct timeout set "customtimeout"
}
}
testing the updated timeout policy.
% conntrack -E
It should display:
[UPDATE] tcp 6 120 ESTABLISHED src=172.16.19.128 dst=172.16.19.1
sport=22 dport=41360 [UNREPLIED] src=172.16.19.1 dst=172.16.19.128
sport=41360 dport=22
CT EXPECTATION
add ct expectation [family] table name { protocol protocol ; dport dport ; timeout timeout ; size size ; [l3proto family ;] }
delete ct expectation [family] table name
list ct expectations
Ct expectation is used to create connection expectations. Expectations are assigned with the ct expectation set
statement. protocol, dport, timeout and size are mandatory, l3proto is derived from the table family by default.
Table 14. conntrack expectation specifications
ββββββββββββ¬ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β β β β
β protocol β layer 4 protocol of the β string (e.g. ip) β
β β expectation object β β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β β β β
β dport β destination port of expected β unsigned integer β
β β connection β β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β β β β
β timeout β timeout value for expectation β unsigned integer β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β β β β
β size β size value for expectation β unsigned integer β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β β β β
β l3proto β layer 3 protocol of the β address family (e.g. ip) β
β β expectation object β β
ββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β β β β
β comment β per ct expectation comment β string β
β β field β β
ββββββββββββ΄ββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββ
defining and assigning ct expectation policy.
table ip filter {
ct expectation expect {
protocol udp
dport 9876
timeout 2m
size 8
l3proto ip
}
chain input {
type filter hook input priority filter; policy accept;
ct expectation set "expect"
}
}
COUNTER
add counter [family] table name [{ [ packets packets bytes bytes ; ] [ comment comment ; }]
delete counter [family] table name
list counters
Table 15. Counter specifications
βββββββββββ¬ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββΌββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β packets β initial count of packets β unsigned integer (64 bit) β
βββββββββββΌββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β bytes β initial count of bytes β unsigned integer (64 bit) β
βββββββββββΌββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β comment β per counter comment field β string β
βββββββββββ΄ββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββ
Using named counters.
nft add counter filter http
nft add rule filter input tcp dport 80 counter name \"http\"
Using named counters with maps.
nft add counter filter http
nft add counter filter https
nft add rule filter input counter name tcp dport map { 80 : \"http\", 443 : \"https\" }
QUOTA
add quota [family] table name { [over|until] bytes BYTE_UNIT [ used bytes BYTE_UNIT ] ; [ comment comment ; ] }
BYTE_UNIT := bytes | kbytes | mbytes
delete quota [family] table name
list quotas
Table 16. Quota specifications
βββββββββββ¬βββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββΌβββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β quota β quota limit, used as the quota β Two arguments, unsigned integer β
β β name β (64 bit) and string: bytes, β
β β β kbytes, mbytes. "over" and β
β β β "until" go before these β
β β β arguments β
βββββββββββΌβββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β used β initial value of used quota β Two arguments, unsigned integer β
β β β (64 bit) and string: bytes, β
β β β kbytes, mbytes β
βββββββββββΌβββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β comment β per quota comment field β string β
βββββββββββ΄βββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββ
Using named quotas.
nft add quota filter user123 { over 20 mbytes }
nft add rule filter input ip saddr 192.168.10.123 quota name \"user123\"
Using named quotas with maps.
nft add quota filter user123 { over 20 mbytes }
nft add quota filter user124 { over 20 mbytes }
nft add rule filter input quota name ip saddr map { 192.168.10.123 : \"user123\", 192.168.10.124 : \"user124\" }
EXPRESSIONS
Expressions represent values, either constants like network addresses, port numbers, etc., or data gathered from the
packet during ruleset evaluation. Expressions can be combined using binary, logical, relational and other types of
expressions to form complex or relational (match) expressions. They are also used as arguments to certain types of
operations, like NAT, packet marking etc.
Each expression has a data type, which determines the size, parsing and representation of symbolic values and type
compatibility with other expressions.
DESCRIBE COMMAND
describe expression | data type
The describe command shows information about the type of an expression and its data type. A data type may also be
given, in which nft will display more information about the type.
The describe command.
$ nft describe tcp flags
payload expression, datatype tcp_flag (TCP flag) (basetype bitmask, integer), 8 bits
predefined symbolic constants:
fin 0x01
syn 0x02
rst 0x04
psh 0x08
ack 0x10
urg 0x20
ecn 0x40
cwr 0x80
DATA TYPES
Data types determine the size, parsing and representation of symbolic values and type compatibility of expressions. A
number of global data types exist, in addition some expression types define further data types specific to the
expression type. Most data types have a fixed size, some however may have a dynamic size, f.i. the string type. Some
types also have predefined symbolic constants. Those can be listed using the nft describe command:
$ nft describe ct_state
datatype ct_state (conntrack state) (basetype bitmask, integer), 32 bits
pre-defined symbolic constants (in hexadecimal):
invalid 0x00000001
new ...
Types may be derived from lower order types, f.i. the IPv4 address type is derived from the integer type, meaning an
IPv4 address can also be specified as an integer value.
In certain contexts (set and map definitions), it is necessary to explicitly specify a data type. Each type has a
name which is used for this.
INTEGER TYPE
βββββββββββ¬ββββββββββ¬βββββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
βββββββββββΌββββββββββΌβββββββββββΌββββββββββββ€
β β β β β
β Integer β integer β variable β - β
βββββββββββ΄ββββββββββ΄βββββββββββ΄ββββββββββββ
The integer type is used for numeric values. It may be specified as a decimal, hexadecimal or octal number. The
integer type does not have a fixed size, its size is determined by the expression for which it is used.
BITMASK TYPE
βββββββββββ¬ββββββββββ¬βββββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
βββββββββββΌββββββββββΌβββββββββββΌββββββββββββ€
β β β β β
β Bitmask β bitmask β variable β integer β
βββββββββββ΄ββββββββββ΄βββββββββββ΄ββββββββββββ
The bitmask type (bitmask) is used for bitmasks.
In expressions the bits of a bitmask may be specified as bit[,bit]... with bit being the value of the bit or a
pre-defined symbolic constant, if any (for example ct stateβs bit 0x1 has the symbolic constant new).
Equality of a value with such bitmask is given, if the value has any of the bitmaskβs bits set (and optionally
others).
The syntax expression value / mask is identical to expression and mask == value. For example tcp flags syn,ack /
syn,ack,fin,rst is the same as tcp flags and (syn|ack|fin|rst) == syn|ack.
Note that expression bit[,bit]... is not generally the same as expression {bit[,bit]...} and analogously with a named
set. The latter forms are lookups in a set and will match only if the set contains exactly one value that matches.
They are however effectively the same (with matching bitmasks typically being faster) when all bits are semantically
mutually exclusive.
Examples: * tcp flags syn,ack matches packets that have the SYN, the ACK or both SYN and ACK flags set. Other flags
are ignored. tcp flags { syn, ack } matches packets that have either only the SYN or only the ACK flag set. All other
flags must be unset. * ct state established,related and ct state { established, related } * match exactly the same
packets, because the bits of *ct state are all mutually exclusive.
As usual, the the nft describe command may be used to get details on a data type, which for bitmasks shows the
symbolic names and values of the bits.
STRING TYPE
ββββββββββ¬ββββββββββ¬βββββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
ββββββββββΌββββββββββΌβββββββββββΌββββββββββββ€
β β β β β
β String β string β variable β - β
ββββββββββ΄ββββββββββ΄βββββββββββ΄ββββββββββββ
The string type is used for character strings. A string begins with an alphabetic character (a-zA-Z) followed by zero
or more alphanumeric characters or the characters /, -, _ and .. In addition, anything enclosed in double quotes (")
is recognized as a string.
String specification.
# Interface name
filter input iifname eth0
# Weird interface name
filter input iifname "(eth0)"
LINK LAYER ADDRESS TYPE
ββββββββββββββββββββββ¬ββββββββββ¬βββββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
ββββββββββββββββββββββΌββββββββββΌβββββββββββΌββββββββββββ€
β β β β β
β Link layer address β lladdr β variable β integer β
ββββββββββββββββββββββ΄ββββββββββ΄βββββββββββ΄ββββββββββββ
The link layer address type is used for link layer addresses. Link layer addresses are specified as a variable amount
of groups of two hexadecimal digits separated using colons (:).
Link layer address specification.
# Ethernet destination MAC address
filter input ether daddr 20:c9:d0:43:12:d9
IPV4 ADDRESS TYPE
ββββββββββββββββ¬ββββββββββββ¬βββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
ββββββββββββββββΌββββββββββββΌβββββββββΌββββββββββββ€
β β β β β
β IPV4 address β ipv4_addr β 32 bit β integer β
ββββββββββββββββ΄ββββββββββββ΄βββββββββ΄ββββββββββββ
The IPv4 address type is used for IPv4 addresses. Addresses are specified in either dotted decimal, dotted
hexadecimal, dotted octal, decimal, hexadecimal, octal notation or as a host name. A host name will be resolved using
the standard system resolver.
IPv4 address specification.
# dotted decimal notation
filter output ip daddr 127.0.0.1
# host name
filter output ip daddr localhost
IPV6 ADDRESS TYPE
ββββββββββββββββ¬ββββββββββββ¬ββββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
ββββββββββββββββΌββββββββββββΌββββββββββΌββββββββββββ€
β β β β β
β IPv6 address β ipv6_addr β 128 bit β integer β
ββββββββββββββββ΄ββββββββββββ΄ββββββββββ΄ββββββββββββ
The IPv6 address type is used for IPv6 addresses. Addresses are specified as a host name or as hexadecimal halfwords
separated by colons. Addresses might be enclosed in square brackets ("[]") to differentiate them from port numbers.
IPv6 address specification.
# abbreviated loopback address
filter output ip6 daddr ::1
IPv6 address specification with bracket notation.
# without [] the port number (22) would be parsed as part of the
# ipv6 address
ip6 nat prerouting tcp dport 2222 dnat to [1ce::d0]:22
BOOLEAN TYPE
βββββββββββ¬ββββββββββ¬ββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
βββββββββββΌββββββββββΌββββββββΌββββββββββββ€
β β β β β
β Boolean β boolean β 1 bit β integer β
βββββββββββ΄ββββββββββ΄ββββββββ΄ββββββββββββ
The boolean type is a syntactical helper type in userspace. Its use is in the right-hand side of a (typically
implicit) relational expression to change the expression on the left-hand side into a boolean check (usually for
existence).
Table 17. The following keywords will automatically resolve into a boolean type with given value
βββββββββββ¬ββββββββ
β Keyword β Value β
βββββββββββΌββββββββ€
β β β
β exists β 1 β
βββββββββββΌββββββββ€
β β β
β missing β 0 β
βββββββββββ΄ββββββββ
Table 18. expressions support a boolean comparison
ββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββ
β Expression β Behaviour β
ββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββ€
β β β
β fib β Check route existence. β
ββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββ€
β β β
β exthdr β Check IPv6 extension header existence. β
ββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββ€
β β β
β tcp option β Check TCP option header existence. β
ββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββ
Boolean specification.
# match if route exists
filter input fib daddr . iif check exists
# match only non-fragmented packets in IPv6 traffic
filter input exthdr frag missing
# match if TCP timestamp option is present
filter input tcp option timestamp exists
ICMP TYPE TYPE
βββββββββββββ¬ββββββββββββ¬ββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
βββββββββββββΌββββββββββββΌββββββββΌββββββββββββ€
β β β β β
β ICMP Type β icmp_type β 8 bit β integer β
βββββββββββββ΄ββββββββββββ΄ββββββββ΄ββββββββββββ
The ICMP Type type is used to conveniently specify the ICMP headerβs type field.
Table 19. Keywords may be used when specifying the ICMP type
βββββββββββββββββββββββββββ¬ββββββββ
β Keyword β Value β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β echo-reply β 0 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β destination-unreachable β 3 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β source-quench β 4 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β redirect β 5 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β echo-request β 8 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β router-advertisement β 9 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β router-solicitation β 10 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β time-exceeded β 11 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β parameter-problem β 12 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β timestamp-request β 13 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β timestamp-reply β 14 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β info-request β 15 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β info-reply β 16 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β address-mask-request β 17 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β address-mask-reply β 18 β
βββββββββββββββββββββββββββ΄ββββββββ
ICMP Type specification.
# match ping packets
filter output icmp type { echo-request, echo-reply }
ICMP CODE TYPE
βββββββββββββ¬ββββββββββββ¬ββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
βββββββββββββΌββββββββββββΌββββββββΌββββββββββββ€
β β β β β
β ICMP Code β icmp_code β 8 bit β integer β
βββββββββββββ΄ββββββββββββ΄ββββββββ΄ββββββββββββ
The ICMP Code type is used to conveniently specify the ICMP headerβs code field.
ICMPV6 TYPE TYPE
βββββββββββββββ¬ββββββββββββββ¬ββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
βββββββββββββββΌββββββββββββββΌββββββββΌββββββββββββ€
β β β β β
β ICMPv6 Type β icmpv6_type β 8 bit β integer β
βββββββββββββββ΄ββββββββββββββ΄ββββββββ΄ββββββββββββ
The ICMPv6 Type type is used to conveniently specify the ICMPv6 headerβs type field.
Table 20. keywords may be used when specifying the ICMPv6 type:
βββββββββββββββββββββββββββ¬ββββββββ
β Keyword β Value β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β destination-unreachable β 1 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β packet-too-big β 2 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β time-exceeded β 3 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β parameter-problem β 4 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β echo-request β 128 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β echo-reply β 129 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β mld-listener-query β 130 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β mld-listener-report β 131 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β mld-listener-done β 132 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β mld-listener-reduction β 132 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β nd-router-solicit β 133 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β nd-router-advert β 134 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β nd-neighbor-solicit β 135 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β nd-neighbor-advert β 136 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β nd-redirect β 137 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β router-renumbering β 138 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β ind-neighbor-solicit β 141 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β ind-neighbor-advert β 142 β
βββββββββββββββββββββββββββΌββββββββ€
β β β
β mld2-listener-report β 143 β
βββββββββββββββββββββββββββ΄ββββββββ
ICMPv6 Type specification.
# match ICMPv6 ping packets
filter output icmpv6 type { echo-request, echo-reply }
ICMPV6 CODE TYPE
βββββββββββββββ¬ββββββββββββββ¬ββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
βββββββββββββββΌββββββββββββββΌββββββββΌββββββββββββ€
β β β β β
β ICMPv6 Code β icmpv6_code β 8 bit β integer β
βββββββββββββββ΄ββββββββββββββ΄ββββββββ΄ββββββββββββ
The ICMPv6 Code type is used to conveniently specify the ICMPv6 headerβs code field.
CONNTRACK TYPES
Table 21. overview of types used in ct expression and statement
ββββββββββββββββββββββββ¬ββββββββββββ¬ββββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
ββββββββββββββββββββββββΌββββββββββββΌββββββββββΌββββββββββββ€
β β β β β
β conntrack state β ct_state β 4 byte β bitmask β
ββββββββββββββββββββββββΌββββββββββββΌββββββββββΌββββββββββββ€
β β β β β
β conntrack direction β ct_dir β 8 bit β integer β
ββββββββββββββββββββββββΌββββββββββββΌββββββββββΌββββββββββββ€
β β β β β
β conntrack status β ct_status β 4 byte β bitmask β
ββββββββββββββββββββββββΌββββββββββββΌββββββββββΌββββββββββββ€
β β β β β
β conntrack event bits β ct_event β 4 byte β bitmask β
ββββββββββββββββββββββββΌββββββββββββΌββββββββββΌββββββββββββ€
β β β β β
β conntrack label β ct_label β 128 bit β bitmask β
ββββββββββββββββββββββββ΄ββββββββββββ΄ββββββββββ΄ββββββββββββ
For each of the types above, keywords are available for convenience:
Table 22. conntrack state (ct_state)
βββββββββββββββ¬ββββββββ
β Keyword β Value β
βββββββββββββββΌββββββββ€
β β β
β invalid β 1 β
βββββββββββββββΌββββββββ€
β β β
β established β 2 β
βββββββββββββββΌββββββββ€
β β β
β related β 4 β
βββββββββββββββΌββββββββ€
β β β
β new β 8 β
βββββββββββββββΌββββββββ€
β β β
β untracked β 64 β
βββββββββββββββ΄ββββββββ
Table 23. conntrack direction (ct_dir)
ββββββββββββ¬ββββββββ
β Keyword β Value β
ββββββββββββΌββββββββ€
β β β
β original β 0 β
ββββββββββββΌββββββββ€
β β β
β reply β 1 β
ββββββββββββ΄ββββββββ
Table 24. conntrack status (ct_status)
βββββββββββββββββ¬ββββββββ¬ββββββββββββββββββββββββββββββββββ
β Keyword β Value β Description β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β expected β 1 β Expected connection; conntrack β
β β β helper set it up β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β seen-reply β 2 β Conntrack has seen packets in β
β β β both directions β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β assured β 4 β Conntrack entry will not be β
β β β removed if hash table is full β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β confirmed β 8 β Initial packet processed β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β snat β 16 β Original source address differs β
β β β from reply destination β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β dnat β 32 β Original destination differs β
β β β from reply source β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β seq-adjust β 64 β tcp sequence number rewrite due β
β β β to conntrack helper or synproxy β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β snat-done β 128 β tried to find matching β
β β β snat/masquerade rule β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β dnat-done β 256 β tried to find matching β
β β β dnat/redirect rule β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β dying β 512 β Connection about to be deleted β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β fixed-timeout β 1024 β entry expires even if traffic β
β β β is active β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β helper β 8192 β connection is monitored by β
β β β conntrack helper β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β offload β 16384 β connection is offloaded to a β
β β β flow table β
βββββββββββββββββΌββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β hw-offload β 32768 β connection is offloaded to β
β β β hardware β
βββββββββββββββββ΄ββββββββ΄ββββββββββββββββββββββββββββββββββ
Table 25. conntrack event bits (ct_event)
βββββββββββββ¬ββββββββ
β Keyword β Value β
βββββββββββββΌββββββββ€
β β β
β new β 1 β
βββββββββββββΌββββββββ€
β β β
β related β 2 β
βββββββββββββΌββββββββ€
β β β
β destroy β 4 β
βββββββββββββΌββββββββ€
β β β
β reply β 8 β
βββββββββββββΌββββββββ€
β β β
β assured β 16 β
βββββββββββββΌββββββββ€
β β β
β protoinfo β 32 β
βββββββββββββΌββββββββ€
β β β
β helper β 64 β
βββββββββββββΌββββββββ€
β β β
β mark β 128 β
βββββββββββββΌββββββββ€
β β β
β seqadj β 256 β
βββββββββββββΌββββββββ€
β β β
β secmark β 512 β
βββββββββββββΌββββββββ€
β β β
β label β 1024 β
βββββββββββββ΄ββββββββ
Possible keywords for conntrack label type (ct_label) are read at runtime from /etc/connlabel.conf.
DCCP PKTTYPE TYPE
ββββββββββββββββββββ¬βββββββββββββββ¬ββββββββ¬ββββββββββββ
β Name β Keyword β Size β Base type β
ββββββββββββββββββββΌβββββββββββββββΌββββββββΌββββββββββββ€
β β β β β
β DCCP packet type β dccp_pkttype β 4 bit β integer β
ββββββββββββββββββββ΄βββββββββββββββ΄ββββββββ΄ββββββββββββ
The DCCP packet type abstracts the different legal values of the respective four bit field in the DCCP header, as
stated by RFC4340. Note that possible values 10-15 are considered reserved and therefore not allowed to be used. In
iptables' dccp match, these values are aliased INVALID. With nftables, one may simply match on the numeric value
range, i.e. 10-15.
Table 26. keywords may be used when specifying the DCCP packet type
ββββββββββββ¬ββββββββ
β Keyword β Value β
ββββββββββββΌββββββββ€
β β β
β request β 0 β
ββββββββββββΌββββββββ€
β β β
β response β 1 β
ββββββββββββΌββββββββ€
β β β
β data β 2 β
ββββββββββββΌββββββββ€
β β β
β ack β 3 β
ββββββββββββΌββββββββ€
β β β
β dataack β 4 β
ββββββββββββΌββββββββ€
β β β
β closereq β 5 β
ββββββββββββΌββββββββ€
β β β
β close β 6 β
ββββββββββββΌββββββββ€
β β β
β reset β 7 β
ββββββββββββΌββββββββ€
β β β
β sync β 8 β
ββββββββββββΌββββββββ€
β β β
β syncack β 9 β
ββββββββββββ΄ββββββββ
PRIMARY EXPRESSIONS
The lowest order expression is a primary expression, representing either a constant or a single datum from a packetβs
payload, meta data or a stateful module.
META EXPRESSIONS
meta {length | nfproto | l4proto | protocol | priority}
[meta] {mark | iif | iifname | iiftype | oif | oifname | oiftype | skuid | skgid | nftrace | rtclassid | ibrname | obrname | pkttype | cpu | iifgroup | oifgroup | cgroup | random | ipsec | iifkind | oifkind | time | hour | day }
A meta expression refers to meta data associated with a packet.
There are two types of meta expressions: unqualified and qualified meta expressions. Qualified meta expressions
require the meta keyword before the meta key, unqualified meta expressions can be specified by using the meta key
directly or as qualified meta expressions. Meta l4proto is useful to match a particular transport protocol that is
part of either an IPv4 or IPv6 packet. It will also skip any IPv6 extension headers present in an IPv6 packet.
meta iif, oif, iifname and oifname are used to match the interface a packet arrived on or is about to be sent out on.
iif and oif are used to match on the interface index, whereas iifname and oifname are used to match on the interface
name. This is not the same β assuming the rule
filter input meta iif "foo"
Then this rule can only be added if the interface "foo" exists. Also, the rule will continue to match even if the
interface "foo" is renamed to "bar".
This is because internally the interface index is used. In case of dynamically created interfaces, such as tun/tap or
dialup interfaces (ppp for example), it might be better to use iifname or oifname instead.
In these cases, the name is used so the interface doesnβt have to exist to add such a rule, it will stop matching if
the interface gets renamed and it will match again in case interface gets deleted and later a new interface with the
same name is created.
Like with iptables, wildcard matching on interface name prefixes is available for iifname and oifname matches by
appending an asterisk (*) character. Note however that unlike iptables, nftables does not accept interface names
consisting of the wildcard character only - users are supposed to just skip those always matching expressions. In
order to match on literal asterisk character, one may escape it using backslash (\).
Table 27. Meta expression types
βββββββββββββ¬ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β length β Length of the packet in bytes β integer (32-bit) β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β nfproto β real hook protocol family, β integer (32 bit) β
β β useful only in inet table β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β l4proto β layer 4 protocol, skips ipv6 β integer (8 bit) β
β β extension headers β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β protocol β EtherType protocol value β ether_type β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β priority β TC packet priority β tc_handle β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β mark β Packet mark β mark β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β iif β Input interface index β iface_index β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β iifname β Input interface name β ifname β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β iiftype β Input interface type β iface_type β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β oif β Output interface index β iface_index β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β oifname β Output interface name β ifname β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β oiftype β Output interface hardware type β iface_type β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β sdif β Slave device input interface β iface_index β
β β index β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β sdifname β Slave device interface name β ifname β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β skuid β UID associated with originating β uid β
β β socket β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β skgid β GID associated with originating β gid β
β β socket β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β rtclassid β Routing realm β realm β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β ibrname β Input bridge interface name β ifname β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β obrname β Output bridge interface name β ifname β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β pkttype β packet type β pkt_type β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β cpu β cpu number processing the β integer (32 bit) β
β β packet β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β iifgroup β incoming device group β devgroup β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β oifgroup β outgoing device group β devgroup β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β cgroup β control group net_cls.classid β integer (32 bit) β
β β (for matching on cgroupv2, see β β
β β socket cgroupv2) β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β random β pseudo-random number β integer (32 bit) β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β ipsec β true if packet was ipsec β boolean (1 bit) β
β β encrypted β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β iifkind β Input interface kind β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β oifkind β Output interface kind β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β time β Absolute time of packet β Integer (32 bit) or string β
β β reception β β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β day β Day of week β Integer (8 bit) or string β
βββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ€
β β β β
β hour β Hour of day β String value in the form HH:MM β
β β β or HH:MM:SS. Values are β
β β β expected to be less than 24:00, β
β β β although for technical reasons, β
β β β 23:59:60 is accepted, too. β
βββββββββββββ΄ββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββ
Table 28. Meta expression specific types
βββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β Type β Description β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β iface_index β Interface index (32 bit number). Can be β
β β specified numerically or as name of an β
β β existing interface. β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β ifname β Interface name (16 byte string). Does not β
β β have to exist. β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β iface_type β Interface type (16 bit number). β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β uid β User ID (32 bit number). Can be specified β
β β numerically or as user name. β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β gid β Group ID (32 bit number). Can be β
β β specified numerically or as group name. β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β realm β Routing Realm (32 bit number). Can be β
β β specified numerically or as symbolic name β
β β defined in /etc/iproute2/rt_realms. β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β devgroup_type β Device group (32 bit number). Can be β
β β specified numerically or as symbolic name β
β β defined in /etc/iproute2/group. β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β pkt_type β Packet type: host (addressed to local β
β β host), broadcast (to all), multicast (to β
β β group), other (addressed to another β
β β host). β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β ifkind β Interface kind (16 byte string). See β
β β TYPES in ip-link(8) for a list. β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β time β Either an integer or a date in ISO β
β β format. For example: "2019-06-06 17:00". β
β β Hour and seconds are optional and can be β
β β omitted if desired. If omitted, midnight β
β β will be assumed. The following three β
β β would be equivalent: "2019-06-06", β
β β "2019-06-06 00:00" and "2019-06-06 β
β β 00:00:00". Use a range expression such as β
β β "2019-06-06 10:00"-"2019-06-10 14:00" for β
β β matching a time range. When an integer is β
β β given, it is assumed to be a UNIX β
β β timestamp. β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β day β Either a day of week ("Monday", β
β β "Tuesday", etc.), or an integer between 0 β
β β and 6. Strings are matched β
β β case-insensitively, and a full match is β
β β not expected (e.g. "Mon" would match β
β β "Monday"). When an integer is given, 0 is β
β β Sunday and 6 is Saturday. Use a range β
β β expression such as "Monday"-"Wednesday" β
β β for matching a week day range. β
βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β hour β A string representing an hour in 24-hour β
β β format. Seconds can optionally be β
β β specified. For example, 17:00 and β
β β 17:00:00 would be equivalent. Use a range β
β β expression such as "17:00"-"19:00" for β
β β matching a time range. β
βββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββ
Using meta expressions.
# qualified meta expression
filter output meta oif eth0
filter forward meta iifkind { "tun", "veth" }
# unqualified meta expression
filter output oif eth0
# incoming packet was subject to ipsec processing
raw prerouting meta ipsec exists accept
# match incoming packet from 03:00 to 14:00 local time
raw prerouting meta hour "03:00"-"14:00" counter accept
SOCKET EXPRESSION
socket {transparent | mark | wildcard}
socket cgroupv2 level NUM
Socket expression can be used to search for an existing open TCP/UDP socket and its attributes that can be associated
with a packet. It looks for an established or non-zero bound listening socket (possibly with a non-local address).
You can also use it to match on the socket cgroupv2 at a given ancestor level, e.g. if the socket belongs to cgroupv2
a/b, ancestor level 1 checks for a matching on cgroup a and ancestor level 2 checks for a matching on cgroup b.
Table 29. Available socket attributes
βββββββββββββββ¬ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββ
β Name β Description β Type β
βββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββ€
β β β β
β transparent β Value of the IP_TRANSPARENT β boolean (1 bit) β
β β socket option in the found β β
β β socket. It can be 0 or 1. β β
βββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββ€
β β β β
β mark β Value of the socket mark β mark β
β β (SOL_SOCKET, SO_MARK). β β
βββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββ€
β β β β
β wildcard β Indicates whether the socket is β boolean (1 bit) β
β β wildcard-bound (e.g. 0.0.0.0 or β β
β β ::0). β β
βββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββ€
β β β β
β cgroupv2 β cgroup version 2 for this β cgroupv2 β
β β socket (path from β β
β β /sys/fs/cgroup) β β
βββββββββββββββ΄ββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββ
Using socket expression.
# Mark packets that correspond to a transparent socket. "socket wildcard 0"
# means that zero-bound listener sockets are NOT matched (which is usually
# exactly what you want).
table inet x {
chain y {
type filter hook prerouting priority mangle; policy accept;
socket transparent 1 socket wildcard 0 mark set 0x00000001 accept
}
}
# Trace packets that corresponds to a socket with a mark value of 15
table inet x {
chain y {
type filter hook prerouting priority mangle; policy accept;
socket mark 0x0000000f nftrace set 1
}
}
# Set packet mark to socket mark
table inet x {
chain y {
type filter hook prerouting priority mangle; policy accept;
tcp dport 8080 mark set socket mark
}
}
# Count packets for cgroupv2 "user.slice" at level 1
table inet x {
chain y {
type filter hook input priority filter; policy accept;
socket cgroupv2 level 1 "user.slice" counter
}
}
OSF EXPRESSION
osf [ttl {loose | skip}] {name | version}
The osf expression does passive operating system fingerprinting. This expression compares some data (Window Size,
MSS, options and their order, DF, and others) from packets with the SYN bit set.
Table 30. Available osf attributes
βββββββββββ¬ββββββββββββββββββββββββββββββββββ¬βββββββββ
β Name β Description β Type β
βββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββ€
β β β β
β ttl β Do TTL checks on the packet to β string β
β β determine the operating system. β β
βββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββ€
β β β β
β version β Do OS version checks on the β β
β β packet. β β
βββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββ€
β β β β
β name β Name of the OS signature to β string β
β β match. All signatures can be β β
β β found at pf.os file. Use β β
β β "unknown" for OS signatures β β
β β that the expression could not β β
β β detect. β β
βββββββββββ΄ββββββββββββββββββββββββββββββββββ΄βββββββββ
Available ttl values.
If no TTL attribute is passed, make a true IP header and fingerprint TTL true comparison. This generally works for LANs.
* loose: Check if the IP header's TTL is less than the fingerprint one. Works for globally-routable addresses.
* skip: Do not compare the TTL at all.
Using osf expression.
# Accept packets that match the "Linux" OS genre signature without comparing TTL.
table inet x {
chain y {
type filter hook input priority filter; policy accept;
osf ttl skip name "Linux"
}
}
FIB EXPRESSIONS
fib FIB_TUPLE FIB_RESULT
FIB_TUPLE := { saddr | daddr} [ . { iif | oif } . mark ]
FIB_RESULT := { oif | oifname | check | type }
A fib expression queries the fib (forwarding information base) to obtain information such as the output interface
index.
The first arguments to the fib expression are the input keys to be passed to the fib lookup function. One of saddr or
daddr is mandatory, they are also mutually exclusive.
mark, iif and oif keywords are optional modifiers to influence the search result, see the FIB_TUPLE keyword table
below for a description. The iif and oif tuple keywords are also mutually exclusive.
The last argument to the fib expression is the desired result type.
oif asks to obtain the interface index that would be used to send packets to the packets source (saddr key) or
destination (daddr key). If no routing entry is found, the returned interface index is 0.
oifname is like oif, but it fills the interface name instead. This is useful to check dynamic interfaces such as ppp
devices. If no entry is found, an empty interface name is returned.
type returns the address type such as unicast or multicast. A complete list of supported address types can be shown
with nft describe fib_addrtype.
Table 31. FIB_TUPLE keywords
βββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β flag β Description β
βββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β daddr β Perform a normal route lookup: search fib β
β β for route to the destination address of β
β β the packet. β
βββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β saddr β Perform a reverse route lookup: search β
β β the fib for route to the source address β
β β of the packet. β
βββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β mark β consider the packet mark (nfmark) when β
β β querying the fib. β
βββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β iif β if fib lookups provides a route then β
β β check its output interface is identical β
β β to the packets input interface. β
βββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β oif β if fib lookups provides a route then β
β β check its output interface is identical β
β β to the packets output interface. This β
β β flag can only be used with the type β
β β result. β
βββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββ
Table 32. FIB_RESULT keywords
βββββββββββ¬βββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β Keyword β Description β Result Type β
βββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β oif β Output interface index β iface_index β
βββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β check β Output interface check β boolean β
βββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β oifname β Output interface name β ifname β
βββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β β β β
β type β Address type β fib_addrtype (see nft describe β
β β β fib_addrtype for a list) β
βββββββββββ΄βββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ
The oif and oifname result is only valid in the prerouting, input and forward hooks. The type can be queried from any
one of prerouting, input, forward output and postrouting.
For type, the presence of the iif keyword in the FIB_TUPLE modifiers restrict the available hooks to those where the
packet is associated with an incoming interface, i.e. prerouting, input and forward. Likewise, the oif keyword in the
FIB_TUPLE modifier list will limit the available hooks to forward, output and postrouting.
Using fib expressions.
# drop packets without a reverse path
filter prerouting fib saddr . iif oif missing drop
In this example, 'saddr . iif' looks up a route to the *source address* of the packet and restricts matching
results to the interface that the packet arrived on, then stores the output interface index from the obtained
fib route result.
If no route was found for the source address/input interface combination, the output interface index is zero.
Hence, this rule will drop all packets that do not have a strict reverse path (hypothetical reply packet
would be sent via the interface the tested packet arrived on).
If only 'saddr oif' is used as the input key, then this rule would only drop packets where the fib cannot
find a route. In most setups this will never drop packets because the default route is returned.
# drop packets if the destination ip address is not configured on the incoming interface
filter prerouting fib daddr . iif type != { local, broadcast, multicast } drop
This queries the fib based on the current packets' destination address and the incoming interface.
If the packet is sent to a unicast address that is configured on a different interface, then the packet
will be dropped as such an address would be classified as 'unicast' type.
Without the 'iif' modifier, any address configured on the local machine is 'local', and unicast addresses
not configured on any interface would return the type 'unicast'.
# perform lookup in a specific 'blackhole' table (0xdead, needs ip appropriate ip rule)
filter prerouting meta mark set 0xdead fib daddr . mark type vmap { blackhole : drop, prohibit : jump prohibited, unreachable : drop }
ROUTING EXPRESSIONS
rt [ip | ip6] {classid | nexthop | mtu | ipsec}
A routing expression refers to routing data associated with a packet.
Table 33. Routing expression types
βββββββββββ¬ββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β classid β Routing realm β realm β
βββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β nexthop β Routing nexthop β ipv4_addr/ipv6_addr β
βββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β mtu β TCP maximum segment size of β integer (16 bit) β
β β route β β
βββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β ipsec β route via ipsec tunnel or β boolean β
β β transport β β
βββββββββββ΄ββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββ
Table 34. Routing expression specific types
βββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β Type β Description β
βββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β realm β Routing Realm (32 bit number). Can be β
β β specified numerically or as symbolic name β
β β defined in /etc/iproute2/rt_realms. β
βββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββ
Using routing expressions.
# IP family independent rt expression
filter output rt classid 10
# IP family dependent rt expressions
ip filter output rt nexthop 192.168.0.1
ip6 filter output rt nexthop fd00::1
inet filter output rt ip nexthop 192.168.0.1
inet filter output rt ip6 nexthop fd00::1
# outgoing packet will be encapsulated/encrypted by ipsec
filter output rt ipsec exists
IPSEC EXPRESSIONS
ipsec {in | out} [ spnum NUM ] {reqid | spi}
ipsec {in | out} [ spnum NUM ] {ip | ip6} {saddr | daddr}
An ipsec expression refers to ipsec data associated with a packet.
The in or out keyword needs to be used to specify if the expression should examine inbound or outbound policies. The
in keyword can be used in the prerouting, input and forward hooks. The out keyword applies to forward, output and
postrouting hooks. The optional keyword spnum can be used to match a specific state in a chain, it defaults to 0.
Table 35. Ipsec expression types
βββββββββββ¬βββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β reqid β Request ID β integer (32 bit) β
βββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β spi β Security Parameter Index β integer (32 bit) β
βββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β saddr β Source address of the tunnel β ipv4_addr/ipv6_addr β
βββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β daddr β Destination address of the β ipv4_addr/ipv6_addr β
β β tunnel β β
βββββββββββ΄βββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββ
Note: When using xfrm_interface, this expression is not useable in output hook as the plain packet does not traverse
it with IPsec info attached - use a chain in postrouting hook instead.
NUMGEN EXPRESSION
numgen {inc | random} mod NUM [ offset NUM ]
Create a number generator. The inc or random keywords control its operation mode: In inc mode, the last returned
value is simply incremented. In random mode, a new random number is returned. The value after mod keyword specifies
an upper boundary (read: modulus) which is not reached by returned numbers. The optional offset allows one to
increment the returned value by a fixed offset.
A typical use-case for numgen is load-balancing:
Using numgen expression.
# round-robin between 192.168.10.100 and 192.168.20.200:
add rule nat prerouting dnat to numgen inc mod 2 map \
{ 0 : 192.168.10.100, 1 : 192.168.20.200 }
# probability-based with odd bias using intervals:
add rule nat prerouting dnat to numgen random mod 10 map \
{ 0-2 : 192.168.10.100, 3-9 : 192.168.20.200 }
HASH EXPRESSIONS
jhash {ip saddr | ip6 daddr | tcp dport | udp sport | ether saddr} [. ...] mod NUM [ seed NUM ] [ offset NUM ]
symhash mod NUM [ offset NUM ]
Use a hashing function to generate a number. The functions available are jhash, known as Jenkins Hash, and symhash,
for Symmetric Hash. The jhash requires an expression to determine the parameters of the packet header to apply the
hashing, concatenations are possible as well. The value after mod keyword specifies an upper boundary (read: modulus)
which is not reached by returned numbers. The optional seed is used to specify an init value used as seed in the
hashing function. The optional offset allows one to increment the returned value by a fixed offset.
A typical use-case for jhash and symhash is load-balancing:
Using hash expressions.
# load balance based on source ip between 2 ip addresses:
add rule nat prerouting dnat to jhash ip saddr mod 2 map \
{ 0 : 192.168.10.100, 1 : 192.168.20.200 }
# symmetric load balancing between 2 ip addresses:
add rule nat prerouting dnat to symhash mod 2 map \
{ 0 : 192.168.10.100, 1 : 192.168.20.200 }
PAYLOAD EXPRESSIONS
Payload expressions refer to data from the packetβs payload.
ETHERNET HEADER EXPRESSION
ether {daddr | saddr | type}
Table 36. Ethernet header expression types
βββββββββββ¬ββββββββββββββββββββββββββ¬βββββββββββββ
β Keyword β Description β Type β
βββββββββββΌββββββββββββββββββββββββββΌβββββββββββββ€
β β β β
β daddr β Destination MAC address β ether_addr β
βββββββββββΌββββββββββββββββββββββββββΌβββββββββββββ€
β β β β
β saddr β Source MAC address β ether_addr β
βββββββββββΌββββββββββββββββββββββββββΌβββββββββββββ€
β β β β
β type β EtherType β ether_type β
βββββββββββ΄ββββββββββββββββββββββββββ΄βββββββββββββ
VLAN HEADER EXPRESSION
vlan {id | dei | pcp | type}
The vlan expression is used to match on the vlan header fields. This expression will not work in the ip, ip6 and inet
families, unless the vlan interface is configured with the reorder_hdr off setting. The default is reorder_hdr on
which will automatically remove the vlan tag from the packet. See ip-link(8) for more information. For these families
its easier to match the vlan interface name instead, using the meta iif or meta iifname expression.
Table 37. VLAN header expression
βββββββββββ¬ββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β id β VLAN ID (VID) β integer (12 bit) β
βββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β dei β Drop Eligible Indicator β integer (1 bit) β
βββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β pcp β Priority code point β integer (3 bit) β
βββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β type β EtherType β ether_type β
βββββββββββ΄ββββββββββββββββββββββββββ΄βββββββββββββββββββ
ARP HEADER EXPRESSION
arp {htype | ptype | hlen | plen | operation | saddr { ip | ether } | daddr { ip | ether }
Table 38. ARP header expression
βββββββββββββββ¬ββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β htype β ARP hardware type β integer (16 bit) β
βββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β ptype β EtherType β ether_type β
βββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β hlen β Hardware address len β integer (8 bit) β
βββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β plen β Protocol address len β integer (8 bit) β
βββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β operation β Operation β arp_op β
βββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β saddr ether β Ethernet sender address β ether_addr β
βββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β daddr ether β Ethernet target address β ether_addr β
βββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β saddr ip β IPv4 sender address β ipv4_addr β
βββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β daddr ip β IPv4 target address β ipv4_addr β
βββββββββββββββ΄ββββββββββββββββββββββββββ΄βββββββββββββββββββ
IPV4 HEADER EXPRESSION
ip {version | hdrlength | dscp | ecn | length | id | frag-off | ttl | protocol | checksum | saddr | daddr }
Table 39. IPv4 header expression
βββββββββββββ¬βββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β version β IP header version (4) β integer (4 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β hdrlength β IP header length including β integer (4 bit) FIXME scaling β
β β options β β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β dscp β Differentiated Services Code β dscp β
β β Point β β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β ecn β Explicit Congestion β ecn β
β β Notification β β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β length β Total packet length β integer (16 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β id β IP ID β integer (16 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β frag-off β Fragment offset β integer (16 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β ttl β Time to live β integer (8 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β protocol β Upper layer protocol β inet_proto β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β checksum β IP header checksum β integer (16 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β saddr β Source address β ipv4_addr β
βββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β daddr β Destination address β ipv4_addr β
βββββββββββββ΄βββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββ
Careful with matching on ip length: The Linux kernel might aggregate several packets into one big packet that is
larger than MTU. Moreover, if GRO/GSO maximum size is larger than 65535 (see man ip-link(8), specifically
gro_ipv4_max_size and gso_ipv4_max_size), then ip length might be 0 for such jumbo packets. meta length allows you to
match on the packet length including the IP header size.
ICMP HEADER EXPRESSION
icmp {type | code | checksum | id | sequence | gateway | mtu}
This expression refers to ICMP header fields. When using it in inet, bridge or netdev families, it will cause an
implicit dependency on IPv4 to be created. To match on unusual cases like ICMP over IPv6, one has to add an explicit
meta protocol ip6 match to the rule.
Table 40. ICMP header expression
ββββββββββββ¬ββββββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β type β ICMP type field β icmp_type β
ββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β code β ICMP code field β integer (8 bit) β
ββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β checksum β ICMP checksum field β integer (16 bit) β
ββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β id β ID of echo request/response β integer (16 bit) β
ββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β sequence β sequence number of echo β integer (16 bit) β
β β request/response β β
ββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β gateway β gateway of redirects β integer (32 bit) β
ββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β mtu β MTU of path MTU discovery β integer (16 bit) β
ββββββββββββ΄ββββββββββββββββββββββββββββββ΄βββββββββββββββββββ
IGMP HEADER EXPRESSION
igmp {type | mrt | checksum | group}
This expression refers to IGMP header fields. When using it in inet, bridge or netdev families, it will cause an
implicit dependency on IPv4 to be created. To match on unusual cases like IGMP over IPv6, one has to add an explicit
meta protocol ip6 match to the rule.
Table 41. IGMP header expression
ββββββββββββ¬βββββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌβββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β type β IGMP type field β igmp_type β
ββββββββββββΌβββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β mrt β IGMP maximum response time β integer (8 bit) β
β β field β β
ββββββββββββΌβββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β checksum β IGMP checksum field β integer (16 bit) β
ββββββββββββΌβββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β group β Group address β integer (32 bit) β
ββββββββββββ΄βββββββββββββββββββββββββββββ΄βββββββββββββββββββ
IPV6 HEADER EXPRESSION
ip6 {version | dscp | ecn | flowlabel | length | nexthdr | hoplimit | saddr | daddr}
This expression refers to the ipv6 header fields. Caution when using ip6 nexthdr, the value only refers to the next
header, i.e. ip6 nexthdr tcp will only match if the ipv6 packet does not contain any extension headers. Packets that
are fragmented or e.g. contain a routing extension headers will not be matched. Please use meta l4proto if you wish
to match the real transport header and ignore any additional extension headers instead.
Table 42. IPv6 header expression
βββββββββββββ¬βββββββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β version β IP header version (6) β integer (4 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β dscp β Differentiated Services Code β dscp β
β β Point β β
βββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β ecn β Explicit Congestion β ecn β
β β Notification β β
βββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β flowlabel β Flow label β integer (20 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β length β Payload length β integer (16 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β nexthdr β Nexthdr protocol β inet_proto β
βββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β hoplimit β Hop limit β integer (8 bit) β
βββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β saddr β Source address β ipv6_addr β
βββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β daddr β Destination address β ipv6_addr β
βββββββββββββ΄βββββββββββββββββββββββββββββββ΄βββββββββββββββββββ
Careful with matching on ip6 length: The Linux kernel might aggregate several packets into one big packet that is
larger than MTU. Moreover, if GRO/GSO maximum size is larger than 65535 (see man ip-link(8), specifically
gro_max_size and gso_max_size), then ip6 length might be 0 for such jumbo packets. meta length allows you to match on
the packet length including the IPv6 header size.
Using ip6 header expressions.
# matching if first extension header indicates a fragment
ip6 nexthdr ipv6-frag
ICMPV6 HEADER EXPRESSION
icmpv6 {type | code | checksum | parameter-problem | packet-too-big | id | sequence | max-delay | taddr | daddr}
This expression refers to ICMPv6 header fields. When using it in inet, bridge or netdev families, it will cause an
implicit dependency on IPv6 to be created. To match on unusual cases like ICMPv6 over IPv4, one has to add an
explicit meta protocol ip match to the rule.
Table 43. ICMPv6 header expression
βββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β type β ICMPv6 type field β icmpv6_type β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β code β ICMPv6 code field β integer (8 bit) β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β checksum β ICMPv6 checksum field β integer (16 bit) β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β parameter-problem β pointer to problem β integer (32 bit) β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β packet-too-big β oversized MTU β integer (32 bit) β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β id β ID of echo request/response β integer (16 bit) β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β sequence β sequence number of echo β integer (16 bit) β
β β request/response β β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β max-delay β maximum response delay of MLD β integer (16 bit) β
β β queries β β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β taddr β target address of neighbor β ipv6_addr β
β β solicit/advert, redirect or MLD β β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β daddr β destination address of redirect β ipv6_addr β
βββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββ
TCP HEADER EXPRESSION
tcp {sport | dport | sequence | ackseq | doff | reserved | flags | window | checksum | urgptr}
Table 44. TCP header expression
ββββββββββββ¬βββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β sport β Source port β inet_service β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β dport β Destination port β inet_service β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β sequence β Sequence number β integer (32 bit) β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β ackseq β Acknowledgement number β integer (32 bit) β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β doff β Data offset β integer (4 bit) FIXME scaling β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β reserved β Reserved area β integer (4 bit) β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β flags β TCP flags β tcp_flag β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β window β Window β integer (16 bit) β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β checksum β Checksum β integer (16 bit) β
ββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β urgptr β Urgent pointer β integer (16 bit) β
ββββββββββββ΄βββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββ
UDP HEADER EXPRESSION
udp {sport | dport | length | checksum}
Table 45. UDP header expression
ββββββββββββ¬ββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β sport β Source port β inet_service β
ββββββββββββΌββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β dport β Destination port β inet_service β
ββββββββββββΌββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β length β Total packet length β integer (16 bit) β
ββββββββββββΌββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β checksum β Checksum β integer (16 bit) β
ββββββββββββ΄ββββββββββββββββββββββ΄βββββββββββββββββββ
UDP-LITE HEADER EXPRESSION
udplite {sport | dport | checksum}
Table 46. UDP-Lite header expression
ββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β sport β Source port β inet_service β
ββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β dport β Destination port β inet_service β
ββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β checksum β Checksum β integer (16 bit) β
ββββββββββββ΄βββββββββββββββββββ΄βββββββββββββββββββ
SCTP HEADER EXPRESSION
sctp {sport | dport | vtag | checksum}
sctp chunk CHUNK [ FIELD ]
CHUNK := data | init | init-ack | sack | heartbeat |
heartbeat-ack | abort | shutdown | shutdown-ack | error |
cookie-echo | cookie-ack | ecne | cwr | shutdown-complete
| asconf-ack | forward-tsn | asconf
FIELD := COMMON_FIELD | DATA_FIELD | INIT_FIELD | INIT_ACK_FIELD |
SACK_FIELD | SHUTDOWN_FIELD | ECNE_FIELD | CWR_FIELD |
ASCONF_ACK_FIELD | FORWARD_TSN_FIELD | ASCONF_FIELD
COMMON_FIELD := type | flags | length
DATA_FIELD := tsn | stream | ssn | ppid
INIT_FIELD := init-tag | a-rwnd | num-outbound-streams |
num-inbound-streams | initial-tsn
INIT_ACK_FIELD := INIT_FIELD
SACK_FIELD := cum-tsn-ack | a-rwnd | num-gap-ack-blocks |
num-dup-tsns
SHUTDOWN_FIELD := cum-tsn-ack
ECNE_FIELD := lowest-tsn
CWR_FIELD := lowest-tsn
ASCONF_ACK_FIELD := seqno
FORWARD_TSN_FIELD := new-cum-tsn
ASCONF_FIELD := seqno
Table 47. SCTP header expression
ββββββββββββ¬βββββββββββββββββββββββββ¬βββββββββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββ€
β β β β
β sport β Source port β inet_service β
ββββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββ€
β β β β
β dport β Destination port β inet_service β
ββββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββ€
β β β β
β vtag β Verification Tag β integer (32 bit) β
ββββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββ€
β β β β
β checksum β Checksum β integer (32 bit) β
ββββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββ€
β β β β
β chunk β Search chunk in packet β without FIELD, boolean β
β β β indicating existence β
ββββββββββββ΄βββββββββββββββββββββββββ΄βββββββββββββββββββββββββ
Table 48. SCTP chunk fields
ββββββββββββββββββββββββ¬ββββββββββββββββ¬βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β Name β Width in bits β Chunk β Notes β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β type β 8 β all β not useful, defined by β
β β β β chunk type β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β flags β 8 β all β semantics defined on β
β β β β per-chunk basis β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β length β 16 β all β length of this chunk in β
β β β β bytes excluding padding β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β tsn β 32 β data β transmission sequence β
β β β β number β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β stream β 16 β data β stream identifier β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β ssn β 16 β data β stream sequence number β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β ppid β 32 β data β payload protocol β
β β β β identifier β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β init-tag β 32 β init, init-ack β initiate tag β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β a-rwnd β 32 β init, init-ack, sack β advertised receiver β
β β β β window credit β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β num-outbound-streams β 16 β init, init-ack β number of outbound β
β β β β streams β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β num-inbound-streams β 16 β init, init-ack β number of inbound streams β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β initial-tsn β 32 β init, init-ack β initial transmit sequence β
β β β β number β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β cum-tsn-ack β 32 β sack, shutdown β cumulative transmission β
β β β β sequence number β
β β β β acknowledged β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β num-gap-ack-blocks β 16 β sack β number of Gap Ack Blocks β
β β β β included β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β num-dup-tsns β 16 β sack β number of duplicate β
β β β β transmission sequence β
β β β β numbers received β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β lowest-tsn β 32 β ecne, cwr β lowest transmission β
β β β β sequence number β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β seqno β 32 β asconf-ack, asconf β sequence number β
ββββββββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β β
β new-cum-tsn β 32 β forward-tsn β new cumulative β
β β β β transmission sequence β
β β β β number β
ββββββββββββββββββββββββ΄ββββββββββββββββ΄βββββββββββββββββββββββ΄ββββββββββββββββββββββββββββ
DCCP HEADER EXPRESSION
dccp {sport | dport | type}
Table 49. DCCP header expression
βββββββββββ¬βββββββββββββββββββ¬βββββββββββββββ
β Keyword β Description β Type β
βββββββββββΌβββββββββββββββββββΌβββββββββββββββ€
β β β β
β sport β Source port β inet_service β
βββββββββββΌβββββββββββββββββββΌβββββββββββββββ€
β β β β
β dport β Destination port β inet_service β
βββββββββββΌβββββββββββββββββββΌβββββββββββββββ€
β β β β
β type β Packet type β dccp_pkttype β
βββββββββββ΄βββββββββββββββββββ΄βββββββββββββββ
AUTHENTICATION HEADER EXPRESSION
ah {nexthdr | hdrlength | reserved | spi | sequence}
Table 50. AH header expression
βββββββββββββ¬βββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β nexthdr β Next header protocol β inet_proto β
βββββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β hdrlength β AH Header length β integer (8 bit) β
βββββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β reserved β Reserved area β integer (16 bit) β
βββββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β spi β Security Parameter Index β integer (32 bit) β
βββββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β sequence β Sequence number β integer (32 bit) β
βββββββββββββ΄βββββββββββββββββββββββββββ΄βββββββββββββββββββ
ENCRYPTED SECURITY PAYLOAD HEADER EXPRESSION
esp {spi | sequence}
Table 51. ESP header expression
ββββββββββββ¬βββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β spi β Security Parameter Index β integer (32 bit) β
ββββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β sequence β Sequence number β integer (32 bit) β
ββββββββββββ΄βββββββββββββββββββββββββββ΄βββββββββββββββββββ
IPCOMP HEADER EXPRESSION
comp {nexthdr | flags | cpi}
Table 52. IPComp header expression
βββββββββββ¬ββββββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β nexthdr β Next header protocol β inet_proto β
βββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β flags β Flags β bitmask β
βββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β cpi β compression Parameter Index β integer (16 bit) β
βββββββββββ΄ββββββββββββββββββββββββββββββ΄βββββββββββββββββββ
GRE HEADER EXPRESSION
gre {flags | version | protocol}
gre ip {version | hdrlength | dscp | ecn | length | id | frag-off | ttl | protocol | checksum | saddr | daddr }
gre ip6 {version | dscp | ecn | flowlabel | length | nexthdr | hoplimit | saddr | daddr}
The gre expression is used to match on the gre header fields. This expression also allows one to match on the IPv4 or
IPv6 packet within the gre header.
Table 53. GRE header expression
ββββββββββββ¬βββββββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β flags β checksum, routing, key, β integer (5 bit) β
β β sequence and strict source β β
β β route flags β β
ββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β version β gre version field, 0 for GRE β integer (3 bit) β
β β and 1 for PPTP β β
ββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β protocol β EtherType of encapsulated β integer (16 bit) β
β β packet β β
ββββββββββββ΄βββββββββββββββββββββββββββββββ΄βββββββββββββββββββ
Matching inner IPv4 destination address encapsulated in gre.
netdev filter ingress gre ip daddr 9.9.9.9 counter
GENEVE HEADER EXPRESSION
geneve {vni | flags}
geneve ether {daddr | saddr | type}
geneve vlan {id | dei | pcp | type}
geneve ip {version | hdrlength | dscp | ecn | length | id | frag-off | ttl | protocol | checksum | saddr | daddr }
geneve ip6 {version | dscp | ecn | flowlabel | length | nexthdr | hoplimit | saddr | daddr}
geneve tcp {sport | dport | sequence | ackseq | doff | reserved | flags | window | checksum | urgptr}
geneve udp {sport | dport | length | checksum}
The geneve expression is used to match on the geneve header fields. The geneve header encapsulates a ethernet frame
within a udp packet. This expression requires that you restrict the matching to udp packets (usually at port 6081
according to IANA-assigned ports).
Table 54. GENEVE header expression
ββββββββββββ¬ββββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββΌββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β protocol β EtherType of encapsulated β integer (16 bit) β
β β packet β β
ββββββββββββΌββββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β vni β Virtual Network ID (VNI) β integer (24 bit) β
ββββββββββββ΄ββββββββββββββββββββββββββββ΄βββββββββββββββββββ
Matching inner TCP destination port encapsulated in geneve.
netdev filter ingress udp dport 4789 geneve tcp dport 80 counter
GRETAP HEADER EXPRESSION
gretap {vni | flags}
gretap ether {daddr | saddr | type}
gretap vlan {id | dei | pcp | type}
gretap ip {version | hdrlength | dscp | ecn | length | id | frag-off | ttl | protocol | checksum | saddr | daddr }
gretap ip6 {version | dscp | ecn | flowlabel | length | nexthdr | hoplimit | saddr | daddr}
gretap tcp {sport | dport | sequence | ackseq | doff | reserved | flags | window | checksum | urgptr}
gretap udp {sport | dport | length | checksum}
The gretap expression is used to match on the encapsulated ethernet frame within the gre header. Use the gre
expression to match on the gre header fields.
Matching inner TCP destination port encapsulated in gretap.
netdev filter ingress gretap tcp dport 80 counter
VXLAN HEADER EXPRESSION
vxlan {vni | flags}
vxlan ether {daddr | saddr | type}
vxlan vlan {id | dei | pcp | type}
vxlan ip {version | hdrlength | dscp | ecn | length | id | frag-off | ttl | protocol | checksum | saddr | daddr }
vxlan ip6 {version | dscp | ecn | flowlabel | length | nexthdr | hoplimit | saddr | daddr}
vxlan tcp {sport | dport | sequence | ackseq | doff | reserved | flags | window | checksum | urgptr}
vxlan udp {sport | dport | length | checksum}
The vxlan expression is used to match on the vxlan header fields. The vxlan header encapsulates a ethernet frame
within a udp packet. This expression requires that you restrict the matching to udp packets (usually at port 4789
according to IANA-assigned ports).
Table 55. VXLAN header expression
βββββββββββ¬βββββββββββββββββββββββββββ¬βββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β flags β vxlan flags β integer (8 bit) β
βββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββ€
β β β β
β vni β Virtual Network ID (VNI) β integer (24 bit) β
βββββββββββ΄βββββββββββββββββββββββββββ΄βββββββββββββββββββ
Matching inner TCP destination port encapsulated in vxlan.
netdev filter ingress udp dport 4789 vxlan tcp dport 80 counter
RAW PAYLOAD EXPRESSION
@base,offset,length
The raw payload expression instructs to load length bits starting at offset bits. Bit 0 refers to the very first bit
β in the C programming language, this corresponds to the topmost bit, i.e. 0x80 in case of an octet. They are useful
to match headers that do not have a human-readable template expression yet. Note that nft will not add dependencies
for Raw payload expressions. If you e.g. want to match protocol fields of a transport header with protocol number 5,
you need to manually exclude packets that have a different transport header, for instance by using meta l4proto 5
before the raw expression.
Table 56. Supported payload protocol bases
ββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β Base β Description β
ββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β ll β Link layer, for example the Ethernet β
β β header β
ββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β nh β Network header, for example IPv4 or IPv6 β
ββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β th β Transport Header, for example TCP β
ββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β ih β Inner Header / Payload, i.e. after the L4 β
β β transport level header β
ββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββ
Matching destination port of both UDP and TCP.
inet filter input meta l4proto {tcp, udp} @th,16,16 { 53, 80 }
The above can also be written as
inet filter input meta l4proto {tcp, udp} th dport { 53, 80 }
it is more convenient, but like the raw expression notation no dependencies are created or checked. It is the users
responsibility to restrict matching to those header types that have a notion of ports. Otherwise, rules using raw
expressions will errnously match unrelated packets, e.g. mis-interpreting ESP packets SPI field as a port.
Rewrite arp packet target hardware address if target protocol address matches a given address.
input meta iifname enp2s0 arp ptype 0x0800 arp htype 1 arp hlen 6 arp plen 4 @nh,192,32 0xc0a88f10 @nh,144,48 set 0x112233445566 accept
EXTENSION HEADER EXPRESSIONS
Extension header expressions refer to data from variable-sized protocol headers, such as IPv6 extension headers, TCP
options and IPv4 options.
nftables currently supports matching (finding) a given ipv6 extension header, TCP option or IPv4 option.
hbh {nexthdr | hdrlength}
frag {nexthdr | frag-off | more-fragments | id}
rt {nexthdr | hdrlength | type | seg-left}
dst {nexthdr | hdrlength}
mh {nexthdr | hdrlength | checksum | type}
srh {flags | tag | sid | seg-left}
tcp option {eol | nop | maxseg | window | sack-perm | sack | sack0 | sack1 | sack2 | sack3 | timestamp | mptcp } tcp_option_field
ip option { lsrr | ra | rr | ssrr } ip_option_field
The following syntaxes are valid only in a relational expression with boolean type on right-hand side for checking
header existence only:
exthdr {hbh | frag | rt | dst | mh}
tcp option {eol | nop | maxseg | window | sack-perm | sack | sack0 | sack1 | sack2 | sack3 | timestamp | mptcp }
ip option { lsrr | ra | rr | ssrr }
dccp option dccp_option_type
Table 57. IPv6 extension headers
βββββββββββ¬βββββββββββββββββββββββββ
β Keyword β Description β
βββββββββββΌβββββββββββββββββββββββββ€
β β β
β hbh β Hop by Hop β
βββββββββββΌβββββββββββββββββββββββββ€
β β β
β rt β Routing Header β
βββββββββββΌβββββββββββββββββββββββββ€
β β β
β frag β Fragmentation header β
βββββββββββΌβββββββββββββββββββββββββ€
β β β
β dst β dst options β
βββββββββββΌβββββββββββββββββββββββββ€
β β β
β mh β Mobility Header β
βββββββββββΌβββββββββββββββββββββββββ€
β β β
β srh β Segment Routing Header β
βββββββββββ΄βββββββββββββββββββββββββ
Table 58. TCP Options
βββββββββββββ¬ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββ
β Keyword β Description β TCP option fields β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β eol β End if option list β - β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β nop β 1 Byte TCP Nop padding option β - β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β maxseg β TCP Maximum Segment Size β length, size β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β window β TCP Window Scaling β length, count β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β sack-perm β TCP SACK permitted β length β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β sack β TCP Selective Acknowledgement β length, left, right β
β β (alias of block 0) β β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β sack0 β TCP Selective Acknowledgement β length, left, right β
β β (block 0) β β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β sack1 β TCP Selective Acknowledgement β length, left, right β
β β (block 1) β β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β sack2 β TCP Selective Acknowledgement β length, left, right β
β β (block 2) β β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β sack3 β TCP Selective Acknowledgement β length, left, right β
β β (block 3) β β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β timestamp β TCP Timestamps β length, tsval, tsecr β
βββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββ€
β β β β
β mptcp β Multipath TCP β subtype β
βββββββββββββ΄ββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββ
Data types can be queried with nft describe tcp option keyword [ fieldname ].
TCP option matching also supports raw expression syntax to access arbitrary options:
tcp option
tcp option @number,offset,length
Table 59. IP Options
βββββββββββ¬ββββββββββββββββββββββ¬ββββββββββββββββββββ
β Keyword β Description β IP option fields β
βββββββββββΌββββββββββββββββββββββΌββββββββββββββββββββ€
β β β β
β lsrr β Loose Source Route β length, ptr, addr β
βββββββββββΌββββββββββββββββββββββΌββββββββββββββββββββ€
β β β β
β ra β Router Alert β length, value β
βββββββββββΌββββββββββββββββββββββΌββββββββββββββββββββ€
β β β β
β rr β Record Route β length, ptr, addr β
βββββββββββΌββββββββββββββββββββββΌββββββββββββββββββββ€
β β β β
β ssrr β Strict Source Route β length, ptr, addr β
βββββββββββ΄ββββββββββββββββββββββ΄ββββββββββββββββββββ
finding TCP options.
filter input tcp option sack-perm exists counter
matching TCP options.
filter input tcp option maxseg size lt 536
matching IPv6 exthdr.
ip6 filter input frag more-fragments 1 counter
finding IP option.
filter input ip option lsrr exists counter
finding DCCP option.
filter input dccp option 40 exists counter
CONNTRACK EXPRESSIONS
Conntrack expressions refer to meta data of the connection tracking entry associated with a packet.
There are three types of conntrack expressions. Some conntrack expressions require the flow direction before the
conntrack key, others must be used directly because they are direction agnostic. The packets, bytes and avgpkt
keywords can be used with or without a direction. If the direction is omitted, the sum of the original and the reply
direction is returned. The same is true for the zone, if a direction is given, the zone is only matched if the zone
id is tied to the given direction.
ct {state | direction | status | mark | expiration | helper | label | count | id}
ct [original | reply] {l3proto | protocol | bytes | packets | avgpkt | zone}
ct {original | reply} {proto-src | proto-dst}
ct {original | reply} {ip | ip6} {saddr | daddr}
The conntrack-specific types in this table are described in the sub-section CONNTRACK TYPES above.
Table 60. Conntrack expressions
ββββββββββββββ¬ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ
β Keyword β Description β Type β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β state β State of the connection β ct_state β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β direction β Direction of the packet β ct_dir β
β β relative to the connection β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β status β Status of the connection β ct_status β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β mark β Connection mark β mark β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β expiration β Connection expiration time β time β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β helper β Helper associated with the β string β
β β connection β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β label β Connection tracking label bit β ct_label β
β β or symbolic name defined in β β
β β connlabel.conf in the nftables β β
β β include path β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β l3proto β Layer 3 protocol of the β nf_proto β
β β connection β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β saddr β Source address of the β ipv4_addr/ipv6_addr β
β β connection for the given β β
β β direction β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β daddr β Destination address of the β ipv4_addr/ipv6_addr β
β β connection for the given β β
β β direction β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β protocol β Layer 4 protocol of the β inet_proto β
β β connection for the given β β
β β direction β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β proto-src β Layer 4 protocol source for the β integer (16 bit) β
β β given direction β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β proto-dst β Layer 4 protocol destination β integer (16 bit) β
β β for the given direction β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β packets β packet count seen in the given β integer (64 bit) β
β β direction or sum of original β β
β β and reply β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β bytes β byte count seen, see β integer (64 bit) β
β β description for packets keyword β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β avgpkt β average bytes per packet, see β integer (64 bit) β
β β description for packets keyword β β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β zone β conntrack zone β integer (16 bit) β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β count β number of current connections β integer (32 bit) β
ββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€
β β β β
β id β Connection id β ct_id β
ββββββββββββββ΄ββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββ
restrict the number of parallel connections to a server.
nft add set filter ssh_flood '{ type ipv4_addr; flags dynamic; }'
nft add rule filter input tcp dport 22 add @ssh_flood '{ ip saddr ct count over 2 }' reject
STATEMENTS
Statements represent actions to be performed. They can alter control flow (return, jump to a different chain, accept
or drop the packet) or can perform actions, such as logging, rejecting a packet, etc.
Statements exist in two kinds. Terminal statements unconditionally terminate evaluation of the current rule,
non-terminal statements either only conditionally or never terminate evaluation of the current rule, in other words,
they are passive from the rule evaluation perspective. There can be an arbitrary amount of non-terminal statements in
a rule, but only a single terminal statement as the final statement.
VERDICT STATEMENTS
The verdict statements alter control flow in the ruleset and issue policy decisions for packets.
{accept | drop | continue | return}
{jump | goto} CHAIN
CHAIN := chain_name | { statement ... }
accept and drop are absolute verdicts β they terminate chain evaluation, as if the packet would have reached the end
of the base chain with the equivalent policy decision set. See the section called βOVERALL EVALUATION OF THE RULESETβ
for more details.
accept Terminate evaluation early. Evaluation
continues in the next base chain of
higher or possibly equal priority from
the same hook or in the first base chain
of a later hook, if any. This means the
packet can still be dropped in another
base chain as well as any chain called
from it. For example, an accept verdict
in a chain of the forward hook still
allows one to drop the packet in another
forward hook base chain (or a chain
called from it) that has a higher
priority number or in a chain attached to
the postrouting hook.
drop Immediately drop the packet and terminate
ruleset evaluation. No further evaluation
takes place. It is not possible to
override a drop verdict.
jump CHAIN Store the current position in the call
stack of chains and continue evaluation
at the first rule of CHAIN. When the end
of CHAIN is reached, an implicit return
verdict is issued. When an absolute
verdict is issued (respectively implied
by a verdict-like statement) in CHAIN,
evaluation terminates as described above.
goto CHAIN Equal to jump except that the current
position is not stored in the call stack
of chains.
return End evaluation of the current chain, pop
the most recently added position from the
call stack of chains and continue
evaluation after that position. When
thereβs no position to pop (which is the
case when the current chain is either the
base chain or a regular chain that was
reached solely via goto verdicts) end
evaluation of the current base chain (and
any regular chains called from it) using
the base chainβs policy as implicit
verdict.
continue Continue evaluation with the next rule.
This is the default behaviour in case a
rule issues no verdict.
An alternative to specifying the name of an existing, regular chain in CHAIN is to specify an anonymous chain ad-hoc.
Like with anonymous sets, it canβt be referenced from another rule and will be removed along with the rule containing
it.
All the above applies analogously to statements that imply a verdict: redirect, dnat, snat and masquerade internally
issue an accept verdict at the end of their respective actions. reject and synproxy internally issue a drop verdict
at the end of their respective actions. These statements thus behave like their implied verdicts, but with side
effects.
Using verdict statements.
# process packets from eth0 and the internal network in from_lan
# chain, drop all packets from eth0 with different source addresses.
filter input iif eth0 ip saddr 192.168.0.0/24 jump from_lan
filter input iif eth0 drop
# jump and goto statements support anonymous chain creation
filter input iif eth0 jump { ip saddr 192.168.0.0/24 drop ; udp dport domain drop; }
PAYLOAD STATEMENT
payload_expression set value
The payload statement alters packet content. It can be used for example to set ip DSCP (diffserv) header field or
ipv6 flow labels.
route some packets instead of bridging.
# redirect tcp:http from 192.160.0.0/16 to local machine for routing instead of bridging
# assumes 00:11:22:33:44:55 is local MAC address.
bridge input meta iif eth0 ip saddr 192.168.0.0/16 tcp dport 80 meta pkttype set host ether daddr set 00:11:22:33:44:55
Set IPv4 DSCP header field.
ip forward ip dscp set 42
EXTENSION HEADER STATEMENT
extension_header_expression set value
The extension header statement alters packet content in variable-sized headers. This can currently be used to alter
the TCP Maximum segment size of packets, similar to the TCPMSS target in iptables.
change tcp mss.
tcp flags syn tcp option maxseg size set 1360
# set a size based on route information:
tcp flags syn tcp option maxseg size set rt mtu
You can also remove tcp options via reset keyword.
remove tcp option.
tcp flags syn reset tcp option sack-perm
LOG STATEMENT
log [prefix quoted_string] [level syslog-level] [flags log-flags]
log group nflog_group [prefix quoted_string] [queue-threshold value] [snaplen size]
log level audit
The log statement enables logging of matching packets. When this statement is used from a rule, the Linux kernel will
print some information on all matching packets, such as header fields, via the kernel log (where it can be read with
dmesg(1) or read in the syslog).
In the second form of invocation (if nflog_group is specified), the Linux kernel will pass the packet to
nfnetlink_log which will send the log through a netlink socket to the specified group. One userspace process may
subscribe to the group to receive the logs, see man(8) ulogd for the Netfilter userspace log daemon and
libnetfilter_log documentation for details in case you would like to develop a custom program to digest your logs.
In the third form of invocation (if level audit is specified), the Linux kernel writes a message into the audit
buffer suitably formatted for reading with auditd. Therefore no further formatting options (such as prefix or flags)
are allowed in this mode.
This is a non-terminating statement, so the rule evaluation continues after the packet is logged.
Table 61. log statement options
βββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β Keyword β Description β Type β
βββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β β β β
β prefix β Log message prefix β quoted string β
βββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β β β β
β level β Syslog level of logging β string: emerg, alert, crit, β
β β β err, warn [default], notice, β
β β β info, debug, audit β
βββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β β β β
β group β NFLOG group to send messages to β unsigned integer (16 bit) β
βββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β β β β
β snaplen β Length of packet payload to β unsigned integer (32 bit) β
β β include in netlink message β β
βββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β β β β
β queue-threshold β Number of packets to queue β unsigned integer (32 bit) β
β β inside the kernel before β β
β β sending them to userspace β β
βββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββ
Table 62. log-flags
ββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ
β Flag β Description β
ββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β tcp sequence β Log TCP sequence numbers. β
ββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β tcp options β Log options from the TCP packet header. β
ββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β ip options β Log options from the IP/IPv6 packet β
β β header. β
ββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β skuid β Log the userid of the process which β
β β generated the packet. β
ββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β ether β Decode MAC addresses and protocol. β
ββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β all β Enable all log flags listed above. β
ββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ
Using log statement.
# log the UID which generated the packet and ip options
ip filter output log flags skuid flags ip options
# log the tcp sequence numbers and tcp options from the TCP packet
ip filter output log flags tcp sequence,options
# enable all supported log flags
ip6 filter output log flags all
REJECT STATEMENT
reject [ with REJECT_WITH ]
REJECT_WITH := icmp icmp_reject_code |
icmpv6 icmpv6_reject_code |
icmpx icmpx_reject_code |
tcp reset
A reject statement tries to send back an error packet in response to the matched packet and then internally issues a
drop verdict. Itβs thus a terminating statement with all consequences of the latter (see the section called βOVERALL
EVALUATION OF THE RULESETβ respectively the section called βVERDICT STATEMENTSβ). This statement is only valid in
base chains using the prerouting, input, forward or output hooks, and regular chains which are only called from those
chains.
Table 63. Keywords may be used to reject when specifying the ICMP code
ββββββββββββββββββββ¬ββββββββ
β Keyword β Value β
ββββββββββββββββββββΌββββββββ€
β β β
β net-unreachable β 0 β
ββββββββββββββββββββΌββββββββ€
β β β
β host-unreachable β 1 β
ββββββββββββββββββββΌββββββββ€
β β β
β prot-unreachable β 2 β
ββββββββββββββββββββΌββββββββ€
β β β
β port-unreachable β 3 β
ββββββββββββββββββββΌββββββββ€
β β β
β frag-needed β 4 β
ββββββββββββββββββββΌββββββββ€
β β β
β net-prohibited β 9 β
ββββββββββββββββββββΌββββββββ€
β β β
β host-prohibited β 10 β
ββββββββββββββββββββΌββββββββ€
β β β
β admin-prohibited β 13 β
ββββββββββββββββββββ΄ββββββββ
Table 64. keywords may be used to reject when specifying the ICMPv6 code
ββββββββββββββββββββ¬ββββββββ
β Keyword β Value β
ββββββββββββββββββββΌββββββββ€
β β β
β no-route β 0 β
ββββββββββββββββββββΌββββββββ€
β β β
β admin-prohibited β 1 β
ββββββββββββββββββββΌββββββββ€
β β β
β addr-unreachable β 3 β
ββββββββββββββββββββΌββββββββ€
β β β
β port-unreachable β 4 β
ββββββββββββββββββββΌββββββββ€
β β β
β policy-fail β 5 β
ββββββββββββββββββββΌββββββββ€
β β β
β reject-route β 6 β
ββββββββββββββββββββ΄ββββββββ
The ICMPvX Code type abstraction is a set of values which overlap between ICMP and ICMPv6 Code types to be used from
the inet family.
Table 65. keywords may be used when specifying the ICMPvX code
ββββββββββββββββββββ¬ββββββββ
β Keyword β Value β
ββββββββββββββββββββΌββββββββ€
β β β
β no-route β 0 β
ββββββββββββββββββββΌββββββββ€
β β β
β port-unreachable β 1 β
ββββββββββββββββββββΌββββββββ€
β β β
β host-unreachable β 2 β
ββββββββββββββββββββΌββββββββ€
β β β
β admin-prohibited β 3 β
ββββββββββββββββββββ΄ββββββββ
The common default ICMP code to reject is port-unreachable.
Note that in bridge family, reject statement is only allowed in base chains which hook into input or prerouting.
COUNTER STATEMENT
A counter statement sets the hit count of packets along with the number of bytes.
counter packets number bytes number
counter { packets number | bytes number }
CONNTRACK STATEMENT
The conntrack statement can be used to set the conntrack mark and conntrack labels.
ct {mark | event | label | zone} set value
The ct statement sets meta data associated with a connection. The zone id has to be assigned before a conntrack
lookup takes place, i.e. this has to be done in prerouting and possibly output (if locally generated packets need to
be placed in a distinct zone), with a hook priority of raw (-300).
Unlike iptables, where the helper assignment happens in the raw table, the helper needs to be assigned after a
conntrack entry has been found, i.e. it will not work when used with hook priorities equal or before -200.
Table 66. Conntrack statement types
βββββββββββ¬ββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β Keyword β Description β Value β
βββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β event β conntrack event bits β bitmask, integer (32 bit) β
βββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β helper β name of ct helper object to β quoted string β
β β assign to the connection β β
βββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β mark β Connection tracking mark β mark β
βββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β label β Connection tracking label β label β
βββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β zone β conntrack zone β integer (16 bit) β
βββββββββββ΄ββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββ
save packet nfmark in conntrack.
ct mark set meta mark
set zone mapped via interface.
table inet raw {
chain prerouting {
type filter hook prerouting priority raw;
ct zone set iif map { "eth1" : 1, "veth1" : 2 }
}
chain output {
type filter hook output priority raw;
ct zone set oif map { "eth1" : 1, "veth1" : 2 }
}
}
restrict events reported by ctnetlink.
ct event set new,related,destroy
NOTRACK STATEMENT
The notrack statement allows one to disable connection tracking for certain packets.
notrack
Note that for this statement to be effective, it has to be applied to packets before a conntrack lookup happens.
Therefore, it needs to sit in a chain with either prerouting or output hook and a hook priority of -300 (raw) or
less.
See SYNPROXY STATEMENT for an example usage.
META STATEMENT
A meta statement sets the value of a meta expression. The existing meta fields are: priority, mark, pkttype, nftrace.
meta {mark | priority | pkttype | nftrace | broute} set value
A meta statement sets meta data associated with a packet.
Table 67. Meta statement types
ββββββββββββ¬ββββββββββββββββββββββββββββββββββ¬ββββββββββββ
β Keyword β Description β Value β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββ€
β β β β
β priority β TC packet priority β tc_handle β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββ€
β β β β
β mark β Packet mark β mark β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββ€
β β β β
β pkttype β packet type β pkt_type β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββ€
β β β β
β nftrace β ruleset packet tracing on/off. β 0, 1 β
β β Use monitor trace command to β β
β β watch traces β β
ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββ€
β β β β
β broute β broute on/off. packets are β 0, 1 β
β β routed instead of being bridged β β
ββββββββββββ΄ββββββββββββββββββββββββββββββββββ΄ββββββββββββ
LIMIT STATEMENT
limit rate [over] packet_number / TIME_UNIT [burst packet_number packets]
limit rate [over] byte_number BYTE_UNIT / TIME_UNIT [burst byte_number BYTE_UNIT]
TIME_UNIT := second | minute | hour | day
BYTE_UNIT := bytes | kbytes | mbytes
A limit statement matches at a limited rate using a token bucket filter. A rule using this statement will match until
this limit is reached. It can be used in combination with the log statement to give limited logging. The optional
over keyword makes it match over the specified rate.
The burst value influences the bucket size, i.e. jitter tolerance. With packet-based limit, the bucket holds exactly
burst packets, by default five. If you specify packet burst, it must be a non-zero value. With byte-based limit, the
bucketβs minimum size is the given rateβs byte value and the burst value adds to that, by default zero bytes.
Table 68. limit statement values
βββββββββββββββββ¬ββββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β Value β Description β Type β
βββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β packet_number β Number of packets β unsigned integer (32 bit) β
βββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β byte_number β Number of bytes β unsigned integer (32 bit) β
βββββββββββββββββ΄ββββββββββββββββββββ΄ββββββββββββββββββββββββββββ
NAT STATEMENTS
snat [[ip | ip6] [ prefix ] to] TARGET_SPEC [FLAGS]
dnat [[ip | ip6] [ prefix ] to] TARGET_SPEC [FLAGS]
masquerade [to :PORT_SPEC] [FLAGS]
redirect [to :PORT_SPEC] [FLAGS]
TARGET_SPEC := ADDR_SPEC | [ADDR_SPEC] :PORT_SPEC
ADDR_SPEC := address | address - address
PORT_SPEC := port | port - port
FLAGS := FLAG [, FLAGS]
FLAG := persistent | random | fully-random
The nat statements are only valid from nat chain types.
The snat and masquerade statements specify that the source address/port of the packet should be modified. While snat
is only valid in the postrouting and input chains, masquerade makes sense only in postrouting. The dnat and redirect
statements are only valid in the prerouting and output chains, they specify that the destination address/port of the
packet should be modified. You can use non-base chains which are called from base chains of nat chain type too. All
future packets in this connection will also be mangled, and rules should cease being examined.
The masquerade statement is a special form of snat which always uses the outgoing interfaceβs IP address to translate
to. It is particularly useful on gateways with dynamic (public) IP addresses.
The redirect statement is a special form of dnat which always translates the destination address to the local hostβs
one. It comes in handy to intercept traffic passing a router and feeding it to a locally running daemon, e.g. when
building a transparent proxy or application-layer gateway.
For TARGET_SPEC, one may specify addresses, ports, or both. If no address or no port is specified, the respective
packet header field remains unchanged.
When used in the inet family (available with kernel 5.2), the dnat and snat statements require the use of the ip and
ip6 keyword in case an address is provided, see the examples below.
Before kernel 4.18 nat statements require both prerouting and postrouting base chains to be present since otherwise
packets on the return path wonβt be seen by netfilter and therefore no reverse translation will take place.
The optional prefix keyword allows one to map n source addresses to n destination addresses. See Advanced NAT
examples below.
If the address for dnat is an IPv4 loopback address (i.e. 127.0.0.0/8) the "net.ipv4.conf.*.route_localnet" sysctl
for the input interface needs to be set to 1. Otherwise packets will be dropped by the routing code as "martians".
Table 69. NAT statement values
ββββββββββββββ¬βββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β Expression β Description β Type β
ββββββββββββββΌβββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β address β Specifies that the β ipv4_addr, ipv6_addr, e.g. β
β β source/destination address of β abcd::1234, or you can use a β
β β the packet should be modified. β mapping, e.g. meta mark map { β
β β You may specify a mapping to β 10 : 192.168.1.2, 20 : β
β β relate a list of tuples β 192.168.1.3 } β
β β composed of arbitrary β β
β β expression key with address β β
β β value. β β
ββββββββββββββΌβββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β β β β
β port β Specifies that the β port number (16 bit) β
β β source/destination port of the β β
β β packet should be modified. β β
ββββββββββββββ΄βββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββ
Table 70. NAT statement flags
ββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β Flag β Description β
ββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β persistent β Gives a client the same β
β β source-/destination-address for each β
β β connection. β
ββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β random β In kernel 5.0 and newer this is the same β
β β as fully-random. In earlier kernels the β
β β port mapping will be randomized using a β
β β seeded MD5 hash mix using source and β
β β destination address and destination port. β
ββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β fully-random β If used then port mapping is generated β
β β based on a 32-bit pseudo-random β
β β algorithm. β
ββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββ
Using NAT statements.
# create a suitable table/chain setup for all further examples
add table nat
add chain nat prerouting { type nat hook prerouting priority dstnat; }
add chain nat postrouting { type nat hook postrouting priority srcnat; }
# translate source addresses of all packets leaving via eth0 to address 1.2.3.4
add rule nat postrouting oif eth0 snat to 1.2.3.4
# redirect all traffic entering via eth0 to destination address 192.168.1.120
add rule nat prerouting iif eth0 dnat to 192.168.1.120
# translate source addresses of all packets leaving via eth0 to whatever
# locally generated packets would use as source to reach the same destination
add rule nat postrouting oif eth0 masquerade
# redirect incoming TCP traffic for port 22 to port 2222
add rule nat prerouting tcp dport 22 redirect to :2222
# inet family:
# handle ip dnat:
add rule inet nat prerouting dnat ip to 10.0.2.99
# handle ip6 dnat:
add rule inet nat prerouting dnat ip6 to fe80::dead
# this masquerades both ipv4 and ipv6:
add rule inet nat postrouting meta oif ppp0 masquerade
Advanced NAT examples.
# map prefixes in one network to that of another, e.g. 10.141.11.4 is mangled to 192.168.2.4,
# 10.141.11.5 is mangled to 192.168.2.5 and so on.
add rule nat postrouting snat ip prefix to ip saddr map { 10.141.11.0/24 : 192.168.2.0/24 }
# map a source address, source port combination to a pool of destination addresses and ports:
add rule nat postrouting dnat to ip saddr . tcp dport map { 192.168.1.2 . 80 : 10.141.10.2-10.141.10.5 . 8888-8999 }
# The above example generates the following NAT expression:
#
# [ nat dnat ip addr_min reg 1 addr_max reg 10 proto_min reg 9 proto_max reg 11 ]
#
# which expects to obtain the following tuple:
# IP address (min), source port (min), IP address (max), source port (max)
# to be obtained from the map. The given addresses and ports are inclusive.
# This also works with named maps and in combination with both concatenations and ranges:
table ip nat {
map ipportmap {
typeof ip saddr : interval ip daddr . tcp dport
flags interval
elements = { 192.168.1.2 : 10.141.10.1-10.141.10.3 . 8888-8999, 192.168.2.0/24 : 10.141.11.5-10.141.11.20 . 8888-8999 }
}
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
ip protocol tcp dnat ip to ip saddr map @ipportmap
}
}
@ipportmap maps network prefixes to a range of hosts and ports.
The new destination is taken from the range provided by the map element.
Same for the destination port.
Note the use of the "interval" keyword in the typeof description.
This is required so nftables knows that it has to ask for twice the
amount of storage for each key-value pair in the map.
": ipv4_addr . inet_service" would allow associating one address and one port
with each key. But for this case, for each key, two addresses and two ports
(The minimum and maximum values for both) have to be stored.
TPROXY STATEMENT
Tproxy redirects the packet to a local socket without changing the packet header in any way. If any of the arguments
is missing the data of the incoming packet is used as parameter. Tproxy matching requires another rule that ensures
the presence of transport protocol header is specified.
tproxy to address:port
tproxy to {address | :port}
This syntax can be used in ip/ip6 tables where network layer protocol is obvious. Either IP address or port can be
specified, but at least one of them is necessary.
tproxy {ip | ip6} to address[:port]
tproxy to :port
This syntax can be used in inet tables. The ip/ip6 parameter defines the family the rule will match. The address
parameter must be of this family. When only port is defined, the address family should not be specified. In this case
the rule will match for both families.
Table 71. tproxy attributes
βββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
β Name β Description β
βββββββββββΌβββββββββββββββββββββββββββββββββββββββ€
β β β
β address β IP address the listening socket with β
β β IP_TRANSPARENT option is bound to. β
βββββββββββΌβββββββββββββββββββββββββββββββββββββββ€
β β β
β port β Port the listening socket with β
β β IP_TRANSPARENT option is bound to. β
βββββββββββ΄βββββββββββββββββββββββββββββββββββββββ
Example ruleset for tproxy statement.
table ip x {
chain y {
type filter hook prerouting priority mangle; policy accept;
tcp dport ntp tproxy to 1.1.1.1 accept
udp dport ssh tproxy to :2222 accept
}
}
table ip6 x {
chain y {
type filter hook prerouting priority mangle; policy accept;
tcp dport ntp tproxy to [dead::beef] accept
udp dport ssh tproxy to :2222 accept
}
}
table inet x {
chain y {
type filter hook prerouting priority mangle; policy accept;
tcp dport 321 tproxy to :22 accept
tcp dport 99 tproxy ip to 1.1.1.1:999 accept
udp dport 155 tproxy ip6 to [dead::beef]:smux accept
}
}
Note that the tproxy statement is non-terminal to allow post-processing of packets. This allows packets to be logged
for debugging as well as updating the mark to ensure that packets are delivered locally through policy routing rules.
Example ruleset for tproxy statement with logging and meta mark.
table inet x {
chain y {
type filter hook prerouting priority mangle; policy accept;
udp dport 9999 goto {
tproxy to :1234 log prefix "packet tproxied: " meta mark set 1 accept
log prefix "no socket on port 1234 or not transparent?: " drop
}
}
}
As packet headers are unchanged, packets might be forwarded instead of delivered locally. As mentioned above, this
can be avoided by adding policy routing rules and the packet mark.
Example policy routing rules for local redirection.
ip rule add fwmark 1 lookup 100
ip route add local 0.0.0.0/0 dev lo table 100
This is a change in behavior compared to the legacy iptables TPROXY target which is terminal. To terminate the packet
processing after the tproxy statement, remember to issue a verdict as in the example above.
SYNPROXY STATEMENT
This statement will process TCP three-way-handshake parallel in netfilter context to protect either local or backend
system. This statement requires connection tracking because sequence numbers need to be translated.
synproxy [mss mss_value] [wscale wscale_value] [SYNPROXY_FLAGS]
Table 72. synproxy statement attributes
ββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ
β Name β Description β
ββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β mss β Maximum segment size announced to β
β β clients. This must match the backend. β
ββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β β β
β wscale β Window scale announced to clients. This β
β β must match the backend. β
ββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ
Table 73. synproxy statement flags
βββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββ
β Flag β Description β
βββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββ€
β β β
β sack-perm β Pass client selective acknowledgement β
β β option to backend (will be disabled if β
β β not present). β
βββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββ€
β β β
β timestamp β Pass client timestamp option to backend β
β β (will be disabled if not present, also β
β β needed for selective acknowledgement and β
β β window scaling). β
βββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββ
Example ruleset for synproxy statement.
Determine tcp options used by backend, from an external system
tcpdump -pni eth0 -c 1 'tcp[tcpflags] == (tcp-syn|tcp-ack) && port 80' &
telnet 192.0.2.42 80
18:57:24.693307 IP 192.0.2.42.80 > 192.0.2.43.48757:
Flags [S.], seq 360414582, ack 788841994, win 14480,
options [mss 1460,sackOK,
TS val 1409056151 ecr 9690221,
nop,wscale 9],
length 0
Switch tcp_loose mode off, so conntrack will mark out-of-flow packets as state INVALID.
echo 0 > /proc/sys/net/netfilter/nf_conntrack_tcp_loose
Make SYN packets untracked.
table ip x {
chain y {
type filter hook prerouting priority raw; policy accept;
tcp flags syn notrack
}
}
Catch UNTRACKED (SYN packets) and INVALID (3WHS ACK packets) states and send
them to SYNPROXY. This rule will respond to SYN packets with SYN+ACK
syncookies, create ESTABLISHED for valid client response (3WHS ACK packets) and
drop incorrect cookies. Flags combinations not expected during 3WHS will not
match and continue (e.g. SYN+FIN, SYN+ACK). Finally, drop invalid packets, this
will be out-of-flow packets that were not matched by SYNPROXY.
table ip x {
chain z {
type filter hook input priority filter; policy accept;
ct state invalid, untracked synproxy mss 1460 wscale 9 timestamp sack-perm
ct state invalid drop
}
}
FLOW STATEMENT
A flow statement allows us to select what flows you want to accelerate forwarding through layer 3 network stack
bypass. You have to specify the flowtable name where you want to offload this flow.
flow add @flowtable
QUEUE STATEMENT
This statement passes the packet to userspace using the nfnetlink_queue handler. The packet is put into the queue
identified by its 16-bit queue number. Userspace can inspect and optionally modify the packet if desired. Userspace
must provide a drop or accept verdict. In case of accept, processing resumes with the next base chain hook, not the
rule following the queue verdict. See libnetfilter_queue documentation for details.
queue [flags QUEUE_FLAGS] [to queue_number]
queue [flags QUEUE_FLAGS] [to queue_number_from - queue_number_to]
queue [flags QUEUE_FLAGS] [to QUEUE_EXPRESSION ]
QUEUE_FLAGS := QUEUE_FLAG [, QUEUE_FLAGS]
QUEUE_FLAG := bypass | fanout
QUEUE_EXPRESSION := numgen | hash | symhash | MAP STATEMENT
QUEUE_EXPRESSION can be used to compute a queue number at run-time with the hash or numgen expressions. It also
allows one to use the map statement to assign fixed queue numbers based on external inputs such as the source ip
address or interface names.
Table 74. queue statement values
βββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β Value β Description β Type β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β queue_number β Sets queue number, default is β unsigned integer (16 bit) β
β β 0. β β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β queue_number_from β Sets initial queue in the β unsigned integer (16 bit) β
β β range, if fanout is used. β β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ€
β β β β
β queue_number_to β Sets closing queue in the β unsigned integer (16 bit) β
β β range, if fanout is used. β β
βββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββ
Table 75. queue statement flags
ββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β Flag β Description β
ββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β bypass β Let packets go through if userspace β
β β application cannot back off. Before using β
β β this flag, read libnetfilter_queue β
β β documentation for performance tuning β
β β recommendations. β
ββββββββββΌββββββββββββββββββββββββββββββββββββββββββββ€
β β β
β fanout β Distribute packets between several β
β β queues. β
ββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββ
DUP STATEMENT
The dup statement is used to duplicate a packet and send the copy to a different destination.
dup to device
dup to address device device
Table 76. Dup statement values
ββββββββββββββ¬βββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β Expression β Description β Type β
ββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β β β β
β address β Specifies that the copy of the β ipv4_addr, ipv6_addr, e.g. β
β β packet should be sent to a new β abcd::1234, or you can use a β
β β gateway. β mapping, e.g. ip saddr map { β
β β β 192.168.1.2 : 10.1.1.1 } β
ββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β β β β
β device β Specifies that the copy should β string β
β β be transmitted via device. β β
ββββββββββββββ΄βββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββ
Using the dup statement.
# send to machine with ip address 10.2.3.4 on eth0
ip filter forward dup to 10.2.3.4 device "eth0"
# copy raw frame to another interface
netdev ingress dup to "eth0"
dup to "eth0"
# combine with map dst addr to gateways
dup to ip daddr map { 192.168.7.1 : "eth0", 192.168.7.2 : "eth1" }
FWD STATEMENT
The fwd statement is used to redirect a raw packet to another interface. It is only available in the netdev family
ingress and egress hooks. It is similar to the dup statement except that no copy is made.
You can also specify the address of the next hop and the device to forward the packet to. This updates the source and
destination MAC address of the packet by transmitting it through the neighboring layer. This also decrements the ttl
field of the IP packet. This provides a way to effectively bypass the classical forwarding path, thus skipping the
fib (forwarding information base) lookup.
fwd to device
fwd [ip | ip6] to address device device
Using the fwd statement.
# redirect raw packet to device
netdev ingress fwd to "eth0"
# forward packet to next hop 192.168.200.1 via eth0 device
netdev ingress ether saddr set fwd ip to 192.168.200.1 device "eth0"
SET STATEMENT
The set statement is used to dynamically add or update elements in a set from the packet path. The set setname must
already exist in the given table and must have been created with one or both of the dynamic and the timeout flags.
The dynamic flag is required if the set statement expression includes a stateful object. The timeout flag is implied
if the set is created with a timeout, and is required if the set statement updates elements, rather than adding them.
Furthermore, these sets should specify both a maximum set size (to prevent memory exhaustion), and their elements
should have a timeout (so their number will not grow indefinitely) either from the set definition or from the
statement that adds or updates them. The set statement can be used to e.g. create dynamic blacklists.
Dynamic updates are also supported with maps. In this case, the add or update rule needs to provide both the key and
the data element (value), separated via :.
{add | update} @setname { expression [timeout timeout] [comment string] }
Example for simple blacklist.
# declare a set, bound to table "filter", in family "ip".
# Timeout and size are mandatory because we will add elements from packet path.
# Entries will timeout after one minute, after which they might be
# re-added if limit condition persists.
nft add set ip filter blackhole \
"{ type ipv4_addr; flags dynamic; timeout 1m; size 65536; }"
# declare a set to store the limit per saddr.
# This must be separate from blackhole since the timeout is different
nft add set ip filter flood \
"{ type ipv4_addr; flags dynamic; timeout 10s; size 128000; }"
# whitelist internal interface.
nft add rule ip filter input meta iifname "internal" accept
# drop packets coming from blacklisted ip addresses.
nft add rule ip filter input ip saddr @blackhole counter drop
# add source ip addresses to the blacklist if more than 10 tcp connection
# requests occurred per second and ip address.
nft add rule ip filter input tcp flags syn tcp dport ssh \
add @flood { ip saddr limit rate over 10/second } \
add @blackhole { ip saddr } \
drop
# inspect state of the sets.
nft list set ip filter flood
nft list set ip filter blackhole
# manually add two addresses to the blackhole.
nft add element filter blackhole { 10.2.3.4, 10.23.1.42 }
MAP STATEMENT
The map statement is used to lookup data based on some specific input key.
expression map { MAP_ELEMENTS }
MAP_ELEMENTS := MAP_ELEMENT [, MAP_ELEMENTS]
MAP_ELEMENT := key : value
The key is a value returned by expression.
Using the map statement.
# select DNAT target based on TCP dport:
# connections to port 80 are redirected to 192.168.1.100,
# connections to port 8888 are redirected to 192.168.1.101
nft add rule ip nat prerouting dnat tcp dport map { 80 : 192.168.1.100, 8888 : 192.168.1.101 }
# source address based SNAT:
# packets from net 192.168.1.0/24 will appear as originating from 10.0.0.1,
# packets from net 192.168.2.0/24 will appear as originating from 10.0.0.2
nft add rule ip nat postrouting snat to ip saddr map { 192.168.1.0/24 : 10.0.0.1, 192.168.2.0/24 : 10.0.0.2 }
VMAP STATEMENT
The verdict map (vmap) statement works analogous to the map statement, but contains verdicts as values.
expression vmap { VMAP_ELEMENTS }
VMAP_ELEMENTS := VMAP_ELEMENT [, VMAP_ELEMENTS]
VMAP_ELEMENT := key : verdict
Using the vmap statement.
# jump to different chains depending on layer 4 protocol type:
nft add rule ip filter input ip protocol vmap { tcp : jump tcp-chain, udp : jump udp-chain , icmp : jump icmp-chain }
XT STATEMENT
This represents an xt statement from xtables compat interface. It is a fallback if translation is not available or
not complete.
xt TYPE NAME
TYPE := match | target | watcher
Seeing this means the ruleset (or parts of it) were created by iptables-nft and one should use that to manage it.
BEWARE: nftables wonβt restore these statements.
ADDITIONAL COMMANDS
These are some additional commands included in nft.
LIST HOOKS
This shows the list of functions that have been registered for the given protocol family, including functions that
have been registered implicitly by kernel modules such as nf_conntrack.
list hooks [family]
list hooks netdev [ device DEVICE_NAME ]
list hooks is enough to display everything that is active on the system. Hooks in the netdev family are tied to a
network device. If no device name is given, nft will query all network devices in the current network namespace.
Example Usage:
List all active netfilter hooks in either the ip or ip6 stack.
% nft list hooks inet
family ip {
hook prerouting {
-0000000400 ipv4_conntrack_defrag [nf_defrag_ipv4]
-0000000200 ipv4_conntrack_in [nf_conntrack]
-0000000100 nf_nat_ipv4_pre_routing [nf_nat]
}
hook input {
0000000000 chain inet filter input [nf_tables]
+0000000100 nf_nat_ipv4_local_in [nf_nat]
[..]
The above shows a host that has nat, conntrack and ipv4 packet defragmentation enabled. For each hook location for
the queried family a list of active hooks using the format
priority identifier [module_name]
will be shown.
The priority value dictates the order in which the hooks are called. The list is sorted, the lowest number is run
first.
The priority value of hooks registered by the kernel cannot be changed. For basechains registered by nftables, this
value corresponds to the priority value specified in the base chain definition.
After the numerical value, information about the hook is shown. For basechains defined in nftables this includes the
table family, the table name and the basechains name. For hooks coming from kernel modules, the function name is used
instead.
If a module name is given, the hook was registered by the kernel module with this name. You can use modinfo module
name to obtain more information about the module.
This functionality requires a kernel built with the option CONFIG_NETFILTER_NETLINK_HOOK enabled, either as a module
or builtin. The module is named nfnetlink_hook.
MONITOR
The monitor command allows you to listen to Netlink events produced by the nf_tables subsystem. These are either
related to creation and deletion of objects or to packets for which meta nftrace was enabled. When they occur, nft
will print to stdout the monitored events in either JSON or native nft format.
monitor [new | destroy] MONITOR_OBJECT
monitor trace
MONITOR_OBJECT := tables | chains | sets | rules | elements | ruleset
To filter events related to a concrete object, use one of the keywords in MONITOR_OBJECT.
To filter events related to a concrete action, use keyword new or destroy.
The second form of invocation takes no further options and exclusively prints events generated for packets with
nftrace enabled.
Hit ^C to finish the monitor operation.
Listen to all events, report in native nft format.
% nft monitor
Listen to deleted rules, report in JSON format.
% nft -j monitor destroy rules
Listen to both new and destroyed chains, in native nft format.
% nft monitor chains
Listen to ruleset events such as table, chain, rule, set, counters and quotas, in native nft format.
% nft monitor ruleset
Trace incoming packets from host 10.0.0.1.
% nft add rule filter input ip saddr 10.0.0.1 meta nftrace set 1
% nft monitor trace
ERROR REPORTING
When an error is detected, nft shows the line(s) containing the error, the position of the erroneous parts in the
input stream and marks up the erroneous parts using carets (^). If the error results from the combination of two
expressions or statements, the part imposing the constraints which are violated is marked using tildes (~).
For errors returned by the kernel, nft cannot detect which parts of the input caused the error and the entire command
is marked.
Error caused by single incorrect expression.
:1:19-22: Error: Interface does not exist
filter output oif eth0
^^^^
Error caused by invalid combination of two expressions.
:1:28-36: Error: Right hand side of relational expression (==) must be constant
filter output tcp dport == tcp dport
~~ ^^^^^^^^^
Error returned by the kernel.
:0:0-23: Error: Could not process rule: Operation not permitted
filter output oif wlan0
^^^^^^^^^^^^^^^^^^^^^^^
EXIT STATUS
On success, nft exits with a status of 0. Unspecified errors cause it to exit with a status of 1, memory allocation
errors with a status of 2, unable to open Netlink socket with 3.
SEE ALSO
libnftables(3), libnftables-json(5), iptables(8), ip6tables(8), arptables(8), ebtables(8), ip(8), tc(8)
There is an official wiki at: https://wiki.nftables.org
AUTHORS
nftables was written by Patrick McHardy and Pablo Neira Ayuso, among many other contributors from the Netfilter
community.
COPYRIGHT
Copyright Β© 2008-2014 Patrick McHardy Copyright Β© 2013-2018 Pablo Neira Ayuso
nftables is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This documentation is licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0 license, CC BY-SA
4.0 http://creativecommons.org/licenses/by-sa/4.0/.
12/05/2025 NFT(8)