2 Implementation of TCP4 protocol services.
4 Copyright (c) 2005 - 2017, 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 (IP4_IS_LOCAL_BROADCAST (NTOHL (Ip
))) {
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 (!IP4_IS_VALID_NETMASK (NTOHL (SubnetMask
)) ||
187 (SubnetMask
!= 0 && !NetIp4IsUnicast (NTOHL (Ip
), NTOHL (SubnetMask
)))) {
188 return EFI_INVALID_PARAMETER
;
192 Option
= TcpConfigData
->ControlOption
;
193 if ((NULL
!= Option
) &&
194 (Option
->EnableSelectiveAck
|| Option
->EnablePathMtuDiscovery
)) {
195 return EFI_UNSUPPORTED
;
199 Sock
= SOCK_FROM_THIS (This
);
201 if (NULL
== TcpConfigData
) {
202 return SockFlush (Sock
);
205 Status
= SockConfigure (Sock
, TcpConfigData
);
207 if (EFI_NO_MAPPING
== Status
) {
208 Sock
->ConfigureState
= SO_NO_MAPPING
;
216 Add or delete routing entries.
218 The Routes() function adds or deletes a route from the instance's routing table.
219 The most specific route is selected by comparing the SubnetAddress with the
220 destination IP address's arithmetical AND to the SubnetMask.
221 The default route is added with both SubnetAddress and SubnetMask set to 0.0.0.0.
222 The default route matches all destination IP addresses if there is no more specific route.
223 Direct route is added with GatewayAddress set to 0.0.0.0. Packets are sent to
224 the destination host if its address can be found in the Address Resolution Protocol (ARP)
225 cache or it is on the local subnet. If the instance is configured to use default
226 address, a direct route to the local network will be added automatically.
227 Each TCP instance has its own independent routing table. Instance that uses the
228 default IP address will have a copy of the EFI_IP4_CONFIG_PROTOCOL's routing table.
229 The copy will be updated automatically whenever the IP driver reconfigures its
230 instance. As a result, the previous modification to the instance's local copy
231 will be lost. The priority of checking the route table is specific with IP
232 implementation and every IP implementation must comply with RFC 1122.
234 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
235 @param DeleteRoute If TRUE, delete the specified route from routing
236 table; if FALSE, add the specified route to
238 DestinationAddress and SubnetMask are used as
239 the keywords to search route entry.
240 @param SubnetAddress The destination network.
241 @param SubnetMask The subnet mask for the destination network.
242 @param GatewayAddress The gateway address for this route.
243 It must be on the same subnet with the station
244 address unless a direct route is specified.
246 @retval EFI_SUCCESS The operation completed successfully.
247 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance has not been
249 @retval EFI_NO_MAPPING When using a default address, configuration
250 (through DHCP, BOOTP, RARP, etc.) is not
252 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
253 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to add the
254 entry to the routing table.
255 @retval EFI_NOT_FOUND This route is not in the routing table.
256 @retval EFI_ACCESS_DENIED This route is already in the routing table.
257 @retval EFI_UNSUPPORTED The TCP driver does not support this operation.
263 IN EFI_TCP4_PROTOCOL
*This
,
264 IN BOOLEAN DeleteRoute
,
265 IN EFI_IPv4_ADDRESS
*SubnetAddress
,
266 IN EFI_IPv4_ADDRESS
*SubnetMask
,
267 IN EFI_IPv4_ADDRESS
*GatewayAddress
271 TCP4_ROUTE_INFO RouteInfo
;
274 return EFI_INVALID_PARAMETER
;
277 Sock
= SOCK_FROM_THIS (This
);
279 RouteInfo
.DeleteRoute
= DeleteRoute
;
280 RouteInfo
.SubnetAddress
= SubnetAddress
;
281 RouteInfo
.SubnetMask
= SubnetMask
;
282 RouteInfo
.GatewayAddress
= GatewayAddress
;
284 return SockRoute (Sock
, &RouteInfo
);
289 Initiate a nonblocking TCP connection request for an active TCP instance.
291 The Connect() function will initiate an active open to the remote peer configured
292 in current TCP instance if it is configured active. If the connection succeeds
293 or fails due to any error, the ConnectionToken->CompletionToken.Event will be
294 signaled and ConnectionToken->CompletionToken.Status will be updated accordingly.
295 This function can only be called for the TCP instance in Tcp4StateClosed state.
296 The instance will transfer into Tcp4StateSynSent if the function returns EFI_SUCCESS.
297 If TCP three way handshake succeeds, its state will become Tcp4StateEstablished,
298 otherwise, the state will return to Tcp4StateClosed.
300 @param This Pointer to the EFI_TCP4_PROTOCOL instance
301 @param ConnectionToken Pointer to the connection token to return when
302 the TCP three way handshake finishes.
304 @retval EFI_SUCCESS The connection request is successfully initiated
305 and the state of this TCPv4 instance has
306 been changed to Tcp4StateSynSent.
307 @retval EFI_NOT_STARTED This EFI_TCP4_PROTOCOL instance hasn't been
309 @retval EFI_ACCESS_DENIED The instance is not configured as an active one
310 or it is not in Tcp4StateClosed state.
311 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
312 @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resource to
313 initiate the active open.
314 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
320 IN EFI_TCP4_PROTOCOL
*This
,
321 IN EFI_TCP4_CONNECTION_TOKEN
*ConnectionToken
327 NULL
== ConnectionToken
||
328 NULL
== ConnectionToken
->CompletionToken
.Event
) {
329 return EFI_INVALID_PARAMETER
;
332 Sock
= SOCK_FROM_THIS (This
);
334 return SockConnect (Sock
, ConnectionToken
);
339 Listen on the passive instance to accept an incoming connection request.
341 The Accept() function initiates an asynchronous accept request to wait for an
342 incoming connection on the passive TCP instance. If a remote peer successfully
343 establishes a connection with this instance, a new TCP instance will be created
344 and its handle will be returned in ListenToken->NewChildHandle. The newly created
345 instance is configured by inheriting the passive instance's configuration and is
346 ready for use upon return. The instance is in the Tcp4StateEstablished state.
347 The ListenToken->CompletionToken.Event will be signaled when a new connection
348 is accepted, user aborts the listen or connection is reset. This function only
349 can be called when current TCP instance is in Tcp4StateListen state.
351 @param This Pointer to the EFI_TCP4_PROTOCOL instance
352 @param ListenToken Pointer to the listen token to return when
355 @retval EFI_SUCCESS The listen token has been queued successfully.
356 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
358 @retval EFI_ACCESS_DENIED The instatnce is not a passive one or it is not
359 in Tcp4StateListen state or a same listen token
360 has already existed in the listen token queue of
362 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
363 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish
365 @retval EFI_DEVICE_ERROR Any unexpected and not belonged to above category error.
371 IN EFI_TCP4_PROTOCOL
*This
,
372 IN EFI_TCP4_LISTEN_TOKEN
*ListenToken
378 NULL
== ListenToken
||
379 NULL
== ListenToken
->CompletionToken
.Event
) {
380 return EFI_INVALID_PARAMETER
;
383 Sock
= SOCK_FROM_THIS (This
);
385 return SockAccept (Sock
, ListenToken
);
390 Queues outgoing data into the transmit queue.
392 The Transmit() function queues a sending request to this TCPv4 instance along
393 with the user data. The status of the token is updated and the event in the token
394 will be signaled once the data is sent out or some error occurs.
396 @param This Pointer to the EFI_TCP4_PROTOCOL instance
397 @param Token Pointer to the completion token to queue to the
400 @retval EFI_SUCCESS The data has been queued for transmission.
401 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
403 @retval EFI_NO_MAPPING When using a default address, configuration
404 (DHCP, BOOTP, RARP, etc.) is not finished yet.
405 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
406 @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
407 * A transmit completion token with the same
408 Token-> CompletionToken.Event was already in the
410 * The current instance is in Tcp4StateClosed state
411 * The current instance is a passive one and
412 it is in Tcp4StateListen state.
413 * User has called Close() to disconnect this
415 @retval EFI_NOT_READY The completion token could not be queued because
416 the transmit queue is full.
417 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of
419 @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or
426 IN EFI_TCP4_PROTOCOL
*This
,
427 IN EFI_TCP4_IO_TOKEN
*Token
435 NULL
== Token
->CompletionToken
.Event
||
436 NULL
== Token
->Packet
.TxData
||
437 0 == Token
->Packet
.TxData
->FragmentCount
||
438 0 == Token
->Packet
.TxData
->DataLength
440 return EFI_INVALID_PARAMETER
;
443 Status
= Tcp4ChkDataBuf (
444 (UINT32
) Token
->Packet
.TxData
->DataLength
,
445 (UINT32
) Token
->Packet
.TxData
->FragmentCount
,
446 Token
->Packet
.TxData
->FragmentTable
448 if (EFI_ERROR (Status
)) {
452 Sock
= SOCK_FROM_THIS (This
);
454 return SockSend (Sock
, Token
);
460 Place an asynchronous receive request into the receiving queue.
462 The Receive() function places a completion token into the receive packet queue.
463 This function is always asynchronous. The caller must allocate the
464 Token->CompletionToken.Event and the FragmentBuffer used to receive data. He also
465 must fill the DataLength which represents the whole length of all FragmentBuffer.
466 When the receive operation completes, the EFI TCPv4 Protocol driver updates the
467 Token->CompletionToken.Status and Token->Packet.RxData fields and the
468 Token->CompletionToken.Event is signaled. If got data the data and its length
469 will be copy into the FragmentTable, in the same time the full length of received
470 data will be recorded in the DataLength fields. Providing a proper notification
471 function and context for the event will enable the user to receive the notification
472 and receiving status. That notification function is guaranteed to not be re-entered.
474 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
475 @param Token Pointer to a token that is associated with the
476 receive data descriptor.
478 @retval EFI_SUCCESS The receive completion token was cached.
479 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
481 @retval EFI_NO_MAPPING When using a default address, configuration
482 (DHCP, BOOTP, RARP, etc.) is not finished yet.
483 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
484 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued
485 due to a lack of system resources.
486 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
487 The EFI TCPv4 Protocol instance has been reset
489 @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
490 * A receive completion token with the same
491 Token->CompletionToken.Event was already in
493 * The current instance is in Tcp4StateClosed state.
494 * The current instance is a passive one and it
495 is in Tcp4StateListen state.
496 * User has called Close() to disconnect this
498 @retval EFI_CONNECTION_FIN The communication peer has closed the connection
499 and there is no any buffered data in the receive
500 buffer of this instance.
501 @retval EFI_NOT_READY The receive request could not be queued because
502 the receive queue is full.
508 IN EFI_TCP4_PROTOCOL
*This
,
509 IN EFI_TCP4_IO_TOKEN
*Token
517 NULL
== Token
->CompletionToken
.Event
||
518 NULL
== Token
->Packet
.RxData
||
519 0 == Token
->Packet
.RxData
->FragmentCount
||
520 0 == Token
->Packet
.RxData
->DataLength
522 return EFI_INVALID_PARAMETER
;
525 Status
= Tcp4ChkDataBuf (
526 (UINT32
) Token
->Packet
.RxData
->DataLength
,
527 (UINT32
) Token
->Packet
.RxData
->FragmentCount
,
528 Token
->Packet
.RxData
->FragmentTable
530 if (EFI_ERROR (Status
)) {
534 Sock
= SOCK_FROM_THIS (This
);
536 return SockRcv (Sock
, Token
);
542 Disconnecting a TCP connection gracefully or reset a TCP connection.
544 Initiate an asynchronous close token to TCP driver. After Close() is called,
545 any buffered transmission data will be sent by TCP driver and the current
546 instance will have a graceful close working flow described as RFC 793 if
547 AbortOnClose is set to FALSE, otherwise, a rest packet will be sent by TCP
548 driver to fast disconnect this connection. When the close operation completes
549 successfully the TCP instance is in Tcp4StateClosed state, all pending
550 asynchronous operation is signaled and any buffers used for TCP network traffic
553 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
554 @param CloseToken Pointer to the close token to return when
557 @retval EFI_SUCCESS The operation completed successfully.
558 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
560 @retval EFI_ACCESS_DENIED One or more of the following are TRUE:
561 * Configure() has been called with TcpConfigData
562 set to NULL and this function has not returned.
563 * Previous Close() call on this instance has not
565 @retval EFI_INVALID_PARAMETER One ore more parameters are invalid.
566 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resource to finish the
568 @retval EFI_DEVICE_ERROR Any unexpected and not belonged to above
575 IN EFI_TCP4_PROTOCOL
*This
,
576 IN EFI_TCP4_CLOSE_TOKEN
*CloseToken
582 NULL
== CloseToken
||
583 NULL
== CloseToken
->CompletionToken
.Event
) {
584 return EFI_INVALID_PARAMETER
;
587 Sock
= SOCK_FROM_THIS (This
);
589 return SockClose (Sock
, CloseToken
, CloseToken
->AbortOnClose
);
594 Abort an asynchronous connection, listen, transmission or receive request.
596 The Cancel() function aborts a pending connection, listen, transmit or receive
597 request. If Token is not NULL and the token is in the connection, listen,
598 transmission or receive queue when it is being cancelled, its Token->Status
599 will be set to EFI_ABORTED and then Token->Event will be signaled. If the token
600 is not in one of the queues, which usually means that the asynchronous operation
601 has completed, EFI_NOT_FOUND is returned. If Token is NULL all asynchronous token
602 issued by Connect(), Accept(), Transmit() and Receive()will be aborted.
603 NOTE: It has not been implemented currently.
605 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
606 @param Token Pointer to a token that has been issued by
607 Connect(), Accept(), Transmit() or Receive(). If
608 NULL, all pending tokens issued by above four
609 functions will be aborted.
611 @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event
613 @retval EFI_INVALID_PARAMETER This is NULL.
614 @retval EFI_NOT_STARTED This instance hasn's been configured.
615 @retval EFI_NO_MAPPING When using the default address, configuration
616 (DHCP, BOOTP,RARP, etc.) hasn's finished yet.
617 @retval EFI_NOT_FOUND The asynchronous I/O request isn's found in the
618 transmission or receive queue. It has either
619 completed or wasn's issued by Transmit() and Receive().
620 @retval EFI_UNSUPPORTED The operation is not supported in current
627 IN EFI_TCP4_PROTOCOL
*This
,
628 IN EFI_TCP4_COMPLETION_TOKEN
*Token OPTIONAL
631 return EFI_UNSUPPORTED
;
636 Poll to receive incoming data and transmit outgoing segments.
638 The Poll() function increases the rate that data is moved between the network
639 and application and can be called when the TCP instance is created successfully.
640 Its use is optional. In some implementations, the periodical timer in the MNP
641 driver may not poll the underlying communications device fast enough to avoid
642 drop packets. Drivers and applications that are experiencing packet loss should
643 try calling the Poll() function in a high frequency.
645 @param This Pointer to the EFI_TCP4_PROTOCOL instance.
647 @retval EFI_SUCCESS Incoming or outgoing data was processed.
648 @retval EFI_INVALID_PARAMETER This is NULL.
649 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
650 @retval EFI_NOT_READY No incoming or outgoing data was processed.
651 @retval EFI_TIMEOUT Data was dropped out of the transmission or
652 receive queue. Consider increasing the polling
659 IN EFI_TCP4_PROTOCOL
*This
666 return EFI_INVALID_PARAMETER
;
669 Sock
= SOCK_FROM_THIS (This
);
671 Status
= Sock
->ProtoHandler (Sock
, SOCK_POLL
, NULL
);