3 Copyright (c) 2005 - 2006, Intel Corporation<BR>
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php<BR>
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #define TCP_COMP_VAL(Min, Max, Default, Val) \
17 ((((Val) <= (Max)) && ((Val) >= (Min))) ? (Val) : (Default))
20 Add or remove a route entry in the IP route table associated with this TCP instance.
22 @param Tcb Pointer to the TCP_CB of this TCP instance.
23 @param RouteInfo Pointer to the route info to be processed.
25 @retval EFI_SUCCESS The operation completed successfully.
26 @retval EFI_NOT_STARTED The driver instance has not been started.
27 @retval EFI_NO_MAPPING When using the default address, configuration(DHCP,
28 BOOTP, RARP, etc.) is not finished yet.
29 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
30 @retval EFI_NOT_FOUND This route is not in the routing table
31 (when RouteInfo->DeleteRoute is TRUE).
32 @retval EFI_ACCESS_DENIED The route is already defined in the routing table
33 (when RouteInfo->DeleteRoute is FALSE).
38 IN TCP4_ROUTE_INFO
*RouteInfo
49 RouteInfo
->DeleteRoute
,
50 RouteInfo
->SubnetAddress
,
51 RouteInfo
->SubnetMask
,
52 RouteInfo
->GatewayAddress
59 Get the operational settings of this TCP instance.
61 @param Tcb Pointer to the TCP_CB of this TCP instance.
62 @param Mode Pointer to the buffer to store the operational
65 @retval EFI_SUCCESS The mode data is read.
66 @retval EFI_NOT_STARTED No configuration data is available because this
67 instance hasn't been started.
73 IN OUT TCP4_MODE_DATA
*Mode
77 EFI_TCP4_CONFIG_DATA
*ConfigData
;
78 EFI_TCP4_ACCESS_POINT
*AccessPoint
;
79 EFI_TCP4_OPTION
*Option
;
84 if (!SOCK_IS_CONFIGURED (Sock
) && (Mode
->Tcp4ConfigData
!= NULL
)) {
85 return EFI_NOT_STARTED
;
88 if (Mode
->Tcp4State
!= NULL
) {
89 *(Mode
->Tcp4State
) = (EFI_TCP4_CONNECTION_STATE
) Tcb
->State
;
92 if (Mode
->Tcp4ConfigData
!= NULL
) {
94 ConfigData
= Mode
->Tcp4ConfigData
;
95 AccessPoint
= &(ConfigData
->AccessPoint
);
96 Option
= ConfigData
->ControlOption
;
98 ConfigData
->TypeOfService
= Tcb
->TOS
;
99 ConfigData
->TimeToLive
= Tcb
->TTL
;
101 AccessPoint
->UseDefaultAddress
= Tcb
->UseDefaultAddr
;
103 CopyMem (&AccessPoint
->StationAddress
, &Tcb
->LocalEnd
.Ip
, sizeof (EFI_IPv4_ADDRESS
));
104 AccessPoint
->SubnetMask
= Tcb
->SubnetMask
;
105 AccessPoint
->StationPort
= NTOHS (Tcb
->LocalEnd
.Port
);
107 CopyMem (&AccessPoint
->RemoteAddress
, &Tcb
->RemoteEnd
.Ip
, sizeof (EFI_IPv4_ADDRESS
));
108 AccessPoint
->RemotePort
= NTOHS (Tcb
->RemoteEnd
.Port
);
109 AccessPoint
->ActiveFlag
= (BOOLEAN
) (Tcb
->State
!= TCP_LISTEN
);
111 if (Option
!= NULL
) {
112 Option
->ReceiveBufferSize
= GET_RCV_BUFFSIZE (Tcb
->Sk
);
113 Option
->SendBufferSize
= GET_SND_BUFFSIZE (Tcb
->Sk
);
114 Option
->MaxSynBackLog
= GET_BACKLOG (Tcb
->Sk
);
116 Option
->ConnectionTimeout
= Tcb
->ConnectTimeout
/ TCP_TICK_HZ
;
117 Option
->DataRetries
= Tcb
->MaxRexmit
;
118 Option
->FinTimeout
= Tcb
->FinWait2Timeout
/ TCP_TICK_HZ
;
119 Option
->TimeWaitTimeout
= Tcb
->TimeWaitTimeout
/ TCP_TICK_HZ
;
120 Option
->KeepAliveProbes
= Tcb
->MaxKeepAlive
;
121 Option
->KeepAliveTime
= Tcb
->KeepAliveIdle
/ TCP_TICK_HZ
;
122 Option
->KeepAliveInterval
= Tcb
->KeepAlivePeriod
/ TCP_TICK_HZ
;
124 Option
->EnableNagle
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
));
125 Option
->EnableTimeStamp
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
));
126 Option
->EnableWindowScaling
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
));
128 Option
->EnableSelectiveAck
= FALSE
;
129 Option
->EnablePathMtuDiscovery
= FALSE
;
133 Ip
= Tcb
->IpInfo
->Ip
;
136 return Ip
->GetModeData (Ip
, Mode
->Ip4ModeData
, Mode
->MnpConfigData
, Mode
->SnpModeData
);
141 If AP->StationPort isn't zero, check whether the access point
142 is registered, else generate a random station port for this
145 @param AP Pointer to the access point.
147 @retval EFI_SUCCESS The check is passed or the port is assigned.
148 @retval EFI_INVALID_PARAMETER The non-zero station port is already used.
149 @retval EFI_OUT_OF_RESOURCES No port can be allocated.
154 IN EFI_TCP4_ACCESS_POINT
*AP
159 if (0 != AP
->StationPort
) {
161 // check if a same endpoint is bound
163 if (TcpFindTcbByPeer (&AP
->StationAddress
, AP
->StationPort
)) {
165 return EFI_INVALID_PARAMETER
;
169 // generate a random port
173 if (TCP4_PORT_USER_RESERVED
== mTcp4RandomPort
) {
174 mTcp4RandomPort
= TCP4_PORT_KNOWN
;
179 while (TcpFindTcbByPeer (&AP
->StationAddress
, mTcp4RandomPort
)) {
183 if (mTcp4RandomPort
<= TCP4_PORT_KNOWN
) {
186 DEBUG ((EFI_D_ERROR
, "Tcp4Bind: no port can be allocated "
189 return EFI_OUT_OF_RESOURCES
;
192 mTcp4RandomPort
= TCP4_PORT_KNOWN
+ 1;
199 AP
->StationPort
= mTcp4RandomPort
;
207 Flush the Tcb add its associated protocols.
209 @param Tcb Pointer to the TCP_CB to be flushed.
218 TCP4_PROTO_DATA
*TcpProto
;
220 IpIoConfigIp (Tcb
->IpInfo
, NULL
);
223 TcpProto
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
225 if (SOCK_IS_CONFIGURED (Sock
)) {
226 RemoveEntryList (&Tcb
->List
);
229 // Uninstall the device path protocl.
231 gBS
->UninstallProtocolInterface (
233 &gEfiDevicePathProtocolGuid
,
236 gBS
->FreePool (Sock
->DevicePath
);
238 TcpSetVariableData (TcpProto
->TcpService
);
241 NetbufFreeList (&Tcb
->SndQue
);
242 NetbufFreeList (&Tcb
->RcvQue
);
246 Attach a Pcb to the socket.
248 @param Sk Pointer to the socket of this TCP instance.
250 @retval EFI_SUCCESS The operation is completed successfully.
251 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
260 TCP4_PROTO_DATA
*ProtoData
;
263 Tcb
= AllocateZeroPool (sizeof (TCP_CB
));
267 DEBUG ((EFI_D_ERROR
, "Tcp4ConfigurePcb: failed to allocate a TCB\n"));
269 return EFI_OUT_OF_RESOURCES
;
272 ProtoData
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
273 IpIo
= ProtoData
->TcpService
->IpIo
;
276 // Create an IpInfo for this Tcb.
278 Tcb
->IpInfo
= IpIoAddIp (IpIo
);
279 if (Tcb
->IpInfo
== NULL
) {
282 return EFI_OUT_OF_RESOURCES
;
285 InitializeListHead (&Tcb
->List
);
286 InitializeListHead (&Tcb
->SndQue
);
287 InitializeListHead (&Tcb
->RcvQue
);
289 Tcb
->State
= TCP_CLOSED
;
291 ProtoData
->TcpPcb
= Tcb
;
297 Detach the Pcb of the socket.
299 @param Sk Pointer to the socket of this TCP instance.
307 TCP4_PROTO_DATA
*ProtoData
;
310 ProtoData
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
311 Tcb
= ProtoData
->TcpPcb
;
313 ASSERT (Tcb
!= NULL
);
317 IpIoRemoveIp (ProtoData
->TcpService
->IpIo
, Tcb
->IpInfo
);
321 ProtoData
->TcpPcb
= NULL
;
326 Configure the Pcb using CfgData.
328 @param Sk Pointer to the socket of this TCP instance.
329 @param CfgData Pointer to the TCP configuration data.
331 @retval EFI_SUCCESS The operation is completed successfully.
332 @retval EFI_INVALID_PARAMETER A same access point has been configured in
333 another TCP instance.
334 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
340 IN EFI_TCP4_CONFIG_DATA
*CfgData
343 EFI_IP4_CONFIG_DATA IpCfgData
;
345 EFI_TCP4_OPTION
*Option
;
346 TCP4_PROTO_DATA
*TcpProto
;
349 ASSERT ((CfgData
!= NULL
) && (Sk
!= NULL
) && (Sk
->SockHandle
!= NULL
));
351 TcpProto
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
352 Tcb
= TcpProto
->TcpPcb
;
354 ASSERT (Tcb
!= NULL
);
357 // Add Ip for send pkt to the peer
359 CopyMem (&IpCfgData
, &mIpIoDefaultIpConfigData
, sizeof (IpCfgData
));
360 IpCfgData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
361 IpCfgData
.UseDefaultAddress
= CfgData
->AccessPoint
.UseDefaultAddress
;
362 IpCfgData
.StationAddress
= CfgData
->AccessPoint
.StationAddress
;
363 IpCfgData
.SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
364 IpCfgData
.ReceiveTimeout
= (UINT32
) (-1);
367 // Configure the IP instance this Tcb consumes.
369 Status
= IpIoConfigIp (Tcb
->IpInfo
, &IpCfgData
);
370 if (EFI_ERROR (Status
)) {
375 // Get the default address info if the instance is configured to use default address.
377 if (CfgData
->AccessPoint
.UseDefaultAddress
) {
378 CfgData
->AccessPoint
.StationAddress
= IpCfgData
.StationAddress
;
379 CfgData
->AccessPoint
.SubnetMask
= IpCfgData
.SubnetMask
;
383 // check if we can bind this endpoint in CfgData
385 Status
= Tcp4Bind (&(CfgData
->AccessPoint
));
387 if (EFI_ERROR (Status
)) {
388 DEBUG ((EFI_D_ERROR
, "Tcp4ConfigurePcb: Bind endpoint failed "
389 "with %r\n", Status
));
395 // Initalize the operating information in this Tcb
397 ASSERT (Tcb
->State
== TCP_CLOSED
&&
398 IsListEmpty (&Tcb
->SndQue
) &&
399 IsListEmpty (&Tcb
->RcvQue
));
401 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
402 Tcb
->State
= TCP_CLOSED
;
405 Tcb
->RcvMss
= TcpGetRcvMss (Sk
);
408 Tcb
->Rto
= 3 * TCP_TICK_HZ
;
410 Tcb
->CWnd
= Tcb
->SndMss
;
411 Tcb
->Ssthresh
= 0xffffffff;
413 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
415 Tcb
->KeepAliveIdle
= TCP_KEEPALIVE_IDLE_MIN
;
416 Tcb
->KeepAlivePeriod
= TCP_KEEPALIVE_PERIOD
;
417 Tcb
->MaxKeepAlive
= TCP_MAX_KEEPALIVE
;
418 Tcb
->MaxRexmit
= TCP_MAX_LOSS
;
419 Tcb
->FinWait2Timeout
= TCP_FIN_WAIT2_TIME
;
420 Tcb
->TimeWaitTimeout
= TCP_TIME_WAIT_TIME
;
421 Tcb
->ConnectTimeout
= TCP_CONNECT_TIME
;
424 // initialize Tcb in the light of CfgData
426 Tcb
->TTL
= CfgData
->TimeToLive
;
427 Tcb
->TOS
= CfgData
->TypeOfService
;
429 Tcb
->UseDefaultAddr
= CfgData
->AccessPoint
.UseDefaultAddress
;
431 CopyMem (&Tcb
->LocalEnd
.Ip
, &CfgData
->AccessPoint
.StationAddress
, sizeof (IP4_ADDR
));
432 Tcb
->LocalEnd
.Port
= HTONS (CfgData
->AccessPoint
.StationPort
);
433 Tcb
->SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
435 if (CfgData
->AccessPoint
.ActiveFlag
) {
436 CopyMem (&Tcb
->RemoteEnd
.Ip
, &CfgData
->AccessPoint
.RemoteAddress
, sizeof (IP4_ADDR
));
437 Tcb
->RemoteEnd
.Port
= HTONS (CfgData
->AccessPoint
.RemotePort
);
439 Tcb
->RemoteEnd
.Ip
= 0;
440 Tcb
->RemoteEnd
.Port
= 0;
443 Option
= CfgData
->ControlOption
;
445 if (Option
!= NULL
) {
448 (UINT32
) (TCP_COMP_VAL (
449 TCP_RCV_BUF_SIZE_MIN
,
452 Option
->ReceiveBufferSize
458 (UINT32
) (TCP_COMP_VAL (
459 TCP_SND_BUF_SIZE_MIN
,
462 Option
->SendBufferSize
469 (UINT32
) (TCP_COMP_VAL (
473 Option
->MaxSynBackLog
478 Tcb
->MaxRexmit
= (UINT16
) TCP_COMP_VAL (
484 Tcb
->FinWait2Timeout
= TCP_COMP_VAL (
486 TCP_FIN_WAIT2_TIME_MAX
,
488 (UINT32
) (Option
->FinTimeout
* TCP_TICK_HZ
)
491 if (Option
->TimeWaitTimeout
!= 0) {
492 Tcb
->TimeWaitTimeout
= TCP_COMP_VAL (
494 TCP_TIME_WAIT_TIME_MAX
,
496 (UINT32
) (Option
->TimeWaitTimeout
* TCP_TICK_HZ
)
499 Tcb
->TimeWaitTimeout
= 0;
502 if (Option
->KeepAliveProbes
!= 0) {
503 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
505 Tcb
->MaxKeepAlive
= (UINT8
) TCP_COMP_VAL (
506 TCP_MAX_KEEPALIVE_MIN
,
509 Option
->KeepAliveProbes
511 Tcb
->KeepAliveIdle
= TCP_COMP_VAL (
512 TCP_KEEPALIVE_IDLE_MIN
,
513 TCP_KEEPALIVE_IDLE_MAX
,
514 TCP_KEEPALIVE_IDLE_MIN
,
515 (UINT32
) (Option
->KeepAliveTime
* TCP_TICK_HZ
)
517 Tcb
->KeepAlivePeriod
= TCP_COMP_VAL (
518 TCP_KEEPALIVE_PERIOD_MIN
,
519 TCP_KEEPALIVE_PERIOD
,
520 TCP_KEEPALIVE_PERIOD
,
521 (UINT32
) (Option
->KeepAliveInterval
* TCP_TICK_HZ
)
525 Tcb
->ConnectTimeout
= TCP_COMP_VAL (
526 TCP_CONNECT_TIME_MIN
,
529 (UINT32
) (Option
->ConnectionTimeout
* TCP_TICK_HZ
)
532 if (Option
->EnableNagle
== FALSE
) {
533 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
);
536 if (Option
->EnableTimeStamp
== FALSE
) {
537 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
);
540 if (Option
->EnableWindowScaling
== FALSE
) {
541 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
);
546 // The socket is bound, the <SrcIp, SrcPort, DstIp, DstPort> is
547 // determined, construct the IP device path and install it.
549 Status
= TcpInstallDevicePath (Sk
);
550 if (EFI_ERROR (Status
)) {
555 // update state of Tcb and socket
557 if (CfgData
->AccessPoint
.ActiveFlag
== FALSE
) {
559 TcpSetState (Tcb
, TCP_LISTEN
);
560 SockSetState (Sk
, SO_LISTENING
);
562 Sk
->ConfigureState
= SO_CONFIGURED_PASSIVE
;
565 Sk
->ConfigureState
= SO_CONFIGURED_ACTIVE
;
577 The procotol handler provided to the socket layer, used to
578 dispatch the socket level requests by calling the corresponding
581 @param Sock Pointer to the socket of this TCP instance.
582 @param Request The code of this operation request.
583 @param Data Pointer to the operation specific data passed in
584 together with the operation request.
586 @retval EFI_SUCCESS The socket request is completed successfully.
587 @retval other The error status returned by the corresponding TCP
594 IN SOCK_REQUEST Request
,
595 IN VOID
*Data OPTIONAL
599 TCP4_PROTO_DATA
*ProtoData
;
600 EFI_IP4_PROTOCOL
*Ip
;
602 ProtoData
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
603 Tcb
= ProtoData
->TcpPcb
;
607 Ip
= ProtoData
->TcpService
->IpIo
->Ip
;
613 // After user received data from socket buffer, socket will
614 // notify TCP using this message to give it a chance to send out
615 // window update information
617 ASSERT (Tcb
!= NULL
);
618 TcpOnAppConsume (Tcb
);
623 ASSERT (Tcb
!= NULL
);
640 Tcb
->SndPsh
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
);
641 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
);
646 Tcb
->SndUp
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
) - 1;
647 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
);
653 TcpOnAppConnect (Tcb
);
659 return Tcp4AttachPcb (Sock
);
671 Tcp4DetachPcb (Sock
);
677 return Tcp4ConfigurePcb (
679 (EFI_TCP4_CONFIG_DATA
*) Data
686 ASSERT ((Data
!= NULL
) && (Tcb
!= NULL
));
688 return Tcp4GetMode (Tcb
, (TCP4_MODE_DATA
*) Data
);
694 ASSERT ((Data
!= NULL
) && (Tcb
!= NULL
));
696 return Tcp4Route (Tcb
, (TCP4_ROUTE_INFO
*) Data
);
699 return EFI_UNSUPPORTED
;