]>
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
)) {
44 OldWin
= TCP_SUB_SEQ (
45 Tcb
->RcvWl2
+ Tcb
->RcvWnd
,
54 Compute the current receive window.
56 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
58 @return The size of the current receive window, in bytes.
74 OldWin
= TcpRcvWinOld (Tcb
);
76 Win
= SockGetFreeSpace (Sk
, SOCK_RCV_BUF
);
80 Increase
= Win
- OldWin
;
84 // Receiver's SWS: don't advertise a bigger window
85 // unless it can be increased by at least one Mss or
86 // half of the receive buffer.
88 if ((Increase
> Tcb
->SndMss
) || (2 * Increase
>= GET_RCV_BUFFSIZE (Sk
))) {
96 Compute the value to fill in the window size field of the outgoing segment.
98 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
99 @param[in] Syn The flag to indicate whether the outgoing segment
102 @return The value of the local receive window size used to fill the outgoing segment.
114 // RFC requires that initial window not be scaled
117 Wnd
= GET_RCV_BUFFSIZE (Tcb
->Sk
);
119 Wnd
= TcpRcvWinNow (Tcb
);
124 Wnd
= MIN (Wnd
>> Tcb
->RcvWndScale
, 0xffff);
125 return NTOHS ((UINT16
)Wnd
);
129 Get the maximum SndNxt.
131 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
133 @return The sequence number of the maximum SndNxt.
144 if (IsListEmpty (&Tcb
->SndQue
)) {
148 Entry
= Tcb
->SndQue
.BackLink
;
149 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
151 ASSERT (TCP_SEQ_GEQ (TCPSEG_NETBUF (Nbuf
)->End
, Tcb
->SndNxt
));
152 return TCPSEG_NETBUF (Nbuf
)->End
;
156 Compute how much data to send.
158 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
159 @param[in] Force If TRUE, to ignore the sender's SWS avoidance algorithm and send
162 @return The length of the data can be sent. If 0, no data can be sent.
181 // TCP should NOT send data beyond the send window
182 // and congestion window. The right edge of send
183 // window is defined as SND.WL2 + SND.WND. The right
184 // edge of congestion window is defined as SND.UNA +
188 Limit
= Tcb
->SndWl2
+ Tcb
->SndWnd
;
190 if (TCP_SEQ_GT (Limit
, Tcb
->SndUna
+ Tcb
->CWnd
)) {
191 Limit
= Tcb
->SndUna
+ Tcb
->CWnd
;
194 if (TCP_SEQ_GT (Limit
, Tcb
->SndNxt
)) {
195 Win
= TCP_SUB_SEQ (Limit
, Tcb
->SndNxt
);
199 // The data to send contains two parts: the data on the
200 // socket send queue, and the data on the TCB's send
201 // buffer. The later can be non-zero if the peer shrinks
202 // its advertised window.
204 Left
= GET_SND_DATASIZE (Sk
) + TCP_SUB_SEQ (TcpGetMaxSndNxt (Tcb
), Tcb
->SndNxt
);
206 Len
= MIN (Win
, Left
);
208 if (Len
> Tcb
->SndMss
) {
212 if ((Force
!= 0) || ((Len
== 0) && (Left
== 0))) {
216 if ((Len
== 0) && (Left
!= 0)) {
217 goto SetPersistTimer
;
221 // Sender's SWS avoidance: Don't send a small segment unless
222 // a)A full-sized segment can be sent,
223 // b)At least one-half of the maximum sized windows that
224 // the other end has ever advertised.
225 // c)It can send everything it has, and either it isn't
226 // expecting an ACK, or the Nagle algorithm is disabled.
228 if ((Len
== Tcb
->SndMss
) || (2 * Len
>= Tcb
->SndWndMax
)) {
233 ((Tcb
->SndNxt
== Tcb
->SndUna
) || TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
))
240 // RFC1122 suggests to set a timer when SWSA forbids TCP
241 // sending more data, and combines it with a probe timer.
244 if (!TCP_TIMER_ON (Tcb
->EnabledTimer
, TCP_TIMER_REXMIT
)) {
247 "TcpDataToSend: enter persistent state for TCB %p\n",
251 if (!Tcb
->ProbeTimerOn
) {
252 TcpSetProbeTimer (Tcb
);
260 Build the TCP header of the TCP segment and transmit the segment by IP.
262 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
263 @param[in] Nbuf Pointer to the buffer containing the segment to be
266 @retval 0 The segment was sent out successfully.
267 @retval -1 An error condition occurred.
282 ASSERT ((Nbuf
!= NULL
) && (Nbuf
->Tcp
== NULL
));
284 if (TcpVerifySegment (Nbuf
) == 0) {
288 DataLen
= Nbuf
->TotalSize
;
290 Seg
= TCPSEG_NETBUF (Nbuf
);
291 Syn
= TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
);
294 Len
= TcpSynBuildOption (Tcb
, Nbuf
);
296 Len
= TcpBuildOption (Tcb
, Nbuf
);
299 ASSERT ((Len
% 4 == 0) && (Len
<= 40));
301 Len
+= sizeof (TCP_HEAD
);
303 Head
= (TCP_HEAD
*)NetbufAllocSpace (
309 ASSERT (Head
!= NULL
);
313 Head
->SrcPort
= Tcb
->LocalEnd
.Port
;
314 Head
->DstPort
= Tcb
->RemoteEnd
.Port
;
315 Head
->Seq
= NTOHL (Seg
->Seq
);
316 Head
->Ack
= NTOHL (Tcb
->RcvNxt
);
317 Head
->HeadLen
= (UINT8
)(Len
>> 2);
319 Head
->Wnd
= TcpComputeWnd (Tcb
, Syn
);
323 // Check whether to set the PSH flag.
325 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_PSH
);
328 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
) &&
329 TCP_SEQ_BETWEEN (Seg
->Seq
, Tcb
->SndPsh
, Seg
->End
)
332 TCP_SET_FLG (Seg
->Flag
, TCP_FLG_PSH
);
333 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
);
334 } else if ((Seg
->End
== Tcb
->SndNxt
) && (GET_SND_DATASIZE (Tcb
->Sk
) == 0)) {
335 TCP_SET_FLG (Seg
->Flag
, TCP_FLG_PSH
);
340 // Check whether to set the URG flag and the urgent pointer.
342 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_URG
);
344 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
) && TCP_SEQ_LEQ (Seg
->Seq
, Tcb
->SndUp
)) {
345 TCP_SET_FLG (Seg
->Flag
, TCP_FLG_URG
);
347 if (TCP_SEQ_LT (Tcb
->SndUp
, Seg
->End
)) {
348 Seg
->Urg
= (UINT16
)TCP_SUB_SEQ (Tcb
->SndUp
, Seg
->Seq
);
350 Seg
->Urg
= (UINT16
)MIN (
360 Head
->Flag
= Seg
->Flag
;
361 Head
->Urg
= NTOHS (Seg
->Urg
);
362 Head
->Checksum
= TcpChecksum (Nbuf
, Tcb
->HeadSum
);
365 // Update the TCP session's control information.
367 Tcb
->RcvWl2
= Tcb
->RcvNxt
;
369 Tcb
->RcvWnd
= NTOHS (Head
->Wnd
);
373 // Clear the delayedack flag.
377 return TcpSendIpPacket (Tcb
, Nbuf
, &Tcb
->LocalEnd
.Ip
, &Tcb
->RemoteEnd
.Ip
, Tcb
->Sk
->IpVersion
);
381 Get a segment from the Tcb's SndQue.
383 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
384 @param[in] Seq The sequence number of the segment.
385 @param[in] Len The maximum length of the segment.
387 @return Pointer to the segment. If NULL, some error occurred.
391 TcpGetSegmentSndQue (
408 ASSERT ((Tcb
!= NULL
) && TCP_SEQ_LEQ (Seq
, Tcb
->SndNxt
) && (Len
> 0));
411 // Find the segment that contains the Seq.
418 NET_LIST_FOR_EACH (Cur
, Head
) {
419 Node
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
420 Seg
= TCPSEG_NETBUF (Node
);
422 if (TCP_SEQ_LT (Seq
, Seg
->End
) && TCP_SEQ_LEQ (Seg
->Seq
, Seq
)) {
427 if ((Cur
== Head
) || (Seg
== NULL
) || (Node
== NULL
)) {
432 // Return the buffer if it can be returned without
435 if ((Seg
->Seq
== Seq
) &&
436 TCP_SEQ_LEQ (Seg
->End
, Seg
->Seq
+ Len
) &&
437 !NET_BUF_SHARED (Node
)
445 // Create a new buffer and copy data there.
447 Nbuf
= NetbufAlloc (Len
+ TCP_MAX_HEAD
);
453 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
458 if (TCP_SEQ_LT (Seq
+ Len
, Seg
->End
)) {
462 CopyLen
= TCP_SUB_SEQ (End
, Seq
);
463 Offset
= TCP_SUB_SEQ (Seq
, Seg
->Seq
);
466 // If SYN is set and out of the range, clear the flag.
467 // Because the sequence of the first byte is SEG.SEQ+1,
468 // adjust Offset by -1. If SYN is in the range, copy
471 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
472 if (TCP_SEQ_LT (Seg
->Seq
, Seq
)) {
473 TCP_CLEAR_FLG (Flag
, TCP_FLG_SYN
);
481 // If FIN is set and in the range, copy one byte less,
482 // and if it is out of the range, clear the flag.
484 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
485 if (Seg
->End
== End
) {
488 TCP_CLEAR_FLG (Flag
, TCP_FLG_FIN
);
492 ASSERT (CopyLen
>= 0);
495 // Copy data to the segment
498 Data
= NetbufAllocSpace (Nbuf
, CopyLen
, NET_BUF_TAIL
);
499 ASSERT (Data
!= NULL
);
501 if ((INT32
)NetbufCopy (Node
, Offset
, CopyLen
, Data
) != CopyLen
) {
506 CopyMem (TCPSEG_NETBUF (Nbuf
), Seg
, sizeof (TCP_SEG
));
508 TCPSEG_NETBUF (Nbuf
)->Seq
= Seq
;
509 TCPSEG_NETBUF (Nbuf
)->End
= End
;
510 TCPSEG_NETBUF (Nbuf
)->Flag
= Flag
;
520 Get a segment from the Tcb's socket buffer.
522 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
523 @param[in] Seq The sequence number of the segment.
524 @param[in] Len The maximum length of the segment.
526 @return Pointer to the segment. If NULL, some error occurred.
540 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
));
542 Nbuf
= NetbufAlloc (Len
+ TCP_MAX_HEAD
);
547 "TcpGetSegmentSock: failed to allocate a netbuf for TCB %p\n",
554 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
560 // copy data to the segment.
562 Data
= NetbufAllocSpace (Nbuf
, Len
, NET_BUF_TAIL
);
563 ASSERT (Data
!= NULL
);
565 DataGet
= SockGetDataToSend (Tcb
->Sk
, 0, Len
, Data
);
570 TCPSEG_NETBUF (Nbuf
)->Seq
= Seq
;
571 TCPSEG_NETBUF (Nbuf
)->End
= Seq
+ Len
;
573 InsertTailList (&(Tcb
->SndQue
), &(Nbuf
->List
));
576 SockDataSent (Tcb
->Sk
, DataGet
);
583 Get a segment starting from sequence Seq of a maximum
586 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
587 @param[in] Seq The sequence number of the segment.
588 @param[in] Len The maximum length of the segment.
590 @return Pointer to the segment. If NULL, some error occurred.
602 ASSERT (Tcb
!= NULL
);
605 // Compare the SndNxt with the max sequence number sent.
607 if ((Len
!= 0) && TCP_SEQ_LT (Seq
, TcpGetMaxSndNxt (Tcb
))) {
608 Nbuf
= TcpGetSegmentSndQue (Tcb
, Seq
, Len
);
610 Nbuf
= TcpGetSegmentSock (Tcb
, Seq
, Len
);
613 if (TcpVerifySegment (Nbuf
) == 0) {
622 Retransmit the segment from sequence Seq.
624 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
625 @param[in] Seq The sequence number of the segment to be retransmitted.
627 @retval 0 Retransmission succeeded.
628 @retval -1 Error condition occurred.
641 // Compute the maximum length of retransmission. It is
642 // limited by three factors:
643 // 1. Less than SndMss
644 // 2. Must in the current send window
645 // 3. Will not change the boundaries of queued segments.
649 // Handle the Window Retraction if TCP window scale is enabled according to RFC7323:
650 // On first retransmission, or if the sequence number is out of
651 // window by less than 2^Rcv.Wind.Shift, then do normal
652 // retransmission(s) without regard to the receiver window as long
653 // as the original segment was in window when it was sent.
655 if ((Tcb
->SndWndScale
!= 0) &&
656 (TCP_SEQ_GT (Seq
, Tcb
->RetxmitSeqMax
) || TCP_SEQ_BETWEEN (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
, Tcb
->SndWl2
+ Tcb
->SndWnd
+ (1 << Tcb
->SndWndScale
))))
658 Len
= TCP_SUB_SEQ (Tcb
->SndNxt
, Seq
);
661 "TcpRetransmit: retransmission without regard to the receiver window for TCB %p\n",
664 } else if (TCP_SEQ_GEQ (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
)) {
665 Len
= TCP_SUB_SEQ (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
);
669 "TcpRetransmit: retransmission cancelled because send window too small for TCB %p\n",
676 Len
= MIN (Len
, Tcb
->SndMss
);
678 Nbuf
= TcpGetSegmentSndQue (Tcb
, Seq
, Len
);
683 if (TcpVerifySegment (Nbuf
) == 0) {
687 if (TcpTransmitSegment (Tcb
, Nbuf
) != 0) {
691 if (TCP_SEQ_GT (Seq
, Tcb
->RetxmitSeqMax
)) {
692 Tcb
->RetxmitSeqMax
= Seq
;
696 // The retransmitted buffer may be on the SndQue,
697 // trim TCP head because all the buffers on SndQue
700 ASSERT (Nbuf
->Tcp
!= NULL
);
701 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
716 Verify that all the segments in SndQue are in good shape.
718 @param[in] Head Pointer to the head node of the SndQue.
720 @retval 0 At least one segment is broken.
721 @retval 1 All segments in the specific queue are in good shape.
733 if (IsListEmpty (Head
)) {
738 // Initialize the Seq.
740 Entry
= Head
->ForwardLink
;
741 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
742 Seq
= TCPSEG_NETBUF (Nbuf
)->Seq
;
744 NET_LIST_FOR_EACH (Entry
, Head
) {
745 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
747 if (TcpVerifySegment (Nbuf
) == 0) {
752 // All the node in the SndQue should has:
753 // SEG.SEQ = LAST_SEG.END
755 if (Seq
!= TCPSEG_NETBUF (Nbuf
)->Seq
) {
759 Seq
= TCPSEG_NETBUF (Nbuf
)->End
;
766 Check whether to send data/SYN/FIN and piggyback an ACK.
768 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
769 @param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm
770 and send out data by force.
772 @return The number of bytes sent.
789 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
) && (Tcb
->State
!= TCP_LISTEN
));
793 if ((Tcb
->State
== TCP_CLOSED
) || TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
)) {
799 // Compute how much data can be sent
801 Len
= TcpDataToSend (Tcb
, Force
);
804 ASSERT ((Tcb
->State
) < (ARRAY_SIZE (mTcpOutFlag
)));
805 Flag
= mTcpOutFlag
[Tcb
->State
];
807 if ((Flag
& TCP_FLG_SYN
) != 0) {
813 // Only send a segment without data if SYN or
816 if ((Len
== 0) && ((Flag
& (TCP_FLG_SYN
| TCP_FLG_FIN
)) == 0)) {
820 Nbuf
= TcpGetSegment (Tcb
, Seq
, Len
);
825 "TcpToSendData: failed to get a segment for TCB %p\n",
832 Seg
= TCPSEG_NETBUF (Nbuf
);
835 // Set the TcpSeg in Nbuf.
837 Len
= Nbuf
->TotalSize
;
839 if (TCP_FLG_ON (Flag
, TCP_FLG_SYN
)) {
843 if ((Flag
& TCP_FLG_FIN
) != 0) {
845 // Send FIN if all data is sent, and FIN is
848 if ((TcpGetMaxSndNxt (Tcb
) == Tcb
->SndNxt
) &&
849 (GET_SND_DATASIZE (Tcb
->Sk
) == 0) &&
850 TCP_SEQ_LT (End
+ 1, Tcb
->SndWnd
+ Tcb
->SndWl2
)
855 "TcpToSendData: send FIN to peer for TCB %p in state %s\n",
857 mTcpStateName
[Tcb
->State
])
862 TCP_CLEAR_FLG (Flag
, TCP_FLG_FIN
);
870 if ((TcpVerifySegment (Nbuf
) == 0) || (TcpCheckSndQue (&Tcb
->SndQue
) == 0)) {
873 "TcpToSendData: discard a broken segment for TCB %p\n",
880 // Don't send an empty segment here.
882 if (Seg
->End
== Seg
->Seq
) {
885 "TcpToSendData: created a empty segment for TCB %p, free it now\n",
892 if (TcpTransmitSegment (Tcb
, Nbuf
) != 0) {
893 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
896 if ((Flag
& TCP_FLG_FIN
) != 0) {
897 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
);
903 Sent
+= TCP_SUB_SEQ (End
, Seq
);
906 // All the buffers in the SndQue are headless.
908 ASSERT (Nbuf
->Tcp
!= NULL
);
910 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
916 // Update the status in TCB.
920 if ((Flag
& TCP_FLG_FIN
) != 0) {
921 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
);
924 if (TCP_SEQ_GT (End
, Tcb
->SndNxt
)) {
928 if (!TCP_TIMER_ON (Tcb
->EnabledTimer
, TCP_TIMER_REXMIT
)) {
929 TcpSetTimer (Tcb
, TCP_TIMER_REXMIT
, Tcb
->Rto
);
933 // Enable RTT measurement only if not in retransmit.
934 // Karn's algorithm requires not to update RTT when in loss.
936 if ((Tcb
->CongestState
== TCP_CONGEST_OPEN
) && !TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
)) {
939 "TcpToSendData: set RTT measure sequence %d for TCB %p\n",
944 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
948 } while (Len
== Tcb
->SndMss
);
961 Send an ACK immediately.
963 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
974 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
980 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
982 Seg
= TCPSEG_NETBUF (Nbuf
);
983 Seg
->Seq
= Tcb
->SndNxt
;
984 Seg
->End
= Tcb
->SndNxt
;
985 Seg
->Flag
= TCP_FLG_ACK
;
987 if (TcpTransmitSegment (Tcb
, Nbuf
) == 0) {
988 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
996 Send a zero probe segment. It can be used by keepalive and zero window probe.
998 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
1000 @retval 0 The zero probe segment was sent out successfully.
1001 @retval other An error condition occurred.
1013 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
1019 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
1022 // SndNxt-1 is out of window. The peer should respond
1025 Seg
= TCPSEG_NETBUF (Nbuf
);
1026 Seg
->Seq
= Tcb
->SndNxt
- 1;
1027 Seg
->End
= Tcb
->SndNxt
- 1;
1028 Seg
->Flag
= TCP_FLG_ACK
;
1030 Result
= TcpTransmitSegment (Tcb
, Nbuf
);
1037 Check whether to send an ACK or delayed ACK.
1039 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
1050 // Generally, TCP should send a delayed ACK unless:
1051 // 1. ACK at least every other FULL sized segment received.
1052 // 2. Packets received out of order.
1053 // 3. Receiving window is open.
1055 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
) || (Tcb
->DelayedAck
>= 1)) {
1060 TcpNow
= TcpRcvWinNow (Tcb
);
1062 if (TcpNow
> TcpRcvWinOld (Tcb
)) {
1069 "TcpToSendAck: scheduled a delayed ACK for TCB %p\n",
1074 // Schedule a delayed ACK.
1080 Send a RESET segment in response to the segment received.
1082 @param[in] Tcb Pointer to the TCP_CB of this TCP instance. May be NULL.
1083 @param[in] Head TCP header of the segment that triggers the reset.
1084 @param[in] Len Length of the segment that triggers the reset.
1085 @param[in] Local Local IP address.
1086 @param[in] Remote Remote peer's IP address.
1087 @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack,
1088 IP_VERSION_6 indicates TCP is running on IP6 stack.
1090 @retval 0 A reset was sent or there is no need to send it.
1091 @retval -1 No reset is sent.
1099 IN EFI_IP_ADDRESS
*Local
,
1100 IN EFI_IP_ADDRESS
*Remote
,
1109 // Don't respond to a Reset with reset.
1111 if ((Head
->Flag
& TCP_FLG_RST
) != 0) {
1115 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
1121 Nhead
= (TCP_HEAD
*)NetbufAllocSpace (
1127 ASSERT (Nhead
!= NULL
);
1130 Nhead
->Flag
= TCP_FLG_RST
;
1133 // Derive Seq/ACK from the segment if no TCB
1134 // is associated with it, otherwise derive from the Tcb.
1137 if (TCP_FLG_ON (Head
->Flag
, TCP_FLG_ACK
)) {
1138 Nhead
->Seq
= Head
->Ack
;
1142 TCP_SET_FLG (Nhead
->Flag
, TCP_FLG_ACK
);
1143 Nhead
->Ack
= HTONL (NTOHL (Head
->Seq
) + Len
);
1146 Nhead
->Seq
= HTONL (Tcb
->SndNxt
);
1147 Nhead
->Ack
= HTONL (Tcb
->RcvNxt
);
1148 TCP_SET_FLG (Nhead
->Flag
, TCP_FLG_ACK
);
1151 Nhead
->SrcPort
= Head
->DstPort
;
1152 Nhead
->DstPort
= Head
->SrcPort
;
1153 Nhead
->HeadLen
= (UINT8
)(sizeof (TCP_HEAD
) >> 2);
1155 Nhead
->Wnd
= HTONS (0xFFFF);
1156 Nhead
->Checksum
= 0;
1159 if (Version
== IP_VERSION_4
) {
1160 HeadSum
= NetPseudoHeadChecksum (Local
->Addr
[0], Remote
->Addr
[0], 6, 0);
1162 HeadSum
= NetIp6PseudoHeadChecksum (&Local
->v6
, &Remote
->v6
, 6, 0);
1165 Nhead
->Checksum
= TcpChecksum (Nbuf
, HeadSum
);
1167 TcpSendIpPacket (Tcb
, Nbuf
, Local
, Remote
, Version
);
1175 Verify that the segment is in good shape.
1177 @param[in] Nbuf The buffer that contains the segment to be checked.
1179 @retval 0 The segment is broken.
1180 @retval 1 The segment is in good shape.
1196 NET_CHECK_SIGNATURE (Nbuf
, NET_BUF_SIGNATURE
);
1198 Seg
= TCPSEG_NETBUF (Nbuf
);
1199 Len
= Nbuf
->TotalSize
;
1203 if (Head
->Flag
!= Seg
->Flag
) {
1207 Len
-= (Head
->HeadLen
<< 2);
1210 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
1214 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
1218 if (Seg
->Seq
+ Len
!= Seg
->End
) {