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/UefiBootServicesTableLib.h>
27 #include <Library/UefiLib.h>
29 #define SOCK_SND_BUF 0
30 #define SOCK_RCV_BUF 1
32 #define SOCK_BUFF_LOW_WATER (2 * 1024)
33 #define SOCK_RCV_BUFF_SIZE (8 * 1024)
34 #define SOCK_SND_BUFF_SIZE (8 * 1024)
35 #define SOCK_BACKLOG 5
37 #define PROTO_RESERVED_LEN 20
39 #define SO_NO_MORE_DATA 0x0001
44 // When a socket is created it enters into SO_UNCONFIGURED,
45 // no actions can be taken on this socket, only after calling
46 // SockConfigure. The state transition diagram of socket is
49 // SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING
51 // | ---> SO_LISTENING |
53 // |------------------SO_DISCONNECTING<-- SO_CONNECTED
55 // A passive socket can only go into SO_LISTENING and
56 // SO_UNCONFIGURED state. SO_XXXING state is a middle state
57 // when a socket is undergoing a protocol procedure such
58 // as requesting a TCP connection.
75 /// Socket configure state
80 SO_CONFIGURED_PASSIVE
,
82 } SOCK_CONFIGURE_STATE
;
84 #define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
86 #define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
88 #define SOCK_IS_CONFIGURED(Sock) \
89 (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
90 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
92 #define SOCK_IS_CONFIGURED_ACTIVE(Sock) \
93 ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
95 #define SOCK_IS_CONNECTED_PASSIVE(Sock) \
96 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
98 #define SOCK_IS_NO_MAPPING(Sock) \
99 ((Sock)->ConfigureState == SO_NO_MAPPING)
101 #define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
103 #define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
105 #define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
107 #define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
109 #define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
111 #define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
113 #define SOCK_SIGNATURE SIGNATURE_32 ('S', 'O', 'C', 'K')
115 #define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
117 #define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
119 #define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
121 #define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
123 #define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
125 #define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
127 #define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
129 #define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
131 #define GET_BACKLOG(Sock) ((Sock)->BackLog)
133 #define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
135 #define SND_BUF_HDR_LEN(Sock) \
136 ((SockBufFirst (&((Sock)->SndBuffer)))->TotalSize)
138 #define RCV_BUF_HDR_LEN(Sock) \
139 ((SockBufFirst (&((Sock)->RcvBuffer)))->TotalSize)
141 #define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
143 #define PROTO_TOKEN_FORM_SOCK(SockToken, Type) \
144 ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
146 typedef struct _SOCKET SOCKET
;
149 /// Socket completion token
151 typedef struct _SOCK_COMPLETION_TOKEN
{
152 EFI_EVENT Event
; ///< The event to be issued
153 EFI_STATUS Status
; ///< The status to be issued
154 } SOCK_COMPLETION_TOKEN
;
157 /// The application token with data packet
159 typedef struct _SOCK_IO_TOKEN
{
160 SOCK_COMPLETION_TOKEN Token
;
168 /// The request issued from socket layer to protocol layer.
171 SOCK_ATTACH
, ///< Attach current socket to a new PCB
172 SOCK_DETACH
, ///< Detach current socket from the PCB
173 SOCK_CONFIGURE
, ///< Configure attached PCB
174 SOCK_FLUSH
, ///< Flush attached PCB
175 SOCK_SND
, ///< Need protocol to send something
176 SOCK_SNDPUSH
, ///< Need protocol to send pushed data
177 SOCK_SNDURG
, ///< Need protocol to send urgent data
178 SOCK_CONSUMED
, ///< Application has retrieved data from socket
179 SOCK_CONNECT
, ///< Need to connect to a peer
180 SOCK_CLOSE
, ///< Need to close the protocol process
181 SOCK_ABORT
, ///< Need to reset the protocol process
182 SOCK_POLL
, ///< Need to poll to the protocol layer
183 SOCK_ROUTE
, ///< Need to add a route information
184 SOCK_MODE
, ///< Need to get the mode data of the protocol
185 SOCK_GROUP
///< Need to join a mcast group
192 SOCK_DGRAM
, ///< This socket providing datagram service
193 SOCK_STREAM
///< This socket providing stream service
197 /// The buffer structure of rcvd data and send data used by socket.
199 typedef struct _SOCK_BUFFER
{
200 UINT32 HighWater
; ///< The buffersize upper limit of sock_buffer
201 UINT32 LowWater
; ///< The low warter mark of sock_buffer
202 NET_BUF_QUEUE
*DataQueue
; ///< The queue to buffer data
206 The handler of protocol for request from socket.
208 @param Socket The socket issuing the request to protocol
209 @param Request The request issued by socket
210 @param RequestData The request related data
212 @retval EFI_SUCCESS The socket request is completed successfully.
213 @retval other The error status returned by the corresponding TCP
219 (*SOCK_PROTO_HANDLER
) (
221 IN SOCK_REQUEST Request
,
227 // Socket provided oprerations for low layer protocol
231 // Socket provided operations for user interface
235 Set the state of the socket.
237 @param Sock Pointer to the socket.
238 @param State The new state to be set.
248 Called by the low layer protocol to indicate the socket a connection is
251 This function just changes the socket's state to SO_CONNECTED
252 and signals the token used for connection establishment.
254 @param Sock Pointer to the socket associated with the
255 established connection.
259 SockConnEstablished (
264 Called by the low layer protocol to indicate the connection is closed.
266 This function flushes the socket, sets the state to SO_CLOSED and signals
269 @param Sock Pointer to the socket associated with the closed
279 Called by low layer protocol to indicate that some data is sent or processed.
281 This function trims the sent data in the socket send buffer, signals the data
284 @param Sock Pointer to the socket.
285 @param Count The length of the data processed or sent, in bytes.
295 Called by the low layer protocol to copy some data in socket send
296 buffer starting from the specific offset to a buffer provided by
299 @param Sock Pointer to the socket.
300 @param Offset The start point of the data to be copied.
301 @param Len The length of the data to be copied.
302 @param Dest Pointer to the destination to copy the data.
304 @return The data size copied.
316 Called by the low layer protocol to indicate that there
317 will be no more data from the communication peer.
319 This function set the socket's state to SO_NO_MORE_DATA and
320 signal all queued IO tokens with the error status EFI_CONNECTION_FIN.
322 @param Sock Pointer to the socket.
331 Called by the low layer protocol to deliver received data to socket layer.
333 This function will append the data to the socket receive buffer, set ther
334 urgent data length and then check if any receive token can be signaled.
336 @param Sock Pointer to the socket.
337 @param NetBuffer Pointer to the buffer that contains the received
339 @param UrgLen The length of the urgent data in the received data.
345 IN OUT NET_BUF
*NetBuffer
,
350 Get the length of the free space of the specific socket buffer.
352 @param Sock Pointer to the socket.
353 @param Which Flag to indicate which socket buffer to check,
354 either send buffer or receive buffer.
356 @return The length of the free space, in bytes.
366 Clone a new socket including its associated protocol control block.
368 @param Sock Pointer to the socket to be cloned.
370 @return Pointer to the newly cloned socket. If NULL, error condition occurred.
379 Signal the receive token with the specific error or
380 set socket error code after error is received.
382 @param Sock Pointer to the socket.
383 @param Error The error code received.
393 /// Proto type of the create callback
397 (*SOCK_CREATE_CALLBACK
) (
403 /// Proto type of the destroy callback
407 (*SOCK_DESTROY_CALLBACK
) (
413 /// The initialize data for create a new socket.
415 typedef struct _SOCK_INIT_DATA
{
419 SOCKET
*Parent
; ///< The parent of this socket
420 UINT32 BackLog
; ///< The connection limit for listening socket
421 UINT32 SndBufferSize
; ///< The high warter mark of send buffer
422 UINT32 RcvBufferSize
; ///< The high warter mark of receive buffer
423 VOID
*Protocol
; ///< The pointer to protocol function template
424 ///< wanted to install on socket
427 // Callbacks after socket is created and before socket is to be destroyed.
429 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
430 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
431 VOID
*Context
; ///< The context of the callback
434 // Opaque protocol data.
439 SOCK_PROTO_HANDLER ProtoHandler
; ///< The handler of protocol for socket request
441 EFI_HANDLE DriverBinding
; ///< The driver binding handle
445 /// The union type of TCP and UDP protocol.
447 typedef union _NET_PROTOCOL
{
448 EFI_TCP4_PROTOCOL TcpProtocol
; ///< Tcp protocol
449 EFI_UDP4_PROTOCOL UdpProtocol
; ///< Udp protocol
453 /// The socket structure representing a network service access point
458 // Socket description information
460 UINT32 Signature
; ///< Signature of the socket
461 EFI_HANDLE SockHandle
; ///< The virtual handle of the socket
462 EFI_HANDLE DriverBinding
; ///< Socket's driver binding protocol
463 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
464 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
466 SOCK_CONFIGURE_STATE ConfigureState
;
470 EFI_LOCK Lock
; ///< The lock of socket
471 SOCK_BUFFER SndBuffer
; ///< Send buffer of application's data
472 SOCK_BUFFER RcvBuffer
; ///< Receive buffer of received data
473 EFI_STATUS SockError
; ///< The error returned by low layer protocol
477 // Fields used to manage the connection request
479 UINT32 BackLog
; ///< the limit of connection to this socket
480 UINT32 ConnCnt
; ///< the current count of connections to it
481 SOCKET
*Parent
; ///< listening parent that accept the connection
482 LIST_ENTRY ConnectionList
; ///< the connections maintained by this socket
485 // The queue to buffer application's asynchronous token
487 LIST_ENTRY ListenTokenList
;
488 LIST_ENTRY RcvTokenList
;
489 LIST_ENTRY SndTokenList
;
490 LIST_ENTRY ProcessingSndTokenList
;
492 SOCK_COMPLETION_TOKEN
*ConnectionToken
; ///< app's token to signal if connected
493 SOCK_COMPLETION_TOKEN
*CloseToken
; ///< app's token to signal if closed
496 // Interface for low level protocol
498 SOCK_PROTO_HANDLER ProtoHandler
; ///< The request handler of protocol
499 UINT8 ProtoReserved
[PROTO_RESERVED_LEN
]; ///< Data fields reserved for protocol
500 NET_PROTOCOL NetProtocol
; ///< TCP or UDP protocol socket used
503 // Callbacks after socket is created and before socket is to be destroyed.
505 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
506 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
507 VOID
*Context
; ///< The context of the callback
511 /// The token structure buffered in socket layer.
513 typedef struct _SOCK_TOKEN
{
514 LIST_ENTRY TokenList
; ///< The entry to add in the token list
515 SOCK_COMPLETION_TOKEN
*Token
; ///< The application's token
516 UINT32 RemainDataLen
; ///< Unprocessed data length
517 SOCKET
*Sock
; ///< The poninter to the socket this token
522 /// Reserved data to access the NET_BUF delivered by UDP driver.
524 typedef struct _UDP_RSV_DATA
{
526 EFI_UDP4_SESSION_DATA Session
;
530 /// Reserved data to access the NET_BUF delivered by TCP driver.
532 typedef struct _TCP_RSV_DATA
{
537 Create a socket and its associated protocol control block
538 with the intial data SockInitData and protocol specific
541 @param SockInitData Inital data to setting the socket.
543 @return Pointer to the newly created socket. If NULL, error condition occured.
548 IN SOCK_INIT_DATA
*SockInitData
552 Destory the socket Sock and its associated protocol control block.
554 @param Sock The socket to be destroyed.
556 @retval EFI_SUCCESS The socket Sock is destroyed successfully.
557 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
566 Configure the specific socket Sock using configuration data ConfigData.
568 @param Sock Pointer to the socket to be configured.
569 @param ConfigData Pointer to the configuration data.
571 @retval EFI_SUCCESS The socket is configured successfully.
572 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket or the
573 socket is already configured.
583 Initiate a connection establishment process.
585 @param Sock Pointer to the socket to initiate the initate the
587 @param Token Pointer to the token used for the connection
590 @retval EFI_SUCCESS The connection is initialized successfully.
591 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
592 socket is closed, or the socket is not configured to
593 be an active one, or the token is already in one of
595 @retval EFI_NO_MAPPING The IP address configuration operation is not
597 @retval EFI_NOT_STARTED The socket is not configured.
607 Issue a listen token to get an existed connected network instance
608 or wait for a connection if there is none.
610 @param Sock Pointer to the socket to accept connections.
611 @param Token The token to accept a connection.
613 @retval EFI_SUCCESS Either a connection is accpeted or the Token is
614 buffered for further acception.
615 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
616 socket is closed, or the socket is not configured to
617 be a passive one, or the token is already in one of
619 @retval EFI_NO_MAPPING The IP address configuration operation is not
621 @retval EFI_NOT_STARTED The socket is not configured.
622 @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit.
632 Issue a token with data to the socket to send out.
634 @param Sock Pointer to the socket to process the token with
636 @param Token The token with data that needs to send out.
638 @retval EFI_SUCCESS The token is processed successfully.
639 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
640 socket is closed, or the socket is not in a
641 synchronized state , or the token is already in one
642 of this socket's lists.
643 @retval EFI_NO_MAPPING The IP address configuration operation is not
645 @retval EFI_NOT_STARTED The socket is not configured.
646 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
656 Issue a token to get data from the socket.
658 @param Sock Pointer to the socket to get data from.
659 @param Token The token to store the received data from the
662 @retval EFI_SUCCESS The token is processed successfully.
663 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
664 socket is closed, or the socket is not in a
665 synchronized state , or the token is already in one
666 of this socket's lists.
667 @retval EFI_NO_MAPPING The IP address configuration operation is not
669 @retval EFI_NOT_STARTED The socket is not configured.
670 @retval EFI_CONNECTION_FIN The connection is closed and there is no more data.
671 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
681 Reset the socket and its associated protocol control block.
683 @param Sock Pointer to the socket to be flushed.
685 @retval EFI_SUCCESS The socket is flushed successfully.
686 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
695 Close or abort the socket associated connection.
697 @param Sock Pointer to the socket of the connection to close or
699 @param Token The token for close operation.
700 @param OnAbort TRUE for aborting the connection, FALSE to close it.
702 @retval EFI_SUCCESS The close or abort operation is initialized
704 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
705 socket is closed, or the socket is not in a
706 synchronized state , or the token is already in one
707 of this socket's lists.
708 @retval EFI_NO_MAPPING The IP address configuration operation is not
710 @retval EFI_NOT_STARTED The socket is not configured.
721 Get the mode data of the low layer protocol.
723 @param Sock Pointer to the socket to get mode data from.
724 @param Mode Pointer to the data to store the low layer mode
727 @retval EFI_SUCCESS The mode data is got successfully.
728 @retval EFI_NOT_STARTED The socket is not configured.
738 Configure the low level protocol to join a multicast group for
739 this socket's connection.
741 @param Sock Pointer to the socket of the connection to join the
742 specific multicast group.
743 @param GroupInfo Pointer to the multicast group info.
745 @retval EFI_SUCCESS The configuration is done successfully.
746 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
747 @retval EFI_NOT_STARTED The socket is not configured.
757 Add or remove route information in IP route table associated
760 @param Sock Pointer to the socket associated with the IP route
762 @param RouteInfo Pointer to the route information to be processed.
764 @retval EFI_SUCCESS The route table is updated successfully.
765 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
766 @retval EFI_NO_MAPPING The IP address configuration operation is not
768 @retval EFI_NOT_STARTED The socket is not configured.
778 // Supporting function to operate on socket buffer
782 Get the first buffer block in the specific socket buffer.
784 @param Sockbuf Pointer to the socket buffer.
786 @return Pointer to the first buffer in the queue. NULL if the queue is empty.
791 IN SOCK_BUFFER
*Sockbuf
795 Get the next buffer block in the specific socket buffer.
797 @param Sockbuf Pointer to the socket buffer.
798 @param SockEntry Pointer to the buffer block prior to the required
801 @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
802 the tail or head entry.
807 IN SOCK_BUFFER
*Sockbuf
,
808 IN NET_BUF
*SockEntry