3 Copyright (c) 2005 - 2006, Intel Corporation
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
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.
26 #include <Protocol/IP4.h>
27 #include <Protocol/Tcp4.h>
28 #include <Protocol/Udp4.h>
30 #include <Library/NetLib.h>
31 #include <Library/DebugLib.h>
32 #include <Library/UefiRuntimeServicesTableLib.h>
33 #include <Library/UefiDriverEntryPoint.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/BaseLib.h>
36 #include <Library/UefiLib.h>
37 #include <Library/MemoryAllocationLib.h>
38 #include <Library/BaseMemoryLib.h>
40 #define SOCK_SND_BUF 0
41 #define SOCK_RCV_BUF 1
43 #define SOCK_BUFF_LOW_WATER 2 * 1024
44 #define SOCK_RCV_BUFF_SIZE 8 * 1024
45 #define SOCK_SND_BUFF_SIZE 8 * 1024
46 #define SOCK_BACKLOG 5
48 #define PROTO_RESERVED_LEN 20
50 #define SO_NO_MORE_DATA 0x0001
55 // When a socket is created it enters into SO_UNCONFIGURED,
56 // no actions can be taken on this socket, only after calling
57 // SockConfigure. The state transition diagram of socket is
60 // SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING
62 // | ---> SO_LISTENING |
64 // |------------------SO_DISCONNECTING<-- SO_CONNECTED
66 // A passive socket can only go into SO_LISTENING and
67 // SO_UNCONFIGURED state. SO_XXXING state is a middle state
68 // when a socket is undergoing a protocol procedure such
69 // as requesting a TCP connection.
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
;
152 typedef struct _SOCK_COMPLETION_TOKEN
{
155 } SOCK_COMPLETION_TOKEN
;
157 typedef struct _SOCK_IO_TOKEN
{
158 SOCK_COMPLETION_TOKEN Token
;
166 // the request issued from socket layer to protocol layer
169 SOCK_ATTACH
, // attach current socket to a new PCB
170 SOCK_DETACH
, // detach current socket from the PCB
171 SOCK_CONFIGURE
, // configure attached PCB
172 SOCK_FLUSH
, // flush attached PCB
173 SOCK_SND
, // need protocol to send something
174 SOCK_SNDPUSH
, // need protocol to send pushed data
175 SOCK_SNDURG
, // need protocol to send urgent data
176 SOCK_CONSUMED
, // application has retrieved data from socket
177 SOCK_CONNECT
, // need to connect to a peer
178 SOCK_CLOSE
, // need to close the protocol process
179 SOCK_ABORT
, // need to reset the protocol process
180 SOCK_POLL
, // need to poll to the protocol layer
181 SOCK_ROUTE
, // need to add a route information
182 SOCK_MODE
, // need to get the mode data of the protocol
183 SOCK_GROUP
// need to join a mcast group
190 SOCK_DGRAM
, // this socket providing datagram service
191 SOCK_STREAM
// this socket providing stream service
195 // the handler of protocol for request from socket
199 (*SOCK_PROTO_HANDLER
) (
200 IN SOCKET
* Socket
, // the socket issuing the request to protocol
201 IN SOCK_REQUEST Request
, // the request issued by socket
202 IN VOID
*RequestData
// the request related data
206 // the buffer structure of rcvd data and send data used by socket
208 typedef struct _SOCK_BUFFER
{
209 UINT32 HighWater
; // the buffersize upper limit of sock_buffer
210 UINT32 LowWater
; // the low warter mark of sock_buffer
211 NET_BUF_QUEUE
*DataQueue
; // the queue to buffer data
215 // the initialize data for create a new socket
217 typedef struct _SOCK_INIT_DATA
{
221 SOCKET
*Parent
; // the parent of this socket
222 UINT32 BackLog
; // the connection limit for listening socket
223 UINT32 SndBufferSize
; // the high warter mark of send buffer
224 UINT32 RcvBufferSize
; // the high warter mark of receive buffer
225 VOID
*Protocol
; // the pointer to protocol function template
226 // wanted to install on socket
228 SOCK_PROTO_HANDLER ProtoHandler
;
230 EFI_HANDLE DriverBinding
; // the driver binding handle
234 // socket provided oprerations for low layer protocol
238 // socket provided operations for user interface
247 // when the connection establishment process for a Sock
248 // is finished low layer protocol calling this function
249 // to notify socket layer
252 SockConnEstablished (
262 // called by low layer protocol to trim send buffer of
263 // Sock, when Count data is sent out completely
272 // called by low layer protocol to get Len of data from
273 // socket to send and copy it in Dest
284 // called by low layer protocol to notify socket no more data can be
293 // called by low layer protocol to append a NetBuffer
294 // to rcv buffer of sock
299 IN NET_BUF
*NetBuffer
,
321 // the socket structure representing a network service access point
326 // socket description information
329 EFI_HANDLE SockHandle
; // the virtual handle of the socket
330 EFI_HANDLE DriverBinding
; // socket't driver binding protocol
331 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
332 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
333 SOCK_CONFIGURE_STATE ConfigureState
;
337 NET_LOCK Lock
; // the lock of socket
338 SOCK_BUFFER SndBuffer
; // send buffer of application's data
339 SOCK_BUFFER RcvBuffer
; // receive buffer of received data
340 EFI_STATUS SockError
; // the error returned by low layer protocol
344 // fields used to manage the connection request
346 UINT32 BackLog
; // the limit of connection to this socket
347 UINT32 ConnCnt
; // the current count of connections to it
348 SOCKET
*Parent
; // listening parent that accept the connection
349 NET_LIST_ENTRY ConnectionList
; // the connections maintained by this socket
351 // the queue to buffer application's asynchronous token
353 NET_LIST_ENTRY ListenTokenList
;
354 NET_LIST_ENTRY RcvTokenList
;
355 NET_LIST_ENTRY SndTokenList
;
356 NET_LIST_ENTRY ProcessingSndTokenList
;
358 SOCK_COMPLETION_TOKEN
*ConnectionToken
; // app's token to signal if connected
359 SOCK_COMPLETION_TOKEN
*CloseToken
; // app's token to signal if closed
362 // interface for low level protocol
364 SOCK_PROTO_HANDLER ProtoHandler
; // the request handler of protocol
365 UINT8 ProtoReserved
[PROTO_RESERVED_LEN
]; // Data fields reserved for protocol
367 EFI_TCP4_PROTOCOL TcpProtocol
;
368 EFI_UDP4_PROTOCOL UdpProtocol
;
373 // the token structure buffered in socket layer
375 typedef struct _SOCK_TOKEN
{
376 NET_LIST_ENTRY TokenList
; // the entry to add in the token list
377 SOCK_COMPLETION_TOKEN
*Token
; // The application's token
378 UINT32 RemainDataLen
; // unprocessed data length
379 SOCKET
*Sock
; // the poninter to the socket this token
384 // reserved data to access the NET_BUF delivered by UDP driver
386 typedef struct _UDP_RSV_DATA
{
388 EFI_UDP4_SESSION_DATA Session
;
392 // reserved data to access the NET_BUF delivered by TCP driver
394 typedef struct _TCP_RSV_DATA
{
399 // call it to creat a socket and attach it to a PCB
403 IN SOCK_INIT_DATA
*SockInitData
,
409 // call it to destroy a socket and its related PCB
417 // call it to configure a socket and its related PCB
426 // call it to connect a socket to the peer
435 // call it to issue an asynchronous listen token to the socket
444 // Call it to send data using this socket
453 // Call it to receive data from this socket
462 // Call it to flush a socket
470 // Call it to close a socket in the light of policy in Token
480 // Call it to get the mode data of low layer protocol
489 // call it to add this socket instance into a group
498 // call it to add a route entry for this socket instance
507 // Supporting function to operate on socket buffer
511 IN SOCK_BUFFER
*Sockbuf
516 IN SOCK_BUFFER
*Sockbuf
,
517 IN NET_BUF
*SockEntry