4 Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>
5 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>
33 #include <Library/PrintLib.h>
35 #define SOCK_SND_BUF 0
36 #define SOCK_RCV_BUF 1
38 #define SOCK_BUFF_LOW_WATER (2 * 1024)
39 #define SOCK_RCV_BUFF_SIZE (8 * 1024)
40 #define SOCK_SND_BUFF_SIZE (8 * 1024)
41 #define SOCK_BACKLOG 5
43 #define PROTO_RESERVED_LEN 20
45 #define SO_NO_MORE_DATA 0x0001
50 // When a socket is created it enters into SO_UNCONFIGURED,
51 // no actions can be taken on this socket, only after calling
52 // SockConfigure. The state transition diagram of socket is
55 // SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING
57 // | ---> SO_LISTENING |
59 // |------------------SO_DISCONNECTING<-- SO_CONNECTED
61 // A passive socket can only go into SO_LISTENING and
62 // SO_UNCONFIGURED state. SO_XXXING state is a middle state
63 // when a socket is undergoing a protocol procedure such
64 // as requesting a TCP connection.
73 #define SO_LISTENING 1
74 #define SO_CONNECTING 2
75 #define SO_CONNECTED 3
76 #define SO_DISCONNECTING 4
79 /// Socket configure state
81 #define SO_UNCONFIGURED 0
82 #define SO_CONFIGURED_ACTIVE 1
83 #define SO_CONFIGURED_PASSIVE 2
84 #define SO_NO_MAPPING 3
87 Set socket SO_NO_MORE_DATA flag.
89 @param Sock Pointer to the socket
92 #define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
95 Check whether the socket is unconfigured.
97 @param Sock Pointer to the socket
99 @retval True The socket is unconfigued
100 @retval False The socket is not unconfigued
103 #define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
106 Check whether the socket is configured.
108 @param Sock Pointer to the socket
110 @retval True The socket is configued
111 @retval False The socket is not configued
114 #define SOCK_IS_CONFIGURED(Sock) \
115 (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
116 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
119 Check whether the socket is configured to active mode.
121 @param Sock Pointer to the socket
123 @retval True The socket is configued to active mode
124 @retval False The socket is not configued to active mode
127 #define SOCK_IS_CONFIGURED_ACTIVE(Sock) \
128 ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
131 Check whether the socket is configured to passive mode.
133 @param Sock Pointer to the socket
135 @retval True The socket is configued to passive mode
136 @retval False The socket is not configued to passive mode
139 #define SOCK_IS_CONNECTED_PASSIVE(Sock) \
140 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
143 Check whether the socket is mapped.
145 @param Sock Pointer to the socket
147 @retval True The socket is no mapping
148 @retval False The socket is mapped
151 #define SOCK_IS_NO_MAPPING(Sock) \
152 ((Sock)->ConfigureState == SO_NO_MAPPING)
155 Check whether the socket is closed.
157 @param Sock Pointer to the socket
159 @retval True The socket is closed
160 @retval False The socket is not closed
163 #define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
166 Check whether the socket is listening.
168 @param Sock Pointer to the socket
170 @retval True The socket is listening
171 @retval False The socket is not listening
174 #define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
177 Check whether the socket is connecting.
179 @param Sock Pointer to the socket
181 @retval True The socket is connecting
182 @retval False The socket is not connecting
185 #define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
188 Check whether the socket has connected.
190 @param Sock Pointer to the socket
192 @retval True The socket has connected
193 @retval False The socket has not connected
196 #define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
199 Check whether the socket is disconnecting.
201 @param Sock Pointer to the socket
203 @retval True The socket is disconnecting
204 @retval False The socket is not disconnecting
207 #define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
210 Check whether the socket is no more data.
212 @param Sock Pointer to the socket
214 @retval True The socket is no more data
215 @retval False The socket still has data
218 #define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
221 Set the size of the receive buffer.
223 @param Sock Pointer to the socket
224 @param Size The size to set
227 #define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
230 Get the size of the receive buffer.
232 @param Sock Pointer to the socket
234 @return The receive buffer size
237 #define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
240 Get the size of the receive data.
242 @param Sock Pointer to the socket
244 @return The received data size
247 #define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
250 Set the size of the send buffer.
252 @param Sock Pointer to the socket
253 @param Size The size to set
256 #define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
259 Get the size of the send buffer.
261 @param Sock Pointer to the socket
263 @return The send buffer size
266 #define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
269 Get the size of the send data.
271 @param Sock Pointer to the socket
273 @return The send data size
276 #define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
279 Set the backlog value of the socket.
281 @param Sock Pointer to the socket
282 @param Value The value to set
285 #define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
288 Get the backlog value of the socket.
290 @param Sock Pointer to the socket
292 @return The backlog value
295 #define GET_BACKLOG(Sock) ((Sock)->BackLog)
298 Set the socket with error state.
300 @param Sock Pointer to the socket
301 @param Error The error state
304 #define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
306 #define SND_BUF_HDR_LEN(Sock) \
307 ((SockBufFirst (&((Sock)->SndBuffer)))->TotalSize)
309 #define RCV_BUF_HDR_LEN(Sock) \
310 ((SockBufFirst (&((Sock)->RcvBuffer)))->TotalSize)
312 #define SOCK_SIGNATURE SIGNATURE_32 ('S', 'O', 'C', 'K')
314 #define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
316 #define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
318 #define PROTO_TOKEN_FORM_SOCK(SockToken, Type) \
319 ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
321 typedef struct _SOCKET SOCKET
;
324 /// Socket completion token
326 typedef struct _SOCK_COMPLETION_TOKEN
{
327 EFI_EVENT Event
; ///< The event to be issued
328 EFI_STATUS Status
; ///< The status to be issued
329 } SOCK_COMPLETION_TOKEN
;
337 /// The application token with data packet
339 typedef struct _SOCK_IO_TOKEN
{
340 SOCK_COMPLETION_TOKEN Token
;
345 /// The request issued from socket layer to protocol layer.
347 #define SOCK_ATTACH 0 ///< Attach current socket to a new PCB
348 #define SOCK_DETACH 1 ///< Detach current socket from the PCB
349 #define SOCK_CONFIGURE 2 ///< Configure attached PCB
350 #define SOCK_FLUSH 3 ///< Flush attached PCB
351 #define SOCK_SND 4 ///< Need protocol to send something
352 #define SOCK_SNDPUSH 5 ///< Need protocol to send pushed data
353 #define SOCK_SNDURG 6 ///< Need protocol to send urgent data
354 #define SOCK_CONSUMED 7 ///< Application has retrieved data from socket
355 #define SOCK_CONNECT 8 ///< Need to connect to a peer
356 #define SOCK_CLOSE 9 ///< Need to close the protocol process
357 #define SOCK_ABORT 10 ///< Need to reset the protocol process
358 #define SOCK_POLL 11 ///< Need to poll to the protocol layer
359 #define SOCK_ROUTE 12 ///< Need to add a route information
360 #define SOCK_MODE 13 ///< Need to get the mode data of the protocol
361 #define SOCK_GROUP 14 ///< Need to join a mcast group
367 SockDgram
, ///< This socket providing datagram service
368 SockStream
///< This socket providing stream service
372 /// The buffer structure of rcvd data and send data used by socket.
374 typedef struct _SOCK_BUFFER
{
375 UINT32 HighWater
; ///< The buffersize upper limit of sock_buffer
376 UINT32 LowWater
; ///< The low warter mark of sock_buffer
377 NET_BUF_QUEUE
*DataQueue
; ///< The queue to buffer data
381 The handler of protocol for request from socket.
383 @param Socket The socket issuing the request to protocol
384 @param Request The request issued by socket
385 @param RequestData The request related data
387 @retval EFI_SUCCESS The socket request is completed successfully.
388 @retval other The error status returned by the corresponding TCP
394 (*SOCK_PROTO_HANDLER
) (
402 // Socket provided oprerations for low layer protocol
406 // Socket provided operations for user interface
410 Set the state of the socket.
412 @param Sock Pointer to the socket.
413 @param State The new socket state to be set.
423 Called by the low layer protocol to indicate the socket a connection is
426 This function just changes the socket's state to SO_CONNECTED
427 and signals the token used for connection establishment.
429 @param Sock Pointer to the socket associated with the
430 established connection.
434 SockConnEstablished (
439 Called by the low layer protocol to indicate the connection is closed.
441 This function flushes the socket, sets the state to SO_CLOSED and signals
444 @param Sock Pointer to the socket associated with the closed
454 Called by low layer protocol to indicate that some data is sent or processed.
456 This function trims the sent data in the socket send buffer, signals the data
459 @param Sock Pointer to the socket.
460 @param Count The length of the data processed or sent, in bytes.
470 Called by the low layer protocol to copy some data in socket send
471 buffer starting from the specific offset to a buffer provided by
474 @param Sock Pointer to the socket.
475 @param Offset The start point of the data to be copied.
476 @param Len The length of the data to be copied.
477 @param Dest Pointer to the destination to copy the data.
479 @return The data size copied.
491 Called by the low layer protocol to indicate that there
492 will be no more data from the communication peer.
494 This function set the socket's state to SO_NO_MORE_DATA and
495 signal all queued IO tokens with the error status EFI_CONNECTION_FIN.
497 @param Sock Pointer to the socket.
506 Called by the low layer protocol to deliver received data to socket layer.
508 This function will append the data to the socket receive buffer, set ther
509 urgent data length and then check if any receive token can be signaled.
511 @param Sock Pointer to the socket.
512 @param NetBuffer Pointer to the buffer that contains the received
514 @param UrgLen The length of the urgent data in the received data.
520 IN OUT NET_BUF
*NetBuffer
,
525 Get the length of the free space of the specific socket buffer.
527 @param Sock Pointer to the socket.
528 @param Which Flag to indicate which socket buffer to check,
529 either send buffer or receive buffer.
531 @return The length of the free space, in bytes.
541 Clone a new socket including its associated protocol control block.
543 @param Sock Pointer to the socket to be cloned.
545 @return Pointer to the newly cloned socket. If NULL, error condition occurred.
554 Signal the receive token with the specific error or
555 set socket error code after error is received.
557 @param Sock Pointer to the socket.
558 @param Error The error code received.
568 /// Proto type of the create callback
572 (*SOCK_CREATE_CALLBACK
) (
578 /// Proto type of the destroy callback
582 (*SOCK_DESTROY_CALLBACK
) (
588 /// The initialize data for create a new socket.
590 typedef struct _SOCK_INIT_DATA
{
594 SOCKET
*Parent
; ///< The parent of this socket
595 UINT32 BackLog
; ///< The connection limit for listening socket
596 UINT32 SndBufferSize
; ///< The high warter mark of send buffer
597 UINT32 RcvBufferSize
; ///< The high warter mark of receive buffer
598 VOID
*Protocol
; ///< The pointer to protocol function template
599 ///< wanted to install on socket
602 // Callbacks after socket is created and before socket is to be destroyed.
604 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
605 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
606 VOID
*Context
; ///< The context of the callback
609 // Opaque protocol data.
614 SOCK_PROTO_HANDLER ProtoHandler
; ///< The handler of protocol for socket request
616 EFI_HANDLE DriverBinding
; ///< The driver binding handle
620 /// The union type of TCP and UDP protocol.
622 typedef union _NET_PROTOCOL
{
623 EFI_TCP4_PROTOCOL TcpProtocol
; ///< Tcp protocol
624 EFI_UDP4_PROTOCOL UdpProtocol
; ///< Udp protocol
628 /// The socket structure representing a network service access point
633 // Socket description information
635 UINT32 Signature
; ///< Signature of the socket
636 EFI_HANDLE SockHandle
; ///< The virtual handle of the socket
637 EFI_HANDLE DriverBinding
; ///< Socket's driver binding protocol
638 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
639 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
641 UINT8 ConfigureState
;
645 EFI_LOCK Lock
; ///< The lock of socket
646 SOCK_BUFFER SndBuffer
; ///< Send buffer of application's data
647 SOCK_BUFFER RcvBuffer
; ///< Receive buffer of received data
648 EFI_STATUS SockError
; ///< The error returned by low layer protocol
652 // Fields used to manage the connection request
654 UINT32 BackLog
; ///< the limit of connection to this socket
655 UINT32 ConnCnt
; ///< the current count of connections to it
656 SOCKET
*Parent
; ///< listening parent that accept the connection
657 LIST_ENTRY ConnectionList
; ///< the connections maintained by this socket
660 // The queue to buffer application's asynchronous token
662 LIST_ENTRY ListenTokenList
;
663 LIST_ENTRY RcvTokenList
;
664 LIST_ENTRY SndTokenList
;
665 LIST_ENTRY ProcessingSndTokenList
;
667 SOCK_COMPLETION_TOKEN
*ConnectionToken
; ///< app's token to signal if connected
668 SOCK_COMPLETION_TOKEN
*CloseToken
; ///< app's token to signal if closed
671 // Interface for low level protocol
673 SOCK_PROTO_HANDLER ProtoHandler
; ///< The request handler of protocol
674 UINT8 ProtoReserved
[PROTO_RESERVED_LEN
]; ///< Data fields reserved for protocol
675 NET_PROTOCOL NetProtocol
; ///< TCP or UDP protocol socket used
678 // Callbacks after socket is created and before socket is to be destroyed.
680 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
681 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
682 VOID
*Context
; ///< The context of the callback
686 /// The token structure buffered in socket layer.
688 typedef struct _SOCK_TOKEN
{
689 LIST_ENTRY TokenList
; ///< The entry to add in the token list
690 SOCK_COMPLETION_TOKEN
*Token
; ///< The application's token
691 UINT32 RemainDataLen
; ///< Unprocessed data length
692 SOCKET
*Sock
; ///< The poninter to the socket this token
697 /// Reserved data to access the NET_BUF delivered by UDP driver.
699 typedef struct _UDP_RSV_DATA
{
701 EFI_UDP4_SESSION_DATA Session
;
705 /// Reserved data to access the NET_BUF delivered by TCP driver.
707 typedef struct _TCP_RSV_DATA
{
712 Create a socket and its associated protocol control block
713 with the intial data SockInitData and protocol specific
716 @param SockInitData Inital data to setting the socket.
718 @return Pointer to the newly created socket. If NULL, error condition occured.
723 IN SOCK_INIT_DATA
*SockInitData
727 Destroy the socket Sock and its associated protocol control block.
729 @param Sock The socket to be destroyed.
731 @retval EFI_SUCCESS The socket Sock is destroyed successfully.
732 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
741 Configure the specific socket Sock using configuration data ConfigData.
743 @param Sock Pointer to the socket to be configured.
744 @param ConfigData Pointer to the configuration data.
746 @retval EFI_SUCCESS The socket is configured successfully.
747 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket or the
748 socket is already configured.
758 Initiate a connection establishment process.
760 @param Sock Pointer to the socket to initiate the initate the
762 @param Token Pointer to the token used for the connection
765 @retval EFI_SUCCESS The connection is initialized successfully.
766 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
767 socket is closed, or the socket is not configured to
768 be an active one, or the token is already in one of
770 @retval EFI_NO_MAPPING The IP address configuration operation is not
772 @retval EFI_NOT_STARTED The socket is not configured.
782 Issue a listen token to get an existed connected network instance
783 or wait for a connection if there is none.
785 @param Sock Pointer to the socket to accept connections.
786 @param Token The token to accept a connection.
788 @retval EFI_SUCCESS Either a connection is accpeted or the Token is
789 buffered for further acception.
790 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
791 socket is closed, or the socket is not configured to
792 be a passive one, or the token is already in one of
794 @retval EFI_NO_MAPPING The IP address configuration operation is not
796 @retval EFI_NOT_STARTED The socket is not configured.
797 @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit.
807 Issue a token with data to the socket to send out.
809 @param Sock Pointer to the socket to process the token with
811 @param Token The token with data that needs to send out.
813 @retval EFI_SUCCESS The token is processed successfully.
814 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
815 socket is closed, or the socket is not in a
816 synchronized state , or the token is already in one
817 of this socket's lists.
818 @retval EFI_NO_MAPPING The IP address configuration operation is not
820 @retval EFI_NOT_STARTED The socket is not configured.
821 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
831 Issue a token to get data from the socket.
833 @param Sock Pointer to the socket to get data from.
834 @param Token The token to store the received data from the
837 @retval EFI_SUCCESS The token is processed successfully.
838 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
839 socket is closed, or the socket is not in a
840 synchronized state , or the token is already in one
841 of this socket's lists.
842 @retval EFI_NO_MAPPING The IP address configuration operation is not
844 @retval EFI_NOT_STARTED The socket is not configured.
845 @retval EFI_CONNECTION_FIN The connection is closed and there is no more data.
846 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
856 Reset the socket and its associated protocol control block.
858 @param Sock Pointer to the socket to be flushed.
860 @retval EFI_SUCCESS The socket is flushed successfully.
861 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
870 Close or abort the socket associated connection.
872 @param Sock Pointer to the socket of the connection to close or
874 @param Token The token for close operation.
875 @param OnAbort TRUE for aborting the connection, FALSE to close it.
877 @retval EFI_SUCCESS The close or abort operation is initialized
879 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
880 socket is closed, or the socket is not in a
881 synchronized state , or the token is already in one
882 of this socket's lists.
883 @retval EFI_NO_MAPPING The IP address configuration operation is not
885 @retval EFI_NOT_STARTED The socket is not configured.
896 Get the mode data of the low layer protocol.
898 @param Sock Pointer to the socket to get mode data from.
899 @param Mode Pointer to the data to store the low layer mode
902 @retval EFI_SUCCESS The mode data is got successfully.
903 @retval EFI_NOT_STARTED The socket is not configured.
913 Configure the low level protocol to join a multicast group for
914 this socket's connection.
916 @param Sock Pointer to the socket of the connection to join the
917 specific multicast group.
918 @param GroupInfo Pointer to the multicast group info.
920 @retval EFI_SUCCESS The configuration is done successfully.
921 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
922 @retval EFI_NOT_STARTED The socket is not configured.
932 Add or remove route information in IP route table associated
935 @param Sock Pointer to the socket associated with the IP route
937 @param RouteInfo Pointer to the route information to be processed.
939 @retval EFI_SUCCESS The route table is updated successfully.
940 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
941 @retval EFI_NO_MAPPING The IP address configuration operation is not
943 @retval EFI_NOT_STARTED The socket is not configured.
953 // Supporting function to operate on socket buffer
957 Get the first buffer block in the specific socket buffer.
959 @param Sockbuf Pointer to the socket buffer.
961 @return Pointer to the first buffer in the queue. NULL if the queue is empty.
966 IN SOCK_BUFFER
*Sockbuf
970 Get the next buffer block in the specific socket buffer.
972 @param Sockbuf Pointer to the socket buffer.
973 @param SockEntry Pointer to the buffer block prior to the required
976 @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
977 the tail or head entry.
982 IN SOCK_BUFFER
*Sockbuf
,
983 IN NET_BUF
*SockEntry