Initial state CLOSE D iv ct :a n. tio ca N pli SY Ap nd: se
Application.: passive Öpening Send: ---
Send timeout: RST
eo
A LISTE S ppli ; en ca YN N d: tio e: S ACK SY n.: ceiv N, ST Re d : SY se : R Passive opening N nd sen ive e c da Re ta SYN_REC SYN_SEN Receive: SYN send: SYN, ACK V T Simultanous opening AC K
ng ni pe
Reading: Section 24.3, 24.5
ACK
FIN_WAIT_ 2
Application: close or timeout
Passive close Re Se ceiv nd e: : A FIN CK CLOSE_WA IT Simultanous close Application: close Receive: FIN Send: FIN CLOSIN Send: ACK G R Receive: ACK Se ece LAST_AC Receive: ACK Send: --nd ive :A :F K Send: --CK IN ,A C K TIME_WA IT Receive: FIN Send: ACK 2 MSL timeout Active close
Ap Se p lica nd tio : F n: IN clo se
FIN_WAIT_ 1 Receive: Send: ---
Re Se ceiv nd e: : A SY CK N ,
CK
Application: close Send: FIN
:A ive ce --Re nd : Se
TCP: Connection and Timer Management
ESTABLISHE D
Data transmission
MSL: max. segment life
Fall 2004
FSU CIS 5930 Internet Protocols
1
Fall 2004
FSU CIS 5930 Internet Protocols
2
Transition from CLOSED to SYN_SENT
Road to establish a connection • LISTEN
• • • • •
– Passive opening, waiting for connection request from others
• SYN_SENT – After sending SYN, but before receiving ACK
• SYN_RECV – Receiving SYN from others
• ESTABLISHED
User/application calls connect() socket API sys_socketcall maps it to tcp_v4_connect() tcp_v4_connect() invokes tcp_connect() tcp_connect() sends SYN packet Changes state from CLOSED to SYN_SENT
– Finished three-way handshaking Fall 2004
FSU CIS 5930 Internet Protocols
3
Fall 2004
FSU CIS 5930 Internet Protocols
4
1
Transition from LISTEN to SYN_RECV
Transition from SYN_SENT to ESTABLISHED
• • • •
User/application calls listen() socket API Changing state from CLOSED to LISTEN Receiving SYN packet from another party Changing state from LISTEN to SYN_RECV • Sending packet with SYN and ACK Fall 2004
FSU CIS 5930 Internet Protocols
• • • •
5
Fall 2004
Transition from SYN_SENT to SYN_RECV • • • •
Currently in SYN_SENT state Receiving packet with SYN and ACK Sending ACK Changing from SYN_SENT to ESTABLISHED state
FSU CIS 5930 Internet Protocols
6
Transition from SYN_RECV to ESTABLISHED
Currently in SYN_SENT state Receiving SYN packet (without ACK) Sending SYN and ACK Changing to SYN_RECV state
• • • •
Currently in SYN_RECV state (we sent SYN/ACK) Receiving ACK packet Changing to ESTABLISHED state
• Simultaneous connection establishment Fall 2004
FSU CIS 5930 Internet Protocols
7
Fall 2004
FSU CIS 5930 Internet Protocols
8
2
tcp_rcv_state_process()
Tearing down a connection • Two ways to terminate a connection
• Big function to handle TCP state transitions • Specific behavior depending on current state and packet received
– Graceful close: all data transmitted – Abort: data can get lost
• Closing related state – – – – – – –
if (th->ack) { switch (sk->state) { case TCP_SYN_RECV: … tcp_set_state(sk, TCP_ESTABLISHED); } } Fall 2004
FSU CIS 5930 Internet Protocols
9
FIN_WAIT_1: We close but not receive ACK FIN_WAIT_2: We close and receive ACK CLOSING: Both FINed, waiting for ACK TIME_WAIT: graceful close (wait some time) CLOSE_WAIT: They close and we ACK LAST_ACK: They close, then we close, waiting for ACK CLOSED: connection is now closed
Fall 2004
FSU CIS 5930 Internet Protocols
10
Transition from ESTABLISHED to FIN_WAIT_1
Transition from ESTABLISHED to CLOSE_WAIT
• • • • •
• • • •
User/application calls close() socket API sys_socketcall() maps it to sys_shutdown() Which calls tcp_close() (in TCP case) Sending FIN packet Changing state from ESTABLISHED to FIN_WAIT_1
Currently in ESTABLISHED state Receiving FIN packet Sending ACK to FIN Changing state from ESTABLISHED to CLOSE_WAIT
• tcp_fin() Fall 2004
FSU CIS 5930 Internet Protocols
11
Fall 2004
FSU CIS 5930 Internet Protocols
12
3
Transition from CLOSE_WAIT to LAST_ACK
Transition from FIN_WAIT_1 to FIN_WAIT_2
• Currently in CLOSE_WAIT state • (receiving FIN from another party) • Finally we finish data transmission, we also close • We send FIN packet • Changing to LAST_ACK state to wait for the ACK packet
• • • •
Fall 2004
Fall 2004
FSU CIS 5930 Internet Protocols
13
Currently we are in FIN_WAIT_1 (We sent FIN) Receiving ACK (to FIN) Changing state from FIN_WAIT_1 to FIN_WAIT_2 • (we have not received FIN from another party) FSU CIS 5930 Internet Protocols
14
Transition from FIN_WAIT_2 to TIME_WAIT
Transition from FIN_WAIT_1 to TIME_WAIT
• • • •
• • • •
We are in FIN_WAIT_2 (we sent FIN and ACKed) Receiving FIN, sending ACK Changing state from FIN_WAIT_2 to TIME_WAIT
Currently we are in FIN_WAIT_1 (we sent FIN) Receiving ACK and FIN Changing state from FIN_WAIT_1 to TIME_WAIT
• For graceful close, wait for 2 MSL Fall 2004
FSU CIS 5930 Internet Protocols
15
Fall 2004
FSU CIS 5930 Internet Protocols
16
4
Transition from FIN_WAIT_1 to CLOSING
Transition from CLOSING to TIME_WAIT
Currently in FIN_WAIT_1 state (we sent FIN but not ACKed) Receiving FIN Sending ACK Changing from FIN_WAIT_1 to CLOSING state • Waiting for ACK
• • • •
Fall 2004
Fall 2004
• • • • •
FSU CIS 5930 Internet Protocols
17
Currently in CLOSING state Both sides FINed We Acked another party We waiting for being ACKed by another party • Receiving ACK • Changing state from CLOSING to TIME_WAIT
Timer management
struct timer_list { struct list_head list; unsigned long expires; unsigned long data; void (*function)(unsigned long); volatile int running; }
– SYNACK: waiting for ACK to our SYN – Retransmit: for data retransmission, exponential backoff – Delay ACK: hoping for piggy-back ACK – Keepalive: checking if a connection alive – Probe: testing if zero window size still applies – FIN_WAIT_2: switch to CLOSED if no FIN received – TWKill: how long to stay in TIME_WAIT FSU CIS 5930 Internet Protocols