PatrickMcHardykaber@trash.net2008-2014Patrick McHardynft8nft
Administration tool for packet filtering and classification
nftdirectoryfilenamecmdnftDescription
nft is used to set up, maintain and inspect packet
filtering and classification rules in the Linux kernel.
Options
For a full summary of options, run nft --help.
Show help message and all options.
Show version.
Numeric output: Addresses and other information
that might need network traffic to resolve to symbolic names
are shown numerically (default behaviour). When used twice,
internet services are translated. When used twice, internet
services and UIDs/GIDs are also shown numerically. When used
three times, protocol numbers are also shown numerically.
Translate IP addresses to DNS names.
Show rule handles in output.
Add the directory directory to the list of directories to by searched for included files.
Read input from filename.
Read input from an interactive readline CLI.
Input file formatLexical 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 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 filesinclude "filename"
Other files can be included by using the include statement.
The directories to be searched for include files can be specified using
the option.
Symbolic variablesdefinevariable = expr$variable
Symbolic variables can be defined using the define statement.
Variable references are expressions and can be used initialize other variables.
The scope of a definition is the current block and all blocks contained within.
Using symbolic variables
define int_if1 = eth0
define int_if2 = eth1
define int_ifs = { $int_if1, $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.
IPv4 address family.
IPv6 address family.
Internet (IPv4/IPv6) address family.
ARP address family, handling packets vi
Bridge address family, handling packets which traverse a bridge device.
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.
IPv4/IPv6/Inet address family hooksHookDescriptionprerouting
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.
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.
ARP address family hooksHookDescriptioninput
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.
Tablesadddeletelistflushtablefamilytable
Tables are containers for chains and sets. They are identified by their address family
and their name. The address family must be one of
ipip6inetarpbridge.
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.
Add a new table for the given family with the given name.
Delete the specified table.
List all chains and rules of the specified table.
Flush all chains and rules of the specified table.
Chainsaddchainfamilytablechainhookprioritypolicyaddcreatedeletelistflushchainfamilytablechainrenamechainfamilytablechainnewname
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.
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.
Simlar to the add command, but returns an error if the
chain already exists.
Delete the specified chain. The chain must not contain any rules or be
used as jump target.
Rename the specified chain.
List all rules of the specified chain.
Flush all rules of the specified chain.
Rulesaddinsertrulefamilytablechainposition positionstatementdeleterulefamilytablechainhandle handle
Rules are constructed from two kinds of components according to a set
of grammatical rules: expressions and statements.
Add a new rule described by the list of statements. The rule is appended to the
given chain unless a position is specified, in which case the rule is appended to
the rule given by the position.
Similar to the add command, but the rule is prepended to the
beginning of the chain or before the rule at the given position.
Delete the specified rule.
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 commanddescribeexpression
The describe command shows information about the type of an expression and
its data type.
The describe command
$ nft describe tcp flags
payload expression, datatype tcp_flag (TCP flag) (basetype bitmask, integer), 8 bits
pre-defined 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.
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
NameKeywordSizeBase typeIntegerintegervariable-
The integer type is used for numeric values. It may be specified as decimal, hexadecimal
or octal number. The integer type doesn't have a fixed size, its size is determined by the
expression for which it is used.
Bitmask type
The bitmask type (bitmask) is used for bitmasks.
String type
NameKeywordSizeBase typeStringstringvariable-
The string type is used to 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
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
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
The IPv6 address type is used for IPv6 addresses. FIXME
IPv6 address specification
# abbreviated loopback address
filter output ip6 daddr ::1
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 expressionsmetalengthnfprotol4protoprotocolprioritymetamarkiifiifnameiiftypeoifoifnameoiftypeskuidskgidnftracertclassid
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 expression typesKeywordDescriptionTypelengthLength of the packet in bytesinteger (32 bit)protocolEthertype protocol valueether_typepriorityTC packet priorityinteger (32 bit)markPacket markpacketmarkiifInput interface indexiface_indexiifnameInput interface namestringiiftypeInput interface typeiface_typeoifOutput interface indexiface_indexoifnameOutput interface namestringoiftypeOutput interface hardware typeiface_typeskuidUID associated with originating socketuidskgidGID associated with originating socketgidrtclassidRouting realmrealm
Meta expression specific typesTypeDescriptioniface_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.
Using meta expressions
# qualified meta expression
filter output meta oif eth0
# unqualified meta expression
filter output oif eth0
Payload expressions
Payload expressions refer to data from the packet's payload.
Ethernet header expressionetherethernet header field
Ethernet header expression typesKeywordDescriptionTypedaddrDestination MAC addressether_addrsaddrSource MAC addressether_addrtypeEtherTypeether_type
VLAN header expressionvlanVLAN header field
VLAN header expressionKeywordDescriptionTypeidVLAN ID (VID)integer (12 bit)cfiCanonical Format IndicatorflagpcpPriority code pointinteger (3 bit)typeEtherTypeethertype
blaIPv6 extension header expressions
IPv6 extension header expressions refer to data from an IPv6 packet's extension headers.
Conntrack expressions
Conntrack expressions refer to meta data of the connection tracking entry associated with a packet.
ctstatedirectionstatusmarkexpirationhelperl3protosaddrdaddrprotocolproto-srcproto-dst
Conntrack expressionsKeywordDescriptionTypestateState of the connectionct_statedirectionDirection of the packet relative to the connectionct_dirstatusStatus of the connectionct_statusmarkConnection markpacketmarkexpirationConnection expiration timetimehelperHelper associated with the connectionstringl3protoLayer 3 protocol of the connectionnf_proto FIXMEsaddrSource address of the connection for the given directionipv4_addr/ipv6_addrdaddrDestination address of the connection for the given directionipv4_addr/ipv6_addrprotocolLayer 4 protocol of the connection for the given directioninet_protoproto-srcLayer 4 protocol source for the given directionFIXMEproto-dstLayer 4 protocol destination for the given directionFIXME