iptables Chains and Extensions
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
| Match | Valid example placement | Restrictions and purpose |
|---|---|---|
| connmark | mangle/PREROUTING | Reads the tracked connection mark; this is not the current packet mark. |
| conntrack | filter/INPUT, filter/FORWARD, filter/OUTPUT | Reads connection metadata after tracking. A conntrack match does not itself enable tracking for just that rule. |
| iprange | filter/INPUT or filter/FORWARD | Matches an explicit source/destination address interval. |
| mac | filter/INPUT, filter/FORWARD, or mangle/PREROUTING | Requires a suitable received Ethernet packet. It tests the link sender, not a remote user identity. |
| mark | mangle/PREROUTING or filter/INPUT | Reads packet metadata. Set the mark before the routing decision when policy routing needs it. |
| multiport | filter/INPUT, filter/FORWARD, filter/OUTPUT | Requires TCP, UDP, UDP-Lite, DCCP, or SCTP; at most 15 slots, with a range using two. |
| owner | filter/OUTPUT or mangle/POSTROUTING | Limited 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
| Target | Placement | Qualification |
|---|---|---|
| CONNMARK | Use 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. |
| CT | raw/PREROUTING or raw/OUTPUT | Configures tracking before normal connection lookup; --notrack bypasses it. |
| DNAT | nat/PREROUTING or nat/OUTPUT | Changes the destination address/port through a connection binding. |
| MARK | Use mangle at the hook appropriate to the consumer. | For policy routing: PREROUTING for received traffic, OUTPUT for locally generated traffic. |
| MASQUERADE | nat/POSTROUTING | Uses an outgoing-interface address for source NAT. |
| REJECT | filter/INPUT, filter/FORWARD, filter/OUTPUT in traditional IPv4 iptables | Drops the packet and requests a response; tcp-reset requires TCP. |
| SNAT | nat/POSTROUTING; also nat/INPUT since Linux 2.6.36 | Changes the source address/port. |
| SECMARK / CONNSECMARK | security/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.