nft (nftables) (3/4)
A header expression in nftables is a header name plus a field (ip saddr, tcp dport, vlan id, geneve vni). The kernel turns each one into a payload load that compares fixed bytes of the packet, so one generic engine can filter every protocol from Ethernet up to fields inside a GENEVE tunnel.
102.1 Why is it used?
- Match exact header fields: source IP, ports, TTL, TCP flags, ICMP sequence, ARP operation, VLAN ID.
- Filter traffic inside a tunnel (GRE, GENEVE) without decapsulating it.
- Unlike iptables, no separate kernel module per protocol: every field is just base + offset + length.
- Linux only (lab: nftables 1.0.9, kernel 6.18). The table family decides which headers you get: arp in the arp family, Ethernet/VLAN in netdev ingress.
102.2 How it works
- Step 1
1.Client 192.168.122.10 sends TCP SYN to port 80
- Step 2
2.Packet reaches the nft input hook in the server kernel
- Step 3
3.Rule compares ip saddr (network header + 12, 4 bytes) and tcp dport (transport header + 2, 2 bytes)
- Step 4
4.Match -> packet goes on to the web app
- Step 5
5.A packet for port 22 matches tcp dport 22 drop and stops there
- Step 1
1.Client wraps a ping in GENEVE (outer UDP dport 6081)
- Step 2
2.Packet hits the netdev ingress hook on server eth0
- Step 3
3.geneve vni 100 checks the tunnel ID
- Step 4
4.geneve ip daddr 172.16.100.80 reads the inner IP header
- Step 5
5.Inner ICMP echo-request is matched without decapsulation
102.3 From expression to bytecode
Lab: Docker network, client 192.168.122.10, server 192.168.122.80. SCTP, GENEVE and VLAN modules are loaded; socket, osf and xfrm are not built, fib/numgen/hash are modules but not loaded (so those are shown as tables).
nft --debug=netlink add rule inet filter \ input ip saddr 192.168.122.10 \ tcp dport 80 counterinet filter input [ meta load nfproto => reg 1 ] [ cmp eq reg 1 0x00000002 ] [ payload load 4b @ network header + 12 => reg 1 ] [ cmp eq reg 1 0x0a7aa8c0 ] [ meta load l4proto => reg 1 ] [ cmp eq reg 1 0x00000006 ] [ payload load 2b @ transport header + 2 => reg 1 ] [ cmp eq reg 1 0x00005000 ] [ counter pkts 0 bytes 0 ]- ip saddr = 4 bytes at network header + 12 (the IPv4 source address).
- tcp dport = 2 bytes at transport header + 2.
- nft adds the meta nfproto / meta l4proto checks itself: these are called dependencies.
102.4 IPv4, ICMP, TCP, UDP rules
nft add rule inet filter input \ ip ttl lt 64 icmp type echo-request counternft add rule inet filter input \ icmp type echo-request icmp sequence 2 dropnft add rule inet filter input \ tcp flags syn / syn,ack tcp dport 80 counternft add rule inet filter input \ tcp dport 22 dropnft add rule inet filter input \ udp dport 53 udp length gt 20 counternft add rule inet filter input \ ip length gt 1000 counterping -c 3 -W 1 192.168.122.80nc -zv -w 2 192.168.122.80 22pc$ ping -c 3 -W 1 192.168.122.8064 bytes from 192.168.122.80: icmp_seq=1 ttl=64 time=0.216 ms64 bytes from 192.168.122.80: icmp_seq=3 ttl=64 time=0.097 ms3 packets transmitted, 2 received, 33.3333% packet loss, time 2055mspc$ nc -w 2 192.168.122.80 80 </dev/nullsrv webpc$ nc -zv -w 2 192.168.122.80 22nc: connect to 192.168.122.80 port 22 (tcp) timed out: Operation in progresssrv$ nft list chain inet filter inputip ttl < 64 icmp type echo-request counter packets 1 bytes 84icmp type echo-request icmp sequence 2 droptcp flags syn / syn,ack tcp dport 80 counter packets 1 bytes 60tcp dport 22 dropudp dport 53 udp length > 20 counter packets 1 bytes 46ip length > 1000 counter packets 1 bytes 1228- icmp sequence 2 drop: of three pings, seq 2 was lost (33% loss).
- tcp flags syn / syn,ack counts only the first packet of a new connection.
- drop (not reject) makes nc time out instead of getting Connection refused.
102.5 ARP, Ethernet and IPv6 rules
nft add rule arp filter input \ arp operation request \ arp daddr ip 192.168.122.80 counternft add rule arp filter input \ arp operation reply counternft add rule arp filter input arp htype 1 \ arp ptype 0x0800 arp hlen 6 \ arp plen 4 counternft add rule inet filter input \ ether saddr 92:78:db:a8:42:4c \ ether type ip counter$ nft list table arp filterarp operation request arp daddr ip 192.168.122.80 counter packets 1 bytes 28arp operation reply counter packets 1 bytes 28arp htype 1 arp ptype ip arp hlen 6 arp plen 4 counter packets 3 bytes 84$ nft list chain inet filter inputether saddr 92:78:db:a8:42:4c ether type ip counter packets 2 bytes 168nft add rule inet filter input \ ip6 daddr ::1 ip6 hoplimit 1 dropnft add rule inet filter input \ icmpv6 type echo-request \ icmpv6 sequence 1 counternft add rule inet filter input \ ip6 nexthdr ipv6-icmp counterping -6 -c 2 -W 1 -t 1 ::1ping -6 -c 2 -W 1 ::1$ ping -6 -c 2 -W 1 -t 1 ::12 packets transmitted, 0 received, 100% packet loss, time 1009ms$ ping -6 -c 2 -W 1 ::164 bytes from ::1: icmp_seq=1 ttl=64 time=0.056 ms64 bytes from ::1: icmp_seq=2 ttl=64 time=0.075 ms2 packets transmitted, 2 received, 0% packet loss, time 1020ms$ nft list chain inet filter inputip6 daddr ::1 ip6 hoplimit 1 dropicmpv6 type echo-request icmpv6 sequence 1 counter packets 1 bytes 104ip6 nexthdr ipv6-icmp counter packets 4 bytes 416102.6 rt, SCTP, GENEVE and VLAN
- rt ip nexthop 192.168.122.1 (output chain) counts packets routed via the Docker gateway: the 2 pings to 8.8.8.8 counted, LAN pings did not (direct route).
- SCTP (telecom signalling) packets are made of chunks; sctp chunk init exists checks for an INIT chunk.
nft add rule inet filter output \ rt ip nexthop 192.168.122.1 counterip route get 8.8.8.8nft add rule inet filter input \ sctp dport 5060 sctp chunk init exists \ counterecho hello-sctp | \ socat - SCTP4:192.168.122.80:5060$ ip route get 8.8.8.88.8.8.8 via 192.168.122.1 dev eth0 src 192.168.122.80 uid 0$ nft list chain inet filter outputrt ip nexthop 192.168.122.1 counter packets 2 bytes 168srv$ cat /tmp/sctp.txthello-sctpsrv$ nft list chain inet filter inputsctp dport 5060 sctp chunk init exists counter packets 1 bytes 68sctp dport 5060 sctp chunk data exists counter packets 1 bytes 60sctp dport 5060 sctp chunk cookie-echo exists counter packets 1 bytes 264ip link add gnv0 type geneve id 100 \ remote 192.168.122.80ip link add link eth0 name eth0.100 \ type vlan id 100nft add rule netdev lab ingress \ udp dport 6081 geneve vni 100 counternft add rule netdev lab ingress \ udp dport 6081 geneve ip daddr \ 172.16.100.80 geneve icmp type \ echo-request counternft add rule netdev lab ingress \ vlan id 100 vlan type arp counterping -c 3 -W 1 172.16.100.80ping -c 1 -W 1 172.16.200.80pc$ ping -c 3 -W 1 172.16.100.803 packets transmitted, 3 received, 0% packet loss, time 2056mssrv$ nft list table netdev labtype filter hook ingress device "eth0" priority filter; policy accept;udp dport 6081 geneve vni 100 counter packets 4 bytes 508udp dport 6081 geneve ip daddr 172.16.100.80 geneve icmp type echo-request counter packets 3 bytes 402vlan id 100 vlan type arp counter packets 3 bytes 84102.7 Header fields by layer
| Header | Layer | Key fields |
|---|---|---|
| ether | L2 | saddr, daddr, type |
| vlan | L2 | id (12 bit), dei, pcp, type (inet needs reorder_hdr off; netdev ingress is better) |
| arp | L2 | htype, ptype, hlen, plen, operation, saddr/daddr ether|ip |
| ip | L3 | version, hdrlength, dscp, ecn, length, id, frag-off, ttl, protocol, checksum, saddr, daddr |
| icmp / igmp | L3 | type, code, id, sequence, gateway, mtu / type, mrt, group |
| ip6 / icmpv6 | L3 | flowlabel, nexthdr, hoplimit / packet-too-big, taddr, max-delay |
| tcp | L4 | sport, dport, sequence, ackseq, doff, flags, window, checksum, urgptr |
| udp / udplite | L4 | sport, dport, length, checksum |
| sctp / dccp | L4 | vtag, chunk NAME exists (18 chunk types) / type |
| ah / esp / comp | IPsec | spi, sequence (ESP shows only these, rest is encrypted) / cpi |
| gre / geneve | Tunnel | gre protocol, gre ip daddr / geneve vni, geneve ip|tcp|udp FIELD |
102.8 Special expressions: socket, osf, fib, numgen, hash
| Expression | What it does |
|---|---|
| socket transparent | mark | wildcard | cgroupv2 | Does the packet belong to a local socket |
| osf name "Linux" | Passive OS fingerprint from the SYN |
| fib saddr . iif oif | Routing lookup, reverse path check |
| fib daddr type local | Address type of the destination |
| rt classid | nexthop | mtu / rt ipsec exists | Route info of the packet |
| ipsec in|out reqid|spi | IPsec policy info |
| numgen inc|random mod N, jhash ... mod N | Numbers for load balancing, used with a map in DNAT |
102.9 Common mistakes
- Thinking ip6 nexthdr tcp matches every IPv6 TCP packet: with an extension header (hop-by-hop, fragment) nexthdr holds that header's number and the rule misses. Use meta l4proto tcp or tcp dport.
- Matching geneve fields without udp dport 6081 first.
- Using vlan fields in an inet table with reorder_hdr on; use netdev ingress.
- Using socket/osf/fib without checking kernel support (CONFIG_NFT_*).
102.10 Interview tips
- nft compiles a rule to bytecode; each header field becomes payload load (base header, offset, length) + cmp. Show it with nft --debug=netlink.
- One generic engine, no module per protocol (unlike iptables matches).
- To block an IP inside a GENEVE tunnel: netdev ingress, udp dport 6081 geneve ip saddr X drop.
- For the L4 protocol in IPv6 prefer meta l4proto over ip6 nexthdr.