Firewalls
Stateful firewall
A firewall that remembers connections, so it can allow replies while refusing new connections in.
A stateful firewall remembers the connections that pass through it (connection tracking). It sees each packet as new, established (part of a known connection) or related. The classic policy: accept established and related, allow new connections from inside, drop new ones from outside. It is about who started the conversation, not which ports are open.
How a stateful firewall works, step by step
A stateless firewall judges each packet alone, by address and port. A stateful one
remembers connections in the kernel's conntrack table, and sorts every packet into a state: new (the first
packet of a flow), established (a packet belonging to a flow already seen), related (a new flow a helper ties
to an existing one, such as an FTP data channel or an ICMP error), or invalid. Follow the
stateful inspection lab, where ridgeGate stands between campus 10.41.1.0/24,
the public gallery 10.41.2.0/24 and outside, with nightDesk at 10.41.1.10 and the starCatalogue
web server at 203.0.113.20:
- The inside opens a connection out.
nightDesksends a SYN tostarCatalogue. It is new, a rule letscampusstart conversations towardoutside, and conntrack records the flow. - The reply comes home. The server's answer is established against that record, so the rule on line 1, "accept established and related", takes it, though no rule names a source port.
- A lookalike is shut out. A
visitoroutside sends from port 80 to the archive at10.41.1.20:9200, the packet a stateless--sport 80rule mistakes for an answer. No conversation in the table matches, so it is new from outside, and theDROPpolicy takes it. - Direction, not host. Strangers may start port 80 on
gallery, andgallerymay start nothing towardcampus, though packets cross between them both ways all day. The rule is about who started the conversation.
This is the whole logic of a home router's protection and of a VPN gateway: the inside reaches out, replies return, nothing new gets in.
What it looks like on a Linux machine
On Linux the state rule is one line, inserted at the top of the forward chain so the busiest rule reads first:
iptables -I FORWARD 1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
On nftables the same reads ct state established,related accept. With it in place the lab's list needs no rule for
answers: each line below it speaks only for the first packet of a conversation, and line 1 has counted the answers.
$ iptables -L FORWARD -v -n --line-numbers
Chain FORWARD (policy DROP 3 packets, 180 bytes)
num pkts bytes target prot opt in out source destination
1 11 922 ACCEPT 0 -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 1 60 ACCEPT 6 -- campus outside 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
3 0 0 ACCEPT 6 -- outside gallery 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
The table itself shows in conntrack, one line per conversation, each written twice: the first packet as the firewall saw it, then the shape a legal answer must have.
$ conntrack -L
conntrack v1.4.8 (conntrack-tools): 3 flow entries have been shown.
tcp 6 118 SYN_SENT src=10.41.1.10 dst=203.0.113.99 sport=55230 dport=80 [UNREPLIED] src=203.0.113.99 dst=10.41.1.10 sport=80 dport=55230 mark=0 use=3
tcp 6 118 TIME_WAIT src=10.41.1.10 dst=203.0.113.20 sport=37772 dport=80 src=203.0.113.20 dst=10.41.1.10 sport=80 dport=37772 [ASSURED] mark=0 use=1
tcp 6 299 ESTABLISHED src=10.41.1.10 dst=203.0.113.20 sport=37778 dport=80 src=203.0.113.20 dst=10.41.1.10 sport=80 dport=37778 [ASSURED] mark=0 use=1
[ASSURED] means traffic has gone both ways, and [UNREPLIED] marks an opening nobody answered. The visitor's
lookalike, sent from port 80 with curl --local-port 80, gets nowhere:
curl: (28) Connection timed out after 3003 milliseconds. The same table underlies the home router in the
port forwarding lab, and the access lists of the router ACLs lab show
what a firewall without it has to do.
What goes wrong, and how you see it
Asymmetric routing breaks state. If a flow's reply returns through a different firewall that never saw
the request, conntrack marks it INVALID and drops it. conntrack -S shows the invalid counter rising, and
connections hang. Fix the routing, or the two firewalls must share state.
You forgot the established rule. With only "allow new from inside", the inside's requests leave, but their replies
match no rule and are dropped, so nothing the inside starts ever completes. Accept established,related first.
A drop sits above it. A ban or a block written on line 1 reads every packet before the state rule does, answers
included. Keep the state rule on top, and remember the reverse too: a ban under it never touches a conversation that
is already going, until conntrack -D deletes its line.
A helper is missing. Active FTP's data channel is a related flow a helper recognises. Without
nf_conntrack_ftp, the data connection looks new and is dropped; the control channel works but transfers stall. The
file transfer lab shows that second connection.
Idle flows expire. A UDP conntrack entry lives 30 seconds, an established TCP one much longer. A long-idle connection whose entry has timed out is treated as new and blocked; keepalives hold the entry open.
Questions people ask
Stateful vs stateless firewall? A stateless filter decides packet by packet on addresses and ports; a stateful one tracks the connection, so a single "accept established,related" rule lets every reply home without opening ports inbound. It is simpler and safer.
Is a stateful firewall the same as NAT? No. NAT also uses conntrack and blocks unsolicited inbound as a side effect, but a firewall's protection is the explicit policy you can read, and it works on IPv6 where NAT is not used.
What are related connections? New flows a helper ties to one already allowed: FTP data channels, SIP media, and ICMP errors about a tracked flow. They are accepted by the same established,related rule.
Read next
- Firewall Rules deciding, packet by packet, what may reach or cross a device.
- Access control list An ordered list of permit and deny lines a router checks each packet against, applied to a card in or out.
- NAT A router rewriting addresses as packets pass, usually to hide many private devices behind one address.
- Asymmetric routing Traffic there and back taking different paths, because each router picks its own way out.
- UDP hole punching Two peers behind NAT sending to each other at once, so each router takes the other's packets for replies.
- Security groups and network ACLs The cloud's two firewalls: stateful allow rules on each instance, and stateless numbered rules on each subnet.
- FTP File transfer over two kinds of connection: commands on port 21, and a separate data connection per transfer.
- DMZ A network apart for the servers the internet may reach, so breaking into one opens nothing behind it.
- Firewall types From packet filters to next-generation firewalls: how much of the traffic each kind understands.
- Next-generation firewall A stateful firewall that also identifies the application in each flow and applies proxy and IPS rules to it.
- Packet crafting Building one packet by hand — choosing every flag and field — to probe a target a single reply at a time.
- Firewalking Using a port the firewall allows, plus TTL-limited probes, to map the routers and hosts hidden behind it.