OSI 7 Layers
lAYER INDEX | LAYER | Protocol Data Unit(PDU) | Functions | Protocals |
---|---|---|---|---|
7 | Application | Data | TFTP,HTTP,SNMP,FTP,SMTP,DNS,Telnet, DHCP | |
6 | Presentaton | Data | encoding, decoding, encrypt, decrypt | SSL, WPA, WEP, Kerberos |
5 | Session | Data | maintaining communication between two devices | PPTP, TSL/SSL, RTP, NetBios, SOCKS, RTP |
4 | Transport | Segments | TCP,UDP | |
3 | Network | Packets | Add logical address of sender and receiver | IP,ICMP,RIP,OSPF,BGP,IGMP |
2 | Data link | Frame | Error control | SLIP,CSLIP,PPP,ARP,RARP,MTU |
1 | Physical | Bits | SO2110,IEEE802,IEEE802.2 |
Five Layers
lAYER | LAYER | Value |
---|---|---|
5 | Application | Data |
4 | Transport | Segments |
3 | Network | Packets |
2 | datalink | Frame |
1 | Physical | Bits |
Protocals Relationships
Based on RFC793(RFC, 1981):
+------+ +-----+ +-----+ +-----+
|Telnet| | FTP | |Voice| ... | | Application Level
+------+ +-----+ +-----+ +-----+
| | | |
+-----+ +-----+ +-----+
| TCP | | RTP | ... | | Host Level
+-----+ +-----+ +-----+
| | |
+-------------------------------+
| Internet Protocol & ICMP | Gateway Level
+-------------------------------+
|
+---------------------------+
| Local Network Protocol | Network Level
+---------------------------+
TCP
- Connection Oriented
- Reliable
- ACK, SYN (every message)
- Sender -> SYN
- Receiver -> ACK
- Congestion control
Based on RFC793(RFC, 1981):
TCP Header Format
-
Sequence Number: 32 bits
The sequence number of the first data octet in this segment (except
when SYN is present). If SYN is present, the sequence number is the
initial sequence number (ISN) and the first data octet is ISN+1. -
Acknowledgment Number: 32 bits
If the ACK control bit is set this field contains the value of the
next sequence number the sender of the segment is expecting to
receive. Once a connection is established this is always sent. -
Data Offset: 4 bits
The number of 32 bit words in the TCP Header. This indicates where
the data begins. The TCP header (even one including options) is an
integral number of 32 bits long. -
Reserved: 6 bits
Reserved for future use. Must be zero.
-
Control Bits: 6 bits (from left to right):
Name Explain URG Urgent Pointer field significant ACK Acknowledgment field significant PSH Push Function RST Reset the connection SYN Synchronize sequence numbers FIN No more data from sender -
Window: 16 bits
The number of data octets beginning with the one indicated in the acknowledgment field which the sender of this segment is willing to accept.(该段的发送者愿意接受的ACK字段的起始位置)
-
Checksum: 16 bits
The checksum field is the 16 bit one’s complement of the one’s complement sum of all 16 bit words in the header and text. If a segment contains an odd number of header and text octets to be checksummed, the last octet is padded on the right with zeros to form a 16 bit word for checksum purposes. The pad is not transmitted as part of the segment. While computing the checksum, the checksum field itself is replaced with zeros.
-
Urgent Pointer: 16 bits
This field communicates the current value of the urgent pointer as a positive offset from the sequence number in this segment(Urgent Pointer代表从sequence number开始的正向偏移值). The urgent pointer points to the sequence number of the octet following the urgent data. This field is only be interpreted in segments with the URG control bit set.
-
Options: variable
术语
- 发送序列变量(Send Sequence Variables)
term | explain |
---|---|
SND.UNA | send unacknowledged |
SND.NXT | send next |
SND.WND | send window |
SND.UP | send urgent pointer |
SND.WL1 | segment sequence number used for last window update |
SND.WL2 | segment acknowledgment number used for last window update |
ISS | initial send sequence number |
- 接收序列变量 (Receive Sequence Variables)
term | explain |
---|---|
RCV.NXT | receive next |
RCV.WND | receive window |
RCV.UP | receive urgent pointer |
IRS | initial receive sequence number |
- Current Segment Variables
term | explain |
---|---|
SEG.SEQ | segment sequence number |
SEG.ACK | segment acknowledgment number |
SEG.LEN | segment length |
SEG.WND | segment window |
SEG.UP | segment urgent pointer |
- ELSE
term | explain |
---|---|
TCB | transmission control block,created and partially filled in with data from the OPEN command parameters. |
A connection progresses through a series of states during its lifetime. The states are: LISTEN, SYN-SENT, SYN-RECEIVED, ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT, and the fictional state CLOSED. CLOSED (虚构的)is fictional because it represents the state when there is no TCB, and therefore, no connection. Briefly the meanings of the states are:
-
LISTEN - represents waiting for a connection request from any remote TCP and port. 等待任何远程的 TCP 和端口连接请求
-
SYN-SENT - represents waiting for a matching connection request
after having sent a connection request. -
SYN-RECEIVED - represents waiting for a confirming connection
request acknowledgment after having both received and sent a
connection request. -
ESTABLISHED - represents an open connection, data received can be
delivered to the user. The normal state for the data transfer phase
of the connection. -
FIN-WAIT-1 - represents waiting for a connection termination request
from the remote TCP, or an acknowledgment of the connection
termination request previously sent. -
FIN-WAIT-2 - represents waiting for a connection termination request
from the remote TCP. -
CLOSE-WAIT - represents waiting for a connection termination request
from the local user. -
CLOSING - represents waiting for a connection termination request
acknowledgment from the remote TCP. -
LAST-ACK - represents waiting for an acknowledgment of the
connection termination request previously sent to the remote TCP
(which includes an acknowledgment of its connection termination
request). -
TIME-WAIT - represents waiting for enough time to pass to be sure
the remote TCP received the acknowledgment of its connection
termination request. -
CLOSED - represents no connection state at all.
+---------+ ---------\ 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 +---------+
| 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 |
+---------+ +---------+
Figure 6: TCP Connection State Diagram
TCP 3-Way Handshake Process
TCP P TCP Q
1. CLOSED LISTEN
2. SYN-SENT --> <SEQ=100><CTL=SYN> --> SYN-RECEIVED
3. ESTABLISHED <-- <SEQ=300><ACK=101><CTL=SYN,ACK> <-- SYN-RECEIVED
4. ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK> --> ESTABLISHED
5. ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK><DATA> --> ESTABLISHED
Basic 3-Way Handshake for Connection Synchronization
- Step 1 (SYN) : In the first step, client wants to establish a connection with server, so it sends a segment with SYN(Synchronize Sequence Number) which informs server that client is likely to start communication and with what sequence number it starts segments with
- Step 2 (SYN + ACK): Server responds to the client request with SYN-ACK signal bits set. Acknowledgement(ACK) signifies the response of segment it received and SYN signifies with what sequence number it is likely to start the segments with
- Step 3 (ACK) : In the final part client acknowledges the response of server and they both establish a reliable connection with which they will start the actual data transfer
假定当发生崩溃导致A的TCP内存丢失时,两个用户进程A和B正在相互通信。支持A的TCP的操作系统可能存在某种错误恢复机制。当TCP再次启动时,A可能会从头开始或从恢复点重新开始。结果,A可能会尝试再次打开连接或尝试发送它认为已打开的连接。在后一种情况下,它从本地(A的)TCP收到错误消息“连接未打开”。为了建立连接,A的TCP将发送一个包含SYN的段。这种情况导致出现下图所示的示例。TCP A崩溃后,用户尝试重新打开连接。同时,TCP B认为连接已打开。
Half-Open Connection Discovery
TCP A TCP B
1. (CRASH) (send 300,receive 100)
2. CLOSED ESTABLISHED
3. SYN-SENT --> <SEQ=400><CTL=SYN> --> (??)
4. (!!) <-- <SEQ=300><ACK=100><CTL=ACK> <-- ESTABLISHED
5. SYN-SENT --> <SEQ=100><CTL=RST> --> (Abort!!)
6. SYN-SENT CLOSED
7. SYN-SENT --> <SEQ=400><CTL=SYN> -->
Half-Open Connection Discovery
Why Three-way Handshake
三步握手是为了防止已经建立的连接又再次建立,这会造成混乱。因此,TCP设计了reset控制信息字段。如果接收到的TCP是一个非同步状态 (i.e., SYN-SENT, SYN-RECEIVED),它就会转而去监听reset字段。一旦TCP处在一个同步状态 (ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT),它就会放弃这个连接并通知用户(RFC, 1981)。
References
- RFC, Transmission Control Protocal, 1981 , viewed 22 Feb 2020, https://tools.ietf.org/html/rfc793