Concepts / Firewalls

Firewalls

Stateful firewall

Also called conntrack, established, related, connection tracking

A firewall that remembers connections, so it can allow replies while refusing new connections in.

A reply gets in because the firewall saw the request go out
A reply gets in because the firewall saw the request go out

The firewall notes every connection it lets out. A packet coming back that belongs to a noted connection is let in. A new connection from outside matches nothing in the table and is dropped, even to the same laptop and port.

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:

  1. The inside opens a connection out. nightDesk sends a SYN to starCatalogue. It is new, a rule lets campus start conversations toward outside, and conntrack records the flow.
  2. 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.
  3. A lookalike is shut out. A visitor outside sends from port 80 to the archive at 10.41.1.20:9200, the packet a stateless --sport 80 rule mistakes for an answer. No conversation in the table matches, so it is new from outside, and the DROP policy takes it.
  4. Direction, not host. Strangers may start port 80 on gallery, and gallery may start nothing toward campus, 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.

See it on real machines

Read next

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab