2 Definitions for the EFI Socket layer library.
4 Copyright (c) 2011, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license
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.
15 #ifndef _EFI_SOCKET_LIB_H_
16 #define _EFI_SOCKET_LIB_H_
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiLib.h>
26 #include <Protocol/EfiSocket.h>
27 #include <Protocol/ServiceBinding.h>
28 #include <Protocol/Tcp4.h>
29 #include <Protocol/Tcp6.h>
30 #include <Protocol/Udp4.h>
31 #include <Protocol/Udp6.h>
35 //------------------------------------------------------------------------------
37 //------------------------------------------------------------------------------
39 #define DEBUG_TPL 0x40000000 ///< Display TPL change messages
41 #define TPL_SOCKETS TPL_CALLBACK ///< TPL for routine synchronization
43 //------------------------------------------------------------------------------
45 //------------------------------------------------------------------------------
47 #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */
48 #define DBG_ENTER() DEBUG (( DEBUG_INFO, "Entering " __FUNCTION__ "\n" )) ///< Display routine entry
49 #define DBG_EXIT() DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ "\n" )) ///< Display routine exit
50 #define DBG_EXIT_DEC(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %d\n", Status )) ///< Display routine exit with decimal value
51 #define DBG_EXIT_HEX(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: 0x%08x\n", Status )) ///< Display routine exit with hex value
52 #define DBG_EXIT_STATUS(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", Status: %r\n", Status )) ///< Display routine exit with status value
53 #define DBG_EXIT_TF(Status) DEBUG (( DEBUG_INFO, "Exiting " __FUNCTION__ ", returning %s\n", (FALSE == Status) ? L"FALSE" : L"TRUE" )) ///< Display routine with TRUE/FALSE value
55 #define DBG_ENTER() ///< Display routine entry
56 #define DBG_EXIT() ///< Display routine exit
57 #define DBG_EXIT_DEC(Status) ///< Display routine exit with decimal value
58 #define DBG_EXIT_HEX(Status) ///< Display routine exit with hex value
59 #define DBG_EXIT_STATUS(Status) ///< Display routine exit with status value
60 #define DBG_EXIT_TF(Status) ///< Display routine with TRUE/FALSE value
63 #define DIM(x) ( sizeof ( x ) / sizeof ( x[0] )) ///< Compute the number of entries in an array
68 This macro which is enabled when debug is enabled verifies that
69 the new TPL value is >= the current TPL value.
75 #if !defined(MDEPKG_NDEBUG)
78 Verify that the TPL is at the correct level
80 #define VERIFY_AT_TPL(tpl) \
82 EFI_TPL PreviousTpl; \
84 PreviousTpl = EfiGetCurrentTpl ( ); \
85 if ( PreviousTpl != tpl ) { \
86 DEBUG (( DEBUG_ERROR | DEBUG_TPL, \
87 "Current TPL: %d, New TPL: %d\r\n", \
88 PreviousTpl, tpl )); \
89 ASSERT ( PreviousTpl == tpl ); \
93 #define VERIFY_TPL(tpl) \
95 EFI_TPL PreviousTpl; \
97 PreviousTpl = EfiGetCurrentTpl ( ); \
98 if ( PreviousTpl > tpl ) { \
99 DEBUG (( DEBUG_ERROR | DEBUG_TPL, \
100 "Current TPL: %d, New TPL: %d\r\n", \
101 PreviousTpl, tpl )); \
102 ASSERT ( PreviousTpl <= tpl ); \
106 #else // MDEPKG_NDEBUG
108 #define VERIFY_AT_TPL(tpl) ///< Verify that the TPL is at the correct level
109 #define VERIFY_TPL(tpl) ///< Verify that the TPL is at the correct level
111 #endif // MDEPKG_NDEBUG
114 Raise TPL to the specified level
116 #define RAISE_TPL(PreviousTpl, tpl) \
117 VERIFY_TPL ( tpl ); \
118 PreviousTpl = gBS->RaiseTPL ( tpl ); \
119 DEBUG (( DEBUG_TPL | DEBUG_TPL, \
124 Restore the TPL to the previous value
126 #define RESTORE_TPL(tpl) \
127 DEBUG (( DEBUG_TPL | DEBUG_TPL, \
130 gBS->RestoreTPL ( tpl )
132 //------------------------------------------------------------------------------
134 //------------------------------------------------------------------------------
136 typedef struct _ESL_SERVICE ESL_SERVICE
; ///< Forward delcaration
139 Protocol binding and installation control structure
141 The driver uses this structure to simplify the driver binding processing.
144 CHAR16
* pName
; ///< Protocol name
145 EFI_GUID
* pNetworkBinding
; ///< Network service binding protocol for socket support
146 EFI_GUID
* pNetworkProtocolGuid
;///< Network protocol GUID
147 CONST EFI_GUID
* pTagGuid
; ///< Tag to mark protocol in use
148 UINTN ServiceListOffset
; ///< Offset in ::ESL_LAYER for the list of services
149 UINTN RxIo
; ///< Number of receive ESL_IO_MGMT structures for data
150 UINTN TxIoNormal
; ///< Number of transmit ESL_IO_MGMT structures for normal data
151 UINTN TxIoUrgent
; ///< Number of transmit ESL_IO_MGMT structures for urgent data
152 } ESL_SOCKET_BINDING
;
154 //------------------------------------------------------------------------------
156 //------------------------------------------------------------------------------
158 extern CONST EFI_GUID mEslIp4ServiceGuid
; ///< Tag GUID for the IPv4 layer
159 extern CONST EFI_GUID mEslIp6ServiceGuid
; ///< Tag GUID for the IPv6 layer
160 extern CONST EFI_GUID mEslTcp4ServiceGuid
; ///< Tag GUID for the TCPv4 layer
161 extern CONST EFI_GUID mEslTcp6ServiceGuid
; ///< Tag GUID for the TCPv6 layer
162 extern CONST EFI_GUID mEslUdp4ServiceGuid
; ///< Tag GUID for the UDPv4 layer
163 extern CONST EFI_GUID mEslUdp6ServiceGuid
; ///< Tag GUID for the UDPv6 layer
165 //------------------------------------------------------------------------------
167 //------------------------------------------------------------------------------
169 extern CONST ESL_SOCKET_BINDING cEslSocketBinding
[];///< List of network service bindings
170 extern CONST UINTN cEslSocketBindingEntries
; ///< Number of network service bindings
172 //------------------------------------------------------------------------------
173 // DXE Support Routines
174 //------------------------------------------------------------------------------
177 Creates a child handle and installs a protocol.
179 When the socket application is linked against UseSocketDxe, the ::socket
180 routine indirectly calls this routine in SocketDxe to create a child
181 handle if necessary and install the socket protocol on the handle.
182 Upon return, EslServiceGetProtocol in UseSocketLib returns the
183 ::EFI_SOCKET_PROTOCOL address to the socket routine.
185 @param [in] pThis Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
186 @param [in] pChildHandle Pointer to the handle of the child to create. If it is NULL,
187 then a new handle is created. If it is a pointer to an existing UEFI handle,
188 then the protocol is added to the existing UEFI handle.
190 @retval EFI_SUCCESS The protocol was added to ChildHandle.
191 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
192 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
194 @retval other The child handle was not created
200 IN EFI_SERVICE_BINDING_PROTOCOL
* pThis
,
201 IN OUT EFI_HANDLE
* pChildHandle
205 Destroys a child handle with a protocol installed on it.
207 When the socket application is linked against UseSocketDxe, the ::close
208 routine indirectly calls this routine in SocketDxe to undo the operations
209 done by the ::EslDxeCreateChild routine. This routine removes the socket
210 protocol from the handle and then destroys the child handle if there are
211 no other protocols attached.
213 @param [in] pThis Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
214 @param [in] ChildHandle Handle of the child to destroy
216 @retval EFI_SUCCESS The protocol was removed from ChildHandle.
217 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
218 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
219 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
220 because its services are being used.
221 @retval other The child handle was not destroyed
227 IN EFI_SERVICE_BINDING_PROTOCOL
* pThis
,
228 IN EFI_HANDLE ChildHandle
232 Install the socket service
234 SocketDxe uses this routine to announce the socket interface to
237 @param [in] pImageHandle Address of the image handle
239 @retval EFI_SUCCESS Service installed successfully
244 IN EFI_HANDLE
* pImageHandle
248 Uninstall the socket service
250 SocketDxe uses this routine to notify EFI that the socket layer
251 is no longer available.
253 @param [in] ImageHandle Handle for the image.
255 @retval EFI_SUCCESS Service installed successfully
260 IN EFI_HANDLE ImageHandle
263 //------------------------------------------------------------------------------
264 // Service Support Routines
265 //------------------------------------------------------------------------------
268 Connect to the network service bindings
270 Walk the network service protocols on the controller handle and
271 locate any that are not in use. Create ::ESL_SERVICE structures to
272 manage the network layer interfaces for the socket driver. Tag
273 each of the network interfaces that are being used. Finally, this
274 routine calls ESL_SOCKET_BINDING::pfnInitialize to prepare the network
275 interface for use by the socket layer.
277 @param [in] BindingHandle Handle for protocol binding.
278 @param [in] Controller Handle of device to work with.
280 @retval EFI_SUCCESS This driver is added to Controller.
281 @retval other This driver does not support this device.
287 IN EFI_HANDLE BindingHandle
,
288 IN EFI_HANDLE Controller
292 Shutdown the connections to the network layer by locating the
293 tags on the network interfaces established by ::EslServiceConnect.
294 This routine calls ESL_SOCKET_BINDING::pfnShutdown to shutdown the any
295 activity on the network interface and then free the ::ESL_SERVICE
298 @param [in] BindingHandle Handle for protocol binding.
299 @param [in] Controller Handle of device to stop driver on.
301 @retval EFI_SUCCESS This driver is removed Controller.
302 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
303 @retval other This driver was not removed from this device.
308 EslServiceDisconnect (
309 IN EFI_HANDLE BindingHandle
,
310 IN EFI_HANDLE Controller
314 Initialize the service layer
316 @param [in] ImageHandle Handle for the image.
322 IN EFI_HANDLE ImageHandle
326 Shutdown the service layer
335 //------------------------------------------------------------------------------
336 // Socket Protocol Routines
337 //------------------------------------------------------------------------------
340 Bind a name to a socket.
342 This routine calls the network specific layer to save the network
343 address of the local connection point.
345 The ::bind routine calls this routine to connect a name
346 (network address and port) to a socket on the local machine.
348 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
350 @param [in] pSockAddr Address of a sockaddr structure that contains the
351 connection point on the local machine. An IPv4 address
352 of INADDR_ANY specifies that the connection is made to
353 all of the network stacks on the platform. Specifying a
354 specific IPv4 address restricts the connection to the
355 network stack supporting that address. Specifying zero
356 for the port causes the network layer to assign a port
357 number from the dynamic range. Specifying a specific
358 port number causes the network layer to use that port.
360 @param [in] SockAddrLength Specifies the length in bytes of the sockaddr structure.
362 @param [out] pErrno Address to receive the errno value upon completion.
364 @retval EFI_SUCCESS - Socket successfully created
369 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
370 IN CONST
struct sockaddr
* pSockAddr
,
371 IN socklen_t SockAddrLength
,
376 Determine if the socket is closed
378 This routine checks the state of the socket to determine if
379 the network specific layer has completed the close operation.
381 The ::close routine polls this routine to determine when the
382 close operation is complete. The close operation needs to
383 reverse the operations of the ::EslSocketAllocate routine.
385 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
386 @param [out] pErrno Address to receive the errno value upon completion.
388 @retval EFI_SUCCESS Socket successfully closed
389 @retval EFI_NOT_READY Close still in progress
390 @retval EFI_ALREADY Close operation already in progress
391 @retval Other Failed to close the socket
396 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
401 Start the close operation on the socket
403 This routine calls the network specific layer to initiate the
404 close state machine. This routine then calls the network
405 specific layer to determine if the close state machine has gone
406 to completion. The result from this poll is returned to the
409 The ::close routine calls this routine to start the close
410 operation which reverses the operations of the
411 ::EslSocketAllocate routine. The close routine then polls
412 the ::EslSocketClosePoll routine to determine when the
415 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
416 @param [in] bCloseNow Boolean to control close behavior
417 @param [out] pErrno Address to receive the errno value upon completion.
419 @retval EFI_SUCCESS Socket successfully closed
420 @retval EFI_NOT_READY Close still in progress
421 @retval EFI_ALREADY Close operation already in progress
422 @retval Other Failed to close the socket
426 EslSocketCloseStart (
427 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
428 IN BOOLEAN bCloseNow
,
433 Connect to a remote system via the network.
435 This routine calls the network specific layer to establish
436 the remote system address and establish the connection to
439 The ::connect routine calls this routine to establish a
440 connection with the specified remote system. This routine
441 is designed to be polled by the connect routine for completion
442 of the network connection.
444 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
446 @param [in] pSockAddr Network address of the remote system.
448 @param [in] SockAddrLength Length in bytes of the network address.
450 @param [out] pErrno Address to receive the errno value upon completion.
452 @retval EFI_SUCCESS The connection was successfully established.
453 @retval EFI_NOT_READY The connection is in progress, call this routine again.
454 @retval Others The connection attempt failed.
459 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
460 IN
const struct sockaddr
* pSockAddr
,
461 IN socklen_t SockAddrLength
,
466 Get the local address.
468 This routine calls the network specific layer to get the network
469 address of the local host connection point.
471 The ::getsockname routine calls this routine to obtain the network
472 address associated with the local host connection point.
474 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
476 @param [out] pAddress Network address to receive the local system address
478 @param [in,out] pAddressLength Length of the local network address structure
480 @param [out] pErrno Address to receive the errno value upon completion.
482 @retval EFI_SUCCESS - Local address successfully returned
486 EslSocketGetLocalAddress (
487 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
488 OUT
struct sockaddr
* pAddress
,
489 IN OUT socklen_t
* pAddressLength
,
494 Get the peer address.
496 This routine calls the network specific layer to get the remote
497 system connection point.
499 The ::getpeername routine calls this routine to obtain the network
500 address of the remote connection point.
502 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
504 @param [out] pAddress Network address to receive the remote system address
506 @param [in,out] pAddressLength Length of the remote network address structure
508 @param [out] pErrno Address to receive the errno value upon completion.
510 @retval EFI_SUCCESS - Remote address successfully returned
514 EslSocketGetPeerAddress (
515 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
516 OUT
struct sockaddr
* pAddress
,
517 IN OUT socklen_t
* pAddressLength
,
522 Establish the known port to listen for network connections.
524 This routine calls into the network protocol layer to establish
525 a handler that is called upon connection completion. The handler
526 is responsible for inserting the connection into the FIFO.
528 The ::listen routine indirectly calls this routine to place the
529 socket into a state that enables connection attempts. Connections
530 are placed in a FIFO that is serviced by the application. The
531 application calls the ::accept (::EslSocketAccept) routine to
532 remove the next connection from the FIFO and get the associated
535 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
537 @param [in] Backlog Backlog specifies the maximum FIFO depth for
538 the connections waiting for the application
539 to call accept. Connection attempts received
540 while the queue is full are refused.
542 @param [out] pErrno Address to receive the errno value upon completion.
544 @retval EFI_SUCCESS - Socket successfully created
545 @retval Other - Failed to enable the socket for listen
550 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
556 Get the socket options
558 This routine handles the socket level options and passes the
559 others to the network specific layer.
561 The ::getsockopt routine calls this routine to retrieve the
562 socket options one at a time by name.
564 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
565 @param [in] level Option protocol level
566 @param [in] OptionName Name of the option
567 @param [out] pOptionValue Buffer to receive the option value
568 @param [in,out] pOptionLength Length of the buffer in bytes,
569 upon return length of the option value in bytes
570 @param [out] pErrno Address to receive the errno value upon completion.
572 @retval EFI_SUCCESS - Socket data successfully received
577 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
580 OUT
void * __restrict option_value
,
581 IN OUT socklen_t
* __restrict option_len
,
586 Set the socket options
588 This routine handles the socket level options and passes the
589 others to the network specific layer.
591 The ::setsockopt routine calls this routine to adjust the socket
592 options one at a time by name.
594 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
595 @param [in] level Option protocol level
596 @param [in] OptionName Name of the option
597 @param [in] pOptionValue Buffer containing the option value
598 @param [in] OptionLength Length of the buffer in bytes
599 @param [out] pErrno Address to receive the errno value upon completion.
601 @retval EFI_SUCCESS - Option successfully set
606 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
609 IN CONST
void * option_value
,
610 IN socklen_t option_len
,
615 Poll a socket for pending activity.
617 This routine builds a detected event mask which is returned to
618 the caller in the buffer provided.
620 The ::poll routine calls this routine to determine if the socket
621 needs to be serviced as a result of connection, error, receive or
624 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
626 @param [in] Events Events of interest for this socket
628 @param [in] pEvents Address to receive the detected events
630 @param [out] pErrno Address to receive the errno value upon completion.
632 @retval EFI_SUCCESS - Socket successfully polled
633 @retval EFI_INVALID_PARAMETER - When pEvents is NULL
638 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
645 Receive data from a network connection.
647 This routine calls the network specific routine to remove the
648 next portion of data from the receive queue and return it to the
651 The ::recvfrom routine calls this routine to determine if any data
652 is received from the remote system. Note that the other routines
653 ::recv and ::read are layered on top of ::recvfrom.
655 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
657 @param [in] Flags Message control flags
659 @param [in] BufferLength Length of the the buffer
661 @param [in] pBuffer Address of a buffer to receive the data.
663 @param [in] pDataLength Number of received data bytes in the buffer.
665 @param [out] pAddress Network address to receive the remote system address
667 @param [in,out] pAddressLength Length of the remote network address structure
669 @param [out] pErrno Address to receive the errno value upon completion.
671 @retval EFI_SUCCESS - Socket data successfully received
676 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
678 IN
size_t BufferLength
,
680 OUT
size_t * pDataLength
,
681 OUT
struct sockaddr
* pAddress
,
682 IN OUT socklen_t
* pAddressLength
,
687 Shutdown the socket receive and transmit operations
689 This routine sets a flag to stop future transmissions and calls
690 the network specific layer to cancel the pending receive operation.
692 The ::shutdown routine calls this routine to stop receive and transmit
693 operations on the socket.
695 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
697 @param [in] How Which operations to stop
699 @param [out] pErrno Address to receive the errno value upon completion.
701 @retval EFI_SUCCESS - Socket operations successfully shutdown
706 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
712 Send data using a network connection.
714 This routine calls the network specific layer to queue the data
715 for transmission. Eventually the buffer will reach the head of
716 the queue and will get transmitted over the network. For datagram
717 sockets there is no guarantee that the data reaches the application
718 running on the remote system.
720 The ::sendto routine calls this routine to send data to the remote
721 system. Note that ::send and ::write are layered on top of ::sendto.
723 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
725 @param [in] Flags Message control flags
727 @param [in] BufferLength Length of the the buffer
729 @param [in] pBuffer Address of a buffer containing the data to send
731 @param [in] pDataLength Address to receive the number of data bytes sent
733 @param [in] pAddress Network address of the remote system address
735 @param [in] AddressLength Length of the remote network address structure
737 @param [out] pErrno Address to receive the errno value upon completion.
739 @retval EFI_SUCCESS - Socket data successfully queued for transmit
744 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
746 IN
size_t BufferLength
,
747 IN CONST UINT8
* pBuffer
,
748 OUT
size_t * pDataLength
,
749 IN
const struct sockaddr
* pAddress
,
750 IN socklen_t AddressLength
,
754 //------------------------------------------------------------------------------
756 #endif // _EFI_SOCKET_LIB_H_