Extracts from RFC793: https://tools.ietf.org/html/rfc793
TCP Header Format
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 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Source Port: 16 bits
- The source port number.
- Destination Port: 16 bits
- The destination port number.
- 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):
- 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.
- 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.
The checksum also covers a 96 bit pseudo header conceptually prefixed to the TCP header. This pseudo header contains the Source Address, the Destination Address, the Protocol, and TCP length. This gives the TCP protection against misrouted segments. This information is carried in the Internet Protocol and is transferred across the TCP/Network interface in the arguments or results of calls by the TCP on the IP.
+--------+--------+--------+--------+
| Source Address |
+--------+--------+--------+--------+
| Destination Address |
+--------+--------+--------+--------+
| zero | PTCL | TCP Length |
+--------+--------+--------+--------+
The TCP Length is the TCP header length plus the data length in octets (this is not an explicitly transmitted quantity, but is computed), and it does not count the 12 octets of the pseudo header.
- 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. 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
- Options may occupy space at the end of the TCP header and are a multiple of 8 bits in length. All options are included in the checksum. An option may begin on any octet boundary. There are two cases for the format of an option:
- Case 1: A single octet of option-kind.
- Case 2: An octet of option-kind, an octet of option-length, and the actual option-data octets. The option-length counts the two octets of option-kind and option-length as well as the option-data octets. Note that the list of options may be shorter than the data offset field might imply. The content of the header beyond the End-of-Option option must be header padding (i.e., zero).
- Options may occupy space at the end of the TCP header and are a multiple of 8 bits in length. All options are included in the checksum. An option may begin on any octet boundary. There are two cases for the format of an option:
A TCP must implement all options. Currently defined options include (kind indicated in octal):
Kind Length Meaning
---- ------ -------
0 - End of option list.
1 - No-Operation.
2 4 Maximum Segment Size.
End of Option List
+--------+
|00000000|
+--------+
Kind=0
This option code indicates the end of the option list. This might not coincide with the end of the TCP header according to the Data Offset field. This is used at the end of all options, not the end of each option, and need only be used if the end of the options would not otherwise coincide with the end of the TCP header.
No-Operation
+--------+
|00000001|
+--------+
Kind=1
This option code may be used between options, for example, to align the beginning of a subsequent option on a word boundary. There is no guarantee that senders will use this option, so receivers must be prepared to process options even if they do not begin on a word boundary.
Maximum Segment Size
+--------+--------+---------+--------+
|00000010|00000100| max seg size |
+--------+--------+---------+--------+
Kind=2 Length=4
- 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
Send Sequence Space
1 2 3 4
----------|----------|----------|----------
SND.UNA SND.NXT SND.UNA
+SND.WND
- old sequence numbers which have been acknowledged
- sequence numbers of unacknowledged data
- sequence numbers allowed for new data transmission
- future sequence numbers which are not yet allowed
- RCV.NXT = receive next
- RCV.WND = receive window
- RCV.UP = receive urgent pointer
- IRS = initial receive sequence number
Receive Sequence Space
1 2 3
----------|----------|----------
RCV.NXT RCV.NXT
+RCV.WND
- old sequence numbers which have been acknowledged
- sequence numbers allowed for new reception
- future sequence numbers which are not yet allowed
- SEG.SEQ = segment sequence number
- SEG.ACK = segment acknowledgment number
- SEG.LEN = segment length
- SEG.WND = segment window
- SEG.UP = segment urgent pointer
- SEG.PRC = segment precedence value
A connection progresses through a series of states during its lifetime. Note that CLOSED state 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.
- 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.
A TCP connection progresses from one state to another in response to events. The events are :
- the user calls, OPEN, SEND, RECEIVE, CLOSE, ABORT, and STATUS;
- the incoming segments, particularly those containing the SYN, ACK, RST and FIN flags;
- and timeouts.
This state diagram illustrates only state changes, together with the causing events and resulting actions, but addresses neither error conditions nor actions which are not connected with state changes.
+---------+ ---------\ 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 |
+---------+ +---------+
In response to sending data the TCP will receive acknowledgments. The following comparisons are needed to process the acknowledgments.
- SND.UNA == oldest unacknowledged sequence number ?
- SND.NXT == next sequence number to be sent ?
- SEG.ACK == acknowledgment from the receiving TCP (next sequence number expected by the receiving TCP) ?
- SEG.SEQ == first sequence number of a segment ?
- SEG.LEN == the number of octets occupied by the data in the segment (counting SYN and FIN) ?
- SEG.SEQ + SEG.LEN - 1 == last sequence number of a segment ?
A new acknowledgment (called an "acceptable ack"), is one for which the inequality below holds:
- SND.UNA < SEG.ACK =< SND.NXT
A segment on the retransmission queue is fully acknowledged if the sum of its sequence number and length is less or equal than the acknowledgment value in the incoming segment.
When data is received the following comparisons are needed:
- RCV.NXT == next sequence number expected on an incoming segments, and is the left or lower edge of the receive window ?
- RCV.NXT + RCV.WND - 1 == last sequence number expected on an incoming segment, and is the right or upper edge of the receive window ?
- SEG.SEQ == first sequence number occupied by the incoming segment ?
- SEG.SEQ + SEG.LEN - 1 == last sequence number occupied by the incoming segment ?
A segment is judged to occupy a portion of valid receive sequence space if
- RCV.NXT =< SEG.SEQ < RCV.NXT + RCV.WND
or
- RCV.NXT =< SEG.SEQ + SEG.LEN - 1 < RCV.NXT + RCV.WND
The first part of this test checks to see if the beginning of the segment falls in the window, the second part of the test checks to see if the end of the segment falls in the window; if the segment passes either part of the test it contains data in the window. Actually, it is a little more complicated than this. Due to zero windows and zero length segments, we have four cases for the acceptability of an incoming segment:
Segment Receive Test
Length Window
------- ------- -------------------------------------------
0 0 SEG.SEQ = RCV.NXT
0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
>0 0 not acceptable
>0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND
Note that when the receive window is zero no segments should be acceptable except ACK segments. Thus, it is be possible for a TCP to maintain a zero receive window while transmitting data and receiving ACKs. However, even when the receive window is zero, a TCP must process the RST and URG fields of all incoming segments.
We have taken advantage of the numbering scheme to protect certain control information as well. This is achieved by implicitly including some control flags in the sequence space so they can be retransmitted and acknowledged without confusion (i.e., one and only one copy of the control will be acted upon). Control information is not physically carried in the segment data space. Consequently, we must adopt rules for implicitly assigning sequence numbers to control. The SYN and FIN are the only controls requiring this protection, and these controls are used only at connection opening and closing. For sequence number purposes, the SYN is considered to occur before the first actual data octet of the segment in which it occurs, while the FIN is considered to occur after the last actual data octet in a segment in which it occurs. The segment length (SEG.LEN) includes both data and sequence space occupying controls. When a SYN is present then SEG.SEQ is the sequence number of the SYN.
When new connections are created, an initial sequence number (ISN) generator is employed which selects a new 32 bit ISN. The generator is bound to a (possibly fictitious) 32 bit clock whose low order bit is incremented roughly every 4 microseconds. Thus, the ISN cycles approximately every 4.55 hours. Since we assume that segments will stay in the network no more than the Maximum Segment Lifetime (MSL) and that the MSL is less than 4.55 hours we can reasonably assume that ISN’s will be unique.
For each connection there is a send sequence number and a receive sequence number. The initial send sequence number (ISS) is chosen by the data sending TCP, and the initial receive sequence number (IRS) is learned during the connection establishing procedure.
For a connection to be established or initialized, the two TCPs must synchronize on each other’s initial sequence numbers. This is done in an exchange of connection establishing segments carrying a control bit called "SYN" (for synchronize) and the initial sequence numbers. As a shorthand, segments carrying the SYN bit are also called "SYNs". Hence, the solution requires a suitable mechanism for picking an initial sequence number and a slightly involved handshake to exchange the ISN’s. The synchronization requires each side to send it’s own initial sequence number and to receive a confirmation of it in acknowledgment from the other side. Each side must also receive the other side’s initial sequence number and send a confirming acknowledgment.
- A --> B
- SYN my sequence number is X
- A <-- B
- ACK your sequence number is X
- SYN my sequence number is Y
- A --> B
- ACK your sequence number is Y
This is called the three way (or three message) handshake.
A three way handshake is necessary because sequence numbers are not tied to a global clock in the network, and TCPs may have different mechanisms for picking the ISN’s. The receiver of the first SYN has no way of knowing whether the segment was an old delayed one or not, unless it remembers the last sequence number used on the connection (which is not always possible), and so it must ask the sender to verify this SYN.
Once the connection is established data is communicated by the exchange of segments. Because segments may be lost due to errors (checksum test failure), or network congestion, TCP uses retransmission (after a timeout) to ensure delivery of every segment. Duplicate segments may arrive due to network or TCP retransmission. As discussed in the section on sequence numbers the TCP performs certain tests on the sequence and acknowledgment numbers in the segments to verify their acceptability.
- The sender of data keeps track of the next sequence number to use in the variable SND.NXT
- The receiver of data keeps track of the next sequence number to expect in the variable RCV.NXT
- The sender of data keeps track of the oldest unacknowledged sequence number in the variable SND.UNA
- If the data flow is momentarily idle and all data sent has been acknowledged then the three variables will be equal
- When the sender creates a segment and transmits it the sender advances SND.NXT
- When the receiver accepts a segment it advances RCV.NXT and sends an acknowledgment
- When the data sender receives an acknowledgment it advances SND.UNA
- The extent to which the values of these variables differ is a measure of the delay in the communication
- The amount by which the variables are advanced is the length of the data in the segment
- Note that once in the ESTABLISHED state all segments must carry current acknowledgment information
- The CLOSE user call implies a push function, as does the FIN control flag in an incoming segment
Because of the variability of the networks that compose an internetwork system and the wide range of uses of TCP connections the retransmission timeout must be dynamically determined. One procedure for determining a retransmission time out is given here as an illustration.
An Example Retransmission Timeout Procedure:
- Measure the elapsed time between sending a data octet with a particular sequence number and receiving an acknowledgment that
covers that sequence number (segments sent do not have to match segments received).
- This measured elapsed time is the Round Trip Time (RTT).
- Next compute a Smoothed Round Trip Time (SRTT) as:
- SRTT = ( ALPHA * SRTT ) + ((1 - ALPHA) * RTT)
- and based on this, compute the retransmission timeout (RTO) as:
- RTO = min[UBOUND,max[LBOUND,(BETA * SRTT)]]
- where UBOUND is an upper bound on the timeout (e.g., 1 minute),
- LBOUND is a lower bound on the timeout (e.g., 1 second),
- ALPHA is a smoothing factor (e.g., .8 to .9),
- and BETA is a delay variance factor (e.g., 1.3 to 2.0).
- RTO = min[UBOUND,max[LBOUND,(BETA * SRTT)]]
The objective of the TCP urgent mechanism is to allow the sending user to stimulate the receiving user to accept some urgent data and to permit the receiving TCP to indicate to the receiving user when all the currently known urgent data has been received by the user.
This mechanism permits a point in the data stream to be designated as the end of urgent information. Whenever this point is in advance of the receive sequence number (RCV.NXT) at the receiving TCP, that TCP must tell the user to go into "urgent mode"; when the receive sequence number catches up to the urgent pointer, the TCP must tell user to go into "normal mode". If the urgent pointer is updated while the user is in "urgent mode", the update will be invisible to the user.
The method employs a urgent field which is carried in all segments transmitted. The URG control flag indicates that the urgent field is meaningful and must be added to the segment sequence number to yield the urgent pointer. The absence of this flag indicates that there is no urgent data outstanding.
To send an urgent indication the user must also send at least one data octet. If the sending user also indicates a push, timely delivery of the urgent information to the destination process is enhanced.
The window sent in each segment indicates the range of sequence numbers the sender of the window (the data receiver) is currently prepared to accept. There is an assumption that this is related to the currently available data buffer space available for this connection.
Indicating a large window encourages transmissions. If more data arrives than can be accepted, it will be discarded. This will result in excessive retransmissions, adding unnecessarily to the load on the network and the TCPs. Indicating a small window may restrict the transmission of data to the point of introducing a round trip delay between each new segment transmitted.
The mechanisms provided allow a TCP to advertise a large window and to subsequently advertise a much smaller window without having accepted that much data. This, so called "shrinking the window," is strongly discouraged. The robustness principle dictates that TCPs will not shrink the window themselves, but will be prepared for such behavior on the part of other TCPs.
The sending TCP must be prepared to accept from the user and send at least one octet of new data even if the send window is zero. The sending TCP must regularly retransmit to the receiving TCP even when the window is zero. Two minutes is recommended for the retransmission interval when the window is zero. This retransmission is essential to guarantee that when either TCP has a zero window the re-opening of the window will be reliably reported to the other.
When the receiving TCP has a zero window and a segment arrives it must still send an acknowledgment showing its next expected sequence number and current window (zero).
The sending TCP packages the data to be transmitted into segments which fit the current window, and may repackage segments on the retransmission queue. Such repackaging is not required, but may be helpful.
In a connection with a one-way data flow, the window information will be carried in acknowledgment segments that all have the same sequence number so there will be no way to reorder them if they arrive out of order. This is not a serious problem, but it will allow the window information to be on occasion temporarily based on old reports from the data receiver. A refinement to avoid this problem is to act on the window information from segments that carry the highest acknowledgment number (that is segments with acknowledgment number equal or greater than the highest previously received).
The window management procedure has significant influence on the communication performance. The following comments are suggestions to implementers.
Window Management Suggestions:
- Allocating a very small window causes data to be transmitted in many small segments when better performance is achieved using fewer large segments.
- One suggestion for avoiding small windows is for the receiver to defer updating a window until the additional allocation is at least X percent of the maximum allocation possible for the connection (where X might be 20 to 40).
- Another suggestion is for the sender to avoid sending small segments by waiting until the window is large enough before sending data. If the the user signals a push function then the data must be sent even if it is a small segment.
- Note that the acknowledgments should not be delayed or unnecessary retransmissions will result. One strategy would be to send an acknowledgment when a small segment arrives (with out updating the window information), and then to send another acknowledgment with new window information when the window is larger.
- The segment sent to probe a zero window may also begin a break up of transmitted data into smaller and smaller segments. If a segment containing a single data octet sent to probe a zero window is accepted, it consumes one octet of the window now available. If the sending TCP simply sends as much as it can whenever the window is non zero, the transmitted data will be broken into alternating big and small segments. As time goes on, occasional pauses in the receiver making window allocation available will result in breaking the big segments into a small and not quite so big pair. And after a while the data transmission will be in mostly small segments.
- The suggestion here is that the TCP implementations need to actively attempt to combine small window allocations into larger windows, since the mechanisms for managing the window tend to lead to many small windows in the simplest minded implementations.
The timeout, if present, permits the caller to set up a timeout for all data submitted to TCP. If data is not successfully delivered to the destination within the timeout period, the TCP will abort the connection. The present global default is five minutes.
OPEN(local port, foreign socket, active/passive [, timeout] [, precedence] [, security/compartment] [, options])
-> local connection name
SEND(local connection name, buffer address, byte count, PUSH flag, URGENT flag [,timeout])
RECEIVE(local connection name, buffer address, byte count)
-> byte count, urgent flag, push flag
CLOSE (local connection name)
STATUS(local connection name) -> status data
This is an implementation dependent user command and could be excluded without adverse effect. Information returned would typically come from the TCB associated with the connection. This command returns a data block containing the following information:
- local socket,
- foreign socket,
- local connection name,
- receive window,
- send window,
- connection state,
- number of buffers awaiting acknowledgment,
- number of buffers pending receipt,
- urgent state,
- precedence,
- security/compartment,
- and transmission timeout.
ABORT(local connection name)
It is assumed that the operating system environment provides a means for the TCP to asynchronously signal the user program. When the TCP does signal a user program, certain information is passed to the user. Often in the specification the information will be an error message. In other cases there will be information relating to the completion of processing a SEND or RECEIVE or other user call.
The following information is provided:
- Local Connection Name:
- Always
- Response String:
- Always
- Buffer Address:
- SEND & RECEIVE
- Byte count (counts bytes received):
- RECEIVE
- Push flag:
- RECEIVE
- Urgent flag:
- RECEIVE
Events that occur:
- User Calls
- OPEN
- SEND
- RECEIVE
- CLOSE
- ABORT
- STATUS
- Arriving Segments
- SEGMENT ARRIVES
- Timeouts
- USER TIMEOUT
- RETRANSMISSION TIMEOUT
- TIME-WAIT TIMEOUT
- CLOSED STATE
- Create a new transmission control block (TCB) to hold connection state information. Fill in local socket identifier, foreign socket, precedence, security/compartment, and user timeout information. Note that some parts of the foreign socket may be unspecified in a passive OPEN and are to be filled in by the parameters of the incoming SYN segment.
- Verify the security and precedence requested are allowed for this user, if not return "error: precedence not allowed" or "error: security/compartment not allowed."
- If passive enter the LISTEN state and return.
- If active and the foreign socket is unspecified, return "error: foreign socket unspecified";
- if active and the foreign socket is specified, issue a SYN segment. An initial send sequence number (ISS) is selected. A SYN segment of the form
<SEQ=ISS><CTL=SYN>
is sent. Set SND.UNA = ISS , SND.NXT = ISS+1, enter SYN-SENT state, and return. - If the caller does not have access to the local socket specified, return "error: connection illegal for this process".
- If there is no room to create a new connection, return "error: insufficient resources".
- LISTEN STATE
- If active and the foreign socket is specified, then change the connection from passive to active, select an ISS. Send a SYN segment, set SND.UNA = ISS, SND.NXT = ISS+1. Enter SYN-SENT state.
- Data associated with SEND may be sent with SYN segment or queued for transmission after entering ESTABLISHED state.
- The urgent bit if requested in the command must be sent with the data segments sent as a result of this command.
- If there is no room to queue the request, respond with "error: insufficient resources".
- If Foreign socket was not specified, then return "error: foreign socket unspecified".
- SYN-SENT STATE
- SYN-RECEIVED STATE
- ESTABLISHED STATE
- FIN-WAIT-1 STATE
- FIN-WAIT-2 STATE
- CLOSE-WAIT STATE
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT STATE
- Return "error: connection already exists".
- CLOSED STATE
- If the user does not have access to such a connection, then return "error: connection illegal for this process".
- Otherwise, return "error: connection does not exist".
- LISTEN STATE
- If the foreign socket is specified, then change the connection from passive to active, select an ISS. Send a SYN segment, set SND.UNA = ISS, SND.NXT = ISS+1. Enter SYN-SENT state. Data associated with SEND may be sent with SYN segment or queued for transmission after entering ESTABLISHED state. The urgent bit if requested in the command must be sent with the data segments sent as a result of this command.
- If there is no room to queue the request, respond with "error: insufficient resources".
- If Foreign socket was not specified, then return "error: foreign socket unspecified".
- SYN-SENT STATE
- SYN-RECEIVED STATE
- Queue the data for transmission after entering ESTABLISHED state.
- If no space to queue, respond with "error: insufficient resources".
- ESTABLISHED STATE
- CLOSE-WAIT STATE
- Segmentize the buffer and send it with a piggybacked acknowledgment (acknowledgment value = RCV.NXT).
- If there is insufficient space to remember this buffer, simply return "error: insufficient resources".
- If the urgent flag is set, then SND.UP = SND.NXT-1 and set the urgent pointer in the outgoing segments.
- FIN-WAIT-1 STATE
- FIN-WAIT-2 STATE
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT STATE
- Return "error: connection closing" and do not service request.
- CLOSED STATE
- If the user does not have access to such a connection, return "error: connection illegal for this process".
- Otherwise return "error: connection does not exist".
- LISTEN STATE
- SYN-SENT STATE
- SYN-RECEIVED STATE
- Queue for processing after entering ESTABLISHED state.
- If there is no room to queue this request, respond with "error: insufficient resources".
- Queue for processing after entering ESTABLISHED state.
- If there is no room to queue this request, respond with "error: insufficient resources".
- Queue for processing after entering ESTABLISHED state.
- If there is no room to queue this request, respond with "error: insufficient resources".
- ESTABLISHED STATE
- FIN-WAIT-1 STATE
- FIN-WAIT-2 STATE
- If insufficient incoming segments are queued to satisfy the request, queue the request.
- If there is no queue space to remember the RECEIVE, respond with "error: insufficient resources".
- Reassemble queued incoming segments into receive buffer and return to user.
- Mark "push seen" (PUSH) if this is the case.
- If RCV.UP is in advance of the data currently being passed to the user notify the user of the presence of urgent data.
- When the TCP takes responsibility for delivering data to the user that fact must be communicated to the sender via an acknowledgment. The formation of such an acknowledgment is described below in the discussion of processing an incoming segment.
- CLOSE-WAIT STATE
- Since the remote side has already sent FIN, RECEIVEs must be satisfied by text already on hand, but not yet delivered to the user.
- If no text is awaiting delivery, the RECEIVE will get a "error: connection closing" response.
- Otherwise, any remaining text can be used to satisfy the RECEIVE.
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT STATE
- Return "error: connection closing".
- CLOSED STATE
- If the user does not have access to such a connection, return "error: connection illegal for this process".
- Otherwise, return "error: connection does not exist".
- LISTEN STATE
- Any outstanding RECEIVEs are returned with "error: closing" responses.
- Delete TCB, enter CLOSED state, and return.
- SYN-SENT STATE
- Delete the TCB and return "error: closing" responses to any queued SENDs, or RECEIVEs.
- SYN-RECEIVED STATE
- If no SENDs have been issued and there is no pending data to send, then form a FIN segment and send it, and enter FIN-WAIT-1 state;
- otherwise queue for processing after entering ESTABLISHED state.
- ESTABLISHED STATE
- Queue this until all preceding SENDs have been segmentized, then form a FIN segment and send it.
- In any case, enter FIN-WAIT-1 state.
- FIN-WAIT-1 STATE
- FIN-WAIT-2 STATE
- Strictly speaking, this is an error and should receive a "error: connection closing" response. An "ok" response would be acceptable, too, as long as a second FIN is not emitted (the first FIN may be retransmitted though).
- CLOSE-WAIT STATE
- Queue this request until all preceding SENDs have been segmentized; then send a FIN segment, enter CLOSING state.
- CLOSING STATE
- Respond with "error: connection closing".
- LAST-ACK STATE
- Respond with "error: connection closing".
- TIME-WAIT STATE
- Respond with "error: connection closing".
- CLOSED STATE
- If the user should not have access to such a connection, return "error: connection illegal for this process".
- Otherwise return "error: connection does not exist".
- LISTEN STATE
- Any outstanding RECEIVEs should be returned with "error: connection reset" responses.
- Delete TCB, enter CLOSED state, and return.
- SYN-SENT STATE
- All queued SENDs and RECEIVEs should be given "connection reset" notification, delete the TCB, enter CLOSED state, and return.
- SYN-RECEIVED STATE
- ESTABLISHED STATE
- FIN-WAIT-1 STATE
- FIN-WAIT-2 STATE
- CLOSE-WAIT STATE
- Send a reset segment:
<SEQ=SND.NXT><CTL=RST>
- All queued SENDs and RECEIVEs should be given "connection reset" notification;
- all segments queued for transmission (except for the RST formed above) or retransmission should be flushed, delete the TCB, enter CLOSED state, and return.
- Send a reset segment:
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT STATE
- Respond with "ok" and delete the TCB, enter CLOSED state, and return.
- CLOSED STATE
- If the user should not have access to such a connection, return "error: connection illegal for this process".
- Otherwise return "error: connection does not exist".
- LISTEN STATE
- SYN-SENT STATE
- SYN-RECEIVED STATE
- ESTABLISHED STATE
- FIN-WAIT-1 STATE
- FIN-WAIT-2 STATE
- CLOSE-WAIT STATE
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT STATE
- Return associated "state", and the TCB pointer
- If the state is CLOSED then
- all data in the incoming segment is discarded.
- An incoming segment containing a RST is discarded.
- An incoming segment not containing a RST causes a RST to be sent in response.
- The acknowledgment and sequence field values are selected to make the reset sequence acceptable to the TCP that sent the offending segment.
- If the ACK bit is off, sequence number zero is used,
<SEQ=0><ACK=SEG.SEQ+SEG.LEN><CTL=RST,ACK>
. - If the ACK bit is on,
<SEQ=SEG.ACK><CTL=RST>
.
- If the state is LISTEN then
- first check for an RST
- An incoming RST should be ignored.
- Return.
- second check for an ACK
- Any acknowledgment is bad if it arrives on a connection still in the LISTEN state. An acceptable reset segment should be formed for any arriving ACK-bearing segment. The RST should be formatted as follows:
<SEQ=SEG.ACK><CTL=RST>
- Return.
- Any acknowledgment is bad if it arrives on a connection still in the LISTEN state. An acceptable reset segment should be formed for any arriving ACK-bearing segment. The RST should be formatted as follows:
- third check for a SYN
- If the SYN bit is set, check the security.
- If the security/compartment on the incoming segment does not exactly match the security/compartment in the TCB then send a reset
<SEQ=SEG.ACK><CTL=RST>
and return.
- If the security/compartment on the incoming segment does not exactly match the security/compartment in the TCB then send a reset
- If the SEG.PRC is greater than the TCB.PRC then if allowed by the user and the system set TCB.PRC<-SEG.PRC, if not allowed send a reset
<SEQ=SEG.ACK><CTL=RST>
and return. - If the SEG.PRC is less than the TCB.PRC then continue.
- Set RCV.NXT = SEG.SEQ+1, IRS = SEG.SEQ and any other control or text should be queued for processing later.
- ISS should be selected and a SYN segment sent of the form:
<SEQ=ISS><ACK=RCV.NXT><CTL=SYN,ACK>
- SND.NXT = ISS+1 and SND.UNA = ISS. The connection state should be changed to SYN-RECEIVED state.
- Note that any other incoming control or data (combined with SYN) will be processed in the SYN-RECEIVED state, but processing of SYN and ACK should not be repeated. If the listen was not fully specified (i.e., the foreign socket was not fully specified), then the unspecified fields should be filled in now.
- If the SYN bit is set, check the security.
- fourth other text or control
- Any other control or text-bearing segment (not containing SYN) must have an ACK and thus would be discarded by the ACK processing.
- An incoming RST segment could not be valid, since it could not have been sent in response to anything sent by this incarnation of the connection.
- So you are unlikely to get here, but if you do, drop the segment, and return.
- first check for an RST
- If the state is SYN-SENT then
- first check the ACK bit
- If the ACK bit is set
- If SEG.ACK =< ISS or SEG.ACK > SND.NXT send a reset (unless the RST bit is set, if so drop the segment and return)
<SEQ=SEG.ACK><CTL=RST>
and discard the segment. Return. - If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
- If SEG.ACK =< ISS or SEG.ACK > SND.NXT send a reset (unless the RST bit is set, if so drop the segment and return)
- If the ACK bit is set
- second check the RST bit
- If the RST bit is set
- If the ACK was acceptable then signal the user "error: connection reset", drop the segment, enter CLOSED state, delete TCB, and return.
- Otherwise (no ACK) drop the segment and return.
- If the RST bit is set
- third check the security and precedence
- If the security/compartment in the segment does not exactly match the security/compartment in the TCB, send a reset
- If there is an ACK
<SEQ=SEG.ACK><CTL=RST>
- Otherwise
<SEQ=0><ACK=SEG.SEQ+SEG.LEN><CTL=RST,ACK>
- If there is an ACK
- If there is an ACK
- The precedence in the segment must match the precedence in the TCB, if not, send a reset
<SEQ=SEG.ACK><CTL=RST>
- The precedence in the segment must match the precedence in the TCB, if not, send a reset
- If there is no ACK
- If the precedence in the segment is higher than the precedence in the TCB then if allowed by the user and the system raise the precedence in the TCB to that in the segment, if not allowed to raise the prec then send a reset.
<SEQ=0><ACK=SEG.SEQ+SEG.LEN><CTL=RST,ACK>
- If the precedence in the segment is lower than the precedence in the TCB continue.
- If the precedence in the segment is higher than the precedence in the TCB then if allowed by the user and the system raise the precedence in the TCB to that in the segment, if not allowed to raise the prec then send a reset.
- If a reset was sent, discard the segment and return.
- If the security/compartment in the segment does not exactly match the security/compartment in the TCB, send a reset
- fourth check the SYN bit
- This step should be reached only if the ACK is ok, or there is no ACK, and it the segment did not contain a RST.
- If the SYN bit is on and the security/compartment and precedence are acceptable then, RCV.NXT = SEG.SEQ+1, IRS = SEG.SEQ. SND.UNA should be advanced to equal SEG.ACK (if there is an ACK), and any segments on the retransmission queue which are thereby acknowledged should be removed.
- If SND.UNA > ISS (our SYN has been ACKed), change the connection state to ESTABLISHED state, form an ACK segment
<SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>
and send it. Data or controls which were queued for transmission may be included. - If there are other controls or text in the segment then continue processing at the sixth step below where the URG bit is checked, otherwise return.
- Otherwise enter SYN-RECEIVED state, form a SYN,ACK segment
<SEQ=ISS><ACK=RCV.NXT><CTL=SYN,ACK>
and send it. - If there are other controls or text in the segment, queue them for processing after the ESTABLISHED state has been reached, return.
- fifth, if neither of the SYN or RST bits is set then drop the segment and return.
- first check the ACK bit
Otherwise (state not equal to CLOSE, LISTEN or SYN_SENT state),
- Check sequence number,
- SYN-RECEIVED STATE
- ESTABLISHED STATE
- FIN-WAIT-1 STATE
- FIN-WAIT-2 STATE
- CLOSE-WAIT STATE
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT STATE
- Segments are processed in sequence. Initial tests on arrival are used to discard old duplicates, but further processing is done in SEG.SEQ order.
- If a segment’s contents straddle the boundary between old and new, only the new parts should be processed.
- There are four cases for the acceptability test for an incoming segment:
Segment Receive Test Length Window 0 0 SEG.SEQ = RCV.NXT 0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
0 0 not acceptable 0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND * If the RCV.WND is zero, no segments will be acceptable, but special allowance should be made to accept valid ACKs, URGs and RSTs. * If an incoming segment is not acceptable, an acknowledgment should be sent in reply (unless the RST bit is set, if so drop the segment and return):
<SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>
* After sending the acknowledgment, drop the unacceptable segment and return. * In the following it is assumed that the segment is the idealized segment that begins at RCV.NXT and does not exceed the window. * One could tailor actual segments to fit this assumption by trimming off any portions that lie outside the window (including SYN and FIN), and only processing further if the segment then begins at RCV.NXT. * Segments with higher begining sequence numbers may be held for later processing.
- Check the RST bit,
- SYN-RECEIVED STATE
- If the RST bit is set
- If this connection was initiated with a passive OPEN (i.e., came from the LISTEN state), then return this connection to LISTEN state and return. The user need not be informed.
- If this connection was initiated with an active OPEN (i.e., came from SYN-SENT state) then the connection was refused, signal the user "connection refused".
- In either case, all segments on the retransmission queue should be removed.
- And in the active OPEN case, enter the CLOSED state and delete the TCB, and return.
- If the RST bit is set
- ESTABLISHED
- FIN-WAIT-1
- FIN-WAIT-2
- CLOSE-WAIT
- If the RST bit is set then, any outstanding RECEIVEs and SEND should receive "reset" responses.
- All segment queues should be flushed.
- Users should also receive an unsolicited general "connection reset" signal.
- Enter the CLOSED state, delete the TCB, and return.
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT
- If the RST bit is set then, enter the CLOSED state, delete the TCB, and return.
- SYN-RECEIVED STATE
- Check security and precedence,
- SYN-RECEIVED
- If the security/compartment and precedence in the segment do not exactly match the security/compartment and precedence in the TCB then send a reset, and return.
- ESTABLISHED STATE
- If the security/compartment and precedence in the segment do not exactly match the security/compartment and precedence in the TCB then send a reset, any outstanding RECEIVEs and SEND should receive "reset" responses.
- All segment queues should be flushed.
- Users should also receive an unsolicited general "connection reset" signal.
- Enter the CLOSED state, delete the TCB, and return.
- Note this check is placed following the sequence check to prevent a segment from an old connection between these ports with a different security or precedence from causing an abort of the current connection.
- SYN-RECEIVED
- Check the SYN bit,
- SYN-RECEIVED
- ESTABLISHED STATE
- FIN-WAIT STATE-1
- FIN-WAIT STATE-2
- CLOSE-WAIT STATE
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT STATE
- If the SYN is in the window it is an error, send a reset, any outstanding RECEIVEs and SEND should receive "reset" responses, all segment queues should be flushed, the user should also receive an unsolicited general "connection reset" signal, enter the CLOSED state, delete the TCB, and return.
- If the SYN is not in the window this step would not be reached and an ack would have been sent in the first step (sequence number check).
- Check the ACK field,
- if the ACK bit is off drop the segment and return
- if the ACK bit is on
- SYN-RECEIVED STATE
- If SND.UNA =< SEG.ACK =< SND.NXT then enter ESTABLISHED state and continue processing
- If the segment acknowledgment is not acceptable, form a reset segment,
<SEQ=SEG.ACK><CTL=RST>
and send it
- ESTABLISHED STATE
- If SND.UNA < SEG.ACK =< SND.NXT then SND.UNA = SEG.ACK
- Any segments on the retransmission queue which are thereby entirely acknowledged are removed.
- Users should receive positive acknowledgments for buffers which have been SENT and fully acknowledged (i.e., SEND buffer should be returned with "ok" response).
- If the ACK is a duplicate (SEG.ACK < SND.UNA), it can be ignored.
- If the ACK acks something not yet sent (SEG.ACK > SND.NXT) then send an ACK, drop the segment, and return.
- If SND.UNA < SEG.ACK =< SND.NXT, the send window should be updated.
- If (SND.WL1 < SEG.SEQ or (SND.WL1 == SEG.SEQ and SND.WL2 =< SEG.ACK)), set SND.WND = SEG.WND, set SND.WL1 = SEG.SEQ, and set SND.WL2 = SEG.ACK
- Note that SND.WND is an offset from SND.UNA, that SND.WL1 records the sequence number of the last segment used to update SND.WND, and that SND.WL2 records the acknowledgment number of the last segment used to update SND.WND. The check here prevents using old segments to update the window.
- FIN-WAIT-1 STATE
- In addition to the processing for the ESTABLISHED state, if our FIN is now acknowledged then enter FIN-WAIT-2 state and continue processing in that state.
- FIN-WAIT-2 STATE
- In addition to the processing for the ESTABLISHED state, if the retransmission queue is empty, the user’s CLOSE can be acknowledged (_"ok"__) but do not delete the TCB.
- CLOSE-WAIT STATE
- Do the same processing as for the ESTABLISHED state.
- CLOSING STATE
- In addition to the processing for the ESTABLISHED state, if the ACK acknowledges our FIN then enter the TIME-WAIT state, otherwise ignore the segment.
- LAST-ACK STATE
- The only thing that can arrive in this state is an acknowledgment of our FIN. If our FIN is now acknowledged, delete the TCB, enter the CLOSED state, and return.
- TIME-WAIT STATE
- The only thing that can arrive in this state is a retransmission of the remote FIN. Acknowledge it, and restart the 2 MSL timeout.
- SYN-RECEIVED STATE
- Check the URG bit,
- ESTABLISHED STATE
- FIN-WAIT-1 STATE
- FIN-WAIT-2 STATE
- If the URG bit is set, RCV.UP = max(RCV.UP,SEG.UP), and signal the user that the remote side has urgent data if the urgent pointer (RCV.UP) is in advance of the data consumed.
- If the user has already been signaled (or is still in the "urgent mode") for this continuous sequence of urgent data, do not signal the user again.
- CLOSE-WAIT STATE
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT
- This should not occur, since a FIN has been received from the remote side.
- Ignore the URG.
- Process the segment text,
- ESTABLISHED STATE
- FIN-WAIT-1 STATE
- FIN-WAIT-2 STATE
- Once in the ESTABLISHED state, it is possible to deliver segment text to user RECEIVE buffers.
- Text from segments can be moved into buffers until either the buffer is full or the segment is empty.
- If the segment empties and carries an PUSH flag, then the user is informed, when the buffer is returned, that a PUSH has been received.
- When the TCP takes responsibility for delivering the data to the user it must also acknowledge the receipt of the data.
- Once the TCP takes responsibility for the data it advances RCV.NXT over the data accepted, and adjusts RCV.WND as apporopriate to the current buffer availability. The total of RCV.NXT and RCV.WND should not be reduced.
- Send an acknowledgment of the form:
<SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>
- This acknowledgment should be piggybacked on a segment being transmitted if possible without incurring undue delay.
- CLOSE-WAIT STATE
- CLOSING STATE
- LAST-ACK STATE
- TIME-WAIT STATE
- This should not occur, since a FIN has been received from the remote side.
- Ignore the segment text.
- Check the FIN bit:
- Do not process the FIN if the state is CLOSED, LISTEN or SYN-SENT since the SEG.SEQ cannot be validated; drop the segment and return. If the FIN bit is set, signal the user "connection closing" and return any pending RECEIVEs with same message, advance RCV.NXT over the FIN, and send an acknowledgment for the FIN. Note that FIN implies PUSH for any segment text not yet delivered to the user.
- SYN-RECEIVED STATE
- ESTABLISHED STATE
- Enter the CLOSE-WAIT state.
- FIN-WAIT-1 STATE
- If our FIN has been ACKed (perhaps in this segment), then enter TIME-WAIT state, start the time-wait timer, turn off the other timers; otherwise enter the CLOSING state.
- FIN-WAIT-2 STATE
- Enter the TIME-WAIT state. Start the time-wait timer, turn off the other timers.
- CLOSE-WAIT STATE
- Remain in the CLOSE-WAIT state.
- CLOSING STATE
- Remain in the CLOSING state.
- LAST-ACK STATE
- Remain in the LAST-ACK state.
- TIME-WAIT STATE
- Remain in the TIME-WAIT state. Restart the 2 MSL time-wait timeout.
and return.
For any state if the user timeout expires, flush all queues, signal the user "error: connection aborted due to user timeout" in general and for any outstanding calls, delete the TCB, enter the CLOSED state and return.
For any state if the retransmission timeout expires on a segment in the retransmission queue, send the segment at the front of the retransmission queue again, reinitialize the retransmission timer, and return.
If the time-wait timeout expires on a connection delete the TCB, enter the CLOSED state and return.