Firewalls: Packet Filters, Stateful Inspection and Proxies
Why firewalls exist, how a packet filter decides from headers alone, why stateless rules break replies and how connection tracking fixes them, what a proxy firewall does differently, and where firewalls sit today, from home routers to cloud security groups.
kvi5hnuCode Sprout team member17 min readOn this page
In the early Internet, almost everyone on it was a researcher, and hosts accepted traffic from anywhere. By the 1990s the Internet had millions of users, some of them hostile, and attacks on buggy host software were common. Keeping every host on a site patched was impossible, especially old machines that no longer got updates. The practical answer was to stop unwanted traffic before it reached the hosts at all. A device that does this is a firewall: a router, or a host acting as a gateway, that restricts the traffic it passes.
There are two main kinds, and the difference is the layer they work at:
- A packet-filtering firewall is a router that drops some packets. It looks at headers (addresses, protocol, ports, flags) and forwards or discards each packet. The connection still runs directly between the two hosts.
- A proxy firewall is a host that terminates connections. A client connects to the proxy, and the proxy opens its own connection to the server and relays data between the two at the application layer.
This article explains both, with a real stateless filter, a real stateful filter and a real proxy from the course lab. Article 35 adds address translation (NAT), and article 37 covers writing complete rule sets with nftables.
Where a firewall sits
A firewall is useful only if traffic can't get around it, so it sits at a choke point: between your network and your Internet connection, or between parts of a network. A common layout has three zones:

- The inside network holds the machines being protected. They can usually start connections to anywhere.
- The outside is the Internet. Nothing there may start a connection inward, except where a rule allows it.
- A DMZ (demilitarized zone, sometimes called a perimeter network) holds servers that must be reachable from the Internet, such as a public web or mail server. Outsiders may reach them only on the ports those services use. If a DMZ server is broken into, rules stop it from starting connections to the inside.
Packet filters
A packet filter applies rules to each packet. A rule is a set of conditions plus an action:
- Conditions compare header fields: source and destination addresses or prefixes, the protocol (TCP, UDP, ICMP), source and destination ports, TCP flags, ICMP types, IP options, and which interface the packet arrived on or will leave by.
- Actions are mainly accept (forward the packet), drop (discard it silently) and reject (discard it and tell the sender, with an ICMP error or a TCP reset).
Rules are checked in order and the first matching rule decides. If nothing matches, the chain's default policy applies. A firewall that should allow only what is needed uses a default policy of drop, so anything not explicitly allowed is blocked.
Stateless filtering, and why replies break
The simplest packet filter is stateless: it judges each packet on its own, with no memory of earlier packets. That sounds reasonable until you try to allow "connections from inside to outside" and nothing else.
In the lab, r1 is the router between client (192.0.2.10, inside) and server (198.51.100.10, outside). Without any rules, the client's web request works:
curl http://198.51.100.10/ -> HTTP 200
Now r1 gets a stateless rule set in nftables, Linux's packet-filtering framework: accept packets that arrive on the inside interface (eth0) and leave on the outside one (br1), and drop everything else.
curl http://198.51.100.10/ -> timed out after 3 s (exit 28)
The request now fails. To read the counters, you need one fact about TCP from later in the course (article 59): a connection opens with a three-way handshake. The client sends a SYN packet, the server answers with a SYN-ACK, and the client confirms with an ACK. Every later packet also carries the ACK flag. The counters show why the request failed:
table inet fw {
chain forward {
type filter hook forward priority filter; policy drop;
iifname "eth0" oifname "br1" counter packets 3 bytes 180 accept comment "inside to outside"
counter packets 5 bytes 296 comment "everything else is dropped"
}
}
The client's SYN went out (it was sent 3 times, because TCP retransmitted it when no answer came). The server answered with SYN-ACKs, 5 of them, since it also retransmits its SYN-ACK when no ACK comes back, and they arrived on br1 going toward eth0. No rule allows that direction, so they were dropped. A connection needs packets in both directions, and a filter with no memory can't tell a reply from an unwanted packet coming in.
Older stateless firewalls worked around this with rules such as "allow inbound TCP if the ACK flag is set", since every TCP packet after the first SYN carries ACK. That lets replies through, but it also lets in any packet an attacker crafts with ACK set, and it does nothing for UDP, which has no flags. Stateless filters are still used where speed matters more than precision, such as router access lists (ACLs) and some cloud network ACLs, but they're never a complete firewall on their own.
Stateful filtering: connection tracking
A stateful firewall keeps a table of the flows passing through it. When it sees the first packet of a flow that its rules allow, it records the flow. Later packets that belong to a recorded flow, in either direction, are recognized as part of it. Linux does this with connection tracking (conntrack), and nftables rules can match on the result with ct state:
ct state | Meaning |
|---|---|
new | The first packet of a flow not seen before, such as a TCP SYN |
established | A packet of a flow that has already seen traffic in both directions |
related | A new flow that belongs to an existing one, such as an ICMP error about a tracked connection |
invalid | A packet that doesn't fit any valid state, such as a stray SYN-ACK with no SYN |
Here's the same router with a stateful rule set: accept replies and related traffic, drop invalid packets, and allow new connections only from inside to outside.
curl http://198.51.100.10/ -> HTTP 200
While a second connection was open, the connection-tracking table on r1 looked like this:
tcp 6 118 TIME_WAIT src=192.0.2.10 dst=198.51.100.10 sport=34194 dport=80 src=198.51.100.10 dst=192.0.2.10 sport=80 dport=34194 [ASSURED]
tcp 6 431998 ESTABLISHED src=192.0.2.10 dst=198.51.100.10 sport=34198 dport=80 src=198.51.100.10 dst=192.0.2.10 sport=80 dport=34198 [ASSURED]
Each line is one tracked flow. It records the original direction (src=192.0.2.10 dst=198.51.100.10 sport=... dport=80) and the reply direction it expects (the same addresses and ports swapped). (6 is TCP's protocol number.) The number after tcp 6 is the seconds until the entry expires if no more packets arrive. Linux keeps an established TCP connection for 5 days by default (432,000 seconds), while the first connection, which had already closed, is in TIME_WAIT (a short waiting state TCP goes through after a close, article 61) and will be forgotten in 2 minutes. [ASSURED] means traffic was seen in both directions, so this entry won't be the first to be dropped if the table fills up.
A connection started from outside still fails, because its first packet is new and arrives on the outside interface:
curl from server to 192.0.2.10:22: timed out (exit 28)
The final counters tell the story:
table inet fw {
chain forward {
type filter hook forward priority filter; policy drop;
ct state established,related counter packets 13 bytes 1150 accept comment "replies and related traffic"
ct state invalid counter packets 1 bytes 60 drop
iifname "eth0" oifname "br1" ct state new counter packets 2 bytes 120 accept comment "new connections from inside"
counter packets 4 bytes 236 comment "everything else is dropped"
}
}
Notice the single packet counted as invalid. It was a late SYN-ACK retransmission from the earlier failed attempt, answering a SYN the firewall never tracked. Dropping such packets is exactly what stateful filtering is for.
Proxy firewalls
A proxy firewall isn't a router. It's a host with interfaces on both sides, usually with IP forwarding turned off, running one application-layer gateway (ALG) per service it supports. A client connects to the proxy, tells it what it wants, and the proxy makes its own connection to the server and relays the data. The figure contrasts this with a packet filter: one connection through the filter, two connections through the proxy.

In the lab, IP forwarding on r1 was turned off, so it can no longer route packets between the networks. A direct request fails:
curl http://198.51.100.10/ -> timed out after 3 s (exit 28)
A tiny HTTP proxy runs on r1 at 192.0.2.1 port 3128, and the client is told to use it:
curl -x http://192.0.2.1:3128 http://198.51.100.10/ -> HTTP 200
The proxy's log shows the two separate connections:
proxy: 192.0.2.10:42050 asked for http://198.51.100.10/
proxy: opened its own connection 198.51.100.1:37054 -> 198.51.100.10:80
And the web server's log shows who it thinks the client is:
198.51.100.1 - - [date] "GET / HTTP/1.1" 200 -
The server never saw 192.0.2.10. As far as the outside world knows, every request came from the proxy. This also means the inside hosts can use addresses that aren't reachable from the Internet at all, since only the proxy talks to the outside.
Proxies have real strengths:
- They understand the application, so they can enforce rules a packet filter can't, such as blocking certain URLs, scanning downloads for malware, or requiring users to log in.
- Nothing from outside ever reaches an inside host directly, since the proxy builds every outgoing connection itself.
They also have costs. Every service needs its own proxy software, so a new application doesn't work until someone adds and runs a proxy for it. Clients must usually be configured to use the proxy, and the proxy must keep up with the full traffic load at the application layer.
The two common kinds are:
- HTTP proxies (web proxies). The client sends its request to the proxy instead of the server, with the full URL, as in the lab. For HTTPS, the client sends
CONNECT host:443, and the proxy opens a TCP connection and then just relays the encrypted bytes, so it sees only the host name. Organizations that need to inspect HTTPS content install their own certificate authority on every managed device and let the proxy decrypt and re-encrypt traffic, which is called TLS inspection. - SOCKS proxies. SOCKS is a generic relay protocol that isn't tied to one application. SOCKS5 (RFC 1928) adds authentication, UDP relaying and IPv6, compared with version 4. An application must support SOCKS to use it. Today you're most likely to meet SOCKS as the local proxy that
ssh -Dcreates, or in privacy tools such as Tor.
Clients find a proxy in one of three ways:
- Manual configuration.
- A proxy auto-config (PAC) file. This is a small JavaScript function that returns which proxy to use for each URL. WPAD (Web Proxy Auto-Discovery) is a way to find the PAC file automatically through DHCP or DNS. It was never standardized, and it has a long history of security problems, so many organizations now turn it off.
- A transparent ("intercepting") proxy. The network quietly redirects traffic to the proxy, so clients need no configuration at all.
Firewalls today
The basic types haven't changed, but where they run has:
- Home routers combine NAT with a stateful filter that allows outgoing connections and their replies, and blocks new inbound ones. Article 35 shows why NAT behaves like a firewall for inbound traffic.
- Host firewalls run on the computer itself: nftables (often managed by firewalld or ufw) on Linux, where the
iptablescommand in older guides now usually creates nftables rules behind the scenes (article 37), Windows Defender Firewall, and pf plus the application firewall on macOS. They protect a laptop on untrusted Wi-Fi, where no network firewall exists. - Cloud security groups are stateful filters attached to each virtual machine or network interface. Some clouds also have stateless network ACLs at the subnet level, and forgetting to allow return traffic there is a classic mistake.
- Next-generation firewalls add application identification, intrusion prevention, user identity and often TLS inspection to stateful filtering. They blur the line between packet filters and proxies.
Common questions
What's the difference between drop and reject?
Drop discards the packet silently, so the sender waits and retries until it times out, as the 3-second curl failures in the lab did. Reject discards it and sends back an ICMP "unreachable" error or a TCP reset, so the sender fails immediately. Drop gives less information to someone scanning your network. Reject is friendlier for traffic you expect, such as blocked outgoing connections on an internal network.
Does a stateful firewall track UDP, which has no connections?
Yes. It treats a UDP flow (the same addresses and ports) as a pseudo-connection: the first datagram creates an entry, replies in the other direction are established, and the entry expires after a period with no traffic, 30 seconds by default on Linux, or 120 seconds once traffic has gone both ways. ICMP echo requests and replies are tracked the same way, using the echo identifier.
Why do tracked TCP connections stay for days, but UDP ones for seconds?
TCP tells the firewall when a connection ends (with a FIN or RST packet, TCP's close and reset signals), so a long timeout for idle established connections is safe: the entry is removed sooner when the connection closes. UDP gives no such signal, so the only way to clean up is a short idle timeout. That's why long-lived UDP applications send keepalive packets.
What happens when the connection-tracking table is full?
New flows can't be tracked, so their packets are dropped, and Linux logs "nf_conntrack: table full, dropping packet". Busy servers and NAT gateways raise the limit (net.netfilter.nf_conntrack_max) or exclude traffic that doesn't need tracking. A flood of new flows that fills the table is a denial-of-service attack in itself.
Is a proxy more secure than a packet filter?
It can enforce finer rules, because it understands the application and nothing outside ever talks directly to an inside host. But it's also complex software exposed to hostile input, and it only works for applications it supports. Most networks today use stateful packet filtering for everything, plus a web proxy for HTTP and HTTPS where content control is required.
If I use HTTPS through a proxy, can the proxy read my traffic?
Not with a plain CONNECT proxy: it sees the host name and the amount of data, but the content is encrypted end to end. It can read the content only if it performs TLS inspection, which works only when your device trusts the certificate authority the proxy uses. Managed corporate devices often do, and your own devices normally don't.
Do I need a firewall for IPv6 if I have NAT for IPv4?
Yes. IPv6 hosts usually have globally reachable addresses and no NAT, so the stateful filter is what blocks unwanted inbound connections. Home routers apply the same "outbound allowed, inbound blocked" policy to IPv6 by default, as recommended in RFC 6092.
Summary
- A firewall restricts the traffic passing a choke point, so the hosts behind it are exposed to less of the Internet. Common zones are inside, outside and a DMZ for public servers.
- A packet filter matches header fields against ordered rules and accepts, drops or rejects each packet. A default policy of drop blocks anything not explicitly allowed.
- A stateless filter can't tell replies from unwanted inbound packets, as the lab showed when SYN-ACKs were dropped. A stateful filter tracks flows (conntrack on Linux) and allows packets that belong to a flow its rules accepted.
- Tracked flows have states (new, established, related, invalid) and idle timeouts: long for established TCP, short for UDP.
- A proxy firewall terminates connections and makes its own, so servers see only the proxy's address. HTTP proxies and SOCKS are the common types. Proxies understand applications but need explicit support for each one.
- Firewalls now run on home routers, on hosts, as cloud security groups, and as next-generation appliances, and they're one layer of defense, not the whole of it.
Practice questions
1. A stateless rule set allows TCP from inside to outside on any port and drops everything else. Users report that no web pages load. Explain exactly which packets are dropped.
Answer
The outgoing SYN from the client is allowed, but the server's SYN-ACK travels from outside to inside, which no rule allows, so it's dropped. The handshake never completes, and the client keeps retransmitting the SYN until it gives up. The fix is a stateful rule that accepts established and related packets.
2. Write, in words, the four rules (in order) for a stateful firewall that lets inside hosts reach the Internet, lets the Internet reach a DMZ web server on TCP port 443, and blocks everything else.
Answer
- Accept packets in state established or related.
- Drop packets in state invalid.
- Accept new connections arriving on the inside interface going to the outside interface.
- Accept new TCP connections from the outside interface to the web server's address on destination port 443.
The default policy is drop, so everything else is blocked. The order matters only a little here, but putting the established rule first means most packets are accepted after one check.
3. In the lab's conntrack output, what does the second set of src=... dst=... fields describe, and why does the firewall need it?
Answer
It describes the reply direction: the packets the firewall expects to see coming back, with source and destination addresses and ports swapped. The firewall uses it to recognize returning packets as part of the tracked connection. With NAT, the reply tuple also shows the translated address, which is how the NAT knows where to send replies (article 35).
4. A server behind a proxy firewall logs every visitor's address as 198.51.100.1. Why, and how can the real client address be passed on?
Answer
The proxy opens its own connection to the server, so the TCP connection's source address is the proxy's. HTTP proxies usually add the original client address in a header such as Forwarded (RFC 7239) or X-Forwarded-For. The server must trust that header only when the request really came from its own proxy.
5. Why is a short idle timeout needed for UDP entries, and what problem does it cause for applications?
Answer
UDP has no close signal, so the firewall can only forget a flow when it goes quiet. If an application is silent for longer than the timeout (30 to 120 seconds on Linux by default), its entry disappears and later replies from outside are dropped as new inbound traffic. Applications such as VPNs (virtual private networks) and VoIP (voice over IP) send small keepalive packets more often than the timeout to keep the entry alive.
6. Your cloud VM has a security group allowing inbound TCP port 22, and its subnet has a stateless network ACL that allows inbound TCP 22 and no outbound rules besides the default deny. Can you connect with SSH?
Answer
No. The security group is stateful, so it would allow the replies, but the stateless subnet ACL judges the server's outbound replies (from port 22 to your ephemeral port, the temporary port your client picked, article 4) on their own, and no outbound rule allows them. You need an outbound ACL rule allowing TCP to the ephemeral port range, typically 1024 to 65535.
Previous: Configuration and DNS Together: Dynamic DNS Updates and Attacks on Configuration
Next: Network Address Translation: NAPT, Mapping and Filtering Behavior, Hairpinning and Carrier-Grade NAT

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.