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>
33 #define SOCK_SND_BUF 0
34 #define SOCK_RCV_BUF 1
36 #define SOCK_BUFF_LOW_WATER (2 * 1024)
37 #define SOCK_RCV_BUFF_SIZE (8 * 1024)
38 #define SOCK_SND_BUFF_SIZE (8 * 1024)
39 #define SOCK_BACKLOG 5
41 #define PROTO_RESERVED_LEN 20
43 #define SO_NO_MORE_DATA 0x0001
48 // When a socket is created it enters into SO_UNCONFIGURED,
49 // no actions can be taken on this socket, only after calling
50 // SockConfigure. The state transition diagram of socket is
53 // SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING
55 // | ---> SO_LISTENING |
57 // |------------------SO_DISCONNECTING<-- SO_CONNECTED
59 // A passive socket can only go into SO_LISTENING and
60 // SO_UNCONFIGURED state. SO_XXXING state is a middle state
61 // when a socket is undergoing a protocol procedure such
62 // as requesting a TCP connection.
79 /// Socket configure state
84 SO_CONFIGURED_PASSIVE
,
86 } SOCK_CONFIGURE_STATE
;
89 Set socket SO_NO_MORE_DATA flag.
91 @param Sock Pointer to the socket
94 #define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
97 Check whether the socket is unconfigured.
99 @param Sock Pointer to the socket
101 @retval True The socket is unconfigued
102 @retval False The socket is not unconfigued
105 #define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
108 Check whether the socket is configured.
110 @param Sock Pointer to the socket
112 @retval True The socket is configued
113 @retval False The socket is not configued
116 #define SOCK_IS_CONFIGURED(Sock) \
117 (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
118 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
121 Check whether the socket is configured to active mode.
123 @param Sock Pointer to the socket
125 @retval True The socket is configued to active mode
126 @retval False The socket is not configued to active mode
129 #define SOCK_IS_CONFIGURED_ACTIVE(Sock) \
130 ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
133 Check whether the socket is configured to passive mode.
135 @param Sock Pointer to the socket
137 @retval True The socket is configued to passive mode
138 @retval False The socket is not configued to passive mode
141 #define SOCK_IS_CONNECTED_PASSIVE(Sock) \
142 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
145 Check whether the socket is mapped.
147 @param Sock Pointer to the socket
149 @retval True The socket is no mapping
150 @retval False The socket is mapped
153 #define SOCK_IS_NO_MAPPING(Sock) \
154 ((Sock)->ConfigureState == SO_NO_MAPPING)
157 Check whether the socket is closed.
159 @param Sock Pointer to the socket
161 @retval True The socket is closed
162 @retval False The socket is not closed
165 #define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
168 Check whether the socket is listening.
170 @param Sock Pointer to the socket
172 @retval True The socket is listening
173 @retval False The socket is not listening
176 #define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
179 Check whether the socket is connecting.
181 @param Sock Pointer to the socket
183 @retval True The socket is connecting
184 @retval False The socket is not connecting
187 #define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
190 Check whether the socket has connected.
192 @param Sock Pointer to the socket
194 @retval True The socket has connected
195 @retval False The socket has not connected
198 #define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
201 Check whether the socket is disconnecting.
203 @param Sock Pointer to the socket
205 @retval True The socket is disconnecting
206 @retval False The socket is not disconnecting
209 #define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
212 Check whether the socket is no more data.
214 @param Sock Pointer to the socket
216 @retval True The socket is no more data
217 @retval False The socket still has data
220 #define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
223 Set the size of the receive buffer.
225 @param Sock Pointer to the socket
226 @param Size The size to set
229 #define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
232 Get the size of the receive buffer.
234 @param Sock Pointer to the socket
236 @return The receive buffer size
239 #define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
242 Get the size of the receive data.
244 @param Sock Pointer to the socket
246 @return The received data size
249 #define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
252 Set the size of the send buffer.
254 @param Sock Pointer to the socket
255 @param Size The size to set
258 #define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
261 Get the size of the send buffer.
263 @param Sock Pointer to the socket
265 @return The send buffer size
268 #define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
271 Get the size of the send data.
273 @param Sock Pointer to the socket
275 @return The send data size
278 #define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
281 Set the backlog value of the socket.
283 @param Sock Pointer to the socket
284 @param Value The value to set
287 #define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
290 Get the backlog value of the socket.
292 @param Sock Pointer to the socket
294 @return The backlog value
297 #define GET_BACKLOG(Sock) ((Sock)->BackLog)
300 Set the socket with error state.
302 @param Sock Pointer to the socket
303 @param Error The error state
306 #define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
308 #define SND_BUF_HDR_LEN(Sock) \
309 ((SockBufFirst (&((Sock)->SndBuffer)))->TotalSize)
311 #define RCV_BUF_HDR_LEN(Sock) \
312 ((SockBufFirst (&((Sock)->RcvBuffer)))->TotalSize)
314 #define SOCK_SIGNATURE SIGNATURE_32 ('S', 'O', 'C', 'K')
316 #define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
318 #define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
320 #define PROTO_TOKEN_FORM_SOCK(SockToken, Type) \
321 ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
323 typedef struct _SOCKET SOCKET
;
326 /// Socket completion token
328 typedef struct _SOCK_COMPLETION_TOKEN
{
329 EFI_EVENT Event
; ///< The event to be issued
330 EFI_STATUS Status
; ///< The status to be issued
331 } SOCK_COMPLETION_TOKEN
;
334 /// The application token with data packet
336 typedef struct _SOCK_IO_TOKEN
{
337 SOCK_COMPLETION_TOKEN Token
;
345 /// The request issued from socket layer to protocol layer.
348 SOCK_ATTACH
, ///< Attach current socket to a new PCB
349 SOCK_DETACH
, ///< Detach current socket from the PCB
350 SOCK_CONFIGURE
, ///< Configure attached PCB
351 SOCK_FLUSH
, ///< Flush attached PCB
352 SOCK_SND
, ///< Need protocol to send something
353 SOCK_SNDPUSH
, ///< Need protocol to send pushed data
354 SOCK_SNDURG
, ///< Need protocol to send urgent data
355 SOCK_CONSUMED
, ///< Application has retrieved data from socket
356 SOCK_CONNECT
, ///< Need to connect to a peer
357 SOCK_CLOSE
, ///< Need to close the protocol process
358 SOCK_ABORT
, ///< Need to reset the protocol process
359 SOCK_POLL
, ///< Need to poll to the protocol layer
360 SOCK_ROUTE
, ///< Need to add a route information
361 SOCK_MODE
, ///< Need to get the mode data of the protocol
362 SOCK_GROUP
///< Need to join a mcast group
369 SOCK_DGRAM
, ///< This socket providing datagram service
370 SOCK_STREAM
///< This socket providing stream service
374 /// The buffer structure of rcvd data and send data used by socket.
376 typedef struct _SOCK_BUFFER
{
377 UINT32 HighWater
; ///< The buffersize upper limit of sock_buffer
378 UINT32 LowWater
; ///< The low warter mark of sock_buffer
379 NET_BUF_QUEUE
*DataQueue
; ///< The queue to buffer data
383 The handler of protocol for request from socket.
385 @param Socket The socket issuing the request to protocol
386 @param Request The request issued by socket
387 @param RequestData The request related data
389 @retval EFI_SUCCESS The socket request is completed successfully.
390 @retval other The error status returned by the corresponding TCP
396 (*SOCK_PROTO_HANDLER
) (
398 IN SOCK_REQUEST Request
,
404 // Socket provided oprerations for low layer protocol
408 // Socket provided operations for user interface
412 Set the state of the socket.
414 @param Sock Pointer to the socket.
415 @param State The new state to be set.
425 Called by the low layer protocol to indicate the socket a connection is
428 This function just changes the socket's state to SO_CONNECTED
429 and signals the token used for connection establishment.
431 @param Sock Pointer to the socket associated with the
432 established connection.
436 SockConnEstablished (
441 Called by the low layer protocol to indicate the connection is closed.
443 This function flushes the socket, sets the state to SO_CLOSED and signals
446 @param Sock Pointer to the socket associated with the closed
456 Called by low layer protocol to indicate that some data is sent or processed.
458 This function trims the sent data in the socket send buffer, signals the data
461 @param Sock Pointer to the socket.
462 @param Count The length of the data processed or sent, in bytes.
472 Called by the low layer protocol to copy some data in socket send
473 buffer starting from the specific offset to a buffer provided by
476 @param Sock Pointer to the socket.
477 @param Offset The start point of the data to be copied.
478 @param Len The length of the data to be copied.
479 @param Dest Pointer to the destination to copy the data.
481 @return The data size copied.
493 Called by the low layer protocol to indicate that there
494 will be no more data from the communication peer.
496 This function set the socket's state to SO_NO_MORE_DATA and
497 signal all queued IO tokens with the error status EFI_CONNECTION_FIN.
499 @param Sock Pointer to the socket.
508 Called by the low layer protocol to deliver received data to socket layer.
510 This function will append the data to the socket receive buffer, set ther
511 urgent data length and then check if any receive token can be signaled.
513 @param Sock Pointer to the socket.
514 @param NetBuffer Pointer to the buffer that contains the received
516 @param UrgLen The length of the urgent data in the received data.
522 IN OUT NET_BUF
*NetBuffer
,
527 Get the length of the free space of the specific socket buffer.
529 @param Sock Pointer to the socket.
530 @param Which Flag to indicate which socket buffer to check,
531 either send buffer or receive buffer.
533 @return The length of the free space, in bytes.
543 Clone a new socket including its associated protocol control block.
545 @param Sock Pointer to the socket to be cloned.
547 @return Pointer to the newly cloned socket. If NULL, error condition occurred.
556 Signal the receive token with the specific error or
557 set socket error code after error is received.
559 @param Sock Pointer to the socket.
560 @param Error The error code received.
570 /// Proto type of the create callback
574 (*SOCK_CREATE_CALLBACK
) (
580 /// Proto type of the destroy callback
584 (*SOCK_DESTROY_CALLBACK
) (
590 /// The initialize data for create a new socket.
592 typedef struct _SOCK_INIT_DATA
{
596 SOCKET
*Parent
; ///< The parent of this socket
597 UINT32 BackLog
; ///< The connection limit for listening socket
598 UINT32 SndBufferSize
; ///< The high warter mark of send buffer
599 UINT32 RcvBufferSize
; ///< The high warter mark of receive buffer
600 VOID
*Protocol
; ///< The pointer to protocol function template
601 ///< wanted to install on socket
604 // Callbacks after socket is created and before socket is to be destroyed.
606 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
607 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
608 VOID
*Context
; ///< The context of the callback
611 // Opaque protocol data.
616 SOCK_PROTO_HANDLER ProtoHandler
; ///< The handler of protocol for socket request
618 EFI_HANDLE DriverBinding
; ///< The driver binding handle
622 /// The union type of TCP and UDP protocol.
624 typedef union _NET_PROTOCOL
{
625 EFI_TCP4_PROTOCOL TcpProtocol
; ///< Tcp protocol
626 EFI_UDP4_PROTOCOL UdpProtocol
; ///< Udp protocol
630 /// The socket structure representing a network service access point
635 // Socket description information
637 UINT32 Signature
; ///< Signature of the socket
638 EFI_HANDLE SockHandle
; ///< The virtual handle of the socket
639 EFI_HANDLE DriverBinding
; ///< Socket's driver binding protocol
640 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
641 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
643 SOCK_CONFIGURE_STATE ConfigureState
;
647 EFI_LOCK Lock
; ///< The lock of socket
648 SOCK_BUFFER SndBuffer
; ///< Send buffer of application's data
649 SOCK_BUFFER RcvBuffer
; ///< Receive buffer of received data
650 EFI_STATUS SockError
; ///< The error returned by low layer protocol
654 // Fields used to manage the connection request
656 UINT32 BackLog
; ///< the limit of connection to this socket
657 UINT32 ConnCnt
; ///< the current count of connections to it
658 SOCKET
*Parent
; ///< listening parent that accept the connection
659 LIST_ENTRY ConnectionList
; ///< the connections maintained by this socket
662 // The queue to buffer application's asynchronous token
664 LIST_ENTRY ListenTokenList
;
665 LIST_ENTRY RcvTokenList
;
666 LIST_ENTRY SndTokenList
;
667 LIST_ENTRY ProcessingSndTokenList
;
669 SOCK_COMPLETION_TOKEN
*ConnectionToken
; ///< app's token to signal if connected
670 SOCK_COMPLETION_TOKEN
*CloseToken
; ///< app's token to signal if closed
673 // Interface for low level protocol
675 SOCK_PROTO_HANDLER ProtoHandler
; ///< The request handler of protocol
676 UINT8 ProtoReserved
[PROTO_RESERVED_LEN
]; ///< Data fields reserved for protocol
677 NET_PROTOCOL NetProtocol
; ///< TCP or UDP protocol socket used
680 // Callbacks after socket is created and before socket is to be destroyed.
682 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
683 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
684 VOID
*Context
; ///< The context of the callback
688 /// The token structure buffered in socket layer.
690 typedef struct _SOCK_TOKEN
{
691 LIST_ENTRY TokenList
; ///< The entry to add in the token list
692 SOCK_COMPLETION_TOKEN
*Token
; ///< The application's token
693 UINT32 RemainDataLen
; ///< Unprocessed data length
694 SOCKET
*Sock
; ///< The poninter to the socket this token
699 /// Reserved data to access the NET_BUF delivered by UDP driver.
701 typedef struct _UDP_RSV_DATA
{
703 EFI_UDP4_SESSION_DATA Session
;
707 /// Reserved data to access the NET_BUF delivered by TCP driver.
709 typedef struct _TCP_RSV_DATA
{
714 Create a socket and its associated protocol control block
715 with the intial data SockInitData and protocol specific
718 @param SockInitData Inital data to setting the socket.
720 @return Pointer to the newly created socket. If NULL, error condition occured.
725 IN SOCK_INIT_DATA
*SockInitData
729 Destory the socket Sock and its associated protocol control block.
731 @param Sock The socket to be destroyed.
733 @retval EFI_SUCCESS The socket Sock is destroyed successfully.
734 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
743 Configure the specific socket Sock using configuration data ConfigData.
745 @param Sock Pointer to the socket to be configured.
746 @param ConfigData Pointer to the configuration data.
748 @retval EFI_SUCCESS The socket is configured successfully.
749 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket or the
750 socket is already configured.
760 Initiate a connection establishment process.
762 @param Sock Pointer to the socket to initiate the initate the
764 @param Token Pointer to the token used for the connection
767 @retval EFI_SUCCESS The connection is initialized successfully.
768 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
769 socket is closed, or the socket is not configured to
770 be an active one, or the token is already in one of
772 @retval EFI_NO_MAPPING The IP address configuration operation is not
774 @retval EFI_NOT_STARTED The socket is not configured.
784 Issue a listen token to get an existed connected network instance
785 or wait for a connection if there is none.
787 @param Sock Pointer to the socket to accept connections.
788 @param Token The token to accept a connection.
790 @retval EFI_SUCCESS Either a connection is accpeted or the Token is
791 buffered for further acception.
792 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
793 socket is closed, or the socket is not configured to
794 be a passive one, or the token is already in one of
796 @retval EFI_NO_MAPPING The IP address configuration operation is not
798 @retval EFI_NOT_STARTED The socket is not configured.
799 @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit.
809 Issue a token with data to the socket to send out.
811 @param Sock Pointer to the socket to process the token with
813 @param Token The token with data that needs to send out.
815 @retval EFI_SUCCESS The token is processed successfully.
816 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
817 socket is closed, or the socket is not in a
818 synchronized state , or the token is already in one
819 of this socket's lists.
820 @retval EFI_NO_MAPPING The IP address configuration operation is not
822 @retval EFI_NOT_STARTED The socket is not configured.
823 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
833 Issue a token to get data from the socket.
835 @param Sock Pointer to the socket to get data from.
836 @param Token The token to store the received data from the
839 @retval EFI_SUCCESS The token is processed successfully.
840 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
841 socket is closed, or the socket is not in a
842 synchronized state , or the token is already in one
843 of this socket's lists.
844 @retval EFI_NO_MAPPING The IP address configuration operation is not
846 @retval EFI_NOT_STARTED The socket is not configured.
847 @retval EFI_CONNECTION_FIN The connection is closed and there is no more data.
848 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
858 Reset the socket and its associated protocol control block.
860 @param Sock Pointer to the socket to be flushed.
862 @retval EFI_SUCCESS The socket is flushed successfully.
863 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
872 Close or abort the socket associated connection.
874 @param Sock Pointer to the socket of the connection to close or
876 @param Token The token for close operation.
877 @param OnAbort TRUE for aborting the connection, FALSE to close it.
879 @retval EFI_SUCCESS The close or abort operation is initialized
881 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
882 socket is closed, or the socket is not in a
883 synchronized state , or the token is already in one
884 of this socket's lists.
885 @retval EFI_NO_MAPPING The IP address configuration operation is not
887 @retval EFI_NOT_STARTED The socket is not configured.
898 Get the mode data of the low layer protocol.
900 @param Sock Pointer to the socket to get mode data from.
901 @param Mode Pointer to the data to store the low layer mode
904 @retval EFI_SUCCESS The mode data is got successfully.
905 @retval EFI_NOT_STARTED The socket is not configured.
915 Configure the low level protocol to join a multicast group for
916 this socket's connection.
918 @param Sock Pointer to the socket of the connection to join the
919 specific multicast group.
920 @param GroupInfo Pointer to the multicast group info.
922 @retval EFI_SUCCESS The configuration is done successfully.
923 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
924 @retval EFI_NOT_STARTED The socket is not configured.
934 Add or remove route information in IP route table associated
937 @param Sock Pointer to the socket associated with the IP route
939 @param RouteInfo Pointer to the route information to be processed.
941 @retval EFI_SUCCESS The route table is updated successfully.
942 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
943 @retval EFI_NO_MAPPING The IP address configuration operation is not
945 @retval EFI_NOT_STARTED The socket is not configured.
955 // Supporting function to operate on socket buffer
959 Get the first buffer block in the specific socket buffer.
961 @param Sockbuf Pointer to the socket buffer.
963 @return Pointer to the first buffer in the queue. NULL if the queue is empty.
968 IN SOCK_BUFFER
*Sockbuf
972 Get the next buffer block in the specific socket buffer.
974 @param Sockbuf Pointer to the socket buffer.
975 @param SockEntry Pointer to the buffer block prior to the required
978 @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
979 the tail or head entry.
984 IN SOCK_BUFFER
*Sockbuf
,
985 IN NET_BUF
*SockEntry