2 TCP input process routines.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 Check whether the sequence number of the incoming segment is acceptable.
15 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
16 @param[in] Seg Pointer to the incoming segment.
18 @retval 1 The sequence number is acceptable.
19 @retval 0 The sequence number is not acceptable.
28 return (TCP_SEQ_LEQ (Tcb
->RcvNxt
, Seg
->End
) &&
29 TCP_SEQ_LT (Seg
->Seq
, Tcb
->RcvWl2
+ Tcb
->RcvWnd
));
33 NewReno fast recovery defined in RFC3782.
35 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
36 @param[in] Seg Segment that triggers the fast recovery.
49 // Step 1: Three duplicate ACKs and not in fast recovery
51 if (Tcb
->CongestState
!= TCP_CONGEST_RECOVER
) {
53 // Step 1A: Invoking fast retransmission.
55 FlightSize
= TCP_SUB_SEQ (Tcb
->SndNxt
, Tcb
->SndUna
);
57 Tcb
->Ssthresh
= MAX (FlightSize
>> 1, (UINT32
)(2 * Tcb
->SndMss
));
58 Tcb
->Recover
= Tcb
->SndNxt
;
60 Tcb
->CongestState
= TCP_CONGEST_RECOVER
;
61 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
64 // Step 2: Entering fast retransmission
66 TcpRetransmit (Tcb
, Tcb
->SndUna
);
67 Tcb
->CWnd
= Tcb
->Ssthresh
+ 3 * Tcb
->SndMss
;
71 "TcpFastRecover: enter fast retransmission for TCB %p, recover point is %d\n",
79 // During fast recovery, execute Step 3, 4, 5 of RFC3782
81 if (Seg
->Ack
== Tcb
->SndUna
) {
83 // Step 3: Fast Recovery,
84 // If this is a duplicated ACK, increse Cwnd by SMSS.
87 // Step 4 is skipped here only to be executed later
90 Tcb
->CWnd
+= Tcb
->SndMss
;
93 "TcpFastRecover: received another duplicated ACK (%d) for TCB %p\n",
99 // New data is ACKed, check whether it is a
100 // full ACK or partial ACK
102 if (TCP_SEQ_GEQ (Seg
->Ack
, Tcb
->Recover
)) {
104 // Step 5 - Full ACK:
105 // deflate the congestion window, and exit fast recovery
107 FlightSize
= TCP_SUB_SEQ (Tcb
->SndNxt
, Tcb
->SndUna
);
109 Tcb
->CWnd
= MIN (Tcb
->Ssthresh
, FlightSize
+ Tcb
->SndMss
);
111 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
114 "TcpFastRecover: received a full ACK(%d) for TCB %p, exit fast recovery\n",
120 // Step 5 - Partial ACK:
121 // fast retransmit the first unacknowledge field
122 // , then deflate the CWnd
124 TcpRetransmit (Tcb
, Seg
->Ack
);
125 Acked
= TCP_SUB_SEQ (Seg
->Ack
, Tcb
->SndUna
);
128 // Deflate the CWnd by the amount of new data
129 // ACKed by SEG.ACK. If more than one SMSS data
130 // is ACKed, add back SMSS byte to CWnd after
132 if (Acked
>= Tcb
->SndMss
) {
133 Acked
-= Tcb
->SndMss
;
140 "TcpFastRecover: received a partial ACK(%d) for TCB %p\n",
149 NewReno fast loss recovery defined in RFC3792.
151 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
152 @param[in] Seg Segment that triggers the fast loss recovery.
161 if (TCP_SEQ_GT (Seg
->Ack
, Tcb
->SndUna
)) {
163 // New data is ACKed, check whether it is a
164 // full ACK or partial ACK
166 if (TCP_SEQ_GEQ (Seg
->Ack
, Tcb
->LossRecover
)) {
168 // Full ACK: exit the loss recovery.
171 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
175 "TcpFastLossRecover: received a full ACK(%d) for TCB %p\n",
182 // fast retransmit the first unacknowledge field.
184 TcpRetransmit (Tcb
, Seg
->Ack
);
187 "TcpFastLossRecover: received a partial ACK(%d) for TCB %p\n",
196 Compute the RTT as specified in RFC2988.
198 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
199 @param[in] Measure Currently measured RTT in heartbeats.
211 // Step 2.3: Compute the RTO for subsequent RTT measurement.
213 if (Tcb
->SRtt
!= 0) {
214 Var
= Tcb
->SRtt
- (Measure
<< TCP_RTT_SHIFT
);
220 Tcb
->RttVar
= (3 * Tcb
->RttVar
+ Var
) >> 2;
221 Tcb
->SRtt
= 7 * (Tcb
->SRtt
>> 3) + Measure
;
224 // Step 2.2: compute the first RTT measure
226 Tcb
->SRtt
= Measure
<< TCP_RTT_SHIFT
;
227 Tcb
->RttVar
= Measure
<< (TCP_RTT_SHIFT
- 1);
230 Tcb
->Rto
= (Tcb
->SRtt
+ MAX (8, 4 * Tcb
->RttVar
)) >> TCP_RTT_SHIFT
;
233 // Step 2.4: Limit the RTO to at least 1 second
234 // Step 2.5: Limit the RTO to a maximum value that
235 // is at least 60 second
237 if (Tcb
->Rto
< TCP_RTO_MIN
) {
238 Tcb
->Rto
= TCP_RTO_MIN
;
239 } else if (Tcb
->Rto
> TCP_RTO_MAX
) {
240 Tcb
->Rto
= TCP_RTO_MAX
;
245 "TcpComputeRtt: new RTT for TCB %p computed SRTT: %d RTTVAR: %d RTO: %d\n",
254 Trim the data; SYN and FIN to fit into the window defined by Left and Right.
256 @param[in] Nbuf The buffer that contains a received TCP segment without an IP header.
257 @param[in] Left The sequence number of the window's left edge.
258 @param[in] Right The sequence number of the window's right edge.
260 @retval 0 The segment is broken.
261 @retval 1 The segment is in good shape.
275 Seg
= TCPSEG_NETBUF (Nbuf
);
278 // If the segment is completely out of window,
279 // truncate every thing, include SYN and FIN.
281 if (TCP_SEQ_LEQ (Seg
->End
, Left
) || TCP_SEQ_LEQ (Right
, Seg
->Seq
)) {
282 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_SYN
);
283 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_FIN
);
286 NetbufTrim (Nbuf
, Nbuf
->TotalSize
, NET_BUF_HEAD
);
291 // Adjust the buffer header
293 if (TCP_SEQ_LT (Seg
->Seq
, Left
)) {
294 Drop
= TCP_SUB_SEQ (Left
, Seg
->Seq
);
295 Urg
= Seg
->Seq
+ Seg
->Urg
;
298 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
299 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_SYN
);
304 // Adjust the urgent point
306 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_URG
)) {
307 if (TCP_SEQ_LT (Urg
, Seg
->Seq
)) {
308 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_URG
);
310 Seg
->Urg
= (UINT16
)TCP_SUB_SEQ (Urg
, Seg
->Seq
);
315 NetbufTrim (Nbuf
, Drop
, NET_BUF_HEAD
);
320 // Adjust the buffer tail
322 if (TCP_SEQ_GT (Seg
->End
, Right
)) {
323 Drop
= TCP_SUB_SEQ (Seg
->End
, Right
);
326 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
327 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_FIN
);
332 NetbufTrim (Nbuf
, Drop
, NET_BUF_TAIL
);
336 return TcpVerifySegment (Nbuf
);
340 Trim off the data outside the tcb's receive window.
342 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
343 @param[in] Nbuf Pointer to the NET_BUF containing the received tcp segment.
345 @retval 0 The segment is broken.
346 @retval 1 The segment is in good shape.
355 return TcpTrimSegment (Nbuf
, Tcb
->RcvNxt
, Tcb
->RcvWl2
+ Tcb
->RcvWnd
);
359 Process the data and FIN flag, and check whether to deliver
360 data to the socket layer.
362 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
364 @retval 0 No error occurred to deliver data.
365 @retval -1 An error condition occurred. The proper response is to reset the
380 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
));
383 // make sure there is some data queued,
384 // and TCP is in a proper state
386 if (IsListEmpty (&Tcb
->RcvQue
) || !TCP_CONNECTED (Tcb
->State
)) {
391 // Deliver data to the socket layer
393 Entry
= Tcb
->RcvQue
.ForwardLink
;
396 while (Entry
!= &Tcb
->RcvQue
) {
397 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
398 Seg
= TCPSEG_NETBUF (Nbuf
);
400 if (TcpVerifySegment (Nbuf
) == 0) {
403 "TcpToSendData: discard a broken segment for TCB %p\n",
410 ASSERT (Nbuf
->Tcp
== NULL
);
412 if (TCP_SEQ_GT (Seg
->Seq
, Seq
)) {
416 Entry
= Entry
->ForwardLink
;
420 RemoveEntryList (&Nbuf
->List
);
423 // RFC793 Eighth step: process FIN in sequence
425 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
427 // The peer sends to us junky data after FIN,
428 // reset the connection.
430 if (!IsListEmpty (&Tcb
->RcvQue
)) {
433 "TcpDeliverData: data received after FIN from peer of TCB %p, reset connection\n",
443 "TcpDeliverData: processing FIN from peer of TCB %p\n",
447 switch (Tcb
->State
) {
449 case TCP_ESTABLISHED
:
451 TcpSetState (Tcb
, TCP_CLOSE_WAIT
);
456 if (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
)) {
457 TcpSetState (Tcb
, TCP_CLOSING
);
466 TcpSetState (Tcb
, TCP_TIME_WAIT
);
467 TcpClearAllTimer (Tcb
);
469 if (Tcb
->TimeWaitTimeout
!= 0) {
470 TcpSetTimer (Tcb
, TCP_TIMER_2MSL
, Tcb
->TimeWaitTimeout
);
474 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
489 // The peer sends to us junk FIN byte. Discard
490 // the buffer then reset the connection
499 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
505 // Don't delay the ack if PUSH flag is on.
507 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_PSH
)) {
508 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
511 if (Nbuf
->TotalSize
!= 0) {
514 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RCVD_URG
) &&
515 TCP_SEQ_LEQ (Seg
->Seq
, Tcb
->RcvUp
))
517 if (TCP_SEQ_LEQ (Seg
->End
, Tcb
->RcvUp
)) {
518 Urgent
= Nbuf
->TotalSize
;
520 Urgent
= TCP_SUB_SEQ (Tcb
->RcvUp
, Seg
->Seq
) + 1;
524 SockDataRcvd (Tcb
->Sk
, Nbuf
, Urgent
);
527 if (TCP_FIN_RCVD (Tcb
->State
)) {
528 SockNoMoreData (Tcb
->Sk
);
538 Store the data into the reassemble queue.
540 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
541 @param[in] Nbuf Pointer to the buffer containing the data to be queued.
543 @retval 0 An error condition occurred.
544 @retval 1 No error occurred to queue data.
559 ASSERT ((Tcb
!= NULL
) && (Nbuf
!= NULL
) && (Nbuf
->Tcp
== NULL
));
563 Seg
= TCPSEG_NETBUF (Nbuf
);
567 // Fast path to process normal case. That is,
568 // no out-of-order segments are received.
570 if (IsListEmpty (Head
)) {
571 InsertTailList (Head
, &Nbuf
->List
);
576 // Find the point to insert the buffer
578 for (Prev
= Head
, Cur
= Head
->ForwardLink
;
580 Prev
= Cur
, Cur
= Cur
->ForwardLink
)
582 Node
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
584 if (TCP_SEQ_LT (Seg
->Seq
, TCPSEG_NETBUF (Node
)->Seq
)) {
590 // Check whether the current segment overlaps with the
594 Node
= NET_LIST_USER_STRUCT (Prev
, NET_BUF
, List
);
596 if (TCP_SEQ_LT (Seg
->Seq
, TCPSEG_NETBUF (Node
)->End
)) {
597 if (TCP_SEQ_LEQ (Seg
->End
, TCPSEG_NETBUF (Node
)->End
)) {
601 if (TcpTrimSegment (Nbuf
, TCPSEG_NETBUF (Node
)->End
, Seg
->End
) == 0) {
607 InsertHeadList (Prev
, &Nbuf
->List
);
609 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
612 // Check the segments after the insert point.
614 while (Cur
!= Head
) {
615 Node
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
617 if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node
)->End
, Seg
->End
)) {
618 Cur
= Cur
->ForwardLink
;
620 RemoveEntryList (&Node
->List
);
625 if (TCP_SEQ_LT (TCPSEG_NETBUF (Node
)->Seq
, Seg
->End
)) {
626 if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node
)->Seq
, Seg
->Seq
)) {
627 RemoveEntryList (&Nbuf
->List
);
631 if (TcpTrimSegment (Nbuf
, Seg
->Seq
, TCPSEG_NETBUF (Node
)->Seq
) == 0) {
632 RemoveEntryList (&Nbuf
->List
);
639 Cur
= Cur
->ForwardLink
;
646 Adjust the send queue or the retransmit queue.
648 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
649 @param[in] Ack The acknowledge sequence number of the received segment.
651 @retval 0 An error condition occurred.
652 @retval 1 No error occurred.
667 Cur
= Head
->ForwardLink
;
669 while (Cur
!= Head
) {
670 Node
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
671 Seg
= TCPSEG_NETBUF (Node
);
673 if (TCP_SEQ_GEQ (Seg
->Seq
, Ack
)) {
678 // Remove completely ACKed segments
680 if (TCP_SEQ_LEQ (Seg
->End
, Ack
)) {
681 Cur
= Cur
->ForwardLink
;
683 RemoveEntryList (&Node
->List
);
688 return TcpTrimSegment (Node
, Ack
, Seg
->End
);
695 Process the received TCP segments.
697 @param[in] Nbuf Buffer that contains received a TCP segment without an IP header.
698 @param[in] Src Source address of the segment, or the peer's IP address.
699 @param[in] Dst Destination address of the segment, or the local end's IP
701 @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates
704 @retval 0 Segment processed successfully. It is either accepted or
705 discarded. However, no connection is reset by the segment.
706 @retval -1 A connection is reset by the segment.
712 IN EFI_IP_ADDRESS
*Src
,
713 IN EFI_IP_ADDRESS
*Dst
,
728 ASSERT ((Version
== IP_VERSION_4
) || (Version
== IP_VERSION_6
));
730 NET_CHECK_SIGNATURE (Nbuf
, NET_BUF_SIGNATURE
);
735 Head
= (TCP_HEAD
*)NetbufGetByte (Nbuf
, 0, NULL
);
736 ASSERT (Head
!= NULL
);
738 if (Nbuf
->TotalSize
< sizeof (TCP_HEAD
)) {
739 DEBUG ((DEBUG_NET
, "TcpInput: received a malformed packet\n"));
743 Len
= Nbuf
->TotalSize
- (Head
->HeadLen
<< 2);
745 if ((Head
->HeadLen
< 5) || (Len
< 0)) {
746 DEBUG ((DEBUG_NET
, "TcpInput: received a malformed packet\n"));
751 if (Version
== IP_VERSION_4
) {
752 Checksum
= NetPseudoHeadChecksum (Src
->Addr
[0], Dst
->Addr
[0], 6, 0);
754 Checksum
= NetIp6PseudoHeadChecksum (&Src
->v6
, &Dst
->v6
, 6, 0);
757 Checksum
= TcpChecksum (Nbuf
, Checksum
);
760 DEBUG ((DEBUG_ERROR
, "TcpInput: received a checksum error packet\n"));
764 if (TCP_FLG_ON (Head
->Flag
, TCP_FLG_SYN
)) {
768 if (TCP_FLG_ON (Head
->Flag
, TCP_FLG_FIN
)) {
778 (BOOLEAN
)TCP_FLG_ON (Head
->Flag
, TCP_FLG_SYN
)
781 if ((Tcb
== NULL
) || (Tcb
->State
== TCP_CLOSED
)) {
782 DEBUG ((DEBUG_NET
, "TcpInput: send reset because no TCB found\n"));
788 Seg
= TcpFormatNetbuf (Tcb
, Nbuf
);
791 // RFC1122 recommended reaction to illegal option
792 // (in fact, an illegal option length) is reset.
794 if (TcpParseOption (Nbuf
->Tcp
, &Option
) == -1) {
797 "TcpInput: reset the peer because of malformed option for TCB %p\n",
805 // From now on, the segment is headless
807 NetbufTrim (Nbuf
, (Head
->HeadLen
<< 2), NET_BUF_HEAD
);
811 // Process the segment in LISTEN state.
813 if (Tcb
->State
== TCP_LISTEN
) {
815 // First step: Check RST
817 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_RST
)) {
820 "TcpInput: discard a reset segment for TCB %p in listening\n",
828 // Second step: Check ACK.
829 // Any ACK sent to TCP in LISTEN is reseted.
831 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
)) {
834 "TcpInput: send reset because of segment with ACK for TCB %p in listening\n",
842 // Third step: Check SYN
844 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
846 // create a child TCB to handle the data
850 Tcb
= TcpCloneTcb (Parent
);
854 "TcpInput: discard a segment because failed to clone a child for TCB %p\n",
863 "TcpInput: create a child for TCB %p in listening\n",
868 // init the TCB structure
870 IP6_COPY_ADDRESS (&Tcb
->LocalEnd
.Ip
, Dst
);
871 IP6_COPY_ADDRESS (&Tcb
->RemoteEnd
.Ip
, Src
);
872 Tcb
->LocalEnd
.Port
= Head
->DstPort
;
873 Tcb
->RemoteEnd
.Port
= Head
->SrcPort
;
875 TcpInitTcbLocal (Tcb
);
876 TcpInitTcbPeer (Tcb
, Seg
, &Option
);
878 TcpSetState (Tcb
, TCP_SYN_RCVD
);
879 TcpSetTimer (Tcb
, TCP_TIMER_CONNECT
, Tcb
->ConnectTimeout
);
880 if (TcpTrimInWnd (Tcb
, Nbuf
) == 0) {
883 "TcpInput: discard a broken segment for TCB %p\n",
894 } else if (Tcb
->State
== TCP_SYN_SENT
) {
896 // First step: Check ACK bit
898 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
) && (Seg
->Ack
!= Tcb
->Iss
+ 1)) {
901 "TcpInput: send reset because of wrong ACK received for TCB %p in SYN_SENT\n",
909 // Second step: Check RST bit
911 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_RST
)) {
912 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
)) {
915 "TcpInput: connection reset by peer for TCB %p in SYN_SENT\n",
919 SOCK_ERROR (Tcb
->Sk
, EFI_CONNECTION_RESET
);
920 goto DROP_CONNECTION
;
924 "TcpInput: discard a reset segment because of no ACK for TCB %p in SYN_SENT\n",
933 // Third step: Check security and precedence. Skipped
937 // Fourth step: Check SYN. Pay attention to simultaneous open
939 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
940 TcpInitTcbPeer (Tcb
, Seg
, &Option
);
942 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
)) {
943 Tcb
->SndUna
= Seg
->Ack
;
946 TcpClearTimer (Tcb
, TCP_TIMER_REXMIT
);
948 if (TCP_SEQ_GT (Tcb
->SndUna
, Tcb
->Iss
)) {
949 TcpSetState (Tcb
, TCP_ESTABLISHED
);
951 TcpClearTimer (Tcb
, TCP_TIMER_CONNECT
);
952 TcpDeliverData (Tcb
);
954 if ((Tcb
->CongestState
== TCP_CONGEST_OPEN
) &&
955 TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
))
957 TcpComputeRtt (Tcb
, Tcb
->RttMeasure
);
958 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
961 if (TcpTrimInWnd (Tcb
, Nbuf
) == 0) {
964 "TcpInput: discard a broken segment for TCB %p\n",
971 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
975 "TcpInput: connection established for TCB %p in SYN_SENT\n",
982 // Received a SYN segment without ACK, simultaneous open.
984 TcpSetState (Tcb
, TCP_SYN_RCVD
);
986 ASSERT (Tcb
->SndNxt
== Tcb
->Iss
+ 1);
988 if ((TcpAdjustSndQue (Tcb
, Tcb
->SndNxt
) == 0) || (TcpTrimInWnd (Tcb
, Nbuf
) == 0)) {
991 "TcpInput: discard a broken segment for TCB %p\n",
1000 "TcpInput: simultaneous open for TCB %p in SYN_SENT\n",
1012 // Process segment in SYN_RCVD or TCP_CONNECTED states
1016 // Clear probe timer since the RecvWindow is opened.
1018 if (Tcb
->ProbeTimerOn
&& (Seg
->Wnd
!= 0)) {
1019 TcpClearTimer (Tcb
, TCP_TIMER_PROBE
);
1020 Tcb
->ProbeTimerOn
= FALSE
;
1024 // First step: Check whether SEG.SEQ is acceptable
1026 if (TcpSeqAcceptable (Tcb
, Seg
) == 0) {
1029 "TcpInput: sequence acceptance test failed for segment of TCB %p\n",
1033 if (!TCP_FLG_ON (Seg
->Flag
, TCP_FLG_RST
)) {
1040 if ((TCP_SEQ_LT (Seg
->Seq
, Tcb
->RcvWl2
)) &&
1041 (Tcb
->RcvWl2
== Seg
->End
) &&
1042 !TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
| TCP_FLG_FIN
))
1044 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
1048 // Second step: Check the RST
1050 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_RST
)) {
1051 DEBUG ((DEBUG_WARN
, "TcpInput: connection reset for TCB %p\n", Tcb
));
1053 if (Tcb
->State
== TCP_SYN_RCVD
) {
1054 SOCK_ERROR (Tcb
->Sk
, EFI_CONNECTION_REFUSED
);
1057 // This TCB comes from either a LISTEN TCB,
1058 // or active open TCB with simultaneous open.
1059 // Do NOT signal user CONNECTION refused
1060 // if it comes from a LISTEN TCB.
1062 } else if ((Tcb
->State
== TCP_ESTABLISHED
) ||
1063 (Tcb
->State
== TCP_FIN_WAIT_1
) ||
1064 (Tcb
->State
== TCP_FIN_WAIT_2
) ||
1065 (Tcb
->State
== TCP_CLOSE_WAIT
))
1067 SOCK_ERROR (Tcb
->Sk
, EFI_CONNECTION_RESET
);
1071 goto DROP_CONNECTION
;
1075 // Trim the data and flags.
1077 if (TcpTrimInWnd (Tcb
, Nbuf
) == 0) {
1080 "TcpInput: discard a broken segment for TCB %p\n",
1088 // Third step: Check security and precedence, Ignored
1092 // Fourth step: Check the SYN bit.
1094 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
1097 "TcpInput: connection reset because received extra SYN for TCB %p\n",
1101 SOCK_ERROR (Tcb
->Sk
, EFI_CONNECTION_RESET
);
1102 goto RESET_THEN_DROP
;
1106 // Fifth step: Check the ACK
1108 if (!TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
)) {
1111 "TcpInput: segment discard because of no ACK for connected TCB %p\n",
1117 if ((Tcb
->IpInfo
->IpVersion
== IP_VERSION_6
) && (Tcb
->Tick
== 0)) {
1118 Tcp6RefreshNeighbor (Tcb
, Src
, TCP6_KEEP_NEIGHBOR_TIME
* TICKS_PER_SECOND
);
1119 Tcb
->Tick
= TCP6_REFRESH_NEIGHBOR_TICK
;
1123 if (Tcb
->State
== TCP_SYN_RCVD
) {
1124 if (TCP_SEQ_LT (Tcb
->SndUna
, Seg
->Ack
) &&
1125 TCP_SEQ_LEQ (Seg
->Ack
, Tcb
->SndNxt
))
1127 Tcb
->SndWnd
= Seg
->Wnd
;
1128 Tcb
->SndWndMax
= MAX (Tcb
->SndWnd
, Tcb
->SndWndMax
);
1129 Tcb
->SndWl1
= Seg
->Seq
;
1130 Tcb
->SndWl2
= Seg
->Ack
;
1131 TcpSetState (Tcb
, TCP_ESTABLISHED
);
1133 TcpClearTimer (Tcb
, TCP_TIMER_CONNECT
);
1134 TcpDeliverData (Tcb
);
1138 "TcpInput: connection established for TCB %p in SYN_RCVD\n",
1143 // Continue the process as ESTABLISHED state
1148 "TcpInput: send reset because of wrong ACK for TCB %p in SYN_RCVD\n",
1156 if (TCP_SEQ_LT (Seg
->Ack
, Tcb
->SndUna
)) {
1159 "TcpInput: ignore the out-of-data ACK for connected TCB %p\n",
1164 } else if (TCP_SEQ_GT (Seg
->Ack
, Tcb
->SndNxt
)) {
1167 "TcpInput: discard segment for future ACK for connected TCB %p\n",
1176 // From now on: SND.UNA <= SEG.ACK <= SND.NXT.
1178 if (TCP_FLG_ON (Option
.Flag
, TCP_OPTION_RCVD_TS
)) {
1180 // update TsRecent as specified in page 16 RFC1323.
1181 // RcvWl2 equals to the variable "LastAckSent"
1184 if (TCP_SEQ_LEQ (Seg
->Seq
, Tcb
->RcvWl2
) &&
1185 TCP_SEQ_LT (Tcb
->RcvWl2
, Seg
->End
))
1187 Tcb
->TsRecent
= Option
.TSVal
;
1188 Tcb
->TsRecentAge
= mTcpTick
;
1191 TcpComputeRtt (Tcb
, TCP_SUB_TIME (mTcpTick
, Option
.TSEcr
));
1192 } else if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
)) {
1193 ASSERT (Tcb
->CongestState
== TCP_CONGEST_OPEN
);
1195 TcpComputeRtt (Tcb
, Tcb
->RttMeasure
);
1196 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
1199 if (Seg
->Ack
== Tcb
->SndNxt
) {
1200 TcpClearTimer (Tcb
, TCP_TIMER_REXMIT
);
1202 TcpSetTimer (Tcb
, TCP_TIMER_REXMIT
, Tcb
->Rto
);
1206 // Count duplicate acks.
1208 if ((Seg
->Ack
== Tcb
->SndUna
) &&
1209 (Tcb
->SndUna
!= Tcb
->SndNxt
) &&
1210 (Seg
->Wnd
== Tcb
->SndWnd
) &&
1219 // Congestion avoidance, fast recovery and fast retransmission.
1221 if (((Tcb
->CongestState
== TCP_CONGEST_OPEN
) && (Tcb
->DupAck
< 3)) ||
1222 (Tcb
->CongestState
== TCP_CONGEST_LOSS
))
1224 if (TCP_SEQ_GT (Seg
->Ack
, Tcb
->SndUna
)) {
1225 if (Tcb
->CWnd
< Tcb
->Ssthresh
) {
1226 Tcb
->CWnd
+= Tcb
->SndMss
;
1228 Tcb
->CWnd
+= MAX (Tcb
->SndMss
* Tcb
->SndMss
/ Tcb
->CWnd
, 1);
1231 Tcb
->CWnd
= MIN (Tcb
->CWnd
, TCP_MAX_WIN
<< Tcb
->SndWndScale
);
1234 if (Tcb
->CongestState
== TCP_CONGEST_LOSS
) {
1235 TcpFastLossRecover (Tcb
, Seg
);
1238 TcpFastRecover (Tcb
, Seg
);
1241 if (TCP_SEQ_GT (Seg
->Ack
, Tcb
->SndUna
)) {
1242 if (TcpAdjustSndQue (Tcb
, Seg
->Ack
) == 0) {
1245 "TcpInput: discard a broken segment for TCB %p\n",
1252 Tcb
->SndUna
= Seg
->Ack
;
1254 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
) &&
1255 TCP_SEQ_LT (Tcb
->SndUp
, Seg
->Ack
))
1257 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
);
1262 // Update window info
1264 if (TCP_SEQ_LT (Tcb
->SndWl1
, Seg
->Seq
) ||
1265 ((Tcb
->SndWl1
== Seg
->Seq
) && TCP_SEQ_LEQ (Tcb
->SndWl2
, Seg
->Ack
)))
1267 Right
= Seg
->Ack
+ Seg
->Wnd
;
1269 if (TCP_SEQ_LT (Right
, Tcb
->SndWl2
+ Tcb
->SndWnd
)) {
1270 if ((Tcb
->SndWl1
== Seg
->Seq
) &&
1271 (Tcb
->SndWl2
== Seg
->Ack
) &&
1279 "TcpInput: peer shrinks the window for connected TCB %p\n",
1283 if ((Tcb
->CongestState
== TCP_CONGEST_RECOVER
) &&
1284 (TCP_SEQ_LT (Right
, Tcb
->Recover
)))
1286 Tcb
->Recover
= Right
;
1289 if ((Tcb
->CongestState
== TCP_CONGEST_LOSS
) &&
1290 (TCP_SEQ_LT (Right
, Tcb
->LossRecover
)))
1292 Tcb
->LossRecover
= Right
;
1295 if (TCP_SEQ_LT (Right
, Tcb
->SndNxt
)) {
1297 // Check for Window Retraction in RFC7923 section 2.4.
1298 // The lower n bits of the peer's actual receive window is wiped out if TCP
1299 // window scale is enabled, it will look like the peer is shrinking the window.
1300 // Check whether the SndNxt is out of the advertised receive window by more than
1301 // 2^Rcv.Wind.Shift before moving the SndNxt to the left.
1305 "TcpInput: peer advise negative useable window for connected TCB %p\n",
1308 Usable
= TCP_SUB_SEQ (Tcb
->SndNxt
, Right
);
1309 if ((Usable
>> Tcb
->SndWndScale
) > 0) {
1312 "TcpInput: SndNxt is out of window by more than window scale for TCB %p\n",
1315 Tcb
->SndNxt
= Right
;
1318 if (Right
== Tcb
->SndUna
) {
1319 TcpClearTimer (Tcb
, TCP_TIMER_REXMIT
);
1320 TcpSetProbeTimer (Tcb
);
1325 Tcb
->SndWnd
= Seg
->Wnd
;
1326 Tcb
->SndWndMax
= MAX (Tcb
->SndWnd
, Tcb
->SndWndMax
);
1327 Tcb
->SndWl1
= Seg
->Seq
;
1328 Tcb
->SndWl2
= Seg
->Ack
;
1333 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
) &&
1334 (Tcb
->SndUna
== Tcb
->SndNxt
))
1338 "TcpInput: local FIN is ACKed by peer for connected TCB %p\n",
1342 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
);
1346 // Transit the state if proper.
1348 switch (Tcb
->State
) {
1349 case TCP_FIN_WAIT_1
:
1351 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
)) {
1352 TcpSetState (Tcb
, TCP_FIN_WAIT_2
);
1354 TcpClearAllTimer (Tcb
);
1355 TcpSetTimer (Tcb
, TCP_TIMER_FINWAIT2
, Tcb
->FinWait2Timeout
);
1358 case TCP_FIN_WAIT_2
:
1362 case TCP_CLOSE_WAIT
:
1367 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
)) {
1368 TcpSetState (Tcb
, TCP_TIME_WAIT
);
1370 TcpClearAllTimer (Tcb
);
1372 if (Tcb
->TimeWaitTimeout
!= 0) {
1373 TcpSetTimer (Tcb
, TCP_TIMER_2MSL
, Tcb
->TimeWaitTimeout
);
1377 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
1389 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
)) {
1390 TcpSetState (Tcb
, TCP_CLOSED
);
1399 if (Tcb
->TimeWaitTimeout
!= 0) {
1400 TcpSetTimer (Tcb
, TCP_TIMER_2MSL
, Tcb
->TimeWaitTimeout
);
1404 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
1418 // Sixth step: Check the URG bit.update the Urg point
1419 // if in TCP_CAN_RECV, otherwise, leave the RcvUp intact.
1424 TcpSetKeepaliveTimer (Tcb
);
1426 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_URG
) && !TCP_FIN_RCVD (Tcb
->State
)) {
1429 "TcpInput: received urgent data from peer for connected TCB %p\n",
1433 Urg
= Seg
->Seq
+ Seg
->Urg
;
1435 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RCVD_URG
) &&
1436 TCP_SEQ_GT (Urg
, Tcb
->RcvUp
))
1441 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RCVD_URG
);
1446 // Seventh step: Process the segment data
1448 if (Seg
->End
!= Seg
->Seq
) {
1449 if (TCP_FIN_RCVD (Tcb
->State
)) {
1452 "TcpInput: connection reset because data is lost for connected TCB %p\n",
1456 goto RESET_THEN_DROP
;
1459 if (TCP_LOCAL_CLOSED (Tcb
->State
) && (Nbuf
->TotalSize
!= 0)) {
1462 "TcpInput: connection reset because data is lost for connected TCB %p\n",
1466 goto RESET_THEN_DROP
;
1469 if (TcpQueueData (Tcb
, Nbuf
) == 0) {
1472 "TcpInput: discard a broken segment for TCB %p\n",
1479 if (TcpDeliverData (Tcb
) == -1) {
1480 goto RESET_THEN_DROP
;
1483 if (!IsListEmpty (&Tcb
->RcvQue
)) {
1484 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
1489 // Eighth step: check the FIN.
1490 // This step is moved to TcpDeliverData. FIN will be
1491 // processed in sequence there. Check the comments in
1492 // the beginning of the file header for information.
1496 // Tcb is a new child of the listening Parent,
1499 if (Parent
!= NULL
) {
1500 Tcb
->Parent
= Parent
;
1504 if ((Tcb
->State
!= TCP_CLOSED
) &&
1505 (TcpToSendData (Tcb
, 0) == 0) &&
1506 (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
) || (Nbuf
->TotalSize
!= 0)))
1515 TcpSendReset (Tcb
, Head
, Len
, Dst
, Src
, Version
);
1518 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
));
1527 TcpSendReset (Tcb
, Head
, Len
, Dst
, Src
, Version
);
1532 // Tcb is a child of Parent, and it doesn't survive
1534 DEBUG ((DEBUG_WARN
, "TcpInput: Discard a packet\n"));
1537 if ((Parent
!= NULL
) && (Tcb
!= NULL
)) {
1538 ASSERT (Tcb
->Sk
!= NULL
);
1546 Process the received ICMP error messages for TCP.
1548 @param[in] Nbuf The buffer that contains part of the TCP segment without an IP header
1549 truncated from the ICMP error packet.
1550 @param[in] IcmpErr The ICMP error code interpreted from an ICMP error packet.
1551 @param[in] Src Source address of the ICMP error message.
1552 @param[in] Dst Destination address of the ICMP error message.
1553 @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates
1561 IN EFI_IP_ADDRESS
*Src
,
1562 IN EFI_IP_ADDRESS
*Dst
,
1569 EFI_STATUS IcmpErrStatus
;
1570 BOOLEAN IcmpErrIsHard
;
1571 BOOLEAN IcmpErrNotify
;
1573 if (Nbuf
->TotalSize
< sizeof (TCP_HEAD
)) {
1577 Head
= (TCP_HEAD
*)NetbufGetByte (Nbuf
, 0, NULL
);
1578 ASSERT (Head
!= NULL
);
1580 Tcb
= TcpLocateTcb (
1588 if ((Tcb
== NULL
) || (Tcb
->State
== TCP_CLOSED
)) {
1593 // Validate the sequence number.
1595 Seq
= NTOHL (Head
->Seq
);
1596 if (!(TCP_SEQ_LEQ (Tcb
->SndUna
, Seq
) && TCP_SEQ_LT (Seq
, Tcb
->SndNxt
))) {
1600 IcmpErrStatus
= IpIoGetIcmpErrStatus (
1607 if (IcmpErrNotify
) {
1608 SOCK_ERROR (Tcb
->Sk
, IcmpErrStatus
);
1611 if (IcmpErrIsHard
) {