]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/EfiSocketLib/Tcp6.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / StdLib / EfiSocketLib / Tcp6.c
1 /** @file
2 Implement the TCP6 driver support for the socket layer.
3
4 Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7
8 \section ConnectionManagement Connection Management
9
10 The ::EslTcp6Listen routine initially places the SOCK_STREAM or
11 SOCK_SEQPACKET socket into a listen state. When a remote machine
12 makes a connection to the socket, the TCPv6 network layer calls
13 ::EslTcp6ListenComplete to complete the connection processing.
14 EslTcp6ListenComplete manages the connections by placing them in
15 FIFO order in a queue to be serviced by the application. When the
16 number of connections exceeds the backlog (ESL_SOCKET::MaxFifoDepth),
17 the new connection is closed. Eventually, the application indirectly
18 calls ::EslTcp6Accept to remove the next connection from the queue
19 and get the associated socket.
20
21 **/
22
23 #include "Socket.h"
24
25
26 /**
27 Attempt to connect to a remote TCP port
28
29 This routine starts the connection processing for a SOCK_STREAM
30 or SOCK_SEQPAKCET socket using the TCPv6 network layer. It
31 configures the local TCPv6 connection point and then attempts to
32 connect to a remote system. Upon completion, the
33 ::EslTcp6ConnectComplete routine gets called with the connection
34 status.
35
36 This routine is called by ::EslSocketConnect to initiate the TCPv6
37 network specific connect operations. The connection processing is
38 initiated by this routine and finished by ::EslTcp6ConnectComplete.
39 This pair of routines walks through the list of local TCPv6
40 connection points until a connection to the remote system is
41 made.
42
43 @param [in] pSocket Address of an ::ESL_SOCKET structure.
44
45 @retval EFI_SUCCESS The connection was successfully established.
46 @retval EFI_NOT_READY The connection is in progress, call this routine again.
47 @retval Others The connection attempt failed.
48
49 **/
50 EFI_STATUS
51 EslTcp6ConnectStart (
52 IN ESL_SOCKET * pSocket
53 );
54
55
56 /**
57 Process the connection attempt
58
59 A system has initiated a connection attempt with a socket in the
60 listen state. Attempt to complete the connection.
61
62 The TCPv6 layer calls this routine when a connection is made to
63 the socket in the listen state. See the
64 \ref ConnectionManagement section.
65
66 @param [in] Event The listen completion event
67
68 @param [in] pPort Address of an ::ESL_PORT structure.
69
70 **/
71 VOID
72 EslTcp6ListenComplete (
73 IN EFI_EVENT Event,
74 IN ESL_PORT * pPort
75 );
76
77
78 /**
79 Accept a network connection.
80
81 This routine waits for a network connection to the socket and
82 returns the remote network address to the caller if requested.
83
84 This routine is called by ::EslSocketAccept to handle the TCPv6 protocol
85 specific accept operations for SOCK_STREAM and SOCK_SEQPACKET sockets.
86 See the \ref ConnectionManagement section.
87
88 @param [in] pSocket Address of an ::ESL_SOCKET structure.
89
90 @param [in] pSockAddr Address of a buffer to receive the remote
91 network address.
92
93 @param [in, out] pSockAddrLength Length in bytes of the address buffer.
94 On output specifies the length of the
95 remote network address.
96
97 @retval EFI_SUCCESS Remote address is available
98 @retval Others Remote address not available
99
100 **/
101 EFI_STATUS
102 EslTcp6Accept (
103 IN ESL_SOCKET * pSocket,
104 IN struct sockaddr * pSockAddr,
105 IN OUT socklen_t * pSockAddrLength
106 )
107 {
108 ESL_PORT * pPort;
109 struct sockaddr_in6 * pRemoteAddress;
110 ESL_TCP6_CONTEXT * pTcp6;
111 EFI_STATUS Status;
112
113 DBG_ENTER ( );
114
115 //
116 // Validate the socket length
117 //
118 pRemoteAddress = (struct sockaddr_in6 *) pSockAddr;
119 if (( NULL == pSockAddrLength )
120 || ( sizeof ( *pRemoteAddress ) > *pSockAddrLength )) {
121 //
122 // Invalid socket address
123 //
124 Status = EFI_INVALID_PARAMETER;
125 pSocket->errno = EINVAL;
126 DEBUG (( DEBUG_ACCEPT,
127 "ERROR - Invalid address length\r\n" ));
128 }
129 else {
130 //
131 // Assume success
132 //
133 Status = EFI_SUCCESS;
134
135 //
136 // Locate the address context
137 //
138 pPort = pSocket->pPortList;
139 pTcp6 = &pPort->Context.Tcp6;
140
141 //
142 // Fill-in the remote address structure
143 //
144 ZeroMem ( pRemoteAddress, sizeof ( *pRemoteAddress ));
145 pRemoteAddress->sin6_len = sizeof ( *pRemoteAddress );
146 pRemoteAddress->sin6_family = AF_INET6;
147 pRemoteAddress->sin6_port = SwapBytes16 ( pTcp6->ConfigData.AccessPoint.RemotePort );
148 CopyMem ( &pRemoteAddress->sin6_addr.__u6_addr.__u6_addr8 [ 0 ],
149 &pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
150 sizeof ( pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr ));
151 }
152
153 //
154 // Return the operation status
155 //
156 DBG_EXIT_STATUS ( Status );
157 return Status;
158 }
159
160
161 /**
162 Process the remote connection completion event.
163
164 This routine handles the completion of a connection attempt. It
165 releases the port (TCPv6 adapter connection) in the case of an
166 error and start a connection attempt on the next port. If the
167 connection attempt was successful then this routine releases all
168 of the other ports.
169
170 This routine is called by the TCPv6 layer when a connect request
171 completes. It sets the ESL_SOCKET::bConnected flag to notify the
172 ::EslTcp6ConnectComplete routine that the connection is available.
173 The flag is set when the connection is established or no more ports
174 exist in the list. The connection status is passed via
175 ESL_SOCKET::ConnectStatus.
176
177 @param [in] Event The connect completion event
178
179 @param [in] pPort Address of an ::ESL_PORT structure.
180
181 **/
182 VOID
183 EslTcp6ConnectComplete (
184 IN EFI_EVENT Event,
185 IN ESL_PORT * pPort
186 )
187 {
188 BOOLEAN bRemoveFirstPort;
189 BOOLEAN bRemovePorts;
190 ESL_PORT * pNextPort;
191 ESL_SOCKET * pSocket;
192 ESL_TCP6_CONTEXT * pTcp6;
193 EFI_STATUS Status;
194
195 DBG_ENTER ( );
196
197 //
198 // Locate the TCP context
199 //
200 pSocket = pPort->pSocket;
201 pTcp6 = &pPort->Context.Tcp6;
202
203 //
204 // Get the connection status
205 //
206 bRemoveFirstPort = FALSE;
207 bRemovePorts = FALSE;
208 Status = pTcp6->ConnectToken.CompletionToken.Status;
209 pSocket->ConnectStatus = Status;
210 if ( !EFI_ERROR ( Status )) {
211 //
212 // The connection was successful
213 //
214 DEBUG (( DEBUG_CONNECT,
215 "0x%08x: Port connected to [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n",
216 pPort,
217 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
218 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[1],
219 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[2],
220 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[3],
221 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[4],
222 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[5],
223 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[6],
224 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[7],
225 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[8],
226 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[9],
227 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[10],
228 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[11],
229 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[12],
230 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[13],
231 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[14],
232 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[15],
233 pTcp6->ConfigData.AccessPoint.RemotePort ));
234
235 //
236 // Start the receive operations
237 //
238 pSocket->bConfigured = TRUE;
239 pSocket->State = SOCKET_STATE_CONNECTED;
240 EslSocketRxStart ( pPort );
241
242 //
243 // Remove the rest of the ports
244 //
245 bRemovePorts = TRUE;
246 }
247 else {
248 //
249 // The connection failed
250 //
251 if ( pPort->bConfigured ) {
252 DEBUG (( DEBUG_CONNECT,
253 "0x%08x: Port connection to [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d failed, Status: %r\r\n",
254 pPort,
255 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
256 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[1],
257 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[2],
258 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[3],
259 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[4],
260 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[5],
261 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[6],
262 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[7],
263 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[8],
264 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[9],
265 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[10],
266 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[11],
267 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[12],
268 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[13],
269 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[14],
270 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[15],
271 pTcp6->ConfigData.AccessPoint.RemotePort,
272 Status ));
273 }
274
275 //
276 // Close the current port
277 //
278 Status = EslSocketPortClose ( pPort );
279 if ( !EFI_ERROR ( Status )) {
280 DEBUG (( DEBUG_CONNECT,
281 "0x%08x: Port closed\r\n",
282 pPort ));
283 }
284 else {
285 DEBUG (( DEBUG_CONNECT,
286 "ERROR - Failed to close port 0x%08x, Status: %r\r\n",
287 pPort,
288 Status ));
289 }
290
291 //
292 // Try to connect using the next port
293 //
294 Status = EslTcp6ConnectStart ( pSocket );
295 if ( EFI_NOT_READY != Status ) {
296 bRemoveFirstPort = TRUE;
297 }
298 }
299
300 //
301 // Remove the ports if necessary
302 //
303 if ( bRemoveFirstPort || bRemovePorts ) {
304 //
305 // Remove the first port if necessary
306 //
307 pPort = pSocket->pPortList;
308 if (( !bRemoveFirstPort ) && ( NULL != pPort )) {
309 pPort = pPort->pLinkSocket;
310 }
311
312 //
313 // Remove the rest of the list
314 //
315 while ( NULL != pPort ) {
316 pNextPort = pPort->pLinkSocket;
317 EslSocketPortClose ( pPort );
318 if ( !EFI_ERROR ( Status )) {
319 DEBUG (( DEBUG_CONNECT,
320 "0x%08x: Port closed\r\n",
321 pPort ));
322 }
323 else {
324 DEBUG (( DEBUG_CONNECT,
325 "ERROR - Failed to close port 0x%08x, Status: %r\r\n",
326 pPort,
327 Status ));
328 }
329 pPort = pNextPort;
330 }
331
332 //
333 // Notify the poll routine
334 //
335 pSocket->bConnected = TRUE;
336 }
337
338 DBG_EXIT ( );
339 }
340
341
342 /**
343 Poll for completion of the connection attempt.
344
345 This routine polls the ESL_SOCKET::bConnected flag to determine
346 when the connection attempt is complete.
347
348 This routine is called from ::EslSocketConnect to determine when
349 the connection is complete. The ESL_SOCKET::bConnected flag is
350 set by ::EslTcp6ConnectComplete when the TCPv6 layer establishes
351 a connection or runs out of local network adapters. This routine
352 gets the connection status from ESL_SOCKET::ConnectStatus.
353
354 @param [in] pSocket Address of an ::ESL_SOCKET structure.
355
356 @retval EFI_SUCCESS The connection was successfully established.
357 @retval EFI_NOT_READY The connection is in progress, call this routine again.
358 @retval Others The connection attempt failed.
359
360 **/
361 EFI_STATUS
362 EslTcp6ConnectPoll (
363 IN ESL_SOCKET * pSocket
364 )
365 {
366 EFI_STATUS Status;
367
368 DBG_ENTER ( );
369
370 //
371 // Determine if the connection is complete
372 //
373 if ( !pSocket->bConnected ) {
374 //
375 // Not connected
376 //
377 pSocket->errno = EAGAIN;
378 Status = EFI_NOT_READY;
379 }
380 else {
381 //
382 // The connection processing is complete
383 //
384 pSocket->bConnected = FALSE;
385
386 //
387 // Translate the connection status
388 //
389 Status = pSocket->ConnectStatus;
390 switch ( Status ) {
391 default:
392 case EFI_DEVICE_ERROR:
393 pSocket->errno = EIO;
394 break;
395
396 case EFI_ABORTED:
397 pSocket->errno = ECONNABORTED;
398 break;
399
400 case EFI_ACCESS_DENIED:
401 pSocket->errno = EACCES;
402 break;
403
404 case EFI_CONNECTION_RESET:
405 pSocket->errno = ECONNRESET;
406 break;
407
408 case EFI_INVALID_PARAMETER:
409 pSocket->errno = EADDRNOTAVAIL;
410 break;
411
412 case EFI_HOST_UNREACHABLE:
413 case EFI_NO_RESPONSE:
414 pSocket->errno = EHOSTUNREACH;
415 break;
416
417 case EFI_NO_MAPPING:
418 pSocket->errno = EAFNOSUPPORT;
419 break;
420
421 case EFI_NO_MEDIA:
422 case EFI_NETWORK_UNREACHABLE:
423 pSocket->errno = ENETDOWN;
424 break;
425
426 case EFI_OUT_OF_RESOURCES:
427 pSocket->errno = ENOBUFS;
428 break;
429
430 case EFI_PORT_UNREACHABLE:
431 case EFI_PROTOCOL_UNREACHABLE:
432 case EFI_CONNECTION_REFUSED:
433 pSocket->errno = ECONNREFUSED;
434 break;
435
436 case EFI_SUCCESS:
437 pSocket->errno = 0;
438 break;
439
440 case EFI_TIMEOUT:
441 pSocket->errno = ETIMEDOUT;
442 break;
443
444 case EFI_UNSUPPORTED:
445 pSocket->errno = EOPNOTSUPP;
446 break;
447 }
448
449 //
450 // Display the translation
451 //
452 DEBUG (( DEBUG_CONNECT,
453 "ERROR - errno: %d, Status: %r\r\n",
454 pSocket->errno,
455 Status ));
456 }
457
458 //
459 // Return the initialization status
460 //
461 DBG_EXIT_STATUS ( Status );
462 return Status;
463 }
464
465
466 /**
467 Attempt to connect to a remote TCP port
468
469 This routine starts the connection processing for a SOCK_STREAM
470 or SOCK_SEQPAKCET socket using the TCPv6 network layer. It
471 configures the local TCPv6 connection point and then attempts to
472 connect to a remote system. Upon completion, the
473 ::EslTcp6ConnectComplete routine gets called with the connection
474 status.
475
476 This routine is called by ::EslSocketConnect to initiate the TCPv6
477 network specific connect operations. The connection processing is
478 initiated by this routine and finished by ::EslTcp6ConnectComplete.
479 This pair of routines walks through the list of local TCPv6
480 connection points until a connection to the remote system is
481 made.
482
483 @param [in] pSocket Address of an ::ESL_SOCKET structure.
484
485 @retval EFI_SUCCESS The connection was successfully established.
486 @retval EFI_NOT_READY The connection is in progress, call this routine again.
487 @retval Others The connection attempt failed.
488
489 **/
490 EFI_STATUS
491 EslTcp6ConnectStart (
492 IN ESL_SOCKET * pSocket
493 )
494 {
495 ESL_PORT * pPort;
496 ESL_TCP6_CONTEXT * pTcp6;
497 EFI_TCP6_PROTOCOL * pTcp6Protocol;
498 EFI_SIMPLE_NETWORK_MODE SnpModeData;
499 EFI_STATUS Status;
500
501 DBG_ENTER ( );
502
503 //
504 // Determine if any more local adapters are available
505 //
506 pPort = pSocket->pPortList;
507 if ( NULL != pPort ) {
508 //
509 // Configure the port
510 //
511 pTcp6 = &pPort->Context.Tcp6;
512 pTcp6->ConfigData.AccessPoint.ActiveFlag = TRUE;
513 pTcp6->ConfigData.TrafficClass = 0;
514 pTcp6->ConfigData.HopLimit = 255;
515 pTcp6Protocol = pPort->pProtocol.TCPv6;
516 Status = pTcp6Protocol->Configure ( pTcp6Protocol,
517 &pTcp6->ConfigData );
518 if ( EFI_ERROR ( Status )) {
519 DEBUG (( DEBUG_CONNECT,
520 "ERROR - Failed to configure the Tcp6 port, Status: %r\r\n",
521 Status ));
522 }
523 else {
524 DEBUG (( DEBUG_CONNECT,
525 "0x%08x: Port configured\r\n",
526 pPort ));
527 pPort->bConfigured = TRUE;
528
529 //
530 // Verify the port connection
531 //
532 Status = pTcp6Protocol->GetModeData ( pTcp6Protocol,
533 NULL,
534 NULL,
535 NULL,
536 NULL,
537 &SnpModeData );
538 if ( !EFI_ERROR ( Status )) {
539 if ( SnpModeData.MediaPresentSupported
540 && ( !SnpModeData.MediaPresent )) {
541 //
542 // Port is not connected to the network
543 //
544 Status = EFI_NO_MEDIA;
545 }
546 else {
547 //
548 // Attempt the connection to the remote system
549 //
550 Status = pTcp6Protocol->Connect ( pTcp6Protocol,
551 &pTcp6->ConnectToken );
552 }
553 }
554 if ( EFI_ERROR ( Status )) {
555 //
556 // Connection error
557 //
558 DEBUG (( DEBUG_CONNECT,
559 "ERROR - Port 0x%08x not connected, Status: %r\r\n",
560 pPort,
561 Status ));
562 }
563 }
564 if ( !EFI_ERROR ( Status )) {
565 //
566 // Connection in progress
567 //
568 pSocket->errno = EINPROGRESS;
569 DEBUG (( DEBUG_CONNECT,
570 "0x%08x: Port attempting connection to [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n",
571 pPort,
572 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
573 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[1],
574 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[2],
575 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[3],
576 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[4],
577 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[5],
578 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[6],
579 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[7],
580 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[8],
581 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[9],
582 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[10],
583 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[11],
584 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[12],
585 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[13],
586 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[14],
587 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[15],
588 pTcp6->ConfigData.AccessPoint.RemotePort ));
589 }
590 else {
591 //
592 // Error return path is through EslTcp6ConnectComplete to
593 // enable retry on other ports
594 //
595 // Status to errno translation gets done in EslTcp4ConnectPoll
596 //
597 pTcp6->ConnectToken.CompletionToken.Status = Status;
598
599 //
600 // Continue with the next port
601 //
602 gBS->CheckEvent ( pTcp6->ConnectToken.CompletionToken.Event );
603 gBS->SignalEvent ( pTcp6->ConnectToken.CompletionToken.Event );
604 }
605 Status = EFI_NOT_READY;
606 }
607 else {
608 //
609 // No more local adapters available
610 //
611 pSocket->errno = ENETUNREACH;
612 Status = EFI_NO_RESPONSE;
613 }
614
615 //
616 // Return the operation status
617 //
618 DBG_EXIT_STATUS ( Status );
619 return Status;
620 }
621
622
623 /**
624 Establish the known port to listen for network connections.
625
626 This routine places the port into a state that enables connection
627 attempts.
628
629 This routine is called by ::EslSocketListen to handle the network
630 specifics of the listen operation for SOCK_STREAM and SOCK_SEQPACKET
631 sockets. See the \ref ConnectionManagement section.
632
633 @param [in] pSocket Address of an ::ESL_SOCKET structure.
634
635 @retval EFI_SUCCESS - Socket successfully created
636 @retval Other - Failed to enable the socket for listen
637
638 **/
639 EFI_STATUS
640 EslTcp6Listen (
641 IN ESL_SOCKET * pSocket
642 )
643 {
644 ESL_PORT * pNextPort;
645 ESL_PORT * pPort;
646 ESL_TCP6_CONTEXT * pTcp6;
647 EFI_TCP6_PROTOCOL * pTcp6Protocol;
648 EFI_STATUS Status;
649
650 DBG_ENTER ( );
651
652 //
653 // Verify the socket layer synchronization
654 //
655 VERIFY_TPL ( TPL_SOCKETS );
656
657 //
658 // Use for/break instead of goto
659 //
660 for ( ; ; ) {
661 //
662 // Assume no ports are available
663 //
664 pSocket->errno = EOPNOTSUPP;
665 Status = EFI_NOT_READY;
666
667 //
668 // Walk the list of ports
669 //
670 pPort = pSocket->pPortList;
671 while ( NULL != pPort ) {
672 //
673 // Assume success
674 //
675 pSocket->errno = 0;
676
677 //
678 // Use for/break insteak of goto
679 //
680 for ( ; ; ) {
681 //
682 // Create the listen completion event
683 //
684 pTcp6 = &pPort->Context.Tcp6;
685 Status = gBS->CreateEvent ( EVT_NOTIFY_SIGNAL,
686 TPL_SOCKETS,
687 (EFI_EVENT_NOTIFY)EslTcp6ListenComplete,
688 pPort,
689 &pTcp6->ListenToken.CompletionToken.Event );
690 if ( EFI_ERROR ( Status )) {
691 DEBUG (( DEBUG_ERROR | DEBUG_LISTEN,
692 "ERROR - Failed to create the listen completion event, Status: %r\r\n",
693 Status ));
694 pSocket->errno = ENOMEM;
695 break;
696 }
697 DEBUG (( DEBUG_POOL,
698 "0x%08x: Created listen completion event\r\n",
699 pTcp6->ListenToken.CompletionToken.Event ));
700
701 //
702 // Configure the port
703 //
704 pTcp6Protocol = pPort->pProtocol.TCPv6;
705 Status = pTcp6Protocol->Configure ( pTcp6Protocol,
706 &pTcp6->ConfigData );
707 if ( EFI_ERROR ( Status )) {
708 DEBUG (( DEBUG_LISTEN,
709 "ERROR - Failed to configure the Tcp6 port, Status: %r\r\n",
710 Status ));
711 switch ( Status ) {
712 case EFI_ACCESS_DENIED:
713 pSocket->errno = EACCES;
714 break;
715
716 default:
717 case EFI_DEVICE_ERROR:
718 pSocket->errno = EIO;
719 break;
720
721 case EFI_INVALID_PARAMETER:
722 pSocket->errno = EADDRNOTAVAIL;
723 break;
724
725 case EFI_NO_MAPPING:
726 pSocket->errno = EAFNOSUPPORT;
727 break;
728
729 case EFI_OUT_OF_RESOURCES:
730 pSocket->errno = ENOBUFS;
731 break;
732
733 case EFI_UNSUPPORTED:
734 pSocket->errno = EOPNOTSUPP;
735 break;
736 }
737 break;
738 }
739 DEBUG (( DEBUG_LISTEN,
740 "0x%08x: Port configured\r\n",
741 pPort ));
742 pPort->bConfigured = TRUE;
743
744 //
745 // Start the listen operation on the port
746 //
747 Status = pTcp6Protocol->Accept ( pTcp6Protocol,
748 &pTcp6->ListenToken );
749 if ( EFI_ERROR ( Status )) {
750 DEBUG (( DEBUG_LISTEN,
751 "ERROR - Failed Tcp6 accept, Status: %r\r\n",
752 Status ));
753 switch ( Status ) {
754 case EFI_ACCESS_DENIED:
755 pSocket->errno = EACCES;
756 break;
757
758 default:
759 case EFI_DEVICE_ERROR:
760 pSocket->errno = EIO;
761 break;
762
763 case EFI_INVALID_PARAMETER:
764 pSocket->errno = EADDRNOTAVAIL;
765 break;
766
767 case EFI_NOT_STARTED:
768 pSocket->errno = ENETDOWN;
769 break;
770
771 case EFI_OUT_OF_RESOURCES:
772 pSocket->errno = ENOBUFS;
773 break;
774 }
775 break;
776 }
777 DEBUG (( DEBUG_LISTEN,
778 "0x%08x: Listen pending on Port\r\n",
779 pPort ));
780
781 //
782 // Listen is pending on this port
783 //
784 break;
785 }
786
787 //
788 // Get the next port
789 //
790 pNextPort = pPort->pLinkSocket;
791
792 //
793 // Close the port upon error
794 //
795 if ( EFI_ERROR ( Status )) {
796 EslSocketPortCloseStart ( pPort, TRUE, DEBUG_LISTEN );
797 }
798
799 //
800 // Set the next port
801 //
802 pPort = pNextPort;
803 }
804
805 //
806 // Determine if any ports are in the listen state
807 //
808 if ( NULL == pSocket->pPortList ) {
809 //
810 // No ports in the listen state
811 //
812 pSocket->MaxFifoDepth = 0;
813
814 //
815 // Return the last error detected
816 //
817 break;
818 }
819
820 //
821 // Mark the socket as configured
822 //
823 pSocket->bConfigured = TRUE;
824 Status = EFI_SUCCESS;
825 pSocket->errno = 0;
826
827 //
828 // All done
829 //
830 DEBUG (( DEBUG_LISTEN,
831 "0x%08x: pSocket - Listen pending on socket\r\n",
832 pSocket ));
833 break;
834 }
835
836 //
837 // Return the operation status
838 //
839 DBG_EXIT_STATUS ( Status );
840 return Status;
841 }
842
843
844 /**
845 Process the connection attempt
846
847 A system has initiated a connection attempt with a socket in the
848 listen state. Attempt to complete the connection.
849
850 The TCPv6 layer calls this routine when a connection is made to
851 the socket in the listen state. See the
852 \ref ConnectionManagement section.
853
854 @param [in] Event The listen completion event
855
856 @param [in] pPort Address of an ::ESL_PORT structure.
857
858 **/
859 VOID
860 EslTcp6ListenComplete (
861 IN EFI_EVENT Event,
862 IN ESL_PORT * pPort
863 )
864 {
865 EFI_HANDLE ChildHandle;
866 struct sockaddr_in6 LocalAddress;
867 EFI_TCP6_CONFIG_DATA * pConfigData;
868 ESL_PORT * pNewPort;
869 ESL_SOCKET * pNewSocket;
870 ESL_SOCKET * pSocket;
871 ESL_TCP6_CONTEXT * pTcp6;
872 EFI_TCP6_PROTOCOL * pTcp6Protocol;
873 EFI_STATUS Status;
874 EFI_HANDLE TcpPortHandle;
875 EFI_STATUS TempStatus;
876
877 DBG_ENTER ( );
878 VERIFY_AT_TPL ( TPL_SOCKETS );
879
880 //
881 // Assume success
882 //
883 Status = EFI_SUCCESS;
884
885 //
886 // Determine if this connection fits into the connection FIFO
887 //
888 pSocket = pPort->pSocket;
889 TcpPortHandle = pPort->Context.Tcp6.ListenToken.NewChildHandle;
890 if (( SOCKET_STATE_LISTENING == pSocket->State )
891 && ( pSocket->MaxFifoDepth > pSocket->FifoDepth )) {
892 //
893 // Allocate a socket for this connection
894 //
895 ChildHandle = NULL;
896 Status = EslSocketAllocate ( &ChildHandle,
897 DEBUG_CONNECTION,
898 &pNewSocket );
899 if ( !EFI_ERROR ( Status )) {
900 //
901 // Clone the socket parameters
902 //
903 pNewSocket->pApi = pSocket->pApi;
904 pNewSocket->Domain = pSocket->Domain;
905 pNewSocket->Protocol = pSocket->Protocol;
906 pNewSocket->Type = pSocket->Type;
907
908 //
909 // Build the local address
910 //
911 pTcp6 = &pPort->Context.Tcp6;
912 LocalAddress.sin6_len = (uint8_t)pNewSocket->pApi->MinimumAddressLength;
913 LocalAddress.sin6_family = AF_INET6;
914 LocalAddress.sin6_port = 0;
915 CopyMem ( &LocalAddress.sin6_addr.__u6_addr.__u6_addr8 [ 0 ],
916 &pTcp6->ConfigData.AccessPoint.StationAddress.Addr [ 0 ],
917 sizeof ( pTcp6->ConfigData.AccessPoint.StationAddress.Addr ));
918
919 //
920 // Allocate a port for this connection
921 // Note in this instance Configure may not be called with NULL!
922 //
923 Status = EslSocketPortAllocate ( pNewSocket,
924 pPort->pService,
925 TcpPortHandle,
926 (struct sockaddr *)&LocalAddress,
927 FALSE,
928 DEBUG_CONNECTION,
929 &pNewPort );
930 if ( !EFI_ERROR ( Status )) {
931 //
932 // Restart the listen operation on the port
933 //
934 pTcp6Protocol = pPort->pProtocol.TCPv6;
935 Status = pTcp6Protocol->Accept ( pTcp6Protocol,
936 &pTcp6->ListenToken );
937
938 //
939 // Close the TCP port using SocketClose
940 //
941 TcpPortHandle = NULL;
942 pTcp6 = &pNewPort->Context.Tcp6;
943
944 //
945 // Check for an accept call error
946 //
947 if ( !EFI_ERROR ( Status )) {
948 //
949 // Get the port configuration
950 //
951 pNewPort->bConfigured = TRUE;
952 pConfigData = &pTcp6->ConfigData;
953 pConfigData->ControlOption = &pTcp6->Option;
954 pTcp6Protocol = pNewPort->pProtocol.TCPv6;
955 Status = pTcp6Protocol->GetModeData ( pTcp6Protocol,
956 NULL,
957 pConfigData,
958 NULL,
959 NULL,
960 NULL );
961 if ( !EFI_ERROR ( Status )) {
962 //
963 // Add the new socket to the connection FIFO
964 //
965 if ( NULL == pSocket->pFifoTail ) {
966 //
967 // First connection
968 //
969 pSocket->pFifoHead = pNewSocket;
970 }
971 else {
972 //
973 // Add to end of list.
974 //
975 pSocket->pFifoTail->pNextConnection = pNewSocket;
976 }
977 pSocket->pFifoTail = pNewSocket;
978 pSocket->FifoDepth += 1;
979
980 //
981 // Update the socket state
982 //
983 pNewSocket->State = SOCKET_STATE_IN_FIFO;
984
985 //
986 // Log the connection
987 //
988 DEBUG (( DEBUG_CONNECTION | DEBUG_INFO,
989 "0x%08x: Socket on port [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d connected to [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n",
990 pNewSocket,
991 pConfigData->AccessPoint.StationAddress.Addr[0],
992 pConfigData->AccessPoint.StationAddress.Addr[1],
993 pConfigData->AccessPoint.StationAddress.Addr[2],
994 pConfigData->AccessPoint.StationAddress.Addr[3],
995 pConfigData->AccessPoint.StationAddress.Addr[4],
996 pConfigData->AccessPoint.StationAddress.Addr[5],
997 pConfigData->AccessPoint.StationAddress.Addr[6],
998 pConfigData->AccessPoint.StationAddress.Addr[7],
999 pConfigData->AccessPoint.StationAddress.Addr[8],
1000 pConfigData->AccessPoint.StationAddress.Addr[9],
1001 pConfigData->AccessPoint.StationAddress.Addr[10],
1002 pConfigData->AccessPoint.StationAddress.Addr[11],
1003 pConfigData->AccessPoint.StationAddress.Addr[12],
1004 pConfigData->AccessPoint.StationAddress.Addr[13],
1005 pConfigData->AccessPoint.StationAddress.Addr[14],
1006 pConfigData->AccessPoint.StationAddress.Addr[15],
1007 pConfigData->AccessPoint.StationPort,
1008 pConfigData->AccessPoint.RemoteAddress.Addr[0],
1009 pConfigData->AccessPoint.RemoteAddress.Addr[1],
1010 pConfigData->AccessPoint.RemoteAddress.Addr[2],
1011 pConfigData->AccessPoint.RemoteAddress.Addr[3],
1012 pConfigData->AccessPoint.RemoteAddress.Addr[4],
1013 pConfigData->AccessPoint.RemoteAddress.Addr[5],
1014 pConfigData->AccessPoint.RemoteAddress.Addr[6],
1015 pConfigData->AccessPoint.RemoteAddress.Addr[7],
1016 pConfigData->AccessPoint.RemoteAddress.Addr[8],
1017 pConfigData->AccessPoint.RemoteAddress.Addr[9],
1018 pConfigData->AccessPoint.RemoteAddress.Addr[10],
1019 pConfigData->AccessPoint.RemoteAddress.Addr[11],
1020 pConfigData->AccessPoint.RemoteAddress.Addr[12],
1021 pConfigData->AccessPoint.RemoteAddress.Addr[13],
1022 pConfigData->AccessPoint.RemoteAddress.Addr[14],
1023 pConfigData->AccessPoint.RemoteAddress.Addr[15],
1024 pConfigData->AccessPoint.RemotePort ));
1025 DEBUG (( DEBUG_CONNECTION | DEBUG_INFO,
1026 "0x%08x: Listen socket adding socket 0x%08x to FIFO, depth: %d\r\n",
1027 pSocket,
1028 pNewSocket,
1029 pSocket->FifoDepth ));
1030
1031 //
1032 // Start the receive operation
1033 //
1034 EslSocketRxStart ( pNewPort );
1035 }
1036 else {
1037 DEBUG (( DEBUG_ERROR | DEBUG_CONNECTION | DEBUG_INFO,
1038 "ERROR - GetModeData failed on port 0x%08x, Status: %r\r\n",
1039 pNewPort,
1040 Status ));
1041 }
1042 }
1043 else {
1044 //
1045 // The listen failed on this port
1046 //
1047 DEBUG (( DEBUG_LISTEN | DEBUG_INFO,
1048 "ERROR - Listen failed on port 0x%08x, Status: %r\r\n",
1049 pPort,
1050 Status ));
1051
1052 //
1053 // Close the listening port
1054 //
1055 EslSocketPortCloseStart ( pPort, TRUE, DEBUG_LISTEN );
1056 }
1057 }
1058
1059 //
1060 // Done with the socket if necessary
1061 //
1062 if ( EFI_ERROR ( Status )) {
1063 TempStatus = EslSocketCloseStart ( &pNewSocket->SocketProtocol,
1064 TRUE,
1065 &pSocket->errno );
1066 ASSERT ( EFI_SUCCESS == TempStatus );
1067 }
1068 }
1069 }
1070 else {
1071 DEBUG (( DEBUG_CONNECTION,
1072 "0x%08x: Socket FIFO full, connection refused\r\n",
1073 pSocket ));
1074
1075 //
1076 // The FIFO is full or the socket is in the wrong state
1077 //
1078 Status = EFI_BUFFER_TOO_SMALL;
1079 }
1080
1081 //
1082 // Close the connection if necessary
1083 //
1084 if (( EFI_ERROR ( Status ))
1085 && ( NULL == TcpPortHandle )) {
1086 //
1087 // TODO: Finish this code path
1088 // The new connection does not fit into the connection FIFO
1089 //
1090 // Process:
1091 // Call close
1092 // Release the resources
1093
1094 }
1095
1096 DBG_EXIT ( );
1097 }
1098
1099
1100 /**
1101 Get the local socket address.
1102
1103 This routine returns the IPv6 address and TCP port number associated
1104 with the local socket.
1105
1106 This routine is called by ::EslSocketGetLocalAddress to determine the
1107 network address for the SOCK_STREAM or SOCK_SEQPACKET socket.
1108
1109 @param [in] pPort Address of an ::ESL_PORT structure.
1110
1111 @param [out] pSockAddr Network address to receive the local system address
1112
1113 **/
1114 VOID
1115 EslTcp6LocalAddressGet (
1116 IN ESL_PORT * pPort,
1117 OUT struct sockaddr * pSockAddr
1118 )
1119 {
1120 struct sockaddr_in6 * pLocalAddress;
1121 ESL_TCP6_CONTEXT * pTcp6;
1122
1123 DBG_ENTER ( );
1124
1125 //
1126 // Return the local address
1127 //
1128 pTcp6 = &pPort->Context.Tcp6;
1129 pLocalAddress = (struct sockaddr_in6 *)pSockAddr;
1130 pLocalAddress->sin6_family = AF_INET6;
1131 pLocalAddress->sin6_port = SwapBytes16 ( pTcp6->ConfigData.AccessPoint.StationPort );
1132 CopyMem ( &pLocalAddress->sin6_addr,
1133 &pTcp6->ConfigData.AccessPoint.StationAddress.Addr[0],
1134 sizeof ( pLocalAddress->sin6_addr ));
1135
1136 DBG_EXIT ( );
1137 }
1138
1139
1140 /**
1141 Set the local port address.
1142
1143 This routine sets the local port address.
1144
1145 This support routine is called by ::EslSocketPortAllocate.
1146
1147 @param [in] pPort Address of an ESL_PORT structure
1148 @param [in] pSockAddr Address of a sockaddr structure that contains the
1149 connection point on the local machine. An IPv6 address
1150 of INADDR_ANY specifies that the connection is made to
1151 all of the network stacks on the platform. Specifying a
1152 specific IPv6 address restricts the connection to the
1153 network stack supporting that address. Specifying zero
1154 for the port causes the network layer to assign a port
1155 number from the dynamic range. Specifying a specific
1156 port number causes the network layer to use that port.
1157
1158 @param [in] bBindTest TRUE = run bind testing
1159
1160 @retval EFI_SUCCESS The operation was successful
1161
1162 **/
1163 EFI_STATUS
1164 EslTcp6LocalAddressSet (
1165 IN ESL_PORT * pPort,
1166 IN CONST struct sockaddr * pSockAddr,
1167 IN BOOLEAN bBindTest
1168 )
1169 {
1170 EFI_TCP6_ACCESS_POINT * pAccessPoint;
1171 CONST struct sockaddr_in6 * pIpAddress;
1172 EFI_STATUS Status;
1173
1174 DBG_ENTER ( );
1175
1176 //
1177 // Validate the address
1178 //
1179 pIpAddress = (struct sockaddr_in6 *)pSockAddr;
1180 //
1181 // TODO: Fix the following check
1182 //
1183 /*
1184 if ( INADDR_BROADCAST == pIpAddress->sin6_addr.s_addr ) {
1185 //
1186 // The local address must not be the broadcast address
1187 //
1188 Status = EFI_INVALID_PARAMETER;
1189 pPort->pSocket->errno = EADDRNOTAVAIL;
1190 }
1191 else {
1192 */
1193 {
1194 //
1195 // Set the local address
1196 //
1197 pAccessPoint = &pPort->Context.Tcp6.ConfigData.AccessPoint;
1198 CopyMem ( &pAccessPoint->StationAddress.Addr[0],
1199 &pIpAddress->sin6_addr.__u6_addr.__u6_addr8 [ 0 ],
1200 sizeof ( pIpAddress->sin6_addr.__u6_addr.__u6_addr8 [ 0 ]));
1201
1202 //
1203 // Validate the IP address
1204 //
1205 pAccessPoint->StationPort = 0;
1206 Status = bBindTest ? EslSocketBindTest ( pPort, EADDRNOTAVAIL )
1207 : EFI_SUCCESS;
1208 if ( !EFI_ERROR ( Status )) {
1209 //
1210 // Set the port number
1211 //
1212 pAccessPoint->StationPort = SwapBytes16 ( pIpAddress->sin6_port );
1213 pPort->pSocket->bAddressSet = TRUE;
1214
1215 //
1216 // Display the local address
1217 //
1218 DEBUG (( DEBUG_BIND,
1219 "0x%08x: Port, Local Tcp6 Address: [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n",
1220 pPort,
1221 pAccessPoint->StationAddress.Addr[0],
1222 pAccessPoint->StationAddress.Addr[1],
1223 pAccessPoint->StationAddress.Addr[2],
1224 pAccessPoint->StationAddress.Addr[3],
1225 pAccessPoint->StationAddress.Addr[4],
1226 pAccessPoint->StationAddress.Addr[5],
1227 pAccessPoint->StationAddress.Addr[6],
1228 pAccessPoint->StationAddress.Addr[7],
1229 pAccessPoint->StationAddress.Addr[8],
1230 pAccessPoint->StationAddress.Addr[9],
1231 pAccessPoint->StationAddress.Addr[10],
1232 pAccessPoint->StationAddress.Addr[11],
1233 pAccessPoint->StationAddress.Addr[12],
1234 pAccessPoint->StationAddress.Addr[13],
1235 pAccessPoint->StationAddress.Addr[14],
1236 pAccessPoint->StationAddress.Addr[15],
1237 pAccessPoint->StationPort ));
1238 }
1239 }
1240
1241 //
1242 // Return the operation status
1243 //
1244 DBG_EXIT_STATUS ( Status );
1245 return Status;
1246 }
1247
1248
1249 /**
1250 Free a receive packet
1251
1252 This routine performs the network specific operations necessary
1253 to free a receive packet.
1254
1255 This routine is called by ::EslSocketPortCloseTxDone to free a
1256 receive packet.
1257
1258 @param [in] pPacket Address of an ::ESL_PACKET structure.
1259 @param [in, out] pRxBytes Address of the count of RX bytes
1260
1261 **/
1262 VOID
1263 EslTcp6PacketFree (
1264 IN ESL_PACKET * pPacket,
1265 IN OUT size_t * pRxBytes
1266 )
1267 {
1268 DBG_ENTER ( );
1269
1270 //
1271 // Account for the receive bytes
1272 //
1273 *pRxBytes -= pPacket->Op.Tcp6Rx.RxData.DataLength;
1274 DBG_EXIT ( );
1275 }
1276
1277
1278 /**
1279 Initialize the network specific portions of an ::ESL_PORT structure.
1280
1281 This routine initializes the network specific portions of an
1282 ::ESL_PORT structure for use by the socket.
1283
1284 This support routine is called by ::EslSocketPortAllocate
1285 to connect the socket with the underlying network adapter
1286 running the TCPv6 protocol.
1287
1288 @param [in] pPort Address of an ESL_PORT structure
1289 @param [in] DebugFlags Flags for debug messages
1290
1291 @retval EFI_SUCCESS - Socket successfully created
1292
1293 **/
1294 EFI_STATUS
1295 EslTcp6PortAllocate (
1296 IN ESL_PORT * pPort,
1297 IN UINTN DebugFlags
1298 )
1299 {
1300 EFI_TCP6_ACCESS_POINT * pAccessPoint;
1301 ESL_SOCKET * pSocket;
1302 ESL_TCP6_CONTEXT * pTcp6;
1303 EFI_STATUS Status;
1304
1305 DBG_ENTER ( );
1306
1307 //
1308 // Use for/break instead of goto
1309 for ( ; ; ) {
1310 //
1311 // Allocate the close event
1312 //
1313 pSocket = pPort->pSocket;
1314 pTcp6 = &pPort->Context.Tcp6;
1315 Status = gBS->CreateEvent ( EVT_NOTIFY_SIGNAL,
1316 TPL_SOCKETS,
1317 (EFI_EVENT_NOTIFY)EslSocketPortCloseComplete,
1318 pPort,
1319 &pTcp6->CloseToken.CompletionToken.Event);
1320 if ( EFI_ERROR ( Status )) {
1321 DEBUG (( DEBUG_ERROR | DebugFlags,
1322 "ERROR - Failed to create the close event, Status: %r\r\n",
1323 Status ));
1324 pSocket->errno = ENOMEM;
1325 break;
1326 }
1327 DEBUG (( DEBUG_CLOSE | DEBUG_POOL,
1328 "0x%08x: Created close event\r\n",
1329 pTcp6->CloseToken.CompletionToken.Event ));
1330
1331 //
1332 // Allocate the connection event
1333 //
1334 Status = gBS->CreateEvent ( EVT_NOTIFY_SIGNAL,
1335 TPL_SOCKETS,
1336 (EFI_EVENT_NOTIFY)EslTcp6ConnectComplete,
1337 pPort,
1338 &pTcp6->ConnectToken.CompletionToken.Event);
1339 if ( EFI_ERROR ( Status )) {
1340 DEBUG (( DEBUG_ERROR | DebugFlags,
1341 "ERROR - Failed to create the connect event, Status: %r\r\n",
1342 Status ));
1343 pSocket->errno = ENOMEM;
1344 break;
1345 }
1346 DEBUG (( DEBUG_CLOSE | DEBUG_POOL,
1347 "0x%08x: Created connect event\r\n",
1348 pTcp6->ConnectToken.CompletionToken.Event ));
1349
1350 //
1351 // Initialize the port
1352 //
1353 pSocket->TxPacketOffset = OFFSET_OF ( ESL_PACKET, Op.Tcp6Tx.TxData );
1354 pSocket->TxTokenEventOffset = OFFSET_OF ( ESL_IO_MGMT, Token.Tcp6Tx.CompletionToken.Event );
1355 pSocket->TxTokenOffset = OFFSET_OF ( EFI_TCP6_IO_TOKEN, Packet.TxData );
1356
1357 //
1358 // Save the cancel, receive and transmit addresses
1359 // pPort->pfnRxCancel = NULL; since the UEFI implementation returns EFI_UNSUPPORTED
1360 //
1361 pPort->pfnConfigure = (PFN_NET_CONFIGURE)pPort->pProtocol.TCPv6->Configure;
1362 pPort->pfnRxPoll = (PFN_NET_POLL)pPort->pProtocol.TCPv6->Poll;
1363 pPort->pfnRxStart = (PFN_NET_IO_START)pPort->pProtocol.TCPv6->Receive;
1364 pPort->pfnTxStart = (PFN_NET_IO_START)pPort->pProtocol.TCPv6->Transmit;
1365
1366 //
1367 // Set the configuration flags
1368 //
1369 pAccessPoint = &pPort->Context.Tcp6.ConfigData.AccessPoint;
1370 pAccessPoint->ActiveFlag = FALSE;
1371 pTcp6->ConfigData.TrafficClass = 0;
1372 pTcp6->ConfigData.HopLimit = 255;
1373 break;
1374 }
1375
1376 //
1377 // Return the operation status
1378 //
1379 DBG_EXIT_STATUS ( Status );
1380 return Status;
1381 }
1382
1383
1384 /**
1385 Close a Tcp6 port.
1386
1387 This routine releases the network specific resources allocated by
1388 ::EslTcp6PortAllocate.
1389
1390 This routine is called by ::EslSocketPortClose.
1391 See the \ref PortCloseStateMachine section.
1392
1393 @param [in] pPort Address of an ::ESL_PORT structure.
1394
1395 @retval EFI_SUCCESS The port is closed
1396 @retval other Port close error
1397
1398 **/
1399 EFI_STATUS
1400 EslTcp6PortClose (
1401 IN ESL_PORT * pPort
1402 )
1403 {
1404 UINTN DebugFlags;
1405 ESL_TCP6_CONTEXT * pTcp6;
1406 EFI_STATUS Status;
1407
1408 DBG_ENTER ( );
1409
1410 //
1411 // Locate the port in the socket list
1412 //
1413 Status = EFI_SUCCESS;
1414 DebugFlags = pPort->DebugFlags;
1415 pTcp6 = &pPort->Context.Tcp6;
1416
1417 //
1418 // Done with the connect event
1419 //
1420 if ( NULL != pTcp6->ConnectToken.CompletionToken.Event ) {
1421 Status = gBS->CloseEvent ( pTcp6->ConnectToken.CompletionToken.Event );
1422 if ( !EFI_ERROR ( Status )) {
1423 DEBUG (( DebugFlags | DEBUG_POOL,
1424 "0x%08x: Closed connect event\r\n",
1425 pTcp6->ConnectToken.CompletionToken.Event ));
1426 }
1427 else {
1428 DEBUG (( DEBUG_ERROR | DebugFlags,
1429 "ERROR - Failed to close the connect event, Status: %r\r\n",
1430 Status ));
1431 ASSERT ( EFI_SUCCESS == Status );
1432 }
1433 }
1434
1435 //
1436 // Done with the close event
1437 //
1438 if ( NULL != pTcp6->CloseToken.CompletionToken.Event ) {
1439 Status = gBS->CloseEvent ( pTcp6->CloseToken.CompletionToken.Event );
1440 if ( !EFI_ERROR ( Status )) {
1441 DEBUG (( DebugFlags | DEBUG_POOL,
1442 "0x%08x: Closed close event\r\n",
1443 pTcp6->CloseToken.CompletionToken.Event ));
1444 }
1445 else {
1446 DEBUG (( DEBUG_ERROR | DebugFlags,
1447 "ERROR - Failed to close the close event, Status: %r\r\n",
1448 Status ));
1449 ASSERT ( EFI_SUCCESS == Status );
1450 }
1451 }
1452
1453 //
1454 // Done with the listen completion event
1455 //
1456 if ( NULL != pTcp6->ListenToken.CompletionToken.Event ) {
1457 Status = gBS->CloseEvent ( pTcp6->ListenToken.CompletionToken.Event );
1458 if ( !EFI_ERROR ( Status )) {
1459 DEBUG (( DebugFlags | DEBUG_POOL,
1460 "0x%08x: Closed listen completion event\r\n",
1461 pTcp6->ListenToken.CompletionToken.Event ));
1462 }
1463 else {
1464 DEBUG (( DEBUG_ERROR | DebugFlags,
1465 "ERROR - Failed to close the listen completion event, Status: %r\r\n",
1466 Status ));
1467 ASSERT ( EFI_SUCCESS == Status );
1468 }
1469 }
1470
1471 //
1472 // Return the operation status
1473 //
1474 DBG_EXIT_STATUS ( Status );
1475 return Status;
1476 }
1477
1478
1479 /**
1480 Perform the network specific close operation on the port.
1481
1482 This routine performs a cancel operations on the TCPv6 port to
1483 shutdown the receive operations on the port.
1484
1485 This routine is called by the ::EslSocketPortCloseTxDone
1486 routine after the port completes all of the transmission.
1487
1488 @param [in] pPort Address of an ::ESL_PORT structure.
1489
1490 @retval EFI_SUCCESS The port is closed, not normally returned
1491 @retval EFI_NOT_READY The port is still closing
1492 @retval EFI_ALREADY_STARTED Error, the port is in the wrong state,
1493 most likely the routine was called already.
1494
1495 **/
1496 EFI_STATUS
1497 EslTcp6PortCloseOp (
1498 IN ESL_PORT * pPort
1499 )
1500 {
1501 ESL_TCP6_CONTEXT * pTcp6;
1502 EFI_TCP6_PROTOCOL * pTcp6Protocol;
1503 EFI_STATUS Status;
1504
1505 DBG_ENTER ( );
1506
1507 //
1508 // Close the configured port
1509 //
1510 Status = EFI_SUCCESS;
1511 pTcp6 = &pPort->Context.Tcp6;
1512 pTcp6Protocol = pPort->pProtocol.TCPv6;
1513 pTcp6->CloseToken.AbortOnClose = pPort->bCloseNow;
1514 Status = pTcp6Protocol->Close ( pTcp6Protocol,
1515 &pTcp6->CloseToken );
1516 if ( !EFI_ERROR ( Status )) {
1517 DEBUG (( pPort->DebugFlags | DEBUG_CLOSE | DEBUG_INFO,
1518 "0x%08x: Port close started\r\n",
1519 pPort ));
1520 }
1521 else {
1522 DEBUG (( DEBUG_ERROR | pPort->DebugFlags | DEBUG_CLOSE | DEBUG_INFO,
1523 "ERROR - Close failed on port 0x%08x, Status: %r\r\n",
1524 pPort,
1525 Status ));
1526 }
1527
1528 //
1529 // Return the operation status
1530 //
1531 DBG_EXIT_STATUS ( Status );
1532 return Status;
1533 }
1534
1535
1536 /**
1537 Receive data from a network connection.
1538
1539 This routine attempts to return buffered data to the caller. The
1540 data is removed from the urgent queue if the message flag MSG_OOB
1541 is specified, otherwise data is removed from the normal queue.
1542 See the \ref ReceiveEngine section.
1543
1544 This routine is called by ::EslSocketReceive to handle the network
1545 specific receive operation to support SOCK_STREAM and SOCK_SEQPACKET
1546 sockets.
1547
1548 @param [in] pPort Address of an ::ESL_PORT structure.
1549
1550 @param [in] pPacket Address of an ::ESL_PACKET structure.
1551
1552 @param [in] pbConsumePacket Address of a BOOLEAN indicating if the packet is to be consumed
1553
1554 @param [in] BufferLength Length of the the buffer
1555
1556 @param [in] pBuffer Address of a buffer to receive the data.
1557
1558 @param [in] pDataLength Number of received data bytes in the buffer.
1559
1560 @param [out] pAddress Network address to receive the remote system address
1561
1562 @param [out] pSkipBytes Address to receive the number of bytes skipped
1563
1564 @return Returns the address of the next free byte in the buffer.
1565
1566 **/
1567 UINT8 *
1568 EslTcp6Receive (
1569 IN ESL_PORT * pPort,
1570 IN ESL_PACKET * pPacket,
1571 IN BOOLEAN * pbConsumePacket,
1572 IN size_t BufferLength,
1573 IN UINT8 * pBuffer,
1574 OUT size_t * pDataLength,
1575 OUT struct sockaddr * pAddress,
1576 OUT size_t * pSkipBytes
1577 )
1578 {
1579 size_t DataLength;
1580 struct sockaddr_in6 * pRemoteAddress;
1581 ESL_TCP6_CONTEXT * pTcp6;
1582
1583 DBG_ENTER ( );
1584
1585 //
1586 // Return the remote system address if requested
1587 //
1588 if ( NULL != pAddress ) {
1589 //
1590 // Build the remote address
1591 //
1592 pTcp6 = &pPort->Context.Tcp6;
1593 DEBUG (( DEBUG_RX,
1594 "Getting packet remote address: [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n",
1595 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
1596 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[1],
1597 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[2],
1598 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[3],
1599 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[4],
1600 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[5],
1601 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[6],
1602 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[7],
1603 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[8],
1604 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[9],
1605 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[10],
1606 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[11],
1607 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[12],
1608 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[13],
1609 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[14],
1610 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[15],
1611 pTcp6->ConfigData.AccessPoint.RemotePort ));
1612 pRemoteAddress = (struct sockaddr_in6 *)pAddress;
1613 CopyMem ( &pRemoteAddress->sin6_addr,
1614 &pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
1615 sizeof ( pRemoteAddress->sin6_addr ));
1616 pRemoteAddress->sin6_port = SwapBytes16 ( pTcp6->ConfigData.AccessPoint.RemotePort );
1617 }
1618
1619 //
1620 // Determine the amount of received data
1621 //
1622 DataLength = pPacket->ValidBytes;
1623 if ( BufferLength < DataLength ) {
1624 DataLength = BufferLength;
1625 }
1626
1627 //
1628 // Move the data into the buffer
1629 //
1630 DEBUG (( DEBUG_RX,
1631 "0x%08x: Port copy packet 0x%08x data into 0x%08x, 0x%08x bytes\r\n",
1632 pPort,
1633 pPacket,
1634 pBuffer,
1635 DataLength ));
1636 CopyMem ( pBuffer, pPacket->pBuffer, DataLength );
1637
1638 //
1639 // Set the next buffer address
1640 //
1641 pBuffer += DataLength;
1642
1643 //
1644 // Determine if the data is being read
1645 //
1646 if ( *pbConsumePacket ) {
1647 //
1648 // Account for the bytes consumed
1649 //
1650 pPacket->pBuffer += DataLength;
1651 pPacket->ValidBytes -= DataLength;
1652 DEBUG (( DEBUG_RX,
1653 "0x%08x: Port account for 0x%08x bytes\r\n",
1654 pPort,
1655 DataLength ));
1656
1657 //
1658 // Determine if the entire packet was consumed
1659 //
1660 if (( 0 == pPacket->ValidBytes )
1661 || ( SOCK_STREAM != pPort->pSocket->Type )) {
1662 //
1663 // All done with this packet
1664 // Account for any discarded data
1665 //
1666 *pSkipBytes = pPacket->ValidBytes;
1667 }
1668 else
1669 {
1670 //
1671 // More data to consume later
1672 //
1673 *pbConsumePacket = FALSE;
1674 }
1675 }
1676
1677 //
1678 // Return the data length and the buffer address
1679 //
1680 *pDataLength = DataLength;
1681 DBG_EXIT_HEX ( pBuffer );
1682 return pBuffer;
1683 }
1684
1685
1686 /**
1687 Get the remote socket address.
1688
1689 This routine returns the address of the remote connection point
1690 associated with the SOCK_STREAM or SOCK_SEQPACKET socket.
1691
1692 This routine is called by ::EslSocketGetPeerAddress to detemine
1693 the TCPv6 address and por number associated with the network adapter.
1694
1695 @param [in] pPort Address of an ::ESL_PORT structure.
1696
1697 @param [out] pAddress Network address to receive the remote system address
1698
1699 **/
1700 VOID
1701 EslTcp6RemoteAddressGet (
1702 IN ESL_PORT * pPort,
1703 OUT struct sockaddr * pAddress
1704 )
1705 {
1706 struct sockaddr_in6 * pRemoteAddress;
1707 ESL_TCP6_CONTEXT * pTcp6;
1708
1709 DBG_ENTER ( );
1710
1711 //
1712 // Return the remote address
1713 //
1714 pTcp6 = &pPort->Context.Tcp6;
1715 pRemoteAddress = (struct sockaddr_in6 *)pAddress;
1716 pRemoteAddress->sin6_family = AF_INET6;
1717 pRemoteAddress->sin6_port = SwapBytes16 ( pTcp6->ConfigData.AccessPoint.RemotePort );
1718 CopyMem ( &pRemoteAddress->sin6_addr,
1719 &pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
1720 sizeof ( pRemoteAddress->sin6_addr ));
1721
1722 DBG_EXIT ( );
1723 }
1724
1725
1726 /**
1727 Set the remote address
1728
1729 This routine sets the remote address in the port.
1730
1731 This routine is called by ::EslSocketConnect to specify the
1732 remote network address.
1733
1734 @param [in] pPort Address of an ::ESL_PORT structure.
1735
1736 @param [in] pSockAddr Network address of the remote system.
1737
1738 @param [in] SockAddrLength Length in bytes of the network address.
1739
1740 @retval EFI_SUCCESS The operation was successful
1741
1742 **/
1743 EFI_STATUS
1744 EslTcp6RemoteAddressSet (
1745 IN ESL_PORT * pPort,
1746 IN CONST struct sockaddr * pSockAddr,
1747 IN socklen_t SockAddrLength
1748 )
1749 {
1750 CONST struct sockaddr_in6 * pRemoteAddress;
1751 ESL_TCP6_CONTEXT * pTcp6;
1752 EFI_STATUS Status;
1753
1754 DBG_ENTER ( );
1755
1756 //
1757 // Set the remote address
1758 //
1759 pTcp6 = &pPort->Context.Tcp6;
1760 pRemoteAddress = (struct sockaddr_in6 *)pSockAddr;
1761 CopyMem ( &pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr [ 0 ],
1762 &pRemoteAddress->sin6_addr.__u6_addr.__u6_addr8 [ 0 ],
1763 sizeof ( pRemoteAddress->sin6_addr.__u6_addr.__u6_addr8 ));
1764 pTcp6->ConfigData.AccessPoint.RemotePort = SwapBytes16 ( pRemoteAddress->sin6_port );
1765 Status = EFI_SUCCESS;
1766
1767 //
1768 // TODO: Fix the following check
1769 //
1770 /*
1771 if ( INADDR_BROADCAST == pRemoteAddress->sin6_addr.s_addr ) {
1772 DEBUG (( DEBUG_CONNECT,
1773 "ERROR - Invalid remote address\r\n" ));
1774 Status = EFI_INVALID_PARAMETER;
1775 pPort->pSocket->errno = EAFNOSUPPORT;
1776 }
1777 */
1778
1779 //
1780 // Return the operation status
1781 //
1782 DBG_EXIT_STATUS ( Status );
1783 return Status;
1784 }
1785
1786
1787 /**
1788 Process the receive completion
1789
1790 This routine queues the data in FIFO order in either the urgent
1791 or normal data queues depending upon the type of data received.
1792 See the \ref ReceiveEngine section.
1793
1794 This routine is called by the TCPv6 driver when some data is
1795 received.
1796
1797 Buffer the data that was just received.
1798
1799 @param [in] Event The receive completion event
1800
1801 @param [in] pIo Address of an ::ESL_IO_MGMT structure
1802
1803 **/
1804 VOID
1805 EslTcp6RxComplete (
1806 IN EFI_EVENT Event,
1807 IN ESL_IO_MGMT * pIo
1808 )
1809 {
1810 BOOLEAN bUrgent;
1811 size_t LengthInBytes;
1812 ESL_PACKET * pPacket;
1813 EFI_STATUS Status;
1814
1815 DBG_ENTER ( );
1816
1817 //
1818 // Get the operation status.
1819 //
1820 Status = pIo->Token.Tcp6Rx.CompletionToken.Status;
1821
1822 //
1823 // +--------------------+ +---------------------------+
1824 // | ESL_IO_MGMT | | ESL_PACKET |
1825 // | | | |
1826 // | +---------------+ +-----------------------+ |
1827 // | | Token | | EFI_Tcp6_RECEIVE_DATA | |
1828 // | | RxData --> | | |
1829 // | | | +-----------------------+---+
1830 // | | Event | | Data Buffer |
1831 // +----+---------------+ | |
1832 // | |
1833 // +---------------------------+
1834 //
1835 //
1836 // Duplicate the buffer address and length for use by the
1837 // buffer handling code in EslTcp6Receive. These fields are
1838 // used when a partial read is done of the data from the
1839 // packet.
1840 //
1841 pPacket = pIo->pPacket;
1842 pPacket->pBuffer = pPacket->Op.Tcp6Rx.RxData.FragmentTable[0].FragmentBuffer;
1843 LengthInBytes = pPacket->Op.Tcp6Rx.RxData.DataLength;
1844 pPacket->ValidBytes = LengthInBytes;
1845
1846 //
1847 // Get the data type so that it may be linked to the
1848 // correct receive buffer list on the ESL_SOCKET structure
1849 //
1850 bUrgent = pPacket->Op.Tcp6Rx.RxData.UrgentFlag;
1851
1852 //
1853 // Complete this request
1854 //
1855 EslSocketRxComplete ( pIo, Status, LengthInBytes, bUrgent );
1856 DBG_EXIT ( );
1857 }
1858
1859
1860 /**
1861 Start a receive operation
1862
1863 This routine posts a receive buffer to the TCPv6 driver.
1864 See the \ref ReceiveEngine section.
1865
1866 This support routine is called by EslSocketRxStart.
1867
1868 @param [in] pPort Address of an ::ESL_PORT structure.
1869 @param [in] pIo Address of an ::ESL_IO_MGMT structure.
1870
1871 **/
1872 VOID
1873 EslTcp6RxStart (
1874 IN ESL_PORT * pPort,
1875 IN ESL_IO_MGMT * pIo
1876 )
1877 {
1878 ESL_PACKET * pPacket;
1879
1880 DBG_ENTER ( );
1881
1882 //
1883 // Initialize the buffer for receive
1884 //
1885 pPacket = pIo->pPacket;
1886 pIo->Token.Tcp6Rx.Packet.RxData = &pPacket->Op.Tcp6Rx.RxData;
1887 pPacket->Op.Tcp6Rx.RxData.DataLength = sizeof ( pPacket->Op.Tcp6Rx.Buffer );
1888 pPacket->Op.Tcp6Rx.RxData.FragmentCount = 1;
1889 pPacket->Op.Tcp6Rx.RxData.FragmentTable[0].FragmentLength = pPacket->Op.Tcp6Rx.RxData.DataLength;
1890 pPacket->Op.Tcp6Rx.RxData.FragmentTable[0].FragmentBuffer = &pPacket->Op.Tcp6Rx.Buffer[0];
1891
1892 DBG_EXIT ( );
1893 }
1894
1895
1896 /**
1897 Determine if the socket is configured.
1898
1899 This routine uses the flag ESL_SOCKET::bConfigured to determine
1900 if the network layer's configuration routine has been called.
1901
1902 This routine is called by EslSocketIsConfigured to verify
1903 that the socket has been configured.
1904
1905 @param [in] pSocket Address of an ::ESL_SOCKET structure.
1906
1907 @retval EFI_SUCCESS - The port is connected
1908 @retval EFI_NOT_STARTED - The port is not connected
1909
1910 **/
1911 EFI_STATUS
1912 EslTcp6SocketIsConfigured (
1913 IN ESL_SOCKET * pSocket
1914 )
1915 {
1916 EFI_STATUS Status;
1917
1918 DBG_ENTER ( );
1919
1920 //
1921 // Determine the socket configuration status
1922 //
1923 Status = pSocket->bConfigured ? EFI_SUCCESS : EFI_NOT_STARTED;
1924
1925 //
1926 // Return the port connected state.
1927 //
1928 DBG_EXIT_STATUS ( Status );
1929 return Status;
1930 }
1931
1932
1933 /**
1934 Buffer data for transmission over a network connection.
1935
1936 This routine buffers data for the transmit engine in one of two
1937 queues, one for urgent (out-of-band) data and the other for normal
1938 data. The urgent data is provided to TCP as soon as it is available,
1939 allowing the TCP layer to schedule transmission of the urgent data
1940 between packets of normal data.
1941
1942 This routine is called by ::EslSocketTransmit to buffer
1943 data for transmission. When the \ref TransmitEngine has resources,
1944 this routine will start the transmission of the next buffer on
1945 the network connection.
1946
1947 Transmission errors are returned during the next transmission or
1948 during the close operation. Only buffering errors are returned
1949 during the current transmission attempt.
1950
1951 @param [in] pSocket Address of an ::ESL_SOCKET structure
1952
1953 @param [in] Flags Message control flags
1954
1955 @param [in] BufferLength Length of the the buffer
1956
1957 @param [in] pBuffer Address of a buffer to receive the data.
1958
1959 @param [in] pDataLength Number of received data bytes in the buffer.
1960
1961 @param [in] pAddress Network address of the remote system address
1962
1963 @param [in] AddressLength Length of the remote network address structure
1964
1965 @retval EFI_SUCCESS - Socket data successfully buffered
1966
1967 **/
1968 EFI_STATUS
1969 EslTcp6TxBuffer (
1970 IN ESL_SOCKET * pSocket,
1971 IN int Flags,
1972 IN size_t BufferLength,
1973 IN CONST UINT8 * pBuffer,
1974 OUT size_t * pDataLength,
1975 IN const struct sockaddr * pAddress,
1976 IN socklen_t AddressLength
1977 )
1978 {
1979 BOOLEAN bUrgent;
1980 BOOLEAN bUrgentQueue;
1981 ESL_PACKET * pPacket;
1982 ESL_IO_MGMT ** ppActive;
1983 ESL_IO_MGMT ** ppFree;
1984 ESL_PORT * pPort;
1985 ESL_PACKET ** ppQueueHead;
1986 ESL_PACKET ** ppQueueTail;
1987 ESL_PACKET * pPreviousPacket;
1988 size_t * pTxBytes;
1989 EFI_TCP6_TRANSMIT_DATA * pTxData;
1990 EFI_STATUS Status;
1991 EFI_TPL TplPrevious;
1992
1993 DBG_ENTER ( );
1994
1995 //
1996 // Assume failure
1997 //
1998 Status = EFI_UNSUPPORTED;
1999 pSocket->errno = ENOTCONN;
2000 *pDataLength = 0;
2001
2002 //
2003 // Verify that the socket is connected
2004 //
2005 if ( SOCKET_STATE_CONNECTED == pSocket->State ) {
2006 //
2007 // Locate the port
2008 //
2009 pPort = pSocket->pPortList;
2010 if ( NULL != pPort ) {
2011 //
2012 // Determine the queue head
2013 //
2014 bUrgent = (BOOLEAN)( 0 != ( Flags & MSG_OOB ));
2015 bUrgentQueue = bUrgent
2016 && ( !pSocket->bOobInLine )
2017 && pSocket->pApi->bOobSupported;
2018 if ( bUrgentQueue ) {
2019 ppQueueHead = &pSocket->pTxOobPacketListHead;
2020 ppQueueTail = &pSocket->pTxOobPacketListTail;
2021 ppActive = &pPort->pTxOobActive;
2022 ppFree = &pPort->pTxOobFree;
2023 pTxBytes = &pSocket->TxOobBytes;
2024 }
2025 else {
2026 ppQueueHead = &pSocket->pTxPacketListHead;
2027 ppQueueTail = &pSocket->pTxPacketListTail;
2028 ppActive = &pPort->pTxActive;
2029 ppFree = &pPort->pTxFree;
2030 pTxBytes = &pSocket->TxBytes;
2031 }
2032
2033 //
2034 // Verify that there is enough room to buffer another
2035 // transmit operation
2036 //
2037 if ( pSocket->MaxTxBuf > *pTxBytes ) {
2038 if ( pPort->bTxFlowControl ) {
2039 DEBUG (( DEBUG_TX,
2040 "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\r\n0x%08x: pPort, TX flow control released, Max bytes: %d > %d bufferred bytes\r\n",
2041 pPort,
2042 pSocket->MaxTxBuf,
2043 *pTxBytes ));
2044 pPort->bTxFlowControl = FALSE;
2045 }
2046
2047 //
2048 // Attempt to allocate the packet
2049 //
2050 Status = EslSocketPacketAllocate ( &pPacket,
2051 sizeof ( pPacket->Op.Tcp6Tx )
2052 - sizeof ( pPacket->Op.Tcp6Tx.Buffer )
2053 + BufferLength,
2054 0,
2055 DEBUG_TX );
2056 if ( !EFI_ERROR ( Status )) {
2057 //
2058 // Initialize the transmit operation
2059 //
2060 pTxData = &pPacket->Op.Tcp6Tx.TxData;
2061 pTxData->Push = TRUE || bUrgent;
2062 pTxData->Urgent = bUrgent;
2063 pTxData->DataLength = (UINT32) BufferLength;
2064 pTxData->FragmentCount = 1;
2065 pTxData->FragmentTable[0].FragmentLength = (UINT32) BufferLength;
2066 pTxData->FragmentTable[0].FragmentBuffer = &pPacket->Op.Tcp6Tx.Buffer[0];
2067
2068 //
2069 // Copy the data into the buffer
2070 //
2071 CopyMem ( &pPacket->Op.Tcp6Tx.Buffer[0],
2072 pBuffer,
2073 BufferLength );
2074
2075 //
2076 // Synchronize with the socket layer
2077 //
2078 RAISE_TPL ( TplPrevious, TPL_SOCKETS );
2079
2080 //
2081 // Stop transmission after an error
2082 //
2083 if ( !EFI_ERROR ( pSocket->TxError )) {
2084 //
2085 // Display the request
2086 //
2087 DEBUG (( DEBUG_TX,
2088 "Send %d %s bytes from 0x%08x\r\n",
2089 BufferLength,
2090 bUrgent ? L"urgent" : L"normal",
2091 pBuffer ));
2092
2093 //
2094 // Queue the data for transmission
2095 //
2096 pPacket->pNext = NULL;
2097 pPreviousPacket = *ppQueueTail;
2098 if ( NULL == pPreviousPacket ) {
2099 *ppQueueHead = pPacket;
2100 }
2101 else {
2102 pPreviousPacket->pNext = pPacket;
2103 }
2104 *ppQueueTail = pPacket;
2105 DEBUG (( DEBUG_TX,
2106 "0x%08x: Packet on %s transmit list\r\n",
2107 pPacket,
2108 bUrgentQueue ? L"urgent" : L"normal" ));
2109
2110 //
2111 // Account for the buffered data
2112 //
2113 *pTxBytes += BufferLength;
2114 *pDataLength = BufferLength;
2115
2116 //
2117 // Start the transmit engine if it is idle
2118 //
2119 if ( NULL != *ppFree ) {
2120 EslSocketTxStart ( pPort,
2121 ppQueueHead,
2122 ppQueueTail,
2123 ppActive,
2124 ppFree );
2125 }
2126 }
2127 else {
2128 //
2129 // Previous transmit error
2130 // Stop transmission
2131 //
2132 Status = pSocket->TxError;
2133 pSocket->errno = EIO;
2134
2135 //
2136 // Free the packet
2137 //
2138 EslSocketPacketFree ( pPacket, DEBUG_TX );
2139 }
2140
2141 //
2142 // Release the socket layer synchronization
2143 //
2144 RESTORE_TPL ( TplPrevious );
2145 }
2146 else {
2147 //
2148 // Packet allocation failed
2149 //
2150 pSocket->errno = ENOMEM;
2151 }
2152 }
2153 else {
2154 if ( !pPort->bTxFlowControl ) {
2155 DEBUG (( DEBUG_TX,
2156 "0x%08x: pPort, TX flow control applied, Max bytes %d <= %d bufferred bytes\r\nTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\r\n",
2157 pPort,
2158 pSocket->MaxTxBuf,
2159 *pTxBytes ));
2160 pPort->bTxFlowControl = TRUE;
2161 }
2162 //
2163 // Not enough buffer space available
2164 //
2165 pSocket->errno = EAGAIN;
2166 Status = EFI_NOT_READY;
2167 }
2168 }
2169 }
2170
2171 //
2172 // Return the operation status
2173 //
2174 DBG_EXIT_STATUS ( Status );
2175 return Status;
2176 }
2177
2178
2179 /**
2180 Process the normal data transmit completion
2181
2182 This routine use ::EslSocketTxComplete to perform the transmit
2183 completion processing for normal data.
2184
2185 This routine is called by the TCPv6 network layer when a
2186 normal data transmit request completes.
2187
2188 @param [in] Event The normal transmit completion event
2189
2190 @param [in] pIo The ESL_IO_MGMT structure address
2191
2192 **/
2193 VOID
2194 EslTcp6TxComplete (
2195 IN EFI_EVENT Event,
2196 IN ESL_IO_MGMT * pIo
2197 )
2198 {
2199 UINT32 LengthInBytes;
2200 ESL_PACKET * pPacket;
2201 ESL_PORT * pPort;
2202 ESL_SOCKET * pSocket;
2203 EFI_STATUS Status;
2204
2205 DBG_ENTER ( );
2206
2207 //
2208 // Locate the active transmit packet
2209 //
2210 pPacket = pIo->pPacket;
2211 pPort = pIo->pPort;
2212 pSocket = pPort->pSocket;
2213
2214 //
2215 // Get the transmit length and status
2216 //
2217 LengthInBytes = pPacket->Op.Tcp6Tx.TxData.DataLength;
2218 pSocket->TxBytes -= LengthInBytes;
2219 Status = pIo->Token.Tcp6Tx.CompletionToken.Status;
2220
2221 //
2222 // Complete the transmit operation
2223 //
2224 EslSocketTxComplete ( pIo,
2225 LengthInBytes,
2226 Status,
2227 "Normal ",
2228 &pSocket->pTxPacketListHead,
2229 &pSocket->pTxPacketListTail,
2230 &pPort->pTxActive,
2231 &pPort->pTxFree );
2232 DBG_EXIT ( );
2233 }
2234
2235
2236 /**
2237 Process the urgent data transmit completion
2238
2239 This routine use ::EslSocketTxComplete to perform the transmit
2240 completion processing for urgent data.
2241
2242 This routine is called by the TCPv6 network layer when a
2243 urgent data transmit request completes.
2244
2245 @param [in] Event The urgent transmit completion event
2246
2247 @param [in] pIo The ESL_IO_MGMT structure address
2248
2249 **/
2250 VOID
2251 EslTcp6TxOobComplete (
2252 IN EFI_EVENT Event,
2253 IN ESL_IO_MGMT * pIo
2254 )
2255 {
2256 UINT32 LengthInBytes;
2257 ESL_PACKET * pPacket;
2258 ESL_PORT * pPort;
2259 ESL_SOCKET * pSocket;
2260 EFI_STATUS Status;
2261
2262 DBG_ENTER ( );
2263
2264 //
2265 // Locate the active transmit packet
2266 //
2267 pPacket = pIo->pPacket;
2268 pPort = pIo->pPort;
2269 pSocket = pPort->pSocket;
2270
2271 //
2272 // Get the transmit length and status
2273 //
2274 LengthInBytes = pPacket->Op.Tcp6Tx.TxData.DataLength;
2275 pSocket->TxOobBytes -= LengthInBytes;
2276 Status = pIo->Token.Tcp6Tx.CompletionToken.Status;
2277
2278 //
2279 // Complete the transmit operation
2280 //
2281 EslSocketTxComplete ( pIo,
2282 LengthInBytes,
2283 Status,
2284 "Urgent ",
2285 &pSocket->pTxOobPacketListHead,
2286 &pSocket->pTxOobPacketListTail,
2287 &pPort->pTxOobActive,
2288 &pPort->pTxOobFree );
2289 DBG_EXIT ( );
2290 }
2291
2292
2293 /**
2294 Verify the adapter's IP address
2295
2296 This support routine is called by EslSocketBindTest.
2297
2298 @param [in] pPort Address of an ::ESL_PORT structure.
2299 @param [in] pConfigData Address of the configuration data
2300
2301 @retval EFI_SUCCESS - The IP address is valid
2302 @retval EFI_NOT_STARTED - The IP address is invalid
2303
2304 **/
2305 EFI_STATUS
2306 EslTcp6VerifyLocalIpAddress (
2307 IN ESL_PORT * pPort,
2308 IN EFI_TCP6_CONFIG_DATA * pConfigData
2309 )
2310 {
2311 UINTN AddressCount;
2312 EFI_IP6_ADDRESS_INFO * pAddressInfo;
2313 UINTN DataSize;
2314 EFI_TCP6_ACCESS_POINT * pAccess;
2315 EFI_IP6_CONFIG_INTERFACE_INFO * pIpConfigData;
2316 EFI_IP6_CONFIG_PROTOCOL * pIpConfigProtocol;
2317 ESL_SERVICE * pService;
2318 EFI_STATUS Status;
2319
2320 DBG_ENTER ( );
2321
2322 //
2323 // Use break instead of goto
2324 //
2325 pIpConfigData = NULL;
2326 for ( ; ; ) {
2327 //
2328 // Determine if the IP address is specified
2329 //
2330 pAccess = &pConfigData->AccessPoint;
2331 DEBUG (( DEBUG_BIND,
2332 "Requested IP address: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\r\n",
2333 pAccess->StationAddress.Addr[0],
2334 pAccess->StationAddress.Addr[1],
2335 pAccess->StationAddress.Addr[2],
2336 pAccess->StationAddress.Addr[3],
2337 pAccess->StationAddress.Addr[4],
2338 pAccess->StationAddress.Addr[5],
2339 pAccess->StationAddress.Addr[6],
2340 pAccess->StationAddress.Addr[7],
2341 pAccess->StationAddress.Addr[8],
2342 pAccess->StationAddress.Addr[9],
2343 pAccess->StationAddress.Addr[10],
2344 pAccess->StationAddress.Addr[11],
2345 pAccess->StationAddress.Addr[12],
2346 pAccess->StationAddress.Addr[13],
2347 pAccess->StationAddress.Addr[14],
2348 pAccess->StationAddress.Addr[15]));
2349 if (( 0 == pAccess->StationAddress.Addr [ 0 ])
2350 && ( 0 == pAccess->StationAddress.Addr [ 1 ])
2351 && ( 0 == pAccess->StationAddress.Addr [ 2 ])
2352 && ( 0 == pAccess->StationAddress.Addr [ 3 ])
2353 && ( 0 == pAccess->StationAddress.Addr [ 4 ])
2354 && ( 0 == pAccess->StationAddress.Addr [ 5 ])
2355 && ( 0 == pAccess->StationAddress.Addr [ 6 ])
2356 && ( 0 == pAccess->StationAddress.Addr [ 7 ])
2357 && ( 0 == pAccess->StationAddress.Addr [ 8 ])
2358 && ( 0 == pAccess->StationAddress.Addr [ 9 ])
2359 && ( 0 == pAccess->StationAddress.Addr [ 10 ])
2360 && ( 0 == pAccess->StationAddress.Addr [ 11 ])
2361 && ( 0 == pAccess->StationAddress.Addr [ 12 ])
2362 && ( 0 == pAccess->StationAddress.Addr [ 13 ])
2363 && ( 0 == pAccess->StationAddress.Addr [ 14 ])
2364 && ( 0 == pAccess->StationAddress.Addr [ 15 ]))
2365 {
2366 Status = EFI_SUCCESS;
2367 break;
2368 }
2369
2370 //
2371 // Open the configuration protocol
2372 //
2373 pService = pPort->pService;
2374 Status = gBS->OpenProtocol ( pService->Controller,
2375 &gEfiIp6ConfigProtocolGuid,
2376 (VOID **)&pIpConfigProtocol,
2377 NULL,
2378 NULL,
2379 EFI_OPEN_PROTOCOL_GET_PROTOCOL );
2380 if ( EFI_ERROR ( Status )) {
2381 DEBUG (( DEBUG_ERROR,
2382 "ERROR - IP Configuration Protocol not available, Status: %r\r\n",
2383 Status ));
2384 break;
2385 }
2386
2387 //
2388 // Get the IP configuration data size
2389 //
2390 DataSize = 0;
2391 Status = pIpConfigProtocol->GetData ( pIpConfigProtocol,
2392 Ip6ConfigDataTypeInterfaceInfo,
2393 &DataSize,
2394 NULL );
2395 if ( EFI_BUFFER_TOO_SMALL != Status ) {
2396 DEBUG (( DEBUG_ERROR,
2397 "ERROR - Failed to get IP Configuration data size, Status: %r\r\n",
2398 Status ));
2399 break;
2400 }
2401
2402 //
2403 // Allocate the configuration data buffer
2404 //
2405 pIpConfigData = AllocatePool ( DataSize );
2406 if ( NULL == pIpConfigData ) {
2407 DEBUG (( DEBUG_ERROR,
2408 "ERROR - Not enough memory to allocate IP Configuration data!\r\n" ));
2409 Status = EFI_OUT_OF_RESOURCES;
2410 break;
2411 }
2412
2413 //
2414 // Get the IP configuration
2415 //
2416 Status = pIpConfigProtocol->GetData ( pIpConfigProtocol,
2417 Ip6ConfigDataTypeInterfaceInfo,
2418 &DataSize,
2419 pIpConfigData );
2420 if ( EFI_ERROR ( Status )) {
2421 DEBUG (( DEBUG_ERROR,
2422 "ERROR - Failed to return IP Configuration data, Status: %r\r\n",
2423 Status ));
2424 break;
2425 }
2426
2427 //
2428 // Display the current configuration
2429 //
2430 DEBUG (( DEBUG_BIND,
2431 "Actual adapter IP address: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\r\n",
2432 pIpConfigData->HwAddress.Addr [ 0 ],
2433 pIpConfigData->HwAddress.Addr [ 1 ],
2434 pIpConfigData->HwAddress.Addr [ 2 ],
2435 pIpConfigData->HwAddress.Addr [ 3 ],
2436 pIpConfigData->HwAddress.Addr [ 4 ],
2437 pIpConfigData->HwAddress.Addr [ 5 ],
2438 pIpConfigData->HwAddress.Addr [ 6 ],
2439 pIpConfigData->HwAddress.Addr [ 7 ],
2440 pIpConfigData->HwAddress.Addr [ 8 ],
2441 pIpConfigData->HwAddress.Addr [ 9 ],
2442 pIpConfigData->HwAddress.Addr [ 10 ],
2443 pIpConfigData->HwAddress.Addr [ 11 ],
2444 pIpConfigData->HwAddress.Addr [ 12 ],
2445 pIpConfigData->HwAddress.Addr [ 13 ],
2446 pIpConfigData->HwAddress.Addr [ 14 ],
2447 pIpConfigData->HwAddress.Addr [ 15 ]));
2448
2449 //
2450 // Validate the hardware address
2451 //
2452 Status = EFI_SUCCESS;
2453 if (( 16 == pIpConfigData->HwAddressSize )
2454 && ( pAccess->StationAddress.Addr [ 0 ] == pIpConfigData->HwAddress.Addr [ 0 ])
2455 && ( pAccess->StationAddress.Addr [ 1 ] == pIpConfigData->HwAddress.Addr [ 1 ])
2456 && ( pAccess->StationAddress.Addr [ 2 ] == pIpConfigData->HwAddress.Addr [ 2 ])
2457 && ( pAccess->StationAddress.Addr [ 3 ] == pIpConfigData->HwAddress.Addr [ 3 ])
2458 && ( pAccess->StationAddress.Addr [ 4 ] == pIpConfigData->HwAddress.Addr [ 4 ])
2459 && ( pAccess->StationAddress.Addr [ 5 ] == pIpConfigData->HwAddress.Addr [ 5 ])
2460 && ( pAccess->StationAddress.Addr [ 6 ] == pIpConfigData->HwAddress.Addr [ 6 ])
2461 && ( pAccess->StationAddress.Addr [ 7 ] == pIpConfigData->HwAddress.Addr [ 7 ])
2462 && ( pAccess->StationAddress.Addr [ 8 ] == pIpConfigData->HwAddress.Addr [ 8 ])
2463 && ( pAccess->StationAddress.Addr [ 9 ] == pIpConfigData->HwAddress.Addr [ 9 ])
2464 && ( pAccess->StationAddress.Addr [ 10 ] == pIpConfigData->HwAddress.Addr [ 10 ])
2465 && ( pAccess->StationAddress.Addr [ 11 ] == pIpConfigData->HwAddress.Addr [ 11 ])
2466 && ( pAccess->StationAddress.Addr [ 12 ] == pIpConfigData->HwAddress.Addr [ 12 ])
2467 && ( pAccess->StationAddress.Addr [ 13 ] == pIpConfigData->HwAddress.Addr [ 13 ])
2468 && ( pAccess->StationAddress.Addr [ 14 ] == pIpConfigData->HwAddress.Addr [ 14 ])
2469 && ( pAccess->StationAddress.Addr [ 15 ] == pIpConfigData->HwAddress.Addr [ 15 ])) {
2470 break;
2471 }
2472
2473 //
2474 // Walk the list of other IP addresses assigned to this adapter
2475 //
2476 for ( AddressCount = 0; pIpConfigData->AddressInfoCount > AddressCount; AddressCount += 1 ) {
2477 pAddressInfo = &pIpConfigData->AddressInfo [ AddressCount ];
2478
2479 //
2480 // Display the IP address
2481 //
2482 DEBUG (( DEBUG_BIND,
2483 "Actual adapter IP address: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\r\n",
2484 pAddressInfo->Address.Addr [ 0 ],
2485 pAddressInfo->Address.Addr [ 1 ],
2486 pAddressInfo->Address.Addr [ 2 ],
2487 pAddressInfo->Address.Addr [ 3 ],
2488 pAddressInfo->Address.Addr [ 4 ],
2489 pAddressInfo->Address.Addr [ 5 ],
2490 pAddressInfo->Address.Addr [ 6 ],
2491 pAddressInfo->Address.Addr [ 7 ],
2492 pAddressInfo->Address.Addr [ 8 ],
2493 pAddressInfo->Address.Addr [ 9 ],
2494 pAddressInfo->Address.Addr [ 10 ],
2495 pAddressInfo->Address.Addr [ 11 ],
2496 pAddressInfo->Address.Addr [ 12 ],
2497 pAddressInfo->Address.Addr [ 13 ],
2498 pAddressInfo->Address.Addr [ 14 ],
2499 pAddressInfo->Address.Addr [ 15 ]));
2500
2501 //
2502 // Validate the IP address
2503 //
2504 if (( pAccess->StationAddress.Addr [ 0 ] == pAddressInfo->Address.Addr [ 0 ])
2505 && ( pAccess->StationAddress.Addr [ 1 ] == pAddressInfo->Address.Addr [ 1 ])
2506 && ( pAccess->StationAddress.Addr [ 2 ] == pAddressInfo->Address.Addr [ 2 ])
2507 && ( pAccess->StationAddress.Addr [ 3 ] == pAddressInfo->Address.Addr [ 3 ])
2508 && ( pAccess->StationAddress.Addr [ 4 ] == pAddressInfo->Address.Addr [ 4 ])
2509 && ( pAccess->StationAddress.Addr [ 5 ] == pAddressInfo->Address.Addr [ 5 ])
2510 && ( pAccess->StationAddress.Addr [ 6 ] == pAddressInfo->Address.Addr [ 6 ])
2511 && ( pAccess->StationAddress.Addr [ 7 ] == pAddressInfo->Address.Addr [ 7 ])
2512 && ( pAccess->StationAddress.Addr [ 8 ] == pAddressInfo->Address.Addr [ 8 ])
2513 && ( pAccess->StationAddress.Addr [ 9 ] == pAddressInfo->Address.Addr [ 9 ])
2514 && ( pAccess->StationAddress.Addr [ 10 ] == pAddressInfo->Address.Addr [ 10 ])
2515 && ( pAccess->StationAddress.Addr [ 11 ] == pAddressInfo->Address.Addr [ 11 ])
2516 && ( pAccess->StationAddress.Addr [ 12 ] == pAddressInfo->Address.Addr [ 12 ])
2517 && ( pAccess->StationAddress.Addr [ 13 ] == pAddressInfo->Address.Addr [ 13 ])
2518 && ( pAccess->StationAddress.Addr [ 14 ] == pAddressInfo->Address.Addr [ 14 ])
2519 && ( pAccess->StationAddress.Addr [ 15 ] == pAddressInfo->Address.Addr [ 15 ])) {
2520 break;
2521 }
2522 }
2523 if ( pIpConfigData->AddressInfoCount > AddressCount ) {
2524 break;
2525 }
2526
2527 //
2528 // The IP address did not match
2529 //
2530 Status = EFI_NOT_STARTED;
2531 break;
2532 }
2533
2534 //
2535 // Free the buffer if necessary
2536 //
2537 if ( NULL != pIpConfigData ) {
2538 FreePool ( pIpConfigData );
2539 }
2540
2541 //
2542 // Return the IP address status
2543 //
2544 DBG_EXIT_STATUS ( Status );
2545 return Status;
2546 }
2547
2548
2549 /**
2550 Interface between the socket layer and the network specific
2551 code that supports SOCK_STREAM and SOCK_SEQPACKET sockets
2552 over TCPv6.
2553 **/
2554 CONST ESL_PROTOCOL_API cEslTcp6Api = {
2555 "TCPv6",
2556 IPPROTO_TCP,
2557 OFFSET_OF ( ESL_PORT, Context.Tcp6.ConfigData ),
2558 OFFSET_OF ( ESL_LAYER, pTcp6List ),
2559 sizeof ( struct sockaddr_in6 ),
2560 sizeof ( struct sockaddr_in6 ),
2561 AF_INET6,
2562 sizeof (((ESL_PACKET *)0 )->Op.Tcp6Rx ),
2563 OFFSET_OF ( ESL_PACKET, Op.Tcp6Rx.Buffer ) - OFFSET_OF ( ESL_PACKET, Op ),
2564 OFFSET_OF ( ESL_IO_MGMT, Token.Tcp6Rx.Packet.RxData ),
2565 TRUE,
2566 EADDRINUSE,
2567 EslTcp6Accept,
2568 EslTcp6ConnectPoll,
2569 EslTcp6ConnectStart,
2570 EslTcp6SocketIsConfigured,
2571 EslTcp6LocalAddressGet,
2572 EslTcp6LocalAddressSet,
2573 EslTcp6Listen,
2574 NULL, // OptionGet
2575 NULL, // OptionSet
2576 EslTcp6PacketFree,
2577 EslTcp6PortAllocate,
2578 EslTcp6PortClose,
2579 EslTcp6PortCloseOp,
2580 FALSE,
2581 EslTcp6Receive,
2582 EslTcp6RemoteAddressGet,
2583 EslTcp6RemoteAddressSet,
2584 EslTcp6RxComplete,
2585 EslTcp6RxStart,
2586 EslTcp6TxBuffer,
2587 EslTcp6TxComplete,
2588 EslTcp6TxOobComplete,
2589 (PFN_API_VERIFY_LOCAL_IP_ADDRESS)EslTcp6VerifyLocalIpAddress
2590 };