3 Copyright (c) 2005 - 2006, Intel Corporation
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
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.
23 #define TCP_COMP_VAL(Min, Max, Default, Val) \
24 ((((Val) <= (Max)) && ((Val) >= (Min))) ? (Val) : (Default))
30 IN TCP4_ROUTE_INFO
*RouteInfo
36 Add or remove a route entry in the IP route table associated
37 with this TCP instance.
41 Tcb - Pointer to the TCP_CB of this TCP instance.
42 RouteInfo - Pointer to the route info to be processed.
46 EFI_SUCCESS - The operation completed successfully.
47 EFI_NOT_STARTED - The driver instance has not been started.
48 EFI_NO_MAPPING - When using the default address, configuration(DHCP,
49 BOOTP, RARP, etc.) is not finished yet.
50 EFI_OUT_OF_RESOURCES - Could not add the entry to the routing table.
51 EFI_NOT_FOUND - This route is not in the routing table
52 (when RouteInfo->DeleteRoute is TRUE).
53 EFI_ACCESS_DENIED - The route is already defined in the routing table
54 (when RouteInfo->DeleteRoute is FALSE).
66 RouteInfo
->DeleteRoute
,
67 RouteInfo
->SubnetAddress
,
68 RouteInfo
->SubnetMask
,
69 RouteInfo
->GatewayAddress
76 Get the operational settings of this TCP instance.
78 @param Tcb Pointer to the TCP_CB of this TCP instance.
79 @param Mode Pointer to the buffer to store the operational
82 @retval EFI_SUCCESS The mode data is read.
83 @retval EFI_NOT_STARTED No configuration data is available because this
84 instance hasn't been started.
91 IN TCP4_MODE_DATA
*Mode
95 EFI_TCP4_CONFIG_DATA
*ConfigData
;
96 EFI_TCP4_ACCESS_POINT
*AccessPoint
;
97 EFI_TCP4_OPTION
*Option
;
102 if (!SOCK_IS_CONFIGURED (Sock
) && (Mode
->Tcp4ConfigData
!= NULL
)) {
103 return EFI_NOT_STARTED
;
106 if (Mode
->Tcp4State
) {
107 *(Mode
->Tcp4State
) = (EFI_TCP4_CONNECTION_STATE
) Tcb
->State
;
110 if (Mode
->Tcp4ConfigData
) {
112 ConfigData
= Mode
->Tcp4ConfigData
;
113 AccessPoint
= &(ConfigData
->AccessPoint
);
114 Option
= ConfigData
->ControlOption
;
116 ConfigData
->TypeOfService
= Tcb
->TOS
;
117 ConfigData
->TimeToLive
= Tcb
->TTL
;
119 AccessPoint
->UseDefaultAddress
= Tcb
->UseDefaultAddr
;
121 NetCopyMem (&AccessPoint
->StationAddress
, &Tcb
->LocalEnd
.Ip
, sizeof (EFI_IPv4_ADDRESS
));
122 AccessPoint
->SubnetMask
= Tcb
->SubnetMask
;
123 AccessPoint
->StationPort
= NTOHS (Tcb
->LocalEnd
.Port
);
125 NetCopyMem (&AccessPoint
->RemoteAddress
, &Tcb
->RemoteEnd
.Ip
, sizeof (EFI_IPv4_ADDRESS
));
126 AccessPoint
->RemotePort
= NTOHS (Tcb
->RemoteEnd
.Port
);
127 AccessPoint
->ActiveFlag
= (BOOLEAN
) (Tcb
->State
!= TCP_LISTEN
);
129 if (Option
!= NULL
) {
130 Option
->ReceiveBufferSize
= GET_RCV_BUFFSIZE (Tcb
->Sk
);
131 Option
->SendBufferSize
= GET_SND_BUFFSIZE (Tcb
->Sk
);
132 Option
->MaxSynBackLog
= GET_BACKLOG (Tcb
->Sk
);
134 Option
->ConnectionTimeout
= Tcb
->ConnectTimeout
/ TCP_TICK_HZ
;
135 Option
->DataRetries
= Tcb
->MaxRexmit
;
136 Option
->FinTimeout
= Tcb
->FinWait2Timeout
/ TCP_TICK_HZ
;
137 Option
->TimeWaitTimeout
= Tcb
->TimeWaitTimeout
/ TCP_TICK_HZ
;
138 Option
->KeepAliveProbes
= Tcb
->MaxKeepAlive
;
139 Option
->KeepAliveTime
= Tcb
->KeepAliveIdle
/ TCP_TICK_HZ
;
140 Option
->KeepAliveInterval
= Tcb
->KeepAlivePeriod
/ TCP_TICK_HZ
;
142 Option
->EnableNagle
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
));
143 Option
->EnableTimeStamp
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
));
144 Option
->EnableWindowScaling
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
))
147 Option
->EnableSelectiveAck
= FALSE
;
148 Option
->EnablePathMtuDiscovery
= FALSE
;
152 Ip
= Tcb
->IpInfo
->Ip
;
155 return Ip
->GetModeData (Ip
, Mode
->Ip4ModeData
, Mode
->MnpConfigData
, Mode
->SnpModeData
);
160 If AP->StationPort isn't zero, check whether the access point
161 is registered, else generate a random station port for this
164 @param AP Pointer to the access point.
166 @retval EFI_SUCCESS The check is passed or the port is assigned.
167 @retval EFI_INVALID_PARAMETER The non-zero station port is already used.
168 @retval EFI_OUT_OF_RESOURCES No port can be allocated.
174 IN EFI_TCP4_ACCESS_POINT
*AP
179 if (0 != AP
->StationPort
) {
181 // check if a same endpoint is bound
183 if (TcpFindTcbByPeer (&AP
->StationAddress
, AP
->StationPort
)) {
185 return EFI_INVALID_PARAMETER
;
189 // generate a random port
193 if (TCP4_PORT_USER_RESERVED
== mTcp4RandomPort
) {
194 mTcp4RandomPort
= TCP4_PORT_KNOWN
;
199 while (TcpFindTcbByPeer (&AP
->StationAddress
, mTcp4RandomPort
)) {
203 if (mTcp4RandomPort
<= TCP4_PORT_KNOWN
) {
206 TCP4_DEBUG_ERROR (("Tcp4Bind: no port can be allocated "
209 return EFI_OUT_OF_RESOURCES
;
212 mTcp4RandomPort
= TCP4_PORT_KNOWN
+ 1;
219 AP
->StationPort
= mTcp4RandomPort
;
227 Flush the Tcb add its associated protocols..
229 @param Tcb Pointer to the TCP_CB to be flushed.
231 @retval EFI_SUCCESS The operation is completed successfully.
241 TCP4_PROTO_DATA
*TcpProto
;
243 IpIoConfigIp (Tcb
->IpInfo
, NULL
);
246 TcpProto
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
248 if (SOCK_IS_CONFIGURED (Sock
)) {
249 NetListRemoveEntry (&Tcb
->List
);
251 TcpSetVariableData (TcpProto
->TcpService
);
254 NetbufFreeList (&Tcb
->SndQue
);
255 NetbufFreeList (&Tcb
->RcvQue
);
265 TCP4_PROTO_DATA
*ProtoData
;
268 Tcb
= NetAllocateZeroPool (sizeof (TCP_CB
));
272 TCP4_DEBUG_ERROR (("Tcp4ConfigurePcb: failed to allocate a TCB\n"));
274 return EFI_OUT_OF_RESOURCES
;
277 ProtoData
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
278 IpIo
= ProtoData
->TcpService
->IpIo
;
281 // Create an IpInfo for this Tcb.
283 Tcb
->IpInfo
= IpIoAddIp (IpIo
);
284 if (Tcb
->IpInfo
== NULL
) {
287 return EFI_OUT_OF_RESOURCES
;
290 NetListInit (&Tcb
->List
);
291 NetListInit (&Tcb
->SndQue
);
292 NetListInit (&Tcb
->RcvQue
);
294 Tcb
->State
= TCP_CLOSED
;
296 ProtoData
->TcpPcb
= Tcb
;
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 Tcb using CfgData.
328 @param Sk Pointer to the socket of this TCP instance.
329 @param SkTcb Pointer to the TCP_CB of this TCP instance.
330 @param CfgData Pointer to the TCP configuration data.
332 @retval EFI_SUCCESS The operation is completed successfully.
333 @retval EFI_INVALID_PARAMETER A same access point has been configured in
334 another TCP instance.
335 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
342 IN EFI_TCP4_CONFIG_DATA
*CfgData
345 EFI_IP4_CONFIG_DATA IpCfgData
;
347 EFI_TCP4_OPTION
*Option
;
348 TCP4_PROTO_DATA
*TcpProto
;
351 ASSERT (CfgData
&& Sk
&& Sk
->SockHandle
);
353 TcpProto
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
354 Tcb
= TcpProto
->TcpPcb
;
356 ASSERT (Tcb
!= NULL
);
359 // Add Ip for send pkt to the peer
361 CopyMem (&IpCfgData
, &mIpIoDefaultIpConfigData
, sizeof (IpCfgData
));
362 IpCfgData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
363 IpCfgData
.UseDefaultAddress
= CfgData
->AccessPoint
.UseDefaultAddress
;
364 IpCfgData
.StationAddress
= CfgData
->AccessPoint
.StationAddress
;
365 IpCfgData
.SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
366 IpCfgData
.ReceiveTimeout
= (UINT32
) (-1);
369 // Configure the IP instance this Tcb consumes.
371 Status
= IpIoConfigIp (Tcb
->IpInfo
, &IpCfgData
);
372 if (EFI_ERROR (Status
)) {
377 // Get the default address info if the instance is configured to use default address.
379 if (CfgData
->AccessPoint
.UseDefaultAddress
) {
380 CfgData
->AccessPoint
.StationAddress
= IpCfgData
.StationAddress
;
381 CfgData
->AccessPoint
.SubnetMask
= IpCfgData
.SubnetMask
;
385 // check if we can bind this endpoint in CfgData
387 Status
= Tcp4Bind (&(CfgData
->AccessPoint
));
389 if (EFI_ERROR (Status
)) {
390 TCP4_DEBUG_ERROR (("Tcp4ConfigurePcb: Bind endpoint failed "
391 "with %r\n", Status
));
397 // Initalize the operating information in this Tcb
399 ASSERT (Tcb
->State
== TCP_CLOSED
&&
400 NetListIsEmpty (&Tcb
->SndQue
) &&
401 NetListIsEmpty (&Tcb
->RcvQue
));
403 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
404 Tcb
->State
= TCP_CLOSED
;
407 Tcb
->RcvMss
= TcpGetRcvMss (Sk
);
410 Tcb
->Rto
= 3 * TCP_TICK_HZ
;
412 Tcb
->CWnd
= Tcb
->SndMss
;
413 Tcb
->Ssthresh
= 0xffffffff;
415 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
417 Tcb
->KeepAliveIdle
= TCP_KEEPALIVE_IDLE_MIN
;
418 Tcb
->KeepAlivePeriod
= TCP_KEEPALIVE_PERIOD
;
419 Tcb
->MaxKeepAlive
= TCP_MAX_KEEPALIVE
;
420 Tcb
->MaxRexmit
= TCP_MAX_LOSS
;
421 Tcb
->FinWait2Timeout
= TCP_FIN_WAIT2_TIME
;
422 Tcb
->TimeWaitTimeout
= TCP_TIME_WAIT_TIME
;
423 Tcb
->ConnectTimeout
= TCP_CONNECT_TIME
;
426 // initialize Tcb in the light of CfgData
428 Tcb
->TTL
= CfgData
->TimeToLive
;
429 Tcb
->TOS
= CfgData
->TypeOfService
;
431 NetCopyMem (&Tcb
->LocalEnd
.Ip
, &CfgData
->AccessPoint
.StationAddress
, sizeof (IP4_ADDR
));
432 Tcb
->LocalEnd
.Port
= HTONS (CfgData
->AccessPoint
.StationPort
);
433 Tcb
->SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
435 NetCopyMem (&Tcb
->RemoteEnd
.Ip
, &CfgData
->AccessPoint
.RemoteAddress
, sizeof (IP4_ADDR
));
436 Tcb
->RemoteEnd
.Port
= HTONS (CfgData
->AccessPoint
.RemotePort
);
438 Option
= CfgData
->ControlOption
;
440 if (Option
!= NULL
) {
443 (UINT32
) (TCP_COMP_VAL (
444 TCP_RCV_BUF_SIZE_MIN
,
447 Option
->ReceiveBufferSize
453 (UINT32
) (TCP_COMP_VAL (
454 TCP_SND_BUF_SIZE_MIN
,
457 Option
->SendBufferSize
464 (UINT32
) (TCP_COMP_VAL (
468 Option
->MaxSynBackLog
473 Tcb
->MaxRexmit
= (UINT16
) TCP_COMP_VAL (
479 Tcb
->FinWait2Timeout
= TCP_COMP_VAL (
481 TCP_FIN_WAIT2_TIME_MAX
,
483 (UINT32
) (Option
->FinTimeout
* TCP_TICK_HZ
)
486 if (Option
->TimeWaitTimeout
!= 0) {
487 Tcb
->TimeWaitTimeout
= TCP_COMP_VAL (
489 TCP_TIME_WAIT_TIME_MAX
,
491 (UINT32
) (Option
->TimeWaitTimeout
* TCP_TICK_HZ
)
494 Tcb
->TimeWaitTimeout
= 0;
497 if (Option
->KeepAliveProbes
!= 0) {
498 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
500 Tcb
->MaxKeepAlive
= (UINT8
) TCP_COMP_VAL (
501 TCP_MAX_KEEPALIVE_MIN
,
504 Option
->KeepAliveProbes
506 Tcb
->KeepAliveIdle
= TCP_COMP_VAL (
507 TCP_KEEPALIVE_IDLE_MIN
,
508 TCP_KEEPALIVE_IDLE_MAX
,
509 TCP_KEEPALIVE_IDLE_MIN
,
510 (UINT32
) (Option
->KeepAliveTime
* TCP_TICK_HZ
)
512 Tcb
->KeepAlivePeriod
= TCP_COMP_VAL (
513 TCP_KEEPALIVE_PERIOD_MIN
,
514 TCP_KEEPALIVE_PERIOD
,
515 TCP_KEEPALIVE_PERIOD
,
516 (UINT32
) (Option
->KeepAliveInterval
* TCP_TICK_HZ
)
520 Tcb
->ConnectTimeout
= TCP_COMP_VAL (
521 TCP_CONNECT_TIME_MIN
,
524 (UINT32
) (Option
->ConnectionTimeout
* TCP_TICK_HZ
)
527 if (Option
->EnableNagle
== FALSE
) {
528 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
);
531 if (Option
->EnableTimeStamp
== FALSE
) {
532 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
);
535 if (Option
->EnableWindowScaling
== FALSE
) {
536 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
);
541 // update state of Tcb and socket
543 if (CfgData
->AccessPoint
.ActiveFlag
== FALSE
) {
545 TcpSetState (Tcb
, TCP_LISTEN
);
546 SockSetState (Sk
, SO_LISTENING
);
548 Sk
->ConfigureState
= SO_CONFIGURED_PASSIVE
;
551 Sk
->ConfigureState
= SO_CONFIGURED_ACTIVE
;
563 The procotol handler provided to the socket layer, used to
564 dispatch the socket level requests by calling the corresponding
567 @param Sock Pointer to the socket of this TCP instance.
568 @param Request The code of this operation request.
569 @param Data Pointer to the operation specific data passed in
570 together with the operation request.
572 @retval EFI_SUCCESS The socket request is completed successfully.
573 @retval other The error status returned by the corresponding TCP
580 IN SOCK_REQUEST Request
,
581 IN VOID
*Data OPTIONAL
585 TCP4_PROTO_DATA
*ProtoData
;
586 EFI_IP4_PROTOCOL
*Ip
;
588 ProtoData
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
589 Tcb
= ProtoData
->TcpPcb
;
593 Ip
= ProtoData
->TcpService
->IpIo
->Ip
;
599 // After user received data from socket buffer, socket will
600 // notify TCP using this message to give it a chance to send out
601 // window update information
604 TcpOnAppConsume (Tcb
);
626 Tcb
->SndPsh
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
);
627 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
);
632 Tcb
->SndUp
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
) - 1;
633 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
);
639 TcpOnAppConnect (Tcb
);
645 return Tcp4AttachPcb (Sock
);
657 Tcp4DetachPcb (Sock
);
663 return Tcp4ConfigurePcb (
665 (EFI_TCP4_CONFIG_DATA
*) Data
672 ASSERT (Data
&& Tcb
);
674 return Tcp4GetMode (Tcb
, (TCP4_MODE_DATA
*) Data
);
680 ASSERT (Data
&& Tcb
);
682 return Tcp4Route (Tcb
, (TCP4_ROUTE_INFO
*) Data
);