nft (nftables) (2/4)
nftables can keep state that lives in the table (named counters, quotas, limits, ct objects), can describe every data type it understands, and can match packet metadata with meta. This part covers those stateful objects, nft describe, data type values like ICMP and conntrack, and meta expressions.
101.1 Why is it used?
- A plain counter inside a rule counts only that rule; a named counter is one object that many rules (or a map) can share.
- Quota tracks bytes (e.g. give a guest laptop only 20 MB), limit tracks a rate (e.g. 10 packets per second).
- Named objects can be listed, reset and deleted independently of the rules that use them.
- nft describe tells you the valid values of any type (tcp flags, ct_state...) without guessing.
- meta matches what the kernel knows about a packet (interface, length, protocol, socket user), not just header fields.
101.2 Named counter: how it works
- Step 1
1.PC 192.168.121.10 sends a request to server 192.168.121.80
- Step 2
2.Packet enters the input chain of table inet filter
- Step 3
3.tcp dport 80 matches -> counter name http goes up
- Step 4
4.For port 443 the map { 22 : "ssh", 443 : "https" } picks the https counter
- Step 5
5.nft list counters shows packets/bytes per named counter
The counter lives in the table, not inside one rule
Lab: server 192.168.121.80 and PC 192.168.121.10 on a Docker network. First check what the kernel has: only nft_compat is loaded, and quota/limit/ct are modules (=m) that are not loaded, so counters are run for real and the rest is shown as option tables.
grep -oE '^nf[a-z_0-9]+' /proc/moduleszcat /proc/config.gz | grep -E \ 'NFT_(QUOTA|LIMIT|CT|REJECT)=|NETLINK_HOOK'$ grep -oE '^nf[a-z_0-9]+' /proc/modulesnfnetlink_cttimeoutnf_conncountnf_conntrack_netlinknf_reject_ipv4nf_reject_ipv6nft_compat$ zcat /proc/config.gz | grep -E ...# CONFIG_NETFILTER_NETLINK_HOOK is not setCONFIG_NFT_CT=mCONFIG_NFT_LIMIT=mCONFIG_NFT_QUOTA=mCONFIG_NFT_REJECT=m101.3 Creating and using named counters
nft add counter inet filter httpnft add counter inet filter https \ '{ packets 0 bytes 0; comment "tls"; }'nft add counter inet filter sshnft add rule inet filter input \ tcp dport 80 counter name httpnft add rule inet filter input counter \ name tcp dport map \ '{ 22 : "ssh", 443 : "https" }'nft list chain inet filter inputtable inet filter { chain input { type filter hook input priority filter; policy accept; tcp dport 80 counter name "http" counter name tcp dport map { 22 : "ssh", 443 : "https" } }}Traffic from the PC: three connections to port 80, one to 443, one to 22 (closed). The SYN to 22 is still counted.
for i in 1 2 3; do \ nc -w 2 192.168.121.80 80 </dev/null; donenc -w 2 192.168.121.80 443 </dev/nullnc -zv -w 2 192.168.121.80 22nft list counterssrv websrv websrv websrv tlsnc: connect to 192.168.121.80 port 22(tcp) failed: Connection refused$ nft list counterstable inet filter { counter http { packets 12 bytes 648 } counter https { comment "tls" packets 4 bytes 216 } counter ssh { packets 1 bytes 60 }}101.4 reset, delete, destroy
- reset counter prints the current value first, then sets it to zero; reset counters inet filter zeroes all counters of the table.
- delete on a counter still used by a rule gives Resource busy: delete the rule first (by handle), then the counter.
- destroy is like delete but gives no error if the object does not exist.
- Counters used inside a map (ssh here) are busy too.
nft reset counter inet filter httpnft list counter inet filter httpnft reset counters inet filternft delete counter inet filter httpnft delete rule inet filter input handle 5nft delete counter inet filter httpnft destroy counter inet filter httpnft delete counter inet filter handle 4$ nft reset counter inet filter http counter http { packets 12 bytes 648$ nft list counter inet filter http counter http { packets 0 bytes 0$ nft delete counter inet filter httpError: Could not process rule: Resource busy$ nft delete rule inet filter input handle 5[exit 0]$ nft delete counter inet filter http[exit 0]$ nft destroy counter inet filter http[exit 0]$ nft delete counter inet filter handle 4Error: Could not process rule: Resource busy101.5 Quota, limit and ct objects
- Step 1
1.Guest laptop 192.168.121.50 sends data through the nft firewall
- Step 2
2.Usage is low -> quota does not match -> packet goes to the Internet
- Step 3
3.Firewall keeps adding bytes to the quota object
- Step 4
4.Total crosses 20 MB -> quota over 20 mbytes matches
- Step 5
5.Next packet is dropped right at the firewall
limit works the same way on a rate, e.g. 10/second
| Option | Meaning |
|---|---|
| over 20 mbytes | Match once usage is above N |
| until 1000 mbytes | Match until N is used |
| used 5 mbytes | Initial used amount |
| quota name q1 | Use the named quota in a rule |
| add limit ... lim1 { rate 10/second; } | Named rate limit object |
| limit name lim1 | Use the named limit in a rule |
| ct helper (type "ftp") | Understands related connections of FTP-like protocols |
| ct timeout (policy = { established: 120, close: 20 }) | Per-state TCP/UDP timeouts |
| ct expectation (protocol udp; dport 9876; timeout 2m) | Expect an incoming connection in advance |
- list, reset and delete/destroy work for quota and limit just like for counters (list quotas, reset quotas, delete limit handle N).
- ct objects are assigned with set: ct helper|timeout|expectation set "name"; they need the nft ct module.
- TCP states for ct timeout: close, close_wait, established, fin_wait, last_ack, retrans, syn_recv, syn_sent, time_wait, unack; UDP: replied, unreplied.
- Other listings: nft list secmarks / synproxys / flowtables / meters (empty in this lab); list hooks needs NETLINK_HOOK in the kernel, which this lab kernel does not have.
101.6 nft describe
nft describe works only in userspace, so it runs on the PC without admin rights.
nft describe tcp flagsnft describe ct_statenft describe booleannft describe ipv4_addr$ nft describe tcp flagspayload expression, datatype tcp_flag(TCP flag) (basetype bitmask, integer), 8 bits fin 0x01 syn 0x02 rst 0x04 psh 0x08 ack 0x10 urg 0x20$ nft describe ct_statedatatype ct_state (conntrack state)(basetype bitmask, integer), 32 bits invalid 0x00000001 new 0x00000008 established 0x00000002 related 0x00000004 untracked 0x00000040$ nft describe booleandatatype boolean (boolean type)(basetype integer), 1 bits exists 1 missing 0$ nft describe ipv4_addrdatatype ipv4_addr (IPv4 address)(basetype integer), 32 bits101.7 Data type values: ICMP, ICMPv6, conntrack
nft add rule inet filter input \ icmp type echo-request counternft add rule inet filter output \ icmp type echo-reply counterping -c 3 -W 1 192.168.121.80 | tail -2nft list ruleset | grep -E "icmp type"3 packets transmitted, 3 received, 0% packet lossrtt min/avg/max/mdev = 0.106/0.132/0.152/0.019 msicmp type echo-request counter packets 3 bytes 252icmp type echo-reply counter packets 3 bytes 252| Type | Key values | Used for |
|---|---|---|
| icmp_type | 0 echo-reply, 3 destination-unreachable, 8 echo-request, 11 time-exceeded | ping, traceroute (time-exceeded) |
| icmp_code | 3 port-unreachable, 10 host-prohibited, 13 admin-prohibited | reject with icmp |
| icmpv6_type | 2 packet-too-big, 128/129 echo, 133-136 nd-router/neighbor solicit/advert | Never block ND and packet-too-big |
| icmpx_code | 0 no-route, 1 port-unreachable, 2 host-unreachable, 3 admin-prohibited | One reject rule for IPv4 + IPv6 in inet tables |
| ct state | invalid 1, established 2, related 4, new 8, untracked 64 | Stateful firewall rules |
| ct direction / ct status | original 0, reply 1 / assured 4, confirmed 8, snat 16, dnat 32 | Direction and flags of a connection |
| dccp_pkttype | 0 request, 1 response ... 7 reset, 8 sync, 9 syncack | dccp type match |
- IPv6 has no ARP: neighbor discovery runs over ICMPv6, so nd-neighbor-solicit/advert must pass.
- IPv6 routers do not fragment, so packet-too-big is essential for path MTU.
- ct label names come from /etc/connlabel.conf.
101.8 Expressions: meta
- Step 1
1.PC sends ping -s 1200 to the server
- Step 2
2.Packet arrives on eth0 of the server kernel
- Step 3
3.Kernel metadata: iifname eth0, length 1228, l4proto icmp, pkttype host
- Step 4
4.nft input chain matches meta rules and counts
- Step 5
5.Reply from local socat app (root) leaves via output: meta skuid 0
Header fields are inside the packet; meta is what the kernel holds about it
nft add rule inet filter input \ 'meta length > 1000 counter'nft add rule inet filter input \ meta l4proto icmp counternft add rule inet filter input \ iifname "eth0" meta nfproto ipv4 counternft add rule inet filter input \ meta pkttype host counternft add rule inet filter output \ meta skuid 0 meta l4proto tcp counterping -c 2 -W 1 192.168.121.80 | tail -1ping -c 2 -s 1200 192.168.121.80nc -w 2 192.168.121.80 80 </dev/nullnft list chain inet filter inputnft list chain inet filter output$ ping -c 2 -s 1200 192.168.121.80PING 192.168.121.80 1200(1228) bytes of data.1208 bytes from 192.168.121.80: icmp_seq=11208 bytes from 192.168.121.80: icmp_seq=22 packets transmitted, 2 received, 0% loss$ nft list chain inet filter inputmeta length > 1000 counter packets 2 bytes 2456meta l4proto icmp counter packets 4 bytes 2624iifname "eth0" meta nfproto ipv4 counter packets 8 bytes 2840meta pkttype host counter packets 8 bytes 2840$ nft list chain inet filter outputmeta skuid 0 meta l4proto tcp counter packets 4 bytes 224- length > 1000 got only 2 packets: only the requests of the big ping are above 1000 bytes.
- meta is optional with mark and iifname, required with length, nfproto and l4proto.
- Other useful keys: skuid/skgid (socket user/group), pkttype (host, broadcast, multicast, other), cgroup, cpu, time/day/hour for time-based rules.
101.9 Comparison: iif vs iifname, anonymous vs named counter
| Point | iif | iifname |
|---|---|---|
| Compares | Interface index | Interface name |
| Interface must exist at load time | Yes | No |
| Speed | Fast | Slightly slower |
| Good for | Static interfaces like eth0 | ppp/VPN created later, wildcards like "ppp*" |
| Point | Anonymous counter | Named counter |
|---|---|---|
| Lives | Inside one rule | In the table as an object |
| Shared | No | Yes, by many rules or a map |
| Lifecycle | Deleted with the rule | list / reset / delete independently |
101.10 Common mistakes
- Deleting a named counter that a rule still uses: Resource busy; delete the rule first.
- Using iif for a VPN/ppp interface that comes up later: the rule fails or never matches; use iifname.
- Blocking all ICMPv6: breaks neighbor discovery and path MTU in IPv6.
- Expecting quota/limit/ct objects to work when the kernel module is not available.
101.11 Interview tips
- Anonymous counter = inside one rule; named counter = table object shared by rules/maps, with its own list/reset/delete.
- reset counter prints the value, then zeroes it.
- quota = bytes, limit = rate; both are stateful objects.
- iif = index (must exist, fast), iifname = name (dynamic interfaces, wildcards).
- Use nft describe to see valid values of a type such as ct_state or tcp flags.