7 · Firewalls and NAT
In one sentence: the kernel’s packet filter, netfilter, runs every packet past lists of rules; a rule can let it through, throw it away (DROP: the sender hears nothing) or refuse it (REJECT: the sender gets an answer), and NAT rules rewrite addresses, which is how Docker lets containers out and publishes their ports.
The problem it solves
Lesson 1’s fourth failure was curl: (28) Connection timed out after 5002 milliseconds: a firewall threw the
packets away. Firewalls are deliberate: a server should accept connections to its service and nothing else. But a
rule written too wide, or in the wrong place, looks exactly like a broken network. You need to read rules, write
them, and know which symptom each kind of rule produces.
Two more everyday problems live in the same machinery. A container’s private address (172.17.x.x) can’t be
answered from the internet, yet it reaches it. And docker run -p makes a container’s port reachable on the
server’s address. Both are address rewriting: NAT.
The idea: a guard with a checklist
Picture a guard at each door of the machine with a list of rules. Each packet is compared with the rules from the top; the first rule that matches decides. If none matches, the chain’s policy (its default) decides.
The doors are chains, each at a point in the packet’s path through the kernel:
packet in ──► PREROUTING ──► for me? ──yes──► INPUT ──► program
(nat: DNAT) │
no
▼
FORWARD ──► POSTROUTING ──► packet out
(nat: MASQUERADE)
program ──► OUTPUT ──────────────────────► POSTROUTING
| Chain | Sees packets | Use it for |
|---|---|---|
INPUT | for this machine | protecting a server |
FORWARD | passing through (a router) | filtering between networks |
OUTPUT | sent by this machine | limiting what it sends |
PREROUTING (nat) | as they arrive | rewriting the destination (DNAT) |
POSTROUTING (nat) | as they leave | rewriting the source (MASQUERADE) |
The tool is iptables. Here it is iptables v1.8.11 (nf_tables): it writes its rules into nftables, the
kernel’s current rule engine, which nft reads and writes directly.
DROP: silence
The lab: Practical 1’s red, router and blue, with nc -lk 8080 running in blue. Block the port on blue:
ip netns exec blue iptables -A INPUT -p tcp --dport 8080 -j DROP
-A INPUT appends to the INPUT chain, -p tcp --dport 8080 matches TCP to port 8080, -j DROP is the verdict.
From red:
nc: connect to 10.0.2.2 port 8080 (tcp) timed out: Operation now in progress
after 5 seconds (-w5). tcpdump in blue sees each SYN arrive, and nothing goes back. -L -n -v lists the rules
with counters, the quickest proof that a rule is the one catching your packets:
pkts bytes target prot opt in out source destination
8 480 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
Ping still works: the rule matches only TCP port 8080.
REJECT: an answer
Replace DROP with REJECT, and red gets an answer at once, in 0.003 s:
nc: connect to 10.0.2.2 port 8080 (tcp) failed: Connection refused
On the wire, blue sent an ICMP error instead of silence:
16:18:00.416206 IP 10.0.2.2 > 10.0.1.2: ICMP 10.0.2.2 tcp port 8080 unreachable, length 68
With -j REJECT --reject-with tcp-reset the answer is a TCP RST, Flags [R.], exactly what a closed port sends.
So “Connection refused” can come from a firewall too: lesson 6’s rule (“refused = something answered”) still
holds, but the something may be a REJECT rule. iptables -L -n -v on the server tells them apart.
| Verdict | Sender sees | After |
|---|---|---|
DROP | timed out | its whole time limit |
REJECT | Connection refused (ICMP port unreachable) | at once |
REJECT --reject-with tcp-reset | Connection refused (RST) | at once |
REJECT --reject-with icmp-host-prohibited | No route to host (ICMP host unreachable, admin prohibited) | at once |
A REJECT’s No route to host comes at once; lesson 5’s unanswered ARP takes about 3 seconds.
The right chain, on the right machine
The same DROP in the router’s FORWARD chain (-d 10.0.2.2 -p tcp --dport 8080) gives red the same timeout, and the
router’s rule counts the SYNs (3 180 DROP ... 10.0.2.2 tcp dpt:8080); a DROP rule on blue would count none,
because they never reach it. A firewall can be on any machine on the path, and the rule counters show which one
is dropping.
A server’s firewall usually starts from default deny: drop everything, then allow what you mean.
-P INPUT DROP
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
Red’s nc to 8080 succeeds, and red’s ping to blue gets 100% packet loss. The ESTABLISHED line lets in replies
to connections blue started itself, so blue can still ping out.
NAT: rewriting addresses
Now take away blue’s route back to red (ip -n blue route del default): red’s connection times out, as in lesson 4.
Instead of a route, have the router put its own address on red’s packets as they leave towards blue:
ip netns exec router iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -o r-blue -j MASQUERADE
The connection works. tcpdump on blue’s veth-blue sees it coming from the router:
16:18:35.195561 IP 10.0.2.1.40864 > 10.0.2.2.8080: Flags [S], seq 1982517039, win 64240, ...
and on red’s veth-red (a second connection), red’s SYN still carries its own address:
16:18:36.324131 IP 10.0.1.2.40870 > 10.0.2.2.8080: Flags [S], seq 3671444475, win 64240, ...
Blue answers 10.0.2.1, on its own link, and the router passes the reply back to red. That’s MASQUERADE,
source NAT, and it’s how every container on docker0 reaches the internet with your server’s address.
The other direction is DNAT, destination NAT: a port forward. Make the router’s 10.0.1.1:9090 lead to blue’s
8080:
ip netns exec router iptables -t nat -A PREROUTING -i r-red -p tcp --dport 9090 -j DNAT --to-destination 10.0.2.2:8080
nc -zv 10.0.1.1 9090 from red succeeds, and blue sees 10.0.1.2 > 10.0.2.2.8080: the destination was rewritten,
the source kept. Port 8080 on 10.0.1.1 is still Connection refused: only 9090 is forwarded. A DNAT’d packet is still forwarded, so with net.ipv4.ip_forward=0 in the router it times out
again. This is docker run -p 9090:8080.
Try it on your computer
The capstone, Practical 3 · Capstone: netcheck, has the checker add rules like these to your lab. From the client, a REJECT shows itself (refused at once); a DROP looks exactly like a missing route back (timed out), so your script has to look on the far machines, at rule counters and routes, to tell those apart.
Go deeper: what iptables wrote into nftables
nft list ruleset shows the DROP rule above as iptables stored it:
table ip filter {
chain INPUT {
type filter hook input priority filter; policy accept;
tcp dport 8080 counter packets 8 bytes 480 drop
}
}
A table holds chains; a chain attached to a hook (input, forward, prerouting…) sees the packets at
that point. iptables names its chains in capitals and adds a counter to every rule. The same rule written with
nft itself, in your own table:
nft add table inet lab
nft add chain inet lab input "{ type filter hook input priority 0; }"
nft add rule inet lab input tcp dport 8080 drop
inet covers IPv4 and IPv6 at once. For tables iptables made, nft warns
# Warning: table ip nat is managed by iptables-nft, do not touch!: change those with iptables, and use your own
tables for nft (nft(8)).
Go deeper: what Docker does with these
Docker’s documentation describes the same pieces on your server’s host side: “Docker creates firewall rules to implement network isolation, port publishing and filtering” (packet filtering and firewalls); “outgoing packets from containers are masqueraded”, and a published port is “a firewall rule in the host, mapping a container port to a port on the Docker host” (port publishing).
It also turns on net.ipv4.ip_forward on the host and, when it does, sets the FORWARD chain’s policy to DROP,
then allows its own containers’ traffic. Your own rules for container traffic belong in the DOCKER-USER chain,
which runs before Docker’s (Docker with iptables).
This course reads the host only; its own rules stay in the lab container.
NAT and ESTABLISHED both rest on connection tracking: the kernel remembers each connection, so the reply to a
masqueraded packet is rewritten back, and a reply matches ESTABLISHED
(iptables-extensions(8), conntrack).
Common misunderstandings
- “Refused means there’s no firewall.” A REJECT rule answers with exactly the same error. Counters on the server’s rules say whether a rule caught it.
- “The server’s rules are the only ones that matter.” A FORWARD rule on any router on the path drops the packets before the server ever sees them.
- “A port forward is just one DNAT rule.” The packet is then forwarded like any other: it needs
ip_forward=1, a FORWARD chain that allows it, and a route back from the server.
Check yourself
1. A connection to port 5432 hangs until the client's timeout. tcpdump on the server shows the SYNs arriving. What do you look at first?
The server's INPUT rules with counters: iptables -L INPUT -n -v. A DROP rule whose packet count
grows with each attempt is the cause. (If the counters don't move, check the route back, lesson 4.)
2. You want a blocked port to fail fast for clients instead of hanging. Which rule?
-j REJECT (optionally --reject-with tcp-reset): the client gets "Connection refused" at once
instead of waiting for its timeout.