TCP
- TCP header
- TCP connections
- TCP checksum and pseudo-header
- UDP
TCP header
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 | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
TCP Header Format
TCP Header fields
- Source and Destination port: demultiplexing
- Sequence and acknowledgement: reliable delivery
- Data Offset: header size, options
- Window: flow control
- Checksum: correctness
- Urgent Pointer: "special place" in the data stream
TCP Header bits
- SYN: I want to establish a connection
- FIN: I will never again send data on this connection
- RST: kill this connection
- PSH: immediate delivery of this data is probably a good idea
- URG: the urgent pointer is valid
- ACK: the acknowledgement field is valid (set in all but the
first SYN packet)
TCP connections
- sequence numbers, windows, etc. must be remembered and applied
to incoming packets
- remembering these numbers is a form of state
- since TCP has state, designers decided to have the peers
explicitly manage this state (called a connection)
- the peers agree on when to establish (open) a
connection, when to tear it down (close), and when the
connection must be thrown away (reset)
- the state on each system reflects an understanding about
the state on the peer
TCP connection establishment
- when I receive a request to establish a connection, I must check:
- that I don't already have this socket: one or more of the port
numbers or IP numbers must differ from existing connections
- that an application on my end desires to be connected
- that I have sufficient resources to handle this connection
- the purpose of the connection establishment phase is to
set up consistent connection state on the two peers
TCP State Machine -- Open
+---------+ ---------\ active OPEN
| CLOSED | \ -----------
+---------+<---------\ \ create TCB
| \ \ snd SYN
passive OPEN | | CLOSE \ \
------------ | | ---------- \ \
create TCB | | delete TCB \ \
V | \ \
+---------+ CLOSE | \
| LISTEN | ---------- | |
+---------+ delete TCB | |
rcv SYN | | SEND | |
----------- | | ------- | V
+---------+ snd SYN,ACK / \ snd SYN +---------+
| |<----------------- ------------------>| |
| SYN | rcv SYN | SYN |
| RCVD |<-----------------------------------------------| SENT |
| | snd ACK | |
| |------------------ -------------------| |
+---------+ rcv ACK of SYN \ / rcv SYN,ACK +---------+
| -------------- | | -----------
| x | | snd ACK
| V V
| CLOSE +---------+
| ------- | ESTAB |
| snd FIN +---------+
TCP 3-way handshake
- from state CLOSED:
- send SYN, enter state SYN SENT
- receive SYN and ACK, send ACK,
enter state ESTAB, or
- receive SYN, send ACK, enter state SYN RCVD, then
proceed as below
- from state LISTEN:
- receive SYN, send SYN and ACK,
enter state SYN RCVD
- receive ACK, enter state ESTAB
- retransmissions in case any of these are dropped
- last ack issue
TCP State Machine -- Close
| CLOSE +---------+
| ------- | ESTAB |
| snd FIN +---------+
| CLOSE | | rcv FIN
V ------- | | -------
+---------+ snd FIN / \ snd ACK +---------+
| FIN |<----------------- ------------------>| CLOSE |
| WAIT-1 |------------------ | WAIT |
+---------+ rcv FIN \ +---------+
| rcv ACK of FIN ------- | CLOSE |
| -------------- snd ACK | ------- |
V x V snd FIN V
+---------+ +---------+ +---------+
|FINWAIT-2| | CLOSING | | LAST-ACK|
+---------+ +---------+ +---------+
| rcv ACK of FIN | rcv ACK of FIN |
| rcv FIN -------------- | Timeout=2MSL -------------- |
| ------- x V ------------ x V
\ snd ACK +---------+delete TCB +---------+
------------------------>|TIME WAIT|------------------>| CLOSED |
+---------+ +---------+
TCP close, part 1
- from state ESTAB:
- receive FIN, send ACK, enter state CLOSE WAIT
- application closes connection, send FIN,
enter state LAST ACK
- receive ACK, enter state CLOSED
- from state ESTAB:
- application closes connection, send FIN,
enter state FINWAIT-1
- receive FIN, send ACK, enter state CLOSING
- receive ACK, enter state TIME WAIT
TCP close, part 2
- from state FINWAIT-1, if we get an ACK:
- receive ACK, enter state FINWAIT-2
- receive FIN, send ACK, enter state TIME WAIT
- from state TIME WAIT, enter state CLOSED after 4 minutes
(2 Maximum Segment Lifetimes)
TCP close: example
TCP congestion control
- TCP Reno:
- if a packet is lost, assume it's because of congestion
- slow down significantly (cut window by a factor of two)
- TCP Vegas:
- measure the round-trip time of packets
- if significantly greater than the minimum, assume due
to congestion, slow down slightly (linear)
- use Reno if packets are lost
Checksum
- In TCP and UDP, the checksum covers the header, data, and a pseudo-header
- Unlike TCP, the UDP checksum is optional, and sent as zero (0x0000) if
not computed
- the TCP and UDP pseudo-header has fields derived from the IP header:
- this header is used in computing the checksum, but never sent
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 address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| zero | Protocol | TCP/UDP length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Pseudo Header Format
UDP header format
RFC 768
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 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
UDP Header Format