2 Tcp request dispatcher implementation.
4 (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2014, 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
);
337 // Close the IP protocol.
340 Tcb
->IpInfo
->ChildHandle
,
341 &gEfiIp4ProtocolGuid
,
342 ProtoData
->TcpService
->IpIo
->Image
,
346 IpIoRemoveIp (ProtoData
->TcpService
->IpIo
, Tcb
->IpInfo
);
350 ProtoData
->TcpPcb
= NULL
;
355 Configure the Pcb using CfgData.
357 @param Sk Pointer to the socket of this TCP instance.
358 @param CfgData Pointer to the TCP configuration data.
360 @retval EFI_SUCCESS The operation is completed successfully.
361 @retval EFI_INVALID_PARAMETER A same access point has been configured in
362 another TCP instance.
363 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
369 IN EFI_TCP4_CONFIG_DATA
*CfgData
372 EFI_IP4_CONFIG_DATA IpCfgData
;
374 EFI_TCP4_OPTION
*Option
;
375 TCP4_PROTO_DATA
*TcpProto
;
378 ASSERT ((CfgData
!= NULL
) && (Sk
!= NULL
) && (Sk
->SockHandle
!= NULL
));
380 TcpProto
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
381 Tcb
= TcpProto
->TcpPcb
;
383 ASSERT (Tcb
!= NULL
);
386 // Add Ip for send pkt to the peer
388 CopyMem (&IpCfgData
, &mIp4IoDefaultIpConfigData
, sizeof (IpCfgData
));
389 IpCfgData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
390 IpCfgData
.UseDefaultAddress
= CfgData
->AccessPoint
.UseDefaultAddress
;
391 IpCfgData
.StationAddress
= CfgData
->AccessPoint
.StationAddress
;
392 IpCfgData
.SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
393 IpCfgData
.ReceiveTimeout
= (UINT32
) (-1);
396 // Configure the IP instance this Tcb consumes.
398 Status
= IpIoConfigIp (Tcb
->IpInfo
, &IpCfgData
);
399 if (EFI_ERROR (Status
)) {
404 // Get the default address info if the instance is configured to use default address.
406 if (CfgData
->AccessPoint
.UseDefaultAddress
) {
407 CfgData
->AccessPoint
.StationAddress
= IpCfgData
.StationAddress
;
408 CfgData
->AccessPoint
.SubnetMask
= IpCfgData
.SubnetMask
;
412 // check if we can bind this endpoint in CfgData
414 Status
= Tcp4Bind (&(CfgData
->AccessPoint
));
416 if (EFI_ERROR (Status
)) {
417 DEBUG ((EFI_D_ERROR
, "Tcp4ConfigurePcb: Bind endpoint failed "
418 "with %r\n", Status
));
424 // Initalize the operating information in this Tcb
426 ASSERT (Tcb
->State
== TCP_CLOSED
&&
427 IsListEmpty (&Tcb
->SndQue
) &&
428 IsListEmpty (&Tcb
->RcvQue
));
430 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
431 Tcb
->State
= TCP_CLOSED
;
434 Tcb
->RcvMss
= TcpGetRcvMss (Sk
);
437 Tcb
->Rto
= 3 * TCP_TICK_HZ
;
439 Tcb
->CWnd
= Tcb
->SndMss
;
440 Tcb
->Ssthresh
= 0xffffffff;
442 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
444 Tcb
->KeepAliveIdle
= TCP_KEEPALIVE_IDLE_MIN
;
445 Tcb
->KeepAlivePeriod
= TCP_KEEPALIVE_PERIOD
;
446 Tcb
->MaxKeepAlive
= TCP_MAX_KEEPALIVE
;
447 Tcb
->MaxRexmit
= TCP_MAX_LOSS
;
448 Tcb
->FinWait2Timeout
= TCP_FIN_WAIT2_TIME
;
449 Tcb
->TimeWaitTimeout
= TCP_TIME_WAIT_TIME
;
450 Tcb
->ConnectTimeout
= TCP_CONNECT_TIME
;
453 // initialize Tcb in the light of CfgData
455 Tcb
->Ttl
= CfgData
->TimeToLive
;
456 Tcb
->Tos
= CfgData
->TypeOfService
;
458 Tcb
->UseDefaultAddr
= CfgData
->AccessPoint
.UseDefaultAddress
;
460 CopyMem (&Tcb
->LocalEnd
.Ip
, &CfgData
->AccessPoint
.StationAddress
, sizeof (IP4_ADDR
));
461 Tcb
->LocalEnd
.Port
= HTONS (CfgData
->AccessPoint
.StationPort
);
462 IP4_COPY_ADDRESS (&Tcb
->SubnetMask
, &CfgData
->AccessPoint
.SubnetMask
);
464 if (CfgData
->AccessPoint
.ActiveFlag
) {
465 CopyMem (&Tcb
->RemoteEnd
.Ip
, &CfgData
->AccessPoint
.RemoteAddress
, sizeof (IP4_ADDR
));
466 Tcb
->RemoteEnd
.Port
= HTONS (CfgData
->AccessPoint
.RemotePort
);
468 Tcb
->RemoteEnd
.Ip
= 0;
469 Tcb
->RemoteEnd
.Port
= 0;
472 Option
= CfgData
->ControlOption
;
474 if (Option
!= NULL
) {
477 (UINT32
) (TCP_COMP_VAL (
478 TCP_RCV_BUF_SIZE_MIN
,
481 Option
->ReceiveBufferSize
487 (UINT32
) (TCP_COMP_VAL (
488 TCP_SND_BUF_SIZE_MIN
,
491 Option
->SendBufferSize
498 (UINT32
) (TCP_COMP_VAL (
502 Option
->MaxSynBackLog
507 Tcb
->MaxRexmit
= (UINT16
) TCP_COMP_VAL (
513 Tcb
->FinWait2Timeout
= TCP_COMP_VAL (
515 TCP_FIN_WAIT2_TIME_MAX
,
517 (UINT32
) (Option
->FinTimeout
* TCP_TICK_HZ
)
520 if (Option
->TimeWaitTimeout
!= 0) {
521 Tcb
->TimeWaitTimeout
= TCP_COMP_VAL (
523 TCP_TIME_WAIT_TIME_MAX
,
525 (UINT32
) (Option
->TimeWaitTimeout
* TCP_TICK_HZ
)
528 Tcb
->TimeWaitTimeout
= 0;
531 if (Option
->KeepAliveProbes
!= 0) {
532 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
534 Tcb
->MaxKeepAlive
= (UINT8
) TCP_COMP_VAL (
535 TCP_MAX_KEEPALIVE_MIN
,
538 Option
->KeepAliveProbes
540 Tcb
->KeepAliveIdle
= TCP_COMP_VAL (
541 TCP_KEEPALIVE_IDLE_MIN
,
542 TCP_KEEPALIVE_IDLE_MAX
,
543 TCP_KEEPALIVE_IDLE_MIN
,
544 (UINT32
) (Option
->KeepAliveTime
* TCP_TICK_HZ
)
546 Tcb
->KeepAlivePeriod
= TCP_COMP_VAL (
547 TCP_KEEPALIVE_PERIOD_MIN
,
548 TCP_KEEPALIVE_PERIOD
,
549 TCP_KEEPALIVE_PERIOD
,
550 (UINT32
) (Option
->KeepAliveInterval
* TCP_TICK_HZ
)
554 Tcb
->ConnectTimeout
= TCP_COMP_VAL (
555 TCP_CONNECT_TIME_MIN
,
558 (UINT32
) (Option
->ConnectionTimeout
* TCP_TICK_HZ
)
561 if (!Option
->EnableNagle
) {
562 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
);
565 if (!Option
->EnableTimeStamp
) {
566 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
);
569 if (!Option
->EnableWindowScaling
) {
570 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
);
575 // The socket is bound, the <SrcIp, SrcPort, DstIp, DstPort> is
576 // determined, construct the IP device path and install it.
578 Status
= TcpInstallDevicePath (Sk
);
579 if (EFI_ERROR (Status
)) {
584 // update state of Tcb and socket
586 if (!CfgData
->AccessPoint
.ActiveFlag
) {
588 TcpSetState (Tcb
, TCP_LISTEN
);
589 SockSetState (Sk
, SO_LISTENING
);
591 Sk
->ConfigureState
= SO_CONFIGURED_PASSIVE
;
594 Sk
->ConfigureState
= SO_CONFIGURED_ACTIVE
;
606 The procotol handler provided to the socket layer, used to
607 dispatch the socket level requests by calling the corresponding
610 @param Sock Pointer to the socket of this TCP instance.
611 @param Request The code of this operation request.
612 @param Data Pointer to the operation specific data passed in
613 together with the operation request.
615 @retval EFI_SUCCESS The socket request is completed successfully.
616 @retval other The error status returned by the corresponding TCP
624 IN VOID
*Data OPTIONAL
628 TCP4_PROTO_DATA
*ProtoData
;
629 EFI_IP4_PROTOCOL
*Ip
;
631 ProtoData
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
632 Tcb
= ProtoData
->TcpPcb
;
636 Ip
= ProtoData
->TcpService
->IpIo
->Ip
.Ip4
;
642 // After user received data from socket buffer, socket will
643 // notify TCP using this message to give it a chance to send out
644 // window update information
646 ASSERT (Tcb
!= NULL
);
647 TcpOnAppConsume (Tcb
);
652 ASSERT (Tcb
!= NULL
);
669 Tcb
->SndPsh
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
);
670 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
);
675 Tcb
->SndUp
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
) - 1;
676 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
);
682 TcpOnAppConnect (Tcb
);
688 return Tcp4AttachPcb (Sock
);
698 Tcp4DetachPcb (Sock
);
704 return Tcp4ConfigurePcb (
706 (EFI_TCP4_CONFIG_DATA
*) Data
711 ASSERT ((Data
!= NULL
) && (Tcb
!= NULL
));
713 return Tcp4GetMode (Tcb
, (TCP4_MODE_DATA
*) Data
);
717 ASSERT ((Data
!= NULL
) && (Tcb
!= NULL
));
719 return Tcp4Route (Tcb
, (TCP4_ROUTE_INFO
*) Data
);
722 return EFI_UNSUPPORTED
;