skip to content

In Linux netfilter — the kernel framework behind both iptables and nftables — what are the filter, nat and mangle tables for, and what changes about a packet's fate depending on which of them a rule lives in?

level: juniorimportance: should knowfreq 58%

answer

  1. rules grouped by what they do
  2. accept/drop versus rewrite versus relabel
  3. one table runs before conntrack
  4. only new flows reach the rewriting table
  5. priority decides who sees it first

basics

~20 s

Netfilter tables group rules by the kind of work they do: filter decides whether a packet is accepted or dropped, nat rewrites source or destination addresses and ports, and mangle alters packet fields such as TTL, DSCP or the firewall mark.

solid answer

~50 s

A netfilter table is a container of chains, and its name tells you what work its rules are meant to do and at what priority they run at each hook. `filter` is the firewall proper — it returns accept, drop or reject verdicts. `nat` rewrites addresses and ports, and it is special because its chains are only traversed by the first packet of each tracked flow; every later packet of that flow is translated automatically by connection tracking. `mangle` changes packet fields and metadata — TTL, DSCP, and the firewall mark that policy routing keys on. Two more exist: `raw`, which runs before connection tracking and is mainly used to exempt traffic from being tracked, and `security` for SELinux packet labels. In nftables the tables are user-named and the hook and priority are declared explicitly, but the same conventional ordering is reproduced.

go deeper

for a junior

Be able to say plainly that filter accepts or drops, nat rewrites addresses and ports, and mangle edits fields such as TTL or a mark. Naming the three and their purpose is the whole ask here.

for a middle

Explain that a table is chains plus a priority at a hook, and that nat chains are only traversed by the first packet of a flow, which is why filtering does not belong there.

for a senior

Show you place rules by purpose and ordering, not habit: know the priority sequence at a shared hook and be able to explain a rule that looks correct but never fires because it sits in the wrong table.

for a principal

Own the convention. Decide whether a fleet standardises on nftables' explicit hook/priority declarations or on the classic table names, and what that means for tooling, auditing and any software that installs its own rules.

## What a table actually is Netfilter is the packet-processing framework inside the Linux kernel. It exposes a small number of *hook points* in the network path, and userspace tools (`iptables`, `nft`, and everything built on them such as firewalld) register rules at those hooks. A **table** is simply a named container of chains, and each chain is attached to one hook. The table name carries two pieces of meaning: what kind of verdict its rules are allowed to produce, and the priority at which its chains run relative to other tables at the same hook. In classic iptables the table set is fixed: `filter`, `nat`, `mangle`, `raw` and `security`. In nftables you create tables with any name you like and declare the hook and priority yourself, so the split is a convention rather than a kernel-imposed structure — but the conventional priorities are the same, which is why translated rulesets behave identically. ## filter — accept or drop This is the firewall in the everyday sense, and it is the default table when a table is not named. It has three chains: `INPUT` for packets addressed to a local process, `FORWARD` for packets the host is routing on behalf of someone else, and `OUTPUT` for packets a local process generated. Verdicts here are terminal: accept, drop (silently discard) or reject (discard and send an ICMP or TCP error). The commonest beginner mistake is to write a rule in `FORWARD` when the traffic is actually addressed to the host itself, and then wonder why nothing was blocked. ## nat — rewrite addresses and ports The `nat` table holds the address-translation verdicts: destination rewriting (`DNAT`, and `REDIRECT` as the special case that points at the local machine) and source rewriting (`SNAT` and `MASQUERADE`). Its chains sit at `PREROUTING`, `OUTPUT`, `POSTROUTING` and `INPUT`. The important semantic — and a favourite interview probe — is that **nat chains are not evaluated for every packet**. They are consulted only for packets whose connection-tracking state is NEW, that is the first packet of a flow. Once a translation is recorded in the conntrack entry, the kernel's NAT engine applies it to every subsequent packet of that flow, in both directions, without re-running any rule. Two consequences follow. First, you never write a reverse rule for the replies; the reverse mapping is implied. Second, the nat table is a bad place to put filtering logic, because most packets never traverse it. ## mangle — change the packet, not its fate `mangle` is present at all five hooks and is for modifying packet header fields and kernel-side metadata: setting TTL/hop limit, setting DSCP/TOS for quality-of-service, and above all setting the *firewall mark* (fwmark), a number attached to the packet or to its conntrack entry. The mark is the standard bridge between the firewall and the routing layer, since policy routing rules can select a routing table by mark. ## raw and security `raw` runs at the earliest priority of all — before connection tracking has created an entry. That is its whole point: it is where you exempt traffic from being tracked, which matters on very high-rate boxes where tracking every flow is expensive. `security` is used by SELinux for packet labelling (SECMARK) and is rarely touched by hand. ## Ordering at a shared hook Several tables can register at the same hook, so the priority decides who sees the packet first. The conventional order, lowest number first, is: raw (-300), mangle (-150), destination NAT (-100), filter (0), security (50), source NAT (100). nftables makes this explicit in the chain declaration: ``` table inet myfw { chain input { type filter hook input priority 0; policy drop; } } ``` That one line encodes what iptables expressed by the table name alone: which hook, what kind of chain, and where in the priority order it sits. ## Why the distinction is worth knowing Picking the wrong table produces rules that are syntactically valid and quietly ineffective. A drop placed in `nat` is skipped by every packet after the first. A destination rewrite placed after the routing decision cannot influence where the packet is routed. A mark set in `filter` on the wrong hook is too late for the routing lookup that was supposed to use it. Understanding tables as *purpose plus priority*, rather than as arbitrary namespaces, is what makes rule placement predictable.

  • Where does the raw table sit relative to connection tracking, and why would you ever use it?
    `raw` registers at the lowest priority at each hook, before conntrack creates an entry. Its main use is exempting selected traffic from connection tracking on high-throughput hosts, where creating and expiring an entry per flow costs memory and CPU and can exhaust the conntrack table. Untracked packets then have no state, so any rules relying on ESTABLISHED matching will not apply to them.
  • What is a firewall mark, and which table normally sets it?
    A firewall mark (fwmark) is an integer the kernel attaches to a packet, or to its conntrack entry, that carries no meaning on the wire — it exists only inside this host. It is normally set in `mangle`. Its usual consumer is policy routing: a rule can select a different routing table based on the mark, which is how source-based or application-based routing is wired up.
  • Does nftables still have filter, nat and mangle tables?
    Not as a kernel-imposed set. In nftables you create tables with arbitrary names and declare each base chain's family, hook and priority yourself. Conventional configurations still use those names and the same priority values, so behaviour matches, and the `iptables` compatibility front-end creates tables with exactly those names when it translates classic rules.

saying these in an interview costs you the question

  • Thinks the nat table filters traffic like filter does
  • Believes nat rules are evaluated for every packet
  • Thinks a reverse rule is needed for NAT replies
  • Calls mangle a place to accept or drop packets
  • Assumes only one table can attach to a hook

context