2 Implementation of TCP4 protocol services.
4 Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 Check the integrity of the data buffer.
22 @param DataLen The total length of the data buffer.
23 @param FragmentCount The fragment count of the fragment table.
24 @param FragmentTable Pointer to the fragment table of the data
27 @retval EFI_SUCCESS The integrity check is passed.
28 @retval EFI_INVALID_PARAMETER The integrity check is failed.
34 IN UINT32 FragmentCount
,
35 IN EFI_TCP4_FRAGMENT_DATA
*FragmentTable
42 for (Index
= 0, Len
= 0; Index
< FragmentCount
; Index
++) {
43 Len
= Len
+ (UINT32
) FragmentTable
[Index
].FragmentLength
;
47 return EFI_INVALID_PARAMETER
;
55 Get the current operational status.
57 The GetModeData() function copies the current operational settings of this
58 EFI TCPv4 Protocol instance into user-supplied buffers. This function can
59 also be used to retrieve the operational setting of underlying drivers
60 such as IPv4, MNP, or SNP.
62 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
63 @param Tcp4State Pointer to the buffer to receive the current TCP
65 @param Tcp4ConfigData Pointer to the buffer to receive the current TCP
67 @param Ip4ModeData Pointer to the buffer to receive the current IPv4
68 configuration data used by the TCPv4 instance.
69 @param MnpConfigData Pointer to the buffer to receive the current MNP
70 configuration data indirectly used by the TCPv4
72 @param SnpModeData Pointer to the buffer to receive the current SNP
73 configuration data indirectly used by the TCPv4
76 @retval EFI_SUCCESS The mode data was read.
77 @retval EFI_NOT_STARTED No configuration data is available because this
78 instance hasn't been started.
79 @retval EFI_INVALID_PARAMETER This is NULL.
85 IN EFI_TCP4_PROTOCOL
*This
,
86 OUT EFI_TCP4_CONNECTION_STATE
*Tcp4State OPTIONAL
,
87 OUT EFI_TCP4_CONFIG_DATA
*Tcp4ConfigData OPTIONAL
,
88 OUT EFI_IP4_MODE_DATA
*Ip4ModeData OPTIONAL
,
89 OUT EFI_MANAGED_NETWORK_CONFIG_DATA
*MnpConfigData OPTIONAL
,
90 OUT EFI_SIMPLE_NETWORK_MODE
*SnpModeData OPTIONAL
93 TCP4_MODE_DATA TcpMode
;
97 return EFI_INVALID_PARAMETER
;
100 Sock
= SOCK_FROM_THIS (This
);
102 TcpMode
.Tcp4State
= Tcp4State
;
103 TcpMode
.Tcp4ConfigData
= Tcp4ConfigData
;
104 TcpMode
.Ip4ModeData
= Ip4ModeData
;
105 TcpMode
.MnpConfigData
= MnpConfigData
;
106 TcpMode
.SnpModeData
= SnpModeData
;
108 return SockGetMode (Sock
, &TcpMode
);
113 Initialize or brutally reset the operational parameters for
114 this EFI TCPv4 instance.
116 The Configure() function does the following:
117 * Initialize this EFI TCPv4 instance, i.e., initialize the communication end
118 setting, specify active open or passive open for an instance.
119 * Reset this TCPv4 instance brutally, i.e., cancel all pending asynchronous
120 tokens, flush transmission and receiving buffer directly without informing
121 the communication peer.
122 No other TCPv4 Protocol operation can be executed by this instance
123 until it is configured properly. For an active TCP4 instance, after a proper
124 configuration it may call Connect() to initiates the three-way handshake.
125 For a passive TCP4 instance, its state will transit to Tcp4StateListen after
126 configuration, and Accept() may be called to listen the incoming TCP connection
127 request. If TcpConfigData is set to NULL, the instance is reset. Resetting
128 process will be done brutally, the state machine will be set to Tcp4StateClosed
129 directly, the receive queue and transmit queue will be flushed, and no traffic is
130 allowed through this instance.
132 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
133 @param TcpConfigData Pointer to the configure data to configure the
136 @retval EFI_SUCCESS The operational settings are set, changed, or
138 @retval EFI_NO_MAPPING When using a default address, configuration
139 (through DHCP, BOOTP, RARP, etc.) is not
141 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
142 @retval EFI_ACCESS_DENIED Configuring TCP instance when it is already
144 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
145 @retval EFI_UNSUPPORTED One or more of the control options are not
146 supported in the implementation.
147 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
153 IN EFI_TCP4_PROTOCOL
*This
,
154 IN EFI_TCP4_CONFIG_DATA
*TcpConfigData OPTIONAL
157 EFI_TCP4_OPTION
*Option
;
164 return EFI_INVALID_PARAMETER
;
168 // Tcp protocol related parameter check will be conducted here
170 if (NULL
!= TcpConfigData
) {
172 CopyMem (&Ip
, &TcpConfigData
->AccessPoint
.RemoteAddress
, sizeof (IP4_ADDR
));
173 if ((Ip
!= 0) && !NetIp4IsUnicast (NTOHL (Ip
), 0)) {
174 return EFI_INVALID_PARAMETER
;
177 if (TcpConfigData
->AccessPoint
.ActiveFlag
&&
178 (0 == TcpConfigData
->AccessPoint
.RemotePort
|| (Ip
== 0))) {
179 return EFI_INVALID_PARAMETER
;
182 if (!TcpConfigData
->AccessPoint
.UseDefaultAddress
) {
184 CopyMem (&Ip
, &TcpConfigData
->AccessPoint
.StationAddress
, sizeof (IP4_ADDR
));
185 CopyMem (&SubnetMask
, &TcpConfigData
->AccessPoint
.SubnetMask
, sizeof (IP4_ADDR
));
186 if (!NetIp4IsUnicast (NTOHL (Ip
), 0) || !IP4_IS_VALID_NETMASK (NTOHL (SubnetMask
))) {
187 return EFI_INVALID_PARAMETER
;
191 Option
= TcpConfigData
->ControlOption
;
192 if ((NULL
!= Option
) &&
193 (Option
->EnableSelectiveAck
|| Option
->EnablePathMtuDiscovery
)) {
194 return EFI_UNSUPPORTED
;
198 Sock
= SOCK_FROM_THIS (This
);
200 if (NULL
== TcpConfigData
) {
201 return SockFlush (Sock
);
204 Status
= SockConfigure (Sock
, TcpConfigData
);
206 if (EFI_NO_MAPPING
== Status
) {
207 Sock
->ConfigureState
= SO_NO_MAPPING
;
215 Add or delete routing entries.
217 The Routes() function adds or deletes a route from the instance's routing table.
218 The most specific route is selected by comparing the SubnetAddress with the
219 destination IP address's arithmetical AND to the SubnetMask.
220 The default route is added with both SubnetAddress and SubnetMask set to 0.0.0.0.
221 The default route matches all destination IP addresses if there is no more specific route.
222 Direct route is added with GatewayAddress set to 0.0.0.0. Packets are sent to
223 the destination host if its address can be found in the Address Resolution Protocol (ARP)
224 cache or it is on the local subnet. If the instance is configured to use default
225 address, a direct route to the local network will be added automatically.
226 Each TCP instance has its own independent routing table. Instance that uses the
227 default IP address will have a copy of the EFI_IP4_CONFIG_PROTOCOL's routing table.
228 The copy will be updated automatically whenever the IP driver reconfigures its
229 instance. As a result, the previous modification to the instance's local copy
230 will be lost. The priority of checking the route table is specific with IP
231 implementation and every IP implementation must comply with RFC 1122.
233 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
234 @param DeleteRoute If TRUE, delete the specified route from routing
235 table; if FALSE, add the specified route to
237 DestinationAddress and SubnetMask are used as
238 the keywords to search route entry.
239 @param SubnetAddress The destination network.
240 @param SubnetMask The subnet mask for the destination network.
241 @param GatewayAddress The gateway address for this route.
242 It must be on the same subnet with the station
243 address unless a direct route is specified.
245 @retval EFI_SUCCESS The operation completed successfully.
246 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance has not been
248 @retval EFI_NO_MAPPING When using a default address, configuration
249 (through DHCP, BOOTP, RARP, etc.) is not
251 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
252 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to add the
253 entry to the routing table.
254 @retval EFI_NOT_FOUND This route is not in the routing table.
255 @retval EFI_ACCESS_DENIED This route is already in the routing table.
256 @retval EFI_UNSUPPORTED The TCP driver does not support this operation.
262 IN EFI_TCP4_PROTOCOL
*This
,
263 IN BOOLEAN DeleteRoute
,
264 IN EFI_IPv4_ADDRESS
*SubnetAddress
,
265 IN EFI_IPv4_ADDRESS
*SubnetMask
,
266 IN EFI_IPv4_ADDRESS
*GatewayAddress
270 TCP4_ROUTE_INFO RouteInfo
;
273 return EFI_INVALID_PARAMETER
;
276 Sock
= SOCK_FROM_THIS (This
);
278 RouteInfo
.DeleteRoute
= DeleteRoute
;
279 RouteInfo
.SubnetAddress
= SubnetAddress
;
280 RouteInfo
.SubnetMask
= SubnetMask
;
281 RouteInfo
.GatewayAddress
= GatewayAddress
;
283 return SockRoute (Sock
, &RouteInfo
);
288 Initiate a nonblocking TCP connection request for an active TCP instance.
290 The Connect() function will initiate an active open to the remote peer configured
291 in current TCP instance if it is configured active. If the connection succeeds
292 or fails due to any error, the ConnectionToken->CompletionToken.Event will be
293 signaled and ConnectionToken->CompletionToken.Status will be updated accordingly.
294 This function can only be called for the TCP instance in Tcp4StateClosed state.
295 The instance will transfer into Tcp4StateSynSent if the function returns EFI_SUCCESS.
296 If TCP three way handshake succeeds, its state will become Tcp4StateEstablished,
297 otherwise, the state will return to Tcp4StateClosed.
299 @param This Pointer to the EFI_TCP4_PROTOCOL instance
300 @param ConnectionToken Pointer to the connection token to return when
301 the TCP three way handshake finishes.
303 @retval EFI_SUCCESS The connection request is successfully initiated
304 and the state of this TCPv4 instance has
305 been changed to Tcp4StateSynSent.
306 @retval EFI_NOT_STARTED This EFI_TCP4_PROTOCOL instance hasn't been
308 @retval EFI_ACCESS_DENIED The instance is not configured as an active one
309 or it is not in Tcp4StateClosed state.
310 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
311 @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resource to
312 initiate the active open.
313 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
319 IN EFI_TCP4_PROTOCOL
*This
,
320 IN EFI_TCP4_CONNECTION_TOKEN
*ConnectionToken
326 NULL
== ConnectionToken
||
327 NULL
== ConnectionToken
->CompletionToken
.Event
) {
328 return EFI_INVALID_PARAMETER
;
331 Sock
= SOCK_FROM_THIS (This
);
333 return SockConnect (Sock
, ConnectionToken
);
338 Listen on the passive instance to accept an incoming connection request.
340 The Accept() function initiates an asynchronous accept request to wait for an
341 incoming connection on the passive TCP instance. If a remote peer successfully
342 establishes a connection with this instance, a new TCP instance will be created
343 and its handle will be returned in ListenToken->NewChildHandle. The newly created
344 instance is configured by inheriting the passive instance's configuration and is
345 ready for use upon return. The instance is in the Tcp4StateEstablished state.
346 The ListenToken->CompletionToken.Event will be signaled when a new connection
347 is accepted, user aborts the listen or connection is reset. This function only
348 can be called when current TCP instance is in Tcp4StateListen state.
350 @param This Pointer to the EFI_TCP4_PROTOCOL instance
351 @param ListenToken Pointer to the listen token to return when
354 @retval EFI_SUCCESS The listen token has been queued successfully.
355 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
357 @retval EFI_ACCESS_DENIED The instatnce is not a passive one or it is not
358 in Tcp4StateListen state or a same listen token
359 has already existed in the listen token queue of
361 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
362 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish
364 @retval EFI_DEVICE_ERROR Any unexpected and not belonged to above category error.
370 IN EFI_TCP4_PROTOCOL
*This
,
371 IN EFI_TCP4_LISTEN_TOKEN
*ListenToken
377 NULL
== ListenToken
||
378 NULL
== ListenToken
->CompletionToken
.Event
) {
379 return EFI_INVALID_PARAMETER
;
382 Sock
= SOCK_FROM_THIS (This
);
384 return SockAccept (Sock
, ListenToken
);
389 Queues outgoing data into the transmit queue.
391 The Transmit() function queues a sending request to this TCPv4 instance along
392 with the user data. The status of the token is updated and the event in the token
393 will be signaled once the data is sent out or some error occurs.
395 @param This Pointer to the EFI_TCP4_PROTOCOL instance
396 @param Token Pointer to the completion token to queue to the
399 @retval EFI_SUCCESS The data has been queued for transmission.
400 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
402 @retval EFI_NO_MAPPING When using a default address, configuration
403 (DHCP, BOOTP, RARP, etc.) is not finished yet.
404 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
405 @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
406 * A transmit completion token with the same
407 Token-> CompletionToken.Event was already in the
409 * The current instance is in Tcp4StateClosed state
410 * The current instance is a passive one and
411 it is in Tcp4StateListen state.
412 * User has called Close() to disconnect this
414 @retval EFI_NOT_READY The completion token could not be queued because
415 the transmit queue is full.
416 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of
418 @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or
425 IN EFI_TCP4_PROTOCOL
*This
,
426 IN EFI_TCP4_IO_TOKEN
*Token
434 NULL
== Token
->CompletionToken
.Event
||
435 NULL
== Token
->Packet
.TxData
||
436 0 == Token
->Packet
.TxData
->FragmentCount
||
437 0 == Token
->Packet
.TxData
->DataLength
439 return EFI_INVALID_PARAMETER
;
442 Status
= Tcp4ChkDataBuf (
443 (UINT32
) Token
->Packet
.TxData
->DataLength
,
444 (UINT32
) Token
->Packet
.TxData
->FragmentCount
,
445 Token
->Packet
.TxData
->FragmentTable
447 if (EFI_ERROR (Status
)) {
451 Sock
= SOCK_FROM_THIS (This
);
453 return SockSend (Sock
, Token
);
459 Place an asynchronous receive request into the receiving queue.
461 The Receive() function places a completion token into the receive packet queue.
462 This function is always asynchronous. The caller must allocate the
463 Token->CompletionToken.Event and the FragmentBuffer used to receive data. He also
464 must fill the DataLength which represents the whole length of all FragmentBuffer.
465 When the receive operation completes, the EFI TCPv4 Protocol driver updates the
466 Token->CompletionToken.Status and Token->Packet.RxData fields and the
467 Token->CompletionToken.Event is signaled. If got data the data and its length
468 will be copy into the FragmentTable, in the same time the full length of received
469 data will be recorded in the DataLength fields. Providing a proper notification
470 function and context for the event will enable the user to receive the notification
471 and receiving status. That notification function is guaranteed to not be re-entered.
473 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
474 @param Token Pointer to a token that is associated with the
475 receive data descriptor.
477 @retval EFI_SUCCESS The receive completion token was cached.
478 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
480 @retval EFI_NO_MAPPING When using a default address, configuration
481 (DHCP, BOOTP, RARP, etc.) is not finished yet.
482 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
483 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued
484 due to a lack of system resources.
485 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
486 The EFI TCPv4 Protocol instance has been reset
488 @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
489 * A receive completion token with the same
490 Token->CompletionToken.Event was already in
492 * The current instance is in Tcp4StateClosed state.
493 * The current instance is a passive one and it
494 is in Tcp4StateListen state.
495 * User has called Close() to disconnect this
497 @retval EFI_CONNECTION_FIN The communication peer has closed the connection
498 and there is no any buffered data in the receive
499 buffer of this instance.
500 @retval EFI_NOT_READY The receive request could not be queued because
501 the receive queue is full.
507 IN EFI_TCP4_PROTOCOL
*This
,
508 IN EFI_TCP4_IO_TOKEN
*Token
516 NULL
== Token
->CompletionToken
.Event
||
517 NULL
== Token
->Packet
.RxData
||
518 0 == Token
->Packet
.RxData
->FragmentCount
||
519 0 == Token
->Packet
.RxData
->DataLength
521 return EFI_INVALID_PARAMETER
;
524 Status
= Tcp4ChkDataBuf (
525 (UINT32
) Token
->Packet
.RxData
->DataLength
,
526 (UINT32
) Token
->Packet
.RxData
->FragmentCount
,
527 Token
->Packet
.RxData
->FragmentTable
529 if (EFI_ERROR (Status
)) {
533 Sock
= SOCK_FROM_THIS (This
);
535 return SockRcv (Sock
, Token
);
541 Disconnecting a TCP connection gracefully or reset a TCP connection.
543 Initiate an asynchronous close token to TCP driver. After Close() is called,
544 any buffered transmission data will be sent by TCP driver and the current
545 instance will have a graceful close working flow described as RFC 793 if
546 AbortOnClose is set to FALSE, otherwise, a rest packet will be sent by TCP
547 driver to fast disconnect this connection. When the close operation completes
548 successfully the TCP instance is in Tcp4StateClosed state, all pending
549 asynchronous operation is signaled and any buffers used for TCP network traffic
552 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
553 @param CloseToken Pointer to the close token to return when
556 @retval EFI_SUCCESS The operation completed successfully.
557 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
559 @retval EFI_ACCESS_DENIED One or more of the following are TRUE:
560 * Configure() has been called with TcpConfigData
561 set to NULL and this function has not returned.
562 * Previous Close() call on this instance has not
564 @retval EFI_INVALID_PARAMETER One ore more parameters are invalid.
565 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resource to finish the
567 @retval EFI_DEVICE_ERROR Any unexpected and not belonged to above
574 IN EFI_TCP4_PROTOCOL
*This
,
575 IN EFI_TCP4_CLOSE_TOKEN
*CloseToken
581 NULL
== CloseToken
||
582 NULL
== CloseToken
->CompletionToken
.Event
) {
583 return EFI_INVALID_PARAMETER
;
586 Sock
= SOCK_FROM_THIS (This
);
588 return SockClose (Sock
, CloseToken
, CloseToken
->AbortOnClose
);
593 Abort an asynchronous connection, listen, transmission or receive request.
595 The Cancel() function aborts a pending connection, listen, transmit or receive
596 request. If Token is not NULL and the token is in the connection, listen,
597 transmission or receive queue when it is being cancelled, its Token->Status
598 will be set to EFI_ABORTED and then Token->Event will be signaled. If the token
599 is not in one of the queues, which usually means that the asynchronous operation
600 has completed, EFI_NOT_FOUND is returned. If Token is NULL all asynchronous token
601 issued by Connect(), Accept(), Transmit() and Receive()will be aborted.
602 NOTE: It has not been implemented currently.
604 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
605 @param Token Pointer to a token that has been issued by
606 Connect(), Accept(), Transmit() or Receive(). If
607 NULL, all pending tokens issued by above four
608 functions will be aborted.
610 @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event
612 @retval EFI_INVALID_PARAMETER This is NULL.
613 @retval EFI_NOT_STARTED This instance hasn's been configured.
614 @retval EFI_NO_MAPPING When using the default address, configuration
615 (DHCP, BOOTP,RARP, etc.) hasn's finished yet.
616 @retval EFI_NOT_FOUND The asynchronous I/O request isn's found in the
617 transmission or receive queue. It has either
618 completed or wasn's issued by Transmit() and Receive().
619 @retval EFI_UNSUPPORTED The operation is not supported in current
626 IN EFI_TCP4_PROTOCOL
*This
,
627 IN EFI_TCP4_COMPLETION_TOKEN
*Token OPTIONAL
630 return EFI_UNSUPPORTED
;
635 Poll to receive incoming data and transmit outgoing segments.
637 The Poll() function increases the rate that data is moved between the network
638 and application and can be called when the TCP instance is created successfully.
639 Its use is optional. In some implementations, the periodical timer in the MNP
640 driver may not poll the underlying communications device fast enough to avoid
641 drop packets. Drivers and applications that are experiencing packet loss should
642 try calling the Poll() function in a high frequency.
644 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
646 @retval EFI_SUCCESS Incoming or outgoing data was processed.
647 @retval EFI_INVALID_PARAMETER This is NULL.
648 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
649 @retval EFI_NOT_READY No incoming or outgoing data was processed.
650 @retval EFI_TIMEOUT Data was dropped out of the transmission or
651 receive queue. Consider increasing the polling
658 IN EFI_TCP4_PROTOCOL
*This
665 return EFI_INVALID_PARAMETER
;
668 Sock
= SOCK_FROM_THIS (This
);
670 Status
= Sock
->ProtoHandler (Sock
, SOCK_POLL
, NULL
);