Linux / OS Fundamentals Series

🧱 iptables (netfilter) from the "Top 1%" Perspective: The 5 Checkpoints Every Packet Passes Through, and What MASQUERADE Really Does

Introduction

This article is part of the Top 1% Series’ full article guide. It’s a spin-off from the iptables commands in the self-built L2TP/IPsec server hands-on lab that open ports for IKE/ESP/L2TP and set up MASQUERADE for clients.

Prerequisites

Getting the Big Picture

In a nutshell

The iptables command isn’t a program that processes rules directly — it’s purely a configuration tool that registers rules with the Linux kernel’s netfilter packet-filtering mechanism, saying “when a packet like this arrives, handle it like this.” The kernel itself is what actually inspects each packet and decides ACCEPT/DROP; iptables has nothing further to do with it once the command finishes running.

graph LR
    subgraph User["User space"]
        Cmd["The iptables command<br/>(assembles rules)"]
    end
    subgraph Kernel["Kernel space"]
        NF["The netfilter framework<br/>(actually inspects packets)"]
    end
    Cmd -->|"Registers rules with the kernel via netlink"| NF
    Packet["Every packet that passes through"] -->|"Inspected one by one"| NF

Fundamentals, Thoroughly Explained

The 5 checkpoints (hook points) a packet passes through

netfilter provides 5 fixed points (hook points) along the path an IP packet travels through the kernel’s network stack, and rules can be applied to every packet passing through each one.

graph LR
    In["Packet arrives"] --> PRE["PREROUTING<br/>(before the routing decision)"]
    PRE --> Route{"Is the destination this host itself,<br/>or somewhere to forward to?"}
    Route -->|"Destined for this host"| INPUT["INPUT"]
    INPUT --> Local["A local process<br/>(strongSwan, sshd, etc.)"]
    Local --> OUTPUT["OUTPUT<br/>(a response generated locally)"]
    Route -->|"Forwarded elsewhere"| FWD["FORWARD"]
    OUTPUT --> POST["POSTROUTING<br/>(right before it's sent out)"]
    FWD --> POST
    POST --> Out["Packet sent out"]

Each command used in the L2TP/IPsec lab can be sorted by which hook it concerns:

ChainPackets that pass through itUse in the lab
INPUTPackets destined for this server itselfLetting the server itself receive UDP 500/4500/1701 and ESP
FORWARDPackets forwarded through this server to somewhere elseLetting L2TP clients’ virtual IP range (10.10.10.0/24) traffic flow
POSTROUTINGPackets right before they’re sent outTranslating clients’ virtual IPs into the server’s real IP (MASQUERADE) to send them onto the internet

Confusing INPUT and FORWARD leads directly to the classic trap: “I allowed connections to the server itself, but client traffic can’t get out” (or the reverse). The core of the distinction between these two chains comes down to: is it addressed to my own machine, or just passing through on its way somewhere else?

Tables: same hook point, sorted by “what to do”

netfilter has multiple tables, each with a different role — “filtering (pass/drop),” “address translation,” and so on. At a single hook point, rules from multiple tables get evaluated in a fixed order.

TableMain purposeMain chains
filter (default)Deciding whether to pass or drop a packetINPUT / FORWARD / OUTPUT
natTranslating source/destination addressesPREROUTING / POSTROUTING / OUTPUT
mangleRewriting header fields (TTL, TOS, etc.)All chains
rawSpecial handling, e.g., excluding a packet from connection tracking (below)PREROUTING / OUTPUT

sudo iptables -A INPUT -p udp --dport 500 -j ACCEPT from the lab adds to the filter table (the default when no table is specified); sudo iptables -t nat -A POSTROUTING ... -j MASQUERADE explicitly targets the nat table.

Rules are evaluated top-to-bottom, and the first match wins

A single chain can hold multiple rules, and a packet is checked against them from top to bottom; the target (ACCEPT/DROP/REJECT/etc.) of the first matching rule decides that packet’s fate. If no rule matches, the chain’s default policy (often DROP or ACCEPT) applies.

Order matters because a broad rule written earlier can shadow a narrower exception written later. If a “deny everything” rule sits too close to the top, any allow rules written after it never get evaluated at all.

Connection tracking (conntrack): the real mechanism behind a “stateful” firewall

netfilter includes a subsystem called conntrack that tracks the state of connections passing through it. This enables stateful decisions — not just “allow packets to this port,” but “only allow packets that are part of an already-established connection.”

StateMeaning
NEWThe first packet of this connection (a brand-new connection)
ESTABLISHEDA packet belonging to an existing connection where two-way traffic is already flowing
RELATEDA packet in a separate connection, but related to an existing one (e.g., an FTP data connection)
INVALIDA packet that doesn’t match any tracked connection — malformed or unexpected

Using this, you can write a safer rule (-m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT) — for example, “only allow FORWARD traffic that’s a response to something the client initiated, and reject brand-new unsolicited packets from outside.” The lab’s simple setup skips this, but real-world firewall designs treat this kind of stateful decision as close to mandatory.

The View from the Top 1% (What Experts See)

MASQUERADE vs. SNAT: a trade-off over “asking for the IP address every single time”

Two targets translate the source address — SNAT and MASQUERADE — and while both achieve the same result (letting multiple internal clients share one external IP address to reach the internet), their internals differ.

Using MASQUERADE in the lab reflects exactly this assumption — a verification environment where the WAN-side IP obtained via DHCP (from PVE or a home router) might change. In a production environment with a confirmed static IP, SNAT is the marginally more efficient choice.

iptables is now often “a compatibility layer wearing nftables’ skin”

netfilter now has a newer configuration tool than iptables: nftables. On many modern distributions (Debian 10+, Ubuntu 20.04+, and others), running the iptables command actually goes through a compatibility layer called iptables-nft, which converts the rule internally and registers it as an nftables ruleset. If iptables --version shows (nf_tables) in its output, this is what’s happening. The rules still look and read the same as classic iptables, but the engine actually inspecting packets in the kernel is nftables — worth knowing as background for when you eventually migrate to native nftables syntax.

Common Misconceptions and Pitfalls

Common Sticking Points (Troubleshooting)

  1. A specific kind of traffic just doesn’t get through: Run sudo iptables -L -v -n --line-numbers and check each chain’s rules along with a packet counter for each one. If the counter on the rule you expected stays at 0, some earlier rule in the chain is likely deciding the packet’s fate first.
  2. You added a rule, but it doesn’t seem to take effect: Double-check you targeted the right table (a missing -t nat, for example) and chain name.
  3. The firewall config is gone after a reboot: As above, this is most likely because rule persistence (iptables-persistent or similar) isn’t set up. Confirm with sudo iptables -L whether the current ruleset is actually empty.
  4. MASQUERADE is set up, but clients still can’t reach the internet: Check whether net.ipv4.ip_forward is enabled (see the sysctl article). Even if the FORWARD chain allows it, if the kernel’s IP forwarding itself is disabled, packets never get forwarded in the first place.

Prevention and Long-Term Fixes

Summary

Starting Today

  1. When designing firewall rules, start by asking “is this destined for the host itself, or just passing through?” and let that decide whether it belongs in INPUT or FORWARD.
  2. After adding or changing a rule, get in the habit of confirming with iptables -L -v -n that packets are actually matching as intended, by watching the counters.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).