2 Common head file for TCP socket.
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Protocol/Tcp4.h>
22 #include <Protocol/Tcp6.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/UefiLib.h>
31 #include <Library/DpcLib.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.
71 #define SO_LISTENING 1
72 #define SO_CONNECTING 2
73 #define SO_CONNECTED 3
74 #define SO_DISCONNECTING 4
77 /// Socket configure state
79 #define SO_UNCONFIGURED 0
80 #define SO_CONFIGURED_ACTIVE 1
81 #define SO_CONFIGURED_PASSIVE 2
82 #define SO_NO_MAPPING 3
85 /// The request issued from socket layer to protocol layer.
87 #define SOCK_ATTACH 0 ///< Attach current socket to a new PCB
88 #define SOCK_DETACH 1 ///< Detach current socket from the PCB
89 #define SOCK_CONFIGURE 2 ///< Configure attached PCB
90 #define SOCK_FLUSH 3 ///< Flush attached PCB
91 #define SOCK_SND 4 ///< Need protocol to send something
92 #define SOCK_SNDPUSH 5 ///< Need protocol to send pushed data
93 #define SOCK_SNDURG 6 ///< Need protocol to send urgent data
94 #define SOCK_CONSUMED 7 ///< Application has retrieved data from socket
95 #define SOCK_CONNECT 8 ///< Need to connect to a peer
96 #define SOCK_CLOSE 9 ///< Need to close the protocol process
97 #define SOCK_ABORT 10 ///< Need to reset the protocol process
98 #define SOCK_POLL 11 ///< Need to poll to the protocol layer
99 #define SOCK_ROUTE 12 ///< Need to add a route information
100 #define SOCK_MODE 13 ///< Need to get the mode data of the protocol
101 #define SOCK_GROUP 14 ///< Need to join a mcast group
104 Set socket SO_NO_MORE_DATA flag.
106 @param[in] Sock Pointer to the socket
109 #define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
112 Check whether the socket is unconfigured.
114 @param[in] Sock Pointer to the socket.
116 @retval TRUE The socket is unconfigued.
117 @retval FALSE The socket is not unconfigued.
120 #define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
123 Check whether the socket is configured.
125 @param[in] Sock Pointer to the socket
127 @retval TRUE The socket is configued
128 @retval FALSE The socket is not configued
131 #define SOCK_IS_CONFIGURED(Sock) \
132 (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
133 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
136 Check whether the socket is configured to active mode.
138 @param[in] Sock Pointer to the socket.
140 @retval TRUE The socket is configued to active mode.
141 @retval FALSE The socket is not configued to active mode.
144 #define SOCK_IS_CONFIGURED_ACTIVE(Sock) ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
147 Check whether the socket is configured to passive mode.
149 @param[in] Sock Pointer to the socket.
151 @retval TRUE The socket is configued to passive mode.
152 @retval FALSE The socket is not configued to passive mode.
155 #define SOCK_IS_CONNECTED_PASSIVE(Sock) ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
158 Check whether the socket is mapped.
160 @param[in] Sock Pointer to the socket.
162 @retval TRUE The socket is not mapping.
163 @retval FALSE The socket is mapped.
166 #define SOCK_IS_NO_MAPPING(Sock) ((Sock)->ConfigureState == SO_NO_MAPPING)
169 Check whether the socket is closed.
171 @param[in] Sock Pointer to the socket.
173 @retval TRUE The socket is closed.
174 @retval FALSE The socket is not closed.
177 #define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
180 Check whether the socket is listening.
182 @param[in] Sock Pointer to the socket.
184 @retval TRUE The socket is listening.
185 @retval FALSE The socket is not listening.
188 #define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
191 Check whether the socket is connecting.
193 @param[in] Sock Pointer to the socket.
195 @retval TRUE The socket is connecting.
196 @retval FALSE The socket is not connecting.
199 #define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
202 Check whether the socket has connected.
204 @param[in] Sock Pointer to the socket.
206 @retval TRUE The socket has connected.
207 @retval FALSE The socket has not connected.
210 #define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
213 Check whether the socket is disconnecting.
215 @param[in] Sock Pointer to the socket.
217 @retval TRUE The socket is disconnecting.
218 @retval FALSE The socket is not disconnecting.
221 #define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
224 Check whether the socket is no more data.
226 @param[in] Sock Pointer to the socket.
228 @retval TRUE The socket is no more data.
229 @retval FALSE The socket still has data.
232 #define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
235 Set the size of the receive buffer.
237 @param[in] Sock Pointer to the socket.
238 @param[in] Size The size to set.
241 #define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
244 Get the size of the receive buffer.
246 @param[in] Sock Pointer to the socket.
248 @return The receive buffer size.
251 #define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
254 Get the size of the receive data.
256 @param[in] Sock Pointer to the socket.
258 @return The received data size.
261 #define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
264 Set the size of the send buffer.
266 @param[in] Sock Pointer to the socket.
267 @param[in] Size The size to set.
270 #define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
273 Get the size of the send buffer.
275 @param[in] Sock Pointer to the socket.
277 @return The send buffer size.
280 #define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
283 Get the size of the send data.
285 @param[in] Sock Pointer to the socket.
287 @return The send data size.
290 #define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
293 Set the backlog value of the socket.
295 @param[in] Sock Pointer to the socket.
296 @param[in] Value The value to set.
299 #define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
302 Get the backlog value of the socket.
304 @param[in] Sock Pointer to the socket.
306 @return The backlog value.
309 #define GET_BACKLOG(Sock) ((Sock)->BackLog)
312 Set the socket with error state.
314 @param[in] Sock Pointer to the socket.
315 @param[in] Error The error state.
318 #define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
320 #define SOCK_SIGNATURE SIGNATURE_32 ('S', 'O', 'C', 'K')
322 #define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
324 #define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
326 #define PROTO_TOKEN_FORM_SOCK(SockToken, Type) ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
328 typedef struct _TCP_SOCKET SOCKET
;
331 /// Socket completion token
333 typedef struct _SOCK_COMPLETION_TOKEN
{
334 EFI_EVENT Event
; ///< The event to be issued
335 EFI_STATUS Status
; ///< The status to be issued
336 } SOCK_COMPLETION_TOKEN
;
344 /// The application token with data packet
346 typedef struct _SOCK_IO_TOKEN
{
347 SOCK_COMPLETION_TOKEN Token
;
355 SockDgram
, ///< This socket providing datagram service
356 SockStream
///< This socket providing stream service
360 /// The buffer structure of rcvd data and send data used by socket.
362 typedef struct _SOCK_BUFFER
{
363 UINT32 HighWater
; ///< The buffersize upper limit of sock_buffer
364 UINT32 LowWater
; ///< The low warter mark of sock_buffer
365 NET_BUF_QUEUE
*DataQueue
; ///< The queue to buffer data
369 The handler of protocol for request from socket.
371 @param[in] Socket The socket issuing the request to protocol.
372 @param[in] Request The request issued by socket.
373 @param[in] RequestData The request related data.
375 @retval EFI_SUCCESS The socket request is completed successfully.
376 @retval other The error status returned by the corresponding TCP
382 (*SOCK_PROTO_HANDLER
) (
389 The Callback funtion called after the TCP socket is created.
391 @param[in] This Pointer to the socket just created.
392 @param[in] Context Context of the socket.
394 @retval EFI_SUCCESS This protocol installed successfully.
395 @retval other Some error occured.
400 (*SOCK_CREATE_CALLBACK
) (
406 The callback function called before the TCP socket is to be destroyed.
408 @param[in] This The TCP socket to be destroyed.
409 @param[in] Context The context.
414 (*SOCK_DESTROY_CALLBACK
) (
420 /// The initialize data for create a new socket.
422 typedef struct _SOCK_INIT_DATA
{
426 SOCKET
*Parent
; ///< The parent of this socket
427 UINT32 BackLog
; ///< The connection limit for listening socket
428 UINT32 SndBufferSize
; ///< The high warter mark of send buffer
429 UINT32 RcvBufferSize
; ///< The high warter mark of receive buffer
431 VOID
*Protocol
; ///< The pointer to protocol function template
432 ///< wanted to install on socket
435 // Callbacks after socket is created and before socket is to be destroyed.
437 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
438 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
439 VOID
*Context
; ///< The context of the callback
442 // Opaque protocol data.
447 SOCK_PROTO_HANDLER ProtoHandler
; ///< The handler of protocol for socket request
449 EFI_HANDLE DriverBinding
; ///< The driver binding handle
453 /// The union type of TCP and UDP protocol.
455 typedef union _NET_PROTOCOL
{
456 EFI_TCP4_PROTOCOL Tcp4Protocol
; ///< Tcp4 protocol
457 EFI_TCP6_PROTOCOL Tcp6Protocol
; ///< Tcp6 protocol
460 /// The socket structure representing a network service access point.
464 // Socket description information
466 UINT32 Signature
; ///< Signature of the socket
467 EFI_HANDLE SockHandle
; ///< The virtual handle of the socket
468 EFI_HANDLE DriverBinding
; ///< Socket's driver binding protocol
469 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
470 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
472 UINT8 ConfigureState
;
476 EFI_LOCK Lock
; ///< The lock of socket
477 SOCK_BUFFER SndBuffer
; ///< Send buffer of application's data
478 SOCK_BUFFER RcvBuffer
; ///< Receive buffer of received data
479 EFI_STATUS SockError
; ///< The error returned by low layer protocol
483 // Fields used to manage the connection request
485 UINT32 BackLog
; ///< the limit of connection to this socket
486 UINT32 ConnCnt
; ///< the current count of connections to it
487 SOCKET
*Parent
; ///< listening parent that accept the connection
488 LIST_ENTRY ConnectionList
; ///< the connections maintained by this socket
490 // The queue to buffer application's asynchronous token
492 LIST_ENTRY ListenTokenList
;
493 LIST_ENTRY RcvTokenList
;
494 LIST_ENTRY SndTokenList
;
495 LIST_ENTRY ProcessingSndTokenList
;
497 SOCK_COMPLETION_TOKEN
*ConnectionToken
; ///< app's token to signal if connected
498 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
505 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 TCP driver.
528 typedef struct _TCP_RSV_DATA
{
533 // Socket provided oprerations for low layer protocol implemented in SockImpl.c
537 Set the state of the socket.
539 @param[in, out] Sock Pointer to the socket.
540 @param[in] State The new socket state to be set.
550 Clone a new socket including its associated protocol control block.
552 @param[in] Sock Pointer to the socket to be cloned.
554 @return Pointer to the newly cloned socket. If NULL, an error condition occurred.
563 Called by the low layer protocol to indicate the socket a connection is
566 This function just changes the socket's state to SO_CONNECTED
567 and signals the token used for connection establishment.
569 @param[in, out] Sock Pointer to the socket associated with the
570 established connection.
574 SockConnEstablished (
579 Called by the low layer protocol to indicate that the connection is closed.
581 This function flushes the socket, sets the state to SO_CLOSED, and signals
584 @param[in, out] Sock Pointer to the socket associated with the closed
594 Called by low layer protocol to indicate that some data is sent or processed.
596 This function trims the sent data in the socket send buffer and signals the data
599 @param[in, out] Sock Pointer to the socket.
600 @param[in] Count The length of the data processed or sent, in bytes.
610 Called by the low layer protocol to copy some data in socket send
611 buffer starting from the specific offset to a buffer provided by
614 @param[in] Sock Pointer to the socket.
615 @param[in] Offset The start point of the data to be copied.
616 @param[in] Len The length of the data to be copied.
617 @param[out] Dest Pointer to the destination to copy the data.
619 @return The data size copied.
631 Called by the low layer protocol to deliver received data to socket layer.
633 This function appends the data to the socket receive buffer, set the
634 urgent data length, then checks if any receive token can be signaled.
636 @param[in, out] Sock Pointer to the socket.
637 @param[in, out] NetBuffer Pointer to the buffer that contains the received data.
638 @param[in] UrgLen The length of the urgent data in the received data.
644 IN OUT NET_BUF
*NetBuffer
,
649 Get the length of the free space of the specific socket buffer.
651 @param[in] Sock Pointer to the socket.
652 @param[in] Which Flag to indicate which socket buffer to check:
653 either send buffer or receive buffer.
655 @return The length of the free space, in bytes.
665 Called by the low layer protocol to indicate that there will be no more data
666 from the communication peer.
668 This function sets the socket's state to SO_NO_MORE_DATA and signals all queued
669 IO tokens with the error status EFI_CONNECTION_FIN.
671 @param[in, out] Sock Pointer to the socket.
680 // Socket provided operations for user interface implemented in SockInterface.c
684 Create a socket and its associated protocol control block
685 with the intial data SockInitData and protocol specific
688 @param[in] SockInitData Inital data to setting the socket.
690 @return Pointer to the newly created socket. If NULL, an error condition occured.
695 IN SOCK_INIT_DATA
*SockInitData
699 Destroy the socket Sock and its associated protocol control block.
701 @param[in, out] Sock The socket to be destroyed.
703 @retval EFI_SUCCESS The socket Sock was destroyed successfully.
704 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
713 Configure the specific socket Sock using configuration data ConfigData.
715 @param[in] Sock Pointer to the socket to be configured.
716 @param[in] ConfigData Pointer to the configuration data.
718 @retval EFI_SUCCESS The socket configured successfully.
719 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
720 socket is already configured.
730 Initiate a connection establishment process.
732 @param[in] Sock Pointer to the socket to initiate the initate the
734 @param[in] Token Pointer to the token used for the connection
737 @retval EFI_SUCCESS The connection initialized successfully.
738 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
739 socket is closed, or the socket is not configured to
740 be an active one, or the token is already in one of
742 @retval EFI_NO_MAPPING The IP address configuration operation is not
744 @retval EFI_NOT_STARTED The socket is not configured.
754 Issue a listen token to get an existed connected network instance,
755 or wait for a connection if there is none.
757 @param[in] Sock Pointer to the socket to accept connections.
758 @param[in] Token The token to accept a connection.
760 @retval EFI_SUCCESS Either a connection is accepted or the Token is
761 buffered for further acceptance.
762 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
763 socket is closed, or the socket is not configured to
764 be a passive one, or the token is already in one of
766 @retval EFI_NO_MAPPING The IP address configuration operation is not
768 @retval EFI_NOT_STARTED The socket is not configured.
769 @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit.
779 Issue a token with data to the socket to send out.
781 @param[in] Sock Pointer to the socket to process the token with
783 @param[in] Token The token with data that needs to send out.
785 @retval EFI_SUCCESS The token processed successfully.
786 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
787 socket is closed, or the socket is not in a
788 synchronized state , or the token is already in one
789 of this socket's lists.
790 @retval EFI_NO_MAPPING The IP address configuration operation is not
792 @retval EFI_NOT_STARTED The socket is not configured.
793 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to a memory limit.
803 Issue a token to get data from the socket.
805 @param[in] Sock Pointer to the socket to get data from.
806 @param[in] Token The token to store the received data from the
809 @retval EFI_SUCCESS The token processed successfully.
810 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
811 socket is closed, or the socket is not in a
812 synchronized state , or the token is already in one
813 of this socket's lists.
814 @retval EFI_NO_MAPPING The IP address configuration operation is not
816 @retval EFI_NOT_STARTED The socket is not configured.
817 @retval EFI_CONNECTION_FIN The connection is closed and there is no more data.
818 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to a memory limit.
828 Reset the socket and its associated protocol control block.
830 @param[in, out] Sock Pointer to the socket to be flushed.
832 @retval EFI_SUCCESS The socket flushed successfully.
833 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
842 Close or abort the socket associated connection.
844 @param[in, out] Sock Pointer to the socket of the connection to close
846 @param[in] Token The token for close operation.
847 @param[in] OnAbort TRUE for aborting the connection, FALSE to close it.
849 @retval EFI_SUCCESS The close or abort operation initialized
851 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
852 socket is closed, or the socket is not in a
853 synchronized state , or the token is already in one
854 of this socket's lists.
855 @retval EFI_NO_MAPPING The IP address configuration operation is not
857 @retval EFI_NOT_STARTED The socket is not configured.
868 Abort the socket associated connection, listen, transmission or receive request.
870 @param[in, out] Sock Pointer to the socket to abort.
871 @param[in] Token Pointer to a token that has been issued by
872 Connect(), Accept(), Transmit() or Receive(). If
873 NULL, all pending tokens issued by the four
874 functions listed above will be aborted.
876 @retval EFI_UNSUPPORTED The operation is not supported in the current
886 Get the mode data of the low layer protocol.
888 @param[in] Sock Pointer to the socket to get mode data from.
889 @param[in, out] Mode Pointer to the data to store the low layer mode
892 @retval EFI_SUCCESS The mode data was obtained successfully.
893 @retval EFI_NOT_STARTED The socket is not configured.
903 Configure the low level protocol to join a multicast group for
904 this socket's connection.
906 @param[in] Sock Pointer to the socket of the connection to join the
907 specific multicast group.
908 @param[in] GroupInfo Pointer to the multicast group information.
910 @retval EFI_SUCCESS The configuration completed successfully.
911 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
912 @retval EFI_NOT_STARTED The socket is not configured.
922 Add or remove route information in IP route table associated
925 @param[in] Sock Pointer to the socket associated with the IP route
927 @param[in] RouteInfo Pointer to the route information to be processed.
929 @retval EFI_SUCCESS The route table updated successfully.
930 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
931 @retval EFI_NO_MAPPING The IP address configuration operation is not
933 @retval EFI_NOT_STARTED The socket is not configured.