4 Copyright (c) 2005 - 2009, 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.
72 #define SO_LISTENING 1
73 #define SO_CONNECTING 2
74 #define SO_CONNECTED 3
75 #define SO_DISCONNECTING 4
78 /// Socket configure state
80 #define SO_UNCONFIGURED 0
81 #define SO_CONFIGURED_ACTIVE 1
82 #define SO_CONFIGURED_PASSIVE 2
83 #define SO_NO_MAPPING 3
86 Set socket SO_NO_MORE_DATA flag.
88 @param Sock Pointer to the socket
91 #define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
94 Check whether the socket is unconfigured.
96 @param Sock Pointer to the socket
98 @retval True The socket is unconfigued
99 @retval False The socket is not unconfigued
102 #define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
105 Check whether the socket is configured.
107 @param Sock Pointer to the socket
109 @retval True The socket is configued
110 @retval False The socket is not configued
113 #define SOCK_IS_CONFIGURED(Sock) \
114 (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
115 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
118 Check whether the socket is configured to active mode.
120 @param Sock Pointer to the socket
122 @retval True The socket is configued to active mode
123 @retval False The socket is not configued to active mode
126 #define SOCK_IS_CONFIGURED_ACTIVE(Sock) \
127 ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
130 Check whether the socket is configured to passive mode.
132 @param Sock Pointer to the socket
134 @retval True The socket is configued to passive mode
135 @retval False The socket is not configued to passive mode
138 #define SOCK_IS_CONNECTED_PASSIVE(Sock) \
139 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
142 Check whether the socket is mapped.
144 @param Sock Pointer to the socket
146 @retval True The socket is no mapping
147 @retval False The socket is mapped
150 #define SOCK_IS_NO_MAPPING(Sock) \
151 ((Sock)->ConfigureState == SO_NO_MAPPING)
154 Check whether the socket is closed.
156 @param Sock Pointer to the socket
158 @retval True The socket is closed
159 @retval False The socket is not closed
162 #define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
165 Check whether the socket is listening.
167 @param Sock Pointer to the socket
169 @retval True The socket is listening
170 @retval False The socket is not listening
173 #define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
176 Check whether the socket is connecting.
178 @param Sock Pointer to the socket
180 @retval True The socket is connecting
181 @retval False The socket is not connecting
184 #define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
187 Check whether the socket has connected.
189 @param Sock Pointer to the socket
191 @retval True The socket has connected
192 @retval False The socket has not connected
195 #define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
198 Check whether the socket is disconnecting.
200 @param Sock Pointer to the socket
202 @retval True The socket is disconnecting
203 @retval False The socket is not disconnecting
206 #define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
209 Check whether the socket is no more data.
211 @param Sock Pointer to the socket
213 @retval True The socket is no more data
214 @retval False The socket still has data
217 #define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
220 Set the size of the receive buffer.
222 @param Sock Pointer to the socket
223 @param Size The size to set
226 #define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
229 Get the size of the receive buffer.
231 @param Sock Pointer to the socket
233 @return The receive buffer size
236 #define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
239 Get the size of the receive data.
241 @param Sock Pointer to the socket
243 @return The received data size
246 #define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
249 Set the size of the send buffer.
251 @param Sock Pointer to the socket
252 @param Size The size to set
255 #define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
258 Get the size of the send buffer.
260 @param Sock Pointer to the socket
262 @return The send buffer size
265 #define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
268 Get the size of the send data.
270 @param Sock Pointer to the socket
272 @return The send data size
275 #define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
278 Set the backlog value of the socket.
280 @param Sock Pointer to the socket
281 @param Value The value to set
284 #define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
287 Get the backlog value of the socket.
289 @param Sock Pointer to the socket
291 @return The backlog value
294 #define GET_BACKLOG(Sock) ((Sock)->BackLog)
297 Set the socket with error state.
299 @param Sock Pointer to the socket
300 @param Error The error state
303 #define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
305 #define SND_BUF_HDR_LEN(Sock) \
306 ((SockBufFirst (&((Sock)->SndBuffer)))->TotalSize)
308 #define RCV_BUF_HDR_LEN(Sock) \
309 ((SockBufFirst (&((Sock)->RcvBuffer)))->TotalSize)
311 #define SOCK_SIGNATURE SIGNATURE_32 ('S', 'O', 'C', 'K')
313 #define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
315 #define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
317 #define PROTO_TOKEN_FORM_SOCK(SockToken, Type) \
318 ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
320 typedef struct _SOCKET SOCKET
;
323 /// Socket completion token
325 typedef struct _SOCK_COMPLETION_TOKEN
{
326 EFI_EVENT Event
; ///< The event to be issued
327 EFI_STATUS Status
; ///< The status to be issued
328 } SOCK_COMPLETION_TOKEN
;
336 /// The application token with data packet
338 typedef struct _SOCK_IO_TOKEN
{
339 SOCK_COMPLETION_TOKEN Token
;
344 /// The request issued from socket layer to protocol layer.
346 #define SOCK_ATTACH 0 ///< Attach current socket to a new PCB
347 #define SOCK_DETACH 1 ///< Detach current socket from the PCB
348 #define SOCK_CONFIGURE 2 ///< Configure attached PCB
349 #define SOCK_FLUSH 3 ///< Flush attached PCB
350 #define SOCK_SND 4 ///< Need protocol to send something
351 #define SOCK_SNDPUSH 5 ///< Need protocol to send pushed data
352 #define SOCK_SNDURG 6 ///< Need protocol to send urgent data
353 #define SOCK_CONSUMED 7 ///< Application has retrieved data from socket
354 #define SOCK_CONNECT 8 ///< Need to connect to a peer
355 #define SOCK_CLOSE 9 ///< Need to close the protocol process
356 #define SOCK_ABORT 10 ///< Need to reset the protocol process
357 #define SOCK_POLL 11 ///< Need to poll to the protocol layer
358 #define SOCK_ROUTE 12 ///< Need to add a route information
359 #define SOCK_MODE 13 ///< Need to get the mode data of the protocol
360 #define SOCK_GROUP 14 ///< Need to join a mcast group
366 SockDgram
, ///< This socket providing datagram service
367 SockStream
///< This socket providing stream service
371 /// The buffer structure of rcvd data and send data used by socket.
373 typedef struct _SOCK_BUFFER
{
374 UINT32 HighWater
; ///< The buffersize upper limit of sock_buffer
375 UINT32 LowWater
; ///< The low warter mark of sock_buffer
376 NET_BUF_QUEUE
*DataQueue
; ///< The queue to buffer data
380 The handler of protocol for request from socket.
382 @param Socket The socket issuing the request to protocol
383 @param Request The request issued by socket
384 @param RequestData The request related data
386 @retval EFI_SUCCESS The socket request is completed successfully.
387 @retval other The error status returned by the corresponding TCP
393 (*SOCK_PROTO_HANDLER
) (
401 // Socket provided oprerations for low layer protocol
405 // Socket provided operations for user interface
409 Set the state of the socket.
411 @param Sock Pointer to the socket.
412 @param State The new socket state to be set.
422 Called by the low layer protocol to indicate the socket a connection is
425 This function just changes the socket's state to SO_CONNECTED
426 and signals the token used for connection establishment.
428 @param Sock Pointer to the socket associated with the
429 established connection.
433 SockConnEstablished (
438 Called by the low layer protocol to indicate the connection is closed.
440 This function flushes the socket, sets the state to SO_CLOSED and signals
443 @param Sock Pointer to the socket associated with the closed
453 Called by low layer protocol to indicate that some data is sent or processed.
455 This function trims the sent data in the socket send buffer, signals the data
458 @param Sock Pointer to the socket.
459 @param Count The length of the data processed or sent, in bytes.
469 Called by the low layer protocol to copy some data in socket send
470 buffer starting from the specific offset to a buffer provided by
473 @param Sock Pointer to the socket.
474 @param Offset The start point of the data to be copied.
475 @param Len The length of the data to be copied.
476 @param Dest Pointer to the destination to copy the data.
478 @return The data size copied.
490 Called by the low layer protocol to indicate that there
491 will be no more data from the communication peer.
493 This function set the socket's state to SO_NO_MORE_DATA and
494 signal all queued IO tokens with the error status EFI_CONNECTION_FIN.
496 @param Sock Pointer to the socket.
505 Called by the low layer protocol to deliver received data to socket layer.
507 This function will append the data to the socket receive buffer, set ther
508 urgent data length and then check if any receive token can be signaled.
510 @param Sock Pointer to the socket.
511 @param NetBuffer Pointer to the buffer that contains the received
513 @param UrgLen The length of the urgent data in the received data.
519 IN OUT NET_BUF
*NetBuffer
,
524 Get the length of the free space of the specific socket buffer.
526 @param Sock Pointer to the socket.
527 @param Which Flag to indicate which socket buffer to check,
528 either send buffer or receive buffer.
530 @return The length of the free space, in bytes.
540 Clone a new socket including its associated protocol control block.
542 @param Sock Pointer to the socket to be cloned.
544 @return Pointer to the newly cloned socket. If NULL, error condition occurred.
553 Signal the receive token with the specific error or
554 set socket error code after error is received.
556 @param Sock Pointer to the socket.
557 @param Error The error code received.
567 /// Proto type of the create callback
571 (*SOCK_CREATE_CALLBACK
) (
577 /// Proto type of the destroy callback
581 (*SOCK_DESTROY_CALLBACK
) (
587 /// The initialize data for create a new socket.
589 typedef struct _SOCK_INIT_DATA
{
593 SOCKET
*Parent
; ///< The parent of this socket
594 UINT32 BackLog
; ///< The connection limit for listening socket
595 UINT32 SndBufferSize
; ///< The high warter mark of send buffer
596 UINT32 RcvBufferSize
; ///< The high warter mark of receive buffer
597 VOID
*Protocol
; ///< The pointer to protocol function template
598 ///< wanted to install on socket
601 // Callbacks after socket is created and before socket is to be destroyed.
603 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
604 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
605 VOID
*Context
; ///< The context of the callback
608 // Opaque protocol data.
613 SOCK_PROTO_HANDLER ProtoHandler
; ///< The handler of protocol for socket request
615 EFI_HANDLE DriverBinding
; ///< The driver binding handle
619 /// The union type of TCP and UDP protocol.
621 typedef union _NET_PROTOCOL
{
622 EFI_TCP4_PROTOCOL TcpProtocol
; ///< Tcp protocol
623 EFI_UDP4_PROTOCOL UdpProtocol
; ///< Udp protocol
627 /// The socket structure representing a network service access point
632 // Socket description information
634 UINT32 Signature
; ///< Signature of the socket
635 EFI_HANDLE SockHandle
; ///< The virtual handle of the socket
636 EFI_HANDLE DriverBinding
; ///< Socket's driver binding protocol
637 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
638 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
640 UINT8 ConfigureState
;
644 EFI_LOCK Lock
; ///< The lock of socket
645 SOCK_BUFFER SndBuffer
; ///< Send buffer of application's data
646 SOCK_BUFFER RcvBuffer
; ///< Receive buffer of received data
647 EFI_STATUS SockError
; ///< The error returned by low layer protocol
651 // Fields used to manage the connection request
653 UINT32 BackLog
; ///< the limit of connection to this socket
654 UINT32 ConnCnt
; ///< the current count of connections to it
655 SOCKET
*Parent
; ///< listening parent that accept the connection
656 LIST_ENTRY ConnectionList
; ///< the connections maintained by this socket
659 // The queue to buffer application's asynchronous token
661 LIST_ENTRY ListenTokenList
;
662 LIST_ENTRY RcvTokenList
;
663 LIST_ENTRY SndTokenList
;
664 LIST_ENTRY ProcessingSndTokenList
;
666 SOCK_COMPLETION_TOKEN
*ConnectionToken
; ///< app's token to signal if connected
667 SOCK_COMPLETION_TOKEN
*CloseToken
; ///< app's token to signal if closed
670 // Interface for low level protocol
672 SOCK_PROTO_HANDLER ProtoHandler
; ///< The request handler of protocol
673 UINT8 ProtoReserved
[PROTO_RESERVED_LEN
]; ///< Data fields reserved for protocol
674 NET_PROTOCOL NetProtocol
; ///< TCP or UDP protocol socket used
677 // Callbacks after socket is created and before socket is to be destroyed.
679 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
680 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
681 VOID
*Context
; ///< The context of the callback
685 /// The token structure buffered in socket layer.
687 typedef struct _SOCK_TOKEN
{
688 LIST_ENTRY TokenList
; ///< The entry to add in the token list
689 SOCK_COMPLETION_TOKEN
*Token
; ///< The application's token
690 UINT32 RemainDataLen
; ///< Unprocessed data length
691 SOCKET
*Sock
; ///< The poninter to the socket this token
696 /// Reserved data to access the NET_BUF delivered by UDP driver.
698 typedef struct _UDP_RSV_DATA
{
700 EFI_UDP4_SESSION_DATA Session
;
704 /// Reserved data to access the NET_BUF delivered by TCP driver.
706 typedef struct _TCP_RSV_DATA
{
711 Create a socket and its associated protocol control block
712 with the intial data SockInitData and protocol specific
715 @param SockInitData Inital data to setting the socket.
717 @return Pointer to the newly created socket. If NULL, error condition occured.
722 IN SOCK_INIT_DATA
*SockInitData
726 Destory the socket Sock and its associated protocol control block.
728 @param Sock The socket to be destroyed.
730 @retval EFI_SUCCESS The socket Sock is destroyed successfully.
731 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
740 Configure the specific socket Sock using configuration data ConfigData.
742 @param Sock Pointer to the socket to be configured.
743 @param ConfigData Pointer to the configuration data.
745 @retval EFI_SUCCESS The socket is configured successfully.
746 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket or the
747 socket is already configured.
757 Initiate a connection establishment process.
759 @param Sock Pointer to the socket to initiate the initate the
761 @param Token Pointer to the token used for the connection
764 @retval EFI_SUCCESS The connection is initialized successfully.
765 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
766 socket is closed, or the socket is not configured to
767 be an active one, or the token is already in one of
769 @retval EFI_NO_MAPPING The IP address configuration operation is not
771 @retval EFI_NOT_STARTED The socket is not configured.
781 Issue a listen token to get an existed connected network instance
782 or wait for a connection if there is none.
784 @param Sock Pointer to the socket to accept connections.
785 @param Token The token to accept a connection.
787 @retval EFI_SUCCESS Either a connection is accpeted or the Token is
788 buffered for further acception.
789 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
790 socket is closed, or the socket is not configured to
791 be a passive one, or the token is already in one of
793 @retval EFI_NO_MAPPING The IP address configuration operation is not
795 @retval EFI_NOT_STARTED The socket is not configured.
796 @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit.
806 Issue a token with data to the socket to send out.
808 @param Sock Pointer to the socket to process the token with
810 @param Token The token with data that needs to send out.
812 @retval EFI_SUCCESS The token is processed successfully.
813 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
814 socket is closed, or the socket is not in a
815 synchronized state , or the token is already in one
816 of this socket's lists.
817 @retval EFI_NO_MAPPING The IP address configuration operation is not
819 @retval EFI_NOT_STARTED The socket is not configured.
820 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
830 Issue a token to get data from the socket.
832 @param Sock Pointer to the socket to get data from.
833 @param Token The token to store the received data from the
836 @retval EFI_SUCCESS The token is processed successfully.
837 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
838 socket is closed, or the socket is not in a
839 synchronized state , or the token is already in one
840 of this socket's lists.
841 @retval EFI_NO_MAPPING The IP address configuration operation is not
843 @retval EFI_NOT_STARTED The socket is not configured.
844 @retval EFI_CONNECTION_FIN The connection is closed and there is no more data.
845 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
855 Reset the socket and its associated protocol control block.
857 @param Sock Pointer to the socket to be flushed.
859 @retval EFI_SUCCESS The socket is flushed successfully.
860 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
869 Close or abort the socket associated connection.
871 @param Sock Pointer to the socket of the connection to close or
873 @param Token The token for close operation.
874 @param OnAbort TRUE for aborting the connection, FALSE to close it.
876 @retval EFI_SUCCESS The close or abort operation is initialized
878 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
879 socket is closed, or the socket is not in a
880 synchronized state , or the token is already in one
881 of this socket's lists.
882 @retval EFI_NO_MAPPING The IP address configuration operation is not
884 @retval EFI_NOT_STARTED The socket is not configured.
895 Get the mode data of the low layer protocol.
897 @param Sock Pointer to the socket to get mode data from.
898 @param Mode Pointer to the data to store the low layer mode
901 @retval EFI_SUCCESS The mode data is got successfully.
902 @retval EFI_NOT_STARTED The socket is not configured.
912 Configure the low level protocol to join a multicast group for
913 this socket's connection.
915 @param Sock Pointer to the socket of the connection to join the
916 specific multicast group.
917 @param GroupInfo Pointer to the multicast group info.
919 @retval EFI_SUCCESS The configuration is done successfully.
920 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
921 @retval EFI_NOT_STARTED The socket is not configured.
931 Add or remove route information in IP route table associated
934 @param Sock Pointer to the socket associated with the IP route
936 @param RouteInfo Pointer to the route information to be processed.
938 @retval EFI_SUCCESS The route table is updated successfully.
939 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
940 @retval EFI_NO_MAPPING The IP address configuration operation is not
942 @retval EFI_NOT_STARTED The socket is not configured.
952 // Supporting function to operate on socket buffer
956 Get the first buffer block in the specific socket buffer.
958 @param Sockbuf Pointer to the socket buffer.
960 @return Pointer to the first buffer in the queue. NULL if the queue is empty.
965 IN SOCK_BUFFER
*Sockbuf
969 Get the next buffer block in the specific socket buffer.
971 @param Sockbuf Pointer to the socket buffer.
972 @param SockEntry Pointer to the buffer block prior to the required
975 @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
976 the tail or head entry.
981 IN SOCK_BUFFER
*Sockbuf
,
982 IN NET_BUF
*SockEntry