Why read the TCP header at all?
Every TCP segment you capture, from a web request to a BGP keepalive, carries the same fixed header. If you can read it field by field, packet captures stop being a wall of hex and start telling you what the two endpoints are thinking. This post is a guided tour of that header: what each field is, how to spot it in a capture, and what every flag actually does. The header itself is defined in RFC 9293, which consolidates the original RFC 793.
We deliberately stay on the header here. How a connection is opened is covered in the three-way handshake post, the reasoning behind acknowledgements and retransmission is in TCP reliability explained, and the window field gets its own deep dive in receive window and window scaling.
The layout at a glance
The fixed part of the header is 20 bytes (five 32-bit words). Options can extend it, up to 60 bytes in total.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-------------------------------+-------------------------------+
| Source Port | Destination Port |
+-------------------------------+-------------------------------+
| Sequence Number |
+---------------------------------------------------------------+
| Acknowledgment Number |
+-------+-------+---------------+-------------------------------+
| Data | |C|E|U|A|P|R|S|F| Window |
| Offset| Rsrvd |W|C|R|C|S|S|Y|I| |
| | |R|E|G|K|H|T|N|N| |
+-------+-------+---------------+-------------------------------+
| Checksum | Urgent Pointer |
+-------------------------------+-------------------------------+
| Options (0 to 40 bytes) |
+---------------------------------------------------------------+Ports: how a segment finds its application
The source and destination ports are 16 bits each. IP gets the packet to the right host; the port gets the segment to the right socket. A TCP connection is identified by the four values (source IP, source port, destination IP, destination port), which is why one server port such as 443 can serve thousands of clients at once.
Well-known ports (0-1023): assigned by IANA to services such as 22 (SSH), 80 (HTTP), 179 (BGP).
Registered ports (1024-49151): assigned on request to specific applications.
Ephemeral ports: picked by the client OS for its side of a connection. IANA suggests 49152-65535, but operating systems choose their own range. Linux, for example, defaults to 32768-60999, which you can check with
sysctl net.ipv4.ip_local_port_range.
Practical tip: in a capture, the side using the low, well-known port is almost always the server. That tells you who is who before you read a single flag.
Sequence and acknowledgment numbers
TCP numbers bytes, not packets. The sequence number is the number of the first data byte in the segment (the exception is a SYN, described below). The acknowledgment number, valid when the ACK flag is set, is the next byte the sender of the ACK expects to receive. It is cumulative: "I have everything before this number."
A worked example with made-up values, to show the arithmetic:
Client -> Server seq=1000 len=500 (bytes 1000-1499)
Server -> Client ack=1500 (got through 1499, send 1500 next)
Client -> Server seq=1500 len=500 (bytes 1500-1999)
Server -> Client ack=2000Two details trip people up:
The initial sequence number (ISN) is not 0 or 1. Each side picks its own starting value when the connection opens. RFC 9293 describes a clock-driven ISN scheme and refers to RFC 6528 for hardening it against prediction.
SYN and FIN each consume one sequence number, even though they carry no data. That is why the ACK to a SYN with seq=X is X+1.
Wireshark and tcpdump show relative sequence numbers by default (starting near 0) to keep things readable. When you need to match against another capture point or a log, use the absolute values: tcpdump -S, or turn off "Relative sequence numbers" in Wireshark's TCP preferences.
Data offset and options
The 4-bit data offset (Wireshark calls it Header Length) counts the header size in 32-bit words. The minimum is 5 (20 bytes) and the maximum is 15 (60 bytes), leaving up to 40 bytes for options. If you see a header length of 32 bytes, that is 12 bytes of options. Common options include:
MSS (kind 2): the largest segment payload the sender is willing to receive. Only sent in SYN segments.
Window scale (kind 3, RFC 7323): a shift count that lets the 16-bit window field describe windows larger than 64 KB. Only negotiated in SYN segments.
SACK permitted (kind 4) and SACK (kind 5, RFC 2018): selective acknowledgments for out-of-order data.
Timestamps (kind 8, RFC 7323): used for round-trip time measurement and protection against wrapped sequence numbers.
The reserved bits sit between the data offset and the flags and should be zero.
The flags, one by one
RFC 9293 defines eight single-bit flags. Here is what each one does in practice.
SYN - synchronize sequence numbers. Opens a connection and carries the ISN and the handshake options.
ACK - the acknowledgment number field is valid. Set on virtually every segment after the first SYN.
FIN - the sender has no more data. Closes one direction of the connection, so a clean close takes a FIN from each side.
RST - abort. Used when a segment arrives for a connection or port that does not exist, or when an application tears a connection down abruptly. An RST is why "connection refused" is instant instead of a timeout.
PSH - asks the receiver to pass the data to the application promptly instead of waiting to buffer more. Senders typically set it on the last segment of a write.
URG - the urgent pointer field is significant. RFC 9293 discourages new use of urgent data, and you will rarely see it outside of legacy tools.
ECE and CWR - the congestion signalling pair from ECN (RFC 3168). ECE echoes that congestion was seen; CWR tells the peer the sender has reduced its sending rate in response. A SYN with both set is how ECN is negotiated.
(RFC 3540 once defined a ninth bit, NS, in the reserved area. It was experimental and RFC 9293 treats that bit as reserved again.)
Window, checksum and urgent pointer
The 16-bit window is the amount of data the sender of this segment is currently willing to receive, which is how a receiver applies flow control. Combined with the scale option it can describe much larger windows; see the window scaling post for the details.
The 16-bit checksum covers the TCP header, the payload, and a pseudo-header with the source and destination IP addresses, protocol and length. That last part means TCP detects segments delivered to the wrong address. One capture gotcha: on a host using checksum offload, outgoing packets captured locally can show an "incorrect" checksum because the NIC fills it in after the capture point. It is usually not a real fault.
The urgent pointer is only meaningful with URG set and is, again, rarely used.
Read a real capture
Here is an illustrative tcpdump excerpt (values invented for teaching, not from a specific test). The command uses -nn to skip name resolution and -S for absolute sequence numbers.
$ sudo tcpdump -nn -S -i eth0 'host 192.0.2.10 and tcp port 443'
IP 192.0.2.10.51514 > 198.51.100.20.443: Flags [S], seq 3051002711, win 64240, options [mss 1460,sackOK,TS val 100 ecr 0,nop,wscale 7], length 0
IP 198.51.100.20.443 > 192.0.2.10.51514: Flags [S.], seq 7788001, ack 3051002712, win 65160, options [mss 1460,sackOK,TS val 200 ecr 100,nop,wscale 7], length 0
IP 192.0.2.10.51514 > 198.51.100.20.443: Flags [.], ack 7788002, win 502, length 0
IP 192.0.2.10.51514 > 198.51.100.20.443: Flags [P.], seq 3051002712:3051002893, ack 7788002, win 502, length 181
IP 198.51.100.20.443 > 192.0.2.10.51514: Flags [.], ack 3051002893, win 509, length 0How to read it:
tcpdump prints flags as letters:
SSYN,.ACK,PPSH,FFIN,RRST. So[S.]is SYN plus ACK.Port 443 on one side marks the server; 51514 is the client's ephemeral port.
The server's ack of 3051002712 is the client's ISN plus 1, because the SYN consumed one number.
The data segment covers
3051002712:3051002893, which is 181 bytes. The server's ack of 3051002893 is exactly the end of that range.The
winvalues on SYN segments are not scaled, which is why they differ from the later ones. Those later values (such as 502) are the raw field; with wscale 7 negotiated, multiply by 128 for the real window.
Handy Wireshark display filters for the same fields:
tcp.flags.syn == 1 && tcp.flags.ack == 0 # connection attempts
tcp.flags.reset == 1 # resets
tcp.port == 179 # BGP sessions
tcp.analysis.retransmission # Wireshark's own analysis flagA quick troubleshooting cheat sheet
SYN with no reply: filtered, dropped, or the host is down.
SYN answered by RST: the host is up but nothing is listening on that port.
Repeated identical ACK numbers: the receiver is missing a segment and keeps asking for the same byte; this is the signal that drives fast retransmit.
Window of 0: the receiver's buffer is full and the sender must pause.
Unexpected RST mid-stream: an application closed abruptly, or a middlebox such as a firewall tore the session down.
Try it yourself
Open a capture of any TCP session you own, pick one segment, and fill in the fields on paper: ports, sequence, ack, header length, flags, window. Then check the next segment in the opposite direction and confirm its ack equals your sequence plus the payload length. Doing that a handful of times is the fastest way to make the header second nature.
Watch the video
Prefer video? This topic has a full episode on YouTube.
Next in this series
The TCP Three-Way Handshake: What Happens Before Any Data Moves →
Comments (0)
Please sign in to comment.