TCP/IP: layers, handshakes, and packets
How data moves across the internet, from layers and handshakes to reading a packet capture.
TCP/IP is the set of rules that moves data across the internet. You do not need to learn all of it. You need four layers, one handshake, a few flags, and the ability to read a tcpdump line.
The four layers
Another model, OSI, has seven layers. TCP/IP uses four. This is the model the internet actually uses.
| Layer | Examples | What it does |
|---|---|---|
| Application | HTTP, SSH, DNS, SMTP | Holds the message you want to send |
| Transport | TCP, UDP, QUIC | Breaks the message into pieces and checks delivery |
| Internet | IP, ICMP | Finds a path from one computer to another |
| Network access | Ethernet, Wi-Fi, ARP | Moves the data to the next device |
Think of it like mailing a letter. You write the letter first. Then you put it in an envelope. Then you put the envelope in a mail bag. Each layer adds its own wrapper.
An HTTP request becomes a TCP segment. The segment becomes an IP packet. The packet becomes an Ethernet frame. The receiver opens each wrapper in reverse.
TLS, the encryption behind HTTPS, sits between the application and transport layers.
The three-way handshake
TCP needs a connection before it sends data. Both sides must agree to talk. That is the handshake.
Client Server
| |
| ----- SYN, seq=x ---------------> |
| |
| <---- SYN+ACK, seq=y, ack=x+1 ---- |
| |
| ----- ACK, ack=y+1 --------------> |
| |
| ===== application data ==========> |
| |
The client sends a SYN. The server answers with SYN+ACK. The client sends ACK. The connection is now ESTABLISHED.
The sequence numbers are not packet numbers. They count bytes. They let each side say "I got everything up to this byte" and notice gaps. They wrap around when they get too big.
Ports, flags, and states
A port is like an apartment number. The IP address finds the building. The port finds the apartment. A browser connects to port 443 for HTTPS. An SSH client connects to port 22.
The TCP header has flags. These are the main ones.
| Flag | Meaning | When you see it |
|---|---|---|
SYN |
Start a connection | First packet |
ACK |
The acknowledgment field is valid | Almost everything after the first SYN |
FIN |
I am done sending | Closing one direction |
RST |
Reset the connection | Abort, refused connection, or error |
PSH |
Push data to the app now | Interactive traffic, often with ACK |
A bare RST is often the interesting one in security work. It can mean the port is closed, the connection was rejected, or something in the middle killed it.
TCP connections have states:
LISTEN: the server is waiting for a connection.SYN_SENT: the client sent a SYN and is waiting.SYN_RECEIVED: the server sent SYN+ACK and is waiting.ESTABLISHED: the connection is open and data can flow.FIN_WAIT,CLOSE_WAIT,LAST_ACK,TIME_WAIT: the stages of closing.
TIME_WAIT confuses people. After the side that closes first sends its last ACK, it waits about 60 to 120 seconds. That way, a delayed copy of the last ACK cannot confuse a new connection that uses the same port pair.
On a busy server, you will see many TIME_WAIT sockets. That is normal. It is not a leak.
To see connection states on Linux:
ss -tan | grep -E 'ESTAB|TIME-WAIT'
ss has mostly replaced netstat. It is faster and reads directly from the kernel.
Read a packet capture
tcpdump and tshark show you the packets on the wire. tcpdump is on almost every Linux machine. tshark is better when you want decoded fields.
Capture an SSH connection:
# -n: do not resolve names. -c 20: stop after 20 packets.
# -i any: listen on all interfaces.
sudo tcpdump -n -c 20 'tcp port 22' -i any
A SYN line looks like this:
10.0.0.5.54321 > 10.0.0.10.22: Flags [S], seq 1234567890, win 64240, options [mss 1460,sackOK,TS val 123 ecr 0,nop,wscale 7], length 0
Here is what it means:
10.0.0.5.54321 > 10.0.0.10.22: source IP and port, then destination IP and port.Flags [S]: SYN.seq 1234567890: the starting sequence number.win 64240: the receive window, or how much data the sender can accept at once.length 0: no data, which is normal for a SYN.
The options part is TCP negotiating details like the maximum segment size. You can ignore it until something looks wrong.
The flag notation is worth learning:
| tcpdump | Meaning |
|---|---|
[S] |
SYN |
[S.] |
SYN+ACK (the dot is ACK) |
[.] |
ACK |
[P.] |
PSH+ACK |
[F.] |
FIN+ACK |
[R] |
RST |
For decoded fields, tshark can output JSON: tshark -i any -f 'tcp port 22' -c 3 -T json. That is useful for scripts or diffs.
MTU and fragmentation
Each hop has a maximum transmission unit, or MTU. It is the largest IP packet the hop will carry. Ethernet's MTU is usually 1500 bytes.
If a packet is bigger than the next hop's MTU, IPv4 can split it. IPv6 drops it and sends back an ICMP "Packet Too Big" message. ICMP is the protocol ping uses.
Two things matter in practice:
- Path MTU discovery. Most modern systems use it. Firewalls that block ICMP can break it without telling you. Then small packets work, but large packets hang.
- TCP segments should fit. TCP uses MSS, the maximum segment size, to agree on a payload size that fits the path. On Ethernet, that is 1460 bytes with IPv4 (1500 minus 20 for IP and 20 for TCP) and 1440 with IPv6 (1500 minus 40 for IP and 20 for TCP).
Bottom line
The four layers explain the structure. The handshake explains how connections start. The flags and states explain how they behave. tcpdump and tshark let you see it.
Once you can read a single tcpdump line, you can debug most network problems that do not come with a clear error message.
Sources: RFC 9293 defines TCP (it replaces RFC 793), RFC 1122 covers host behavior, and the tcpdump man page is well written.