]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/TcpDxe/TcpMain.c
NetworkPkg: Fix potential ASSERT if NetIp4IsUnicast is called
[mirror_edk2.git] / NetworkPkg / TcpDxe / TcpMain.c
1 /** @file
2 Implementation of EFI_TCP4_PROTOCOL and EFI_TCP6_PROTOCOL.
3
4 (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "TcpMain.h"
18
19 /**
20 Check the integrity of the data buffer.
21
22 @param[in] DataLen The total length of the data buffer.
23 @param[in] FragmentCount The fragment count of the fragment table.
24 @param[in] FragmentTable Pointer to the fragment table of the data
25 buffer.
26
27 @retval EFI_SUCCESS The integrity check passed.
28 @retval EFI_INVALID_PARAMETER The integrity check failed.
29
30 **/
31 EFI_STATUS
32 TcpChkDataBuf (
33 IN UINT32 DataLen,
34 IN UINT32 FragmentCount,
35 IN EFI_TCP4_FRAGMENT_DATA *FragmentTable
36 )
37 {
38 UINT32 Index;
39
40 UINT32 Len;
41
42 for (Index = 0, Len = 0; Index < FragmentCount; Index++) {
43 Len = Len + FragmentTable[Index].FragmentLength;
44 }
45
46 if (DataLen != Len) {
47 return EFI_INVALID_PARAMETER;
48 }
49
50 return EFI_SUCCESS;
51 }
52
53 /**
54 Get the current operational status.
55
56 @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
57 @param[out] Tcp4State Pointer to the buffer to receive the current TCP
58 state. Optional parameter that may be NULL.
59 @param[out] Tcp4ConfigData Pointer to the buffer to receive the current TCP
60 configuration. Optional parameter that may be NULL.
61 @param[out] Ip4ModeData Pointer to the buffer to receive the current
62 IPv4 configuration. Optional parameter that may be NULL.
63 @param[out] MnpConfigData Pointer to the buffer to receive the current MNP
64 configuration data indirectly used by the TCPv4
65 Instance. Optional parameter that may be NULL.
66 @param[out] SnpModeData Pointer to the buffer to receive the current SNP
67 configuration data indirectly used by the TCPv4
68 Instance. Optional parameter that may be NULL.
69
70 @retval EFI_SUCCESS The mode data was read.
71 @retval EFI_NOT_STARTED No configuration data is available because this
72 instance hasn't been started.
73 @retval EFI_INVALID_PARAMETER This is NULL.
74
75 **/
76 EFI_STATUS
77 EFIAPI
78 Tcp4GetModeData (
79 IN EFI_TCP4_PROTOCOL *This,
80 OUT EFI_TCP4_CONNECTION_STATE *Tcp4State OPTIONAL,
81 OUT EFI_TCP4_CONFIG_DATA *Tcp4ConfigData OPTIONAL,
82 OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,
83 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
84 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
85 )
86 {
87 TCP4_MODE_DATA TcpMode;
88 SOCKET *Sock;
89
90 if (NULL == This) {
91 return EFI_INVALID_PARAMETER;
92 }
93
94 Sock = SOCK_FROM_THIS (This);
95
96 TcpMode.Tcp4State = Tcp4State;
97 TcpMode.Tcp4ConfigData = Tcp4ConfigData;
98 TcpMode.Ip4ModeData = Ip4ModeData;
99 TcpMode.MnpConfigData = MnpConfigData;
100 TcpMode.SnpModeData = SnpModeData;
101
102 return SockGetMode (Sock, &TcpMode);
103 }
104
105 /**
106 Initialize or brutally reset the operational parameters for
107 this EFI TCPv4 instance.
108
109 @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
110 @param[in] TcpConfigData Pointer to the configure data to configure the
111 instance. Optional parameter that may be NULL.
112
113 @retval EFI_SUCCESS The operational settings were set, changed, or
114 reset successfully.
115 @retval EFI_NO_MAPPING When using a default address, configuration
116 (through DHCP, BOOTP, RARP, etc.) is not
117 finished.
118 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
119 @retval EFI_ACCESS_DENIED Configuring TCP instance when it is already
120 configured.
121 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
122 @retval EFI_UNSUPPORTED One or more of the control options are not
123 supported in the implementation.
124 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
125
126 **/
127 EFI_STATUS
128 EFIAPI
129 Tcp4Configure (
130 IN EFI_TCP4_PROTOCOL * This,
131 IN EFI_TCP4_CONFIG_DATA * TcpConfigData OPTIONAL
132 )
133 {
134 EFI_TCP4_OPTION *Option;
135 SOCKET *Sock;
136 EFI_STATUS Status;
137 IP4_ADDR Ip;
138 IP4_ADDR SubnetMask;
139
140 if (NULL == This) {
141 return EFI_INVALID_PARAMETER;
142 }
143
144 //
145 // Tcp protocol related parameter check will be conducted here
146 //
147 if (NULL != TcpConfigData) {
148
149 CopyMem (&Ip, &TcpConfigData->AccessPoint.RemoteAddress, sizeof (IP4_ADDR));
150 if (IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) {
151 return EFI_INVALID_PARAMETER;
152 }
153
154 if (TcpConfigData->AccessPoint.ActiveFlag && (0 == TcpConfigData->AccessPoint.RemotePort || (Ip == 0))) {
155 return EFI_INVALID_PARAMETER;
156 }
157
158 if (!TcpConfigData->AccessPoint.UseDefaultAddress) {
159
160 CopyMem (&Ip, &TcpConfigData->AccessPoint.StationAddress, sizeof (IP4_ADDR));
161 CopyMem (&SubnetMask, &TcpConfigData->AccessPoint.SubnetMask, sizeof (IP4_ADDR));
162 if (!IP4_IS_VALID_NETMASK (NTOHL (SubnetMask)) ||
163 (SubnetMask != 0 && !NetIp4IsUnicast (NTOHL (Ip), NTOHL (SubnetMask)))) {
164 return EFI_INVALID_PARAMETER;
165 }
166 }
167
168 Option = TcpConfigData->ControlOption;
169 if ((NULL != Option) && (Option->EnableSelectiveAck || Option->EnablePathMtuDiscovery)) {
170 return EFI_UNSUPPORTED;
171 }
172 }
173
174 Sock = SOCK_FROM_THIS (This);
175
176 if (NULL == TcpConfigData) {
177 return SockFlush (Sock);
178 }
179
180 Status = SockConfigure (Sock, TcpConfigData);
181
182 if (EFI_NO_MAPPING == Status) {
183 Sock->ConfigureState = SO_NO_MAPPING;
184 }
185
186 return Status;
187 }
188
189 /**
190 Add or delete routing entries.
191
192 @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
193 @param[in] DeleteRoute If TRUE, delete the specified route from routing
194 table; if FALSE, add the specified route to
195 routing table.
196 @param[in] SubnetAddress The destination network.
197 @param[in] SubnetMask The subnet mask for the destination network.
198 @param[in] GatewayAddress The gateway address for this route.
199
200 @retval EFI_SUCCESS The operation completed successfully.
201 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance has not been
202 configured.
203 @retval EFI_NO_MAPPING When using a default address, configuration
204 (through DHCP, BOOTP, RARP, etc.) is not
205 finished.
206 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
207 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to add the
208 entry to the routing table.
209 @retval EFI_NOT_FOUND This route is not in the routing table.
210 @retval EFI_ACCESS_DENIED This route is already in the routing table.
211 @retval EFI_UNSUPPORTED The TCP driver does not support this operation.
212
213 **/
214 EFI_STATUS
215 EFIAPI
216 Tcp4Routes (
217 IN EFI_TCP4_PROTOCOL *This,
218 IN BOOLEAN DeleteRoute,
219 IN EFI_IPv4_ADDRESS *SubnetAddress,
220 IN EFI_IPv4_ADDRESS *SubnetMask,
221 IN EFI_IPv4_ADDRESS *GatewayAddress
222 )
223 {
224 SOCKET *Sock;
225 TCP4_ROUTE_INFO RouteInfo;
226
227 if (NULL == This) {
228 return EFI_INVALID_PARAMETER;
229 }
230
231 Sock = SOCK_FROM_THIS (This);
232
233 RouteInfo.DeleteRoute = DeleteRoute;
234 RouteInfo.SubnetAddress = SubnetAddress;
235 RouteInfo.SubnetMask = SubnetMask;
236 RouteInfo.GatewayAddress = GatewayAddress;
237
238 return SockRoute (Sock, &RouteInfo);
239 }
240
241 /**
242 Initiate a non-blocking TCP connection request for an active TCP instance.
243
244 @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
245 @param[in] ConnectionToken Pointer to the connection token to return when
246 the TCP three way handshake finishes.
247
248 @retval EFI_SUCCESS The connection request successfully
249 initiated.
250 @retval EFI_NOT_STARTED This EFI_TCP4_PROTOCOL instance hasn't been
251 configured.
252 @retval EFI_ACCESS_DENIED The instance is not configured as an active one,
253 or it is not in Tcp4StateClosed state.
254 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
255 @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resources to
256 initiate the active open.
257 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
258
259 **/
260 EFI_STATUS
261 EFIAPI
262 Tcp4Connect (
263 IN EFI_TCP4_PROTOCOL *This,
264 IN EFI_TCP4_CONNECTION_TOKEN *ConnectionToken
265 )
266 {
267 SOCKET *Sock;
268
269 if (NULL == This || NULL == ConnectionToken || NULL == ConnectionToken->CompletionToken.Event) {
270 return EFI_INVALID_PARAMETER;
271 }
272
273 Sock = SOCK_FROM_THIS (This);
274
275 return SockConnect (Sock, ConnectionToken);
276 }
277
278 /**
279 Listen on the passive instance to accept an incoming connection request.
280
281 @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
282 @param[in] ListenToken Pointer to the listen token to return when
283 operation finishes.
284
285 @retval EFI_SUCCESS The listen token was queued successfully.
286 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
287 configured.
288 @retval EFI_ACCESS_DENIED The instatnce is not a passive one or it is not
289 in Tcp4StateListen state or a same listen token
290 has already existed in the listen token queue of
291 this TCP instance.
292 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
293 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish
294 the operation.
295 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
296
297 **/
298 EFI_STATUS
299 EFIAPI
300 Tcp4Accept (
301 IN EFI_TCP4_PROTOCOL *This,
302 IN EFI_TCP4_LISTEN_TOKEN *ListenToken
303 )
304 {
305 SOCKET *Sock;
306
307 if (NULL == This || NULL == ListenToken || NULL == ListenToken->CompletionToken.Event) {
308 return EFI_INVALID_PARAMETER;
309 }
310
311 Sock = SOCK_FROM_THIS (This);
312
313 return SockAccept (Sock, ListenToken);
314 }
315
316 /**
317 Queues outgoing data into the transmit queue
318
319 @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
320 @param[in] Token Pointer to the completion token to queue to the
321 transmit queue.
322
323 @retval EFI_SUCCESS The data has been queued for transmission.
324 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
325 configured.
326 @retval EFI_NO_MAPPING When using a default address, configuration
327 (DHCP, BOOTP, RARP, etc.) is not finished yet.
328 @retval EFI_INVALID_PARAMETER One or more parameters are invalid
329 @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
330 * A transmit completion token with the same
331 Token-> CompletionToken.Event was already in the
332 transmission queue. * The current instance is in
333 Tcp4StateClosed state. * The current instance is
334 a passive one and it is in Tcp4StateListen
335 state. * User has called Close() to disconnect
336 this connection.
337 @retval EFI_NOT_READY The completion token could not be queued because
338 the transmit queue is full.
339 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of a
340 resource shortage.
341 @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or
342 address.
343
344 **/
345 EFI_STATUS
346 EFIAPI
347 Tcp4Transmit (
348 IN EFI_TCP4_PROTOCOL *This,
349 IN EFI_TCP4_IO_TOKEN *Token
350 )
351 {
352 SOCKET *Sock;
353 EFI_STATUS Status;
354
355 if (NULL == This ||
356 NULL == Token ||
357 NULL == Token->CompletionToken.Event ||
358 NULL == Token->Packet.TxData ||
359 0 == Token->Packet.TxData->FragmentCount ||
360 0 == Token->Packet.TxData->DataLength
361 ) {
362 return EFI_INVALID_PARAMETER;
363 }
364
365 Status = TcpChkDataBuf (
366 Token->Packet.TxData->DataLength,
367 Token->Packet.TxData->FragmentCount,
368 Token->Packet.TxData->FragmentTable
369 );
370 if (EFI_ERROR (Status)) {
371 return Status;
372 }
373
374 Sock = SOCK_FROM_THIS (This);
375
376 return SockSend (Sock, Token);
377 }
378
379 /**
380 Place an asynchronous receive request into the receiving queue.
381
382 @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
383 @param[in] Token Pointer to a token that is associated with the
384 receive data descriptor.
385
386 @retval EFI_SUCCESS The receive completion token was cached
387 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
388 configured.
389 @retval EFI_NO_MAPPING When using a default address, configuration
390 (DHCP, BOOTP, RARP, etc.) is not finished yet.
391 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
392 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued
393 due to a lack of system resources.
394 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
395 @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
396 * A receive completion token with the same
397 Token->CompletionToken.Event was already in the
398 receive queue. * The current instance is in
399 Tcp4StateClosed state. * The current instance is
400 a passive one and it is in Tcp4StateListen
401 state. * User has called Close() to disconnect
402 this connection.
403 @retval EFI_CONNECTION_FIN The communication peer has closed the connection,
404 and there is no any buffered data in the receive
405 buffer of this instance.
406 @retval EFI_NOT_READY The receive request could not be queued because
407 the receive queue is full.
408
409 **/
410 EFI_STATUS
411 EFIAPI
412 Tcp4Receive (
413 IN EFI_TCP4_PROTOCOL *This,
414 IN EFI_TCP4_IO_TOKEN *Token
415 )
416 {
417 SOCKET *Sock;
418 EFI_STATUS Status;
419
420 if (NULL == This ||
421 NULL == Token ||
422 NULL == Token->CompletionToken.Event ||
423 NULL == Token->Packet.RxData ||
424 0 == Token->Packet.RxData->FragmentCount ||
425 0 == Token->Packet.RxData->DataLength
426 ) {
427 return EFI_INVALID_PARAMETER;
428 }
429
430 Status = TcpChkDataBuf (
431 Token->Packet.RxData->DataLength,
432 Token->Packet.RxData->FragmentCount,
433 Token->Packet.RxData->FragmentTable
434 );
435 if (EFI_ERROR (Status)) {
436 return Status;
437 }
438
439 Sock = SOCK_FROM_THIS (This);
440
441 return SockRcv (Sock, Token);
442
443 }
444
445 /**
446 Disconnecting a TCP connection gracefully or reset a TCP connection.
447
448 @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
449 @param[in] CloseToken Pointer to the close token to return when
450 operation finishes.
451
452 @retval EFI_SUCCESS The operation completed successfully.
453 @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
454 configured.
455 @retval EFI_ACCESS_DENIED One or more of the following are TRUE: *
456 Configure() has been called with TcpConfigData
457 set to NULL, and this function has not returned.
458 * Previous Close() call on this instance has not
459 finished.
460 @retval EFI_INVALID_PARAMETER One ore more parameters are invalid.
461 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish the
462 operation.
463 @retval EFI_DEVICE_ERROR Any unexpected category error not belonging to those
464 listed above.
465
466 **/
467 EFI_STATUS
468 EFIAPI
469 Tcp4Close (
470 IN EFI_TCP4_PROTOCOL *This,
471 IN EFI_TCP4_CLOSE_TOKEN *CloseToken
472 )
473 {
474 SOCKET *Sock;
475
476 if (NULL == This || NULL == CloseToken || NULL == CloseToken->CompletionToken.Event) {
477 return EFI_INVALID_PARAMETER;
478 }
479
480 Sock = SOCK_FROM_THIS (This);
481
482 return SockClose (Sock, CloseToken, CloseToken->AbortOnClose);
483 }
484
485 /**
486 Abort an asynchronous connection, listen, transmission or receive request.
487
488 @param This The pointer to the EFI_TCP4_PROTOCOL instance.
489 @param Token The pointer to a token that has been issued by
490 EFI_TCP4_PROTOCOL.Connect(),
491 EFI_TCP4_PROTOCOL.Accept(),
492 EFI_TCP4_PROTOCOL.Transmit() or
493 EFI_TCP4_PROTOCOL.Receive(). If NULL, all pending
494 tokens issued by above four functions will be aborted. Type
495 EFI_TCP4_COMPLETION_TOKEN is defined in
496 EFI_TCP4_PROTOCOL.Connect().
497
498 @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event
499 is signaled.
500 @retval EFI_INVALID_PARAMETER This is NULL.
501 @retval EFI_NOT_STARTED This instance hasn't been configured.
502 @retval EFI_NO_MAPPING When using the default address, configuration
503 (DHCP, BOOTP,RARP, etc.) hasn't finished yet.
504 @retval EFI_NOT_FOUND The asynchronous I/O request isn't found in the
505 transmission or receive queue. It has either
506 completed or wasn't issued by Transmit() and Receive().
507
508 **/
509 EFI_STATUS
510 EFIAPI
511 Tcp4Cancel (
512 IN EFI_TCP4_PROTOCOL *This,
513 IN EFI_TCP4_COMPLETION_TOKEN *Token OPTIONAL
514 )
515 {
516 SOCKET *Sock;
517
518 if (NULL == This) {
519 return EFI_INVALID_PARAMETER;
520 }
521
522 Sock = SOCK_FROM_THIS (This);
523
524 return SockCancel (Sock, Token);
525 }
526
527 /**
528 Poll to receive incoming data and transmit outgoing segments.
529
530 @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
531
532 @retval EFI_SUCCESS Incoming or outgoing data was processed.
533 @retval EFI_INVALID_PARAMETER This is NULL.
534 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
535 @retval EFI_NOT_READY No incoming or outgoing data was processed.
536 @retval EFI_TIMEOUT Data was dropped out of the transmission or
537 receive queue. Consider increasing the polling
538 rate.
539
540 **/
541 EFI_STATUS
542 EFIAPI
543 Tcp4Poll (
544 IN EFI_TCP4_PROTOCOL *This
545 )
546 {
547 SOCKET *Sock;
548 EFI_STATUS Status;
549
550 if (NULL == This) {
551 return EFI_INVALID_PARAMETER;
552 }
553
554 Sock = SOCK_FROM_THIS (This);
555
556 Status = Sock->ProtoHandler (Sock, SOCK_POLL, NULL);
557
558 return Status;
559 }
560
561 /**
562 Get the current operational status.
563
564 The GetModeData() function copies the current operational settings of this EFI TCPv6
565 Protocol instance into user-supplied buffers. This function can also be used to retrieve
566 the operational setting of underlying drivers such as IPv6, MNP, or SNP.
567
568 @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
569 @param[out] Tcp6State The buffer in which the current TCP state is
570 returned. Optional parameter that may be NULL.
571 @param[out] Tcp6ConfigData The buffer in which the current TCP configuration
572 is returned. Optional parameter that may be NULL.
573 @param[out] Ip6ModeData The buffer in which the current IPv6 configuration
574 data used by the TCP instance is returned.
575 Optional parameter that may be NULL.
576 @param[out] MnpConfigData The buffer in which the current MNP configuration
577 data indirectly used by the TCP instance is returned.
578 Optional parameter that may be NULL.
579 @param[out] SnpModeData The buffer in which the current SNP mode data
580 indirectly used by the TCP instance is returned.
581 Optional parameter that may be NULL.
582
583 @retval EFI_SUCCESS The mode data was read.
584 @retval EFI_NOT_STARTED No configuration data is available because this instance hasn't
585 been started.
586 @retval EFI_INVALID_PARAMETER This is NULL.
587
588 **/
589 EFI_STATUS
590 EFIAPI
591 Tcp6GetModeData (
592 IN EFI_TCP6_PROTOCOL *This,
593 OUT EFI_TCP6_CONNECTION_STATE *Tcp6State OPTIONAL,
594 OUT EFI_TCP6_CONFIG_DATA *Tcp6ConfigData OPTIONAL,
595 OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL,
596 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
597 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
598 )
599 {
600 TCP6_MODE_DATA TcpMode;
601 SOCKET *Sock;
602
603 if (NULL == This) {
604 return EFI_INVALID_PARAMETER;
605 }
606
607 Sock = SOCK_FROM_THIS (This);
608
609 TcpMode.Tcp6State = Tcp6State;
610 TcpMode.Tcp6ConfigData = Tcp6ConfigData;
611 TcpMode.Ip6ModeData = Ip6ModeData;
612 TcpMode.MnpConfigData = MnpConfigData;
613 TcpMode.SnpModeData = SnpModeData;
614
615 return SockGetMode (Sock, &TcpMode);
616 }
617
618 /**
619 Initialize or brutally reset the operational parameters for this EFI TCPv6 instance.
620
621 The Configure() function does the following:
622 - Initialize this TCP instance, i.e., initialize the communication end settings and
623 specify active open or passive open for an instance.
624 - Reset this TCP instance brutally, i.e., cancel all pending asynchronous tokens, flush
625 transmission and receiving buffer directly without informing the communication peer.
626
627 No other TCPv6 Protocol operation except Poll() can be executed by this instance until
628 it is configured properly. For an active TCP instance, after a proper configuration it
629 may call Connect() to initiate a three-way handshake. For a passive TCP instance,
630 its state transits to Tcp6StateListen after configuration, and Accept() may be
631 called to listen the incoming TCP connection requests. If Tcp6ConfigData is set to NULL,
632 the instance is reset. The resetting process will be done brutally, the state machine will
633 be set to Tcp6StateClosed directly, the receive queue and transmit queue will be flushed,
634 and no traffic is allowed through this instance.
635
636 @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
637 @param[in] Tcp6ConfigData Pointer to the configure data to configure the instance.
638 If Tcp6ConfigData is set to NULL, the instance is reset.
639
640 @retval EFI_SUCCESS The operational settings were set, changed, or reset
641 successfully.
642 @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source
643 address for this instance, but no source address was available for
644 use.
645 @retval EFI_INVALID_PARAMETER One or more of the following conditions are TRUE:
646 - This is NULL.
647 - Tcp6ConfigData->AccessPoint.StationAddress is neither zero nor
648 one of the configured IP addresses in the underlying IPv6 driver.
649 - Tcp6ConfigData->AccessPoint.RemoteAddress isn't a valid unicast
650 IPv6 address.
651 - Tcp6ConfigData->AccessPoint.RemoteAddress is zero or
652 Tcp6ConfigData->AccessPoint.RemotePort is zero when
653 Tcp6ConfigData->AccessPoint.ActiveFlag is TRUE.
654 - A same access point has been configured in other TCP
655 instance properly.
656 @retval EFI_ACCESS_DENIED Configuring a TCP instance when it is configured without
657 calling Configure() with NULL to reset it.
658 @retval EFI_UNSUPPORTED One or more of the control options are not supported in
659 the implementation.
660 @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
661 executing Configure().
662 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
663
664 **/
665 EFI_STATUS
666 EFIAPI
667 Tcp6Configure (
668 IN EFI_TCP6_PROTOCOL *This,
669 IN EFI_TCP6_CONFIG_DATA *Tcp6ConfigData OPTIONAL
670 )
671 {
672 EFI_TCP6_OPTION *Option;
673 SOCKET *Sock;
674 EFI_STATUS Status;
675 EFI_IPv6_ADDRESS *Ip;
676
677 if (NULL == This) {
678 return EFI_INVALID_PARAMETER;
679 }
680
681 //
682 // Tcp protocol related parameter check will be conducted here
683 //
684 if (NULL != Tcp6ConfigData) {
685
686 Ip = &Tcp6ConfigData->AccessPoint.RemoteAddress;
687 if (!NetIp6IsUnspecifiedAddr (Ip) && !NetIp6IsValidUnicast (Ip)) {
688 return EFI_INVALID_PARAMETER;
689 }
690
691 if (Tcp6ConfigData->AccessPoint.ActiveFlag &&
692 (0 == Tcp6ConfigData->AccessPoint.RemotePort || NetIp6IsUnspecifiedAddr (Ip))
693 ) {
694 return EFI_INVALID_PARAMETER;
695 }
696
697 Ip = &Tcp6ConfigData->AccessPoint.StationAddress;
698 if (!NetIp6IsUnspecifiedAddr (Ip) && !NetIp6IsValidUnicast (Ip)) {
699 return EFI_INVALID_PARAMETER;
700 }
701
702 Option = Tcp6ConfigData->ControlOption;
703 if ((NULL != Option) && (Option->EnableSelectiveAck || Option->EnablePathMtuDiscovery)) {
704 return EFI_UNSUPPORTED;
705 }
706 }
707
708 Sock = SOCK_FROM_THIS (This);
709
710 if (NULL == Tcp6ConfigData) {
711 return SockFlush (Sock);
712 }
713
714 Status = SockConfigure (Sock, Tcp6ConfigData);
715
716 if (EFI_NO_MAPPING == Status) {
717 Sock->ConfigureState = SO_NO_MAPPING;
718 }
719
720 return Status;
721 }
722
723 /**
724 Initiate a nonblocking TCP connection request for an active TCP instance.
725
726 The Connect() function will initiate an active open to the remote peer configured
727 in a current TCP instance if it is configured active. If the connection succeeds or
728 fails due to any error, the ConnectionToken->CompletionToken.Event will be signaled
729 and ConnectionToken->CompletionToken.Status will be updated accordingly. This
730 function can only be called for the TCP instance in the Tcp6StateClosed state. The
731 instance will transfer into Tcp6StateSynSent if the function returns EFI_SUCCESS.
732 If a TCP three-way handshake succeeds, its state will become Tcp6StateEstablished.
733 Otherwise, the state will return to Tcp6StateClosed.
734
735 @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
736 @param[in] ConnectionToken Pointer to the connection token to return when the TCP three
737 way handshake finishes.
738
739 @retval EFI_SUCCESS The connection request successfully initiated and the state of
740 this TCP instance has been changed to Tcp6StateSynSent.
741 @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
742 @retval EFI_ACCESS_DENIED One or more of the following conditions are TRUE:
743 - This instance is not configured as an active one.
744 - This instance is not in Tcp6StateClosed state.
745 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
746 - This is NULL.
747 - ConnectionToken is NULL.
748 - ConnectionToken->CompletionToken.Event is NULL.
749 @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resources to initiate the active open.
750 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
751
752 **/
753 EFI_STATUS
754 EFIAPI
755 Tcp6Connect (
756 IN EFI_TCP6_PROTOCOL *This,
757 IN EFI_TCP6_CONNECTION_TOKEN *ConnectionToken
758 )
759 {
760 SOCKET *Sock;
761
762 if (NULL == This || NULL == ConnectionToken || NULL == ConnectionToken->CompletionToken.Event) {
763 return EFI_INVALID_PARAMETER;
764 }
765
766 Sock = SOCK_FROM_THIS (This);
767
768 return SockConnect (Sock, ConnectionToken);
769 }
770
771 /**
772 Listen on the passive instance to accept an incoming connection request. This is a
773 nonblocking operation.
774
775 The Accept() function initiates an asynchronous accept request to wait for an incoming
776 connection on the passive TCP instance. If a remote peer successfully establishes a
777 connection with this instance, a new TCP instance will be created and its handle will
778 be returned in ListenToken->NewChildHandle. The newly created instance is configured
779 by inheriting the passive instance's configuration and is ready for use upon return.
780 The new instance is in the Tcp6StateEstablished state.
781
782 The ListenToken->CompletionToken.Event will be signaled when a new connection is
783 accepted, when a user aborts the listen or when a connection is reset.
784
785 This function only can be called when a current TCP instance is in Tcp6StateListen state.
786
787 @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
788 @param[in] ListenToken Pointer to the listen token to return when operation finishes.
789
790
791 @retval EFI_SUCCESS The listen token queued successfully.
792 @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
793 @retval EFI_ACCESS_DENIED One or more of the following are TRUE:
794 - This instance is not a passive instance.
795 - This instance is not in Tcp6StateListen state.
796 - The same listen token has already existed in the listen
797 token queue of this TCP instance.
798 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
799 - This is NULL.
800 - ListenToken is NULL.
801 - ListentToken->CompletionToken.Event is NULL.
802 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resource to finish the operation.
803 @retval EFI_DEVICE_ERROR Any unexpected error not belonging to a category listed above.
804
805 **/
806 EFI_STATUS
807 EFIAPI
808 Tcp6Accept (
809 IN EFI_TCP6_PROTOCOL *This,
810 IN EFI_TCP6_LISTEN_TOKEN *ListenToken
811 )
812 {
813 SOCKET *Sock;
814
815 if (NULL == This || NULL == ListenToken || NULL == ListenToken->CompletionToken.Event) {
816 return EFI_INVALID_PARAMETER;
817 }
818
819 Sock = SOCK_FROM_THIS (This);
820
821 return SockAccept (Sock, ListenToken);
822 }
823
824 /**
825 Queues outgoing data into the transmit queue.
826
827 The Transmit() function queues a sending request to this TCP instance along with the
828 user data. The status of the token is updated and the event in the token will be
829 signaled once the data is sent out or an error occurs.
830
831 @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
832 @param[in] Token Pointer to the completion token to queue to the transmit queue.
833
834 @retval EFI_SUCCESS The data has been queued for transmission.
835 @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
836 @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a
837 source address for this instance, but no source address was
838 available for use.
839 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
840 - This is NULL.
841 - Token is NULL.
842 - Token->CompletionToken.Event is NULL.
843 - Token->Packet.TxData is NULL.
844 - Token->Packet.FragmentCount is zero.
845 - Token->Packet.DataLength is not equal to the sum of fragment lengths.
846 @retval EFI_ACCESS_DENIED One or more of the following conditions are TRUE:
847 - A transmit completion token with the same Token->
848 CompletionToken.Event was already in the
849 transmission queue.
850 - The current instance is in Tcp6StateClosed state.
851 - The current instance is a passive one and it is in
852 Tcp6StateListen state.
853 - User has called Close() to disconnect this connection.
854 @retval EFI_NOT_READY The completion token could not be queued because the
855 transmit queue is full.
856 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of resource
857 shortage.
858 @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or address.
859
860 **/
861 EFI_STATUS
862 EFIAPI
863 Tcp6Transmit (
864 IN EFI_TCP6_PROTOCOL *This,
865 IN EFI_TCP6_IO_TOKEN *Token
866 )
867 {
868 SOCKET *Sock;
869 EFI_STATUS Status;
870
871 if (NULL == This ||
872 NULL == Token ||
873 NULL == Token->CompletionToken.Event ||
874 NULL == Token->Packet.TxData ||
875 0 == Token->Packet.TxData->FragmentCount ||
876 0 == Token->Packet.TxData->DataLength
877 ) {
878 return EFI_INVALID_PARAMETER;
879 }
880
881 Status = TcpChkDataBuf (
882 Token->Packet.TxData->DataLength,
883 Token->Packet.TxData->FragmentCount,
884 (EFI_TCP4_FRAGMENT_DATA *) Token->Packet.TxData->FragmentTable
885 );
886 if (EFI_ERROR (Status)) {
887 return Status;
888 }
889
890 Sock = SOCK_FROM_THIS (This);
891
892 return SockSend (Sock, Token);
893 }
894
895 /**
896 Places an asynchronous receive request into the receiving queue.
897
898 The Receive() function places a completion token into the receive packet queue. This
899 function is always asynchronous. The caller must allocate the Token->CompletionToken.Event
900 and the FragmentBuffer used to receive data. The caller also must fill the DataLength that
901 represents the whole length of all FragmentBuffer. When the receive operation completes, the
902 EFI TCPv6 Protocol driver updates the Token->CompletionToken.Status and Token->Packet.RxData
903 fields, and the Token->CompletionToken.Event is signaled. If data obtained, the data and its length
904 will be copied into the FragmentTable; at the same time the full length of received data will
905 be recorded in the DataLength fields. Providing a proper notification function and context
906 for the event enables the user to receive the notification and receiving status. That
907 notification function is guaranteed to not be re-entered.
908
909 @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
910 @param[in] Token Pointer to a token that is associated with the receive data
911 descriptor.
912
913 @retval EFI_SUCCESS The receive completion token was cached.
914 @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
915 @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source
916 address for this instance, but no source address was available for use.
917 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
918 - This is NULL.
919 - Token is NULL.
920 - Token->CompletionToken.Event is NULL.
921 - Token->Packet.RxData is NULL.
922 - Token->Packet.RxData->DataLength is 0.
923 - The Token->Packet.RxData->DataLength is not the
924 sum of all FragmentBuffer length in FragmentTable.
925 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of
926 system resources (usually memory).
927 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
928 The EFI TCPv6 Protocol instance has been reset to startup defaults.
929 @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
930 - A receive completion token with the same Token->CompletionToken.Event
931 was already in the receive queue.
932 - The current instance is in Tcp6StateClosed state.
933 - The current instance is a passive one and it is in
934 Tcp6StateListen state.
935 - User has called Close() to disconnect this connection.
936 @retval EFI_CONNECTION_FIN The communication peer has closed the connection and there is no
937 buffered data in the receive buffer of this instance.
938 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
939
940 **/
941 EFI_STATUS
942 EFIAPI
943 Tcp6Receive (
944 IN EFI_TCP6_PROTOCOL *This,
945 IN EFI_TCP6_IO_TOKEN *Token
946 )
947 {
948 SOCKET *Sock;
949 EFI_STATUS Status;
950
951 if (NULL == This ||
952 NULL == Token ||
953 NULL == Token->CompletionToken.Event ||
954 NULL == Token->Packet.RxData ||
955 0 == Token->Packet.RxData->FragmentCount ||
956 0 == Token->Packet.RxData->DataLength
957 ) {
958 return EFI_INVALID_PARAMETER;
959 }
960
961 Status = TcpChkDataBuf (
962 Token->Packet.RxData->DataLength,
963 Token->Packet.RxData->FragmentCount,
964 (EFI_TCP4_FRAGMENT_DATA *) Token->Packet.RxData->FragmentTable
965 );
966 if (EFI_ERROR (Status)) {
967 return Status;
968 }
969
970 Sock = SOCK_FROM_THIS (This);
971
972 return SockRcv (Sock, Token);
973 }
974
975 /**
976 Disconnecting a TCP connection gracefully or reset a TCP connection. This function is a
977 nonblocking operation.
978
979 Initiate an asynchronous close token to the TCP driver. After Close() is called, any buffered
980 transmission data will be sent by the TCP driver, and the current instance will have a graceful close
981 working flow described as RFC 793 if AbortOnClose is set to FALSE. Otherwise, a rest packet
982 will be sent by TCP driver to fast disconnect this connection. When the close operation completes
983 successfully the TCP instance is in Tcp6StateClosed state, all pending asynchronous
984 operations are signaled, and any buffers used for TCP network traffic are flushed.
985
986 @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
987 @param[in] CloseToken Pointer to the close token to return when operation finishes.
988
989 @retval EFI_SUCCESS The Close() was called successfully.
990 @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
991 @retval EFI_ACCESS_DENIED One or more of the following are TRUE:
992 - CloseToken or CloseToken->CompletionToken.Event is already in use.
993 - Previous Close() call on this instance has not finished.
994 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
995 - This is NULL.
996 - CloseToken is NULL.
997 - CloseToken->CompletionToken.Event is NULL.
998 @retval EFI_OUT_OF_RESOURCES Could not allocate enough resource to finish the operation.
999 @retval EFI_DEVICE_ERROR Any unexpected error not belonging to error categories given above.
1000
1001 **/
1002 EFI_STATUS
1003 EFIAPI
1004 Tcp6Close (
1005 IN EFI_TCP6_PROTOCOL *This,
1006 IN EFI_TCP6_CLOSE_TOKEN *CloseToken
1007 )
1008 {
1009 SOCKET *Sock;
1010
1011 if (NULL == This || NULL == CloseToken || NULL == CloseToken->CompletionToken.Event) {
1012 return EFI_INVALID_PARAMETER;
1013 }
1014
1015 Sock = SOCK_FROM_THIS (This);
1016
1017 return SockClose (Sock, CloseToken, CloseToken->AbortOnClose);
1018 }
1019
1020 /**
1021 Abort an asynchronous connection, listen, transmission or receive request.
1022
1023 The Cancel() function aborts a pending connection, listen, transmit or
1024 receive request.
1025
1026 If Token is not NULL and the token is in the connection, listen, transmission
1027 or receive queue when it is being cancelled, its Token->Status will be set
1028 to EFI_ABORTED and then Token->Event will be signaled.
1029
1030 If the token is not in one of the queues, which usually means that the
1031 asynchronous operation has completed, EFI_NOT_FOUND is returned.
1032
1033 If Token is NULL all asynchronous token issued by Connect(), Accept(),
1034 Transmit() and Receive() will be aborted.
1035
1036 @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
1037 @param[in] Token Pointer to a token that has been issued by
1038 EFI_TCP6_PROTOCOL.Connect(),
1039 EFI_TCP6_PROTOCOL.Accept(),
1040 EFI_TCP6_PROTOCOL.Transmit() or
1041 EFI_TCP6_PROTOCOL.Receive(). If NULL, all pending
1042 tokens issued by above four functions will be aborted. Type
1043 EFI_TCP6_COMPLETION_TOKEN is defined in
1044 EFI_TCP_PROTOCOL.Connect().
1045
1046 @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event
1047 is signaled.
1048 @retval EFI_INVALID_PARAMETER This is NULL.
1049 @retval EFI_NOT_STARTED This instance hasn't been configured.
1050 @retval EFI_NOT_FOUND The asynchronous I/O request isn't found in the transmission or
1051 receive queue. It has either completed or wasn't issued by
1052 Transmit() and Receive().
1053
1054 **/
1055 EFI_STATUS
1056 EFIAPI
1057 Tcp6Cancel (
1058 IN EFI_TCP6_PROTOCOL *This,
1059 IN EFI_TCP6_COMPLETION_TOKEN *Token OPTIONAL
1060 )
1061 {
1062 SOCKET *Sock;
1063
1064 if (NULL == This) {
1065 return EFI_INVALID_PARAMETER;
1066 }
1067
1068 Sock = SOCK_FROM_THIS (This);
1069
1070 return SockCancel (Sock, Token);
1071 }
1072
1073 /**
1074 Poll to receive incoming data and transmit outgoing segments.
1075
1076 The Poll() function increases the rate that data is moved between the network
1077 and application, and can be called when the TCP instance is created successfully.
1078 Its use is optional.
1079
1080 @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
1081
1082 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1083 @retval EFI_INVALID_PARAMETER This is NULL.
1084 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1085 @retval EFI_NOT_READY No incoming or outgoing data is processed.
1086 @retval EFI_TIMEOUT Data was dropped out of the transmission or receive queue.
1087 Consider increasing the polling rate.
1088
1089 **/
1090 EFI_STATUS
1091 EFIAPI
1092 Tcp6Poll (
1093 IN EFI_TCP6_PROTOCOL *This
1094 )
1095 {
1096 SOCKET *Sock;
1097 EFI_STATUS Status;
1098
1099 if (NULL == This) {
1100 return EFI_INVALID_PARAMETER;
1101 }
1102
1103 Sock = SOCK_FROM_THIS (This);
1104
1105 Status = Sock->ProtoHandler (Sock, SOCK_POLL, NULL);
1106
1107 return Status;
1108 }
1109