iptables Chains and Extensions

Topics · Published 2019-08-25 · Revised 2026-11-23
Reviewed corrections applied locally; pre-review version preserved separately · Joomla source

9,237 Total views across this article’s versions; includes the historical baseline where available.

iptables extensions add packet matches and target operations. A match tests a condition; a target performs an action after all the rule’s matches succeed. The kernel applies the installed rules to packets.

Availability depends on the kernel, backend, and installed extensions. The tables below give valid example placements and important restrictions. A chain name alone is insufficient: for example, POSTROUTING exists in mangle and nat, but not in filter.

This is a selection of common extensions. See iptables-extensions(8) for the complete interface, and the command reference for explanations.

Match Extension Chains and Tables

MatchValid example placementRestrictions and purpose
connmarkmangle/PREROUTINGReads the tracked connection mark; this is not the current packet mark.
conntrackfilter/INPUT, filter/FORWARD, filter/OUTPUTReads connection metadata after tracking. A conntrack match does not itself enable tracking for just that rule.
iprangefilter/INPUT or filter/FORWARDMatches an explicit source/destination address interval.
macfilter/INPUT, filter/FORWARD, or mangle/PREROUTINGRequires a suitable received Ethernet packet. It tests the link sender, not a remote user identity.
markmangle/PREROUTING or filter/INPUTReads packet metadata. Set the mark before the routing decision when policy routing needs it.
multiportfilter/INPUT, filter/FORWARD, filter/OUTPUTRequires TCP, UDP, UDP-Lite, DCCP, or SCTP; at most 15 slots, with a range using two.
ownerfilter/OUTPUT or mangle/POSTROUTINGLimited to locally generated packets with applicable socket ownership. OUTPUT/POSTROUTING only; no filter/POSTROUTING chain.

These examples are not exhaustive lists of every supported table. A match must be meaningful at the hook where it runs: information that does not yet exist, such as an outgoing interface before routing, cannot be selected there.

Target Extension Chains and Tables

TargetPlacementQualification
CONNMARKUse mangle for the set/save/restore examples.A tracked connection is required. The manual documents restore-mark in mangle; save and restore copy in opposite directions.
CTraw/PREROUTING or raw/OUTPUTConfigures tracking before normal connection lookup; --notrack bypasses it.
DNATnat/PREROUTING or nat/OUTPUTChanges the destination address/port through a connection binding.
MARKUse mangle at the hook appropriate to the consumer.For policy routing: PREROUTING for received traffic, OUTPUT for locally generated traffic.
MASQUERADEnat/POSTROUTINGUses an outgoing-interface address for source NAT.
REJECTfilter/INPUT, filter/FORWARD, filter/OUTPUT in traditional IPv4 iptablesDrops the packet and requests a response; tcp-reset requires TCP.
SNATnat/POSTROUTING; also nat/INPUT since Linux 2.6.36Changes the source address/port.
SECMARK / CONNSECMARKsecurity/INPUT, security/FORWARD, security/OUTPUT; mangle is also supported for compatibility.Security labels depend on the security subsystem and policy. They are separate from routing marks.

A user-defined chain remains subject to the hooks from which it can be reached. Moving a restricted target into a custom chain does not make an otherwise invalid caller valid. The traditional REJECT restriction can also be checked in the Linux implementation.

Inverse Operand

Where supported, ! inverts a particular match option. For example, this fragment matches source addresses outside the specified interval:

-m iprange ! --src-range 192.168.1.10-192.168.1.20

The -m option selects the extension; it is not itself an extension. Inversion does not negate every other condition in the rule. All of the rule’s conditions still have to succeed.

Loopback Adapter

Loopback traffic uses the lo interface. In the ordinary local-to-local IPv4 path it leaves through OUTPUT and POSTROUTING, then re-enters through PREROUTING and INPUT.

Use an outgoing-interface test such as -o lo on the output side and an incoming-interface test such as -i lo on the input side, in a table that has the selected chain. Loopback is not limited to the INPUT and OUTPUT hooks, even though those are the relevant built-in chains in the filter table. Ordinary loopback delivery does not traverse FORWARD.

Connection Tracking

Connection tracking is a kernel facility shared by stateful filtering and NAT. Traffic may be tracked even if no rule uses the conntrack match. Conversely, packets explicitly exempted with CT --notrack are not tracked merely because a later rule tests their state.

NEW does not mean only the first packet. A flow can remain NEW while tracking has seen traffic in only one direction, including retransmissions. ESTABLISHED indicates that tracking has seen both directions; it is not a test that an application transaction has succeeded.

See the connection-state explanations for RELATED, INVALID, UNTRACKED, and virtual NAT states.