Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1 · What happens when you connect

In one sentence: a connection is a short chain of questions (which address, which way, who on the link, allowed through, anyone listening, any answer), and every “it can’t connect” is one of them answered “no”.

The problem it solves

Your program calls http://web:8080/ and gets an error. Here is the same curl command failing five times, each time for a different reason, on two “machines” joined by a virtual cable (the lab you’ll build in Practical 0, plus the breakages that lessons 3 to 8 teach):

What was wrongcurl -sS printsaftercurl -v addsQuestion (lesson)
no machine is called webcurl: (6) Could not resolve host: webunder a second(no address to try)1 (8)
this machine has no way to 10.0.0.2curl: (7) Failed to connect to 10.0.0.2 port 8080 after 0 ms: Could not connect to server0 msNetwork is unreachable2 (3, 4)
no machine on the link has the addresscurl: (7) Failed to connect to 10.0.0.9 port 8080 after 3076 ms: Could not connect to server3 sNo route to host3 (5)
a firewall throws the packets awaycurl: (28) Connection timed out after 5002 milliseconds5 s(nothing came back)4 (7)
no program listens on port 8080curl: (7) Failed to connect to 10.0.0.2 port 8080 after 0 ms: Could not connect to server0 msConnection refused5 (6)

Three different problems print the same sentence. Guessing from it (“must be the firewall”) turns an outage into an afternoon. Knowing what happens, in what order, when a connection is made lets you check each step instead.

The idea: six questions in order

Think of posting a parcel to a friend in a big apartment building.

  1. Name → address. You know “Sam”; the courier needs a street address. Computers know names like web but send to numbers, IP addresses such as 10.0.0.2. Looking a name up is DNS (or the file /etc/hosts).
  2. Which way? Is the address on your street, or does the parcel go via the depot? The routing table answers for each address: which interface (network card) to use, and through which machine.
  3. Which door? On your street, which house is it? On a network link, “who has 10.0.0.2?” is answered by ARP: the owner replies with its MAC address, the hardware address of its network card.
  4. Let through? A guard at the gate may refuse parcels. A firewall may drop packets or reject them.
  5. Which flat? A building has many flats; a machine has many programs. The port number (8080) says which one. A program has to be listening on it.
  6. Does someone answer? The program reads the request and replies. That’s the application, HTTP here.

Data travels as packets: small chunks, each with the sender’s and receiver’s addresses on the front.

What it looks like on the wire

tcpdump prints every packet that crosses an interface. Here is the one successful curl from red (10.0.0.1) to the server on blue (10.0.0.2), seen on blue’s end of the cable, long lines cut:

14:06:16.256706 ARP, Request who-has 10.0.0.2 tell 10.0.0.1, length 28
14:06:16.256718 ARP, Reply 10.0.0.2 is-at 1a:b5:b8:0f:df:2d, length 28
14:06:16.256726 IP 10.0.0.1.47446 > 10.0.0.2.8080: Flags [S], seq 1246910117, win 64240, ...
14:06:16.256739 IP 10.0.0.2.8080 > 10.0.0.1.47446: Flags [S.], seq 2919238786, ack 1246910118, ...
14:06:16.256751 IP 10.0.0.1.47446 > 10.0.0.2.8080: Flags [.], ack 1, win 502, ...
14:06:16.256811 IP 10.0.0.1.47446 > 10.0.0.2.8080: Flags [P.], seq 1:78, ack 1, ... length 77: HTTP: GET / HTTP/1.1
...
14:06:16.256824 IP 10.0.0.2.8080 > 10.0.0.1.47446: Flags [P.], seq 1:64, ack 78, ... length 63: HTTP: HTTP/1.1 200 OK
...
14:06:16.256861 IP 10.0.0.2.8080 > 10.0.0.1.47446: Flags [F.], seq 64, ack 78, ...

Top to bottom, the questions are answered in order. The name was already an address. Two ARP packets find blue’s MAC. Then the TCP handshake: [S] (SYN, “can we talk?”), [S.] (SYN-ACK, “yes”), [.] (ACK, “good”). Then the request, the answer, and [F.] (FIN) to close. It took 0.2 milliseconds.

Each failure in the table at the top stops at one of these questions (its last column). The plain message hides the difference; the time and curl -v (which prints each step curl takes) show it.

Layers

The six questions are usually drawn as layers, each built on the one below:

 application   HTTP, DNS            question 6 (and 1: DNS is an application too)
 transport     TCP, UDP: ports      question 5
 network       IP: addresses,       questions 2 and 4
               routes, firewall
 link          Ethernet: MAC, ARP   question 3

When you debug, ask the questions in order: each depends on the ones before it, so there’s no point checking the port while there’s no route. The capstone of this course is a script that does exactly that.

Try it on your computer

Practical 0, after lesson 2, has you build the two machines, red and blue, and the cable these outputs came from. The later practicals add the router, the switch and the firewall.

Go deeper: why the timings differ
  • 0 ms, refused. Blue’s kernel (the core of Linux, which does all the networking) got the SYN, found nothing listening on 8080, and answered with a TCP reset (Flags [R.] in tcpdump) 13 microseconds later.
  • 0 ms, unreachable. Red’s own routing table had no route, so the kernel refused before sending anything.
  • About 3 s, no route to host. Red sent 3 ARP requests for 10.0.0.9, about 1 s apart, and nobody answered. The kernel then marked the neighbour FAILED (ip neigh shows 10.0.0.9 dev veth-red FAILED) and told curl No route to host. The 3 and the 1 s are the settings mcast_solicit = 3 and retrans_time_ms = 1000 on red’s end of the cable (ip-sysctl, neighbour section).
  • 5 s, timed out. The firewall dropped the SYN silently, and red’s kernel kept resending it. curl stopped at the --max-time 5 it was given. Without a limit, on this machine’s kernel, red resent the SYN 10 times (the last 68 s after the first), and curl gave up after 134 s. The settings behind it are tcp_syn_retries and tcp_syn_linear_timeouts (same page, TCP section).

So fast means someone said no; slow means nobody said anything.

Common misunderstandings

  • “Connection refused means a firewall.” Refused means a machine answered “nothing listens there”. The network worked. A firewall that drops shows up as a timeout instead (lesson 7 shows the REJECT case).
  • “ping works, so the service is fine.” Ping tests questions 2–4 with a different protocol (ICMP, the protocol ping uses, with no ports). It says nothing about the port or the program.

Check yourself

1. curl fails "after 0 ms" with Could not connect to server. Which two questions could it be, and how do you tell? Route (2) or port (5): both are immediate. curl -v says Network is unreachable for a missing route, Connection refused for no listener.
2. A request hangs for exactly as long as your client's timeout. Which question is the likeliest suspect, and why not "nobody listening"? A firewall dropping packets (4): nothing comes back, so the client waits. If nobody were listening, the other machine would answer "refused" at once.
3. A colleague has a web server that answers ping but not curl. Which questions has ping already answered "yes"? Route, neighbour, and that the firewall lets ICMP through: 2, 3 and part of 4. Still open: whether the firewall lets TCP to that port through, whether anything listens on it, and whether it answers.

Next: lesson 2 · The Linux network stack.