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
) {
54 // Step 1A: Invoking fast retransmission.
56 FlightSize
= TCP_SUB_SEQ (Tcb
->SndNxt
, Tcb
->SndUna
);
58 Tcb
->Ssthresh
= MAX (FlightSize
>> 1, (UINT32
) (2 * Tcb
->SndMss
));
59 Tcb
->Recover
= Tcb
->SndNxt
;
61 Tcb
->CongestState
= TCP_CONGEST_RECOVER
;
62 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
65 // Step 2: Entering fast retransmission
67 TcpRetransmit (Tcb
, Tcb
->SndUna
);
68 Tcb
->CWnd
= Tcb
->Ssthresh
+ 3 * Tcb
->SndMss
;
72 "TcpFastRecover: enter fast retransmission for TCB %p, recover point is %d\n",
80 // During fast recovery, execute Step 3, 4, 5 of RFC3782
82 if (Seg
->Ack
== Tcb
->SndUna
) {
85 // Step 3: Fast Recovery,
86 // If this is a duplicated ACK, increse Cwnd by SMSS.
89 // Step 4 is skipped here only to be executed later
92 Tcb
->CWnd
+= Tcb
->SndMss
;
95 "TcpFastRecover: received another duplicated ACK (%d) for TCB %p\n",
103 // New data is ACKed, check whether it is a
104 // full ACK or partial ACK
106 if (TCP_SEQ_GEQ (Seg
->Ack
, Tcb
->Recover
)) {
109 // Step 5 - Full ACK:
110 // deflate the congestion window, and exit fast recovery
112 FlightSize
= TCP_SUB_SEQ (Tcb
->SndNxt
, Tcb
->SndUna
);
114 Tcb
->CWnd
= MIN (Tcb
->Ssthresh
, FlightSize
+ Tcb
->SndMss
);
116 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
119 "TcpFastRecover: received a full ACK(%d) for TCB %p, exit fast recovery\n",
127 // Step 5 - Partial ACK:
128 // fast retransmit the first unacknowledge field
129 // , then deflate the CWnd
131 TcpRetransmit (Tcb
, Seg
->Ack
);
132 Acked
= TCP_SUB_SEQ (Seg
->Ack
, Tcb
->SndUna
);
135 // Deflate the CWnd by the amount of new data
136 // ACKed by SEG.ACK. If more than one SMSS data
137 // is ACKed, add back SMSS byte to CWnd after
139 if (Acked
>= Tcb
->SndMss
) {
140 Acked
-= Tcb
->SndMss
;
148 "TcpFastRecover: received a partial ACK(%d) for TCB %p\n",
158 NewReno fast loss recovery defined in RFC3792.
160 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
161 @param[in] Seg Segment that triggers the fast loss recovery.
170 if (TCP_SEQ_GT (Seg
->Ack
, Tcb
->SndUna
)) {
173 // New data is ACKed, check whether it is a
174 // full ACK or partial ACK
176 if (TCP_SEQ_GEQ (Seg
->Ack
, Tcb
->LossRecover
)) {
179 // Full ACK: exit the loss recovery.
182 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
186 "TcpFastLossRecover: received a full ACK(%d) for TCB %p\n",
195 // fast retransmit the first unacknowledge field.
197 TcpRetransmit (Tcb
, Seg
->Ack
);
200 "TcpFastLossRecover: received a partial ACK(%d) for TCB %p\n",
209 Compute the RTT as specified in RFC2988.
211 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
212 @param[in] Measure Currently measured RTT in heartbeats.
224 // Step 2.3: Compute the RTO for subsequent RTT measurement.
226 if (Tcb
->SRtt
!= 0) {
228 Var
= Tcb
->SRtt
- (Measure
<< TCP_RTT_SHIFT
);
234 Tcb
->RttVar
= (3 * Tcb
->RttVar
+ Var
) >> 2;
235 Tcb
->SRtt
= 7 * (Tcb
->SRtt
>> 3) + Measure
;
239 // Step 2.2: compute the first RTT measure
241 Tcb
->SRtt
= Measure
<< TCP_RTT_SHIFT
;
242 Tcb
->RttVar
= Measure
<< (TCP_RTT_SHIFT
- 1);
245 Tcb
->Rto
= (Tcb
->SRtt
+ MAX (8, 4 * Tcb
->RttVar
)) >> TCP_RTT_SHIFT
;
248 // Step 2.4: Limit the RTO to at least 1 second
249 // Step 2.5: Limit the RTO to a maxium value that
250 // is at least 60 second
252 if (Tcb
->Rto
< TCP_RTO_MIN
) {
253 Tcb
->Rto
= TCP_RTO_MIN
;
255 } else if (Tcb
->Rto
> TCP_RTO_MAX
) {
256 Tcb
->Rto
= TCP_RTO_MAX
;
262 "TcpComputeRtt: new RTT for TCB %p computed SRTT: %d RTTVAR: %d RTO: %d\n",
272 Trim the data; SYN and FIN to fit into the window defined by Left and Right.
274 @param[in] Nbuf The buffer that contains a received TCP segment without an IP header.
275 @param[in] Left The sequence number of the window's left edge.
276 @param[in] Right The sequence number of the window's right edge.
278 @retval 0 The segment is broken.
279 @retval 1 The segment is in good shape.
293 Seg
= TCPSEG_NETBUF (Nbuf
);
296 // If the segment is completely out of window,
297 // truncate every thing, include SYN and FIN.
299 if (TCP_SEQ_LEQ (Seg
->End
, Left
) || TCP_SEQ_LEQ (Right
, Seg
->Seq
)) {
301 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_SYN
);
302 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_FIN
);
305 NetbufTrim (Nbuf
, Nbuf
->TotalSize
, NET_BUF_HEAD
);
310 // Adjust the buffer header
312 if (TCP_SEQ_LT (Seg
->Seq
, Left
)) {
314 Drop
= TCP_SUB_SEQ (Left
, Seg
->Seq
);
315 Urg
= Seg
->Seq
+ Seg
->Urg
;
318 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
319 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_SYN
);
324 // Adjust the urgent point
326 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_URG
)) {
328 if (TCP_SEQ_LT (Urg
, Seg
->Seq
)) {
330 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_URG
);
332 Seg
->Urg
= (UINT16
) TCP_SUB_SEQ (Urg
, Seg
->Seq
);
337 NetbufTrim (Nbuf
, Drop
, NET_BUF_HEAD
);
342 // Adjust the buffer tail
344 if (TCP_SEQ_GT (Seg
->End
, Right
)) {
346 Drop
= TCP_SUB_SEQ (Seg
->End
, Right
);
349 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
350 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_FIN
);
355 NetbufTrim (Nbuf
, Drop
, NET_BUF_TAIL
);
359 return TcpVerifySegment (Nbuf
);
363 Trim off the data outside the tcb's receive window.
365 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
366 @param[in] Nbuf Pointer to the NET_BUF containing the received tcp segment.
368 @retval 0 The segment is broken.
369 @retval 1 The segment is in good shape.
378 return TcpTrimSegment (Nbuf
, Tcb
->RcvNxt
, Tcb
->RcvWl2
+ Tcb
->RcvWnd
);
382 Process the data and FIN flag, and check whether to deliver
383 data to the socket layer.
385 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
387 @retval 0 No error occurred to deliver data.
388 @retval -1 An error condition occurred. The proper response is to reset the
403 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
));
406 // make sure there is some data queued,
407 // and TCP is in a proper state
409 if (IsListEmpty (&Tcb
->RcvQue
) || !TCP_CONNECTED (Tcb
->State
)) {
415 // Deliver data to the socket layer
417 Entry
= Tcb
->RcvQue
.ForwardLink
;
420 while (Entry
!= &Tcb
->RcvQue
) {
421 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
422 Seg
= TCPSEG_NETBUF (Nbuf
);
424 if (TcpVerifySegment (Nbuf
) == 0) {
427 "TcpToSendData: discard a broken segment for TCB %p\n",
434 ASSERT (Nbuf
->Tcp
== NULL
);
436 if (TCP_SEQ_GT (Seg
->Seq
, Seq
)) {
440 Entry
= Entry
->ForwardLink
;
444 RemoveEntryList (&Nbuf
->List
);
447 // RFC793 Eighth step: process FIN in sequence
449 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
452 // The peer sends to us junky data after FIN,
453 // reset the connection.
455 if (!IsListEmpty (&Tcb
->RcvQue
)) {
458 "TcpDeliverData: data received after FIN from peer of TCB %p, reset connection\n",
468 "TcpDeliverData: processing FIN from peer of TCB %p\n",
472 switch (Tcb
->State
) {
474 case TCP_ESTABLISHED
:
476 TcpSetState (Tcb
, TCP_CLOSE_WAIT
);
481 if (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
)) {
483 TcpSetState (Tcb
, TCP_CLOSING
);
492 TcpSetState (Tcb
, TCP_TIME_WAIT
);
493 TcpClearAllTimer (Tcb
);
495 if (Tcb
->TimeWaitTimeout
!= 0) {
497 TcpSetTimer (Tcb
, TCP_TIMER_2MSL
, Tcb
->TimeWaitTimeout
);
502 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
516 // The peer sends to us junk FIN byte. Discard
517 // the buffer then reset the connection
526 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
532 // Don't delay the ack if PUSH flag is on.
534 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_PSH
)) {
536 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
539 if (Nbuf
->TotalSize
!= 0) {
542 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RCVD_URG
) &&
543 TCP_SEQ_LEQ (Seg
->Seq
, Tcb
->RcvUp
))
546 if (TCP_SEQ_LEQ (Seg
->End
, Tcb
->RcvUp
)) {
547 Urgent
= Nbuf
->TotalSize
;
549 Urgent
= TCP_SUB_SEQ (Tcb
->RcvUp
, Seg
->Seq
) + 1;
553 SockDataRcvd (Tcb
->Sk
, Nbuf
, Urgent
);
556 if (TCP_FIN_RCVD (Tcb
->State
)) {
558 SockNoMoreData (Tcb
->Sk
);
568 Store the data into the reassemble queue.
570 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
571 @param[in] Nbuf Pointer to the buffer containing the data to be queued.
573 @retval 0 An error condition occurred.
574 @retval 1 No error occurred to queue data.
589 ASSERT ((Tcb
!= NULL
) && (Nbuf
!= NULL
) && (Nbuf
->Tcp
== NULL
));
593 Seg
= TCPSEG_NETBUF (Nbuf
);
597 // Fast path to process normal case. That is,
598 // no out-of-order segments are received.
600 if (IsListEmpty (Head
)) {
602 InsertTailList (Head
, &Nbuf
->List
);
607 // Find the point to insert the buffer
609 for (Prev
= Head
, Cur
= Head
->ForwardLink
;
611 Prev
= Cur
, Cur
= Cur
->ForwardLink
) {
613 Node
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
615 if (TCP_SEQ_LT (Seg
->Seq
, TCPSEG_NETBUF (Node
)->Seq
)) {
621 // Check whether the current segment overlaps with the
625 Node
= NET_LIST_USER_STRUCT (Prev
, NET_BUF
, List
);
627 if (TCP_SEQ_LT (Seg
->Seq
, TCPSEG_NETBUF (Node
)->End
)) {
629 if (TCP_SEQ_LEQ (Seg
->End
, TCPSEG_NETBUF (Node
)->End
)) {
633 if (TcpTrimSegment (Nbuf
, TCPSEG_NETBUF (Node
)->End
, Seg
->End
) == 0) {
639 InsertHeadList (Prev
, &Nbuf
->List
);
641 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
644 // Check the segments after the insert point.
646 while (Cur
!= Head
) {
647 Node
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
649 if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node
)->End
, Seg
->End
)) {
651 Cur
= Cur
->ForwardLink
;
653 RemoveEntryList (&Node
->List
);
658 if (TCP_SEQ_LT (TCPSEG_NETBUF (Node
)->Seq
, Seg
->End
)) {
660 if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node
)->Seq
, Seg
->Seq
)) {
662 RemoveEntryList (&Nbuf
->List
);
666 if (TcpTrimSegment (Nbuf
, Seg
->Seq
, TCPSEG_NETBUF (Node
)->Seq
) == 0) {
667 RemoveEntryList (&Nbuf
->List
);
673 Cur
= Cur
->ForwardLink
;
681 Adjust the send queue or the retransmit queue.
683 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
684 @param[in] Ack The acknowledge seuqence number of the received segment.
686 @retval 0 An error condition occurred.
687 @retval 1 No error occurred.
702 Cur
= Head
->ForwardLink
;
704 while (Cur
!= Head
) {
705 Node
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
706 Seg
= TCPSEG_NETBUF (Node
);
708 if (TCP_SEQ_GEQ (Seg
->Seq
, Ack
)) {
713 // Remove completely ACKed segments
715 if (TCP_SEQ_LEQ (Seg
->End
, Ack
)) {
716 Cur
= Cur
->ForwardLink
;
718 RemoveEntryList (&Node
->List
);
723 return TcpTrimSegment (Node
, Ack
, Seg
->End
);
730 Process the received TCP segments.
732 @param[in] Nbuf Buffer that contains received a TCP segment without an IP header.
733 @param[in] Src Source address of the segment, or the peer's IP address.
734 @param[in] Dst Destination address of the segment, or the local end's IP
736 @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates
739 @retval 0 Segment processed successfully. It is either accepted or
740 discarded. However, no connection is reset by the segment.
741 @retval -1 A connection is reset by the segment.
747 IN EFI_IP_ADDRESS
*Src
,
748 IN EFI_IP_ADDRESS
*Dst
,
763 ASSERT ((Version
== IP_VERSION_4
) || (Version
== IP_VERSION_6
));
765 NET_CHECK_SIGNATURE (Nbuf
, NET_BUF_SIGNATURE
);
770 Head
= (TCP_HEAD
*) NetbufGetByte (Nbuf
, 0, NULL
);
771 ASSERT (Head
!= NULL
);
773 if (Nbuf
->TotalSize
< sizeof (TCP_HEAD
)) {
774 DEBUG ((EFI_D_NET
, "TcpInput: received a malformed packet\n"));
778 Len
= Nbuf
->TotalSize
- (Head
->HeadLen
<< 2);
780 if ((Head
->HeadLen
< 5) || (Len
< 0)) {
782 DEBUG ((EFI_D_NET
, "TcpInput: received a malformed packet\n"));
787 if (Version
== IP_VERSION_4
) {
788 Checksum
= NetPseudoHeadChecksum (Src
->Addr
[0], Dst
->Addr
[0], 6, 0);
790 Checksum
= NetIp6PseudoHeadChecksum (&Src
->v6
, &Dst
->v6
, 6, 0);
793 Checksum
= TcpChecksum (Nbuf
, Checksum
);
796 DEBUG ((EFI_D_ERROR
, "TcpInput: received a checksum error packet\n"));
800 if (TCP_FLG_ON (Head
->Flag
, TCP_FLG_SYN
)) {
804 if (TCP_FLG_ON (Head
->Flag
, TCP_FLG_FIN
)) {
814 (BOOLEAN
) TCP_FLG_ON (Head
->Flag
, TCP_FLG_SYN
)
817 if ((Tcb
== NULL
) || (Tcb
->State
== TCP_CLOSED
)) {
818 DEBUG ((EFI_D_NET
, "TcpInput: send reset because no TCB found\n"));
824 Seg
= TcpFormatNetbuf (Tcb
, Nbuf
);
827 // RFC1122 recommended reaction to illegal option
828 // (in fact, an illegal option length) is reset.
830 if (TcpParseOption (Nbuf
->Tcp
, &Option
) == -1) {
833 "TcpInput: reset the peer because of malformed option for TCB %p\n",
841 // From now on, the segment is headless
843 NetbufTrim (Nbuf
, (Head
->HeadLen
<< 2), NET_BUF_HEAD
);
847 // Process the segment in LISTEN state.
849 if (Tcb
->State
== TCP_LISTEN
) {
851 // First step: Check RST
853 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_RST
)) {
856 "TcpInput: discard a reset segment for TCB %p in listening\n",
864 // Second step: Check ACK.
865 // Any ACK sent to TCP in LISTEN is reseted.
867 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
)) {
870 "TcpInput: send reset because of segment with ACK for TCB %p in listening\n",
878 // Third step: Check SYN
880 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
882 // create a child TCB to handle the data
886 Tcb
= TcpCloneTcb (Parent
);
890 "TcpInput: discard a segment because failed to clone a child for TCB %p\n",
899 "TcpInput: create a child for TCB %p in listening\n",
904 // init the TCB structure
906 IP6_COPY_ADDRESS (&Tcb
->LocalEnd
.Ip
, Dst
);
907 IP6_COPY_ADDRESS (&Tcb
->RemoteEnd
.Ip
, Src
);
908 Tcb
->LocalEnd
.Port
= Head
->DstPort
;
909 Tcb
->RemoteEnd
.Port
= Head
->SrcPort
;
911 TcpInitTcbLocal (Tcb
);
912 TcpInitTcbPeer (Tcb
, Seg
, &Option
);
914 TcpSetState (Tcb
, TCP_SYN_RCVD
);
915 TcpSetTimer (Tcb
, TCP_TIMER_CONNECT
, Tcb
->ConnectTimeout
);
916 if (TcpTrimInWnd (Tcb
, Nbuf
) == 0) {
919 "TcpInput: discard a broken segment for TCB %p\n",
931 } else if (Tcb
->State
== TCP_SYN_SENT
) {
933 // First step: Check ACK bit
935 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
) && (Seg
->Ack
!= Tcb
->Iss
+ 1)) {
939 "TcpInput: send reset because of wrong ACK received for TCB %p in SYN_SENT\n",
947 // Second step: Check RST bit
949 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_RST
)) {
951 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
)) {
955 "TcpInput: connection reset by peer for TCB %p in SYN_SENT\n",
959 SOCK_ERROR (Tcb
->Sk
, EFI_CONNECTION_RESET
);
960 goto DROP_CONNECTION
;
965 "TcpInput: discard a reset segment because of no ACK for TCB %p in SYN_SENT\n",
974 // Third step: Check security and precedence. Skipped
978 // Fourth step: Check SYN. Pay attention to simultaneous open
980 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
982 TcpInitTcbPeer (Tcb
, Seg
, &Option
);
984 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
)) {
986 Tcb
->SndUna
= Seg
->Ack
;
989 TcpClearTimer (Tcb
, TCP_TIMER_REXMIT
);
991 if (TCP_SEQ_GT (Tcb
->SndUna
, Tcb
->Iss
)) {
993 TcpSetState (Tcb
, TCP_ESTABLISHED
);
995 TcpClearTimer (Tcb
, TCP_TIMER_CONNECT
);
996 TcpDeliverData (Tcb
);
998 if ((Tcb
->CongestState
== TCP_CONGEST_OPEN
) &&
999 TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
))
1002 TcpComputeRtt (Tcb
, Tcb
->RttMeasure
);
1003 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
1006 if (TcpTrimInWnd (Tcb
, Nbuf
) == 0) {
1009 "TcpInput: discard a broken segment for TCB %p\n",
1016 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
1020 "TcpInput: connection established for TCB %p in SYN_SENT\n",
1027 // Received a SYN segment without ACK, simultanous open.
1029 TcpSetState (Tcb
, TCP_SYN_RCVD
);
1031 ASSERT (Tcb
->SndNxt
== Tcb
->Iss
+ 1);
1033 if (TcpAdjustSndQue (Tcb
, Tcb
->SndNxt
) == 0 || TcpTrimInWnd (Tcb
, Nbuf
) == 0) {
1036 "TcpInput: discard a broken segment for TCB %p\n",
1045 "TcpInput: simultaneous open for TCB %p in SYN_SENT\n",
1057 // Process segment in SYN_RCVD or TCP_CONNECTED states
1061 // Clear probe timer since the RecvWindow is opened.
1063 if (Tcb
->ProbeTimerOn
&& (Seg
->Wnd
!= 0)) {
1064 TcpClearTimer (Tcb
, TCP_TIMER_PROBE
);
1065 Tcb
->ProbeTimerOn
= FALSE
;
1069 // First step: Check whether SEG.SEQ is acceptable
1071 if (TcpSeqAcceptable (Tcb
, Seg
) == 0) {
1074 "TcpInput: sequence acceptance test failed for segment of TCB %p\n",
1078 if (!TCP_FLG_ON (Seg
->Flag
, TCP_FLG_RST
)) {
1085 if ((TCP_SEQ_LT (Seg
->Seq
, Tcb
->RcvWl2
)) &&
1086 (Tcb
->RcvWl2
== Seg
->End
) &&
1087 !TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
| TCP_FLG_FIN
))
1090 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
1094 // Second step: Check the RST
1096 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_RST
)) {
1098 DEBUG ((EFI_D_WARN
, "TcpInput: connection reset for TCB %p\n", Tcb
));
1100 if (Tcb
->State
== TCP_SYN_RCVD
) {
1102 SOCK_ERROR (Tcb
->Sk
, EFI_CONNECTION_REFUSED
);
1105 // This TCB comes from either a LISTEN TCB,
1106 // or active open TCB with simultanous open.
1107 // Do NOT signal user CONNECTION refused
1108 // if it comes from a LISTEN TCB.
1110 } else if ((Tcb
->State
== TCP_ESTABLISHED
) ||
1111 (Tcb
->State
== TCP_FIN_WAIT_1
) ||
1112 (Tcb
->State
== TCP_FIN_WAIT_2
) ||
1113 (Tcb
->State
== TCP_CLOSE_WAIT
))
1116 SOCK_ERROR (Tcb
->Sk
, EFI_CONNECTION_RESET
);
1121 goto DROP_CONNECTION
;
1125 // Trim the data and flags.
1127 if (TcpTrimInWnd (Tcb
, Nbuf
) == 0) {
1130 "TcpInput: discard a broken segment for TCB %p\n",
1138 // Third step: Check security and precedence, Ignored
1142 // Fourth step: Check the SYN bit.
1144 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
1148 "TcpInput: connection reset because received extra SYN for TCB %p\n",
1152 SOCK_ERROR (Tcb
->Sk
, EFI_CONNECTION_RESET
);
1153 goto RESET_THEN_DROP
;
1156 // Fifth step: Check the ACK
1158 if (!TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
)) {
1161 "TcpInput: segment discard because of no ACK for connected TCB %p\n",
1167 if (Tcb
->IpInfo
->IpVersion
== IP_VERSION_6
&& Tcb
->Tick
== 0) {
1168 Tcp6RefreshNeighbor (Tcb
, Src
, TCP6_KEEP_NEIGHBOR_TIME
* TICKS_PER_SECOND
);
1169 Tcb
->Tick
= TCP6_REFRESH_NEIGHBOR_TICK
;
1173 if (Tcb
->State
== TCP_SYN_RCVD
) {
1175 if (TCP_SEQ_LT (Tcb
->SndUna
, Seg
->Ack
) &&
1176 TCP_SEQ_LEQ (Seg
->Ack
, Tcb
->SndNxt
))
1179 Tcb
->SndWnd
= Seg
->Wnd
;
1180 Tcb
->SndWndMax
= MAX (Tcb
->SndWnd
, Tcb
->SndWndMax
);
1181 Tcb
->SndWl1
= Seg
->Seq
;
1182 Tcb
->SndWl2
= Seg
->Ack
;
1183 TcpSetState (Tcb
, TCP_ESTABLISHED
);
1185 TcpClearTimer (Tcb
, TCP_TIMER_CONNECT
);
1186 TcpDeliverData (Tcb
);
1190 "TcpInput: connection established for TCB %p in SYN_RCVD\n",
1195 // Continue the process as ESTABLISHED state
1200 "TcpInput: send reset because of wrong ACK for TCB %p in SYN_RCVD\n",
1208 if (TCP_SEQ_LT (Seg
->Ack
, Tcb
->SndUna
)) {
1212 "TcpInput: ignore the out-of-data ACK for connected TCB %p\n",
1218 } else if (TCP_SEQ_GT (Seg
->Ack
, Tcb
->SndNxt
)) {
1222 "TcpInput: discard segment for future ACK for connected TCB %p\n",
1231 // From now on: SND.UNA <= SEG.ACK <= SND.NXT.
1233 if (TCP_FLG_ON (Option
.Flag
, TCP_OPTION_RCVD_TS
)) {
1235 // update TsRecent as specified in page 16 RFC1323.
1236 // RcvWl2 equals to the variable "LastAckSent"
1239 if (TCP_SEQ_LEQ (Seg
->Seq
, Tcb
->RcvWl2
) &&
1240 TCP_SEQ_LT (Tcb
->RcvWl2
, Seg
->End
))
1243 Tcb
->TsRecent
= Option
.TSVal
;
1244 Tcb
->TsRecentAge
= mTcpTick
;
1247 TcpComputeRtt (Tcb
, TCP_SUB_TIME (mTcpTick
, Option
.TSEcr
));
1249 } else if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
)) {
1251 ASSERT (Tcb
->CongestState
== TCP_CONGEST_OPEN
);
1253 TcpComputeRtt (Tcb
, Tcb
->RttMeasure
);
1254 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
1257 if (Seg
->Ack
== Tcb
->SndNxt
) {
1259 TcpClearTimer (Tcb
, TCP_TIMER_REXMIT
);
1262 TcpSetTimer (Tcb
, TCP_TIMER_REXMIT
, Tcb
->Rto
);
1266 // Count duplicate acks.
1268 if ((Seg
->Ack
== Tcb
->SndUna
) &&
1269 (Tcb
->SndUna
!= Tcb
->SndNxt
) &&
1270 (Seg
->Wnd
== Tcb
->SndWnd
) &&
1281 // Congestion avoidance, fast recovery and fast retransmission.
1283 if (((Tcb
->CongestState
== TCP_CONGEST_OPEN
) && (Tcb
->DupAck
< 3)) ||
1284 (Tcb
->CongestState
== TCP_CONGEST_LOSS
))
1287 if (TCP_SEQ_GT (Seg
->Ack
, Tcb
->SndUna
)) {
1289 if (Tcb
->CWnd
< Tcb
->Ssthresh
) {
1291 Tcb
->CWnd
+= Tcb
->SndMss
;
1294 Tcb
->CWnd
+= MAX (Tcb
->SndMss
* Tcb
->SndMss
/ Tcb
->CWnd
, 1);
1297 Tcb
->CWnd
= MIN (Tcb
->CWnd
, TCP_MAX_WIN
<< Tcb
->SndWndScale
);
1300 if (Tcb
->CongestState
== TCP_CONGEST_LOSS
) {
1301 TcpFastLossRecover (Tcb
, Seg
);
1305 TcpFastRecover (Tcb
, Seg
);
1308 if (TCP_SEQ_GT (Seg
->Ack
, Tcb
->SndUna
)) {
1310 if (TcpAdjustSndQue (Tcb
, Seg
->Ack
) == 0) {
1313 "TcpInput: discard a broken segment for TCB %p\n",
1320 Tcb
->SndUna
= Seg
->Ack
;
1322 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
) &&
1323 TCP_SEQ_LT (Tcb
->SndUp
, Seg
->Ack
))
1326 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
);
1331 // Update window info
1333 if (TCP_SEQ_LT (Tcb
->SndWl1
, Seg
->Seq
) ||
1334 ((Tcb
->SndWl1
== Seg
->Seq
) && TCP_SEQ_LEQ (Tcb
->SndWl2
, Seg
->Ack
)))
1337 Right
= Seg
->Ack
+ Seg
->Wnd
;
1339 if (TCP_SEQ_LT (Right
, Tcb
->SndWl2
+ Tcb
->SndWnd
)) {
1341 if ((Tcb
->SndWl1
== Seg
->Seq
) &&
1342 (Tcb
->SndWl2
== Seg
->Ack
) &&
1351 "TcpInput: peer shrinks the window for connected TCB %p\n",
1355 if ((Tcb
->CongestState
== TCP_CONGEST_RECOVER
) &&
1356 (TCP_SEQ_LT (Right
, Tcb
->Recover
)))
1359 Tcb
->Recover
= Right
;
1362 if ((Tcb
->CongestState
== TCP_CONGEST_LOSS
) &&
1363 (TCP_SEQ_LT (Right
, Tcb
->LossRecover
)))
1366 Tcb
->LossRecover
= Right
;
1369 if (TCP_SEQ_LT (Right
, Tcb
->SndNxt
)) {
1371 // Check for Window Retraction in RFC7923 section 2.4.
1372 // The lower n bits of the peer's actual receive window is wiped out if TCP
1373 // window scale is enabled, it will look like the peer is shrinking the window.
1374 // Check whether the SndNxt is out of the advertised receive window by more than
1375 // 2^Rcv.Wind.Shift before moving the SndNxt to the left.
1379 "TcpInput: peer advise negative useable window for connected TCB %p\n",
1382 Usable
= TCP_SUB_SEQ (Tcb
->SndNxt
, Right
);
1383 if ((Usable
>> Tcb
->SndWndScale
) > 0) {
1386 "TcpInput: SndNxt is out of window by more than window scale for TCB %p\n",
1389 Tcb
->SndNxt
= Right
;
1391 if (Right
== Tcb
->SndUna
) {
1393 TcpClearTimer (Tcb
, TCP_TIMER_REXMIT
);
1394 TcpSetProbeTimer (Tcb
);
1399 Tcb
->SndWnd
= Seg
->Wnd
;
1400 Tcb
->SndWndMax
= MAX (Tcb
->SndWnd
, Tcb
->SndWndMax
);
1401 Tcb
->SndWl1
= Seg
->Seq
;
1402 Tcb
->SndWl2
= Seg
->Ack
;
1407 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
) &&
1408 (Tcb
->SndUna
== Tcb
->SndNxt
))
1413 "TcpInput: local FIN is ACKed by peer for connected TCB %p\n",
1417 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
);
1421 // Transit the state if proper.
1423 switch (Tcb
->State
) {
1424 case TCP_FIN_WAIT_1
:
1426 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
)) {
1428 TcpSetState (Tcb
, TCP_FIN_WAIT_2
);
1430 TcpClearAllTimer (Tcb
);
1431 TcpSetTimer (Tcb
, TCP_TIMER_FINWAIT2
, Tcb
->FinWait2Timeout
);
1434 case TCP_FIN_WAIT_2
:
1438 case TCP_CLOSE_WAIT
:
1443 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
)) {
1445 TcpSetState (Tcb
, TCP_TIME_WAIT
);
1447 TcpClearAllTimer (Tcb
);
1449 if (Tcb
->TimeWaitTimeout
!= 0) {
1451 TcpSetTimer (Tcb
, TCP_TIMER_2MSL
, Tcb
->TimeWaitTimeout
);
1456 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
1467 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_ACKED
)) {
1469 TcpSetState (Tcb
, TCP_CLOSED
);
1478 if (Tcb
->TimeWaitTimeout
!= 0) {
1480 TcpSetTimer (Tcb
, TCP_TIMER_2MSL
, Tcb
->TimeWaitTimeout
);
1485 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
1497 // Sixth step: Check the URG bit.update the Urg point
1498 // if in TCP_CAN_RECV, otherwise, leave the RcvUp intact.
1503 TcpSetKeepaliveTimer (Tcb
);
1505 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_URG
) && !TCP_FIN_RCVD (Tcb
->State
)) {
1509 "TcpInput: received urgent data from peer for connected TCB %p\n",
1513 Urg
= Seg
->Seq
+ Seg
->Urg
;
1515 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RCVD_URG
) &&
1516 TCP_SEQ_GT (Urg
, Tcb
->RcvUp
))
1523 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RCVD_URG
);
1527 // Seventh step: Process the segment data
1529 if (Seg
->End
!= Seg
->Seq
) {
1531 if (TCP_FIN_RCVD (Tcb
->State
)) {
1535 "TcpInput: connection reset because data is lost for connected TCB %p\n",
1539 goto RESET_THEN_DROP
;
1542 if (TCP_LOCAL_CLOSED (Tcb
->State
) && (Nbuf
->TotalSize
!= 0)) {
1545 "TcpInput: connection reset because data is lost for connected TCB %p\n",
1549 goto RESET_THEN_DROP
;
1552 if (TcpQueueData (Tcb
, Nbuf
) == 0) {
1555 "TcpInput: discard a broken segment for TCB %p\n",
1562 if (TcpDeliverData (Tcb
) == -1) {
1563 goto RESET_THEN_DROP
;
1566 if (!IsListEmpty (&Tcb
->RcvQue
)) {
1567 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
1572 // Eighth step: check the FIN.
1573 // This step is moved to TcpDeliverData. FIN will be
1574 // processed in sequence there. Check the comments in
1575 // the beginning of the file header for information.
1579 // Tcb is a new child of the listening Parent,
1582 if (Parent
!= NULL
) {
1583 Tcb
->Parent
= Parent
;
1587 if ((Tcb
->State
!= TCP_CLOSED
) &&
1588 (TcpToSendData (Tcb
, 0) == 0) &&
1589 (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
) || (Nbuf
->TotalSize
!= 0)))
1599 TcpSendReset (Tcb
, Head
, Len
, Dst
, Src
, Version
);
1602 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
));
1611 TcpSendReset (Tcb
, Head
, Len
, Dst
, Src
, Version
);
1616 // Tcb is a child of Parent, and it doesn't survive
1618 DEBUG ((EFI_D_WARN
, "TcpInput: Discard a packet\n"));
1621 if ((Parent
!= NULL
) && (Tcb
!= NULL
)) {
1623 ASSERT (Tcb
->Sk
!= NULL
);
1631 Process the received ICMP error messages for TCP.
1633 @param[in] Nbuf The buffer that contains part of the TCP segment without an IP header
1634 truncated from the ICMP error packet.
1635 @param[in] IcmpErr The ICMP error code interpreted from an ICMP error packet.
1636 @param[in] Src Source address of the ICMP error message.
1637 @param[in] Dst Destination address of the ICMP error message.
1638 @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates
1646 IN EFI_IP_ADDRESS
*Src
,
1647 IN EFI_IP_ADDRESS
*Dst
,
1654 EFI_STATUS IcmpErrStatus
;
1655 BOOLEAN IcmpErrIsHard
;
1656 BOOLEAN IcmpErrNotify
;
1658 if (Nbuf
->TotalSize
< sizeof (TCP_HEAD
)) {
1662 Head
= (TCP_HEAD
*) NetbufGetByte (Nbuf
, 0, NULL
);
1663 ASSERT (Head
!= NULL
);
1665 Tcb
= TcpLocateTcb (
1673 if (Tcb
== NULL
|| Tcb
->State
== TCP_CLOSED
) {
1679 // Validate the sequence number.
1681 Seq
= NTOHL (Head
->Seq
);
1682 if (!(TCP_SEQ_LEQ (Tcb
->SndUna
, Seq
) && TCP_SEQ_LT (Seq
, Tcb
->SndNxt
))) {
1687 IcmpErrStatus
= IpIoGetIcmpErrStatus (
1694 if (IcmpErrNotify
) {
1696 SOCK_ERROR (Tcb
->Sk
, IcmpErrStatus
);
1699 if (IcmpErrIsHard
) {