4 Copyright (c) 2005 - 2006, Intel Corporation<BR>
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.php<BR>
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.
20 #include <Protocol/Ip4.h>
21 #include <Protocol/Tcp4.h>
22 #include <Protocol/Udp4.h>
24 #include <Library/NetLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/UefiRuntimeServicesTableLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/UefiDriverEntryPoint.h>
31 #include <Library/UefiLib.h>
32 #include <Library/DpcLib.h>
34 #define SOCK_SND_BUF 0
35 #define SOCK_RCV_BUF 1
37 #define SOCK_BUFF_LOW_WATER (2 * 1024)
38 #define SOCK_RCV_BUFF_SIZE (8 * 1024)
39 #define SOCK_SND_BUFF_SIZE (8 * 1024)
40 #define SOCK_BACKLOG 5
42 #define PROTO_RESERVED_LEN 20
44 #define SO_NO_MORE_DATA 0x0001
49 // When a socket is created it enters into SO_UNCONFIGURED,
50 // no actions can be taken on this socket, only after calling
51 // SockConfigure. The state transition diagram of socket is
54 // SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING
56 // | ---> SO_LISTENING |
58 // |------------------SO_DISCONNECTING<-- SO_CONNECTED
60 // A passive socket can only go into SO_LISTENING and
61 // SO_UNCONFIGURED state. SO_XXXING state is a middle state
62 // when a socket is undergoing a protocol procedure such
63 // as requesting a TCP connection.
80 /// Socket configure state
85 SO_CONFIGURED_PASSIVE
,
87 } SOCK_CONFIGURE_STATE
;
90 Set socket SO_NO_MORE_DATA flag.
92 @param Sock Pointer to the socket
95 #define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
98 Check whether the socket is unconfigured.
100 @param Sock Pointer to the socket
102 @retval True The socket is unconfigued
103 @retval False The socket is not unconfigued
106 #define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
109 Check whether the socket is configured.
111 @param Sock Pointer to the socket
113 @retval True The socket is configued
114 @retval False The socket is not configued
117 #define SOCK_IS_CONFIGURED(Sock) \
118 (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
119 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
122 Check whether the socket is configured to active mode.
124 @param Sock Pointer to the socket
126 @retval True The socket is configued to active mode
127 @retval False The socket is not configued to active mode
130 #define SOCK_IS_CONFIGURED_ACTIVE(Sock) \
131 ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
134 Check whether the socket is configured to passive mode.
136 @param Sock Pointer to the socket
138 @retval True The socket is configued to passive mode
139 @retval False The socket is not configued to passive mode
142 #define SOCK_IS_CONNECTED_PASSIVE(Sock) \
143 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
146 Check whether the socket is mapped.
148 @param Sock Pointer to the socket
150 @retval True The socket is no mapping
151 @retval False The socket is mapped
154 #define SOCK_IS_NO_MAPPING(Sock) \
155 ((Sock)->ConfigureState == SO_NO_MAPPING)
158 Check whether the socket is closed.
160 @param Sock Pointer to the socket
162 @retval True The socket is closed
163 @retval False The socket is not closed
166 #define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
169 Check whether the socket is listening.
171 @param Sock Pointer to the socket
173 @retval True The socket is listening
174 @retval False The socket is not listening
177 #define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
180 Check whether the socket is connecting.
182 @param Sock Pointer to the socket
184 @retval True The socket is connecting
185 @retval False The socket is not connecting
188 #define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
191 Check whether the socket has connected.
193 @param Sock Pointer to the socket
195 @retval True The socket has connected
196 @retval False The socket has not connected
199 #define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
202 Check whether the socket is disconnecting.
204 @param Sock Pointer to the socket
206 @retval True The socket is disconnecting
207 @retval False The socket is not disconnecting
210 #define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
213 Check whether the socket is no more data.
215 @param Sock Pointer to the socket
217 @retval True The socket is no more data
218 @retval False The socket still has data
221 #define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
224 Set the size of the receive buffer.
226 @param Sock Pointer to the socket
227 @param Size The size to set
230 #define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
233 Get the size of the receive buffer.
235 @param Sock Pointer to the socket
237 @return The receive buffer size
240 #define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
243 Get the size of the receive data.
245 @param Sock Pointer to the socket
247 @return The received data size
250 #define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
253 Set the size of the send buffer.
255 @param Sock Pointer to the socket
256 @param Size The size to set
259 #define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
262 Get the size of the send buffer.
264 @param Sock Pointer to the socket
266 @return The send buffer size
269 #define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
272 Get the size of the send data.
274 @param Sock Pointer to the socket
276 @return The send data size
279 #define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
282 Set the backlog value of the socket.
284 @param Sock Pointer to the socket
285 @param Value The value to set
288 #define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
291 Get the backlog value of the socket.
293 @param Sock Pointer to the socket
295 @return The backlog value
298 #define GET_BACKLOG(Sock) ((Sock)->BackLog)
301 Set the socket with error state.
303 @param Sock Pointer to the socket
304 @param Error The error state
307 #define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
309 #define SND_BUF_HDR_LEN(Sock) \
310 ((SockBufFirst (&((Sock)->SndBuffer)))->TotalSize)
312 #define RCV_BUF_HDR_LEN(Sock) \
313 ((SockBufFirst (&((Sock)->RcvBuffer)))->TotalSize)
315 #define SOCK_SIGNATURE SIGNATURE_32 ('S', 'O', 'C', 'K')
317 #define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
319 #define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
321 #define PROTO_TOKEN_FORM_SOCK(SockToken, Type) \
322 ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
324 typedef struct _SOCKET SOCKET
;
327 /// Socket completion token
329 typedef struct _SOCK_COMPLETION_TOKEN
{
330 EFI_EVENT Event
; ///< The event to be issued
331 EFI_STATUS Status
; ///< The status to be issued
332 } SOCK_COMPLETION_TOKEN
;
335 /// The application token with data packet
337 typedef struct _SOCK_IO_TOKEN
{
338 SOCK_COMPLETION_TOKEN Token
;
346 /// The request issued from socket layer to protocol layer.
349 SOCK_ATTACH
, ///< Attach current socket to a new PCB
350 SOCK_DETACH
, ///< Detach current socket from the PCB
351 SOCK_CONFIGURE
, ///< Configure attached PCB
352 SOCK_FLUSH
, ///< Flush attached PCB
353 SOCK_SND
, ///< Need protocol to send something
354 SOCK_SNDPUSH
, ///< Need protocol to send pushed data
355 SOCK_SNDURG
, ///< Need protocol to send urgent data
356 SOCK_CONSUMED
, ///< Application has retrieved data from socket
357 SOCK_CONNECT
, ///< Need to connect to a peer
358 SOCK_CLOSE
, ///< Need to close the protocol process
359 SOCK_ABORT
, ///< Need to reset the protocol process
360 SOCK_POLL
, ///< Need to poll to the protocol layer
361 SOCK_ROUTE
, ///< Need to add a route information
362 SOCK_MODE
, ///< Need to get the mode data of the protocol
363 SOCK_GROUP
///< Need to join a mcast group
370 SOCK_DGRAM
, ///< This socket providing datagram service
371 SOCK_STREAM
///< This socket providing stream service
375 /// The buffer structure of rcvd data and send data used by socket.
377 typedef struct _SOCK_BUFFER
{
378 UINT32 HighWater
; ///< The buffersize upper limit of sock_buffer
379 UINT32 LowWater
; ///< The low warter mark of sock_buffer
380 NET_BUF_QUEUE
*DataQueue
; ///< The queue to buffer data
384 The handler of protocol for request from socket.
386 @param Socket The socket issuing the request to protocol
387 @param Request The request issued by socket
388 @param RequestData The request related data
390 @retval EFI_SUCCESS The socket request is completed successfully.
391 @retval other The error status returned by the corresponding TCP
397 (*SOCK_PROTO_HANDLER
) (
399 IN SOCK_REQUEST Request
,
405 // Socket provided oprerations for low layer protocol
409 // Socket provided operations for user interface
413 Set the state of the socket.
415 @param Sock Pointer to the socket.
416 @param State The new state to be set.
426 Called by the low layer protocol to indicate the socket a connection is
429 This function just changes the socket's state to SO_CONNECTED
430 and signals the token used for connection establishment.
432 @param Sock Pointer to the socket associated with the
433 established connection.
437 SockConnEstablished (
442 Called by the low layer protocol to indicate the connection is closed.
444 This function flushes the socket, sets the state to SO_CLOSED and signals
447 @param Sock Pointer to the socket associated with the closed
457 Called by low layer protocol to indicate that some data is sent or processed.
459 This function trims the sent data in the socket send buffer, signals the data
462 @param Sock Pointer to the socket.
463 @param Count The length of the data processed or sent, in bytes.
473 Called by the low layer protocol to copy some data in socket send
474 buffer starting from the specific offset to a buffer provided by
477 @param Sock Pointer to the socket.
478 @param Offset The start point of the data to be copied.
479 @param Len The length of the data to be copied.
480 @param Dest Pointer to the destination to copy the data.
482 @return The data size copied.
494 Called by the low layer protocol to indicate that there
495 will be no more data from the communication peer.
497 This function set the socket's state to SO_NO_MORE_DATA and
498 signal all queued IO tokens with the error status EFI_CONNECTION_FIN.
500 @param Sock Pointer to the socket.
509 Called by the low layer protocol to deliver received data to socket layer.
511 This function will append the data to the socket receive buffer, set ther
512 urgent data length and then check if any receive token can be signaled.
514 @param Sock Pointer to the socket.
515 @param NetBuffer Pointer to the buffer that contains the received
517 @param UrgLen The length of the urgent data in the received data.
523 IN OUT NET_BUF
*NetBuffer
,
528 Get the length of the free space of the specific socket buffer.
530 @param Sock Pointer to the socket.
531 @param Which Flag to indicate which socket buffer to check,
532 either send buffer or receive buffer.
534 @return The length of the free space, in bytes.
544 Clone a new socket including its associated protocol control block.
546 @param Sock Pointer to the socket to be cloned.
548 @return Pointer to the newly cloned socket. If NULL, error condition occurred.
557 Signal the receive token with the specific error or
558 set socket error code after error is received.
560 @param Sock Pointer to the socket.
561 @param Error The error code received.
571 /// Proto type of the create callback
575 (*SOCK_CREATE_CALLBACK
) (
581 /// Proto type of the destroy callback
585 (*SOCK_DESTROY_CALLBACK
) (
591 /// The initialize data for create a new socket.
593 typedef struct _SOCK_INIT_DATA
{
597 SOCKET
*Parent
; ///< The parent of this socket
598 UINT32 BackLog
; ///< The connection limit for listening socket
599 UINT32 SndBufferSize
; ///< The high warter mark of send buffer
600 UINT32 RcvBufferSize
; ///< The high warter mark of receive buffer
601 VOID
*Protocol
; ///< The pointer to protocol function template
602 ///< wanted to install on socket
605 // Callbacks after socket is created and before socket is to be destroyed.
607 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
608 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
609 VOID
*Context
; ///< The context of the callback
612 // Opaque protocol data.
617 SOCK_PROTO_HANDLER ProtoHandler
; ///< The handler of protocol for socket request
619 EFI_HANDLE DriverBinding
; ///< The driver binding handle
623 /// The union type of TCP and UDP protocol.
625 typedef union _NET_PROTOCOL
{
626 EFI_TCP4_PROTOCOL TcpProtocol
; ///< Tcp protocol
627 EFI_UDP4_PROTOCOL UdpProtocol
; ///< Udp protocol
631 /// The socket structure representing a network service access point
636 // Socket description information
638 UINT32 Signature
; ///< Signature of the socket
639 EFI_HANDLE SockHandle
; ///< The virtual handle of the socket
640 EFI_HANDLE DriverBinding
; ///< Socket's driver binding protocol
641 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
642 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
644 SOCK_CONFIGURE_STATE ConfigureState
;
648 EFI_LOCK Lock
; ///< The lock of socket
649 SOCK_BUFFER SndBuffer
; ///< Send buffer of application's data
650 SOCK_BUFFER RcvBuffer
; ///< Receive buffer of received data
651 EFI_STATUS SockError
; ///< The error returned by low layer protocol
655 // Fields used to manage the connection request
657 UINT32 BackLog
; ///< the limit of connection to this socket
658 UINT32 ConnCnt
; ///< the current count of connections to it
659 SOCKET
*Parent
; ///< listening parent that accept the connection
660 LIST_ENTRY ConnectionList
; ///< the connections maintained by this socket
663 // The queue to buffer application's asynchronous token
665 LIST_ENTRY ListenTokenList
;
666 LIST_ENTRY RcvTokenList
;
667 LIST_ENTRY SndTokenList
;
668 LIST_ENTRY ProcessingSndTokenList
;
670 SOCK_COMPLETION_TOKEN
*ConnectionToken
; ///< app's token to signal if connected
671 SOCK_COMPLETION_TOKEN
*CloseToken
; ///< app's token to signal if closed
674 // Interface for low level protocol
676 SOCK_PROTO_HANDLER ProtoHandler
; ///< The request handler of protocol
677 UINT8 ProtoReserved
[PROTO_RESERVED_LEN
]; ///< Data fields reserved for protocol
678 NET_PROTOCOL NetProtocol
; ///< TCP or UDP protocol socket used
681 // Callbacks after socket is created and before socket is to be destroyed.
683 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
684 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
685 VOID
*Context
; ///< The context of the callback
689 /// The token structure buffered in socket layer.
691 typedef struct _SOCK_TOKEN
{
692 LIST_ENTRY TokenList
; ///< The entry to add in the token list
693 SOCK_COMPLETION_TOKEN
*Token
; ///< The application's token
694 UINT32 RemainDataLen
; ///< Unprocessed data length
695 SOCKET
*Sock
; ///< The poninter to the socket this token
700 /// Reserved data to access the NET_BUF delivered by UDP driver.
702 typedef struct _UDP_RSV_DATA
{
704 EFI_UDP4_SESSION_DATA Session
;
708 /// Reserved data to access the NET_BUF delivered by TCP driver.
710 typedef struct _TCP_RSV_DATA
{
715 Create a socket and its associated protocol control block
716 with the intial data SockInitData and protocol specific
719 @param SockInitData Inital data to setting the socket.
721 @return Pointer to the newly created socket. If NULL, error condition occured.
726 IN SOCK_INIT_DATA
*SockInitData
730 Destory the socket Sock and its associated protocol control block.
732 @param Sock The socket to be destroyed.
734 @retval EFI_SUCCESS The socket Sock is destroyed successfully.
735 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
744 Configure the specific socket Sock using configuration data ConfigData.
746 @param Sock Pointer to the socket to be configured.
747 @param ConfigData Pointer to the configuration data.
749 @retval EFI_SUCCESS The socket is configured successfully.
750 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket or the
751 socket is already configured.
761 Initiate a connection establishment process.
763 @param Sock Pointer to the socket to initiate the initate the
765 @param Token Pointer to the token used for the connection
768 @retval EFI_SUCCESS The connection is initialized successfully.
769 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
770 socket is closed, or the socket is not configured to
771 be an active one, or the token is already in one of
773 @retval EFI_NO_MAPPING The IP address configuration operation is not
775 @retval EFI_NOT_STARTED The socket is not configured.
785 Issue a listen token to get an existed connected network instance
786 or wait for a connection if there is none.
788 @param Sock Pointer to the socket to accept connections.
789 @param Token The token to accept a connection.
791 @retval EFI_SUCCESS Either a connection is accpeted or the Token is
792 buffered for further acception.
793 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
794 socket is closed, or the socket is not configured to
795 be a passive one, or the token is already in one of
797 @retval EFI_NO_MAPPING The IP address configuration operation is not
799 @retval EFI_NOT_STARTED The socket is not configured.
800 @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit.
810 Issue a token with data to the socket to send out.
812 @param Sock Pointer to the socket to process the token with
814 @param Token The token with data that needs to send out.
816 @retval EFI_SUCCESS The token is processed successfully.
817 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
818 socket is closed, or the socket is not in a
819 synchronized state , or the token is already in one
820 of this socket's lists.
821 @retval EFI_NO_MAPPING The IP address configuration operation is not
823 @retval EFI_NOT_STARTED The socket is not configured.
824 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
834 Issue a token to get data from the socket.
836 @param Sock Pointer to the socket to get data from.
837 @param Token The token to store the received data from the
840 @retval EFI_SUCCESS The token is processed successfully.
841 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
842 socket is closed, or the socket is not in a
843 synchronized state , or the token is already in one
844 of this socket's lists.
845 @retval EFI_NO_MAPPING The IP address configuration operation is not
847 @retval EFI_NOT_STARTED The socket is not configured.
848 @retval EFI_CONNECTION_FIN The connection is closed and there is no more data.
849 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
859 Reset the socket and its associated protocol control block.
861 @param Sock Pointer to the socket to be flushed.
863 @retval EFI_SUCCESS The socket is flushed successfully.
864 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
873 Close or abort the socket associated connection.
875 @param Sock Pointer to the socket of the connection to close or
877 @param Token The token for close operation.
878 @param OnAbort TRUE for aborting the connection, FALSE to close it.
880 @retval EFI_SUCCESS The close or abort operation is initialized
882 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
883 socket is closed, or the socket is not in a
884 synchronized state , or the token is already in one
885 of this socket's lists.
886 @retval EFI_NO_MAPPING The IP address configuration operation is not
888 @retval EFI_NOT_STARTED The socket is not configured.
899 Get the mode data of the low layer protocol.
901 @param Sock Pointer to the socket to get mode data from.
902 @param Mode Pointer to the data to store the low layer mode
905 @retval EFI_SUCCESS The mode data is got successfully.
906 @retval EFI_NOT_STARTED The socket is not configured.
916 Configure the low level protocol to join a multicast group for
917 this socket's connection.
919 @param Sock Pointer to the socket of the connection to join the
920 specific multicast group.
921 @param GroupInfo Pointer to the multicast group info.
923 @retval EFI_SUCCESS The configuration is done successfully.
924 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
925 @retval EFI_NOT_STARTED The socket is not configured.
935 Add or remove route information in IP route table associated
938 @param Sock Pointer to the socket associated with the IP route
940 @param RouteInfo Pointer to the route information to be processed.
942 @retval EFI_SUCCESS The route table is updated successfully.
943 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
944 @retval EFI_NO_MAPPING The IP address configuration operation is not
946 @retval EFI_NOT_STARTED The socket is not configured.
956 // Supporting function to operate on socket buffer
960 Get the first buffer block in the specific socket buffer.
962 @param Sockbuf Pointer to the socket buffer.
964 @return Pointer to the first buffer in the queue. NULL if the queue is empty.
969 IN SOCK_BUFFER
*Sockbuf
973 Get the next buffer block in the specific socket buffer.
975 @param Sockbuf Pointer to the socket buffer.
976 @param SockEntry Pointer to the buffer block prior to the required
979 @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
980 the tail or head entry.
985 IN SOCK_BUFFER
*Sockbuf
,
986 IN NET_BUF
*SockEntry