2 Implement the TCP4 driver support for the socket layer.
4 Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
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.
14 \section ConnectionManagement Connection Management
16 The ::EslTcp4Listen routine initially places the SOCK_STREAM or
17 SOCK_SEQPACKET socket into a listen state. When a remote machine
18 makes a connection to the socket, the TCPv4 network layer calls
19 ::EslTcp4ListenComplete to complete the connection processing.
20 EslTcp4ListenComplete manages the connections by placing them in
21 FIFO order in a queue to be serviced by the application. When the
22 number of connections exceeds the backlog (ESL_SOCKET::MaxFifoDepth),
23 the new connection is closed. Eventually, the application indirectly
24 calls ::EslTcp4Accept to remove the next connection from the queue
25 and get the associated socket.
33 Attempt to connect to a remote TCP port
35 This routine starts the connection processing for a SOCK_STREAM
36 or SOCK_SEQPAKCET socket using the TCPv4 network layer. It
37 configures the local TCPv4 connection point and then attempts to
38 connect to a remote system. Upon completion, the
39 ::EslTcp4ConnectComplete routine gets called with the connection
42 This routine is called by ::EslSocketConnect to initiate the TCPv4
43 network specific connect operations. The connection processing is
44 initiated by this routine and finished by ::EslTcp4ConnectComplete.
45 This pair of routines walks through the list of local TCPv4
46 connection points until a connection to the remote system is
49 @param [in] pSocket Address of an ::ESL_SOCKET structure.
51 @retval EFI_SUCCESS The connection was successfully established.
52 @retval EFI_NOT_READY The connection is in progress, call this routine again.
53 @retval Others The connection attempt failed.
58 IN ESL_SOCKET
* pSocket
63 Process the connection attempt
65 A system has initiated a connection attempt with a socket in the
66 listen state. Attempt to complete the connection.
68 The TCPv4 layer calls this routine when a connection is made to
69 the socket in the listen state. See the
70 \ref ConnectionManagement section.
72 @param [in] Event The listen completion event
74 @param [in] pPort Address of an ::ESL_PORT structure.
78 EslTcp4ListenComplete (
85 Accept a network connection.
87 This routine waits for a network connection to the socket and
88 returns the remote network address to the caller if requested.
90 This routine is called by ::EslSocketAccept to handle the TCPv4 protocol
91 specific accept operations for SOCK_STREAM and SOCK_SEQPACKET sockets.
92 See the \ref ConnectionManagement section.
94 @param [in] pSocket Address of an ::ESL_SOCKET structure.
96 @param [in] pSockAddr Address of a buffer to receive the remote
99 @param [in, out] pSockAddrLength Length in bytes of the address buffer.
100 On output specifies the length of the
101 remote network address.
103 @retval EFI_SUCCESS Remote address is available
104 @retval Others Remote address not available
109 IN ESL_SOCKET
* pSocket
,
110 IN
struct sockaddr
* pSockAddr
,
111 IN OUT socklen_t
* pSockAddrLength
115 struct sockaddr_in
* pRemoteAddress
;
116 ESL_TCP4_CONTEXT
* pTcp4
;
117 UINT32 RemoteAddress
;
123 // Validate the socket length
125 pRemoteAddress
= (struct sockaddr_in
*) pSockAddr
;
126 if (( NULL
== pSockAddrLength
)
127 || ( sizeof ( *pRemoteAddress
) > *pSockAddrLength
)) {
129 // Invalid socket address
131 Status
= EFI_INVALID_PARAMETER
;
132 pSocket
->errno
= EINVAL
;
133 DEBUG (( DEBUG_ACCEPT
,
134 "ERROR - Invalid address length\r\n" ));
140 Status
= EFI_SUCCESS
;
143 // Locate the address context
145 pPort
= pSocket
->pPortList
;
146 pTcp4
= &pPort
->Context
.Tcp4
;
149 // Fill-in the remote address structure
151 ZeroMem ( pRemoteAddress
, sizeof ( *pRemoteAddress
));
152 pRemoteAddress
->sin_len
= sizeof ( *pRemoteAddress
);
153 pRemoteAddress
->sin_family
= AF_INET
;
154 pRemoteAddress
->sin_port
= SwapBytes16 ( pTcp4
->ConfigData
.AccessPoint
.RemotePort
);
155 RemoteAddress
= pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[3];
157 RemoteAddress
|= pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[2];
159 RemoteAddress
|= pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[1];
161 RemoteAddress
|= pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[0];
162 pRemoteAddress
->sin_addr
.s_addr
= RemoteAddress
;
166 // Return the operation status
168 DBG_EXIT_STATUS ( Status
);
174 Process the remote connection completion event.
176 This routine handles the completion of a connection attempt. It
177 releases the port (TCPv4 adapter connection) in the case of an
178 error and start a connection attempt on the next port. If the
179 connection attempt was successful then this routine releases all
182 This routine is called by the TCPv4 layer when a connect request
183 completes. It sets the ESL_SOCKET::bConnected flag to notify the
184 ::EslTcp4ConnectComplete routine that the connection is available.
185 The flag is set when the connection is established or no more ports
186 exist in the list. The connection status is passed via
187 ESL_SOCKET::ConnectStatus.
189 @param [in] Event The connect completion event
191 @param [in] pPort Address of an ::ESL_PORT structure.
195 EslTcp4ConnectComplete (
200 BOOLEAN bRemoveFirstPort
;
201 BOOLEAN bRemovePorts
;
202 ESL_PORT
* pNextPort
;
203 ESL_SOCKET
* pSocket
;
204 ESL_TCP4_CONTEXT
* pTcp4
;
210 // Locate the TCP context
212 pSocket
= pPort
->pSocket
;
213 pTcp4
= &pPort
->Context
.Tcp4
;
216 // Get the connection status
218 bRemoveFirstPort
= FALSE
;
219 bRemovePorts
= FALSE
;
220 Status
= pTcp4
->ConnectToken
.CompletionToken
.Status
;
221 pSocket
->ConnectStatus
= Status
;
222 if ( !EFI_ERROR ( Status
)) {
224 // The connection was successful
226 DEBUG (( DEBUG_CONNECT
,
227 "0x%08x: Port connected to %d.%d.%d.%d:%d\r\n",
229 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[0],
230 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[1],
231 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[2],
232 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[3],
233 pTcp4
->ConfigData
.AccessPoint
.RemotePort
));
236 // Start the receive operations
238 pSocket
->bConfigured
= TRUE
;
239 pSocket
->State
= SOCKET_STATE_CONNECTED
;
240 EslSocketRxStart ( pPort
);
243 // Remove the rest of the ports
249 // The connection failed
251 if ( pPort
->bConfigured
) {
252 DEBUG (( DEBUG_CONNECT
,
253 "0x%08x: Port connection to %d.%d.%d.%d:%d failed, Status: %r\r\n",
255 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[0],
256 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[1],
257 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[2],
258 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[3],
259 pTcp4
->ConfigData
.AccessPoint
.RemotePort
,
264 // Close the current port
266 Status
= EslSocketPortClose ( pPort
);
267 if ( !EFI_ERROR ( Status
)) {
268 DEBUG (( DEBUG_CONNECT
,
269 "0x%08x: Port closed\r\n",
273 DEBUG (( DEBUG_CONNECT
,
274 "ERROR - Failed to close port 0x%08x, Status: %r\r\n",
280 // Try to connect using the next port
282 Status
= EslTcp4ConnectStart ( pSocket
);
283 if ( EFI_NOT_READY
!= Status
) {
284 bRemoveFirstPort
= TRUE
;
289 // Remove the ports if necessary
291 if ( bRemoveFirstPort
|| bRemovePorts
) {
293 // Remove the first port if necessary
295 pPort
= pSocket
->pPortList
;
296 if (( !bRemoveFirstPort
) && ( NULL
!= pPort
)) {
297 pPort
= pPort
->pLinkSocket
;
301 // Remove the rest of the list
303 while ( NULL
!= pPort
) {
304 pNextPort
= pPort
->pLinkSocket
;
305 EslSocketPortClose ( pPort
);
306 if ( !EFI_ERROR ( Status
)) {
307 DEBUG (( DEBUG_CONNECT
,
308 "0x%08x: Port closed\r\n",
312 DEBUG (( DEBUG_CONNECT
,
313 "ERROR - Failed to close port 0x%08x, Status: %r\r\n",
321 // Notify the poll routine
323 pSocket
->bConnected
= TRUE
;
331 Poll for completion of the connection attempt.
333 This routine polls the ESL_SOCKET::bConnected flag to determine
334 when the connection attempt is complete.
336 This routine is called from ::EslSocketConnect to determine when
337 the connection is complete. The ESL_SOCKET::bConnected flag is
338 set by ::EslTcp4ConnectComplete when the TCPv4 layer establishes
339 a connection or runs out of local network adapters. This routine
340 gets the connection status from ESL_SOCKET::ConnectStatus.
342 @param [in] pSocket Address of an ::ESL_SOCKET structure.
344 @retval EFI_SUCCESS The connection was successfully established.
345 @retval EFI_NOT_READY The connection is in progress, call this routine again.
346 @retval Others The connection attempt failed.
351 IN ESL_SOCKET
* pSocket
359 // Determine if the connection is complete
361 if ( !pSocket
->bConnected
) {
365 pSocket
->errno
= EAGAIN
;
366 Status
= EFI_NOT_READY
;
370 // The connection processing is complete
372 pSocket
->bConnected
= FALSE
;
375 // Translate the connection status
377 Status
= pSocket
->ConnectStatus
;
380 case EFI_DEVICE_ERROR
:
381 pSocket
->errno
= EIO
;
385 pSocket
->errno
= ECONNABORTED
;
388 case EFI_ACCESS_DENIED
:
389 pSocket
->errno
= EACCES
;
392 case EFI_CONNECTION_RESET
:
393 pSocket
->errno
= ECONNRESET
;
396 case EFI_INVALID_PARAMETER
:
397 pSocket
->errno
= EADDRNOTAVAIL
;
400 case EFI_HOST_UNREACHABLE
:
401 case EFI_NO_RESPONSE
:
402 pSocket
->errno
= EHOSTUNREACH
;
406 pSocket
->errno
= EAFNOSUPPORT
;
410 case EFI_NETWORK_UNREACHABLE
:
411 pSocket
->errno
= ENETDOWN
;
414 case EFI_OUT_OF_RESOURCES
:
415 pSocket
->errno
= ENOBUFS
;
418 case EFI_PORT_UNREACHABLE
:
419 case EFI_PROTOCOL_UNREACHABLE
:
420 case EFI_CONNECTION_REFUSED
:
421 pSocket
->errno
= ECONNREFUSED
;
429 pSocket
->errno
= ETIMEDOUT
;
432 case EFI_UNSUPPORTED
:
433 pSocket
->errno
= EOPNOTSUPP
;
438 // Display the translation
440 DEBUG (( DEBUG_CONNECT
,
441 "ERROR - errno: %d, Status: %r\r\n",
447 // Return the initialization status
449 DBG_EXIT_STATUS ( Status
);
455 Attempt to connect to a remote TCP port
457 This routine starts the connection processing for a SOCK_STREAM
458 or SOCK_SEQPAKCET socket using the TCPv4 network layer. It
459 configures the local TCPv4 connection point and then attempts to
460 connect to a remote system. Upon completion, the
461 ::EslTcp4ConnectComplete routine gets called with the connection
464 This routine is called by ::EslSocketConnect to initiate the TCPv4
465 network specific connect operations. The connection processing is
466 initiated by this routine and finished by ::EslTcp4ConnectComplete.
467 This pair of routines walks through the list of local TCPv4
468 connection points until a connection to the remote system is
471 @param [in] pSocket Address of an ::ESL_SOCKET structure.
473 @retval EFI_SUCCESS The connection was successfully established.
474 @retval EFI_NOT_READY The connection is in progress, call this routine again.
475 @retval Others The connection attempt failed.
479 EslTcp4ConnectStart (
480 IN ESL_SOCKET
* pSocket
484 ESL_TCP4_CONTEXT
* pTcp4
;
485 EFI_TCP4_PROTOCOL
* pTcp4Protocol
;
486 EFI_SIMPLE_NETWORK_MODE SnpModeData
;
492 // Determine if any more local adapters are available
494 pPort
= pSocket
->pPortList
;
495 if ( NULL
!= pPort
) {
497 // Configure the port
499 pTcp4
= &pPort
->Context
.Tcp4
;
500 pTcp4
->ConfigData
.AccessPoint
.ActiveFlag
= TRUE
;
501 pTcp4
->ConfigData
.TimeToLive
= 255;
502 pTcp4Protocol
= pPort
->pProtocol
.TCPv4
;
503 Status
= pTcp4Protocol
->Configure ( pTcp4Protocol
,
504 &pTcp4
->ConfigData
);
505 if ( EFI_ERROR ( Status
)) {
506 DEBUG (( DEBUG_CONNECT
,
507 "ERROR - Failed to configure the Tcp4 port, Status: %r\r\n",
511 DEBUG (( DEBUG_CONNECT
,
512 "0x%08x: Port configured\r\n",
514 pPort
->bConfigured
= TRUE
;
517 // Verify the port connection
519 Status
= pTcp4Protocol
->GetModeData ( pTcp4Protocol
,
525 if ( !EFI_ERROR ( Status
)) {
526 if ( SnpModeData
.MediaPresentSupported
527 && ( !SnpModeData
.MediaPresent
)) {
529 // Port is not connected to the network
531 Status
= EFI_NO_MEDIA
;
535 // Attempt the connection to the remote system
537 Status
= pTcp4Protocol
->Connect ( pTcp4Protocol
,
538 &pTcp4
->ConnectToken
);
541 if ( EFI_ERROR ( Status
)) {
545 DEBUG (( DEBUG_CONNECT
,
546 "ERROR - Port 0x%08x not connected, Status: %r\r\n",
551 if ( !EFI_ERROR ( Status
)) {
553 // Connection in progress
555 pSocket
->errno
= EINPROGRESS
;
556 DEBUG (( DEBUG_CONNECT
,
557 "0x%08x: Port attempting connection to %d.%d.%d.%d:%d\r\n",
559 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[0],
560 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[1],
561 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[2],
562 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[3],
563 pTcp4
->ConfigData
.AccessPoint
.RemotePort
));
567 // Error return path is through EslTcp4ConnectComplete to
568 // enable retry on other ports
570 // Status to errno translation gets done in EslTcp4ConnectPoll
572 pTcp4
->ConnectToken
.CompletionToken
.Status
= Status
;
575 // Continue with the next port
577 gBS
->CheckEvent ( pTcp4
->ConnectToken
.CompletionToken
.Event
);
578 gBS
->SignalEvent ( pTcp4
->ConnectToken
.CompletionToken
.Event
);
580 Status
= EFI_NOT_READY
;
584 // No more local adapters available
586 pSocket
->errno
= ENETUNREACH
;
587 Status
= EFI_NO_RESPONSE
;
591 // Return the operation status
593 DBG_EXIT_STATUS ( Status
);
599 Establish the known port to listen for network connections.
601 This routine places the port into a state that enables connection
604 This routine is called by ::EslSocketListen to handle the network
605 specifics of the listen operation for SOCK_STREAM and SOCK_SEQPACKET
606 sockets. See the \ref ConnectionManagement section.
608 @param [in] pSocket Address of an ::ESL_SOCKET structure.
610 @retval EFI_SUCCESS - Socket successfully created
611 @retval Other - Failed to enable the socket for listen
616 IN ESL_SOCKET
* pSocket
619 ESL_PORT
* pNextPort
;
621 ESL_TCP4_CONTEXT
* pTcp4
;
622 EFI_TCP4_PROTOCOL
* pTcp4Protocol
;
628 // Verify the socket layer synchronization
630 VERIFY_TPL ( TPL_SOCKETS
);
633 // Use for/break instead of goto
637 // Assume no ports are available
639 pSocket
->errno
= EOPNOTSUPP
;
640 Status
= EFI_NOT_READY
;
643 // Walk the list of ports
645 pPort
= pSocket
->pPortList
;
646 while ( NULL
!= pPort
) {
653 // Use for/break insteak of goto
657 // Create the listen completion event
659 pTcp4
= &pPort
->Context
.Tcp4
;
660 Status
= gBS
->CreateEvent ( EVT_NOTIFY_SIGNAL
,
662 (EFI_EVENT_NOTIFY
)EslTcp4ListenComplete
,
664 &pTcp4
->ListenToken
.CompletionToken
.Event
);
665 if ( EFI_ERROR ( Status
)) {
666 DEBUG (( DEBUG_ERROR
| DEBUG_LISTEN
,
667 "ERROR - Failed to create the listen completion event, Status: %r\r\n",
669 pSocket
->errno
= ENOMEM
;
673 "0x%08x: Created listen completion event\r\n",
674 pTcp4
->ListenToken
.CompletionToken
.Event
));
677 // Configure the port
679 pTcp4Protocol
= pPort
->pProtocol
.TCPv4
;
680 Status
= pTcp4Protocol
->Configure ( pTcp4Protocol
,
681 &pTcp4
->ConfigData
);
682 if ( EFI_ERROR ( Status
)) {
683 DEBUG (( DEBUG_LISTEN
,
684 "ERROR - Failed to configure the Tcp4 port, Status: %r\r\n",
687 case EFI_ACCESS_DENIED
:
688 pSocket
->errno
= EACCES
;
692 case EFI_DEVICE_ERROR
:
693 pSocket
->errno
= EIO
;
696 case EFI_INVALID_PARAMETER
:
697 pSocket
->errno
= EADDRNOTAVAIL
;
701 pSocket
->errno
= EAFNOSUPPORT
;
704 case EFI_OUT_OF_RESOURCES
:
705 pSocket
->errno
= ENOBUFS
;
708 case EFI_UNSUPPORTED
:
709 pSocket
->errno
= EOPNOTSUPP
;
714 DEBUG (( DEBUG_LISTEN
,
715 "0x%08x: Port configured\r\n",
717 pPort
->bConfigured
= TRUE
;
720 // Start the listen operation on the port
722 Status
= pTcp4Protocol
->Accept ( pTcp4Protocol
,
723 &pTcp4
->ListenToken
);
724 if ( EFI_ERROR ( Status
)) {
725 DEBUG (( DEBUG_LISTEN
,
726 "ERROR - Failed Tcp4 accept, Status: %r\r\n",
729 case EFI_ACCESS_DENIED
:
730 pSocket
->errno
= EACCES
;
734 case EFI_DEVICE_ERROR
:
735 pSocket
->errno
= EIO
;
738 case EFI_INVALID_PARAMETER
:
739 pSocket
->errno
= EADDRNOTAVAIL
;
742 case EFI_NOT_STARTED
:
743 pSocket
->errno
= ENETDOWN
;
746 case EFI_OUT_OF_RESOURCES
:
747 pSocket
->errno
= ENOBUFS
;
752 DEBUG (( DEBUG_LISTEN
,
753 "0x%08x: Listen pending on Port\r\n",
757 // Listen is pending on this port
765 pNextPort
= pPort
->pLinkSocket
;
768 // Close the port upon error
770 if ( EFI_ERROR ( Status
)) {
771 EslSocketPortCloseStart ( pPort
, TRUE
, DEBUG_LISTEN
);
781 // Determine if any ports are in the listen state
783 if ( NULL
== pSocket
->pPortList
) {
785 // No ports in the listen state
787 pSocket
->MaxFifoDepth
= 0;
790 // Return the last error detected
796 // Mark the socket as configured
798 pSocket
->bConfigured
= TRUE
;
799 Status
= EFI_SUCCESS
;
805 DEBUG (( DEBUG_LISTEN
,
806 "0x%08x: pSocket - Listen pending on socket\r\n",
812 // Return the operation status
814 DBG_EXIT_STATUS ( Status
);
820 Process the connection attempt
822 A system has initiated a connection attempt with a socket in the
823 listen state. Attempt to complete the connection.
825 The TCPv4 layer calls this routine when a connection is made to
826 the socket in the listen state. See the
827 \ref ConnectionManagement section.
829 @param [in] Event The listen completion event
831 @param [in] pPort Address of an ::ESL_PORT structure.
835 EslTcp4ListenComplete (
840 EFI_HANDLE ChildHandle
;
841 struct sockaddr_in LocalAddress
;
842 EFI_TCP4_CONFIG_DATA
* pConfigData
;
844 ESL_SOCKET
* pNewSocket
;
845 ESL_SOCKET
* pSocket
;
846 ESL_TCP4_CONTEXT
* pTcp4
;
847 EFI_TCP4_PROTOCOL
* pTcp4Protocol
;
849 EFI_HANDLE TcpPortHandle
;
850 EFI_STATUS TempStatus
;
853 VERIFY_AT_TPL ( TPL_SOCKETS
);
858 Status
= EFI_SUCCESS
;
861 // Determine if this connection fits into the connection FIFO
863 pSocket
= pPort
->pSocket
;
864 TcpPortHandle
= pPort
->Context
.Tcp4
.ListenToken
.NewChildHandle
;
865 if (( SOCKET_STATE_LISTENING
== pSocket
->State
)
866 && ( pSocket
->MaxFifoDepth
> pSocket
->FifoDepth
)) {
868 // Allocate a socket for this connection
871 Status
= EslSocketAllocate ( &ChildHandle
,
874 if ( !EFI_ERROR ( Status
)) {
876 // Clone the socket parameters
878 pNewSocket
->pApi
= pSocket
->pApi
;
879 pNewSocket
->Domain
= pSocket
->Domain
;
880 pNewSocket
->Protocol
= pSocket
->Protocol
;
881 pNewSocket
->Type
= pSocket
->Type
;
884 // Build the local address
886 pTcp4
= &pPort
->Context
.Tcp4
;
887 LocalAddress
.sin_len
= (uint8_t)pNewSocket
->pApi
->MinimumAddressLength
;
888 LocalAddress
.sin_family
= AF_INET
;
889 LocalAddress
.sin_port
= 0;
890 LocalAddress
.sin_addr
.s_addr
= *(UINT32
*)&pTcp4
->ConfigData
.AccessPoint
.StationAddress
.Addr
[0];
893 // Allocate a port for this connection
894 // Note in this instance Configure may not be called with NULL!
896 Status
= EslSocketPortAllocate ( pNewSocket
,
899 (struct sockaddr
*)&LocalAddress
,
903 if ( !EFI_ERROR ( Status
)) {
905 // Restart the listen operation on the port
907 pTcp4Protocol
= pPort
->pProtocol
.TCPv4
;
908 Status
= pTcp4Protocol
->Accept ( pTcp4Protocol
,
909 &pTcp4
->ListenToken
);
912 // Close the TCP port using SocketClose
914 TcpPortHandle
= NULL
;
915 pTcp4
= &pNewPort
->Context
.Tcp4
;
918 // Check for an accept call error
920 if ( !EFI_ERROR ( Status
)) {
922 // Get the port configuration
924 pNewPort
->bConfigured
= TRUE
;
925 pConfigData
= &pTcp4
->ConfigData
;
926 pConfigData
->ControlOption
= &pTcp4
->Option
;
927 pTcp4Protocol
= pNewPort
->pProtocol
.TCPv4
;
928 Status
= pTcp4Protocol
->GetModeData ( pTcp4Protocol
,
934 if ( !EFI_ERROR ( Status
)) {
936 // Add the new socket to the connection FIFO
938 if ( NULL
== pSocket
->pFifoTail
) {
942 pSocket
->pFifoHead
= pNewSocket
;
946 // Add to end of list.
948 pSocket
->pFifoTail
->pNextConnection
= pNewSocket
;
950 pSocket
->pFifoTail
= pNewSocket
;
951 pSocket
->FifoDepth
+= 1;
954 // Update the socket state
956 pNewSocket
->State
= SOCKET_STATE_IN_FIFO
;
959 // Log the connection
961 DEBUG (( DEBUG_CONNECTION
| DEBUG_INFO
,
962 "0x%08x: Socket on port %d.%d.%d.%d:%d connected to %d.%d.%d.%d:%d\r\n",
964 pConfigData
->AccessPoint
.StationAddress
.Addr
[0],
965 pConfigData
->AccessPoint
.StationAddress
.Addr
[1],
966 pConfigData
->AccessPoint
.StationAddress
.Addr
[2],
967 pConfigData
->AccessPoint
.StationAddress
.Addr
[3],
968 pConfigData
->AccessPoint
.StationPort
,
969 pConfigData
->AccessPoint
.RemoteAddress
.Addr
[0],
970 pConfigData
->AccessPoint
.RemoteAddress
.Addr
[1],
971 pConfigData
->AccessPoint
.RemoteAddress
.Addr
[2],
972 pConfigData
->AccessPoint
.RemoteAddress
.Addr
[3],
973 pConfigData
->AccessPoint
.RemotePort
));
974 DEBUG (( DEBUG_CONNECTION
| DEBUG_INFO
,
975 "0x%08x: Listen socket adding socket 0x%08x to FIFO, depth: %d\r\n",
978 pSocket
->FifoDepth
));
981 // Start the receive operation
983 EslSocketRxStart ( pNewPort
);
986 DEBUG (( DEBUG_ERROR
| DEBUG_CONNECTION
| DEBUG_INFO
,
987 "ERROR - GetModeData failed on port 0x%08x, Status: %r\r\n",
994 // The listen failed on this port
996 DEBUG (( DEBUG_LISTEN
| DEBUG_INFO
,
997 "ERROR - Listen failed on port 0x%08x, Status: %r\r\n",
1002 // Close the listening port
1004 EslSocketPortCloseStart ( pPort
, TRUE
, DEBUG_LISTEN
);
1009 // Done with the socket if necessary
1011 if ( EFI_ERROR ( Status
)) {
1012 TempStatus
= EslSocketCloseStart ( &pNewSocket
->SocketProtocol
,
1015 ASSERT ( EFI_SUCCESS
== TempStatus
);
1020 DEBUG (( DEBUG_CONNECTION
,
1021 "0x%08x: Socket FIFO full, connection refused\r\n",
1025 // The FIFO is full or the socket is in the wrong state
1027 Status
= EFI_BUFFER_TOO_SMALL
;
1031 // Close the connection if necessary
1033 if (( EFI_ERROR ( Status
))
1034 && ( NULL
== TcpPortHandle
)) {
1036 // TODO: Finish this code path
1037 // The new connection does not fit into the connection FIFO
1041 // Release the resources
1050 Get the local socket address.
1052 This routine returns the IPv4 address and TCP port number associated
1053 with the local socket.
1055 This routine is called by ::EslSocketGetLocalAddress to determine the
1056 network address for the SOCK_STREAM or SOCK_SEQPACKET socket.
1058 @param [in] pPort Address of an ::ESL_PORT structure.
1060 @param [out] pSockAddr Network address to receive the local system address
1064 EslTcp4LocalAddressGet (
1065 IN ESL_PORT
* pPort
,
1066 OUT
struct sockaddr
* pSockAddr
1069 struct sockaddr_in
* pLocalAddress
;
1070 ESL_TCP4_CONTEXT
* pTcp4
;
1075 // Return the local address
1077 pTcp4
= &pPort
->Context
.Tcp4
;
1078 pLocalAddress
= (struct sockaddr_in
*)pSockAddr
;
1079 pLocalAddress
->sin_family
= AF_INET
;
1080 pLocalAddress
->sin_port
= SwapBytes16 ( pTcp4
->ConfigData
.AccessPoint
.StationPort
);
1081 CopyMem ( &pLocalAddress
->sin_addr
,
1082 &pTcp4
->ConfigData
.AccessPoint
.StationAddress
.Addr
[0],
1083 sizeof ( pLocalAddress
->sin_addr
));
1090 Set the local port address.
1092 This routine sets the local port address.
1094 This support routine is called by ::EslSocketPortAllocate.
1096 @param [in] pPort Address of an ESL_PORT structure
1097 @param [in] pSockAddr Address of a sockaddr structure that contains the
1098 connection point on the local machine. An IPv4 address
1099 of INADDR_ANY specifies that the connection is made to
1100 all of the network stacks on the platform. Specifying a
1101 specific IPv4 address restricts the connection to the
1102 network stack supporting that address. Specifying zero
1103 for the port causes the network layer to assign a port
1104 number from the dynamic range. Specifying a specific
1105 port number causes the network layer to use that port.
1107 @param [in] bBindTest TRUE = run bind testing
1109 @retval EFI_SUCCESS The operation was successful
1113 EslTcp4LocalAddressSet (
1114 IN ESL_PORT
* pPort
,
1115 IN CONST
struct sockaddr
* pSockAddr
,
1116 IN BOOLEAN bBindTest
1119 EFI_TCP4_ACCESS_POINT
* pAccessPoint
;
1120 CONST
struct sockaddr_in
* pIpAddress
;
1121 CONST UINT8
* pIpv4Address
;
1127 // Validate the address
1129 pIpAddress
= (struct sockaddr_in
*)pSockAddr
;
1130 if ( INADDR_BROADCAST
== pIpAddress
->sin_addr
.s_addr
) {
1132 // The local address must not be the broadcast address
1134 Status
= EFI_INVALID_PARAMETER
;
1135 pPort
->pSocket
->errno
= EADDRNOTAVAIL
;
1139 // Set the local address
1141 pIpv4Address
= (UINT8
*)&pIpAddress
->sin_addr
.s_addr
;
1142 pAccessPoint
= &pPort
->Context
.Tcp4
.ConfigData
.AccessPoint
;
1143 pAccessPoint
->StationAddress
.Addr
[0] = pIpv4Address
[0];
1144 pAccessPoint
->StationAddress
.Addr
[1] = pIpv4Address
[1];
1145 pAccessPoint
->StationAddress
.Addr
[2] = pIpv4Address
[2];
1146 pAccessPoint
->StationAddress
.Addr
[3] = pIpv4Address
[3];
1149 // Determine if the default address is used
1151 pAccessPoint
->UseDefaultAddress
= (BOOLEAN
)( 0 == pIpAddress
->sin_addr
.s_addr
);
1154 // Set the subnet mask
1156 if ( pAccessPoint
->UseDefaultAddress
) {
1157 pAccessPoint
->SubnetMask
.Addr
[0] = 0;
1158 pAccessPoint
->SubnetMask
.Addr
[1] = 0;
1159 pAccessPoint
->SubnetMask
.Addr
[2] = 0;
1160 pAccessPoint
->SubnetMask
.Addr
[3] = 0;
1163 pAccessPoint
->SubnetMask
.Addr
[0] = 0xff;
1164 pAccessPoint
->SubnetMask
.Addr
[1] = ( 128 <= pAccessPoint
->StationAddress
.Addr
[0]) ? 0xff : 0;
1165 pAccessPoint
->SubnetMask
.Addr
[2] = ( 192 <= pAccessPoint
->StationAddress
.Addr
[0]) ? 0xff : 0;
1166 pAccessPoint
->SubnetMask
.Addr
[3] = ( 224 <= pAccessPoint
->StationAddress
.Addr
[0]) ? 0xff : 0;
1170 // Validate the IP address
1172 pAccessPoint
->StationPort
= 0;
1173 Status
= bBindTest
? EslSocketBindTest ( pPort
, EADDRNOTAVAIL
)
1175 if ( !EFI_ERROR ( Status
)) {
1177 // Set the port number
1179 pAccessPoint
->StationPort
= SwapBytes16 ( pIpAddress
->sin_port
);
1180 pPort
->pSocket
->bAddressSet
= TRUE
;
1183 // Display the local address
1185 DEBUG (( DEBUG_BIND
,
1186 "0x%08x: Port, Local TCP4 Address: %d.%d.%d.%d:%d\r\n",
1188 pAccessPoint
->StationAddress
.Addr
[0],
1189 pAccessPoint
->StationAddress
.Addr
[1],
1190 pAccessPoint
->StationAddress
.Addr
[2],
1191 pAccessPoint
->StationAddress
.Addr
[3],
1192 pAccessPoint
->StationPort
));
1197 // Return the operation status
1199 DBG_EXIT_STATUS ( Status
);
1205 Free a receive packet
1207 This routine performs the network specific operations necessary
1208 to free a receive packet.
1210 This routine is called by ::EslSocketPortCloseTxDone to free a
1213 @param [in] pPacket Address of an ::ESL_PACKET structure.
1214 @param [in, out] pRxBytes Address of the count of RX bytes
1219 IN ESL_PACKET
* pPacket
,
1220 IN OUT
size_t * pRxBytes
1226 // Account for the receive bytes
1228 *pRxBytes
-= pPacket
->Op
.Tcp4Rx
.RxData
.DataLength
;
1234 Initialize the network specific portions of an ::ESL_PORT structure.
1236 This routine initializes the network specific portions of an
1237 ::ESL_PORT structure for use by the socket.
1239 This support routine is called by ::EslSocketPortAllocate
1240 to connect the socket with the underlying network adapter
1241 running the TCPv4 protocol.
1243 @param [in] pPort Address of an ESL_PORT structure
1244 @param [in] DebugFlags Flags for debug messages
1246 @retval EFI_SUCCESS - Socket successfully created
1250 EslTcp4PortAllocate (
1251 IN ESL_PORT
* pPort
,
1255 EFI_TCP4_ACCESS_POINT
* pAccessPoint
;
1256 ESL_SOCKET
* pSocket
;
1257 ESL_TCP4_CONTEXT
* pTcp4
;
1263 // Use for/break instead of goto
1266 // Allocate the close event
1268 pSocket
= pPort
->pSocket
;
1269 pTcp4
= &pPort
->Context
.Tcp4
;
1270 Status
= gBS
->CreateEvent ( EVT_NOTIFY_SIGNAL
,
1272 (EFI_EVENT_NOTIFY
)EslSocketPortCloseComplete
,
1274 &pTcp4
->CloseToken
.CompletionToken
.Event
);
1275 if ( EFI_ERROR ( Status
)) {
1276 DEBUG (( DEBUG_ERROR
| DebugFlags
,
1277 "ERROR - Failed to create the close event, Status: %r\r\n",
1279 pSocket
->errno
= ENOMEM
;
1282 DEBUG (( DEBUG_CLOSE
| DEBUG_POOL
,
1283 "0x%08x: Created close event\r\n",
1284 pTcp4
->CloseToken
.CompletionToken
.Event
));
1287 // Allocate the connection event
1289 Status
= gBS
->CreateEvent ( EVT_NOTIFY_SIGNAL
,
1291 (EFI_EVENT_NOTIFY
)EslTcp4ConnectComplete
,
1293 &pTcp4
->ConnectToken
.CompletionToken
.Event
);
1294 if ( EFI_ERROR ( Status
)) {
1295 DEBUG (( DEBUG_ERROR
| DebugFlags
,
1296 "ERROR - Failed to create the connect event, Status: %r\r\n",
1298 pSocket
->errno
= ENOMEM
;
1301 DEBUG (( DEBUG_CLOSE
| DEBUG_POOL
,
1302 "0x%08x: Created connect event\r\n",
1303 pTcp4
->ConnectToken
.CompletionToken
.Event
));
1306 // Initialize the port
1308 pSocket
->TxPacketOffset
= OFFSET_OF ( ESL_PACKET
, Op
.Tcp4Tx
.TxData
);
1309 pSocket
->TxTokenEventOffset
= OFFSET_OF ( ESL_IO_MGMT
, Token
.Tcp4Tx
.CompletionToken
.Event
);
1310 pSocket
->TxTokenOffset
= OFFSET_OF ( EFI_TCP4_IO_TOKEN
, Packet
.TxData
);
1313 // Save the cancel, receive and transmit addresses
1314 // pPort->pfnRxCancel = NULL; since the UEFI implementation returns EFI_UNSUPPORTED
1316 pPort
->pfnConfigure
= (PFN_NET_CONFIGURE
)pPort
->pProtocol
.TCPv4
->Configure
;
1317 pPort
->pfnRxPoll
= (PFN_NET_POLL
)pPort
->pProtocol
.TCPv4
->Poll
;
1318 pPort
->pfnRxStart
= (PFN_NET_IO_START
)pPort
->pProtocol
.TCPv4
->Receive
;
1319 pPort
->pfnTxStart
= (PFN_NET_IO_START
)pPort
->pProtocol
.TCPv4
->Transmit
;
1322 // Set the configuration flags
1324 pAccessPoint
= &pPort
->Context
.Tcp4
.ConfigData
.AccessPoint
;
1325 pAccessPoint
->ActiveFlag
= FALSE
;
1326 pTcp4
->ConfigData
.TimeToLive
= 255;
1331 // Return the operation status
1333 DBG_EXIT_STATUS ( Status
);
1341 This routine releases the network specific resources allocated by
1342 ::EslTcp4PortAllocate.
1344 This routine is called by ::EslSocketPortClose.
1345 See the \ref PortCloseStateMachine section.
1347 @param [in] pPort Address of an ::ESL_PORT structure.
1349 @retval EFI_SUCCESS The port is closed
1350 @retval other Port close error
1359 ESL_TCP4_CONTEXT
* pTcp4
;
1365 // Locate the port in the socket list
1367 Status
= EFI_SUCCESS
;
1368 DebugFlags
= pPort
->DebugFlags
;
1369 pTcp4
= &pPort
->Context
.Tcp4
;
1372 // Done with the connect event
1374 if ( NULL
!= pTcp4
->ConnectToken
.CompletionToken
.Event
) {
1375 Status
= gBS
->CloseEvent ( pTcp4
->ConnectToken
.CompletionToken
.Event
);
1376 if ( !EFI_ERROR ( Status
)) {
1377 DEBUG (( DebugFlags
| DEBUG_POOL
,
1378 "0x%08x: Closed connect event\r\n",
1379 pTcp4
->ConnectToken
.CompletionToken
.Event
));
1382 DEBUG (( DEBUG_ERROR
| DebugFlags
,
1383 "ERROR - Failed to close the connect event, Status: %r\r\n",
1385 ASSERT ( EFI_SUCCESS
== Status
);
1390 // Done with the close event
1392 if ( NULL
!= pTcp4
->CloseToken
.CompletionToken
.Event
) {
1393 Status
= gBS
->CloseEvent ( pTcp4
->CloseToken
.CompletionToken
.Event
);
1394 if ( !EFI_ERROR ( Status
)) {
1395 DEBUG (( DebugFlags
| DEBUG_POOL
,
1396 "0x%08x: Closed close event\r\n",
1397 pTcp4
->CloseToken
.CompletionToken
.Event
));
1400 DEBUG (( DEBUG_ERROR
| DebugFlags
,
1401 "ERROR - Failed to close the close event, Status: %r\r\n",
1403 ASSERT ( EFI_SUCCESS
== Status
);
1408 // Done with the listen completion event
1410 if ( NULL
!= pTcp4
->ListenToken
.CompletionToken
.Event
) {
1411 Status
= gBS
->CloseEvent ( pTcp4
->ListenToken
.CompletionToken
.Event
);
1412 if ( !EFI_ERROR ( Status
)) {
1413 DEBUG (( DebugFlags
| DEBUG_POOL
,
1414 "0x%08x: Closed listen completion event\r\n",
1415 pTcp4
->ListenToken
.CompletionToken
.Event
));
1418 DEBUG (( DEBUG_ERROR
| DebugFlags
,
1419 "ERROR - Failed to close the listen completion event, Status: %r\r\n",
1421 ASSERT ( EFI_SUCCESS
== Status
);
1426 // Return the operation status
1428 DBG_EXIT_STATUS ( Status
);
1434 Perform the network specific close operation on the port.
1436 This routine performs a cancel operations on the TCPv4 port to
1437 shutdown the receive operations on the port.
1439 This routine is called by the ::EslSocketPortCloseTxDone
1440 routine after the port completes all of the transmission.
1442 @param [in] pPort Address of an ::ESL_PORT structure.
1444 @retval EFI_SUCCESS The port is closed, not normally returned
1445 @retval EFI_NOT_READY The port is still closing
1446 @retval EFI_ALREADY_STARTED Error, the port is in the wrong state,
1447 most likely the routine was called already.
1451 EslTcp4PortCloseOp (
1455 ESL_TCP4_CONTEXT
* pTcp4
;
1456 EFI_TCP4_PROTOCOL
* pTcp4Protocol
;
1462 // Close the configured port
1464 Status
= EFI_SUCCESS
;
1465 pTcp4
= &pPort
->Context
.Tcp4
;
1466 pTcp4Protocol
= pPort
->pProtocol
.TCPv4
;
1467 pTcp4
->CloseToken
.AbortOnClose
= pPort
->bCloseNow
;
1468 Status
= pTcp4Protocol
->Close ( pTcp4Protocol
,
1469 &pTcp4
->CloseToken
);
1470 if ( !EFI_ERROR ( Status
)) {
1471 DEBUG (( pPort
->DebugFlags
| DEBUG_CLOSE
| DEBUG_INFO
,
1472 "0x%08x: Port close started\r\n",
1476 DEBUG (( DEBUG_ERROR
| pPort
->DebugFlags
| DEBUG_CLOSE
| DEBUG_INFO
,
1477 "ERROR - Close failed on port 0x%08x, Status: %r\r\n",
1483 // Return the operation status
1485 DBG_EXIT_STATUS ( Status
);
1491 Receive data from a network connection.
1493 This routine attempts to return buffered data to the caller. The
1494 data is removed from the urgent queue if the message flag MSG_OOB
1495 is specified, otherwise data is removed from the normal queue.
1496 See the \ref ReceiveEngine section.
1498 This routine is called by ::EslSocketReceive to handle the network
1499 specific receive operation to support SOCK_STREAM and SOCK_SEQPACKET
1502 @param [in] pPort Address of an ::ESL_PORT structure.
1504 @param [in] pPacket Address of an ::ESL_PACKET structure.
1506 @param [in] pbConsumePacket Address of a BOOLEAN indicating if the packet is to be consumed
1508 @param [in] BufferLength Length of the the buffer
1510 @param [in] pBuffer Address of a buffer to receive the data.
1512 @param [in] pDataLength Number of received data bytes in the buffer.
1514 @param [out] pAddress Network address to receive the remote system address
1516 @param [out] pSkipBytes Address to receive the number of bytes skipped
1518 @return Returns the address of the next free byte in the buffer.
1523 IN ESL_PORT
* pPort
,
1524 IN ESL_PACKET
* pPacket
,
1525 IN BOOLEAN
* pbConsumePacket
,
1526 IN
size_t BufferLength
,
1528 OUT
size_t * pDataLength
,
1529 OUT
struct sockaddr
* pAddress
,
1530 OUT
size_t * pSkipBytes
1534 struct sockaddr_in
* pRemoteAddress
;
1535 ESL_TCP4_CONTEXT
* pTcp4
;
1540 // Return the remote system address if requested
1542 if ( NULL
!= pAddress
) {
1544 // Build the remote address
1546 pTcp4
= &pPort
->Context
.Tcp4
;
1548 "Getting packet remote address: %d.%d.%d.%d:%d\r\n",
1549 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[0],
1550 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[1],
1551 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[2],
1552 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[3],
1553 pTcp4
->ConfigData
.AccessPoint
.RemotePort
));
1554 pRemoteAddress
= (struct sockaddr_in
*)pAddress
;
1555 CopyMem ( &pRemoteAddress
->sin_addr
,
1556 &pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[0],
1557 sizeof ( pRemoteAddress
->sin_addr
));
1558 pRemoteAddress
->sin_port
= SwapBytes16 ( pTcp4
->ConfigData
.AccessPoint
.RemotePort
);
1562 // Determine the amount of received data
1564 DataLength
= pPacket
->ValidBytes
;
1565 if ( BufferLength
< DataLength
) {
1566 DataLength
= BufferLength
;
1570 // Move the data into the buffer
1573 "0x%08x: Port copy packet 0x%08x data into 0x%08x, 0x%08x bytes\r\n",
1578 CopyMem ( pBuffer
, pPacket
->pBuffer
, DataLength
);
1581 // Set the next buffer address
1583 pBuffer
+= DataLength
;
1586 // Determine if the data is being read
1588 if ( *pbConsumePacket
) {
1590 // Account for the bytes consumed
1592 pPacket
->pBuffer
+= DataLength
;
1593 pPacket
->ValidBytes
-= DataLength
;
1595 "0x%08x: Port account for 0x%08x bytes\r\n",
1600 // Determine if the entire packet was consumed
1602 if (( 0 == pPacket
->ValidBytes
)
1603 || ( SOCK_STREAM
!= pPort
->pSocket
->Type
)) {
1605 // All done with this packet
1606 // Account for any discarded data
1608 *pSkipBytes
= pPacket
->ValidBytes
;
1613 // More data to consume later
1615 *pbConsumePacket
= FALSE
;
1620 // Return the data length and the buffer address
1622 *pDataLength
= DataLength
;
1623 DBG_EXIT_HEX ( pBuffer
);
1629 Get the remote socket address.
1631 This routine returns the address of the remote connection point
1632 associated with the SOCK_STREAM or SOCK_SEQPACKET socket.
1634 This routine is called by ::EslSocketGetPeerAddress to detemine
1635 the TCPv4 address and por number associated with the network adapter.
1637 @param [in] pPort Address of an ::ESL_PORT structure.
1639 @param [out] pAddress Network address to receive the remote system address
1643 EslTcp4RemoteAddressGet (
1644 IN ESL_PORT
* pPort
,
1645 OUT
struct sockaddr
* pAddress
1648 struct sockaddr_in
* pRemoteAddress
;
1649 ESL_TCP4_CONTEXT
* pTcp4
;
1654 // Return the remote address
1656 pTcp4
= &pPort
->Context
.Tcp4
;
1657 pRemoteAddress
= (struct sockaddr_in
*)pAddress
;
1658 pRemoteAddress
->sin_family
= AF_INET
;
1659 pRemoteAddress
->sin_port
= SwapBytes16 ( pTcp4
->ConfigData
.AccessPoint
.RemotePort
);
1660 CopyMem ( &pRemoteAddress
->sin_addr
,
1661 &pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[0],
1662 sizeof ( pRemoteAddress
->sin_addr
));
1669 Set the remote address
1671 This routine sets the remote address in the port.
1673 This routine is called by ::EslSocketConnect to specify the
1674 remote network address.
1676 @param [in] pPort Address of an ::ESL_PORT structure.
1678 @param [in] pSockAddr Network address of the remote system.
1680 @param [in] SockAddrLength Length in bytes of the network address.
1682 @retval EFI_SUCCESS The operation was successful
1686 EslTcp4RemoteAddressSet (
1687 IN ESL_PORT
* pPort
,
1688 IN CONST
struct sockaddr
* pSockAddr
,
1689 IN socklen_t SockAddrLength
1692 CONST
struct sockaddr_in
* pRemoteAddress
;
1693 ESL_TCP4_CONTEXT
* pTcp4
;
1699 // Set the remote address
1701 pTcp4
= &pPort
->Context
.Tcp4
;
1702 pRemoteAddress
= (struct sockaddr_in
*)pSockAddr
;
1703 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[0] = (UINT8
)( pRemoteAddress
->sin_addr
.s_addr
);
1704 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[1] = (UINT8
)( pRemoteAddress
->sin_addr
.s_addr
>> 8 );
1705 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[2] = (UINT8
)( pRemoteAddress
->sin_addr
.s_addr
>> 16 );
1706 pTcp4
->ConfigData
.AccessPoint
.RemoteAddress
.Addr
[3] = (UINT8
)( pRemoteAddress
->sin_addr
.s_addr
>> 24 );
1707 pTcp4
->ConfigData
.AccessPoint
.RemotePort
= SwapBytes16 ( pRemoteAddress
->sin_port
);
1708 Status
= EFI_SUCCESS
;
1709 if ( INADDR_BROADCAST
== pRemoteAddress
->sin_addr
.s_addr
) {
1710 DEBUG (( DEBUG_CONNECT
,
1711 "ERROR - Invalid remote address\r\n" ));
1712 Status
= EFI_INVALID_PARAMETER
;
1713 pPort
->pSocket
->errno
= EAFNOSUPPORT
;
1717 // Return the operation status
1719 DBG_EXIT_STATUS ( Status
);
1725 Process the receive completion
1727 This routine queues the data in FIFO order in either the urgent
1728 or normal data queues depending upon the type of data received.
1729 See the \ref ReceiveEngine section.
1731 This routine is called by the TCPv4 driver when some data is
1734 Buffer the data that was just received.
1736 @param [in] Event The receive completion event
1738 @param [in] pIo Address of an ::ESL_IO_MGMT structure
1744 IN ESL_IO_MGMT
* pIo
1748 size_t LengthInBytes
;
1749 ESL_PACKET
* pPacket
;
1755 // Get the operation status.
1757 Status
= pIo
->Token
.Tcp4Rx
.CompletionToken
.Status
;
1760 // +--------------------+ +---------------------------+
1761 // | ESL_IO_MGMT | | ESL_PACKET |
1763 // | +---------------+ +-----------------------+ |
1764 // | | Token | | EFI_TCP4_RECEIVE_DATA | |
1765 // | | RxData --> | | |
1766 // | | | +-----------------------+---+
1767 // | | Event | | Data Buffer |
1768 // +----+---------------+ | |
1770 // +---------------------------+
1773 // Duplicate the buffer address and length for use by the
1774 // buffer handling code in EslTcp4Receive. These fields are
1775 // used when a partial read is done of the data from the
1778 pPacket
= pIo
->pPacket
;
1779 pPacket
->pBuffer
= pPacket
->Op
.Tcp4Rx
.RxData
.FragmentTable
[0].FragmentBuffer
;
1780 LengthInBytes
= pPacket
->Op
.Tcp4Rx
.RxData
.DataLength
;
1781 pPacket
->ValidBytes
= LengthInBytes
;
1784 // Get the data type so that it may be linked to the
1785 // correct receive buffer list on the ESL_SOCKET structure
1787 bUrgent
= pPacket
->Op
.Tcp4Rx
.RxData
.UrgentFlag
;
1790 // Complete this request
1792 EslSocketRxComplete ( pIo
, Status
, LengthInBytes
, bUrgent
);
1798 Start a receive operation
1800 This routine posts a receive buffer to the TCPv4 driver.
1801 See the \ref ReceiveEngine section.
1803 This support routine is called by EslSocketRxStart.
1805 @param [in] pPort Address of an ::ESL_PORT structure.
1806 @param [in] pIo Address of an ::ESL_IO_MGMT structure.
1811 IN ESL_PORT
* pPort
,
1812 IN ESL_IO_MGMT
* pIo
1815 ESL_PACKET
* pPacket
;
1820 // Initialize the buffer for receive
1822 pPacket
= pIo
->pPacket
;
1823 pIo
->Token
.Tcp4Rx
.Packet
.RxData
= &pPacket
->Op
.Tcp4Rx
.RxData
;
1824 pPacket
->Op
.Tcp4Rx
.RxData
.DataLength
= sizeof ( pPacket
->Op
.Tcp4Rx
.Buffer
);
1825 pPacket
->Op
.Tcp4Rx
.RxData
.FragmentCount
= 1;
1826 pPacket
->Op
.Tcp4Rx
.RxData
.FragmentTable
[0].FragmentLength
= pPacket
->Op
.Tcp4Rx
.RxData
.DataLength
;
1827 pPacket
->Op
.Tcp4Rx
.RxData
.FragmentTable
[0].FragmentBuffer
= &pPacket
->Op
.Tcp4Rx
.Buffer
[0];
1834 Determine if the socket is configured.
1836 This routine uses the flag ESL_SOCKET::bConfigured to determine
1837 if the network layer's configuration routine has been called.
1839 This routine is called by EslSocketIsConfigured to verify
1840 that the socket has been configured.
1842 @param [in] pSocket Address of an ::ESL_SOCKET structure.
1844 @retval EFI_SUCCESS - The port is connected
1845 @retval EFI_NOT_STARTED - The port is not connected
1849 EslTcp4SocketIsConfigured (
1850 IN ESL_SOCKET
* pSocket
1858 // Determine the socket configuration status
1860 Status
= pSocket
->bConfigured
? EFI_SUCCESS
: EFI_NOT_STARTED
;
1863 // Return the port connected state.
1865 DBG_EXIT_STATUS ( Status
);
1871 Buffer data for transmission over a network connection.
1873 This routine buffers data for the transmit engine in one of two
1874 queues, one for urgent (out-of-band) data and the other for normal
1875 data. The urgent data is provided to TCP as soon as it is available,
1876 allowing the TCP layer to schedule transmission of the urgent data
1877 between packets of normal data.
1879 This routine is called by ::EslSocketTransmit to buffer
1880 data for transmission. When the \ref TransmitEngine has resources,
1881 this routine will start the transmission of the next buffer on
1882 the network connection.
1884 Transmission errors are returned during the next transmission or
1885 during the close operation. Only buffering errors are returned
1886 during the current transmission attempt.
1888 @param [in] pSocket Address of an ::ESL_SOCKET structure
1890 @param [in] Flags Message control flags
1892 @param [in] BufferLength Length of the the buffer
1894 @param [in] pBuffer Address of a buffer to receive the data.
1896 @param [in] pDataLength Number of received data bytes in the buffer.
1898 @param [in] pAddress Network address of the remote system address
1900 @param [in] AddressLength Length of the remote network address structure
1902 @retval EFI_SUCCESS - Socket data successfully buffered
1907 IN ESL_SOCKET
* pSocket
,
1909 IN
size_t BufferLength
,
1910 IN CONST UINT8
* pBuffer
,
1911 OUT
size_t * pDataLength
,
1912 IN
const struct sockaddr
* pAddress
,
1913 IN socklen_t AddressLength
1917 BOOLEAN bUrgentQueue
;
1918 ESL_PACKET
* pPacket
;
1919 ESL_IO_MGMT
** ppActive
;
1920 ESL_IO_MGMT
** ppFree
;
1922 ESL_PACKET
** ppQueueHead
;
1923 ESL_PACKET
** ppQueueTail
;
1924 ESL_PACKET
* pPreviousPacket
;
1926 EFI_TCP4_TRANSMIT_DATA
* pTxData
;
1928 EFI_TPL TplPrevious
;
1935 Status
= EFI_UNSUPPORTED
;
1936 pSocket
->errno
= ENOTCONN
;
1940 // Verify that the socket is connected
1942 if ( SOCKET_STATE_CONNECTED
== pSocket
->State
) {
1946 pPort
= pSocket
->pPortList
;
1947 if ( NULL
!= pPort
) {
1949 // Determine the queue head
1951 bUrgent
= (BOOLEAN
)( 0 != ( Flags
& MSG_OOB
));
1952 bUrgentQueue
= bUrgent
1953 && ( !pSocket
->bOobInLine
)
1954 && pSocket
->pApi
->bOobSupported
;
1955 if ( bUrgentQueue
) {
1956 ppQueueHead
= &pSocket
->pTxOobPacketListHead
;
1957 ppQueueTail
= &pSocket
->pTxOobPacketListTail
;
1958 ppActive
= &pPort
->pTxOobActive
;
1959 ppFree
= &pPort
->pTxOobFree
;
1960 pTxBytes
= &pSocket
->TxOobBytes
;
1963 ppQueueHead
= &pSocket
->pTxPacketListHead
;
1964 ppQueueTail
= &pSocket
->pTxPacketListTail
;
1965 ppActive
= &pPort
->pTxActive
;
1966 ppFree
= &pPort
->pTxFree
;
1967 pTxBytes
= &pSocket
->TxBytes
;
1971 // Verify that there is enough room to buffer another
1972 // transmit operation
1974 if ( pSocket
->MaxTxBuf
> *pTxBytes
) {
1975 if ( pPort
->bTxFlowControl
) {
1977 "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\r\n0x%08x: pPort, TX flow control released, Max bytes: %d > %d bufferred bytes\r\n",
1981 pPort
->bTxFlowControl
= FALSE
;
1985 // Attempt to allocate the packet
1987 Status
= EslSocketPacketAllocate ( &pPacket
,
1988 sizeof ( pPacket
->Op
.Tcp4Tx
)
1989 - sizeof ( pPacket
->Op
.Tcp4Tx
.Buffer
)
1993 if ( !EFI_ERROR ( Status
)) {
1995 // Initialize the transmit operation
1997 pTxData
= &pPacket
->Op
.Tcp4Tx
.TxData
;
1998 pTxData
->Push
= TRUE
|| bUrgent
;
1999 pTxData
->Urgent
= bUrgent
;
2000 pTxData
->DataLength
= (UINT32
) BufferLength
;
2001 pTxData
->FragmentCount
= 1;
2002 pTxData
->FragmentTable
[0].FragmentLength
= (UINT32
) BufferLength
;
2003 pTxData
->FragmentTable
[0].FragmentBuffer
= &pPacket
->Op
.Tcp4Tx
.Buffer
[0];
2006 // Copy the data into the buffer
2008 CopyMem ( &pPacket
->Op
.Tcp4Tx
.Buffer
[0],
2013 // Synchronize with the socket layer
2015 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
2018 // Stop transmission after an error
2020 if ( !EFI_ERROR ( pSocket
->TxError
)) {
2022 // Display the request
2025 "Send %d %s bytes from 0x%08x\r\n",
2027 bUrgent
? L
"urgent" : L
"normal",
2031 // Queue the data for transmission
2033 pPacket
->pNext
= NULL
;
2034 pPreviousPacket
= *ppQueueTail
;
2035 if ( NULL
== pPreviousPacket
) {
2036 *ppQueueHead
= pPacket
;
2039 pPreviousPacket
->pNext
= pPacket
;
2041 *ppQueueTail
= pPacket
;
2043 "0x%08x: Packet on %s transmit list\r\n",
2045 bUrgentQueue
? L
"urgent" : L
"normal" ));
2048 // Account for the buffered data
2050 *pTxBytes
+= BufferLength
;
2051 *pDataLength
= BufferLength
;
2054 // Start the transmit engine if it is idle
2056 if ( NULL
!= *ppFree
) {
2057 EslSocketTxStart ( pPort
,
2066 // Previous transmit error
2067 // Stop transmission
2069 Status
= pSocket
->TxError
;
2070 pSocket
->errno
= EIO
;
2075 EslSocketPacketFree ( pPacket
, DEBUG_TX
);
2079 // Release the socket layer synchronization
2081 RESTORE_TPL ( TplPrevious
);
2085 // Packet allocation failed
2087 pSocket
->errno
= ENOMEM
;
2091 if ( !pPort
->bTxFlowControl
) {
2093 "0x%08x: pPort, TX flow control applied, Max bytes %d <= %d bufferred bytes\r\nTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\r\n",
2097 pPort
->bTxFlowControl
= TRUE
;
2100 // Not enough buffer space available
2102 pSocket
->errno
= EAGAIN
;
2103 Status
= EFI_NOT_READY
;
2109 // Return the operation status
2111 DBG_EXIT_STATUS ( Status
);
2117 Process the normal data transmit completion
2119 This routine use ::EslSocketTxComplete to perform the transmit
2120 completion processing for normal data.
2122 This routine is called by the TCPv4 network layer when a
2123 normal data transmit request completes.
2125 @param [in] Event The normal transmit completion event
2127 @param [in] pIo The ESL_IO_MGMT structure address
2133 IN ESL_IO_MGMT
* pIo
2136 UINT32 LengthInBytes
;
2137 ESL_PACKET
* pPacket
;
2139 ESL_SOCKET
* pSocket
;
2145 // Locate the active transmit packet
2147 pPacket
= pIo
->pPacket
;
2149 pSocket
= pPort
->pSocket
;
2152 // Get the transmit length and status
2154 LengthInBytes
= pPacket
->Op
.Tcp4Tx
.TxData
.DataLength
;
2155 pSocket
->TxBytes
-= LengthInBytes
;
2156 Status
= pIo
->Token
.Tcp4Tx
.CompletionToken
.Status
;
2159 // Complete the transmit operation
2161 EslSocketTxComplete ( pIo
,
2165 &pSocket
->pTxPacketListHead
,
2166 &pSocket
->pTxPacketListTail
,
2174 Process the urgent data transmit completion
2176 This routine use ::EslSocketTxComplete to perform the transmit
2177 completion processing for urgent data.
2179 This routine is called by the TCPv4 network layer when a
2180 urgent data transmit request completes.
2182 @param [in] Event The urgent transmit completion event
2184 @param [in] pIo The ESL_IO_MGMT structure address
2188 EslTcp4TxOobComplete (
2190 IN ESL_IO_MGMT
* pIo
2193 UINT32 LengthInBytes
;
2194 ESL_PACKET
* pPacket
;
2196 ESL_SOCKET
* pSocket
;
2202 // Locate the active transmit packet
2204 pPacket
= pIo
->pPacket
;
2206 pSocket
= pPort
->pSocket
;
2209 // Get the transmit length and status
2211 LengthInBytes
= pPacket
->Op
.Tcp4Tx
.TxData
.DataLength
;
2212 pSocket
->TxOobBytes
-= LengthInBytes
;
2213 Status
= pIo
->Token
.Tcp4Tx
.CompletionToken
.Status
;
2216 // Complete the transmit operation
2218 EslSocketTxComplete ( pIo
,
2222 &pSocket
->pTxOobPacketListHead
,
2223 &pSocket
->pTxOobPacketListTail
,
2224 &pPort
->pTxOobActive
,
2225 &pPort
->pTxOobFree
);
2231 Verify the adapter's IP address
2233 This support routine is called by EslSocketBindTest.
2235 @param [in] pPort Address of an ::ESL_PORT structure.
2236 @param [in] pConfigData Address of the configuration data
2238 @retval EFI_SUCCESS - The IP address is valid
2239 @retval EFI_NOT_STARTED - The IP address is invalid
2243 EslTcp4VerifyLocalIpAddress (
2244 IN ESL_PORT
* pPort
,
2245 IN EFI_TCP4_CONFIG_DATA
* pConfigData
2249 EFI_TCP4_ACCESS_POINT
* pAccess
;
2250 EFI_IP4_CONFIG2_INTERFACE_INFO
* pIfInfo
;
2251 EFI_IP4_CONFIG2_PROTOCOL
* pIpConfig2Protocol
;
2252 ESL_SERVICE
* pService
;
2258 // Use break instead of goto
2263 // Determine if the IP address is specified
2265 pAccess
= &pConfigData
->AccessPoint
;
2266 DEBUG (( DEBUG_BIND
,
2267 "UseDefaultAddress: %s\r\n",
2268 pAccess
->UseDefaultAddress
? L
"TRUE" : L
"FALSE" ));
2269 DEBUG (( DEBUG_BIND
,
2270 "Requested IP address: %d.%d.%d.%d\r\n",
2271 pAccess
->StationAddress
.Addr
[ 0 ],
2272 pAccess
->StationAddress
.Addr
[ 1 ],
2273 pAccess
->StationAddress
.Addr
[ 2 ],
2274 pAccess
->StationAddress
.Addr
[ 3 ]));
2275 if ( pAccess
->UseDefaultAddress
2276 || (( 0 == pAccess
->StationAddress
.Addr
[ 0 ])
2277 && ( 0 == pAccess
->StationAddress
.Addr
[ 1 ])
2278 && ( 0 == pAccess
->StationAddress
.Addr
[ 2 ])
2279 && ( 0 == pAccess
->StationAddress
.Addr
[ 3 ])))
2281 Status
= EFI_SUCCESS
;
2286 // Open the configuration protocol
2288 pService
= pPort
->pService
;
2289 Status
= gBS
->OpenProtocol (
2290 pService
->Controller
,
2291 &gEfiIp4Config2ProtocolGuid
,
2292 (VOID
**)&pIpConfig2Protocol
,
2295 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2297 if ( EFI_ERROR ( Status
)) {
2298 DEBUG (( DEBUG_ERROR
,
2299 "ERROR - IP Configuration Protocol not available, Status: %r\r\n",
2305 // Get the interface information size.
2308 Status
= pIpConfig2Protocol
->GetData (
2310 Ip4Config2DataTypeInterfaceInfo
,
2314 if ( EFI_BUFFER_TOO_SMALL
!= Status
) {
2315 DEBUG (( DEBUG_ERROR
,
2316 "ERROR - Failed to get the interface information size, Status: %r\r\n",
2322 // Allocate the interface information buffer
2324 pIfInfo
= AllocatePool ( DataSize
);
2325 if ( NULL
== pIfInfo
) {
2326 DEBUG (( DEBUG_ERROR
,
2327 "ERROR - Not enough memory to allocate the interface information buffer!\r\n" ));
2328 Status
= EFI_OUT_OF_RESOURCES
;
2333 // Get the interface info.
2335 Status
= pIpConfig2Protocol
->GetData (
2337 Ip4Config2DataTypeInterfaceInfo
,
2341 if ( EFI_ERROR ( Status
)) {
2342 DEBUG (( DEBUG_ERROR
,
2343 "ERROR - Failed to return the interface info, Status: %r\r\n",
2349 // Display the current configuration
2351 DEBUG (( DEBUG_BIND
,
2352 "Actual adapter IP address: %d.%d.%d.%d\r\n",
2353 pIfInfo
->StationAddress
.Addr
[ 0 ],
2354 pIfInfo
->StationAddress
.Addr
[ 1 ],
2355 pIfInfo
->StationAddress
.Addr
[ 2 ],
2356 pIfInfo
->StationAddress
.Addr
[ 3 ]));
2359 // Assume the port is not configured
2361 Status
= EFI_SUCCESS
;
2362 if (( pAccess
->StationAddress
.Addr
[ 0 ] == pIfInfo
->StationAddress
.Addr
[ 0 ])
2363 && ( pAccess
->StationAddress
.Addr
[ 1 ] == pIfInfo
->StationAddress
.Addr
[ 1 ])
2364 && ( pAccess
->StationAddress
.Addr
[ 2 ] == pIfInfo
->StationAddress
.Addr
[ 2 ])
2365 && ( pAccess
->StationAddress
.Addr
[ 3 ] == pIfInfo
->StationAddress
.Addr
[ 3 ])) {
2370 // The IP address did not match
2372 Status
= EFI_NOT_STARTED
;
2377 // Free the buffer if necessary
2379 if ( NULL
!= pIfInfo
) {
2380 FreePool ( pIfInfo
);
2384 // Return the IP address status
2386 DBG_EXIT_STATUS ( Status
);
2392 Interface between the socket layer and the network specific
2393 code that supports SOCK_STREAM and SOCK_SEQPACKET sockets
2396 CONST ESL_PROTOCOL_API cEslTcp4Api
= {
2399 OFFSET_OF ( ESL_PORT
, Context
.Tcp4
.ConfigData
),
2400 OFFSET_OF ( ESL_LAYER
, pTcp4List
),
2401 OFFSET_OF ( struct sockaddr_in
, sin_zero
),
2402 sizeof ( struct sockaddr_in
),
2404 sizeof (((ESL_PACKET
*)0 )->Op
.Tcp4Rx
),
2405 OFFSET_OF ( ESL_PACKET
, Op
.Tcp4Rx
.Buffer
) - OFFSET_OF ( ESL_PACKET
, Op
),
2406 OFFSET_OF ( ESL_IO_MGMT
, Token
.Tcp4Rx
.Packet
.RxData
),
2411 EslTcp4ConnectStart
,
2412 EslTcp4SocketIsConfigured
,
2413 EslTcp4LocalAddressGet
,
2414 EslTcp4LocalAddressSet
,
2419 EslTcp4PortAllocate
,
2424 EslTcp4RemoteAddressGet
,
2425 EslTcp4RemoteAddressSet
,
2430 EslTcp4TxOobComplete
,
2431 (PFN_API_VERIFY_LOCAL_IP_ADDRESS
)EslTcp4VerifyLocalIpAddress