2 Tcp request dispatcher implementation.
4 Copyright (c) 2005 - 2010, Intel Corporation<BR>
5 All rights reserved. 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.
17 #define TCP_COMP_VAL(Min, Max, Default, Val) \
18 ((((Val) <= (Max)) && ((Val) >= (Min))) ? (Val) : (Default))
21 Add or remove a route entry in the IP route table associated with this TCP instance.
23 @param Tcb Pointer to the TCP_CB of this TCP instance.
24 @param RouteInfo Pointer to the route info to be processed.
26 @retval EFI_SUCCESS The operation completed successfully.
27 @retval EFI_NOT_STARTED The driver instance has not been started.
28 @retval EFI_NO_MAPPING When using the default address, configuration(DHCP,
29 BOOTP, RARP, etc.) is not finished yet.
30 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
31 @retval EFI_NOT_FOUND This route is not in the routing table
32 (when RouteInfo->DeleteRoute is TRUE).
33 @retval EFI_ACCESS_DENIED The route is already defined in the routing table
34 (when RouteInfo->DeleteRoute is FALSE).
39 IN TCP4_ROUTE_INFO
*RouteInfo
50 RouteInfo
->DeleteRoute
,
51 RouteInfo
->SubnetAddress
,
52 RouteInfo
->SubnetMask
,
53 RouteInfo
->GatewayAddress
60 Get the operational settings of this TCP instance.
62 @param Tcb Pointer to the TCP_CB of this TCP instance.
63 @param Mode Pointer to the buffer to store the operational
66 @retval EFI_SUCCESS The mode data is read.
67 @retval EFI_NOT_STARTED No configuration data is available because this
68 instance hasn't been started.
74 IN OUT TCP4_MODE_DATA
*Mode
78 EFI_TCP4_CONFIG_DATA
*ConfigData
;
79 EFI_TCP4_ACCESS_POINT
*AccessPoint
;
80 EFI_TCP4_OPTION
*Option
;
85 if (!SOCK_IS_CONFIGURED (Sock
) && (Mode
->Tcp4ConfigData
!= NULL
)) {
86 return EFI_NOT_STARTED
;
89 if (Mode
->Tcp4State
!= NULL
) {
90 *(Mode
->Tcp4State
) = (EFI_TCP4_CONNECTION_STATE
) Tcb
->State
;
93 if (Mode
->Tcp4ConfigData
!= NULL
) {
95 ConfigData
= Mode
->Tcp4ConfigData
;
96 AccessPoint
= &(ConfigData
->AccessPoint
);
97 Option
= ConfigData
->ControlOption
;
99 ConfigData
->TypeOfService
= Tcb
->Tos
;
100 ConfigData
->TimeToLive
= Tcb
->Ttl
;
102 AccessPoint
->UseDefaultAddress
= Tcb
->UseDefaultAddr
;
104 CopyMem (&AccessPoint
->StationAddress
, &Tcb
->LocalEnd
.Ip
, sizeof (EFI_IPv4_ADDRESS
));
105 AccessPoint
->SubnetMask
= Tcb
->SubnetMask
;
106 AccessPoint
->StationPort
= NTOHS (Tcb
->LocalEnd
.Port
);
108 CopyMem (&AccessPoint
->RemoteAddress
, &Tcb
->RemoteEnd
.Ip
, sizeof (EFI_IPv4_ADDRESS
));
109 AccessPoint
->RemotePort
= NTOHS (Tcb
->RemoteEnd
.Port
);
110 AccessPoint
->ActiveFlag
= (BOOLEAN
) (Tcb
->State
!= TCP_LISTEN
);
112 if (Option
!= NULL
) {
113 Option
->ReceiveBufferSize
= GET_RCV_BUFFSIZE (Tcb
->Sk
);
114 Option
->SendBufferSize
= GET_SND_BUFFSIZE (Tcb
->Sk
);
115 Option
->MaxSynBackLog
= GET_BACKLOG (Tcb
->Sk
);
117 Option
->ConnectionTimeout
= Tcb
->ConnectTimeout
/ TCP_TICK_HZ
;
118 Option
->DataRetries
= Tcb
->MaxRexmit
;
119 Option
->FinTimeout
= Tcb
->FinWait2Timeout
/ TCP_TICK_HZ
;
120 Option
->TimeWaitTimeout
= Tcb
->TimeWaitTimeout
/ TCP_TICK_HZ
;
121 Option
->KeepAliveProbes
= Tcb
->MaxKeepAlive
;
122 Option
->KeepAliveTime
= Tcb
->KeepAliveIdle
/ TCP_TICK_HZ
;
123 Option
->KeepAliveInterval
= Tcb
->KeepAlivePeriod
/ TCP_TICK_HZ
;
125 Option
->EnableNagle
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
));
126 Option
->EnableTimeStamp
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
));
127 Option
->EnableWindowScaling
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
));
129 Option
->EnableSelectiveAck
= FALSE
;
130 Option
->EnablePathMtuDiscovery
= FALSE
;
134 Ip
= Tcb
->IpInfo
->Ip
;
137 return Ip
->GetModeData (Ip
, Mode
->Ip4ModeData
, Mode
->MnpConfigData
, Mode
->SnpModeData
);
142 If AP->StationPort isn't zero, check whether the access point
143 is registered, else generate a random station port for this
146 @param AP Pointer to the access point.
148 @retval EFI_SUCCESS The check is passed or the port is assigned.
149 @retval EFI_INVALID_PARAMETER The non-zero station port is already used.
150 @retval EFI_OUT_OF_RESOURCES No port can be allocated.
155 IN EFI_TCP4_ACCESS_POINT
*AP
160 if (0 != AP
->StationPort
) {
162 // check if a same endpoint is bound
164 if (TcpFindTcbByPeer (&AP
->StationAddress
, AP
->StationPort
)) {
166 return EFI_INVALID_PARAMETER
;
170 // generate a random port
174 if (TCP4_PORT_USER_RESERVED
== mTcp4RandomPort
) {
175 mTcp4RandomPort
= TCP4_PORT_KNOWN
;
180 while (TcpFindTcbByPeer (&AP
->StationAddress
, mTcp4RandomPort
)) {
184 if (mTcp4RandomPort
<= TCP4_PORT_KNOWN
) {
187 DEBUG ((EFI_D_ERROR
, "Tcp4Bind: no port can be allocated "
190 return EFI_OUT_OF_RESOURCES
;
193 mTcp4RandomPort
= TCP4_PORT_KNOWN
+ 1;
200 AP
->StationPort
= mTcp4RandomPort
;
208 Flush the Tcb add its associated protocols.
210 @param Tcb Pointer to the TCP_CB to be flushed.
219 TCP4_PROTO_DATA
*TcpProto
;
221 IpIoConfigIp (Tcb
->IpInfo
, NULL
);
224 TcpProto
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
226 if (SOCK_IS_CONFIGURED (Sock
)) {
227 RemoveEntryList (&Tcb
->List
);
230 // Uninstall the device path protocol.
232 if (Sock
->DevicePath
!= NULL
) {
233 gBS
->UninstallProtocolInterface (
235 &gEfiDevicePathProtocolGuid
,
238 FreePool (Sock
->DevicePath
);
241 TcpSetVariableData (TcpProto
->TcpService
);
244 NetbufFreeList (&Tcb
->SndQue
);
245 NetbufFreeList (&Tcb
->RcvQue
);
249 Attach a Pcb to the socket.
251 @param Sk Pointer to the socket of this TCP instance.
253 @retval EFI_SUCCESS The operation is completed successfully.
254 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
263 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
;
288 InitializeListHead (&Tcb
->List
);
289 InitializeListHead (&Tcb
->SndQue
);
290 InitializeListHead (&Tcb
->RcvQue
);
292 Tcb
->State
= TCP_CLOSED
;
294 ProtoData
->TcpPcb
= Tcb
;
300 Detach the Pcb of the socket.
302 @param Sk Pointer to the socket of this TCP instance.
310 TCP4_PROTO_DATA
*ProtoData
;
313 ProtoData
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
314 Tcb
= ProtoData
->TcpPcb
;
316 ASSERT (Tcb
!= NULL
);
320 IpIoRemoveIp (ProtoData
->TcpService
->IpIo
, Tcb
->IpInfo
);
324 ProtoData
->TcpPcb
= NULL
;
329 Configure the Pcb using CfgData.
331 @param Sk Pointer to the socket of this TCP instance.
332 @param CfgData Pointer to the TCP configuration data.
334 @retval EFI_SUCCESS The operation is completed successfully.
335 @retval EFI_INVALID_PARAMETER A same access point has been configured in
336 another TCP instance.
337 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
343 IN EFI_TCP4_CONFIG_DATA
*CfgData
346 EFI_IP4_CONFIG_DATA IpCfgData
;
348 EFI_TCP4_OPTION
*Option
;
349 TCP4_PROTO_DATA
*TcpProto
;
352 ASSERT ((CfgData
!= NULL
) && (Sk
!= NULL
) && (Sk
->SockHandle
!= NULL
));
354 TcpProto
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
355 Tcb
= TcpProto
->TcpPcb
;
357 ASSERT (Tcb
!= NULL
);
360 // Add Ip for send pkt to the peer
362 CopyMem (&IpCfgData
, &mIp4IoDefaultIpConfigData
, sizeof (IpCfgData
));
363 IpCfgData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
364 IpCfgData
.UseDefaultAddress
= CfgData
->AccessPoint
.UseDefaultAddress
;
365 IpCfgData
.StationAddress
= CfgData
->AccessPoint
.StationAddress
;
366 IpCfgData
.SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
367 IpCfgData
.ReceiveTimeout
= (UINT32
) (-1);
370 // Configure the IP instance this Tcb consumes.
372 Status
= IpIoConfigIp (Tcb
->IpInfo
, &IpCfgData
);
373 if (EFI_ERROR (Status
)) {
378 // Get the default address info if the instance is configured to use default address.
380 if (CfgData
->AccessPoint
.UseDefaultAddress
) {
381 CfgData
->AccessPoint
.StationAddress
= IpCfgData
.StationAddress
;
382 CfgData
->AccessPoint
.SubnetMask
= IpCfgData
.SubnetMask
;
386 // check if we can bind this endpoint in CfgData
388 Status
= Tcp4Bind (&(CfgData
->AccessPoint
));
390 if (EFI_ERROR (Status
)) {
391 DEBUG ((EFI_D_ERROR
, "Tcp4ConfigurePcb: Bind endpoint failed "
392 "with %r\n", Status
));
398 // Initalize the operating information in this Tcb
400 ASSERT (Tcb
->State
== TCP_CLOSED
&&
401 IsListEmpty (&Tcb
->SndQue
) &&
402 IsListEmpty (&Tcb
->RcvQue
));
404 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
405 Tcb
->State
= TCP_CLOSED
;
408 Tcb
->RcvMss
= TcpGetRcvMss (Sk
);
411 Tcb
->Rto
= 3 * TCP_TICK_HZ
;
413 Tcb
->CWnd
= Tcb
->SndMss
;
414 Tcb
->Ssthresh
= 0xffffffff;
416 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
418 Tcb
->KeepAliveIdle
= TCP_KEEPALIVE_IDLE_MIN
;
419 Tcb
->KeepAlivePeriod
= TCP_KEEPALIVE_PERIOD
;
420 Tcb
->MaxKeepAlive
= TCP_MAX_KEEPALIVE
;
421 Tcb
->MaxRexmit
= TCP_MAX_LOSS
;
422 Tcb
->FinWait2Timeout
= TCP_FIN_WAIT2_TIME
;
423 Tcb
->TimeWaitTimeout
= TCP_TIME_WAIT_TIME
;
424 Tcb
->ConnectTimeout
= TCP_CONNECT_TIME
;
427 // initialize Tcb in the light of CfgData
429 Tcb
->Ttl
= CfgData
->TimeToLive
;
430 Tcb
->Tos
= CfgData
->TypeOfService
;
432 Tcb
->UseDefaultAddr
= CfgData
->AccessPoint
.UseDefaultAddress
;
434 CopyMem (&Tcb
->LocalEnd
.Ip
, &CfgData
->AccessPoint
.StationAddress
, sizeof (IP4_ADDR
));
435 Tcb
->LocalEnd
.Port
= HTONS (CfgData
->AccessPoint
.StationPort
);
436 Tcb
->SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
438 if (CfgData
->AccessPoint
.ActiveFlag
) {
439 CopyMem (&Tcb
->RemoteEnd
.Ip
, &CfgData
->AccessPoint
.RemoteAddress
, sizeof (IP4_ADDR
));
440 Tcb
->RemoteEnd
.Port
= HTONS (CfgData
->AccessPoint
.RemotePort
);
442 Tcb
->RemoteEnd
.Ip
= 0;
443 Tcb
->RemoteEnd
.Port
= 0;
446 Option
= CfgData
->ControlOption
;
448 if (Option
!= NULL
) {
451 (UINT32
) (TCP_COMP_VAL (
452 TCP_RCV_BUF_SIZE_MIN
,
455 Option
->ReceiveBufferSize
461 (UINT32
) (TCP_COMP_VAL (
462 TCP_SND_BUF_SIZE_MIN
,
465 Option
->SendBufferSize
472 (UINT32
) (TCP_COMP_VAL (
476 Option
->MaxSynBackLog
481 Tcb
->MaxRexmit
= (UINT16
) TCP_COMP_VAL (
487 Tcb
->FinWait2Timeout
= TCP_COMP_VAL (
489 TCP_FIN_WAIT2_TIME_MAX
,
491 (UINT32
) (Option
->FinTimeout
* TCP_TICK_HZ
)
494 if (Option
->TimeWaitTimeout
!= 0) {
495 Tcb
->TimeWaitTimeout
= TCP_COMP_VAL (
497 TCP_TIME_WAIT_TIME_MAX
,
499 (UINT32
) (Option
->TimeWaitTimeout
* TCP_TICK_HZ
)
502 Tcb
->TimeWaitTimeout
= 0;
505 if (Option
->KeepAliveProbes
!= 0) {
506 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
508 Tcb
->MaxKeepAlive
= (UINT8
) TCP_COMP_VAL (
509 TCP_MAX_KEEPALIVE_MIN
,
512 Option
->KeepAliveProbes
514 Tcb
->KeepAliveIdle
= TCP_COMP_VAL (
515 TCP_KEEPALIVE_IDLE_MIN
,
516 TCP_KEEPALIVE_IDLE_MAX
,
517 TCP_KEEPALIVE_IDLE_MIN
,
518 (UINT32
) (Option
->KeepAliveTime
* TCP_TICK_HZ
)
520 Tcb
->KeepAlivePeriod
= TCP_COMP_VAL (
521 TCP_KEEPALIVE_PERIOD_MIN
,
522 TCP_KEEPALIVE_PERIOD
,
523 TCP_KEEPALIVE_PERIOD
,
524 (UINT32
) (Option
->KeepAliveInterval
* TCP_TICK_HZ
)
528 Tcb
->ConnectTimeout
= TCP_COMP_VAL (
529 TCP_CONNECT_TIME_MIN
,
532 (UINT32
) (Option
->ConnectionTimeout
* TCP_TICK_HZ
)
535 if (!Option
->EnableNagle
) {
536 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
);
539 if (!Option
->EnableTimeStamp
) {
540 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
);
543 if (!Option
->EnableWindowScaling
) {
544 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
);
549 // The socket is bound, the <SrcIp, SrcPort, DstIp, DstPort> is
550 // determined, construct the IP device path and install it.
552 Status
= TcpInstallDevicePath (Sk
);
553 if (EFI_ERROR (Status
)) {
558 // update state of Tcb and socket
560 if (!CfgData
->AccessPoint
.ActiveFlag
) {
562 TcpSetState (Tcb
, TCP_LISTEN
);
563 SockSetState (Sk
, SO_LISTENING
);
565 Sk
->ConfigureState
= SO_CONFIGURED_PASSIVE
;
568 Sk
->ConfigureState
= SO_CONFIGURED_ACTIVE
;
580 The procotol handler provided to the socket layer, used to
581 dispatch the socket level requests by calling the corresponding
584 @param Sock Pointer to the socket of this TCP instance.
585 @param Request The code of this operation request.
586 @param Data Pointer to the operation specific data passed in
587 together with the operation request.
589 @retval EFI_SUCCESS The socket request is completed successfully.
590 @retval other The error status returned by the corresponding TCP
598 IN VOID
*Data OPTIONAL
602 TCP4_PROTO_DATA
*ProtoData
;
603 EFI_IP4_PROTOCOL
*Ip
;
605 ProtoData
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
606 Tcb
= ProtoData
->TcpPcb
;
610 Ip
= (EFI_IP4_PROTOCOL
*) (ProtoData
->TcpService
->IpIo
->Ip
);
616 // After user received data from socket buffer, socket will
617 // notify TCP using this message to give it a chance to send out
618 // window update information
620 ASSERT (Tcb
!= NULL
);
621 TcpOnAppConsume (Tcb
);
626 ASSERT (Tcb
!= NULL
);
643 Tcb
->SndPsh
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
);
644 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
);
649 Tcb
->SndUp
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
) - 1;
650 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
);
656 TcpOnAppConnect (Tcb
);
662 return Tcp4AttachPcb (Sock
);
674 Tcp4DetachPcb (Sock
);
680 return Tcp4ConfigurePcb (
682 (EFI_TCP4_CONFIG_DATA
*) Data
689 ASSERT ((Data
!= NULL
) && (Tcb
!= NULL
));
691 return Tcp4GetMode (Tcb
, (TCP4_MODE_DATA
*) Data
);
697 ASSERT ((Data
!= NULL
) && (Tcb
!= NULL
));
699 return Tcp4Route (Tcb
, (TCP4_ROUTE_INFO
*) Data
);
702 return EFI_UNSUPPORTED
;