2 Common head file for TCP socket.
4 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include <Protocol/Tcp4.h>
16 #include <Protocol/Tcp6.h>
18 #include <Library/NetLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/UefiRuntimeServicesTableLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/DpcLib.h>
27 #define SOCK_SND_BUF 0
28 #define SOCK_RCV_BUF 1
30 #define SOCK_BUFF_LOW_WATER (2 * 1024)
31 #define SOCK_RCV_BUFF_SIZE (8 * 1024)
32 #define SOCK_SND_BUFF_SIZE (8 * 1024)
33 #define SOCK_BACKLOG 5
35 #define PROTO_RESERVED_LEN 20
37 #define SO_NO_MORE_DATA 0x0001
42 // When a socket is created it enters into SO_UNCONFIGURED,
43 // no actions can be taken on this socket, only after calling
44 // SockConfigure. The state transition diagram of socket is
47 // SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING
49 // | ---> SO_LISTENING |
51 // |------------------SO_DISCONNECTING<-- SO_CONNECTED
53 // A passive socket can only go into SO_LISTENING and
54 // SO_UNCONFIGURED state. SO_XXXING state is a middle state
55 // when a socket is undergoing a protocol procedure such
56 // as requesting a TCP connection.
65 #define SO_LISTENING 1
66 #define SO_CONNECTING 2
67 #define SO_CONNECTED 3
68 #define SO_DISCONNECTING 4
71 /// Socket configure state
73 #define SO_UNCONFIGURED 0
74 #define SO_CONFIGURED_ACTIVE 1
75 #define SO_CONFIGURED_PASSIVE 2
76 #define SO_NO_MAPPING 3
79 /// The request issued from socket layer to protocol layer.
81 #define SOCK_ATTACH 0 ///< Attach current socket to a new PCB
82 #define SOCK_DETACH 1 ///< Detach current socket from the PCB
83 #define SOCK_CONFIGURE 2 ///< Configure attached PCB
84 #define SOCK_FLUSH 3 ///< Flush attached PCB
85 #define SOCK_SND 4 ///< Need protocol to send something
86 #define SOCK_SNDPUSH 5 ///< Need protocol to send pushed data
87 #define SOCK_SNDURG 6 ///< Need protocol to send urgent data
88 #define SOCK_CONSUMED 7 ///< Application has retrieved data from socket
89 #define SOCK_CONNECT 8 ///< Need to connect to a peer
90 #define SOCK_CLOSE 9 ///< Need to close the protocol process
91 #define SOCK_ABORT 10 ///< Need to reset the protocol process
92 #define SOCK_POLL 11 ///< Need to poll to the protocol layer
93 #define SOCK_ROUTE 12 ///< Need to add a route information
94 #define SOCK_MODE 13 ///< Need to get the mode data of the protocol
95 #define SOCK_GROUP 14 ///< Need to join a mcast group
98 Set socket SO_NO_MORE_DATA flag.
100 @param[in] Sock Pointer to the socket
103 #define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
106 Check whether the socket is unconfigured.
108 @param[in] Sock Pointer to the socket.
110 @retval TRUE The socket is unconfigued.
111 @retval FALSE The socket is not unconfigued.
114 #define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
117 Check whether the socket is configured.
119 @param[in] Sock Pointer to the socket
121 @retval TRUE The socket is configued
122 @retval FALSE The socket is not configued
125 #define SOCK_IS_CONFIGURED(Sock) \
126 (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
127 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
130 Check whether the socket is configured to active mode.
132 @param[in] Sock Pointer to the socket.
134 @retval TRUE The socket is configued to active mode.
135 @retval FALSE The socket is not configued to active mode.
138 #define SOCK_IS_CONFIGURED_ACTIVE(Sock) ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
141 Check whether the socket is configured to passive mode.
143 @param[in] Sock Pointer to the socket.
145 @retval TRUE The socket is configued to passive mode.
146 @retval FALSE The socket is not configued to passive mode.
149 #define SOCK_IS_CONNECTED_PASSIVE(Sock) ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
152 Check whether the socket is mapped.
154 @param[in] Sock Pointer to the socket.
156 @retval TRUE The socket is not mapping.
157 @retval FALSE The socket is mapped.
160 #define SOCK_IS_NO_MAPPING(Sock) ((Sock)->ConfigureState == SO_NO_MAPPING)
163 Check whether the socket is closed.
165 @param[in] Sock Pointer to the socket.
167 @retval TRUE The socket is closed.
168 @retval FALSE The socket is not closed.
171 #define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
174 Check whether the socket is listening.
176 @param[in] Sock Pointer to the socket.
178 @retval TRUE The socket is listening.
179 @retval FALSE The socket is not listening.
182 #define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
185 Check whether the socket is connecting.
187 @param[in] Sock Pointer to the socket.
189 @retval TRUE The socket is connecting.
190 @retval FALSE The socket is not connecting.
193 #define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
196 Check whether the socket has connected.
198 @param[in] Sock Pointer to the socket.
200 @retval TRUE The socket has connected.
201 @retval FALSE The socket has not connected.
204 #define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
207 Check whether the socket is disconnecting.
209 @param[in] Sock Pointer to the socket.
211 @retval TRUE The socket is disconnecting.
212 @retval FALSE The socket is not disconnecting.
215 #define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
218 Check whether the socket is no more data.
220 @param[in] Sock Pointer to the socket.
222 @retval TRUE The socket is no more data.
223 @retval FALSE The socket still has data.
226 #define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
229 Set the size of the receive buffer.
231 @param[in] Sock Pointer to the socket.
232 @param[in] Size The size to set.
235 #define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
238 Get the size of the receive buffer.
240 @param[in] Sock Pointer to the socket.
242 @return The receive buffer size.
245 #define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
248 Get the size of the receive data.
250 @param[in] Sock Pointer to the socket.
252 @return The received data size.
255 #define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
258 Set the size of the send buffer.
260 @param[in] Sock Pointer to the socket.
261 @param[in] Size The size to set.
264 #define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
267 Get the size of the send buffer.
269 @param[in] Sock Pointer to the socket.
271 @return The send buffer size.
274 #define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
277 Get the size of the send data.
279 @param[in] Sock Pointer to the socket.
281 @return The send data size.
284 #define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
287 Set the backlog value of the socket.
289 @param[in] Sock Pointer to the socket.
290 @param[in] Value The value to set.
293 #define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
296 Get the backlog value of the socket.
298 @param[in] Sock Pointer to the socket.
300 @return The backlog value.
303 #define GET_BACKLOG(Sock) ((Sock)->BackLog)
306 Set the socket with error state.
308 @param[in] Sock Pointer to the socket.
309 @param[in] Error The error state.
312 #define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
314 #define SOCK_SIGNATURE SIGNATURE_32 ('S', 'O', 'C', 'K')
316 #define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
318 #define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
320 #define PROTO_TOKEN_FORM_SOCK(SockToken, Type) ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
322 typedef struct _TCP_SOCKET SOCKET
;
325 /// Socket completion token
327 typedef struct _SOCK_COMPLETION_TOKEN
{
328 EFI_EVENT Event
; ///< The event to be issued
329 EFI_STATUS Status
; ///< The status to be issued
330 } SOCK_COMPLETION_TOKEN
;
338 /// The application token with data packet
340 typedef struct _SOCK_IO_TOKEN
{
341 SOCK_COMPLETION_TOKEN Token
;
349 SockDgram
, ///< This socket providing datagram service
350 SockStream
///< This socket providing stream service
354 /// The buffer structure of rcvd data and send data used by socket.
356 typedef struct _SOCK_BUFFER
{
357 UINT32 HighWater
; ///< The buffersize upper limit of sock_buffer
358 UINT32 LowWater
; ///< The low water mark of sock_buffer
359 NET_BUF_QUEUE
*DataQueue
; ///< The queue to buffer data
363 The handler of protocol for request from socket.
365 @param[in] Socket The socket issuing the request to protocol.
366 @param[in] Request The request issued by socket.
367 @param[in] RequestData The request related data.
369 @retval EFI_SUCCESS The socket request is completed successfully.
370 @retval other The error status returned by the corresponding TCP
376 (*SOCK_PROTO_HANDLER
) (
383 The Callback funtion called after the TCP socket is created.
385 @param[in] This Pointer to the socket just created.
386 @param[in] Context Context of the socket.
388 @retval EFI_SUCCESS This protocol installed successfully.
389 @retval other Some error occured.
394 (*SOCK_CREATE_CALLBACK
) (
400 The callback function called before the TCP socket is to be destroyed.
402 @param[in] This The TCP socket to be destroyed.
403 @param[in] Context The context.
408 (*SOCK_DESTROY_CALLBACK
) (
414 /// The initialize data for create a new socket.
416 typedef struct _SOCK_INIT_DATA
{
420 SOCKET
*Parent
; ///< The parent of this socket
421 UINT32 BackLog
; ///< The connection limit for listening socket
422 UINT32 SndBufferSize
; ///< The high water mark of send buffer
423 UINT32 RcvBufferSize
; ///< The high water mark of receive buffer
425 VOID
*Protocol
; ///< The pointer to protocol function template
426 ///< wanted to install on socket
429 // Callbacks after socket is created and before socket is to be destroyed.
431 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
432 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
433 VOID
*Context
; ///< The context of the callback
436 // Opaque protocol data.
441 SOCK_PROTO_HANDLER ProtoHandler
; ///< The handler of protocol for socket request
443 EFI_HANDLE DriverBinding
; ///< The driver binding handle
447 /// The union type of TCP4 and TCP6 protocol.
449 typedef union _NET_PROTOCOL
{
450 EFI_TCP4_PROTOCOL Tcp4Protocol
; ///< Tcp4 protocol
451 EFI_TCP6_PROTOCOL Tcp6Protocol
; ///< Tcp6 protocol
454 /// 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 UINT8 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
484 // The queue to buffer application's asynchronous token
486 LIST_ENTRY ListenTokenList
;
487 LIST_ENTRY RcvTokenList
;
488 LIST_ENTRY SndTokenList
;
489 LIST_ENTRY ProcessingSndTokenList
;
491 SOCK_COMPLETION_TOKEN
*ConnectionToken
; ///< app's token to signal if connected
492 SOCK_COMPLETION_TOKEN
*CloseToken
; ///< app's token to signal if closed
494 // Interface for low level protocol
496 SOCK_PROTO_HANDLER ProtoHandler
; ///< The request handler of protocol
497 UINT8 ProtoReserved
[PROTO_RESERVED_LEN
]; ///< Data fields reserved for protocol
499 NET_PROTOCOL NetProtocol
; ///< TCP4 or TCP6 protocol socket used
501 // Callbacks after socket is created and before socket is to be destroyed.
503 SOCK_CREATE_CALLBACK CreateCallback
; ///< Callback after created
504 SOCK_DESTROY_CALLBACK DestroyCallback
; ///< Callback before destroied
505 VOID
*Context
; ///< The context of the callback
509 /// The token structure buffered in socket layer.
511 typedef struct _SOCK_TOKEN
{
512 LIST_ENTRY TokenList
; ///< The entry to add in the token list
513 SOCK_COMPLETION_TOKEN
*Token
; ///< The application's token
514 UINT32 RemainDataLen
; ///< Unprocessed data length
515 SOCKET
*Sock
; ///< The poninter to the socket this token
520 /// Reserved data to access the NET_BUF delivered by TCP driver.
522 typedef struct _TCP_RSV_DATA
{
527 // Socket provided oprerations for low layer protocol implemented in SockImpl.c
531 Set the state of the socket.
533 @param[in, out] Sock Pointer to the socket.
534 @param[in] State The new socket state to be set.
544 Clone a new socket including its associated protocol control block.
546 @param[in] Sock Pointer to the socket to be cloned.
548 @return Pointer to the newly cloned socket. If NULL, an error condition occurred.
557 Called by the low layer protocol to indicate the socket a connection is
560 This function just changes the socket's state to SO_CONNECTED
561 and signals the token used for connection establishment.
563 @param[in, out] Sock Pointer to the socket associated with the
564 established connection.
568 SockConnEstablished (
573 Called by the low layer protocol to indicate that the connection is closed.
575 This function flushes the socket, sets the state to SO_CLOSED, and signals
578 @param[in, out] Sock Pointer to the socket associated with the closed
588 Called by low layer protocol to indicate that some data is sent or processed.
590 This function trims the sent data in the socket send buffer and signals the data
593 @param[in, out] Sock Pointer to the socket.
594 @param[in] Count The length of the data processed or sent, in bytes.
604 Called by the low layer protocol to copy some data in socket send
605 buffer starting from the specific offset to a buffer provided by
608 @param[in] Sock Pointer to the socket.
609 @param[in] Offset The start point of the data to be copied.
610 @param[in] Len The length of the data to be copied.
611 @param[out] Dest Pointer to the destination to copy the data.
613 @return The data size copied.
625 Called by the low layer protocol to deliver received data to socket layer.
627 This function appends the data to the socket receive buffer, set the
628 urgent data length, then checks if any receive token can be signaled.
630 @param[in, out] Sock Pointer to the socket.
631 @param[in, out] NetBuffer Pointer to the buffer that contains the received data.
632 @param[in] UrgLen The length of the urgent data in the received data.
638 IN OUT NET_BUF
*NetBuffer
,
643 Get the length of the free space of the specific socket buffer.
645 @param[in] Sock Pointer to the socket.
646 @param[in] Which Flag to indicate which socket buffer to check:
647 either send buffer or receive buffer.
649 @return The length of the free space, in bytes.
659 Called by the low layer protocol to indicate that there will be no more data
660 from the communication peer.
662 This function sets the socket's state to SO_NO_MORE_DATA and signals all queued
663 IO tokens with the error status EFI_CONNECTION_FIN.
665 @param[in, out] Sock Pointer to the socket.
674 // Socket provided operations for user interface implemented in SockInterface.c
678 Create a socket and its associated protocol control block
679 with the intial data SockInitData and protocol specific
682 @param[in] SockInitData Inital data to setting the socket.
684 @return Pointer to the newly created socket. If NULL, an error condition occured.
689 IN SOCK_INIT_DATA
*SockInitData
693 Destroy the socket Sock and its associated protocol control block.
695 @param[in, out] Sock The socket to be destroyed.
697 @retval EFI_SUCCESS The socket Sock was destroyed successfully.
698 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
707 Configure the specific socket Sock using configuration data ConfigData.
709 @param[in] Sock Pointer to the socket to be configured.
710 @param[in] ConfigData Pointer to the configuration data.
712 @retval EFI_SUCCESS The socket configured successfully.
713 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
714 socket is already configured.
724 Initiate a connection establishment process.
726 @param[in] Sock Pointer to the socket to initiate the initate the
728 @param[in] Token Pointer to the token used for the connection
731 @retval EFI_SUCCESS The connection initialized successfully.
732 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
733 socket is closed, or the socket is not configured to
734 be an active one, or the token is already in one of
736 @retval EFI_NO_MAPPING The IP address configuration operation is not
738 @retval EFI_NOT_STARTED The socket is not configured.
748 Issue a listen token to get an existed connected network instance,
749 or wait for a connection if there is none.
751 @param[in] Sock Pointer to the socket to accept connections.
752 @param[in] Token The token to accept a connection.
754 @retval EFI_SUCCESS Either a connection is accepted or the Token is
755 buffered for further acceptance.
756 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
757 socket is closed, or the socket is not configured to
758 be a passive one, or the token is already in one of
760 @retval EFI_NO_MAPPING The IP address configuration operation is not
762 @retval EFI_NOT_STARTED The socket is not configured.
763 @retval EFI_OUT_OF_RESOURCE Failed to buffer the Token due to memory limit.
773 Issue a token with data to the socket to send out.
775 @param[in] Sock Pointer to the socket to process the token with
777 @param[in] Token The token with data that needs to send out.
779 @retval EFI_SUCCESS The token processed successfully.
780 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
781 socket is closed, or the socket is not in a
782 synchronized state , or the token is already in one
783 of this socket's lists.
784 @retval EFI_NO_MAPPING The IP address configuration operation is not
786 @retval EFI_NOT_STARTED The socket is not configured.
787 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to a memory limit.
797 Issue a token to get data from the socket.
799 @param[in] Sock Pointer to the socket to get data from.
800 @param[in] Token The token to store the received data from the
803 @retval EFI_SUCCESS The token processed successfully.
804 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
805 socket is closed, or the socket is not in a
806 synchronized state , or the token is already in one
807 of this socket's lists.
808 @retval EFI_NO_MAPPING The IP address configuration operation is not
810 @retval EFI_NOT_STARTED The socket is not configured.
811 @retval EFI_CONNECTION_FIN The connection is closed and there is no more data.
812 @retval EFI_OUT_OF_RESOURCE Failed to buffer the token due to a memory limit.
822 Reset the socket and its associated protocol control block.
824 @param[in, out] Sock Pointer to the socket to be flushed.
826 @retval EFI_SUCCESS The socket flushed successfully.
827 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
836 Close or abort the socket associated connection.
838 @param[in, out] Sock Pointer to the socket of the connection to close
840 @param[in] Token The token for close operation.
841 @param[in] OnAbort TRUE for aborting the connection, FALSE to close it.
843 @retval EFI_SUCCESS The close or abort operation initialized
845 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket, or the
846 socket is closed, or the socket is not in a
847 synchronized state , or the token is already in one
848 of this socket's lists.
849 @retval EFI_NO_MAPPING The IP address configuration operation is not
851 @retval EFI_NOT_STARTED The socket is not configured.
862 Abort the socket associated connection, listen, transmission or receive request.
864 @param[in, out] Sock Pointer to the socket to abort.
865 @param[in] Token Pointer to a token that has been issued by
866 Connect(), Accept(), Transmit() or Receive(). If
867 NULL, all pending tokens issued by the four
868 functions listed above will be aborted.
870 @retval EFI_UNSUPPORTED The operation is not supported in the current
880 Get the mode data of the low layer protocol.
882 @param[in] Sock Pointer to the socket to get mode data from.
883 @param[in, out] Mode Pointer to the data to store the low layer mode
886 @retval EFI_SUCCESS The mode data was obtained successfully.
887 @retval EFI_NOT_STARTED The socket is not configured.
897 Add or remove route information in IP route table associated
900 @param[in] Sock Pointer to the socket associated with the IP route
902 @param[in] RouteInfo Pointer to the route information to be processed.
904 @retval EFI_SUCCESS The route table updated successfully.
905 @retval EFI_ACCESS_DENIED Failed to get the lock to access the socket.
906 @retval EFI_NO_MAPPING The IP address configuration operation is not
908 @retval EFI_NOT_STARTED The socket is not configured.