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))
29 IN TCP4_ROUTE_INFO
*RouteInfo
35 Add or remove a route entry in the IP route table associated
36 with this TCP instance.
40 Tcb - Pointer to the TCP_CB of this TCP instance.
41 RouteInfo - Pointer to the route info to be processed.
45 EFI_SUCCESS - The operation completed successfully.
46 EFI_NOT_STARTED - The driver instance has not been started.
47 EFI_NO_MAPPING - When using the default address, configuration(DHCP,
48 BOOTP, RARP, etc.) is not finished yet.
49 EFI_OUT_OF_RESOURCES - Could not add the entry to the routing table.
50 EFI_NOT_FOUND - This route is not in the routing table
51 (when RouteInfo->DeleteRoute is TRUE).
52 EFI_ACCESS_DENIED - The route is already defined in the routing table
53 (when RouteInfo->DeleteRoute is FALSE).
65 RouteInfo
->DeleteRoute
,
66 RouteInfo
->SubnetAddress
,
67 RouteInfo
->SubnetMask
,
68 RouteInfo
->GatewayAddress
75 Get the operational settings of this TCP instance.
77 @param Tcb Pointer to the TCP_CB of this TCP instance.
78 @param Mode Pointer to the buffer to store the operational
81 @retval EFI_SUCCESS The mode data is read.
82 @retval EFI_NOT_STARTED No configuration data is available because this
83 instance hasn't been started.
89 IN TCP4_MODE_DATA
*Mode
93 EFI_TCP4_CONFIG_DATA
*ConfigData
;
94 EFI_TCP4_ACCESS_POINT
*AccessPoint
;
95 EFI_TCP4_OPTION
*Option
;
100 if (!SOCK_IS_CONFIGURED (Sock
) && (Mode
->Tcp4ConfigData
!= NULL
)) {
101 return EFI_NOT_STARTED
;
104 if (Mode
->Tcp4State
) {
105 *(Mode
->Tcp4State
) = (EFI_TCP4_CONNECTION_STATE
) Tcb
->State
;
108 if (Mode
->Tcp4ConfigData
) {
110 ConfigData
= Mode
->Tcp4ConfigData
;
111 AccessPoint
= &(ConfigData
->AccessPoint
);
112 Option
= ConfigData
->ControlOption
;
114 ConfigData
->TypeOfService
= Tcb
->TOS
;
115 ConfigData
->TimeToLive
= Tcb
->TTL
;
117 AccessPoint
->UseDefaultAddress
= Tcb
->UseDefaultAddr
;
119 CopyMem (&AccessPoint
->StationAddress
, &Tcb
->LocalEnd
.Ip
, sizeof (EFI_IPv4_ADDRESS
));
120 AccessPoint
->SubnetMask
= Tcb
->SubnetMask
;
121 AccessPoint
->StationPort
= NTOHS (Tcb
->LocalEnd
.Port
);
123 CopyMem (&AccessPoint
->RemoteAddress
, &Tcb
->RemoteEnd
.Ip
, sizeof (EFI_IPv4_ADDRESS
));
124 AccessPoint
->RemotePort
= NTOHS (Tcb
->RemoteEnd
.Port
);
125 AccessPoint
->ActiveFlag
= (BOOLEAN
) (Tcb
->State
!= TCP_LISTEN
);
127 if (Option
!= NULL
) {
128 Option
->ReceiveBufferSize
= GET_RCV_BUFFSIZE (Tcb
->Sk
);
129 Option
->SendBufferSize
= GET_SND_BUFFSIZE (Tcb
->Sk
);
130 Option
->MaxSynBackLog
= GET_BACKLOG (Tcb
->Sk
);
132 Option
->ConnectionTimeout
= Tcb
->ConnectTimeout
/ TCP_TICK_HZ
;
133 Option
->DataRetries
= Tcb
->MaxRexmit
;
134 Option
->FinTimeout
= Tcb
->FinWait2Timeout
/ TCP_TICK_HZ
;
135 Option
->TimeWaitTimeout
= Tcb
->TimeWaitTimeout
/ TCP_TICK_HZ
;
136 Option
->KeepAliveProbes
= Tcb
->MaxKeepAlive
;
137 Option
->KeepAliveTime
= Tcb
->KeepAliveIdle
/ TCP_TICK_HZ
;
138 Option
->KeepAliveInterval
= Tcb
->KeepAlivePeriod
/ TCP_TICK_HZ
;
140 Option
->EnableNagle
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
));
141 Option
->EnableTimeStamp
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
));
142 Option
->EnableWindowScaling
= (BOOLEAN
) (!TCP_FLG_ON (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
));
144 Option
->EnableSelectiveAck
= FALSE
;
145 Option
->EnablePathMtuDiscovery
= FALSE
;
149 Ip
= Tcb
->IpInfo
->Ip
;
152 return Ip
->GetModeData (Ip
, Mode
->Ip4ModeData
, Mode
->MnpConfigData
, Mode
->SnpModeData
);
157 If AP->StationPort isn't zero, check whether the access point
158 is registered, else generate a random station port for this
161 @param AP Pointer to the access point.
163 @retval EFI_SUCCESS The check is passed or the port is assigned.
164 @retval EFI_INVALID_PARAMETER The non-zero station port is already used.
165 @retval EFI_OUT_OF_RESOURCES No port can be allocated.
170 IN EFI_TCP4_ACCESS_POINT
*AP
175 if (0 != AP
->StationPort
) {
177 // check if a same endpoint is bound
179 if (TcpFindTcbByPeer (&AP
->StationAddress
, AP
->StationPort
)) {
181 return EFI_INVALID_PARAMETER
;
185 // generate a random port
189 if (TCP4_PORT_USER_RESERVED
== mTcp4RandomPort
) {
190 mTcp4RandomPort
= TCP4_PORT_KNOWN
;
195 while (TcpFindTcbByPeer (&AP
->StationAddress
, mTcp4RandomPort
)) {
199 if (mTcp4RandomPort
<= TCP4_PORT_KNOWN
) {
202 DEBUG ((EFI_D_ERROR
, "Tcp4Bind: no port can be allocated "
205 return EFI_OUT_OF_RESOURCES
;
208 mTcp4RandomPort
= TCP4_PORT_KNOWN
+ 1;
215 AP
->StationPort
= mTcp4RandomPort
;
223 Flush the Tcb add its associated protocols..
225 @param Tcb Pointer to the TCP_CB to be flushed.
227 @retval EFI_SUCCESS The operation is completed successfully.
236 TCP4_PROTO_DATA
*TcpProto
;
238 IpIoConfigIp (Tcb
->IpInfo
, NULL
);
241 TcpProto
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
243 if (SOCK_IS_CONFIGURED (Sock
)) {
244 RemoveEntryList (&Tcb
->List
);
247 // Uninstall the device path protocl.
249 gBS
->UninstallProtocolInterface (
251 &gEfiDevicePathProtocolGuid
,
254 gBS
->FreePool (Sock
->DevicePath
);
256 TcpSetVariableData (TcpProto
->TcpService
);
259 NetbufFreeList (&Tcb
->SndQue
);
260 NetbufFreeList (&Tcb
->RcvQue
);
269 TCP4_PROTO_DATA
*ProtoData
;
272 Tcb
= AllocateZeroPool (sizeof (TCP_CB
));
276 DEBUG ((EFI_D_ERROR
, "Tcp4ConfigurePcb: failed to allocate a TCB\n"));
278 return EFI_OUT_OF_RESOURCES
;
281 ProtoData
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
282 IpIo
= ProtoData
->TcpService
->IpIo
;
285 // Create an IpInfo for this Tcb.
287 Tcb
->IpInfo
= IpIoAddIp (IpIo
);
288 if (Tcb
->IpInfo
== NULL
) {
291 return EFI_OUT_OF_RESOURCES
;
294 InitializeListHead (&Tcb
->List
);
295 InitializeListHead (&Tcb
->SndQue
);
296 InitializeListHead (&Tcb
->RcvQue
);
298 Tcb
->State
= TCP_CLOSED
;
300 ProtoData
->TcpPcb
= Tcb
;
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 Tcb using CfgData.
331 @param Sk Pointer to the socket of this TCP instance.
332 @param SkTcb Pointer to the TCP_CB of this TCP instance.
333 @param CfgData Pointer to the TCP configuration data.
335 @retval EFI_SUCCESS The operation is completed successfully.
336 @retval EFI_INVALID_PARAMETER A same access point has been configured in
337 another TCP instance.
338 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
344 IN EFI_TCP4_CONFIG_DATA
*CfgData
347 EFI_IP4_CONFIG_DATA IpCfgData
;
349 EFI_TCP4_OPTION
*Option
;
350 TCP4_PROTO_DATA
*TcpProto
;
353 ASSERT (CfgData
&& Sk
&& Sk
->SockHandle
);
355 TcpProto
= (TCP4_PROTO_DATA
*) Sk
->ProtoReserved
;
356 Tcb
= TcpProto
->TcpPcb
;
358 ASSERT (Tcb
!= NULL
);
361 // Add Ip for send pkt to the peer
363 CopyMem (&IpCfgData
, &mIpIoDefaultIpConfigData
, sizeof (IpCfgData
));
364 IpCfgData
.DefaultProtocol
= EFI_IP_PROTO_TCP
;
365 IpCfgData
.UseDefaultAddress
= CfgData
->AccessPoint
.UseDefaultAddress
;
366 IpCfgData
.StationAddress
= CfgData
->AccessPoint
.StationAddress
;
367 IpCfgData
.SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
368 IpCfgData
.ReceiveTimeout
= (UINT32
) (-1);
371 // Configure the IP instance this Tcb consumes.
373 Status
= IpIoConfigIp (Tcb
->IpInfo
, &IpCfgData
);
374 if (EFI_ERROR (Status
)) {
379 // Get the default address info if the instance is configured to use default address.
381 if (CfgData
->AccessPoint
.UseDefaultAddress
) {
382 CfgData
->AccessPoint
.StationAddress
= IpCfgData
.StationAddress
;
383 CfgData
->AccessPoint
.SubnetMask
= IpCfgData
.SubnetMask
;
387 // check if we can bind this endpoint in CfgData
389 Status
= Tcp4Bind (&(CfgData
->AccessPoint
));
391 if (EFI_ERROR (Status
)) {
392 DEBUG ((EFI_D_ERROR
, "Tcp4ConfigurePcb: Bind endpoint failed "
393 "with %r\n", Status
));
399 // Initalize the operating information in this Tcb
401 ASSERT (Tcb
->State
== TCP_CLOSED
&&
402 IsListEmpty (&Tcb
->SndQue
) &&
403 IsListEmpty (&Tcb
->RcvQue
));
405 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
406 Tcb
->State
= TCP_CLOSED
;
409 Tcb
->RcvMss
= TcpGetRcvMss (Sk
);
412 Tcb
->Rto
= 3 * TCP_TICK_HZ
;
414 Tcb
->CWnd
= Tcb
->SndMss
;
415 Tcb
->Ssthresh
= 0xffffffff;
417 Tcb
->CongestState
= TCP_CONGEST_OPEN
;
419 Tcb
->KeepAliveIdle
= TCP_KEEPALIVE_IDLE_MIN
;
420 Tcb
->KeepAlivePeriod
= TCP_KEEPALIVE_PERIOD
;
421 Tcb
->MaxKeepAlive
= TCP_MAX_KEEPALIVE
;
422 Tcb
->MaxRexmit
= TCP_MAX_LOSS
;
423 Tcb
->FinWait2Timeout
= TCP_FIN_WAIT2_TIME
;
424 Tcb
->TimeWaitTimeout
= TCP_TIME_WAIT_TIME
;
425 Tcb
->ConnectTimeout
= TCP_CONNECT_TIME
;
428 // initialize Tcb in the light of CfgData
430 Tcb
->TTL
= CfgData
->TimeToLive
;
431 Tcb
->TOS
= CfgData
->TypeOfService
;
433 Tcb
->UseDefaultAddr
= CfgData
->AccessPoint
.UseDefaultAddress
;
435 CopyMem (&Tcb
->LocalEnd
.Ip
, &CfgData
->AccessPoint
.StationAddress
, sizeof (IP4_ADDR
));
436 Tcb
->LocalEnd
.Port
= HTONS (CfgData
->AccessPoint
.StationPort
);
437 Tcb
->SubnetMask
= CfgData
->AccessPoint
.SubnetMask
;
439 if (CfgData
->AccessPoint
.ActiveFlag
) {
440 CopyMem (&Tcb
->RemoteEnd
.Ip
, &CfgData
->AccessPoint
.RemoteAddress
, sizeof (IP4_ADDR
));
441 Tcb
->RemoteEnd
.Port
= HTONS (CfgData
->AccessPoint
.RemotePort
);
443 Tcb
->RemoteEnd
.Ip
= 0;
444 Tcb
->RemoteEnd
.Port
= 0;
447 Option
= CfgData
->ControlOption
;
449 if (Option
!= NULL
) {
452 (UINT32
) (TCP_COMP_VAL (
453 TCP_RCV_BUF_SIZE_MIN
,
456 Option
->ReceiveBufferSize
462 (UINT32
) (TCP_COMP_VAL (
463 TCP_SND_BUF_SIZE_MIN
,
466 Option
->SendBufferSize
473 (UINT32
) (TCP_COMP_VAL (
477 Option
->MaxSynBackLog
482 Tcb
->MaxRexmit
= (UINT16
) TCP_COMP_VAL (
488 Tcb
->FinWait2Timeout
= TCP_COMP_VAL (
490 TCP_FIN_WAIT2_TIME_MAX
,
492 (UINT32
) (Option
->FinTimeout
* TCP_TICK_HZ
)
495 if (Option
->TimeWaitTimeout
!= 0) {
496 Tcb
->TimeWaitTimeout
= TCP_COMP_VAL (
498 TCP_TIME_WAIT_TIME_MAX
,
500 (UINT32
) (Option
->TimeWaitTimeout
* TCP_TICK_HZ
)
503 Tcb
->TimeWaitTimeout
= 0;
506 if (Option
->KeepAliveProbes
!= 0) {
507 TCP_CLEAR_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_KEEPALIVE
);
509 Tcb
->MaxKeepAlive
= (UINT8
) TCP_COMP_VAL (
510 TCP_MAX_KEEPALIVE_MIN
,
513 Option
->KeepAliveProbes
515 Tcb
->KeepAliveIdle
= TCP_COMP_VAL (
516 TCP_KEEPALIVE_IDLE_MIN
,
517 TCP_KEEPALIVE_IDLE_MAX
,
518 TCP_KEEPALIVE_IDLE_MIN
,
519 (UINT32
) (Option
->KeepAliveTime
* TCP_TICK_HZ
)
521 Tcb
->KeepAlivePeriod
= TCP_COMP_VAL (
522 TCP_KEEPALIVE_PERIOD_MIN
,
523 TCP_KEEPALIVE_PERIOD
,
524 TCP_KEEPALIVE_PERIOD
,
525 (UINT32
) (Option
->KeepAliveInterval
* TCP_TICK_HZ
)
529 Tcb
->ConnectTimeout
= TCP_COMP_VAL (
530 TCP_CONNECT_TIME_MIN
,
533 (UINT32
) (Option
->ConnectionTimeout
* TCP_TICK_HZ
)
536 if (Option
->EnableNagle
== FALSE
) {
537 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_NAGLE
);
540 if (Option
->EnableTimeStamp
== FALSE
) {
541 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_TS
);
544 if (Option
->EnableWindowScaling
== FALSE
) {
545 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_NO_WS
);
550 // The socket is bound, the <SrcIp, SrcPort, DstIp, DstPort> is
551 // determined, construct the IP device path and install it.
553 Status
= TcpInstallDevicePath (Sk
);
554 if (EFI_ERROR (Status
)) {
559 // update state of Tcb and socket
561 if (CfgData
->AccessPoint
.ActiveFlag
== FALSE
) {
563 TcpSetState (Tcb
, TCP_LISTEN
);
564 SockSetState (Sk
, SO_LISTENING
);
566 Sk
->ConfigureState
= SO_CONFIGURED_PASSIVE
;
569 Sk
->ConfigureState
= SO_CONFIGURED_ACTIVE
;
581 The procotol handler provided to the socket layer, used to
582 dispatch the socket level requests by calling the corresponding
585 @param Sock Pointer to the socket of this TCP instance.
586 @param Request The code of this operation request.
587 @param Data Pointer to the operation specific data passed in
588 together with the operation request.
590 @retval EFI_SUCCESS The socket request is completed successfully.
591 @retval other The error status returned by the corresponding TCP
598 IN SOCK_REQUEST Request
,
599 IN VOID
*Data OPTIONAL
603 TCP4_PROTO_DATA
*ProtoData
;
604 EFI_IP4_PROTOCOL
*Ip
;
606 ProtoData
= (TCP4_PROTO_DATA
*) Sock
->ProtoReserved
;
607 Tcb
= ProtoData
->TcpPcb
;
611 Ip
= ProtoData
->TcpService
->IpIo
->Ip
;
617 // After user received data from socket buffer, socket will
618 // notify TCP using this message to give it a chance to send out
619 // window update information
622 TcpOnAppConsume (Tcb
);
644 Tcb
->SndPsh
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
);
645 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_PSH
);
650 Tcb
->SndUp
= TcpGetMaxSndNxt (Tcb
) + GET_SND_DATASIZE (Tcb
->Sk
) - 1;
651 TCP_SET_FLG (Tcb
->CtrlFlag
, TCP_CTRL_SND_URG
);
657 TcpOnAppConnect (Tcb
);
663 return Tcp4AttachPcb (Sock
);
675 Tcp4DetachPcb (Sock
);
681 return Tcp4ConfigurePcb (
683 (EFI_TCP4_CONFIG_DATA
*) Data
690 ASSERT (Data
&& Tcb
);
692 return Tcp4GetMode (Tcb
, (TCP4_MODE_DATA
*) Data
);
698 ASSERT (Data
&& Tcb
);
700 return Tcp4Route (Tcb
, (TCP4_ROUTE_INFO
*) Data
);
703 return EFI_UNSUPPORTED
;