2 Misc support routines for tcp.
4 Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/DevicePathLib.h>
20 LIST_ENTRY mTcpRunQue
= {
25 LIST_ENTRY mTcpListenQue
= {
30 TCP_SEQNO mTcpGlobalIss
= 0x4d7e980b;
32 CHAR16
*mTcpStateName
[] = {
48 Initialize the Tcb local related members.
50 @param Tcb Pointer to the TCP_CB of this TCP instance.
59 // Compute the checksum of the fixed parts of pseudo header
61 Tcb
->HeadSum
= NetPseudoHeadChecksum (
68 Tcb
->Iss
= TcpGetIss ();
69 Tcb
->SndUna
= Tcb
->Iss
;
70 Tcb
->SndNxt
= Tcb
->Iss
;
72 Tcb
->SndWl2
= Tcb
->Iss
;
75 Tcb
->RcvWnd
= GET_RCV_BUFFSIZE (Tcb
->Sk
);
78 // First window size is never scaled
82 Tcb
->ProbeTimerOn
= FALSE
;
87 Initialize the peer related members.
89 @param Tcb Pointer to the TCP_CB of this TCP instance.
90 @param Seg Pointer to the segment that contains the peer's
92 @param Opt Pointer to the options announced by the peer.
104 ASSERT ((Tcb
!= NULL
) && (Seg
!= NULL
) && (Opt
!= NULL
));
105 ASSERT (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
));
107 Tcb
->SndWnd
= Seg
->Wnd
;
108 Tcb
->SndWndMax
= Tcb
->SndWnd
;
109 Tcb
->SndWl1
= Seg
->Seq
;
111 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_ACK
)) {
112 Tcb
->SndWl2
= Seg
->Ack
;
114 Tcb
->SndWl2
= Tcb
->Iss
+ 1;
117 if (TCP_FLG_ON (Opt
->Flag
, TCP_OPTION_RCVD_MSS
)) {
118 Tcb
->SndMss
= (UINT16
) MAX (64, Opt
->Mss
);
120 RcvMss
= TcpGetRcvMss (Tcb
->Sk
);
121 if (Tcb
->SndMss
> RcvMss
) {
122 Tcb
->SndMss
= RcvMss
;
127 // One end doesn't support MSS option, use default.
132 Tcb
->CWnd
= Tcb
->SndMss
;
135 Tcb
->RcvNxt
= Tcb
->Irs
+ 1;
137 Tcb
->RcvWl2
= Tcb
->RcvNxt
;
139 if (TCP_FLG_ON (Opt
->Flag
, TCP_OPTION_RCVD_WS
) &&
140 !TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
)) {
142 Tcb
->SndWndScale
= Opt
->WndScale
;
144 Tcb
->RcvWndScale
= TcpComputeScale (Tcb
);
145 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RCVD_WS
);
149 // One end doesn't support window scale option. use zero.
151 Tcb
->RcvWndScale
= 0;
154 if (TCP_FLG_ON (Opt
->Flag
, TCP_OPTION_RCVD_TS
) &&
155 !TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
)) {
157 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_TS
);
158 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_RCVD_TS
);
161 // Compute the effective SndMss per RFC1122
162 // section 4.2.2.6. If timestamp option is
163 // enabled, it will always occupy 12 bytes.
165 Tcb
->SndMss
-= TCP_OPTION_TS_ALIGNED_LEN
;
171 Locate a listen TCB that matchs the Local and Remote.
173 @param Local Pointer to the local (IP, Port).
174 @param Remote Pointer to the remote (IP, Port).
176 @return Pointer to the TCP_CB with the least number of wildcard,
177 if NULL no match is found.
195 NET_LIST_FOR_EACH (Entry
, &mTcpListenQue
) {
196 Node
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
198 if ((Local
->Port
!= Node
->LocalEnd
.Port
) ||
199 !TCP_PEER_MATCH (Remote
, &Node
->RemoteEnd
) ||
200 !TCP_PEER_MATCH (Local
, &Node
->LocalEnd
)) {
206 // Compute the number of wildcard
209 if (Node
->RemoteEnd
.Ip
== 0) {
213 if (Node
->RemoteEnd
.Port
== 0) {
217 if (Node
->LocalEnd
.Ip
== 0) {
236 Try to find one Tcb whose <Ip, Port> equals to <IN Addr, IN Port>.
238 @param Addr Pointer to the IP address needs to match.
239 @param Port The port number needs to match.
241 @return The Tcb which matches the <Addr Port> paire exists or not.
246 IN EFI_IPv4_ADDRESS
*Addr
,
250 TCP_PORTNO LocalPort
;
254 ASSERT ((Addr
!= NULL
) && (Port
!= 0));
256 LocalPort
= HTONS (Port
);
258 NET_LIST_FOR_EACH (Entry
, &mTcpListenQue
) {
259 Tcb
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
261 if (EFI_IP4_EQUAL (Addr
, &Tcb
->LocalEnd
.Ip
) &&
262 (LocalPort
== Tcb
->LocalEnd
.Port
)) {
268 NET_LIST_FOR_EACH (Entry
, &mTcpRunQue
) {
269 Tcb
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
271 if (EFI_IP4_EQUAL (Addr
, &Tcb
->LocalEnd
.Ip
) &&
272 (LocalPort
== Tcb
->LocalEnd
.Port
)) {
283 Locate the TCP_CB related to the socket pair.
285 @param LocalPort The local port number.
286 @param LocalIp The local IP address.
287 @param RemotePort The remote port number.
288 @param RemoteIp The remote IP address.
289 @param Syn Whether to search the listen sockets, if TRUE, the
290 listen sockets are searched.
292 @return Pointer to the related TCP_CB, if NULL no match is found.
297 IN TCP_PORTNO LocalPort
,
299 IN TCP_PORTNO RemotePort
,
309 Local
.Port
= LocalPort
;
312 Remote
.Port
= RemotePort
;
313 Remote
.Ip
= RemoteIp
;
316 // First check for exact match.
318 NET_LIST_FOR_EACH (Entry
, &mTcpRunQue
) {
319 Tcb
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
321 if (TCP_PEER_EQUAL (&Remote
, &Tcb
->RemoteEnd
) &&
322 TCP_PEER_EQUAL (&Local
, &Tcb
->LocalEnd
)) {
324 RemoveEntryList (&Tcb
->List
);
325 InsertHeadList (&mTcpRunQue
, &Tcb
->List
);
332 // Only check listen queue when SYN flag is on
335 return TcpLocateListenTcb (&Local
, &Remote
);
343 Insert a Tcb into the proper queue.
345 @param Tcb Pointer to the TCP_CB to be inserted.
347 @retval 0 The Tcb is inserted successfully.
348 @retval -1 Error condition occurred.
362 ((Tcb
->State
== TCP_LISTEN
) ||
363 (Tcb
->State
== TCP_SYN_SENT
) ||
364 (Tcb
->State
== TCP_SYN_RCVD
) ||
365 (Tcb
->State
== TCP_CLOSED
))
368 if (Tcb
->LocalEnd
.Port
== 0) {
374 if (Tcb
->State
== TCP_LISTEN
) {
375 Head
= &mTcpListenQue
;
379 // Check that Tcb isn't already on the list.
381 NET_LIST_FOR_EACH (Entry
, Head
) {
382 Node
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
384 if (TCP_PEER_EQUAL (&Tcb
->LocalEnd
, &Node
->LocalEnd
) &&
385 TCP_PEER_EQUAL (&Tcb
->RemoteEnd
, &Node
->RemoteEnd
)) {
391 InsertHeadList (Head
, &Tcb
->List
);
398 Clone a TCB_CB from Tcb.
400 @param Tcb Pointer to the TCP_CB to be cloned.
402 @return Pointer to the new cloned TCP_CB, if NULL error condition occurred.
412 Clone
= AllocatePool (sizeof (TCP_CB
));
419 CopyMem (Clone
, Tcb
, sizeof (TCP_CB
));
422 // Increate the reference count of the shared IpInfo.
424 NET_GET_REF (Tcb
->IpInfo
);
426 InitializeListHead (&Clone
->List
);
427 InitializeListHead (&Clone
->SndQue
);
428 InitializeListHead (&Clone
->RcvQue
);
430 Clone
->Sk
= SockClone (Tcb
->Sk
);
431 if (Clone
->Sk
== NULL
) {
432 DEBUG ((EFI_D_ERROR
, "TcpCloneTcb: failed to clone a sock\n"));
437 ((TCP4_PROTO_DATA
*) (Clone
->Sk
->ProtoReserved
))->TcpPcb
= Clone
;
444 Compute an ISS to be used by a new connection.
446 @return The result ISS.
454 mTcpGlobalIss
+= 2048;
455 return mTcpGlobalIss
;
462 @param Sock Pointer to the socket to get mss
464 @return The mss size.
472 EFI_IP4_MODE_DATA Ip4Mode
;
473 TCP4_PROTO_DATA
*TcpProto
;
474 EFI_IP4_PROTOCOL
*Ip
;
476 ASSERT (Sock
!= NULL
);
478 TcpProto
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
479 Ip
= TcpProto
->TcpService
->IpIo
->Ip
.Ip4
;
482 Ip
->GetModeData (Ip
, &Ip4Mode
, NULL
, NULL
);
484 return (UINT16
) (Ip4Mode
.MaxPacketSize
- sizeof (TCP_HEAD
));
491 @param Tcb Pointer to the TCP_CB of this TCP instance.
492 @param State The state to be set.
501 ASSERT (Tcb
->State
< (sizeof (mTcpStateName
) / sizeof (CHAR16
*)));
502 ASSERT (State
< (sizeof (mTcpStateName
) / sizeof (CHAR16
*)));
506 "Tcb (%p) state %s --> %s\n",
508 mTcpStateName
[Tcb
->State
],
509 mTcpStateName
[State
])
515 case TCP_ESTABLISHED
:
517 SockConnEstablished (Tcb
->Sk
);
519 if (Tcb
->Parent
!= NULL
) {
521 // A new connection is accepted by a listening socket, install
524 TcpInstallDevicePath (Tcb
->Sk
);
531 SockConnClosed (Tcb
->Sk
);
541 Compute the TCP segment's checksum.
543 @param Nbuf Pointer to the buffer that contains the TCP
545 @param HeadSum The checksum value of the fixed part of pseudo
548 @return The checksum value.
559 Checksum
= NetbufChecksum (Nbuf
);
560 Checksum
= NetAddChecksum (Checksum
, HeadSum
);
562 Checksum
= NetAddChecksum (
564 HTONS ((UINT16
) Nbuf
->TotalSize
)
567 return (UINT16
) ~Checksum
;
571 Translate the information from the head of the received TCP
572 segment Nbuf contains and fill it into a TCP_SEG structure.
574 @param Tcb Pointer to the TCP_CB of this TCP instance.
575 @param Nbuf Pointer to the buffer contains the TCP segment.
577 @return Pointer to the TCP_SEG that contains the translated TCP head information.
589 Seg
= TCPSEG_NETBUF (Nbuf
);
590 Head
= (TCP_HEAD
*) NetbufGetByte (Nbuf
, 0, NULL
);
591 ASSERT (Head
!= NULL
);
594 Seg
->Seq
= NTOHL (Head
->Seq
);
595 Seg
->Ack
= NTOHL (Head
->Ack
);
596 Seg
->End
= Seg
->Seq
+ (Nbuf
->TotalSize
- (Head
->HeadLen
<< 2));
598 Seg
->Urg
= NTOHS (Head
->Urg
);
599 Seg
->Wnd
= (NTOHS (Head
->Wnd
) << Tcb
->SndWndScale
);
600 Seg
->Flag
= Head
->Flag
;
603 // SYN and FIN flag occupy one sequence space each.
605 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
607 // RFC requires that initial window not be scaled
609 Seg
->Wnd
= NTOHS (Head
->Wnd
);
613 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
622 Reset the connection related with Tcb.
624 @param Tcb Pointer to the TCP_CB of the connection to be
636 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
642 Nhead
= (TCP_HEAD
*) NetbufAllocSpace (
648 ASSERT (Nhead
!= NULL
);
652 Nhead
->Flag
= TCP_FLG_RST
;
653 Nhead
->Seq
= HTONL (Tcb
->SndNxt
);
654 Nhead
->Ack
= HTONL (Tcb
->RcvNxt
);
655 Nhead
->SrcPort
= Tcb
->LocalEnd
.Port
;
656 Nhead
->DstPort
= Tcb
->RemoteEnd
.Port
;
657 Nhead
->HeadLen
= (UINT8
) (sizeof (TCP_HEAD
) >> 2);
659 Nhead
->Wnd
= HTONS (0xFFFF);
662 Nhead
->Checksum
= TcpChecksum (Nbuf
, Tcb
->HeadSum
);
664 TcpSendIpPacket (Tcb
, Nbuf
, Tcb
->LocalEnd
.Ip
, Tcb
->RemoteEnd
.Ip
);
671 Initialize an active connection.
673 @param Tcb Pointer to the TCP_CB that wants to initiate a
682 TcpInitTcbLocal (Tcb
);
683 TcpSetState (Tcb
, TCP_SYN_SENT
);
685 TcpSetTimer (Tcb
, TCP_TIMER_CONNECT
, Tcb
->ConnectTimeout
);
686 TcpToSendData (Tcb
, 1);
691 Initiate the connection close procedure, called when
692 applications want to close the connection.
694 @param Tcb Pointer to the TCP_CB of this TCP instance.
702 ASSERT (Tcb
!= NULL
);
704 if (!IsListEmpty (&Tcb
->RcvQue
) || GET_RCV_DATASIZE (Tcb
->Sk
) != 0) {
706 DEBUG ((EFI_D_WARN
, "TcpOnAppClose: connection reset "
707 "because data is lost for TCB %p\n", Tcb
));
709 TcpResetConnection (Tcb
);
714 switch (Tcb
->State
) {
718 TcpSetState (Tcb
, TCP_CLOSED
);
722 case TCP_ESTABLISHED
:
723 TcpSetState (Tcb
, TCP_FIN_WAIT_1
);
727 TcpSetState (Tcb
, TCP_LAST_ACK
);
733 TcpToSendData (Tcb
, 1);
738 Check whether the application's newly delivered data can be sent out.
740 @param Tcb Pointer to the TCP_CB of this TCP instance.
742 @retval 0 Whether the data is sent out or is buffered for
744 @retval -1 The Tcb is not in a state that data is permitted to
754 switch (Tcb
->State
) {
765 case TCP_ESTABLISHED
:
767 TcpToSendData (Tcb
, 0);
786 Application has consumed some data, check whether
787 to send a window updata ack or a delayed ack.
789 @param Tcb Pointer to the TCP_CB of this TCP instance.
799 switch (Tcb
->State
) {
810 case TCP_ESTABLISHED
:
811 TcpOld
= TcpRcvWinOld (Tcb
);
812 if (TcpRcvWinNow (Tcb
) > TcpOld
) {
814 if (TcpOld
< Tcb
->RcvMss
) {
816 DEBUG ((EFI_D_INFO
, "TcpOnAppConsume: send a window"
817 " update for a window closed Tcb %p\n", Tcb
));
820 } else if (Tcb
->DelayedAck
== 0) {
822 DEBUG ((EFI_D_INFO
, "TcpOnAppConsume: scheduled a delayed"
823 " ACK to update window for Tcb %p\n", Tcb
));
848 Abort the connection by sending a reset segment, called
849 when the application wants to abort the connection.
851 @param Tcb Pointer to the TCP_CB of the TCP instance.
859 DEBUG ((EFI_D_WARN
, "TcpOnAppAbort: connection reset "
860 "issued by application for TCB %p\n", Tcb
));
862 switch (Tcb
->State
) {
864 case TCP_ESTABLISHED
:
868 TcpResetConnection (Tcb
);
874 TcpSetState (Tcb
, TCP_CLOSED
);
878 Install the device path protocol on the TCP instance.
880 @param Sock Pointer to the socket representing the TCP instance.
882 @retval EFI_SUCCESS The device path protocol is installed.
883 @retval other Failed to install the device path protocol.
887 TcpInstallDevicePath (
891 TCP4_PROTO_DATA
*TcpProto
;
892 TCP4_SERVICE_DATA
*TcpService
;
894 IPv4_DEVICE_PATH Ip4DPathNode
;
896 TCP_PORTNO LocalPort
;
897 TCP_PORTNO RemotePort
;
899 TcpProto
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
900 TcpService
= TcpProto
->TcpService
;
901 Tcb
= TcpProto
->TcpPcb
;
903 LocalPort
= NTOHS (Tcb
->LocalEnd
.Port
);
904 RemotePort
= NTOHS (Tcb
->RemoteEnd
.Port
);
905 NetLibCreateIPv4DPathNode (
907 TcpService
->ControllerHandle
,
916 IP4_COPY_ADDRESS (&Ip4DPathNode
.SubnetMask
, &Tcb
->SubnetMask
);
918 Sock
->DevicePath
= AppendDevicePathNode (
919 Sock
->ParentDevicePath
,
920 (EFI_DEVICE_PATH_PROTOCOL
*) &Ip4DPathNode
922 if (Sock
->DevicePath
== NULL
) {
923 return EFI_OUT_OF_RESOURCES
;
926 Status
= gBS
->InstallProtocolInterface (
928 &gEfiDevicePathProtocolGuid
,
929 EFI_NATIVE_INTERFACE
,
932 if (EFI_ERROR (Status
)) {
933 FreePool (Sock
->DevicePath
);