]>
git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/TcpDxe/TcpOutput.c
2 TCP output process routines.
4 Copyright (c) 2009 - 2010, 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.
667 if (TCP_SEQ_LT (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
)) {
670 "TcpRetransmit: retransmission cancelled because send window too small for TCB %p\n",
677 Len
= TCP_SUB_SEQ (Tcb
->SndWl2
+ Tcb
->SndWnd
, Seq
);
678 Len
= MIN (Len
, Tcb
->SndMss
);
680 Nbuf
= TcpGetSegmentSndQue (Tcb
, Seq
, Len
);
685 ASSERT (TcpVerifySegment (Nbuf
) != 0);
687 if (TcpTransmitSegment (Tcb
, Nbuf
) != 0) {
692 // The retransmitted buffer may be on the SndQue,
693 // trim TCP head because all the buffers on SndQue
696 ASSERT (Nbuf
->Tcp
!= NULL
);
697 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
712 Verify that all the segments in SndQue are in good shape.
714 @param[in] Head Pointer to the head node of the SndQue.
716 @retval 0 At least one segment is broken.
717 @retval 1 All segments in the specific queue are in good shape.
729 if (IsListEmpty (Head
)) {
733 // Initialize the Seq.
735 Entry
= Head
->ForwardLink
;
736 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
737 Seq
= TCPSEG_NETBUF (Nbuf
)->Seq
;
739 NET_LIST_FOR_EACH (Entry
, Head
) {
740 Nbuf
= NET_LIST_USER_STRUCT (Entry
, NET_BUF
, List
);
742 if (TcpVerifySegment (Nbuf
) == 0) {
747 // All the node in the SndQue should has:
748 // SEG.SEQ = LAST_SEG.END
750 if (Seq
!= TCPSEG_NETBUF (Nbuf
)->Seq
) {
754 Seq
= TCPSEG_NETBUF (Nbuf
)->End
;
761 Check whether to send data/SYN/FIN and piggyback an ACK.
763 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
764 @param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm
765 and send out data by force.
767 @return The number of bytes sent.
784 ASSERT ((Tcb
!= NULL
) && (Tcb
->Sk
!= NULL
) && (Tcb
->State
!= TCP_LISTEN
));
788 if ((Tcb
->State
== TCP_CLOSED
) || TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
)) {
795 // Compute how much data can be sent
797 Len
= TcpDataToSend (Tcb
, Force
);
800 ASSERT ((Tcb
->State
) < (sizeof (mTcpOutFlag
) / sizeof (mTcpOutFlag
[0])));
801 Flag
= mTcpOutFlag
[Tcb
->State
];
803 if ((Flag
& TCP_FLG_SYN
) != 0) {
810 // Only send a segment without data if SYN or
813 if ((Len
== 0) && ((Flag
& (TCP_FLG_SYN
| TCP_FLG_FIN
)) == 0)) {
817 Nbuf
= TcpGetSegment (Tcb
, Seq
, Len
);
822 "TcpToSendData: failed to get a segment for TCB %p\n",
829 Seg
= TCPSEG_NETBUF (Nbuf
);
832 // Set the TcpSeg in Nbuf.
834 Len
= Nbuf
->TotalSize
;
836 if (TCP_FLG_ON (Flag
, TCP_FLG_SYN
)) {
840 if ((Flag
& TCP_FLG_FIN
) != 0) {
842 // Send FIN if all data is sent, and FIN is
845 if ((TcpGetMaxSndNxt (Tcb
) == Tcb
->SndNxt
) &&
846 (GET_SND_DATASIZE (Tcb
->Sk
) == 0) &&
847 TCP_SEQ_LT (End
+ 1, Tcb
->SndWnd
+ Tcb
->SndWl2
)
851 "TcpToSendData: send FIN to peer for TCB %p in state %s\n",
853 mTcpStateName
[Tcb
->State
])
858 TCP_CLEAR_FLG (Flag
, TCP_FLG_FIN
);
866 ASSERT (TcpVerifySegment (Nbuf
) != 0);
867 ASSERT (TcpCheckSndQue (&Tcb
->SndQue
) != 0);
870 // Don't send an empty segment here.
872 if (Seg
->End
== Seg
->Seq
) {
875 "TcpToSendData: created a empty segment for TCB %p, free it now\n",
883 if (TcpTransmitSegment (Tcb
, Nbuf
) != 0) {
884 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
887 if ((Flag
& TCP_FLG_FIN
) != 0) {
888 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
);
894 Sent
+= TCP_SUB_SEQ (End
, Seq
);
897 // All the buffers in the SndQue are headless.
899 ASSERT (Nbuf
->Tcp
!= NULL
);
901 NetbufTrim (Nbuf
, (Nbuf
->Tcp
->HeadLen
<< 2), NET_BUF_HEAD
);
907 // Update the status in TCB.
911 if ((Flag
& TCP_FLG_FIN
) != 0) {
912 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_FIN_SENT
);
915 if (TCP_SEQ_GT (End
, Tcb
->SndNxt
)) {
919 if (!TCP_TIMER_ON (Tcb
->EnabledTimer
, TCP_TIMER_REXMIT
)) {
920 TcpSetTimer (Tcb
, TCP_TIMER_REXMIT
, Tcb
->Rto
);
924 // Enable RTT measurement only if not in retransmit.
925 // Karn's algorithm requires not to update RTT when in loss.
927 if ((Tcb
->CongestState
== TCP_CONGEST_OPEN
) && !TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
)) {
931 "TcpToSendData: set RTT measure sequence %d for TCB %p\n",
936 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RTT_ON
);
941 } while (Len
== Tcb
->SndMss
);
954 Send an ACK immediately.
956 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
967 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
973 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
975 Seg
= TCPSEG_NETBUF (Nbuf
);
976 Seg
->Seq
= Tcb
->SndNxt
;
977 Seg
->End
= Tcb
->SndNxt
;
978 Seg
->Flag
= TCP_FLG_ACK
;
980 if (TcpTransmitSegment (Tcb
, Nbuf
) == 0) {
981 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
);
989 Send a zero probe segment. It can be used by keepalive and zero window probe.
991 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
993 @retval 0 The zero probe segment was sent out successfully.
994 @retval other An error condition occurred.
1006 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
1012 NetbufReserve (Nbuf
, TCP_MAX_HEAD
);
1015 // SndNxt-1 is out of window. The peer should respond
1018 Seg
= TCPSEG_NETBUF (Nbuf
);
1019 Seg
->Seq
= Tcb
->SndNxt
- 1;
1020 Seg
->End
= Tcb
->SndNxt
- 1;
1021 Seg
->Flag
= TCP_FLG_ACK
;
1023 Result
= TcpTransmitSegment (Tcb
, Nbuf
);
1030 Check whether to send an ACK or delayed ACK.
1032 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
1043 // Generally, TCP should send a delayed ACK unless:
1044 // 1. ACK at least every other FULL sized segment received.
1045 // 2. Packets received out of order.
1046 // 3. Receiving window is open.
1048 if (TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_ACK_NOW
) || (Tcb
->DelayedAck
>= 1)) {
1053 TcpNow
= TcpRcvWinNow (Tcb
);
1055 if (TcpNow
> TcpRcvWinOld (Tcb
)) {
1062 "TcpToSendAck: scheduled a delayed ACK for TCB %p\n",
1067 // Schedule a delayed ACK.
1073 Send a RESET segment in response to the segment received.
1075 @param[in] Tcb Pointer to the TCP_CB of this TCP instance. May be NULL.
1076 @param[in] Head TCP header of the segment that triggers the reset.
1077 @param[in] Len Length of the segment that triggers the reset.
1078 @param[in] Local Local IP address.
1079 @param[in] Remote Remote peer's IP address.
1080 @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack,
1081 IP_VERSION_6 indicates TCP is running on IP6 stack.
1083 @retval 0 A reset was sent or there is no need to send it.
1084 @retval -1 No reset is sent.
1092 IN EFI_IP_ADDRESS
*Local
,
1093 IN EFI_IP_ADDRESS
*Remote
,
1102 // Don't respond to a Reset with reset.
1104 if ((Head
->Flag
& TCP_FLG_RST
) != 0) {
1108 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
1114 Nhead
= (TCP_HEAD
*) NetbufAllocSpace (
1120 ASSERT (Nhead
!= NULL
);
1123 Nhead
->Flag
= TCP_FLG_RST
;
1126 // Derive Seq/ACK from the segment if no TCB
1127 // is associated with it, otherwise derive from the Tcb.
1131 if (TCP_FLG_ON (Head
->Flag
, TCP_FLG_ACK
)) {
1132 Nhead
->Seq
= Head
->Ack
;
1136 TCP_SET_FLG (Nhead
->Flag
, TCP_FLG_ACK
);
1137 Nhead
->Ack
= HTONL (NTOHL (Head
->Seq
) + Len
);
1141 Nhead
->Seq
= HTONL (Tcb
->SndNxt
);
1142 Nhead
->Ack
= HTONL (Tcb
->RcvNxt
);
1143 TCP_SET_FLG (Nhead
->Flag
, TCP_FLG_ACK
);
1146 Nhead
->SrcPort
= Head
->DstPort
;
1147 Nhead
->DstPort
= Head
->SrcPort
;
1148 Nhead
->HeadLen
= (UINT8
) (sizeof (TCP_HEAD
) >> 2);
1150 Nhead
->Wnd
= HTONS (0xFFFF);
1151 Nhead
->Checksum
= 0;
1154 if (Version
== IP_VERSION_4
) {
1155 HeadSum
= NetPseudoHeadChecksum (Local
->Addr
[0], Remote
->Addr
[0], 6, 0);
1157 HeadSum
= NetIp6PseudoHeadChecksum (&Local
->v6
, &Remote
->v6
, 6, 0);
1160 Nhead
->Checksum
= TcpChecksum (Nbuf
, HeadSum
);
1162 TcpSendIpPacket (Tcb
, Nbuf
, Local
, Remote
, Version
);
1170 Verify that the segment is in good shape.
1172 @param[in] Nbuf The buffer that contains the segment to be checked.
1174 @retval 0 The segment is broken.
1175 @retval 1 The segment is in good shape.
1191 NET_CHECK_SIGNATURE (Nbuf
, NET_BUF_SIGNATURE
);
1193 Seg
= TCPSEG_NETBUF (Nbuf
);
1194 Len
= Nbuf
->TotalSize
;
1198 if (Head
->Flag
!= Seg
->Flag
) {
1202 Len
-= (Head
->HeadLen
<< 2);
1205 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
1209 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
1213 if (Seg
->Seq
+ Len
!= Seg
->End
) {