nft (nftables) (4/4)
Expressions read a value from the packet or connection; statements decide what to do with it (verdict, NAT, counter, log, set update). This last nftables part covers tunnel headers (VXLAN, GRETAP), raw payload and options, conntrack, verdicts, packet-changing statements, sets/maps and nft monitor.
103.1 Why is it used?
- Filter traffic inside VXLAN/GRETAP tunnels without decapsulation.
- Match any bits with raw payload when nft has no name for a field.
- Control rule flow between chains with jump, goto and return.
- Rewrite packets: DNAT/SNAT, DSCP, MSS clamping, marks.
- Build dynamic rules with sets (auto-ban a port scanner) and debug with nft monitor trace.
103.2 vxlan and gretap: matching inside the tunnel
- Step 1
1.pc (192.168.123.20) pings 10.0.0.1 over vx0
- Step 2
2.Ping is wrapped in VXLAN, sent on UDP 4789 to gw eth0
- Step 3
3.From outside it is just a UDP packet
- Step 4
4.netdev ingress chain reads the inner header with vxlan ...
- Step 5
5.Rule matches VNI 100 and inner ip daddr 10.0.0.1 -> counter goes up
GRETAP is the same idea, only the wrapper is GRE
nft add rule netdev tun in udp dport \ 4789 vxlan vni 100 counternft add rule netdev tun in udp dport \ 4789 vxlan ip daddr 10.0.0.1 counternft add rule netdev tun in udp dport \ 4789 vxlan tcp dport 80 counternft add rule netdev tun in \ gretap icmp type echo-request counternft add rule netdev tun in \ gretap vni 100 counter# pc: vx0 10.0.0.2, gt0 10.9.9.2ping -c 3 -W 1 10.0.0.1 | tail -2nc -w 2 10.0.0.1 80 </dev/nullping -c 2 -W 1 10.9.9.1 | tail -2nft list table netdev tun$ ping -c 3 -W 1 10.0.0.1 | tail -23 packets transmitted, 3 received, 0% packet loss, time 2054ms$ nc -w 2 10.0.0.1 80 </dev/nullgw web$ nft list table netdev tun type filter hook ingress device "eth0" priority filter; policy accept; udp dport 4789 vxlan vni 100 counter packets 9 bytes 1030 udp dport 4789 vxlan ip daddr 10.0.0.1 counter packets 7 bytes 818 udp dport 4789 vxlan tcp dport 80 counter packets 4 bytes 416 gretap ip daddr 10.9.9.1 counter packets 2 bytes 244 gretap icmp type echo-request counter packets 2 bytes 244$ nft add rule netdev tun in gretap vni 100 counterError: syntax error, unexpected vniadd rule netdev tun in gretap vni 100 counter ^^^- VXLAN: first match udp dport 4789, then vni, flags, or inner ether / vlan / ip / ip6 / tcp / udp fields.
- GRETAP carries an Ethernet frame inside GRE and uses the same kinds of inner fields; gretap vni is a syntax error in nft 1.0.9.
103.3 Raw payload and options
| Expression | Meaning |
|---|---|
| @th,16,16 | 16 bits from bit 16 of the transport header = destination port (listed as th dport) |
| @nh,72,8 | Protocol byte of the IPv4 header (listed as ip protocol) |
| @ll,96,16 | Ethernet type field |
| @ih,0,8 | Payload after the L4 header |
| tcp option maxseg size / sack-perm exists | TCP options in the SYN |
| ip option rr exists | IPv4 record route option |
| exthdr hbh|frag|rt|dst|mh exists|missing | IPv6 extension header present or not |
nft 'add rule inet lab in meta l4proto \ { tcp, udp } @th,16,16 { 53, 80 } counter'nft add rule inet lab in meta nfproto \ ipv4 @nh,72,8 6 counternft add rule netdev tun in \ @ll,96,16 0x0800 counternft 'add rule inet lab in tcp option \ maxseg size > 0 counter'nft add rule inet lab in tcp option \ sack-perm exists counternft add rule inet lab in tcp option \ maxseg size lt 536 counter dropnft add rule inet lab in ip option \ rr exists counter drop# pc: MSS 500 wala SYN, phir record routeip route add 192.168.123.10/32 dev eth0 \ advmss 500time nc -w 2 192.168.123.10 80 </dev/nullip route del 192.168.123.10/32ping -c 2 -W 1 -R 192.168.123.10 | tail -2$ nft list chain inet lab in meta l4proto { tcp, udp } th dport { 53, 80 } counter packets 6 bytes 336 ip protocol tcp counter packets 6 bytes 336$ nft list table netdev tun @ll,96,16 0x800 counter packets 11 bytes 1206$ time nc -w 2 192.168.123.10 80 </dev/nullCommand exited with non-zero status 1real 0m 2.00s$ ping -c 2 -W 1 -R 192.168.123.10 | tail -22 packets transmitted, 0 received, 100% packet loss, time 1025ms$ nft list chain inet lab in tcp option maxseg size > 0 counter packets 3 bytes 180 tcp option sack-perm exists counter packets 3 bytes 180 tcp option maxseg size < 536 counter packets 2 bytes 120 drop ip option rr exists counter packets 2 bytes 248 drop- With raw payload you write the dependency (meta l4proto / nfproto) yourself.
- advmss 500 on the pc made the SYN carry MSS 500, so maxseg size lt 536 dropped it and nc failed after 2 s.
103.4 Conntrack expression
- ct state established,related / ct direction / ct status dnat / ct mark / ct label / ct expiration / ct count over 10.
- ct original|reply ip saddr|daddr and proto-src/proto-dst read either direction of the connection.
- In this lab nft_ct is a module (=m) that is not loaded, so conntrack -L shows the same table the ct expression reads.
$ grep -c '^nft_ct ' /proc/modules0$ zcat /proc/config.gz | grep 'CONFIG_NFT_CT='CONFIG_NFT_CT=m103.5 Verdicts: jump, goto, return
- Step 1
1.web (192.168.123.30) pings gw: input chain jump from_lan
- Step 2
2.from_lan: ip saddr 192.168.123.30 -> return
- Step 3
3.Back in input at the next rule after the jump
- Step 4
4.ip saddr 192.168.123.30 icmp echo-request -> drop
- Step 5
5.pc's ping instead hits accept inside from_lan
goto never comes back: no verdict there means the base chain policy
nft add rule inet lab from_lan \ ip saddr 192.168.123.30 counter returnnft add rule inet lab from_lan \ icmp type echo-request counter acceptnft add rule inet lab in \ ip saddr 192.168.123.0/24 jump from_lannft add rule inet lab in ip saddr \ 192.168.123.30 icmp type echo-request \ counter dropnft insert rule inet lab in ip saddr \ 192.168.123.30 tcp dport 80 goto web_only# pc aur web dono gw ko ping karte hainping -c 2 -W 1 192.168.123.10 | tail -2pc$ ping -c 2 -W 1 192.168.123.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1006msweb$ ping -c 2 -W 1 192.168.123.10 | tail -22 packets transmitted, 0 received, 100% packet loss, time 1023ms$ nft list chain inet lab from_lan ip saddr 192.168.123.30 counter packets 2 bytes 168 return icmp type echo-request counter packets 2 bytes 168 accept$ nft list chain inet lab in ip saddr 192.168.123.30 icmp type echo-request counter packets 2 bytes 168 dropweb$ nc -w 2 192.168.123.10 80 </dev/nullgw web$ nft list chain inet lab web_only tcp dport 80 counter packets 4 bytes 216 accept103.6 Statements that change a packet
# gw: forward hook (pc se web, DNAT ke baad)nft add rule ip natlab mangle ip daddr \ 192.168.123.30 ip dscp set 46 \ meta mark set 0x1nft add rule ip natlab mangle tcp flags \ syn tcp option maxseg size set 1360nft add rule ip natlab mangle \ meta mark 0x1 counter# web: SYN capturetimeout 6 tcpdump -ni eth0 -c 1 -v \ "tcp port 80 and tcp[tcpflags] == tcp-syn" \ 2>&1 | grep -E "tos|mss|captured"nft list chain ip natlab mangleIP (tos 0xb8, ttl 63, id 22935, offset 0, flags [DF], proto TCP (6), length 60) 192.168.123.10.40056 > 192.168.123.30.80: Flags [S], cksum 0x77a8 (incorrect -> 0x5c02), seq 3894323184, win 64240, options [mss 1360,sackOK,TS val 339818715 ecr 0,nop,wscale 10], length 01 packet captured$ nft list chain ip natlab mangle ip daddr 192.168.123.30 ip dscp set ef meta mark set 0x00000001 tcp flags syn tcp option maxseg size set 1360 meta mark 0x00000001 counter packets 4 bytes 216| Statement | What it does |
|---|---|
| ip dscp set 46 / meta mark set 0x1 | Rewrite DSCP (EF, tos 0xb8) / mark the packet |
| tcp option maxseg size set 1360 | rt mtu | MSS clamping |
| log prefix "SSH: " level warn | Write to kernel log, non-terminating |
| reject with icmp|icmpv6|icmpx ... / tcp reset | Drop and send an error back |
| ct mark set meta mark / ct zone set / notrack | Connection mark, zone, or no tracking |
| limit rate over 10/second drop | Token bucket rate limit |
| queue to 3 / dup to 10.2.3.4 / fwd to "eth1" | To userspace (NFQUEUE) / copy / forward (netdev) |
103.7 NAT statements
- Step 1
1.pc connects to gw:8080 (SYN, mss 1460)
- Step 2
2.prerouting: dnat to 192.168.123.30:80
- Step 3
3.forward: DSCP and MSS rewritten
- Step 4
4.postrouting: snat/masquerade -> source becomes gw 192.168.123.10
- Step 5
5.web replies to gw; conntrack reverses both changes and sends it to pc
# gw: nat chains pre + post (natlab)nft add rule ip natlab pre tcp dport 8080 \ dnat to 192.168.123.30:80nft add rule ip natlab post ip daddr \ 192.168.123.30 tcp dport 80 counter \ masquerade# pcnc -w 2 192.168.123.10 8080 </dev/null# gw: masquerade ki jagah snatnft add rule ip natlab post ip daddr \ 192.168.123.30 tcp dport 80 counter \ snat to 192.168.123.10:40000-40100conntrack -L -p tcp --dport 8080 2>&1 \ | head -3$ nc -w 2 192.168.123.10 8080 </dev/null; echo "exit $?"exit 1$ nc -w 2 192.168.123.10 8080 </dev/nullweb saw 192.168.123.10$ conntrack -L -p tcp --dport 8080 2>&1 | head -3tcp 6 114 TIME_WAIT src=192.168.123.20 dst=192.168.123.10 sport=45186 dport=8080 src=192.168.123.30 dst=192.168.123.10 sport=80 dport=45186 [ASSURED] mark=0 use=1tcp 6 117 TIME_WAIT src=192.168.123.20 dst=192.168.123.10 sport=45188 dport=8080 src=192.168.123.30 dst=192.168.123.10 sport=80 dport=40015 [ASSURED] mark=0 use=1- NAT statements work only in chains of type nat; exit 1 before the rule, then web saw 192.168.123.10.
- Port 40015 in the reply direction is inside the snat range 40000-40100.
- Also: dnat to tcp dport map { ... }, masquerade to :10000-20000, redirect to :2222, flags persistent/random/fully-random, tproxy, synproxy (not in this lab kernel).
103.8 Sets, maps and vmap
nft 'add set inet lab blackhole { type \ ipv4_addr; flags dynamic, timeout; \ timeout 1m; }'nft add rule inet lab in ip saddr \ @blackhole counter dropnft 'add rule inet lab in tcp dport 23 \ add @blackhole { ip saddr } counter drop'nft 'add rule inet lab in tcp dport vmap \ { 22 : drop, 80 : accept }'# pc: ping, port 23, phir pingnc -w 2 192.168.123.10 23 </dev/nullnft list set inet lab blackholepc$ ping -c 1 -W 1 192.168.123.10 | tail -21 packets transmitted, 1 received, 0% packet loss, time 0mspc$ nc -w 2 192.168.123.10 23 </dev/nullpc$ ping -c 1 -W 1 192.168.123.10 | tail -21 packets transmitted, 0 received, 100% packet loss, time 0ms$ nft list set inet lab blackhole flags dynamic,timeout elements = { 192.168.123.20 expires 56s240ms }- Probing telnet (23) added the pc to the dynamic set, so its next ping was dropped for 1 minute.
- vmap picks a verdict directly by key; xt target "DNAT" lines are rules made by iptables-nft (Docker DNS).
- add @seen { ip saddr comment "auto" } fails in nft 1.0.9: Key expression comments are not supported.
103.9 nft monitor and trace
timeout 4 nft monitor > /tmp/mon.txt 2>&1 &nft add rule inet lab in udp dport 5000 \ counternft -a list chain inet lab in | grep 5000nft delete rule inet lab in handle 25cat /tmp/mon.txt# trace: prerouting chain, priority -350nft add rule inet lab pre ip saddr \ 192.168.123.20 icmp type echo-request \ meta nftrace set 1timeout 5 nft monitor trace \ > /tmp/trace.txt 2>&1 &# filters: new, destroy, tables, chains,# sets, rules, elements, ruleset# JSON: nft -j monitor destroy rules$ cat /tmp/mon.txtadd rule inet lab in udp dport 5000 counter packets 0 bytes 0# new generation 52 by process 24577 (nft)delete rule inet lab in handle 25# new generation 53 by process 24585 (nft)$ cat /tmp/trace.txttrace id 9684bd9b inet lab pre rule ip saddr 192.168.123.20 icmp type echo-request meta nftrace set 1 (verdict continue)trace id 9684bd9b inet lab pre policy accepttrace id 9684bd9b ip natlab pre policy accepttrace id 9684bd9b inet lab in policy accept103.10 Comparison: jump vs goto vs return
| Verdict | Enters another chain | Comes back | If no verdict there |
|---|---|---|---|
| jump | Yes | Yes, to the next rule of the caller | Continue in caller |
| goto | Yes | No | Base chain policy applies |
| return | No | Returns to the caller | - |
| accept / drop | No | - | Ends this base chain (accept) / packet gone (drop) |
103.11 Common mistakes
- Thinking accept is a final pass: it only ends the current base chain; another base chain or later hook can still drop it (the trace went through three chains).
- Using dnat/snat in a filter-type chain: NAT needs type nat chains.
- Forgetting the dependency (meta l4proto) with raw @th payload.
- Editing the iptables-nft managed table ip nat by hand (Docker rules).
103.12 Interview tips
- jump = evaluate chain and return to caller; goto = never return, base policy if no verdict.
- return after a jump continues at the next rule of the calling chain.
- accept is per base chain; a drop anywhere wins.
- Debug with meta nftrace set 1 + nft monitor trace; watch config changes with nft monitor.
- Dynamic set + add @set { ip saddr } = self-updating blocklist.