Table of Contents
- What is Connection Tracking?
- How Connection Tracking Works
- The Conntrack Table: Structure and Entries
- Key Connection States
- Practical Examples with iptables
- Conntrack Tools for Management and Monitoring
- Performance Considerations
- Troubleshooting Common Issues
- Conclusion
- References
What is Connection Tracking?
Connection tracking (often called “conntrack”) is a core feature of the Linux netfilter framework that monitors network connections and maintains metadata about their state. Unlike “stateless” filtering (which evaluates each packet in isolation), stateful filtering using connection tracking allows the firewall to make decisions based on the history of the connection.
For example:
- A stateless firewall might block all inbound TCP port 80 traffic, including legitimate responses to outbound HTTP requests.
- A stateful firewall, using connection tracking, recognizes that the inbound traffic is part of an existing connection initiated by the host and allows it.
In short, connection tracking turns a static packet filter into an intelligent, context-aware security tool.
How Connection Tracking Works
Connection tracking operates in the kernel, integrated with the netfilter subsystem. Here’s a simplified workflow:
- Packet Ingestion: When a packet enters the network stack, it passes through netfilter hooks (e.g.,
PREROUTING,INPUT). - Conntrack Lookup: The kernel checks if the packet belongs to an existing connection by querying the conntrack table (a in-memory data structure storing connection metadata).
- New Connection Detection: If no existing entry is found, the kernel creates a new conntrack entry and marks the packet as
NEW(see Key Connection States). - State Updates: For subsequent packets in the connection, the kernel updates the conntrack entry (e.g., marking it as
ESTABLISHEDonce a response is received). - Rule Evaluation: iptables rules can now reference the connection’s state (e.g.,
--state ESTABLISHED) to allow/block traffic dynamically.
The Conntrack Table: Structure and Entries
The conntrack table is a kernel-managed in-memory database that stores metadata for all tracked connections. Each entry in the table represents a single connection and includes details like:
| Field | Description |
|---|---|
| Source/Destination IP | The IP addresses of the communicating hosts. |
| Source/Destination Port | The ports used (for TCP/UDP; not applicable for ICMP). |
| Protocol | The network protocol (TCP, UDP, ICMP, etc.). |
| State | The connection’s current state (e.g., NEW, ESTABLISHED). |
| Timeouts | When the entry will expire if no activity is detected. |
| NAT Information | For NAT’d connections: original and translated IPs/ports. |
| Mark | Optional user-defined label for classification. |
Viewing the Conntrack Table
To inspect the conntrack table, use tools like conntrack (part of the conntrack-tools package) or directly read the kernel’s procfs interface:
# View all tracked connections (requires root)
sudo conntrack -L
# Alternative: Read raw conntrack data from procfs
cat /proc/net/nf_conntrack
A sample entry for an HTTP connection might look like this (simplified):
tcp 6 431999 ESTABLISHED src=192.168.1.100 dst=203.0.113.50 sport=45678 dport=80 ...
Here, tcp is the protocol, 6 is the protocol number, 431999 is the remaining timeout (in seconds), ESTABLISHED is the state, and src/dst/sport/dport define the connection endpoints.
Key Connection States
Connection tracking categorizes packets into distinct states, which iptables rules can target. Understanding these states is critical for writing effective firewall policies.
1. NEW
A NEW state indicates the first packet of a connection. For TCP, this is typically a SYN packet initiating a three-way handshake. For UDP (connectionless), it’s the first packet sent from a source to a destination.
Example: A user browsing the web sends a SYN packet to example.com:80—this is marked NEW.
2. ESTABLISHED
Once a NEW connection is acknowledged (e.g., TCP’s three-way handshake completes), the state transitions to ESTABLISHED. Packets in both directions are allowed for ESTABLISHED connections.
Example: After the web server responds with a SYN-ACK, the connection is ESTABLISHED, and subsequent HTTP data packets are marked as such.
3. RELATED
A RELATED state applies to new connections indirectly associated with an existing ESTABLISHED connection. Common examples include:
- FTP data connections (related to an existing FTP control connection).
- ICMP “destination unreachable” messages (related to a failed connection attempt).
To track RELATED connections, netfilter uses helper modules (e.g., nf_conntrack_ftp for FTP).
4. INVALID
Packets marked INVALID cannot be associated with any known connection. This includes:
- Corrupted packets.
- Out-of-order TCP segments with no matching connection.
- Packets with invalid flags (e.g., a TCP
FINpacket for a non-existent connection).
INVALID packets are often dropped as they may indicate scanning or spoofing.
5. UNTRACKED
UNTRACKED packets are explicitly excluded from connection tracking (via rules like iptables -t raw -A PREROUTING -j NOTRACK). This is useful for high-volume, low-security traffic (e.g., internal DNS) to reduce kernel overhead.
Practical Examples with iptables
Let’s translate these concepts into real iptables rules. We’ll assume a default-deny policy (block all traffic unless explicitly allowed) and build a stateful firewall.
Example 1: Allow Established/Related Traffic
The most common use case: Allow return traffic for existing connections while blocking unsolicited inbound traffic.
# Allow ESTABLISHED and RELATED traffic
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
Example 2: Block INVALID Packets
Drop INVALID packets to mitigate potential attacks:
sudo iptables -A INPUT -m state --state INVALID -j DROP
Example 3: Allow New SSH Connections
Permit NEW SSH connections (port 22) from trusted IPs:
# Allow NEW SSH from 192.168.1.0/24 subnet
sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -m state --state NEW -j ACCEPT
Example 4: Handle FTP (RELATED Connections)
FTP uses two connections: a control connection (port 21) and a dynamic data connection. To allow RELATED data connections:
# Load the FTP conntrack helper (required for RELATED detection)
sudo modprobe nf_conntrack_ftp
# Allow FTP control connections (NEW)
sudo iptables -A INPUT -p tcp --dport 21 -m state --state NEW -j ACCEPT
# Allow RELATED data connections (automatically detected by the helper)
sudo iptables -A INPUT -m state --state RELATED -j ACCEPT
Saving Rules
To persist rules across reboots, save them with:
# On Debian/Ubuntu
sudo iptables-save > /etc/iptables/rules.v4
# On RHEL/CentOS
sudo service iptables save
Conntrack Tools for Management and Monitoring
The conntrack-tools package (install with sudo apt install conntrack-tools or sudo yum install conntrack-tools) provides utilities to manage the conntrack table and tune its behavior.
conntrack Command
The conntrack tool lets you list, delete, or monitor connections:
| Command | Purpose |
|---|---|
sudo conntrack -L | List all tracked connections. |
sudo conntrack -D --src 1.2.3.4 | Delete all connections from 1.2.3.4. |
sudo conntrack -E | Monitor connection events in real-time. |
sudo conntrack -S | Show conntrack statistics (e.g., entries created/destroyed). |
Tuning Conntrack with Kernel Parameters
Connection tracking behavior is controlled by kernel parameters under /proc/sys/net/netfilter/ (or via sysctl). Key parameters include:
| Parameter | Purpose | Default Value (varies) |
|---|---|---|
nf_conntrack_max | Maximum number of tracked connections. | 65536 |
nf_conntrack_tcp_timeout_established | Timeout (seconds) for ESTABLISHED TCP. | 432000 (5 days) |
nf_conntrack_udp_timeout | Timeout for UDP NEW connections. | 30 seconds |
Example: Increase the maximum tracked connections to 1 million (for high-traffic servers):
sudo sysctl -w net.netfilter.nf_conntrack_max=1000000
Performance Considerations
Connection tracking is powerful but not free. Each conntrack entry consumes kernel memory (~300–500 bytes per entry), and excessive tracking can:
- Increase latency (due to table lookups).
- Exhaust kernel memory (causing new connections to fail).
- Degrade throughput on high-bandwidth systems.
Mitigation Strategies
- Limit Tracked Traffic: Use
NOTRACKfor low-risk, high-volume traffic (e.g., internal NTP):iptables -t raw -A PREROUTING -p udp --dport 123 -j NOTRACK # NTP - Tune Timeouts: Reduce
ESTABLISHEDtimeouts for short-lived connections (e.g., web traffic):sudo sysctl -w net.netfilter.nf_conntrack_tcp_timeout_established=3600 # 1 hour - Scale
nf_conntrack_max: Matchnf_conntrack_maxto your expected peak connections (e.g., 10k for small servers, 1M+ for load balancers).
Troubleshooting Common Issues
1. “Connection Refused” for Legitimate Traffic
Issue: Return traffic is blocked because ESTABLISHED/RELATED rules are missing.
Fix: Add iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT.
2. Conntrack Table Overflow
Issue: nf_conntrack: table full, dropping packet errors in dmesg.
Fix: Increase nf_conntrack_max or reduce timeouts to evict idle entries faster.
3. FTP/IRC Connections Fail (RELATED Not Working)
Issue: RELATED connections are blocked because helper modules (e.g., nf_conntrack_ftp) are not loaded.
Fix: Load the helper module:
sudo modprobe nf_conntrack_ftp
4. Inactive Connections Persist
Issue: Stale entries linger due to long timeouts.
Fix: Manually delete entries or reduce timeouts:
sudo conntrack -D --proto tcp --dport 80 # Delete all HTTP entries
Conclusion
Connection tracking transforms iptables from a basic packet filter into a dynamic, context-aware firewall. By tracking connection states (NEW, ESTABLISHED, RELATED, etc.), you can enforce granular policies that block threats while allowing legitimate traffic.
Whether you’re securing a home server or a enterprise network, mastering connection tracking is essential. Use tools like conntrack to monitor and tune the conntrack table, and always test rules in a staging environment before deploying to production.