neulab/tldr huggingface dataset:
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 11. conntrack helper specifications ┌─────────┬─────────────────────┬──────────────────────┐ │Keyword  │ Description         │ Type                 │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │type     │ name of helper type │ quoted string (e.g.  │ │         │                     │ "ftp")               │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │protocol │ layer 4 protocol of │ string (e.g. ip)     │ │         │ the helper          │                      │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │l3proto  │ layer 3 protocol of │ address family (e.g. │ │         │ the helper          │ ip)                  │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │comment  │ per ct helper       │ string               │ │         │ comment field       │                      │ └─────────┴─────────────────────┴──────────────────────┘
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 12. conntrack timeout specifications ┌─────────┬─────────────────────┬──────────────────────┐ │Keyword  │ Description         │ Type                 │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │protocol │ layer 4 protocol of │ string (e.g. ip)     │ │         │ the timeout object  │                      │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │state    │ connection state    │ string (e.g.         │ │         │ name                │ "established")       │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │value    │ timeout value for   │ unsigned integer     │ │         │ connection state    │                      │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │l3proto  │ layer 3 protocol of │ address family (e.g. │ │         │ the timeout object  │ ip)                  │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │comment  │ per ct timeout      │ string               │ │         │ comment field       │                      │ └─────────┴─────────────────────┴──────────────────────┘
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: 120, close: 20 } }
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 13. conntrack expectation specifications ┌─────────┬─────────────────────┬──────────────────────┐ │Keyword  │ Description         │ Type                 │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │protocol │ layer 4 protocol of │ string (e.g. ip)     │ │         │ the expectation     │                      │ │         │ object              │                      │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │dport    │ destination port of │ unsigned integer     │ │         │ expected connection │                      │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │timeout  │ timeout value for   │ unsigned integer     │ │         │ expectation         │                      │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │size     │ size value for      │ unsigned integer     │ │         │ expectation         │                      │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │l3proto  │ layer 3 protocol of │ address family (e.g. │ │         │ the expectation     │ ip)                  │ │         │ object              │                      │ ├─────────┼─────────────────────┼──────────────────────┤ │         │                     │                      │ │comment  │ per ct expectation  │ string               │ │         │ comment 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 14. Counter specifications ┌────────┬─────────────────────┬──────────────────────┐ │Keyword │ Description         │ Type                 │ ├────────┼─────────────────────┼──────────────────────┤ │        │                     │                      │ │packets │ initial count of    │ unsigned integer (64 │ │        │ packets             │ bit)                 │ ├────────┼─────────────────────┼──────────────────────┤ │        │                     │                      │ │bytes   │ initial count of    │ unsigned integer (64 │ │        │ bytes               │ bit)                 │ ├────────┼─────────────────────┼──────────────────────┤ │        │                     │                      │ │comment │ per counter comment │ string               │ │        │ field               │                      │ └────────┴─────────────────────┴──────────────────────┘
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 15. Quota specifications ┌────────┬──────────────────────┬──────────────────────┐ │Keyword │ Description          │ Type                 │ ├────────┼──────────────────────┼──────────────────────┤ │        │                      │                      │ │quota   │ quota limit, used as │ Two arguments,       │ │        │ the quota name       │ unsigned integer (64 │ │        │                      │ bit) and string:     │ │        │                      │ bytes, kbytes,       │ │        │                      │ mbytes. "over" and   │ │        │                      │ "until" go before    │ │        │                      │ these arguments      │ ├────────┼──────────────────────┼──────────────────────┤ │        │                      │                      │ │used    │ initial value of     │ Two arguments,       │ │        │ used quota           │ unsigned integer (64 │ │        │                      │ bit) and string:     │ │        │                      │ bytes, kbytes,       │ │        │                      │ mbytes               │ ├────────┼──────────────────────┼──────────────────────┤ │        │                      │                      │ │comment │ per quota comment    │ string               │ │        │ field                │                      │ └────────┴──────────────────────┴──────────────────────┘
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 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 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.
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 │ lladdr  │ variable │ integer   │ │address    │         │          │           │ └───────────┴─────────┴──────────┴───────────┘
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 16. The following keywords will automatically resolve into a boolean type with given value ┌────────┬───────┐ │Keyword │ Value │ ├────────┼───────┤ │        │       │ │exists  │ 1     │ ├────────┼───────┤ │        │       │ │missing │ 0     │ └────────┴───────┘
Table 17. 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 oif 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 18. 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.
Table 19. Keywords may be used 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    │ └─────────────────┴───────┘
ICMPV6 TYPE TYPE ┌────────────┬────────────┬───────┬───────────┐ │Name        │ Keyword    │ Size  │ Base type │ ├────────────┼────────────┼───────┼───────────┤ │            │            │       │           │ │ICMPv6 Type │ icmpx_code │ 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.
Table 21. keywords may be used 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     │ └─────────────────┴───────┘
ICMPVX CODE TYPE ┌────────────┬─────────────┬───────┬───────────┐ │Name        │ Keyword     │ Size  │ Base type │ ├────────────┼─────────────┼───────┼───────────┤ │            │             │       │           │ │ICMPvX Code │ icmpv6_type │ 8 bit │ integer   │ └────────────┴─────────────┴───────┴───────────┘
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 22. keywords may be used when specifying the ICMPvX code ┌─────────────────┬───────┐ │Keyword          │ Value │ ├─────────────────┼───────┤ │                 │       │ │no-route         │ 0     │ ├─────────────────┼───────┤ │                 │       │ │port-unreachable │ 1     │ ├─────────────────┼───────┤ │                 │       │ │host-unreachable │ 2     │ ├─────────────────┼───────┤ │                 │       │ │admin-prohibited │ 3     │ └─────────────────┴───────┘
CONNTRACK TYPES Table 23. overview of types used in ct expression and statement ┌─────────────────┬───────────┬─────────┬───────────┐ │Name             │ Keyword   │ Size    │ Base type │ ├─────────────────┼───────────┼─────────┼───────────┤ │                 │           │         │           │ │conntrack state  │ ct_state  │ 4 byte  │ bitmask   │ ├─────────────────┼───────────┼─────────┼───────────┤ │                 │           │         │           │ │conntrack        │ ct_dir    │ 8 bit   │ integer   │ │direction        │           │         │           │ ├─────────────────┼───────────┼─────────┼───────────┤ │                 │           │         │           │ │conntrack status │ ct_status │ 4 byte  │ bitmask   │ ├─────────────────┼───────────┼─────────┼───────────┤ │                 │           │         │           │ │conntrack event  │ ct_event  │ 4 byte  │ bitmask   │ │bits             │           │         │           │ ├─────────────────┼───────────┼─────────┼───────────┤ │                 │           │         │           │ │conntrack label  │ ct_label  │ 128 bit │ bitmask   │ └─────────────────┴───────────┴─────────┴───────────┘
For each of the types above, keywords are available for convenience:
Table 24. conntrack state (ct_state) ┌────────────┬───────┐ │Keyword     │ Value │ ├────────────┼───────┤ │            │       │ │invalid     │ 1     │ ├────────────┼───────┤ │            │       │ │established │ 2     │ ├────────────┼───────┤ │            │       │ │related     │ 4     │ ├────────────┼───────┤ │            │       │ │new         │ 8     │ ├────────────┼───────┤ │            │       │ │untracked   │ 64    │ └────────────┴───────┘
Table 25. conntrack direction (ct_dir) ┌─────────┬───────┐ │Keyword  │ Value │ ├─────────┼───────┤ │         │       │ │original │ 0     │ ├─────────┼───────┤ │         │       │ │reply    │ 1     │ └─────────┴───────┘
Table 26. conntrack status (ct_status) ┌───────────┬───────┐ │Keyword    │ Value │ ├───────────┼───────┤ │           │       │ │expected   │ 1     │ ├───────────┼───────┤ │           │       │ │seen-reply │ 2     │ ├───────────┼───────┤ │           │       │ │assured    │ 4     │ ├───────────┼───────┤ │           │       │ │confirmed  │ 8     │ ├───────────┼───────┤ │           │       │ │snat       │ 16    │ ├───────────┼───────┤ │           │       │ │dnat       │ 32    │ ├───────────┼───────┤ │           │       │ │dying      │ 512   │ └───────────┴───────┘
Table 27. 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 28. 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     │ └─────────┴───────┘
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 (\).
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
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 31. Available socket attributes ┌────────────┬──────────────────────┬─────────────────┐ │Name        │ Description          │ Type            │ ├────────────┼──────────────────────┼─────────────────┤ │            │                      │                 │ │transparent │ Value of the         │ boolean (1 bit) │ │            │ IP_TRANSPARENT       │                 │ │            │ 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    │ boolean (1 bit) │ │            │ the socket is        │                 │ │            │ wildcard-bound (e.g. │                 │ │            │ 0.0.0.0 or ::0).     │                 │ ├────────────┼──────────────────────┼─────────────────┤ │            │                      │                 │ │cgroupv2    │ cgroup version 2 for │ cgroupv2        │ │            │ this 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 32. Available osf attributes ┌────────┬──────────────────────┬────────┐ │Name    │ Description          │ Type   │ ├────────┼──────────────────────┼────────┤ │        │                      │        │ │ttl     │ Do TTL checks on the │ string │ │        │ packet to determine  │        │ │        │ the operating        │        │ │        │ system.              │        │ ├────────┼──────────────────────┼────────┤ │        │                      │        │ │version │ Do OS version checks │        │ │        │ on the packet.       │        │ ├────────┼──────────────────────┼────────┤ │        │                      │        │ │name    │ Name of the OS       │ string │ │        │ signature to 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 {saddr | daddr | mark | iif | oif} [. ...] {oif | oifname | type}
A fib expression queries the fib (forwarding information base) to obtain information such as the output interface index a particular address would use. The input is a tuple of elements that is used as input to the fib lookup functions.
Table 33. fib expression specific types ┌────────┬──────────────────┬──────────────────┐ │Keyword │ Description      │ Type             │ ├────────┼──────────────────┼──────────────────┤ │        │                  │                  │ │oif     │ Output interface │ integer (32 bit) │ │        │ index            │                  │ ├────────┼──────────────────┼──────────────────┤ │        │                  │                  │ │oifname │ Output interface │ string           │ │        │ name             │                  │ ├────────┼──────────────────┼──────────────────┤ │        │                  │                  │ │type    │ Address type     │ fib_addrtype     │ └────────┴──────────────────┴──────────────────┘
Use nft describe fib_addrtype to get a list of all address types.
Using fib expressions.
# drop packets without a reverse path filter prerouting fib saddr . iif oif missing drop
In this example, 'saddr . iif' looks up routing information based on the source address and the input interface. oif picks the output interface index from the routing information. If no route was found for the source address/input interface combination, the output interface index is zero. In case the input interface is specified as part of the input key, the output interface index is always the same as the input interface index or zero. If only 'saddr oif' is given, then oif can be any interface index or zero.
# drop packets to address not configured on incoming interface filter prerouting fib daddr . iif type != { local, broadcast, multicast } drop
# 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 34. Routing expression types ┌────────┬─────────────────────┬─────────────────────┐ │Keyword │ Description         │ Type                │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │classid │ Routing realm       │ realm               │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │nexthop │ Routing nexthop     │ ipv4_addr/ipv6_addr │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │mtu     │ TCP maximum segment │ integer (16 bit)    │ │        │ size of route       │                     │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │ipsec   │ route via ipsec     │ boolean             │ │        │ tunnel or transport │                     │ └────────┴─────────────────────┴─────────────────────┘
Table 35. 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 36. Ipsec expression types ┌────────┬─────────────────────┬─────────────────────┐ │Keyword │ Description         │ Type                │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │reqid   │ Request ID          │ integer (32 bit)    │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │spi     │ Security Parameter  │ integer (32 bit)    │ │        │ Index               │                     │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │saddr   │ Source address of   │ ipv4_addr/ipv6_addr │ │        │ the tunnel          │                     │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │daddr   │ Destination address │ ipv4_addr/ipv6_addr │ │        │ of the tunnel       │                     │ └────────┴─────────────────────┴─────────────────────┘
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 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 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 refer to data from the packet’s payload.
ETHERNET HEADER EXPRESSION ether {daddr | saddr | type}
Table 37. Ethernet header expression types ┌────────┬────────────────────┬────────────┐ │Keyword │ Description        │ Type       │ ├────────┼────────────────────┼────────────┤ │        │                    │            │ │daddr   │ Destination MAC    │ ether_addr │ │        │ address            │            │ ├────────┼────────────────────┼────────────┤ │        │                    │            │ │saddr   │ Source MAC address │ ether_addr │ ├────────┼────────────────────┼────────────┤ │        │                    │            │ │type    │ EtherType          │ ether_type │ └────────┴────────────────────┴────────────┘
VLAN HEADER EXPRESSION vlan {id | dei | pcp | type}
Table 38. VLAN header expression ┌────────┬─────────────────────┬──────────────────┐ │Keyword │ Description         │ Type             │ ├────────┼─────────────────────┼──────────────────┤ │        │                     │                  │ │id      │ VLAN ID (VID)       │ integer (12 bit) │ ├────────┼─────────────────────┼──────────────────┤ │        │                     │                  │ │dei     │ Drop Eligible       │ integer (1 bit)  │ │        │ Indicator           │                  │ ├────────┼─────────────────────┼──────────────────┤ │        │                     │                  │ │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 39. 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      │ ether_addr       │ │            │ address              │                  │ ├────────────┼──────────────────────┼──────────────────┤ │            │                      │                  │ │daddr ether │ Ethernet target      │ ether_addr       │ │            │ address              │                  │ ├────────────┼──────────────────────┼──────────────────┤ │            │                      │                  │ │saddr ip    │ IPv4 sender address  │ ipv4_addr        │ ├────────────┼──────────────────────┼──────────────────┤ │            │                      │                  │ │daddr ip    │ IPv4 target address  │ ipv4_addr        │ └────────────┴──────────────────────┴──────────────────┘