2 Misc support routines for tcp.
4 Copyright (c) 2005 - 2016, 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
);
160 Tcb
->TsRecent
= Opt
->TSVal
;
163 // Compute the effective SndMss per RFC1122
164 // section 4.2.2.6. If timestamp option is
165 // enabled, it will always occupy 12 bytes.
167 Tcb
->SndMss
-= TCP_OPTION_TS_ALIGNED_LEN
;
173 Locate a listen TCB that matchs the Local and Remote.
175 @param Local Pointer to the local (IP, Port).
176 @param Remote Pointer to the remote (IP, Port).
178 @return Pointer to the TCP_CB with the least number of wildcard,
179 if NULL no match is found.
197 NET_LIST_FOR_EACH (Entry
, &mTcpListenQue
) {
198 Node
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
200 if ((Local
->Port
!= Node
->LocalEnd
.Port
) ||
201 !TCP_PEER_MATCH (Remote
, &Node
->RemoteEnd
) ||
202 !TCP_PEER_MATCH (Local
, &Node
->LocalEnd
)) {
208 // Compute the number of wildcard
211 if (Node
->RemoteEnd
.Ip
== 0) {
215 if (Node
->RemoteEnd
.Port
== 0) {
219 if (Node
->LocalEnd
.Ip
== 0) {
238 Try to find one Tcb whose <Ip, Port> equals to <IN Addr, IN Port>.
240 @param Addr Pointer to the IP address needs to match.
241 @param Port The port number needs to match.
243 @return The Tcb which matches the <Addr Port> paire exists or not.
248 IN EFI_IPv4_ADDRESS
*Addr
,
252 TCP_PORTNO LocalPort
;
256 ASSERT ((Addr
!= NULL
) && (Port
!= 0));
258 LocalPort
= HTONS (Port
);
260 NET_LIST_FOR_EACH (Entry
, &mTcpListenQue
) {
261 Tcb
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
263 if (EFI_IP4_EQUAL (Addr
, &Tcb
->LocalEnd
.Ip
) &&
264 (LocalPort
== Tcb
->LocalEnd
.Port
)) {
270 NET_LIST_FOR_EACH (Entry
, &mTcpRunQue
) {
271 Tcb
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
273 if (EFI_IP4_EQUAL (Addr
, &Tcb
->LocalEnd
.Ip
) &&
274 (LocalPort
== Tcb
->LocalEnd
.Port
)) {
285 Locate the TCP_CB related to the socket pair.
287 @param LocalPort The local port number.
288 @param LocalIp The local IP address.
289 @param RemotePort The remote port number.
290 @param RemoteIp The remote IP address.
291 @param Syn Whether to search the listen sockets, if TRUE, the
292 listen sockets are searched.
294 @return Pointer to the related TCP_CB, if NULL no match is found.
299 IN TCP_PORTNO LocalPort
,
301 IN TCP_PORTNO RemotePort
,
311 Local
.Port
= LocalPort
;
314 Remote
.Port
= RemotePort
;
315 Remote
.Ip
= RemoteIp
;
318 // First check for exact match.
320 NET_LIST_FOR_EACH (Entry
, &mTcpRunQue
) {
321 Tcb
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
323 if (TCP_PEER_EQUAL (&Remote
, &Tcb
->RemoteEnd
) &&
324 TCP_PEER_EQUAL (&Local
, &Tcb
->LocalEnd
)) {
326 RemoveEntryList (&Tcb
->List
);
327 InsertHeadList (&mTcpRunQue
, &Tcb
->List
);
334 // Only check listen queue when SYN flag is on
337 return TcpLocateListenTcb (&Local
, &Remote
);
345 Insert a Tcb into the proper queue.
347 @param Tcb Pointer to the TCP_CB to be inserted.
349 @retval 0 The Tcb is inserted successfully.
350 @retval -1 Error condition occurred.
364 ((Tcb
->State
== TCP_LISTEN
) ||
365 (Tcb
->State
== TCP_SYN_SENT
) ||
366 (Tcb
->State
== TCP_SYN_RCVD
) ||
367 (Tcb
->State
== TCP_CLOSED
))
370 if (Tcb
->LocalEnd
.Port
== 0) {
376 if (Tcb
->State
== TCP_LISTEN
) {
377 Head
= &mTcpListenQue
;
381 // Check that Tcb isn't already on the list.
383 NET_LIST_FOR_EACH (Entry
, Head
) {
384 Node
= NET_LIST_USER_STRUCT (Entry
, TCP_CB
, List
);
386 if (TCP_PEER_EQUAL (&Tcb
->LocalEnd
, &Node
->LocalEnd
) &&
387 TCP_PEER_EQUAL (&Tcb
->RemoteEnd
, &Node
->RemoteEnd
)) {
393 InsertHeadList (Head
, &Tcb
->List
);
400 Clone a TCB_CB from Tcb.
402 @param Tcb Pointer to the TCP_CB to be cloned.
404 @return Pointer to the new cloned TCP_CB, if NULL error condition occurred.
414 Clone
= AllocatePool (sizeof (TCP_CB
));
421 CopyMem (Clone
, Tcb
, sizeof (TCP_CB
));
424 // Increate the reference count of the shared IpInfo.
426 NET_GET_REF (Tcb
->IpInfo
);
428 InitializeListHead (&Clone
->List
);
429 InitializeListHead (&Clone
->SndQue
);
430 InitializeListHead (&Clone
->RcvQue
);
432 Clone
->Sk
= SockClone (Tcb
->Sk
);
433 if (Clone
->Sk
== NULL
) {
434 DEBUG ((EFI_D_ERROR
, "TcpCloneTcb: failed to clone a sock\n"));
439 ((TCP4_PROTO_DATA
*) (Clone
->Sk
->ProtoReserved
))->TcpPcb
= Clone
;
446 Compute an ISS to be used by a new connection.
448 @return The result ISS.
456 mTcpGlobalIss
+= 2048;
457 return mTcpGlobalIss
;
464 @param Sock Pointer to the socket to get mss
466 @return The mss size.
474 EFI_IP4_MODE_DATA Ip4Mode
;
475 TCP4_PROTO_DATA
*TcpProto
;
476 EFI_IP4_PROTOCOL
*Ip
;
478 ASSERT (Sock
!= NULL
);
480 TcpProto
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
481 Ip
= TcpProto
->TcpService
->IpIo
->Ip
.Ip4
;
484 Ip
->GetModeData (Ip
, &Ip4Mode
, NULL
, NULL
);
486 return (UINT16
) (Ip4Mode
.MaxPacketSize
- sizeof (TCP_HEAD
));
493 @param Tcb Pointer to the TCP_CB of this TCP instance.
494 @param State The state to be set.
503 ASSERT (Tcb
->State
< (sizeof (mTcpStateName
) / sizeof (CHAR16
*)));
504 ASSERT (State
< (sizeof (mTcpStateName
) / sizeof (CHAR16
*)));
508 "Tcb (%p) state %s --> %s\n",
510 mTcpStateName
[Tcb
->State
],
511 mTcpStateName
[State
])
517 case TCP_ESTABLISHED
:
519 SockConnEstablished (Tcb
->Sk
);
521 if (Tcb
->Parent
!= NULL
) {
523 // A new connection is accepted by a listening socket, install
526 TcpInstallDevicePath (Tcb
->Sk
);
533 SockConnClosed (Tcb
->Sk
);
543 Compute the TCP segment's checksum.
545 @param Nbuf Pointer to the buffer that contains the TCP
547 @param HeadSum The checksum value of the fixed part of pseudo
550 @return The checksum value.
561 Checksum
= NetbufChecksum (Nbuf
);
562 Checksum
= NetAddChecksum (Checksum
, HeadSum
);
564 Checksum
= NetAddChecksum (
566 HTONS ((UINT16
) Nbuf
->TotalSize
)
569 return (UINT16
) ~Checksum
;
573 Translate the information from the head of the received TCP
574 segment Nbuf contains and fill it into a TCP_SEG structure.
576 @param Tcb Pointer to the TCP_CB of this TCP instance.
577 @param Nbuf Pointer to the buffer contains the TCP segment.
579 @return Pointer to the TCP_SEG that contains the translated TCP head information.
591 Seg
= TCPSEG_NETBUF (Nbuf
);
592 Head
= (TCP_HEAD
*) NetbufGetByte (Nbuf
, 0, NULL
);
593 ASSERT (Head
!= NULL
);
596 Seg
->Seq
= NTOHL (Head
->Seq
);
597 Seg
->Ack
= NTOHL (Head
->Ack
);
598 Seg
->End
= Seg
->Seq
+ (Nbuf
->TotalSize
- (Head
->HeadLen
<< 2));
600 Seg
->Urg
= NTOHS (Head
->Urg
);
601 Seg
->Wnd
= (NTOHS (Head
->Wnd
) << Tcb
->SndWndScale
);
602 Seg
->Flag
= Head
->Flag
;
605 // SYN and FIN flag occupy one sequence space each.
607 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_SYN
)) {
609 // RFC requires that initial window not be scaled
611 Seg
->Wnd
= NTOHS (Head
->Wnd
);
615 if (TCP_FLG_ON (Seg
->Flag
, TCP_FLG_FIN
)) {
624 Reset the connection related with Tcb.
626 @param Tcb Pointer to the TCP_CB of the connection to be
638 Nbuf
= NetbufAlloc (TCP_MAX_HEAD
);
644 Nhead
= (TCP_HEAD
*) NetbufAllocSpace (
650 ASSERT (Nhead
!= NULL
);
654 Nhead
->Flag
= TCP_FLG_RST
;
655 Nhead
->Seq
= HTONL (Tcb
->SndNxt
);
656 Nhead
->Ack
= HTONL (Tcb
->RcvNxt
);
657 Nhead
->SrcPort
= Tcb
->LocalEnd
.Port
;
658 Nhead
->DstPort
= Tcb
->RemoteEnd
.Port
;
659 Nhead
->HeadLen
= (UINT8
) (sizeof (TCP_HEAD
) >> 2);
661 Nhead
->Wnd
= HTONS (0xFFFF);
664 Nhead
->Checksum
= TcpChecksum (Nbuf
, Tcb
->HeadSum
);
666 TcpSendIpPacket (Tcb
, Nbuf
, Tcb
->LocalEnd
.Ip
, Tcb
->RemoteEnd
.Ip
);
673 Initialize an active connection.
675 @param Tcb Pointer to the TCP_CB that wants to initiate a
684 TcpInitTcbLocal (Tcb
);
685 TcpSetState (Tcb
, TCP_SYN_SENT
);
687 TcpSetTimer (Tcb
, TCP_TIMER_CONNECT
, Tcb
->ConnectTimeout
);
688 TcpToSendData (Tcb
, 1);
693 Initiate the connection close procedure, called when
694 applications want to close the connection.
696 @param Tcb Pointer to the TCP_CB of this TCP instance.
704 ASSERT (Tcb
!= NULL
);
706 if (!IsListEmpty (&Tcb
->RcvQue
) || GET_RCV_DATASIZE (Tcb
->Sk
) != 0) {
708 DEBUG ((EFI_D_WARN
, "TcpOnAppClose: connection reset "
709 "because data is lost for TCB %p\n", Tcb
));
711 TcpResetConnection (Tcb
);
716 switch (Tcb
->State
) {
720 TcpSetState (Tcb
, TCP_CLOSED
);
724 case TCP_ESTABLISHED
:
725 TcpSetState (Tcb
, TCP_FIN_WAIT_1
);
729 TcpSetState (Tcb
, TCP_LAST_ACK
);
735 TcpToSendData (Tcb
, 1);
740 Check whether the application's newly delivered data can be sent out.
742 @param Tcb Pointer to the TCP_CB of this TCP instance.
744 @retval 0 Whether the data is sent out or is buffered for
746 @retval -1 The Tcb is not in a state that data is permitted to
756 switch (Tcb
->State
) {
767 case TCP_ESTABLISHED
:
769 TcpToSendData (Tcb
, 0);
788 Application has consumed some data, check whether
789 to send a window updata ack or a delayed ack.
791 @param Tcb Pointer to the TCP_CB of this TCP instance.
801 switch (Tcb
->State
) {
812 case TCP_ESTABLISHED
:
813 TcpOld
= TcpRcvWinOld (Tcb
);
814 if (TcpRcvWinNow (Tcb
) > TcpOld
) {
816 if (TcpOld
< Tcb
->RcvMss
) {
818 DEBUG ((EFI_D_NET
, "TcpOnAppConsume: send a window"
819 " update for a window closed Tcb %p\n", Tcb
));
822 } else if (Tcb
->DelayedAck
== 0) {
824 DEBUG ((EFI_D_NET
, "TcpOnAppConsume: scheduled a delayed"
825 " ACK to update window for Tcb %p\n", Tcb
));
850 Abort the connection by sending a reset segment, called
851 when the application wants to abort the connection.
853 @param Tcb Pointer to the TCP_CB of the TCP instance.
861 DEBUG ((EFI_D_WARN
, "TcpOnAppAbort: connection reset "
862 "issued by application for TCB %p\n", Tcb
));
864 switch (Tcb
->State
) {
866 case TCP_ESTABLISHED
:
870 TcpResetConnection (Tcb
);
876 TcpSetState (Tcb
, TCP_CLOSED
);
880 Install the device path protocol on the TCP instance.
882 @param Sock Pointer to the socket representing the TCP instance.
884 @retval EFI_SUCCESS The device path protocol is installed.
885 @retval other Failed to install the device path protocol.
889 TcpInstallDevicePath (
893 TCP4_PROTO_DATA
*TcpProto
;
894 TCP4_SERVICE_DATA
*TcpService
;
896 IPv4_DEVICE_PATH Ip4DPathNode
;
898 TCP_PORTNO LocalPort
;
899 TCP_PORTNO RemotePort
;
901 TcpProto
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
902 TcpService
= TcpProto
->TcpService
;
903 Tcb
= TcpProto
->TcpPcb
;
905 LocalPort
= NTOHS (Tcb
->LocalEnd
.Port
);
906 RemotePort
= NTOHS (Tcb
->RemoteEnd
.Port
);
907 NetLibCreateIPv4DPathNode (
909 TcpService
->ControllerHandle
,
918 IP4_COPY_ADDRESS (&Ip4DPathNode
.SubnetMask
, &Tcb
->SubnetMask
);
920 Sock
->DevicePath
= AppendDevicePathNode (
921 Sock
->ParentDevicePath
,
922 (EFI_DEVICE_PATH_PROTOCOL
*) &Ip4DPathNode
924 if (Sock
->DevicePath
== NULL
) {
925 return EFI_OUT_OF_RESOURCES
;
928 Status
= gBS
->InstallProtocolInterface (
930 &gEfiDevicePathProtocolGuid
,
931 EFI_NATIVE_INTERFACE
,
934 if (EFI_ERROR (Status
)) {
935 FreePool (Sock
->DevicePath
);