]>
git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/TcpDxe/TcpOutput.c
2 TCP output process routines.
4 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 UINT8 mTcpOutFlag
[] = {
21 TCP_FLG_SYN
, // TCP_SYN_SENT
22 TCP_FLG_SYN
| TCP_FLG_ACK
, // TCP_SYN_RCVD
23 TCP_FLG_ACK
, // TCP_ESTABLISHED
24 TCP_FLG_FIN
| TCP_FLG_ACK
, // TCP_FIN_WAIT_1
25 TCP_FLG_ACK
, // TCP_FIN_WAIT_2
26 TCP_FLG_ACK
| TCP_FLG_FIN
, // TCP_CLOSING
27 TCP_FLG_ACK
, // TCP_TIME_WAIT
28 TCP_FLG_ACK
, // TCP_CLOSE_WAIT
29 TCP_FLG_FIN
| TCP_FLG_ACK
// TCP_LAST_ACK
33 Compute the sequence space left in the old receive window.
35 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
37 @return The sequence space left in the old receive window.
49 if (TCP_SEQ_GT (Tcb
->RcvWl2
+ Tcb
->RcvWnd
, Tcb
->RcvNxt
)) {
51 OldWin
= TCP_SUB_SEQ (
52 Tcb
->RcvWl2
+ Tcb
->RcvWnd
,
61 Compute the current receive window.
63 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
65 @return The size of the current receive window, in bytes.
81 OldWin
= TcpRcvWinOld (Tcb
);
83 Win
= SockGetFreeSpace (Sk
, SOCK_RCV_BUF
);
87 Increase
= Win
- OldWin
;
91 // Receiver's SWS: don't advertise a bigger window
92 // unless it can be increased by at least one Mss or
93 // half of the receive buffer.
95 if ((Increase
> Tcb
->SndMss
) || (2 * Increase
>= GET_RCV_BUFFSIZE (Sk
))) {
104 Compute the value to fill in the window size field of the outgoing segment.
106 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
107 @param[in] Syn The flag to indicate whether the outgoing segment
110 @return The value of the local receive window size used to fill the outgoing segment.
122 // RFC requires that initial window not be scaled
126 Wnd
= GET_RCV_BUFFSIZE (Tcb
->Sk
);
129 Wnd
= TcpRcvWinNow (Tcb
);
134 Wnd
= MIN (Wnd
>> Tcb
->RcvWndScale
, 0xffff);
135 return NTOHS ((UINT16
) Wnd
);
139 Get the maximum SndNxt.
141 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
143 @return The sequence number of the maximum SndNxt.
154 if (IsListEmpty (&Tcb
->SndQue
)) {
158 Entry
= Tcb
->SndQue
.BackLink
;
159 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
161 ASSERT (TCP_SEQ_GEQ (TCPSEG_NETBUF (Nbuf
)->End
, Tcb
->SndNxt
));
162 return TCPSEG_NETBUF (Nbuf
)->End
;
166 Compute how much data to send.
168 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
169 @param[in] Force If TRUE, to ignore the sender's SWS avoidance algorithm and send
172 @return The length of the data can be sent. If 0, no data can be sent.
191 // TCP should NOT send data beyond the send window
192 // and congestion window. The right edge of send
193 // window is defined as SND.WL2 + SND.WND. The right
194 // edge of congestion window is defined as SND.UNA +
198 Limit
= Tcb
->SndWl2
+ Tcb
->SndWnd
;
200 if (TCP_SEQ_GT (Limit
, Tcb
->SndUna
+ Tcb
->CWnd
)) {
202 Limit
= Tcb
->SndUna
+ Tcb
->CWnd
;
205 if (TCP_SEQ_GT (Limit
, Tcb
->SndNxt
)) {
206 Win
= TCP_SUB_SEQ (Limit
, Tcb
->SndNxt
);
210 // The data to send contains two parts: the data on the
211 // socket send queue, and the data on the TCB's send
212 // buffer. The later can be non-zero if the peer shrinks
213 // its advertised window.
215 Left
= GET_SND_DATASIZE (Sk
) + TCP_SUB_SEQ (TcpGetMaxSndNxt (Tcb
), Tcb
->SndNxt
);
217 Len
= MIN (Win
, Left
);
219 if (Len
> Tcb
->SndMss
) {
223 if ((Force
!= 0)|| (Len
== 0 && Left
== 0)) {
227 if (Len
== 0 && Left
!= 0) {
228 goto SetPersistTimer
;
232 // Sender's SWS avoidance: Don't send a small segment unless
233 // a)A full-sized segment can be sent,
234 // b)At least one-half of the maximum sized windows that
235 // the other end has ever advertised.
236 // c)It can send everything it has, and either it isn't
237 // expecting an ACK, or the Nagle algorithm is disabled.
239 if ((Len
== Tcb
->SndMss
) || (2 * Len
>= Tcb
->SndWndMax
)) {
245 ((Tcb
->SndNxt
== Tcb
->SndUna
) || TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
))
252 // RFC1122 suggests to set a timer when SWSA forbids TCP
253 // sending more data, and combines it with a probe timer.
256 if (!TCP_TIMER_ON (Tcb
->EnabledTimer
, TCP_TIMER_REXMIT
)) {
260 "TcpDataToSend: enter persistent state for TCB %p\n",
264 if (!Tcb
->ProbeTimerOn
) {
265 TcpSetProbeTimer (Tcb
);
273 Build the TCP header of the TCP segment and transmit the segment by IP.
275 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
276 @param[in] Nbuf Pointer to the buffer containing the segment to be
279 @retval 0 The segment was sent out successfully.
280 @retval -1 An error condition occurred.
295 ASSERT ((Nbuf
!= NULL
) && (Nbuf
->Tcp
== NULL
) && (TcpVerifySegment (Nbuf
) != 0));
297 DataLen
= Nbuf
->TotalSize
;
299 Seg
= TCPSEG_NETBUF (Nbuf
);
300 Syn
= TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
);
304 Len
= TcpSynBuildOption (Tcb
, Nbuf
);
307 Len
= TcpBuildOption (Tcb
, Nbuf
);
310 ASSERT ((Len
% 4 == 0) && (Len
<= 40));
312 Len
+= sizeof (TCP_HEAD
);
314 Head
= (TCP_HEAD
*) NetbufAllocSpace (
320 ASSERT (Head
!= NULL
);
324 Head
->SrcPort
= Tcb
->LocalEnd
.Port
;
325 Head
->DstPort
= Tcb
->RemoteEnd
.Port
;
326 Head
->Seq
= NTOHL (Seg
->Seq
);
327 Head
->Ack
= NTOHL (Tcb
->RcvNxt
);
328 Head
->HeadLen
= (UINT8
) (Len
>> 2);
330 Head
->Wnd
= TcpComputeWnd (Tcb
, Syn
);
334 // Check whether to set the PSH flag.
336 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_PSH
);
339 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
) &&
340 TCP_SEQ_BETWEEN (Seg
->Seq
, Tcb
->SndPsh
, Seg
->End
)
343 TCP_SET_FLG (Seg
->Flag
, TCP_FLG_PSH
);
344 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
);
346 } else if ((Seg
->End
== Tcb
->SndNxt
) && (GET_SND_DATASIZE (Tcb
->Sk
) == 0)) {
348 TCP_SET_FLG (Seg
->Flag
, TCP_FLG_PSH
);
353 // Check whether to set the URG flag and the urgent pointer.
355 TCP_CLEAR_FLG (Seg
->Flag
, TCP_FLG_URG
);
357 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
) && TCP_SEQ_LEQ (Seg
->Seq
, Tcb
->SndUp
)) {
359 TCP_SET_FLG (Seg
->Flag
, TCP_FLG_URG
);
361 if (TCP_SEQ_LT (Tcb
->SndUp
, Seg
->End
)) {
363 Seg
->Urg
= (UINT16
) TCP_SUB_SEQ (Tcb
->SndUp
, Seg
->Seq
);
366 Seg
->Urg
= (UINT16
) MIN (
367 TCP_SUB_SEQ (Tcb
->SndUp
,
374 Head
->Flag
= Seg
->Flag
;
375 Head
->Urg
= NTOHS (Seg
->Urg
);
376 Head
->Checksum
= TcpChecksum (Nbuf
, Tcb
->HeadSum
);
379 // Update the TCP session's control information.
381 Tcb
->RcvWl2
= Tcb
->RcvNxt
;
383 Tcb
->RcvWnd
= NTOHS (Head
->Wnd
);
387 // Clear the delayedack flag.
391 return TcpSendIpPacket (Tcb
, Nbuf
, &Tcb
->LocalEnd
.Ip
, &Tcb
->RemoteEnd
.Ip
, Tcb
->Sk
->IpVersion
);
395 Get a segment from the Tcb's SndQue.
397 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
398 @param[in] Seq The sequence number of the segment.
399 @param[in] Len The maximum length of the segment.
401 @return Pointer to the segment. If NULL, some error occurred.
405 TcpGetSegmentSndQue (
422 ASSERT ((Tcb
!= NULL
) && TCP_SEQ_LEQ (Seq
, Tcb
->SndNxt
) && (Len
> 0));
425 // Find the segment that contains the Seq.
432 NET_LIST_FOR_EACH (Cur
, Head
) {
433 Node
= NET_LIST_USER_STRUCT (Cur
, NET_BUF
, List
);
434 Seg
= TCPSEG_NETBUF (Node
);
436 if (TCP_SEQ_LT (Seq
, Seg
->End
) && TCP_SEQ_LEQ (Seg
->Seq
, Seq
)) {
442 if ((Cur
== Head
) || (Seg
== NULL
) || (Node
== NULL
)) {
447 // Return the buffer if it can be returned without
450 if ((Seg
->Seq
== Seq
) &&
451 TCP_SEQ_LEQ (Seg
->End
, Seg
->Seq
+ Len
) &&
452 !NET_BUF_SHARED (Node
)
460 // Create a new buffer and copy data there.
462 Nbuf
= NetbufAlloc (Len
+ TCP_MAX_HEAD
);
468 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
473 if (TCP_SEQ_LT (Seq
+ Len
, Seg
->End
)) {
477 CopyLen
= TCP_SUB_SEQ (End
, Seq
);
478 Offset
= TCP_SUB_SEQ (Seq
, Seg
->Seq
);
481 // If SYN is set and out of the range, clear the flag.
482 // Becuase the sequence of the first byte is SEG.SEQ+1,
483 // adjust Offset by -1. If SYN is in the range, copy
486 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
488 if (TCP_SEQ_LT (Seg
->Seq
, Seq
)) {
490 TCP_CLEAR_FLG (Flag
, TCP_FLG_SYN
);
499 // If FIN is set and in the range, copy one byte less,
500 // and if it is out of the range, clear the flag.
502 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
504 if (Seg
->End
== End
) {
509 TCP_CLEAR_FLG (Flag
, TCP_FLG_FIN
);
513 ASSERT (CopyLen
>= 0);
516 // Copy data to the segment
519 Data
= NetbufAllocSpace (Nbuf
, CopyLen
, NET_BUF_TAIL
);
520 ASSERT (Data
!= NULL
);
522 if ((INT32
) NetbufCopy (Node
, Offset
, CopyLen
, Data
) != CopyLen
) {
527 CopyMem (TCPSEG_NETBUF (Nbuf
), Seg
, sizeof (TCP_SEG
));
529 TCPSEG_NETBUF (Nbuf
)->Seq
= Seq
;
530 TCPSEG_NETBUF (Nbuf
)->End
= End
;
531 TCPSEG_NETBUF (Nbuf
)->Flag
= Flag
;
541 Get a segment from the Tcb's socket buffer.
543 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
544 @param[in] Seq The sequence number of the segment.
545 @param[in] Len The maximum length of the segment.
547 @return Pointer to the segment. If NULL, some error occurred.
561 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
));
563 Nbuf
= NetbufAlloc (Len
+ TCP_MAX_HEAD
);
568 "TcpGetSegmentSock: failed to allocate a netbuf for TCB %p\n",
575 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
581 // copy data to the segment.
583 Data
= NetbufAllocSpace (Nbuf
, Len
, NET_BUF_TAIL
);
584 ASSERT (Data
!= NULL
);
586 DataGet
= SockGetDataToSend (Tcb
->Sk
, 0, Len
, Data
);
591 TCPSEG_NETBUF (Nbuf
)->Seq
= Seq
;
592 TCPSEG_NETBUF (Nbuf
)->End
= Seq
+ Len
;
594 InsertTailList (&(Tcb
->SndQue
), &(Nbuf
->List
));
598 SockDataSent (Tcb
->Sk
, DataGet
);
605 Get a segment starting from sequence Seq of a maximum
608 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
609 @param[in] Seq The sequence number of the segment.
610 @param[in] Len The maximum length of the segment.
612 @return Pointer to the segment. If NULL, some error occurred.
624 ASSERT (Tcb
!= NULL
);
627 // Compare the SndNxt with the max sequence number sent.
629 if ((Len
!= 0) && TCP_SEQ_LT (Seq
, TcpGetMaxSndNxt (Tcb
))) {
631 Nbuf
= TcpGetSegmentSndQue (Tcb
, Seq
, Len
);
634 Nbuf
= TcpGetSegmentSock (Tcb
, Seq
, Len
);
637 ASSERT (TcpVerifySegment (Nbuf
) != 0);
642 Retransmit the segment from sequence Seq.
644 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
645 @param[in] Seq The sequence number of the segment to be retransmitted.
647 @retval 0 Retransmission succeeded.
648 @retval -1 Error condition occurred.
661 // Compute the maxium length of retransmission. It is
662 // limited by three factors:
663 // 1. Less than SndMss
664 // 2. Must in the current send window
665 // 3. Will not change the boundaries of queued segments.
669 // Handle the Window Retraction if TCP window scale is enabled according to RFC7323:
670 // On first retransmission, or if the sequence number is out of
671 // window by less than 2^Rcv.Wind.Shift, then do normal
672 // retransmission(s) without regard to the receiver window as long
673 // as the original segment was in window when it was sent.
675 if ((Tcb
->SndWndScale
!= 0) &&
676 (TCP_SEQ_GT (Seq
, Tcb
->RetxmitSeqMax
) || TCP_SEQ_BETWEEN (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
, Tcb
->SndWl2
+ Tcb
->SndWnd
+ (1 << Tcb
->SndWndScale
)))) {
677 Len
= TCP_SUB_SEQ (Tcb
->SndNxt
, Seq
);
680 "TcpRetransmit: retransmission without regard to the receiver window for TCB %p\n",
684 } else if (TCP_SEQ_GEQ (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
)) {
685 Len
= TCP_SUB_SEQ (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
);
690 "TcpRetransmit: retransmission cancelled because send window too small for TCB %p\n",
697 Len
= MIN (Len
, Tcb
->SndMss
);
699 Nbuf
= TcpGetSegmentSndQue (Tcb
, Seq
, Len
);
704 ASSERT (TcpVerifySegment (Nbuf
) != 0);
706 if (TcpTransmitSegment (Tcb
, Nbuf
) != 0) {
710 if (TCP_SEQ_GT (Seq
, Tcb
->RetxmitSeqMax
)) {
711 Tcb
->RetxmitSeqMax
= Seq
;
715 // The retransmitted buffer may be on the SndQue,
716 // trim TCP head because all the buffers on SndQue
719 ASSERT (Nbuf
->Tcp
!= NULL
);
720 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
735 Verify that all the segments in SndQue are in good shape.
737 @param[in] Head Pointer to the head node of the SndQue.
739 @retval 0 At least one segment is broken.
740 @retval 1 All segments in the specific queue are in good shape.
752 if (IsListEmpty (Head
)) {
756 // Initialize the Seq.
758 Entry
= Head
->ForwardLink
;
759 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
760 Seq
= TCPSEG_NETBUF (Nbuf
)->Seq
;
762 NET_LIST_FOR_EACH (Entry
, Head
) {
763 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
765 if (TcpVerifySegment (Nbuf
) == 0) {
770 // All the node in the SndQue should has:
771 // SEG.SEQ = LAST_SEG.END
773 if (Seq
!= TCPSEG_NETBUF (Nbuf
)->Seq
) {
777 Seq
= TCPSEG_NETBUF (Nbuf
)->End
;
784 Check whether to send data/SYN/FIN and piggyback an ACK.
786 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
787 @param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm
788 and send out data by force.
790 @return The number of bytes sent.
807 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
) && (Tcb
->State
!= TCP_LISTEN
));
811 if ((Tcb
->State
== TCP_CLOSED
) || TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
)) {
818 // Compute how much data can be sent
820 Len
= TcpDataToSend (Tcb
, Force
);
823 ASSERT ((Tcb
->State
) < (ARRAY_SIZE (mTcpOutFlag
)));
824 Flag
= mTcpOutFlag
[Tcb
->State
];
826 if ((Flag
& TCP_FLG_SYN
) != 0) {
833 // Only send a segment without data if SYN or
836 if ((Len
== 0) && ((Flag
& (TCP_FLG_SYN
| TCP_FLG_FIN
)) == 0)) {
840 Nbuf
= TcpGetSegment (Tcb
, Seq
, Len
);
845 "TcpToSendData: failed to get a segment for TCB %p\n",
852 Seg
= TCPSEG_NETBUF (Nbuf
);
855 // Set the TcpSeg in Nbuf.
857 Len
= Nbuf
->TotalSize
;
859 if (TCP_FLG_ON (Flag
, TCP_FLG_SYN
)) {
863 if ((Flag
& TCP_FLG_FIN
) != 0) {
865 // Send FIN if all data is sent, and FIN is
868 if ((TcpGetMaxSndNxt (Tcb
) == Tcb
->SndNxt
) &&
869 (GET_SND_DATASIZE (Tcb
->Sk
) == 0) &&
870 TCP_SEQ_LT (End
+ 1, Tcb
->SndWnd
+ Tcb
->SndWl2
)
874 "TcpToSendData: send FIN to peer for TCB %p in state %s\n",
876 mTcpStateName
[Tcb
->State
])
881 TCP_CLEAR_FLG (Flag
, TCP_FLG_FIN
);
889 ASSERT (TcpVerifySegment (Nbuf
) != 0);
890 ASSERT (TcpCheckSndQue (&Tcb
->SndQue
) != 0);
893 // Don't send an empty segment here.
895 if (Seg
->End
== Seg
->Seq
) {
898 "TcpToSendData: created a empty segment for TCB %p, free it now\n",
906 if (TcpTransmitSegment (Tcb
, Nbuf
) != 0) {
907 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
910 if ((Flag
& TCP_FLG_FIN
) != 0) {
911 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
);
917 Sent
+= TCP_SUB_SEQ (End
, Seq
);
920 // All the buffers in the SndQue are headless.
922 ASSERT (Nbuf
->Tcp
!= NULL
);
924 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
930 // Update the status in TCB.
934 if ((Flag
& TCP_FLG_FIN
) != 0) {
935 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
);
938 if (TCP_SEQ_GT (End
, Tcb
->SndNxt
)) {
942 if (!TCP_TIMER_ON (Tcb
->EnabledTimer
, TCP_TIMER_REXMIT
)) {
943 TcpSetTimer (Tcb
, TCP_TIMER_REXMIT
, Tcb
->Rto
);
947 // Enable RTT measurement only if not in retransmit.
948 // Karn's algorithm requires not to update RTT when in loss.
950 if ((Tcb
->CongestState
== TCP_CONGEST_OPEN
) && !TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
)) {
954 "TcpToSendData: set RTT measure sequence %d for TCB %p\n",
959 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
964 } while (Len
== Tcb
->SndMss
);
977 Send an ACK immediately.
979 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
990 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
996 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
998 Seg
= TCPSEG_NETBUF (Nbuf
);
999 Seg
->Seq
= Tcb
->SndNxt
;
1000 Seg
->End
= Tcb
->SndNxt
;
1001 Seg
->Flag
= TCP_FLG_ACK
;
1003 if (TcpTransmitSegment (Tcb
, Nbuf
) == 0) {
1004 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
1005 Tcb
->DelayedAck
= 0;
1012 Send a zero probe segment. It can be used by keepalive and zero window probe.
1014 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
1016 @retval 0 The zero probe segment was sent out successfully.
1017 @retval other An error condition occurred.
1029 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
1035 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
1038 // SndNxt-1 is out of window. The peer should respond
1041 Seg
= TCPSEG_NETBUF (Nbuf
);
1042 Seg
->Seq
= Tcb
->SndNxt
- 1;
1043 Seg
->End
= Tcb
->SndNxt
- 1;
1044 Seg
->Flag
= TCP_FLG_ACK
;
1046 Result
= TcpTransmitSegment (Tcb
, Nbuf
);
1053 Check whether to send an ACK or delayed ACK.
1055 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
1066 // Generally, TCP should send a delayed ACK unless:
1067 // 1. ACK at least every other FULL sized segment received.
1068 // 2. Packets received out of order.
1069 // 3. Receiving window is open.
1071 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
) || (Tcb
->DelayedAck
>= 1)) {
1076 TcpNow
= TcpRcvWinNow (Tcb
);
1078 if (TcpNow
> TcpRcvWinOld (Tcb
)) {
1085 "TcpToSendAck: scheduled a delayed ACK for TCB %p\n",
1090 // Schedule a delayed ACK.
1096 Send a RESET segment in response to the segment received.
1098 @param[in] Tcb Pointer to the TCP_CB of this TCP instance. May be NULL.
1099 @param[in] Head TCP header of the segment that triggers the reset.
1100 @param[in] Len Length of the segment that triggers the reset.
1101 @param[in] Local Local IP address.
1102 @param[in] Remote Remote peer's IP address.
1103 @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack,
1104 IP_VERSION_6 indicates TCP is running on IP6 stack.
1106 @retval 0 A reset was sent or there is no need to send it.
1107 @retval -1 No reset is sent.
1115 IN EFI_IP_ADDRESS
*Local
,
1116 IN EFI_IP_ADDRESS
*Remote
,
1125 // Don't respond to a Reset with reset.
1127 if ((Head
->Flag
& TCP_FLG_RST
) != 0) {
1131 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
1137 Nhead
= (TCP_HEAD
*) NetbufAllocSpace (
1143 ASSERT (Nhead
!= NULL
);
1146 Nhead
->Flag
= TCP_FLG_RST
;
1149 // Derive Seq/ACK from the segment if no TCB
1150 // is associated with it, otherwise derive from the Tcb.
1154 if (TCP_FLG_ON (Head
->Flag
, TCP_FLG_ACK
)) {
1155 Nhead
->Seq
= Head
->Ack
;
1159 TCP_SET_FLG (Nhead
->Flag
, TCP_FLG_ACK
);
1160 Nhead
->Ack
= HTONL (NTOHL (Head
->Seq
) + Len
);
1164 Nhead
->Seq
= HTONL (Tcb
->SndNxt
);
1165 Nhead
->Ack
= HTONL (Tcb
->RcvNxt
);
1166 TCP_SET_FLG (Nhead
->Flag
, TCP_FLG_ACK
);
1169 Nhead
->SrcPort
= Head
->DstPort
;
1170 Nhead
->DstPort
= Head
->SrcPort
;
1171 Nhead
->HeadLen
= (UINT8
) (sizeof (TCP_HEAD
) >> 2);
1173 Nhead
->Wnd
= HTONS (0xFFFF);
1174 Nhead
->Checksum
= 0;
1177 if (Version
== IP_VERSION_4
) {
1178 HeadSum
= NetPseudoHeadChecksum (Local
->Addr
[0], Remote
->Addr
[0], 6, 0);
1180 HeadSum
= NetIp6PseudoHeadChecksum (&Local
->v6
, &Remote
->v6
, 6, 0);
1183 Nhead
->Checksum
= TcpChecksum (Nbuf
, HeadSum
);
1185 TcpSendIpPacket (Tcb
, Nbuf
, Local
, Remote
, Version
);
1193 Verify that the segment is in good shape.
1195 @param[in] Nbuf The buffer that contains the segment to be checked.
1197 @retval 0 The segment is broken.
1198 @retval 1 The segment is in good shape.
1214 NET_CHECK_SIGNATURE (Nbuf
, NET_BUF_SIGNATURE
);
1216 Seg
= TCPSEG_NETBUF (Nbuf
);
1217 Len
= Nbuf
->TotalSize
;
1221 if (Head
->Flag
!= Seg
->Flag
) {
1225 Len
-= (Head
->HeadLen
<< 2);
1228 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
1232 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
1236 if (Seg
->Seq
+ Len
!= Seg
->End
) {