3 Copyright (c) 2005 - 2006, Intel Corporation<BR>
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php<BR>
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Protocol/Ip4.h>
20 #include <Protocol/Tcp4.h>
21 #include <Protocol/Udp4.h>
23 #include <Library/NetLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/UefiRuntimeServicesTableLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/BaseLib.h>
29 #include <Library/UefiLib.h>
30 #include <Library/MemoryAllocationLib.h>
31 #include <Library/BaseMemoryLib.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
;
88 #define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
90 #define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
92 #define SOCK_IS_CONFIGURED(Sock) \
93 (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
94 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
96 #define SOCK_IS_CONFIGURED_ACTIVE(Sock) \
97 ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
99 #define SOCK_IS_CONNECTED_PASSIVE(Sock) \
100 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
102 #define SOCK_IS_NO_MAPPING(Sock) \
103 ((Sock)->ConfigureState == SO_NO_MAPPING)
105 #define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
107 #define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
109 #define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
111 #define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
113 #define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
115 #define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
117 #define SOCK_SIGNATURE SIGNATURE_32 ('S', 'O', 'C', 'K')
119 #define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
121 #define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
123 #define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
125 #define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
127 #define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
129 #define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
131 #define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
133 #define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
135 #define GET_BACKLOG(Sock) ((Sock)->BackLog)
137 #define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
139 #define SND_BUF_HDR_LEN(Sock) \
140 ((SockBufFirst (&((Sock)->SndBuffer)))->TotalSize)
142 #define RCV_BUF_HDR_LEN(Sock) \
143 ((SockBufFirst (&((Sock)->RcvBuffer)))->TotalSize)
145 #define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
147 #define PROTO_TOKEN_FORM_SOCK(SockToken, Type) \
148 ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
150 typedef struct _SOCKET SOCKET
;
153 /// Socket completion token
155 typedef struct _SOCK_COMPLETION_TOKEN
{
156 EFI_EVENT Event
; ///< The event to be issued
157 EFI_STATUS Status
; ///< The status to be issued
158 } SOCK_COMPLETION_TOKEN
;
161 /// The application token with data packet
163 typedef struct _SOCK_IO_TOKEN
{
164 SOCK_COMPLETION_TOKEN Token
;
172 /// The request issued from socket layer to protocol layer.
175 SOCK_ATTACH
, ///< Attach current socket to a new PCB
176 SOCK_DETACH
, ///< Detach current socket from the PCB
177 SOCK_CONFIGURE
, ///< Configure attached PCB
178 SOCK_FLUSH
, ///< Flush attached PCB
179 SOCK_SND
, ///< Need protocol to send something
180 SOCK_SNDPUSH
, ///< Need protocol to send pushed data
181 SOCK_SNDURG
, ///< Need protocol to send urgent data
182 SOCK_CONSUMED
, ///< Application has retrieved data from socket
183 SOCK_CONNECT
, ///< Need to connect to a peer
184 SOCK_CLOSE
, ///< Need to close the protocol process
185 SOCK_ABORT
, ///< Need to reset the protocol process
186 SOCK_POLL
, ///< Need to poll to the protocol layer
187 SOCK_ROUTE
, ///< Need to add a route information
188 SOCK_MODE
, ///< Need to get the mode data of the protocol
189 SOCK_GROUP
///< Need to join a mcast group
196 SOCK_DGRAM
, ///< This socket providing datagram service
197 SOCK_STREAM
///< This socket providing stream service
201 /// The handler of protocol for request from socket.
205 (*SOCK_PROTO_HANDLER
) (
206 IN SOCKET
*Socket
, ///< The socket issuing the request to protocol
207 IN SOCK_REQUEST Request
, ///< The request issued by socket
208 IN VOID
*RequestData
///< The request related data
212 /// The buffer structure of rcvd data and send data used by socket.
214 typedef struct _SOCK_BUFFER
{
215 UINT32 HighWater
; ///< The buffersize upper limit of sock_buffer
216 UINT32 LowWater
; ///< The low warter mark of sock_buffer
217 NET_BUF_QUEUE
*DataQueue
; ///< The queue to buffer data
222 // Socket provided oprerations for low layer protocol
226 // Socket provided operations for user interface
230 Set the state of the socket.
232 @param Sock Pointer to the socket.
233 @param State The new state to be set.
243 Called by the low layer protocol to indicate the socket a connection is
244 established. This function just changes the socket's state to SO_CONNECTED
245 and signals the token used for connection establishment.
247 @param Sock Pointer to the socket associated with the
248 established connection.
252 SockConnEstablished (
257 Called by the low layer protocol to indicate the connection is closed; This
258 function flushes the socket, sets the state to SO_CLOSED and signals the close
261 @param Sock Pointer to the socket associated with the closed
271 Called by low layer protocol to indicate that some data is sent or processed;
272 This function trims the sent data in the socket send buffer, signals the data
275 @param Sock Pointer to the socket.
276 @param Count The length of the data processed or sent, in bytes.
286 Called by the low layer protocol to copy some data in socket send
287 buffer starting from the specific offset to a buffer provided by
290 @param Sock Pointer to the socket.
291 @param Offset The start point of the data to be copied.
292 @param Len The length of the data to be copied.
293 @param Dest Pointer to the destination to copy the data.
295 @return The data size copied.
307 Called by the low layer protocol to indicate that there
308 will be no more data from the communication peer; This
309 function set the socket's state to SO_NO_MORE_DATA and
310 signal all queued IO tokens with the error status
313 @param Sock Pointer to the socket.
322 Called by the low layer protocol to deliver received data to socket layer;
323 This function will append the data to the socket receive buffer, set ther
324 urgent data length and then check if any receive token can be signaled.
326 @param Sock Pointer to the socket.
327 @param NetBuffer Pointer to the buffer that contains the received
329 @param UrgLen The length of the urgent data in the received data.
335 IN NET_BUF
*NetBuffer
,
340 Get the length of the free space of the specific socket buffer.
342 @param Sock Pointer to the socket.
343 @param Which Flag to indicate which socket buffer to check,
344 either send buffer or receive buffer.
346 @return The length of the free space, in bytes.
356 Clone a new socket including its associated protocol control block.
358 @param Sock Pointer to the socket to be cloned.
360 @return Pointer to the newly cloned socket. If NULL, error condition occurred.
369 Signal the receive token with the specific error or
370 set socket error code after error is received.
372 @param Sock Pointer to the socket.
373 @param Error The error code received.
383 /// Proto type of the create callback
387 (*SOCK_CREATE_CALLBACK
) (
393 /// Proto type of the destroy callback
397 (*SOCK_DESTROY_CALLBACK
) (
403 /// The initialize data for create a new socket.
405 typedef struct _SOCK_INIT_DATA
{
409 SOCKET
*Parent
; ///< The parent of this socket
410 UINT32 BackLog
; ///< The connection limit for listening socket
411 UINT32 SndBufferSize
; ///< The high warter mark of send buffer
412 UINT32 RcvBufferSize
; ///< The high warter mark of receive buffer
413 VOID
*Protocol
; ///< The pointer to protocol function template
414 ///< wanted to install on socket
417 // Callbacks after socket is created and before socket is to be destroyed.
419 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
420 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
421 VOID
*Context
; ///< The context of the callback
424 // Opaque protocol data.
429 SOCK_PROTO_HANDLER ProtoHandler
; ///< The handler of protocol for socket request
431 EFI_HANDLE DriverBinding
; ///< The driver binding handle
435 /// The union type of TCP and UDP protocol.
437 typedef union _NET_PROTOCOL
{
438 EFI_TCP4_PROTOCOL TcpProtocol
; ///< Tcp protocol
439 EFI_UDP4_PROTOCOL UdpProtocol
; ///< Udp protocol
443 /// The socket structure representing a network service access point
448 // Socket description information
450 UINT32 Signature
; ///< Signature of the socket
451 EFI_HANDLE SockHandle
; ///< The virtual handle of the socket
452 EFI_HANDLE DriverBinding
; ///< Socket's driver binding protocol
453 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
454 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
456 SOCK_CONFIGURE_STATE ConfigureState
;
460 EFI_LOCK Lock
; ///< The lock of socket
461 SOCK_BUFFER SndBuffer
; ///< Send buffer of application's data
462 SOCK_BUFFER RcvBuffer
; ///< Receive buffer of received data
463 EFI_STATUS SockError
; ///< The error returned by low layer protocol
467 // Fields used to manage the connection request
469 UINT32 BackLog
; ///< the limit of connection to this socket
470 UINT32 ConnCnt
; ///< the current count of connections to it
471 SOCKET
*Parent
; ///< listening parent that accept the connection
472 LIST_ENTRY ConnectionList
; ///< the connections maintained by this socket
475 // The queue to buffer application's asynchronous token
477 LIST_ENTRY ListenTokenList
;
478 LIST_ENTRY RcvTokenList
;
479 LIST_ENTRY SndTokenList
;
480 LIST_ENTRY ProcessingSndTokenList
;
482 SOCK_COMPLETION_TOKEN
*ConnectionToken
; ///< app's token to signal if connected
483 SOCK_COMPLETION_TOKEN
*CloseToken
; ///< app's token to signal if closed
486 // Interface for low level protocol
488 SOCK_PROTO_HANDLER ProtoHandler
; ///< The request handler of protocol
489 UINT8 ProtoReserved
[PROTO_RESERVED_LEN
]; ///< Data fields reserved for protocol
490 NET_PROTOCOL NetProtocol
; ///< TCP or UDP protocol socket used
493 // Callbacks after socket is created and before socket is to be destroyed.
495 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
496 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
497 VOID
*Context
; ///< The context of the callback
501 /// The token structure buffered in socket layer.
503 typedef struct _SOCK_TOKEN
{
504 LIST_ENTRY TokenList
; ///< The entry to add in the token list
505 SOCK_COMPLETION_TOKEN
*Token
; ///< The application's token
506 UINT32 RemainDataLen
; ///< Unprocessed data length
507 SOCKET
*Sock
; ///< The poninter to the socket this token
512 /// Reserved data to access the NET_BUF delivered by UDP driver.
514 typedef struct _UDP_RSV_DATA
{
516 EFI_UDP4_SESSION_DATA Session
;
520 /// Reserved data to access the NET_BUF delivered by TCP driver.
522 typedef struct _TCP_RSV_DATA
{
527 Create a socket and its associated protocol control block
528 with the intial data SockInitData and protocol specific
531 @param SockInitData Inital data to setting the socket.
533 @return Pointer to the newly created socket. If NULL, error condition occured.
538 IN SOCK_INIT_DATA
*SockInitData
542 Destory the socket Sock and its associated protocol control block.
544 @param Sock The socket to be destroyed.
546 @retval EFI_SUCCESS The socket Sock is destroyed successfully.
547 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
556 Configure the specific socket Sock using configuration data ConfigData.
558 @param Sock Pointer to the socket to be configured.
559 @param ConfigData Pointer to the configuration data.
561 @retval EFI_SUCCESS The socket is configured successfully.
562 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket or the
563 socket is already configured.
573 Initiate a connection establishment process.
575 @param Sock Pointer to the socket to initiate the initate the
577 @param Token Pointer to the token used for the connection
580 @retval EFI_SUCCESS The connection is initialized successfully.
581 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
582 socket is closed, or the socket is not configured to
583 be an active one, or the token is already in one of
585 @retval EFI_NO_MAPPING The IP address configuration operation is not
587 @retval EFI_NOT_STARTED The socket is not configured.
597 Issue a listen token to get an existed connected network instance
598 or wait for a connection if there is none.
600 @param Sock Pointer to the socket to accept connections.
601 @param Token The token to accept a connection.
603 @retval EFI_SUCCESS Either a connection is accpeted or the Token is
604 buffered for further acception.
605 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
606 socket is closed, or the socket is not configured to
607 be a passive one, or the token is already in one of
609 @retval EFI_NO_MAPPING The IP address configuration operation is not
611 @retval EFI_NOT_STARTED The socket is not configured.
612 @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit.
622 Issue a token with data to the socket to send out.
624 @param Sock Pointer to the socket to process the token with
626 @param Token The token with data that needs to send out.
628 @retval EFI_SUCCESS The token is processed successfully.
629 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
630 socket is closed, or the socket is not in a
631 synchronized state , or the token is already in one
632 of this socket's lists.
633 @retval EFI_NO_MAPPING The IP address configuration operation is not
635 @retval EFI_NOT_STARTED The socket is not configured.
636 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
646 Issue a token to get data from the socket.
648 @param Sock Pointer to the socket to get data from.
649 @param Token The token to store the received data from the
652 @retval EFI_SUCCESS The token is processed successfully.
653 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
654 socket is closed, or the socket is not in a
655 synchronized state , or the token is already in one
656 of this socket's lists.
657 @retval EFI_NO_MAPPING The IP address configuration operation is not
659 @retval EFI_NOT_STARTED The socket is not configured.
660 @retval EFI_CONNECTION_FIN The connection is closed and there is no more data.
661 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
671 Reset the socket and its associated protocol control block.
673 @param Sock Pointer to the socket to be flushed.
675 @retval EFI_SUCCESS The socket is flushed successfully.
676 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
685 Close or abort the socket associated connection.
687 @param Sock Pointer to the socket of the connection to close or
689 @param Token The token for close operation.
690 @param OnAbort TRUE for aborting the connection, FALSE to close it.
692 @retval EFI_SUCCESS The close or abort operation is initialized
694 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
695 socket is closed, or the socket is not in a
696 synchronized state , or the token is already in one
697 of this socket's lists.
698 @retval EFI_NO_MAPPING The IP address configuration operation is not
700 @retval EFI_NOT_STARTED The socket is not configured.
711 Get the mode data of the low layer protocol.
713 @param Sock Pointer to the socket to get mode data from.
714 @param Mode Pointer to the data to store the low layer mode
717 @retval EFI_SUCCESS The mode data is got successfully.
718 @retval EFI_NOT_STARTED The socket is not configured.
728 Configure the low level protocol to join a multicast group for
729 this socket's connection.
731 @param Sock Pointer to the socket of the connection to join the
732 specific multicast group.
733 @param GroupInfo Pointer to the multicast group info.
735 @retval EFI_SUCCESS The configuration is done successfully.
736 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
737 @retval EFI_NOT_STARTED The socket is not configured.
747 Add or remove route information in IP route table associated
750 @param Sock Pointer to the socket associated with the IP route
752 @param RouteInfo Pointer to the route information to be processed.
754 @retval EFI_SUCCESS The route table is updated successfully.
755 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
756 @retval EFI_NO_MAPPING The IP address configuration operation is not
758 @retval EFI_NOT_STARTED The socket is not configured.
768 // Supporting function to operate on socket buffer
772 Get the first buffer block in the specific socket buffer.
774 @param Sockbuf Pointer to the socket buffer.
776 @return Pointer to the first buffer in the queue. NULL if the queue is empty.
781 IN SOCK_BUFFER
*Sockbuf
785 Get the next buffer block in the specific socket buffer.
787 @param Sockbuf Pointer to the socket buffer.
788 @param SockEntry Pointer to the buffer block prior to the required
791 @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
792 the tail or head entry.
797 IN SOCK_BUFFER
*Sockbuf
,
798 IN NET_BUF
*SockEntry