nftables
nftables
Hierarchy
Tables > Chains > Rules
Tables have a family, and a name. Different families can have different tables. Tables contain
chains that contain a series of rules.
Tables
Command Meaning
nft add table <family> <type> Add a table of family and type
nft delete table <family> <type> Delete a table of family and type
nft flush table <family> <type> Flush the entire ruleset from a table
nft list table <family> <type> [-a] List contents (showing rule numbers or not)
ip ✅ ✅ ✅
ip6 ✅ ✅ ✅
inet ✅ ❌ ❌
Family filter route nat
arp ✅ ❌ ❌
bridge ❌ ❌ ❌
netdev ❌ ❌ ❌
Chains
Command Meaning
nft add chain <table> <name> { type <type> hook <hook> Create the chain with that type, hook, priority, and policy
[device <device>] priority <priority>; policy <policy>; }
nft list chain <table> <name> [-a] List the contents of a chain [with handler numbers]
Where the type can be filter , route and nat as shown above. A chain must follow the type of its
parent table.
Chain Hook
The hook is the corresponding chain endpoint that is being hooked, much like in iptables(6) before:
netdev ingress
Chain Priority
The priority either orders the chains, or declares specific options, such as :
0 Filtering chain
Policies
The chain policy defines the final verdict on the packet. By default, we accept packets. The only
other value at the moment is drop .
Rules
Rules contain a statement of filtering and intent that is inspected and potentially executed in part
or completely. A rule with a target stops the inspection process of the packet through the chain.
Command Meaning
nft add rule <table> <chain> <rule> Add a rule to a table's chain
nft insert rule <table> <chain> [position <pos>] <rule> Insert a rule at a position (by default 0)
nft replace rule <table> <chain> handle <handle> <rule> Replace the rule with a handler with a new one
nft delete rule <table> <chain> handle <handle> <rule> Delete a rule with the given handler
Rules are series of matches and statements. Matches may be, among others
Match Meaning
tcp sequence != 33-45 Matches TCP packets which sequence number isn't
between 33 and 45 included
Inspecting
Use the list and show commands to inspect elements
Command Description
nft list table <family> <type> Show the entire contents of a single table
nft list chain <family> <type> <chain> Show the entire contents of a single chain
nft list counter <family> <type> <counter> Show the values of the counter
nft list set <family> <type> <set> Show the contents of the set
In the content listing commands with rules, add -a to get the handle numbers to remove rules.
A selector can be <family> with a family for all commands. Object commands (sets and counters)
can also take table <family> <type> .
Sets
Sets are very powerful and can even help you store things fast. Sets can be anonymous, or named.
Anonymous sets are bound to a rule, have no name, and cannot be updated. A set can virtually
contain anything.
Command Meaning
nft add rule ip6 filter input tcp dport {telnet, http, https} accept Accept IPv6 TCP destined to telnet, http or https
nft add set ip nat my_new_set { type ipv4_addr; comment "Some Create a named set of IPv4 in the ip nat table
IPv4"; }
nft delete set ip nat collect Delete the set "collect" from ip nat
nft add rule ip filter input ip saddr @blackhole drop Add a rule to drop IPs in a set
nft add rule ip nat postrouting add @collect { ip daddr } Add the destination address to a set
Counters
Counters can also be anonymous or named, with the same restrictions. They show a number of
packets and bytes.
Command Meaning
nft add counter ip nat prerouting out_of_route Add a counter called out_of_route to chain ip nat prerouting
nft add rule ip filter input tcp dport 22 counter accept Add a rule that filters incoming SSH, counts it
anonymously, and accept
nft add rule ip filter input counter tcp dport 22 accept Add a rule that counts all packets then filters and accept
nft list counter inet filter refused_input Show the contents of the counter "refused_input" of table
"inet filter"
nft add rule inet filter input counter name "refused_input" Add a rule that just counts to a named counter
nft reset counter inet filter refused_input Reset the values of the given counter
nft reset counters table inet filter Reset all counters in table "inet filter"
Maps
Maps are objects like counters and sets, but with key-value storage.
Command Meaning
nft list maps List all maps in the ruleset
nft add map inet nat gate_marks { type ether_addr: mark\; } Add a map of MACs to marks in "inet nat"
nft list map inet nat gate_marks List the contents of the map
nft add rule ip nat PREROUTING add @post_map { ether saddr: ip Add a rule to add a pair of source MAC-source IPv4 in a
saddr }
map called "post_map"
Revision #5
Created 9 August 2022 09:54:31 by Amelia
Updated 16 September 2022 23:08:22 by Amelia