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 EFI_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 buffer structure of rcvd data and send data used by socket.
203 typedef struct _SOCK_BUFFER
{
204 UINT32 HighWater
; ///< The buffersize upper limit of sock_buffer
205 UINT32 LowWater
; ///< The low warter mark of sock_buffer
206 NET_BUF_QUEUE
*DataQueue
; ///< The queue to buffer data
210 The handler of protocol for request from socket.
212 @param Socket The socket issuing the request to protocol
213 @param Request The request issued by socket
214 @param RequestData The request related data
216 @retval EFI_SUCCESS The socket request is completed successfully.
217 @retval other The error status returned by the corresponding TCP
223 (*SOCK_PROTO_HANDLER
) (
225 IN SOCK_REQUEST Request
,
231 // Socket provided oprerations for low layer protocol
235 // Socket provided operations for user interface
239 Set the state of the socket.
241 @param Sock Pointer to the socket.
242 @param State The new state to be set.
252 Called by the low layer protocol to indicate the socket a connection is
255 This function just changes the socket's state to SO_CONNECTED
256 and signals the token used for connection establishment.
258 @param Sock Pointer to the socket associated with the
259 established connection.
263 SockConnEstablished (
268 Called by the low layer protocol to indicate the connection is closed.
270 This function flushes the socket, sets the state to SO_CLOSED and signals
273 @param Sock Pointer to the socket associated with the closed
283 Called by low layer protocol to indicate that some data is sent or processed.
285 This function trims the sent data in the socket send buffer, signals the data
288 @param Sock Pointer to the socket.
289 @param Count The length of the data processed or sent, in bytes.
299 Called by the low layer protocol to copy some data in socket send
300 buffer starting from the specific offset to a buffer provided by
303 @param Sock Pointer to the socket.
304 @param Offset The start point of the data to be copied.
305 @param Len The length of the data to be copied.
306 @param Dest Pointer to the destination to copy the data.
308 @return The data size copied.
320 Called by the low layer protocol to indicate that there
321 will be no more data from the communication peer.
323 This function set the socket's state to SO_NO_MORE_DATA and
324 signal all queued IO tokens with the error status EFI_CONNECTION_FIN.
326 @param Sock Pointer to the socket.
335 Called by the low layer protocol to deliver received data to socket layer.
337 This function will append the data to the socket receive buffer, set ther
338 urgent data length and then check if any receive token can be signaled.
340 @param Sock Pointer to the socket.
341 @param NetBuffer Pointer to the buffer that contains the received
343 @param UrgLen The length of the urgent data in the received data.
349 IN NET_BUF
*NetBuffer
,
354 Get the length of the free space of the specific socket buffer.
356 @param Sock Pointer to the socket.
357 @param Which Flag to indicate which socket buffer to check,
358 either send buffer or receive buffer.
360 @return The length of the free space, in bytes.
370 Clone a new socket including its associated protocol control block.
372 @param Sock Pointer to the socket to be cloned.
374 @return Pointer to the newly cloned socket. If NULL, error condition occurred.
383 Signal the receive token with the specific error or
384 set socket error code after error is received.
386 @param Sock Pointer to the socket.
387 @param Error The error code received.
397 /// Proto type of the create callback
401 (*SOCK_CREATE_CALLBACK
) (
407 /// Proto type of the destroy callback
411 (*SOCK_DESTROY_CALLBACK
) (
417 /// The initialize data for create a new socket.
419 typedef struct _SOCK_INIT_DATA
{
423 SOCKET
*Parent
; ///< The parent of this socket
424 UINT32 BackLog
; ///< The connection limit for listening socket
425 UINT32 SndBufferSize
; ///< The high warter mark of send buffer
426 UINT32 RcvBufferSize
; ///< The high warter mark of receive buffer
427 VOID
*Protocol
; ///< The pointer to protocol function template
428 ///< wanted to install on socket
431 // Callbacks after socket is created and before socket is to be destroyed.
433 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
434 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
435 VOID
*Context
; ///< The context of the callback
438 // Opaque protocol data.
443 SOCK_PROTO_HANDLER ProtoHandler
; ///< The handler of protocol for socket request
445 EFI_HANDLE DriverBinding
; ///< The driver binding handle
449 /// The union type of TCP and UDP protocol.
451 typedef union _NET_PROTOCOL
{
452 EFI_TCP4_PROTOCOL TcpProtocol
; ///< Tcp protocol
453 EFI_UDP4_PROTOCOL UdpProtocol
; ///< Udp protocol
457 /// The socket structure representing a network service access point
462 // Socket description information
464 UINT32 Signature
; ///< Signature of the socket
465 EFI_HANDLE SockHandle
; ///< The virtual handle of the socket
466 EFI_HANDLE DriverBinding
; ///< Socket's driver binding protocol
467 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
468 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
470 SOCK_CONFIGURE_STATE ConfigureState
;
474 EFI_LOCK Lock
; ///< The lock of socket
475 SOCK_BUFFER SndBuffer
; ///< Send buffer of application's data
476 SOCK_BUFFER RcvBuffer
; ///< Receive buffer of received data
477 EFI_STATUS SockError
; ///< The error returned by low layer protocol
481 // Fields used to manage the connection request
483 UINT32 BackLog
; ///< the limit of connection to this socket
484 UINT32 ConnCnt
; ///< the current count of connections to it
485 SOCKET
*Parent
; ///< listening parent that accept the connection
486 LIST_ENTRY ConnectionList
; ///< the connections maintained by this socket
489 // The queue to buffer application's asynchronous token
491 LIST_ENTRY ListenTokenList
;
492 LIST_ENTRY RcvTokenList
;
493 LIST_ENTRY SndTokenList
;
494 LIST_ENTRY ProcessingSndTokenList
;
496 SOCK_COMPLETION_TOKEN
*ConnectionToken
; ///< app's token to signal if connected
497 SOCK_COMPLETION_TOKEN
*CloseToken
; ///< app's token to signal if closed
500 // Interface for low level protocol
502 SOCK_PROTO_HANDLER ProtoHandler
; ///< The request handler of protocol
503 UINT8 ProtoReserved
[PROTO_RESERVED_LEN
]; ///< Data fields reserved for protocol
504 NET_PROTOCOL NetProtocol
; ///< TCP or UDP protocol socket used
507 // Callbacks after socket is created and before socket is to be destroyed.
509 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
510 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
511 VOID
*Context
; ///< The context of the callback
515 /// The token structure buffered in socket layer.
517 typedef struct _SOCK_TOKEN
{
518 LIST_ENTRY TokenList
; ///< The entry to add in the token list
519 SOCK_COMPLETION_TOKEN
*Token
; ///< The application's token
520 UINT32 RemainDataLen
; ///< Unprocessed data length
521 SOCKET
*Sock
; ///< The poninter to the socket this token
526 /// Reserved data to access the NET_BUF delivered by UDP driver.
528 typedef struct _UDP_RSV_DATA
{
530 EFI_UDP4_SESSION_DATA Session
;
534 /// Reserved data to access the NET_BUF delivered by TCP driver.
536 typedef struct _TCP_RSV_DATA
{
541 Create a socket and its associated protocol control block
542 with the intial data SockInitData and protocol specific
545 @param SockInitData Inital data to setting the socket.
547 @return Pointer to the newly created socket. If NULL, error condition occured.
552 IN SOCK_INIT_DATA
*SockInitData
556 Destory the socket Sock and its associated protocol control block.
558 @param Sock The socket to be destroyed.
560 @retval EFI_SUCCESS The socket Sock is destroyed successfully.
561 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
570 Configure the specific socket Sock using configuration data ConfigData.
572 @param Sock Pointer to the socket to be configured.
573 @param ConfigData Pointer to the configuration data.
575 @retval EFI_SUCCESS The socket is configured successfully.
576 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket or the
577 socket is already configured.
587 Initiate a connection establishment process.
589 @param Sock Pointer to the socket to initiate the initate the
591 @param Token Pointer to the token used for the connection
594 @retval EFI_SUCCESS The connection is initialized successfully.
595 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
596 socket is closed, or the socket is not configured to
597 be an active one, or the token is already in one of
599 @retval EFI_NO_MAPPING The IP address configuration operation is not
601 @retval EFI_NOT_STARTED The socket is not configured.
611 Issue a listen token to get an existed connected network instance
612 or wait for a connection if there is none.
614 @param Sock Pointer to the socket to accept connections.
615 @param Token The token to accept a connection.
617 @retval EFI_SUCCESS Either a connection is accpeted or the Token is
618 buffered for further acception.
619 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
620 socket is closed, or the socket is not configured to
621 be a passive one, or the token is already in one of
623 @retval EFI_NO_MAPPING The IP address configuration operation is not
625 @retval EFI_NOT_STARTED The socket is not configured.
626 @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit.
636 Issue a token with data to the socket to send out.
638 @param Sock Pointer to the socket to process the token with
640 @param Token The token with data that needs to send out.
642 @retval EFI_SUCCESS The token is processed successfully.
643 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
644 socket is closed, or the socket is not in a
645 synchronized state , or the token is already in one
646 of this socket's lists.
647 @retval EFI_NO_MAPPING The IP address configuration operation is not
649 @retval EFI_NOT_STARTED The socket is not configured.
650 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
660 Issue a token to get data from the socket.
662 @param Sock Pointer to the socket to get data from.
663 @param Token The token to store the received data from the
666 @retval EFI_SUCCESS The token is processed successfully.
667 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
668 socket is closed, or the socket is not in a
669 synchronized state , or the token is already in one
670 of this socket's lists.
671 @retval EFI_NO_MAPPING The IP address configuration operation is not
673 @retval EFI_NOT_STARTED The socket is not configured.
674 @retval EFI_CONNECTION_FIN The connection is closed and there is no more data.
675 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to memory limit.
685 Reset the socket and its associated protocol control block.
687 @param Sock Pointer to the socket to be flushed.
689 @retval EFI_SUCCESS The socket is flushed successfully.
690 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
699 Close or abort the socket associated connection.
701 @param Sock Pointer to the socket of the connection to close or
703 @param Token The token for close operation.
704 @param OnAbort TRUE for aborting the connection, FALSE to close it.
706 @retval EFI_SUCCESS The close or abort operation is initialized
708 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
709 socket is closed, or the socket is not in a
710 synchronized state , or the token is already in one
711 of this socket's lists.
712 @retval EFI_NO_MAPPING The IP address configuration operation is not
714 @retval EFI_NOT_STARTED The socket is not configured.
725 Get the mode data of the low layer protocol.
727 @param Sock Pointer to the socket to get mode data from.
728 @param Mode Pointer to the data to store the low layer mode
731 @retval EFI_SUCCESS The mode data is got successfully.
732 @retval EFI_NOT_STARTED The socket is not configured.
742 Configure the low level protocol to join a multicast group for
743 this socket's connection.
745 @param Sock Pointer to the socket of the connection to join the
746 specific multicast group.
747 @param GroupInfo Pointer to the multicast group info.
749 @retval EFI_SUCCESS The configuration is done successfully.
750 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
751 @retval EFI_NOT_STARTED The socket is not configured.
761 Add or remove route information in IP route table associated
764 @param Sock Pointer to the socket associated with the IP route
766 @param RouteInfo Pointer to the route information to be processed.
768 @retval EFI_SUCCESS The route table is updated successfully.
769 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
770 @retval EFI_NO_MAPPING The IP address configuration operation is not
772 @retval EFI_NOT_STARTED The socket is not configured.
782 // Supporting function to operate on socket buffer
786 Get the first buffer block in the specific socket buffer.
788 @param Sockbuf Pointer to the socket buffer.
790 @return Pointer to the first buffer in the queue. NULL if the queue is empty.
795 IN SOCK_BUFFER
*Sockbuf
799 Get the next buffer block in the specific socket buffer.
801 @param Sockbuf Pointer to the socket buffer.
802 @param SockEntry Pointer to the buffer block prior to the required
805 @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
806 the tail or head entry.
811 IN SOCK_BUFFER
*Sockbuf
,
812 IN NET_BUF
*SockEntry