netfilter is the packet processing framework inside the Linux kernel that lets code inspect, modify, drop or redirect network packets at fixed points called hooks along their path through the networking stack. In Linux networking, it is the foundation beneath iptables, nftables, firewalld and UFW: those tools only configure rules, while netfilter does the actual work in kernel space.
The framework was introduced with the Linux 2.4 kernel and is maintained by the netfilter project. Beyond filtering, it provides connection tracking, network address translation (NAT) and packet mangling, which makes it the core of almost every Linux firewall, router and container network.
How netfilter hooks intercept packets
netfilter works by placing callback hooks at five points in the IPv4 and IPv6 stack. Kernel modules register functions on these hooks, and each packet passing a hook is handed to the registered functions in priority order. Each function returns a verdict such as accept, drop, queue to user space or stolen.
The path a packet takes depends on its destination. Incoming traffic for the local host passes PREROUTING and INPUT, forwarded traffic passes PREROUTING, FORWARD and POSTROUTING, and locally generated traffic passes OUTPUT and POSTROUTING. The table shows each hook and its typical use.
| Hook | Position in the stack | Typical use |
|---|---|---|
| PREROUTING | Right after a packet arrives, before routing | Destination NAT, early drops |
| INPUT | After routing, for the local host | Protecting local services |
| FORWARD | After routing, for other hosts | Router and container filtering |
| OUTPUT | Locally generated packets | Egress control, local DNAT |
| POSTROUTING | Just before transmission | Source NAT and masquerading |
Besides the IP families, netfilter also offers hooks for ARP and bridge traffic, used by arptables and ebtables, and an ingress hook attached to a single network device that runs before PREROUTING.
Core subsystems built on netfilter
netfilter is not a single feature but a set of cooperating subsystems that share the hook infrastructure. The most important one is conntrack, which records every flow and assigns packets a state such as NEW, ESTABLISHED, RELATED or INVALID.
Connection tracking is what makes stateful firewalling possible: a rule can accept replies to established connections without listing every return port. NAT depends on conntrack too, because the kernel must remember the original addresses to translate replies back. The following table lists the main subsystems.
| Subsystem | Function | User-space tool |
|---|---|---|
| Connection tracking | Keeps state per flow | conntrack |
| NAT | Rewrites addresses and ports | iptables -t nat, nft |
| Packet filtering | Accepts or drops packets | iptables, nft |
| Mangle | Changes marks, TTL or DSCP | iptables -t mangle, nft |
| NFQUEUE | Passes packets to user space | Intrusion prevention daemons |
| NFLOG | Sends packet logs to user space | ulogd |
netfilter, iptables and nftables compared
The most common confusion is treating netfilter and iptables as the same thing. netfilter is the kernel framework; iptables is a user-space front end and a kernel table format that uses it. Since roughly 2014, nftables has been the designated successor, offering a single tool for IPv4, IPv6, ARP and bridge rules and a small virtual machine in the kernel that evaluates rule bytecode.
Many distributions now ship iptables-nft, a compatibility layer that accepts classic iptables syntax but writes nftables rules underneath. Higher-level managers such as firewalld and UFW generate rules for either back end.
| Component | Layer | Role |
|---|---|---|
| netfilter | Kernel | Hooks, conntrack, NAT engine |
| iptables | Legacy user space and kernel tables | Per-family rule management |
| nftables | Modern user space and kernel VM | Unified rule language and sets |
| firewalld | Management daemon | Zones and runtime changes |
| UFW | Simplified front end | Host firewall for beginners |
Performance and configuration considerations
netfilter performance depends mainly on rule count, rule structure and conntrack load. Classic iptables evaluates rules linearly, so thousands of entries slow every packet; nftables and the ipset extension solve this with hashed sets and maps that match in near constant time.
The conntrack table has a fixed maximum size, set by nf_conntrack_max. On busy gateways or under a SYN flood, the table can fill, after which new connections are dropped and the kernel logs a table full message. Raising the limit costs memory, so administrators size it against expected concurrent flows.
| Parameter | What it controls | Effect of a wrong value |
|---|---|---|
| nf_conntrack_max | Maximum tracked flows | Dropped connections when full |
| Established timeout | Lifetime of idle TCP entries | Stale entries or broken long sessions |
| Hook priority | Order of chain evaluation | Rules bypassed or applied twice |
| NOTRACK rules | Skips tracking for chosen traffic | Lost state for NAT if misapplied |
For extreme packet rates, filtering can be moved earlier than netfilter using XDP and eBPF, which run in the driver. netfilter remains the better choice when stateful rules and NAT are required.
Common mistakes when working with netfilter
Most production incidents involving netfilter come from rule ordering and mixed tooling, not from kernel defects. The list below covers the errors seen most often.
- Mixing iptables-legacy and nftables on one host, which creates two rule sets that both apply and are hard to audit.
- Forgetting the FORWARD chain when a host routes traffic for containers or virtual machines.
- Applying a default drop policy remotely before allowing SSH, which locks the administrator out.
- Ignoring IPv6, leaving services exposed while only IPv4 rules exist.
- Not persisting rules, so the firewall disappears after reboot.
Container runtimes and Kubernetes components also insert their own chains automatically. Before flushing or replacing a rule set on such a host, administrators should inspect it with nft list ruleset and confirm which software owns each chain, because a careless flush can silently break service routing for every container on the node.
The short version
Picture netfilter as a set of checkpoints on the roads inside a Linux computer. Every piece of network data must pass several checkpoints on its way in, through or out. At each one, guards follow a list of instructions: let it through, turn it away or change the address label.
Tools like iptables and nftables are simply the ways administrators write those instructions. netfilter is the system of checkpoints that actually enforces them.
Where it makes a difference
A small office uses a Linux machine as its internet gateway for 40 employees. The administrator configures nftables so that netfilter applies source NAT in POSTROUTING, letting all internal devices share one public IP address, and a filter in FORWARD that allows outgoing traffic but only accepts incoming packets in the ESTABLISHED or RELATED state.
A port forward in PREROUTING sends inbound TCP port 443 to an internal web server. During a busy week, users report failed connections; the kernel log shows the conntrack table is full at its default size. Raising nf_conntrack_max and shortening the timeout for idle UDP flows resolves the drops without new hardware.