]>
git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/TcpDxe/TcpOutput.c
2 TCP output process routines.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 UINT8 mTcpOutFlag
[] = {
15 TCP_FLG_SYN
, // TCP_SYN_SENT
16 TCP_FLG_SYN
| TCP_FLG_ACK
, // TCP_SYN_RCVD
17 TCP_FLG_ACK
, // TCP_ESTABLISHED
18 TCP_FLG_FIN
| TCP_FLG_ACK
, // TCP_FIN_WAIT_1
19 TCP_FLG_ACK
, // TCP_FIN_WAIT_2
20 TCP_FLG_ACK
| TCP_FLG_FIN
, // TCP_CLOSING
21 TCP_FLG_ACK
, // TCP_TIME_WAIT
22 TCP_FLG_ACK
, // TCP_CLOSE_WAIT
23 TCP_FLG_FIN
| TCP_FLG_ACK
// TCP_LAST_ACK
27 Compute the sequence space left in the old receive window.
29 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
31 @return The sequence space left in the old receive window.
43 if (TCP_SEQ_GT (Tcb
->RcvWl2
+ Tcb
->RcvWnd
, Tcb
->RcvNxt
)) {
45 OldWin
= TCP_SUB_SEQ (
46 Tcb
->RcvWl2
+ Tcb
->RcvWnd
,
55 Compute the current receive window.
57 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
59 @return The size of the current receive window, in bytes.
75 OldWin
= TcpRcvWinOld (Tcb
);
77 Win
= SockGetFreeSpace (Sk
, SOCK_RCV_BUF
);
81 Increase
= Win
- OldWin
;
85 // Receiver's SWS: don't advertise a bigger window
86 // unless it can be increased by at least one Mss or
87 // half of the receive buffer.
89 if ((Increase
> Tcb
->SndMss
) || (2 * Increase
>= GET_RCV_BUFFSIZE (Sk
))) {
98 Compute the value to fill in the window size field of the outgoing segment.
100 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
101 @param[in] Syn The flag to indicate whether the outgoing segment
104 @return The value of the local receive window size used to fill the outgoing segment.
116 // RFC requires that initial window not be scaled
120 Wnd
= GET_RCV_BUFFSIZE (Tcb
->Sk
);
123 Wnd
= TcpRcvWinNow (Tcb
);
128 Wnd
= MIN (Wnd
>> Tcb
->RcvWndScale
, 0xffff);
129 return NTOHS ((UINT16
) Wnd
);
133 Get the maximum SndNxt.
135 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
137 @return The sequence number of the maximum SndNxt.
148 if (IsListEmpty (&Tcb
->SndQue
)) {
152 Entry
= Tcb
->SndQue
.BackLink
;
153 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
155 ASSERT (TCP_SEQ_GEQ (TCPSEG_NETBUF (Nbuf
)->End
, Tcb
->SndNxt
));
156 return TCPSEG_NETBUF (Nbuf
)->End
;
160 Compute how much data to send.
162 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
163 @param[in] Force If TRUE, to ignore the sender's SWS avoidance algorithm and send
166 @return The length of the data can be sent. If 0, no data can be sent.
185 // TCP should NOT send data beyond the send window
186 // and congestion window. The right edge of send
187 // window is defined as SND.WL2 + SND.WND. The right
188 // edge of congestion window is defined as SND.UNA +
192 Limit
= Tcb
->SndWl2
+ Tcb
->SndWnd
;
194 if (TCP_SEQ_GT (Limit
, Tcb
->SndUna
+ Tcb
->CWnd
)) {
196 Limit
= Tcb
->SndUna
+ Tcb
->CWnd
;
199 if (TCP_SEQ_GT (Limit
, Tcb
->SndNxt
)) {
200 Win
= TCP_SUB_SEQ (Limit
, Tcb
->SndNxt
);
204 // The data to send contains two parts: the data on the
205 // socket send queue, and the data on the TCB's send
206 // buffer. The later can be non-zero if the peer shrinks
207 // its advertised window.
209 Left
= GET_SND_DATASIZE (Sk
) + TCP_SUB_SEQ (TcpGetMaxSndNxt (Tcb
), Tcb
->SndNxt
);
211 Len
= MIN (Win
, Left
);
213 if (Len
> Tcb
->SndMss
) {
217 if ((Force
!= 0)|| (Len
== 0 && Left
== 0)) {
221 if (Len
== 0 && Left
!= 0) {
222 goto SetPersistTimer
;
226 // Sender's SWS avoidance: Don't send a small segment unless
227 // a)A full-sized segment can be sent,
228 // b)At least one-half of the maximum sized windows that
229 // the other end has ever advertised.
230 // c)It can send everything it has, and either it isn't
231 // expecting an ACK, or the Nagle algorithm is disabled.
233 if ((Len
== Tcb
->SndMss
) || (2 * Len
>= Tcb
->SndWndMax
)) {
239 ((Tcb
->SndNxt
== Tcb
->SndUna
) || TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
))
246 // RFC1122 suggests to set a timer when SWSA forbids TCP
247 // sending more data, and combines it with a probe timer.
250 if (!TCP_TIMER_ON (Tcb
->EnabledTimer
, TCP_TIMER_REXMIT
)) {
254 "TcpDataToSend: enter persistent state for TCB %p\n",
258 if (!Tcb
->ProbeTimerOn
) {
259 TcpSetProbeTimer (Tcb
);
267 Build the TCP header of the TCP segment and transmit the segment by IP.
269 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
270 @param[in] Nbuf Pointer to the buffer containing the segment to be
273 @retval 0 The segment was sent out successfully.
274 @retval -1 An error condition occurred.
289 ASSERT ((Nbuf
!= NULL
) && (Nbuf
->Tcp
== NULL
));
291 if (TcpVerifySegment (Nbuf
) == 0) {
295 DataLen
= Nbuf
->TotalSize
;
297 Seg
= TCPSEG_NETBUF (Nbuf
);
298 Syn
= TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
);
302 Len
= TcpSynBuildOption (Tcb
, Nbuf
);
305 Len
= TcpBuildOption (Tcb
, Nbuf
);
308 ASSERT ((Len
% 4 == 0) && (Len
<= 40));
310 Len
+= sizeof (TCP_HEAD
);
312 Head
= (TCP_HEAD
*) NetbufAllocSpace (
318 ASSERT (Head
!= NULL
);
322 Head
->SrcPort
= Tcb
->LocalEnd
.Port
;
323 Head
->DstPort
= Tcb
->RemoteEnd
.Port
;
324 Head
->Seq
= NTOHL (Seg
->Seq
);
325 Head
->Ack
= NTOHL (Tcb
->RcvNxt
);
326 Head
->HeadLen
= (UINT8
) (Len
>> 2);
328 Head
->Wnd
= TcpComputeWnd (Tcb
, Syn
);
332 // Check whether to set the PSH flag.
334 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_PSH
);
337 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
) &&
338 TCP_SEQ_BETWEEN (Seg
->Seq
, Tcb
->SndPsh
, Seg
->End
)
341 TCP_SET_FLG (Seg
->Flag
, TCP_FLG_PSH
);
342 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
);
344 } else if ((Seg
->End
== Tcb
->SndNxt
) && (GET_SND_DATASIZE (Tcb
->Sk
) == 0)) {
346 TCP_SET_FLG (Seg
->Flag
, TCP_FLG_PSH
);
351 // Check whether to set the URG flag and the urgent pointer.
353 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_URG
);
355 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
) && TCP_SEQ_LEQ (Seg
->Seq
, Tcb
->SndUp
)) {
357 TCP_SET_FLG (Seg
->Flag
, TCP_FLG_URG
);
359 if (TCP_SEQ_LT (Tcb
->SndUp
, Seg
->End
)) {
361 Seg
->Urg
= (UINT16
) TCP_SUB_SEQ (Tcb
->SndUp
, Seg
->Seq
);
364 Seg
->Urg
= (UINT16
) MIN (
365 TCP_SUB_SEQ (Tcb
->SndUp
,
372 Head
->Flag
= Seg
->Flag
;
373 Head
->Urg
= NTOHS (Seg
->Urg
);
374 Head
->Checksum
= TcpChecksum (Nbuf
, Tcb
->HeadSum
);
377 // Update the TCP session's control information.
379 Tcb
->RcvWl2
= Tcb
->RcvNxt
;
381 Tcb
->RcvWnd
= NTOHS (Head
->Wnd
);
385 // Clear the delayedack flag.
389 return TcpSendIpPacket (Tcb
, Nbuf
, &Tcb
->LocalEnd
.Ip
, &Tcb
->RemoteEnd
.Ip
, Tcb
->Sk
->IpVersion
);
393 Get a segment from the Tcb's SndQue.
395 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
396 @param[in] Seq The sequence number of the segment.
397 @param[in] Len The maximum length of the segment.
399 @return Pointer to the segment. If NULL, some error occurred.
403 TcpGetSegmentSndQue (
420 ASSERT ((Tcb
!= NULL
) && TCP_SEQ_LEQ (Seq
, Tcb
->SndNxt
) && (Len
> 0));
423 // Find the segment that contains the Seq.
430 NET_LIST_FOR_EACH (Cur
, Head
) {
431 Node
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
432 Seg
= TCPSEG_NETBUF (Node
);
434 if (TCP_SEQ_LT (Seq
, Seg
->End
) && TCP_SEQ_LEQ (Seg
->Seq
, Seq
)) {
440 if ((Cur
== Head
) || (Seg
== NULL
) || (Node
== NULL
)) {
445 // Return the buffer if it can be returned without
448 if ((Seg
->Seq
== Seq
) &&
449 TCP_SEQ_LEQ (Seg
->End
, Seg
->Seq
+ Len
) &&
450 !NET_BUF_SHARED (Node
)
458 // Create a new buffer and copy data there.
460 Nbuf
= NetbufAlloc (Len
+ TCP_MAX_HEAD
);
466 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
471 if (TCP_SEQ_LT (Seq
+ Len
, Seg
->End
)) {
475 CopyLen
= TCP_SUB_SEQ (End
, Seq
);
476 Offset
= TCP_SUB_SEQ (Seq
, Seg
->Seq
);
479 // If SYN is set and out of the range, clear the flag.
480 // Becuase the sequence of the first byte is SEG.SEQ+1,
481 // adjust Offset by -1. If SYN is in the range, copy
484 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
486 if (TCP_SEQ_LT (Seg
->Seq
, Seq
)) {
488 TCP_CLEAR_FLG (Flag
, TCP_FLG_SYN
);
497 // If FIN is set and in the range, copy one byte less,
498 // and if it is out of the range, clear the flag.
500 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
502 if (Seg
->End
== End
) {
507 TCP_CLEAR_FLG (Flag
, TCP_FLG_FIN
);
511 ASSERT (CopyLen
>= 0);
514 // Copy data to the segment
517 Data
= NetbufAllocSpace (Nbuf
, CopyLen
, NET_BUF_TAIL
);
518 ASSERT (Data
!= NULL
);
520 if ((INT32
) NetbufCopy (Node
, Offset
, CopyLen
, Data
) != CopyLen
) {
525 CopyMem (TCPSEG_NETBUF (Nbuf
), Seg
, sizeof (TCP_SEG
));
527 TCPSEG_NETBUF (Nbuf
)->Seq
= Seq
;
528 TCPSEG_NETBUF (Nbuf
)->End
= End
;
529 TCPSEG_NETBUF (Nbuf
)->Flag
= Flag
;
539 Get a segment from the Tcb's socket buffer.
541 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
542 @param[in] Seq The sequence number of the segment.
543 @param[in] Len The maximum length of the segment.
545 @return Pointer to the segment. If NULL, some error occurred.
559 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
));
561 Nbuf
= NetbufAlloc (Len
+ TCP_MAX_HEAD
);
566 "TcpGetSegmentSock: failed to allocate a netbuf for TCB %p\n",
573 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
579 // copy data to the segment.
581 Data
= NetbufAllocSpace (Nbuf
, Len
, NET_BUF_TAIL
);
582 ASSERT (Data
!= NULL
);
584 DataGet
= SockGetDataToSend (Tcb
->Sk
, 0, Len
, Data
);
589 TCPSEG_NETBUF (Nbuf
)->Seq
= Seq
;
590 TCPSEG_NETBUF (Nbuf
)->End
= Seq
+ Len
;
592 InsertTailList (&(Tcb
->SndQue
), &(Nbuf
->List
));
596 SockDataSent (Tcb
->Sk
, DataGet
);
603 Get a segment starting from sequence Seq of a maximum
606 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
607 @param[in] Seq The sequence number of the segment.
608 @param[in] Len The maximum length of the segment.
610 @return Pointer to the segment. If NULL, some error occurred.
622 ASSERT (Tcb
!= NULL
);
625 // Compare the SndNxt with the max sequence number sent.
627 if ((Len
!= 0) && TCP_SEQ_LT (Seq
, TcpGetMaxSndNxt (Tcb
))) {
629 Nbuf
= TcpGetSegmentSndQue (Tcb
, Seq
, Len
);
632 Nbuf
= TcpGetSegmentSock (Tcb
, Seq
, Len
);
635 if (TcpVerifySegment (Nbuf
) == 0) {
644 Retransmit the segment from sequence Seq.
646 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
647 @param[in] Seq The sequence number of the segment to be retransmitted.
649 @retval 0 Retransmission succeeded.
650 @retval -1 Error condition occurred.
663 // Compute the maxium length of retransmission. It is
664 // limited by three factors:
665 // 1. Less than SndMss
666 // 2. Must in the current send window
667 // 3. Will not change the boundaries of queued segments.
671 // Handle the Window Retraction if TCP window scale is enabled according to RFC7323:
672 // On first retransmission, or if the sequence number is out of
673 // window by less than 2^Rcv.Wind.Shift, then do normal
674 // retransmission(s) without regard to the receiver window as long
675 // as the original segment was in window when it was sent.
677 if ((Tcb
->SndWndScale
!= 0) &&
678 (TCP_SEQ_GT (Seq
, Tcb
->RetxmitSeqMax
) || TCP_SEQ_BETWEEN (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
, Tcb
->SndWl2
+ Tcb
->SndWnd
+ (1 << Tcb
->SndWndScale
)))) {
679 Len
= TCP_SUB_SEQ (Tcb
->SndNxt
, Seq
);
682 "TcpRetransmit: retransmission without regard to the receiver window for TCB %p\n",
686 } else if (TCP_SEQ_GEQ (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
)) {
687 Len
= TCP_SUB_SEQ (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
);
692 "TcpRetransmit: retransmission cancelled because send window too small for TCB %p\n",
699 Len
= MIN (Len
, Tcb
->SndMss
);
701 Nbuf
= TcpGetSegmentSndQue (Tcb
, Seq
, Len
);
706 if (TcpVerifySegment (Nbuf
) == 0) {
710 if (TcpTransmitSegment (Tcb
, Nbuf
) != 0) {
714 if (TCP_SEQ_GT (Seq
, Tcb
->RetxmitSeqMax
)) {
715 Tcb
->RetxmitSeqMax
= Seq
;
719 // The retransmitted buffer may be on the SndQue,
720 // trim TCP head because all the buffers on SndQue
723 ASSERT (Nbuf
->Tcp
!= NULL
);
724 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
739 Verify that all the segments in SndQue are in good shape.
741 @param[in] Head Pointer to the head node of the SndQue.
743 @retval 0 At least one segment is broken.
744 @retval 1 All segments in the specific queue are in good shape.
756 if (IsListEmpty (Head
)) {
760 // Initialize the Seq.
762 Entry
= Head
->ForwardLink
;
763 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
764 Seq
= TCPSEG_NETBUF (Nbuf
)->Seq
;
766 NET_LIST_FOR_EACH (Entry
, Head
) {
767 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
769 if (TcpVerifySegment (Nbuf
) == 0) {
774 // All the node in the SndQue should has:
775 // SEG.SEQ = LAST_SEG.END
777 if (Seq
!= TCPSEG_NETBUF (Nbuf
)->Seq
) {
781 Seq
= TCPSEG_NETBUF (Nbuf
)->End
;
788 Check whether to send data/SYN/FIN and piggyback an ACK.
790 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
791 @param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm
792 and send out data by force.
794 @return The number of bytes sent.
811 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
) && (Tcb
->State
!= TCP_LISTEN
));
815 if ((Tcb
->State
== TCP_CLOSED
) || TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
)) {
822 // Compute how much data can be sent
824 Len
= TcpDataToSend (Tcb
, Force
);
827 ASSERT ((Tcb
->State
) < (ARRAY_SIZE (mTcpOutFlag
)));
828 Flag
= mTcpOutFlag
[Tcb
->State
];
830 if ((Flag
& TCP_FLG_SYN
) != 0) {
837 // Only send a segment without data if SYN or
840 if ((Len
== 0) && ((Flag
& (TCP_FLG_SYN
| TCP_FLG_FIN
)) == 0)) {
844 Nbuf
= TcpGetSegment (Tcb
, Seq
, Len
);
849 "TcpToSendData: failed to get a segment for TCB %p\n",
856 Seg
= TCPSEG_NETBUF (Nbuf
);
859 // Set the TcpSeg in Nbuf.
861 Len
= Nbuf
->TotalSize
;
863 if (TCP_FLG_ON (Flag
, TCP_FLG_SYN
)) {
867 if ((Flag
& TCP_FLG_FIN
) != 0) {
869 // Send FIN if all data is sent, and FIN is
872 if ((TcpGetMaxSndNxt (Tcb
) == Tcb
->SndNxt
) &&
873 (GET_SND_DATASIZE (Tcb
->Sk
) == 0) &&
874 TCP_SEQ_LT (End
+ 1, Tcb
->SndWnd
+ Tcb
->SndWl2
)
878 "TcpToSendData: send FIN to peer for TCB %p in state %s\n",
880 mTcpStateName
[Tcb
->State
])
885 TCP_CLEAR_FLG (Flag
, TCP_FLG_FIN
);
893 if (TcpVerifySegment (Nbuf
) == 0 || TcpCheckSndQue (&Tcb
->SndQue
) == 0) {
896 "TcpToSendData: discard a broken segment for TCB %p\n",
903 // Don't send an empty segment here.
905 if (Seg
->End
== Seg
->Seq
) {
908 "TcpToSendData: created a empty segment for TCB %p, free it now\n",
915 if (TcpTransmitSegment (Tcb
, Nbuf
) != 0) {
916 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
919 if ((Flag
& TCP_FLG_FIN
) != 0) {
920 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
);
926 Sent
+= TCP_SUB_SEQ (End
, Seq
);
929 // All the buffers in the SndQue are headless.
931 ASSERT (Nbuf
->Tcp
!= NULL
);
933 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
939 // Update the status in TCB.
943 if ((Flag
& TCP_FLG_FIN
) != 0) {
944 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
);
947 if (TCP_SEQ_GT (End
, Tcb
->SndNxt
)) {
951 if (!TCP_TIMER_ON (Tcb
->EnabledTimer
, TCP_TIMER_REXMIT
)) {
952 TcpSetTimer (Tcb
, TCP_TIMER_REXMIT
, Tcb
->Rto
);
956 // Enable RTT measurement only if not in retransmit.
957 // Karn's algorithm requires not to update RTT when in loss.
959 if ((Tcb
->CongestState
== TCP_CONGEST_OPEN
) && !TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
)) {
963 "TcpToSendData: set RTT measure sequence %d for TCB %p\n",
968 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
973 } while (Len
== Tcb
->SndMss
);
986 Send an ACK immediately.
988 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
999 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
1005 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
1007 Seg
= TCPSEG_NETBUF (Nbuf
);
1008 Seg
->Seq
= Tcb
->SndNxt
;
1009 Seg
->End
= Tcb
->SndNxt
;
1010 Seg
->Flag
= TCP_FLG_ACK
;
1012 if (TcpTransmitSegment (Tcb
, Nbuf
) == 0) {
1013 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
1014 Tcb
->DelayedAck
= 0;
1021 Send a zero probe segment. It can be used by keepalive and zero window probe.
1023 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
1025 @retval 0 The zero probe segment was sent out successfully.
1026 @retval other An error condition occurred.
1038 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
1044 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
1047 // SndNxt-1 is out of window. The peer should respond
1050 Seg
= TCPSEG_NETBUF (Nbuf
);
1051 Seg
->Seq
= Tcb
->SndNxt
- 1;
1052 Seg
->End
= Tcb
->SndNxt
- 1;
1053 Seg
->Flag
= TCP_FLG_ACK
;
1055 Result
= TcpTransmitSegment (Tcb
, Nbuf
);
1062 Check whether to send an ACK or delayed ACK.
1064 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
1075 // Generally, TCP should send a delayed ACK unless:
1076 // 1. ACK at least every other FULL sized segment received.
1077 // 2. Packets received out of order.
1078 // 3. Receiving window is open.
1080 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
) || (Tcb
->DelayedAck
>= 1)) {
1085 TcpNow
= TcpRcvWinNow (Tcb
);
1087 if (TcpNow
> TcpRcvWinOld (Tcb
)) {
1094 "TcpToSendAck: scheduled a delayed ACK for TCB %p\n",
1099 // Schedule a delayed ACK.
1105 Send a RESET segment in response to the segment received.
1107 @param[in] Tcb Pointer to the TCP_CB of this TCP instance. May be NULL.
1108 @param[in] Head TCP header of the segment that triggers the reset.
1109 @param[in] Len Length of the segment that triggers the reset.
1110 @param[in] Local Local IP address.
1111 @param[in] Remote Remote peer's IP address.
1112 @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack,
1113 IP_VERSION_6 indicates TCP is running on IP6 stack.
1115 @retval 0 A reset was sent or there is no need to send it.
1116 @retval -1 No reset is sent.
1124 IN EFI_IP_ADDRESS
*Local
,
1125 IN EFI_IP_ADDRESS
*Remote
,
1134 // Don't respond to a Reset with reset.
1136 if ((Head
->Flag
& TCP_FLG_RST
) != 0) {
1140 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
1146 Nhead
= (TCP_HEAD
*) NetbufAllocSpace (
1152 ASSERT (Nhead
!= NULL
);
1155 Nhead
->Flag
= TCP_FLG_RST
;
1158 // Derive Seq/ACK from the segment if no TCB
1159 // is associated with it, otherwise derive from the Tcb.
1163 if (TCP_FLG_ON (Head
->Flag
, TCP_FLG_ACK
)) {
1164 Nhead
->Seq
= Head
->Ack
;
1168 TCP_SET_FLG (Nhead
->Flag
, TCP_FLG_ACK
);
1169 Nhead
->Ack
= HTONL (NTOHL (Head
->Seq
) + Len
);
1173 Nhead
->Seq
= HTONL (Tcb
->SndNxt
);
1174 Nhead
->Ack
= HTONL (Tcb
->RcvNxt
);
1175 TCP_SET_FLG (Nhead
->Flag
, TCP_FLG_ACK
);
1178 Nhead
->SrcPort
= Head
->DstPort
;
1179 Nhead
->DstPort
= Head
->SrcPort
;
1180 Nhead
->HeadLen
= (UINT8
) (sizeof (TCP_HEAD
) >> 2);
1182 Nhead
->Wnd
= HTONS (0xFFFF);
1183 Nhead
->Checksum
= 0;
1186 if (Version
== IP_VERSION_4
) {
1187 HeadSum
= NetPseudoHeadChecksum (Local
->Addr
[0], Remote
->Addr
[0], 6, 0);
1189 HeadSum
= NetIp6PseudoHeadChecksum (&Local
->v6
, &Remote
->v6
, 6, 0);
1192 Nhead
->Checksum
= TcpChecksum (Nbuf
, HeadSum
);
1194 TcpSendIpPacket (Tcb
, Nbuf
, Local
, Remote
, Version
);
1202 Verify that the segment is in good shape.
1204 @param[in] Nbuf The buffer that contains the segment to be checked.
1206 @retval 0 The segment is broken.
1207 @retval 1 The segment is in good shape.
1223 NET_CHECK_SIGNATURE (Nbuf
, NET_BUF_SIGNATURE
);
1225 Seg
= TCPSEG_NETBUF (Nbuf
);
1226 Len
= Nbuf
->TotalSize
;
1230 if (Head
->Flag
!= Seg
->Flag
) {
1234 Len
-= (Head
->HeadLen
<< 2);
1237 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
1241 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
1245 if (Seg
->Seq
+ Len
!= Seg
->End
) {