Skip to content

Lesson 5 of 9 · 28m

Netfilter and nftables: Where Packets Actually Get Dropped

Map tables, chains, hooks, priorities, and conntrack so firewall evidence replaces firewall superstition.

Every operator eventually meets the firewall that 'is definitely disabled' while nft list ruleset contains four included files, a Docker masquerade chain, and a security group doing the real work outside the host. Netfilter is not a single firewall. It is a packet-processing framework; nftables, legacy iptables compatibility, conntrack helpers, NAT, and several applications all hang policy on its hooks.

Build the packet-path mental model

Netfilter hooks exist at fixed positions: prerouting, input, forward, output, and postrouting. Local processes interact with input and output. Forwarded traffic crosses prerouting, forward, and postrouting. NAT usually happens in prerouting for destination NAT and postrouting for source NAT.

network -> prerouting -> routing decision
                         |- local delivery -> input -> process
                         '- forward -> postrouting -> network
process -> output -> routing -> postrouting -> network

If you cannot say whether the traffic is input or forward, you cannot reliably place or debug the rule.

Inspect the real ruleset

Use nftables even on systems with iptables compatibility shims:

sudo nft list ruleset
sudo nft list chain inet filter input
sudo nft list table ip nat

A chain with a policy is the final word only after earlier verdicts:

table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;
        ct state established,related accept
        iifname "lo" accept
        tcp dport 22 accept
        udp dport 51820 accept
    }
}

The order matters. ct state established,related accept is usually the quiet hero that makes return traffic work. If it is missing or comes after a broad drop, perfectly healthy outbound connections fail in both directions.

Count before you conclude

Counters turn argument into evidence:

tcp dport 443 counter packets 0 bytes 0 accept
ip saddr 192.0.2.0/24 counter packets 0 bytes 0 drop

Reload or insert temporary counters near decision points, generate traffic, then inspect increments. A zero counter means traffic did not match that rule — or never reached the hook. A nonzero accept counter followed by no application traffic means the failure is later: routing, conntrack, another table, or the service itself.

NAT belongs to the routing story

Masquerade rewrites the source address as packets leave:

table ip nat {
    chain postrouting {
        type nat hook postrouting priority 100;
        oifname "eth0" ip saddr 10.8.0.0/24 masquerade
    }
}

Without this, private sources leak onto a network that has no route back. With it in the wrong place, replies arrive at the host but never un-translate to the original client. NAT success is stateful: verify conntrack entries rather than assuming the rule fired once and stayed correct.

sudo conntrack -L | head -20

Field notes

  • iptables -L can hide nft-native tables and chains; use nft list ruleset for the host-level picture.
  • Docker, Kubernetes, firewalld, and libvirt all install overlapping netfilter rules; ownership matters before deletion.
  • priority decides chain ordering when multiple base chains share a hook.
  • A rule logging drops is often more useful than changing policy during diagnosis.
  • Stateful firewalling means the first packet and reply packet are not treated symmetrically; debug the flow, not individual packets.

Was this lesson helpful?

Sign in to rate