Octet

What are the iptables tables and chains, and what is each one for?

Show the answer

Say this first

A table groups rules by job, a chain is the point in the packet's path where they run. filter (the default) accepts or drops in INPUT, FORWARD and OUTPUT. nat rewrites addresses in PREROUTING (DNAT), POSTROUTING (SNAT, MASQUERADE), OUTPUT and INPUT, and only sees the first packet of each connection. mangle changes packets or sets marks in all five chains. raw runs before connection tracking, mostly to exempt traffic from it. security is for SELinux labels.

Go deeper

The map

TableJobBuilt-in chains
filterAllow or blockINPUT, FORWARD, OUTPUT
natAddress and port translationPREROUTING, INPUT, OUTPUT, POSTROUTING
mangleMarks, TTL, DSCP, TCPMSSPREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING
rawBefore conntrack, CT --notrackPREROUTING, OUTPUT
securitySELinux security marksINPUT, FORWARD, OUTPUT

The five hooks in one line each

  • PREROUTING: every packet arriving, before the routing decision.
  • INPUT: packets for this host.
  • FORWARD: packets routed through this host.
  • OUTPUT: packets created by this host.
  • POSTROUTING: every packet leaving, after routing.

Working with it

Shell
iptables -S                  # filter table as commands
iptables -t nat -S           # nat table
iptables -N SSH-IN           # user-defined chain
iptables -A INPUT -p tcp --dport 22 -j SSH-IN

Without -t, iptables works on filter. Terminating targets are ACCEPT, DROP and REJECT; LOG and MARK continue to the next rule; RETURN goes back to the calling chain.

Traps

  • Saying "the iptables INPUT chain" as if there were only one. Several tables have an INPUT chain.
  • Forgetting that iptables with no -t is the filter table, so iptables -L hides all NAT rules.
How did you do?Book 8.5 Example Systems