2 Tcp request dispatcher implementation.
4 (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2018, 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<BR>
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 #define TCP_COMP_VAL(Min, Max, Default, Val) \
19 ((((Val) <= (Max)) && ((Val) >= (Min))) ? (Val) : (Default))
22 Add or remove a route entry in the IP route table associated with this TCP instance.
24 @param Tcb Pointer to the TCP_CB of this TCP instance.
25 @param RouteInfo Pointer to the route info to be processed.
27 @retval EFI_SUCCESS The operation completed successfully.
28 @retval EFI_NOT_STARTED The driver instance has not been started.
29 @retval EFI_NO_MAPPING When using the default address, configuration(DHCP,
30 BOOTP, RARP, etc.) is not finished yet.
31 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
32 @retval EFI_NOT_FOUND This route is not in the routing table
33 (when RouteInfo->DeleteRoute is TRUE).
34 @retval EFI_ACCESS_DENIED The route is already defined in the routing table
35 (when RouteInfo->DeleteRoute is FALSE).
40 IN TCP4_ROUTE_INFO
*RouteInfo
43 EFI_IP4_PROTOCOL
*Ip4
;
45 Ip4
= Tcb
->IpInfo
->Ip
.Ip4
;
51 RouteInfo
->DeleteRoute
,
52 RouteInfo
->SubnetAddress
,
53 RouteInfo
->SubnetMask
,
54 RouteInfo
->GatewayAddress
61 Get the operational settings of this TCP instance.
63 @param Tcb Pointer to the TCP_CB of this TCP instance.
64 @param Mode Pointer to the buffer to store the operational
67 @retval EFI_SUCCESS The mode data is read.
68 @retval EFI_NOT_STARTED No configuration data is available because this
69 instance hasn't been started.
75 IN OUT TCP4_MODE_DATA
*Mode
79 EFI_TCP4_CONFIG_DATA
*ConfigData
;
80 EFI_TCP4_ACCESS_POINT
*AccessPoint
;
81 EFI_TCP4_OPTION
*Option
;
86 if (!SOCK_IS_CONFIGURED (Sock
) && (Mode
->Tcp4ConfigData
!= NULL
)) {
87 return EFI_NOT_STARTED
;
90 if (Mode
->Tcp4State
!= NULL
) {
91 *(Mode
->Tcp4State
) = (EFI_TCP4_CONNECTION_STATE
) Tcb
->State
;
94 if (Mode
->Tcp4ConfigData
!= NULL
) {
96 ConfigData
= Mode
->Tcp4ConfigData
;
97 AccessPoint
= &(ConfigData
->AccessPoint
);
98 Option
= ConfigData
->ControlOption
;
100 ConfigData
->TypeOfService
= Tcb
->Tos
;
101 ConfigData
->TimeToLive
= Tcb
->Ttl
;
103 AccessPoint
->UseDefaultAddress
= Tcb
->UseDefaultAddr
;
105 IP4_COPY_ADDRESS (&AccessPoint
->StationAddress
, &Tcb
->LocalEnd
.Ip
);
106 IP4_COPY_ADDRESS (&AccessPoint
->SubnetMask
, &Tcb
->SubnetMask
);
107 AccessPoint
->StationPort
= NTOHS (Tcb
->LocalEnd
.Port
);
109 IP4_COPY_ADDRESS (&AccessPoint
->RemoteAddress
, &Tcb
->RemoteEnd
.Ip
);
110 AccessPoint
->RemotePort
= NTOHS (Tcb
->RemoteEnd
.Port
);
111 AccessPoint
->ActiveFlag
= (BOOLEAN
) (Tcb
->State
!= TCP_LISTEN
);
113 if (Option
!= NULL
) {
114 Option
->ReceiveBufferSize
= GET_RCV_BUFFSIZE (Tcb
->Sk
);
115 Option
->SendBufferSize
= GET_SND_BUFFSIZE (Tcb
->Sk
);
116 Option
->MaxSynBackLog
= GET_BACKLOG (Tcb
->Sk
);
118 Option
->ConnectionTimeout
= Tcb
->ConnectTimeout
/ TCP_TICK_HZ
;
119 Option
->DataRetries
= Tcb
->MaxRexmit
;
120 Option
->FinTimeout
= Tcb
->FinWait2Timeout
/ TCP_TICK_HZ
;
121 Option
->TimeWaitTimeout
= Tcb
->TimeWaitTimeout
/ TCP_TICK_HZ
;
122 Option
->KeepAliveProbes
= Tcb
->MaxKeepAlive
;
123 Option
->KeepAliveTime
= Tcb
->KeepAliveIdle
/ TCP_TICK_HZ
;
124 Option
->KeepAliveInterval
= Tcb
->KeepAlivePeriod
/ TCP_TICK_HZ
;
126 Option
->EnableNagle
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
));
127 Option
->EnableTimeStamp
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
));
128 Option
->EnableWindowScaling
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
));
130 Option
->EnableSelectiveAck
= FALSE
;
131 Option
->EnablePathMtuDiscovery
= FALSE
;
135 Ip
= Tcb
->IpInfo
->Ip
.Ip4
;
138 return Ip
->GetModeData (Ip
, Mode
->Ip4ModeData
, Mode
->MnpConfigData
, Mode
->SnpModeData
);
143 If AP->StationPort isn't zero, check whether the access point
144 is registered, else generate a random station port for this
147 @param AP Pointer to the access point.
149 @retval EFI_SUCCESS The check is passed or the port is assigned.
150 @retval EFI_INVALID_PARAMETER The non-zero station port is already used.
151 @retval EFI_OUT_OF_RESOURCES No port can be allocated.
156 IN EFI_TCP4_ACCESS_POINT
*AP
161 if (0 != AP
->StationPort
) {
163 // check if a same endpoint is bound
165 if (TcpFindTcbByPeer (&AP
->StationAddress
, AP
->StationPort
)) {
167 return EFI_INVALID_PARAMETER
;
171 // generate a random port
175 if (TCP4_PORT_USER_RESERVED
== mTcp4RandomPort
) {
176 mTcp4RandomPort
= TCP4_PORT_KNOWN
;
181 while (TcpFindTcbByPeer (&AP
->StationAddress
, mTcp4RandomPort
)) {
185 if (mTcp4RandomPort
<= TCP4_PORT_KNOWN
) {
188 DEBUG ((EFI_D_ERROR
, "Tcp4Bind: no port can be allocated "
191 return EFI_OUT_OF_RESOURCES
;
194 mTcp4RandomPort
= TCP4_PORT_KNOWN
+ 1;
201 AP
->StationPort
= mTcp4RandomPort
;
209 Flush the Tcb add its associated protocols.
211 @param Tcb Pointer to the TCP_CB to be flushed.
221 IpIoConfigIp (Tcb
->IpInfo
, NULL
);
225 if (SOCK_IS_CONFIGURED (Sock
)) {
226 RemoveEntryList (&Tcb
->List
);
229 // Uninstall the device path protocol.
231 if (Sock
->DevicePath
!= NULL
) {
232 gBS
->UninstallProtocolInterface (
234 &gEfiDevicePathProtocolGuid
,
237 FreePool (Sock
->DevicePath
);
241 NetbufFreeList (&Tcb
->SndQue
);
242 NetbufFreeList (&Tcb
->RcvQue
);
243 Tcb
->State
= TCP_CLOSED
;
247 Attach a Pcb to the socket.
249 @param Sk Pointer to the socket of this TCP instance.
251 @retval EFI_SUCCESS The operation is completed successfully.
252 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
261 TCP4_PROTO_DATA
*ProtoData
;
266 Tcb
= AllocateZeroPool (sizeof (TCP_CB
));
270 DEBUG ((EFI_D_ERROR
, "Tcp4ConfigurePcb: failed to allocate a TCB\n"));
272 return EFI_OUT_OF_RESOURCES
;
275 ProtoData
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
276 IpIo
= ProtoData
->TcpService
->IpIo
;
279 // Create an IpInfo for this Tcb.
281 Tcb
->IpInfo
= IpIoAddIp (IpIo
);
282 if (Tcb
->IpInfo
== NULL
) {
285 return EFI_OUT_OF_RESOURCES
;
289 // Open the new created IP instance BY_CHILD.
291 Status
= gBS
->OpenProtocol (
292 Tcb
->IpInfo
->ChildHandle
,
293 &gEfiIp4ProtocolGuid
,
297 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
299 if (EFI_ERROR (Status
)) {
300 IpIoRemoveIp (IpIo
, Tcb
->IpInfo
);
304 InitializeListHead (&Tcb
->List
);
305 InitializeListHead (&Tcb
->SndQue
);
306 InitializeListHead (&Tcb
->RcvQue
);
308 Tcb
->State
= TCP_CLOSED
;
310 ProtoData
->TcpPcb
= Tcb
;
316 Detach the Pcb of the socket.
318 @param Sk Pointer to the socket of this TCP instance.
326 TCP4_PROTO_DATA
*ProtoData
;
329 ProtoData
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
330 Tcb
= ProtoData
->TcpPcb
;
332 ASSERT (Tcb
!= NULL
);
336 IpIoRemoveIp (ProtoData
->TcpService
->IpIo
, Tcb
->IpInfo
);
340 ProtoData
->TcpPcb
= NULL
;
345 Configure the Pcb using CfgData.
347 @param Sk Pointer to the socket of this TCP instance.
348 @param CfgData Pointer to the TCP configuration data.
350 @retval EFI_SUCCESS The operation is completed successfully.
351 @retval EFI_INVALID_PARAMETER A same access point has been configured in
352 another TCP instance.
353 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
359 IN EFI_TCP4_CONFIG_DATA
*CfgData
362 EFI_IP4_CONFIG_DATA IpCfgData
;
364 EFI_TCP4_OPTION
*Option
;
365 TCP4_PROTO_DATA
*TcpProto
;
368 ASSERT ((CfgData
!= NULL
) && (Sk
!= NULL
) && (Sk
->SockHandle
!= NULL
));
370 TcpProto
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
371 Tcb
= TcpProto
->TcpPcb
;
373 ASSERT (Tcb
!= NULL
);
376 // Add Ip for send pkt to the peer
378 CopyMem (&IpCfgData
, &mIp4IoDefaultIpConfigData
, sizeof (IpCfgData
));
379 IpCfgData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
380 IpCfgData
.UseDefaultAddress
= CfgData
->AccessPoint
.UseDefaultAddress
;
381 IpCfgData
.StationAddress
= CfgData
->AccessPoint
.StationAddress
;
382 IpCfgData
.SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
383 IpCfgData
.ReceiveTimeout
= (UINT32
) (-1);
386 // Configure the IP instance this Tcb consumes.
388 Status
= IpIoConfigIp (Tcb
->IpInfo
, &IpCfgData
);
389 if (EFI_ERROR (Status
)) {
394 // Get the default address info if the instance is configured to use default address.
396 if (CfgData
->AccessPoint
.UseDefaultAddress
) {
397 CfgData
->AccessPoint
.StationAddress
= IpCfgData
.StationAddress
;
398 CfgData
->AccessPoint
.SubnetMask
= IpCfgData
.SubnetMask
;
402 // check if we can bind this endpoint in CfgData
404 Status
= Tcp4Bind (&(CfgData
->AccessPoint
));
406 if (EFI_ERROR (Status
)) {
407 DEBUG ((EFI_D_ERROR
, "Tcp4ConfigurePcb: Bind endpoint failed "
408 "with %r\n", Status
));
414 // Initalize the operating information in this Tcb
416 ASSERT (Tcb
->State
== TCP_CLOSED
&&
417 IsListEmpty (&Tcb
->SndQue
) &&
418 IsListEmpty (&Tcb
->RcvQue
));
420 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
421 Tcb
->State
= TCP_CLOSED
;
424 Tcb
->RcvMss
= TcpGetRcvMss (Sk
);
427 Tcb
->Rto
= 3 * TCP_TICK_HZ
;
429 Tcb
->CWnd
= Tcb
->SndMss
;
430 Tcb
->Ssthresh
= 0xffffffff;
432 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
434 Tcb
->KeepAliveIdle
= TCP_KEEPALIVE_IDLE_MIN
;
435 Tcb
->KeepAlivePeriod
= TCP_KEEPALIVE_PERIOD
;
436 Tcb
->MaxKeepAlive
= TCP_MAX_KEEPALIVE
;
437 Tcb
->MaxRexmit
= TCP_MAX_LOSS
;
438 Tcb
->FinWait2Timeout
= TCP_FIN_WAIT2_TIME
;
439 Tcb
->TimeWaitTimeout
= TCP_TIME_WAIT_TIME
;
440 Tcb
->ConnectTimeout
= TCP_CONNECT_TIME
;
443 // initialize Tcb in the light of CfgData
445 Tcb
->Ttl
= CfgData
->TimeToLive
;
446 Tcb
->Tos
= CfgData
->TypeOfService
;
448 Tcb
->UseDefaultAddr
= CfgData
->AccessPoint
.UseDefaultAddress
;
450 CopyMem (&Tcb
->LocalEnd
.Ip
, &CfgData
->AccessPoint
.StationAddress
, sizeof (IP4_ADDR
));
451 Tcb
->LocalEnd
.Port
= HTONS (CfgData
->AccessPoint
.StationPort
);
452 IP4_COPY_ADDRESS (&Tcb
->SubnetMask
, &CfgData
->AccessPoint
.SubnetMask
);
454 if (CfgData
->AccessPoint
.ActiveFlag
) {
455 CopyMem (&Tcb
->RemoteEnd
.Ip
, &CfgData
->AccessPoint
.RemoteAddress
, sizeof (IP4_ADDR
));
456 Tcb
->RemoteEnd
.Port
= HTONS (CfgData
->AccessPoint
.RemotePort
);
458 Tcb
->RemoteEnd
.Ip
= 0;
459 Tcb
->RemoteEnd
.Port
= 0;
462 Option
= CfgData
->ControlOption
;
464 if (Option
!= NULL
) {
467 (UINT32
) (TCP_COMP_VAL (
468 TCP_RCV_BUF_SIZE_MIN
,
471 Option
->ReceiveBufferSize
477 (UINT32
) (TCP_COMP_VAL (
478 TCP_SND_BUF_SIZE_MIN
,
481 Option
->SendBufferSize
488 (UINT32
) (TCP_COMP_VAL (
492 Option
->MaxSynBackLog
497 Tcb
->MaxRexmit
= (UINT16
) TCP_COMP_VAL (
503 Tcb
->FinWait2Timeout
= TCP_COMP_VAL (
505 TCP_FIN_WAIT2_TIME_MAX
,
507 (UINT32
) (Option
->FinTimeout
* TCP_TICK_HZ
)
510 if (Option
->TimeWaitTimeout
!= 0) {
511 Tcb
->TimeWaitTimeout
= TCP_COMP_VAL (
513 TCP_TIME_WAIT_TIME_MAX
,
515 (UINT32
) (Option
->TimeWaitTimeout
* TCP_TICK_HZ
)
518 Tcb
->TimeWaitTimeout
= 0;
521 if (Option
->KeepAliveProbes
!= 0) {
522 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
524 Tcb
->MaxKeepAlive
= (UINT8
) TCP_COMP_VAL (
525 TCP_MAX_KEEPALIVE_MIN
,
528 Option
->KeepAliveProbes
530 Tcb
->KeepAliveIdle
= TCP_COMP_VAL (
531 TCP_KEEPALIVE_IDLE_MIN
,
532 TCP_KEEPALIVE_IDLE_MAX
,
533 TCP_KEEPALIVE_IDLE_MIN
,
534 (UINT32
) (Option
->KeepAliveTime
* TCP_TICK_HZ
)
536 Tcb
->KeepAlivePeriod
= TCP_COMP_VAL (
537 TCP_KEEPALIVE_PERIOD_MIN
,
538 TCP_KEEPALIVE_PERIOD
,
539 TCP_KEEPALIVE_PERIOD
,
540 (UINT32
) (Option
->KeepAliveInterval
* TCP_TICK_HZ
)
544 Tcb
->ConnectTimeout
= TCP_COMP_VAL (
545 TCP_CONNECT_TIME_MIN
,
548 (UINT32
) (Option
->ConnectionTimeout
* TCP_TICK_HZ
)
551 if (!Option
->EnableNagle
) {
552 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
);
555 if (!Option
->EnableTimeStamp
) {
556 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
);
559 if (!Option
->EnableWindowScaling
) {
560 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
);
565 // The socket is bound, the <SrcIp, SrcPort, DstIp, DstPort> is
566 // determined, construct the IP device path and install it.
568 Status
= TcpInstallDevicePath (Sk
);
569 if (EFI_ERROR (Status
)) {
574 // update state of Tcb and socket
576 if (!CfgData
->AccessPoint
.ActiveFlag
) {
578 TcpSetState (Tcb
, TCP_LISTEN
);
579 SockSetState (Sk
, SO_LISTENING
);
581 Sk
->ConfigureState
= SO_CONFIGURED_PASSIVE
;
584 Sk
->ConfigureState
= SO_CONFIGURED_ACTIVE
;
596 The procotol handler provided to the socket layer, used to
597 dispatch the socket level requests by calling the corresponding
600 @param Sock Pointer to the socket of this TCP instance.
601 @param Request The code of this operation request.
602 @param Data Pointer to the operation specific data passed in
603 together with the operation request.
605 @retval EFI_SUCCESS The socket request is completed successfully.
606 @retval other The error status returned by the corresponding TCP
614 IN VOID
*Data OPTIONAL
618 TCP4_PROTO_DATA
*ProtoData
;
619 EFI_IP4_PROTOCOL
*Ip
;
621 ProtoData
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
622 Tcb
= ProtoData
->TcpPcb
;
626 Ip
= ProtoData
->TcpService
->IpIo
->Ip
.Ip4
;
632 // After user received data from socket buffer, socket will
633 // notify TCP using this message to give it a chance to send out
634 // window update information
636 ASSERT (Tcb
!= NULL
);
637 TcpOnAppConsume (Tcb
);
642 ASSERT (Tcb
!= NULL
);
659 Tcb
->SndPsh
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
);
660 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
);
665 Tcb
->SndUp
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
) - 1;
666 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
);
672 TcpOnAppConnect (Tcb
);
678 return Tcp4AttachPcb (Sock
);
688 Tcp4DetachPcb (Sock
);
694 return Tcp4ConfigurePcb (
696 (EFI_TCP4_CONFIG_DATA
*) Data
701 ASSERT ((Data
!= NULL
) && (Tcb
!= NULL
));
703 return Tcp4GetMode (Tcb
, (TCP4_MODE_DATA
*) Data
);
707 ASSERT ((Data
!= NULL
) && (Tcb
!= NULL
));
709 return Tcp4Route (Tcb
, (TCP4_ROUTE_INFO
*) Data
);
712 return EFI_UNSUPPORTED
;