Stateful Firewalls: Why Replies Get Through Without a Rule
Stateless versus stateful filtering, the state table, the TCP handshake, UDP and ICMP pseudo-states, timeouts and idle connections, rules evaluated on the first packet, an OPNsense example, NAT, asymmetric routing and common mistakes.
We open the rules of the firewall's WAN interface and find no rule at all that lets traffic in from the Internet. Yet web pages arrive, videos play and updates download. How do the replies get through? And why does an SSH session or a business application that has been left idle for a while sometimes drop on its own? Both questions have the same answer: the firewall is stateful and keeps a state table. Let's see how it works, with examples on OPNsense.
Stateless and stateful filtering
A stateless filter examines each packet on its own, with no memory of the previous ones. To let a PC browse the web it therefore needs two rules: one for outgoing requests to port 443 of the servers, and one for incoming replies, which come from port 443 and are addressed to the random port chosen by the client, almost always above 1024. The second rule is a problem: it lets in any packet that pretends to come from port 443, even if nobody asked for it. Router access control lists reduce the risk by checking the TCP flags, but the principle does not change.
A stateful firewall, by contrast, keeps track of connections. When a rule lets the first packet of a connection through, the firewall records its characteristics; subsequent packets, in both directions, are recognised as part of a conversation that has already been authorised. Replies pass because they match a connection opened from the inside, not because a rule allows them. Packets that match nothing are still subject to the rules and, when no rule applies, to the default deny.
The state table
Each entry in the state table describes a conversation in progress. It contains at least:
- the protocol: TCP, UDP, ICMP or another IP protocol;
- the addresses and ports of both ends, for example 192.168.1.10:51514 and 198.51.100.80:443; for ICMP, the identifier of the echo messages takes the place of the ports;
- the state of the connection, which for TCP follows its opening and closing;
- a timeout: the time after which the entry is deleted if no more traffic passes;
- for TCP, the expected sequence numbers: the firewall checks that packets fall inside the expected window and drops those that are out of sequence.
The check is fast: the firewall looks the packet up in the table using an indexed structure, instead of walking through all the rules. This is why connections that are already open put little load on the processor, even with many rules.
TCP: the handshake and the states
TCP (RFC 9293) opens a connection with the three-way handshake: the client sends a SYN segment, the server answers with SYN-ACK and the client confirms with ACK. Let's follow an example: PC 192.168.1.10 opens an HTTPS page on server 198.51.100.80.
- The SYN arrives on the firewall's LAN interface. No state matches, so the firewall evaluates the LAN rules: the Pass rule allows it and the firewall creates the state.
- The SYN-ACK arrives from the WAN. It matches the entry that has just been created: it passes without consulting the WAN rules.
- The client's ACK and the following data update the state, which becomes ESTABLISHED, and renew the timeout with every packet.
- Closing with FIN and ACK, or an RST, moves the state towards closure: the entry stays for a short time, to absorb late packets, and is then deleted.
UDP and ICMP: pseudo-states and timeouts
UDP (RFC 768) has no connections and no flags: there is no handshake to follow. The firewall still creates a pseudo-state with addresses and ports. When a DNS query goes out to port 53, the reply that comes back from the same pair of addresses and ports matches the entry and passes. Since it cannot know when the “conversation” ends, the firewall relies on the timeout alone: the entry stays while packets keep arriving, then it expires.
ICMP (RFC 792) is handled in the same way: an echo request sent by the ping command creates a state, and the echo reply with the same identifier uses it to get back in. ICMP error messages related to an existing connection, such as destination unreachable or the notice that a packet is too big, are recognised as part of that connection. This matters, because blocking them breaks Path MTU Discovery, the mechanism that finds the largest packet size along a path.
Timeouts and idle connections
Each entry has a timeout, renewed with every packet. If a connection stays silent for longer than the timeout, the entry is deleted. Neither the client nor the server knows anything about it: for them the connection is still open. When one of them starts transmitting again, its packet reaches the firewall, finds no entry and is treated as a new packet without a SYN: it is dropped. This is the typical cause of an SSH session that “freezes” after a break, or of a business application that loses its database connection after lunch.
The values depend on the firewall and on its configuration. In pf, the packet filter OPNsense is built on, the default values are roughly these; they can be read from the firewall's shell with pfctl -st (excerpt):
tcp.first 120s
tcp.opening 30s
tcp.established 86400s
tcp.closing 900s
tcp.finwait 45s
tcp.closed 90s
udp.first 60s
udp.single 30s
udp.multiple 60s
icmp.first 20s
icmp.error 10sAn established TCP connection therefore stays in the table through 24 hours of silence, while a UDP exchange expires after about a minute. Other systems use different values: Linux conntrack, for example, normally keeps established TCP connections for five days, while many home routers and carrier NATs have much shorter timeouts. For NAT devices, RFC 4787 asks that a UDP mapping should not expire in less than two minutes, and RFC 5382 that an established TCP connection should not be closed before two hours and four minutes of inactivity, but not every device follows these recommendations. When the table approaches its limit, pf can also shorten the timeouts adaptively.
Rules are evaluated only on the first packet
For every packet the firewall first looks for a matching state; only if it finds none does it read the interface rules, from top to bottom, as explained in the article on how a firewall rule works. If the first matching rule is Pass, the packet passes and a new state is created; if no rule matches, the default deny applies.
Three practical consequences follow. A rule always describes who opens the connection: “LAN to Internet” does not mean “Internet to LAN”. A new Block rule does not interrupt connections that are already established, which carry on as long as their entry stays in the table: after removing a permission it is worth deleting the affected states. Finally, every Pass rule creates states, so rules that are too broad also fill up the table.
The OPNsense example
In OPNsense, as in pf, Pass rules create state implicitly: in the rule's advanced options the state type is keep state by default, and for TCP the state is created by the SYN. The minimal configuration of a home network is therefore:
- Firewall › Rules › LAN: a Pass rule from “LAN net” to any destination, or better, more precise rules for the services that are needed, perhaps grouped with OPNsense aliases;
- Firewall › Rules › WAN: no Pass rule. Replies come back in thanks to the states, and everything else is stopped by the default deny.
The table can be viewed under Firewall › Diagnostics › States, with a filter by address or port and the option of deleting individual entries. The number of states in use compared with the maximum also appears on the dashboard, and the maximum is set under Firewall › Settings › Advanced, together with the optimisation profile that changes the timeouts. Menus may vary with the version. From the shell, pfctl -ss lists the entries; here is a shortened excerpt for our PC:
all tcp 198.51.100.80:443 <- 192.168.1.10:51514 ESTABLISHED:ESTABLISHED
all tcp 203.0.113.25:24871 (192.168.1.10:51514) -> 198.51.100.80:443 ESTABLISHED:ESTABLISHED
all udp 192.168.1.1:53 <- 192.168.1.10:53122 MULTIPLE:SINGLEThe same HTTPS connection appears twice: one entry for its arrival on the LAN interface, with the <- arrow, and one for its departure through the WAN, with the -> arrow and the address translation. The UDP entry is a DNS query to the firewall's resolver. The two states separated by a colon describe the two ends of the conversation.
States and NAT
In the second entry of the example, the private address 192.168.1.10:51514 has become 203.0.113.25:24871, the public WAN address with a new port. In pf the translation is stored in the state entry: when the reply comes back to 203.0.113.25:24871, the firewall finds the entry, restores the original address and port and delivers the packet to the PC. Outbound NAT and stateful filtering therefore work on the same table, as explained in the article on NAT and port forwarding.
Port forwarding works the same way, in the opposite direction: the associated rule lets the first packet in towards the internal server and creates the state, and the server's replies go out thanks to that entry. A server that is reachable from the Internet should sit in a DMZ, with rules that also restrict the connections opened by the server itself.
Asymmetric routing
Stateful filtering assumes that the firewall sees both directions of a connection. If the outbound and return traffic follow different paths, routing is asymmetric and the firewall only sees half of the conversation. A classic case: the LAN contains a second router, 192.168.1.254, which leads to network 10.0.50.0/24 of another department, but the PCs' gateway is the firewall.
The symptom is distinctive: ping may work, but TCP connections hang or drop after a few seconds, and the log shows blocked packets with ACK or PSH-ACK flags and no obvious rule behind them. The proper fix lies in the design: put the second router on a dedicated transit network connected to the firewall, or make sure the clients reach it without going through the firewall. OPNsense also offers the sloppy state state type, which does not check TCP sequencing, and an option to skip filtering for traffic that enters and leaves through the same interface; these are remedies to use with care, because they weaken the checks. The same problem occurs with two firewalls in parallel without state synchronisation, or with two Internet connections used inconsistently.
Common mistakes
- Opening inbound ports “to make the replies work”. It is useless, because replies come back in thanks to the states, and it is dangerous, because those ports become reachable by anyone. If something does not work, the cause lies elsewhere: a missing outbound rule, NAT or DNS.
- Any-any rules. A rule that allows everything from any source to any destination defeats the purpose of the filter: every connection, including that of a compromised device, creates its own state and gets its replies. Rules should be limited to the networks, destinations and ports that are needed.
- Ignoring a full state table. When the maximum is reached, new connections are not created, while existing ones keep working: the network seems to work in fits and starts. Typical causes are a limit that is too low, an infected device or a program that opens thousands of connections. The dashboard, the states page and the
pfctl -sicommand help to spot it. - Timeouts too short for applications with long-lived connections. An aggressive optimisation profile, or an intermediate device with timeouts of a few minutes, breaks SSH sessions, remote desktops, database connections and devices that sit waiting for messages. It is better to enable application keepalives, for example
ServerAliveIntervalin SSH, than to lengthen the timeouts of the whole firewall. - Assuming that state makes everything that was allowed safe. Stateful filtering checks addresses, ports and the consistency of the connection, not its content. Recognising an attack inside an allowed connection requires a detection system such as those described in the article on IDS and IPS.
Conclusions
Replies pass without a rule because the firewall remembers the connections opened from the inside: the rule decides who may start, and the state table handles the rest. In practice:
- do not add Pass rules on the WAN for replies: they are needed only for published services, with port forwarding;
- write rules on the side of whoever opens the connection, limited to the destinations and ports that are needed;
- check regularly how many states are in use compared with the maximum;
- for connections that drop after a pause, check the timeouts along the whole path and enable application keepalives;
- avoid asymmetric routing: every conversation must cross the firewall in both directions;
- after removing a permission, delete the states of connections that are still open.
To go deeper into firewalls, NAT and OPNsense rules with complete examples, the books and the course listed below are a useful next step.
The video
Further reading
- Book: OPNsense® in Practice — Volume 1: The Foundations of a Professional Journey (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 5: LAN Security and Integration (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 4: Routing, NAT, and Internet Access (English edition)
- Video course: Capire i Firewall con OPNsense (video course in Italian)
- Related article: How a Firewall Rule Works
- Related article: NAT and Port Forwarding: How a Private Network Talks to the Internet
- Related article: IDS and IPS: Detecting and Blocking Suspicious Traffic with OPNsense
- Related article: DMZ: Where to Put a Server Reachable from the Internet
- Related article: Aliases in OPNsense: Clearer Firewall Rules That Are Easier to Maintain
- Specialization: Firewalls & Network Security
Technical sources
- RFC 9293 — Transmission Control Protocol (TCP) — Connection opening with the three-way handshake (SYN, SYN-ACK, ACK), sequence numbers, closing with FIN and resetting with RST, connection states.
- RFC 768 e RFC 792 — User Datagram Protocol; Internet Control Message Protocol — Connectionless UDP with no flags; ICMP echo request and echo reply messages with an identifier, error messages such as destination unreachable.
- RFC 4787 e RFC 5382 — NAT Behavioral Requirements for UDP and for TCP — Recommendations for NAT devices: a UDP mapping must not expire in less than two minutes; an established TCP connection must not be closed before two hours and four minutes of inactivity.
- Manuale di pf.conf(5) e pfctl(8) — Packet Filter di OpenBSD e FreeBSD — Rules with keep state, state created by the SYN (flags S/SA), default tcp.*, udp.* and icmp.* timeouts, adaptive timeouts, the state limit; the pfctl -ss, -si and -st commands. Values may change between versions and with the configuration.
- Documentazione ufficiale di OPNsense — Firewall Rules, Diagnostics e Settings — Default keep state type and sloppy state in the advanced rule options, the state table under Firewall › Diagnostics › States, the state limit and optimisation profile under Firewall › Settings › Advanced; menus and options depend on the version.
- RFC 1918 e RFC 5737 — Addresses used in the examples: private networks 192.168.1.0/24 and 10.0.50.0/24, documentation addresses 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24.
Standards and official documentation available from the respective organisations.