]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/EfiSocketLib/Tcp6.c
Merged socket development branch:
[mirror_edk2.git] / StdLib / EfiSocketLib / Tcp6.c
1 /** @file
2 Implement the TCP6 driver support for the socket layer.
3
4 Copyright (c) 2011, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13
14 \section ConnectionManagement Connection Management
15
16 The ::EslTcp6Listen routine initially places the SOCK_STREAM or
17 SOCK_SEQPACKET socket into a listen state. When a remote machine
18 makes a connection to the socket, the TCPv6 network layer calls
19 ::EslTcp6ListenComplete to complete the connection processing.
20 EslTcp6ListenComplete manages the connections by placing them in
21 FIFO order in a queue to be serviced by the application. When the
22 number of connections exceeds the backlog (ESL_SOCKET::MaxFifoDepth),
23 the new connection is closed. Eventually, the application indirectly
24 calls ::EslTcp6Accept to remove the next connection from the queue
25 and get the associated socket.
26
27 **/
28
29 #include "Socket.h"
30
31
32 /**
33 Attempt to connect to a remote TCP port
34
35 This routine starts the connection processing for a SOCK_STREAM
36 or SOCK_SEQPAKCET socket using the TCPv6 network layer. It
37 configures the local TCPv6 connection point and then attempts to
38 connect to a remote system. Upon completion, the
39 ::EslTcp6ConnectComplete routine gets called with the connection
40 status.
41
42 This routine is called by ::EslSocketConnect to initiate the TCPv6
43 network specific connect operations. The connection processing is
44 initiated by this routine and finished by ::EslTcp6ConnectComplete.
45 This pair of routines walks through the list of local TCPv6
46 connection points until a connection to the remote system is
47 made.
48
49 @param [in] pSocket Address of an ::ESL_SOCKET structure.
50
51 @retval EFI_SUCCESS The connection was successfully established.
52 @retval EFI_NOT_READY The connection is in progress, call this routine again.
53 @retval Others The connection attempt failed.
54
55 **/
56 EFI_STATUS
57 EslTcp6ConnectStart (
58 IN ESL_SOCKET * pSocket
59 );
60
61
62 /**
63 Process the connection attempt
64
65 A system has initiated a connection attempt with a socket in the
66 listen state. Attempt to complete the connection.
67
68 The TCPv6 layer calls this routine when a connection is made to
69 the socket in the listen state. See the
70 \ref ConnectionManagement section.
71
72 @param [in] Event The listen completion event
73
74 @param [in] pPort Address of an ::ESL_PORT structure.
75
76 **/
77 VOID
78 EslTcp6ListenComplete (
79 IN EFI_EVENT Event,
80 IN ESL_PORT * pPort
81 );
82
83
84 /**
85 Accept a network connection.
86
87 This routine waits for a network connection to the socket and
88 returns the remote network address to the caller if requested.
89
90 This routine is called by ::EslSocketAccept to handle the TCPv6 protocol
91 specific accept operations for SOCK_STREAM and SOCK_SEQPACKET sockets.
92 See the \ref ConnectionManagement section.
93
94 @param [in] pSocket Address of an ::ESL_SOCKET structure.
95
96 @param [in] pSockAddr Address of a buffer to receive the remote
97 network address.
98
99 @param [in, out] pSockAddrLength Length in bytes of the address buffer.
100 On output specifies the length of the
101 remote network address.
102
103 @retval EFI_SUCCESS Remote address is available
104 @retval Others Remote address not available
105
106 **/
107 EFI_STATUS
108 EslTcp6Accept (
109 IN ESL_SOCKET * pSocket,
110 IN struct sockaddr * pSockAddr,
111 IN OUT socklen_t * pSockAddrLength
112 )
113 {
114 ESL_PORT * pPort;
115 struct sockaddr_in6 * pRemoteAddress;
116 ESL_TCP6_CONTEXT * pTcp6;
117 EFI_STATUS Status;
118
119 DBG_ENTER ( );
120
121 //
122 // Validate the socket length
123 //
124 pRemoteAddress = (struct sockaddr_in6 *) pSockAddr;
125 if (( NULL == pSockAddrLength )
126 || ( sizeof ( *pRemoteAddress ) > *pSockAddrLength )) {
127 //
128 // Invalid socket address
129 //
130 Status = EFI_INVALID_PARAMETER;
131 pSocket->errno = EINVAL;
132 DEBUG (( DEBUG_ACCEPT,
133 "ERROR - Invalid address length\r\n" ));
134 }
135 else {
136 //
137 // Assume success
138 //
139 Status = EFI_SUCCESS;
140
141 //
142 // Locate the address context
143 //
144 pPort = pSocket->pPortList;
145 pTcp6 = &pPort->Context.Tcp6;
146
147 //
148 // Fill-in the remote address structure
149 //
150 ZeroMem ( pRemoteAddress, sizeof ( *pRemoteAddress ));
151 pRemoteAddress->sin6_len = sizeof ( *pRemoteAddress );
152 pRemoteAddress->sin6_family = AF_INET6;
153 pRemoteAddress->sin6_port = SwapBytes16 ( pTcp6->ConfigData.AccessPoint.RemotePort );
154 CopyMem ( &pRemoteAddress->sin6_addr.__u6_addr.__u6_addr8 [ 0 ],
155 &pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
156 sizeof ( pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr ));
157 }
158
159 //
160 // Return the operation status
161 //
162 DBG_EXIT_STATUS ( Status );
163 return Status;
164 }
165
166
167 /**
168 Process the remote connection completion event.
169
170 This routine handles the completion of a connection attempt. It
171 releases the port (TCPv6 adapter connection) in the case of an
172 error and start a connection attempt on the next port. If the
173 connection attempt was successful then this routine releases all
174 of the other ports.
175
176 This routine is called by the TCPv6 layer when a connect request
177 completes. It sets the ESL_SOCKET::bConnected flag to notify the
178 ::EslTcp6ConnectComplete routine that the connection is available.
179 The flag is set when the connection is established or no more ports
180 exist in the list. The connection status is passed via
181 ESL_SOCKET::ConnectStatus.
182
183 @param [in] Event The connect completion event
184
185 @param [in] pPort Address of an ::ESL_PORT structure.
186
187 **/
188 VOID
189 EslTcp6ConnectComplete (
190 IN EFI_EVENT Event,
191 IN ESL_PORT * pPort
192 )
193 {
194 BOOLEAN bRemoveFirstPort;
195 BOOLEAN bRemovePorts;
196 ESL_PORT * pNextPort;
197 ESL_SOCKET * pSocket;
198 ESL_TCP6_CONTEXT * pTcp6;
199 EFI_STATUS Status;
200
201 DBG_ENTER ( );
202
203 //
204 // Locate the TCP context
205 //
206 pSocket = pPort->pSocket;
207 pTcp6 = &pPort->Context.Tcp6;
208
209 //
210 // Get the connection status
211 //
212 bRemoveFirstPort = FALSE;
213 bRemovePorts = FALSE;
214 Status = pTcp6->ConnectToken.CompletionToken.Status;
215 pSocket->ConnectStatus = Status;
216 if ( !EFI_ERROR ( Status )) {
217 //
218 // The connection was successful
219 //
220 DEBUG (( DEBUG_CONNECT,
221 "0x%08x: Port connected to [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n",
222 pPort,
223 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
224 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[1],
225 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[2],
226 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[3],
227 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[4],
228 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[5],
229 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[6],
230 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[7],
231 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[8],
232 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[9],
233 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[10],
234 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[11],
235 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[12],
236 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[13],
237 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[14],
238 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[15],
239 pTcp6->ConfigData.AccessPoint.RemotePort ));
240
241 //
242 // Remove the rest of the ports
243 //
244 bRemovePorts = TRUE;
245 }
246 else {
247 //
248 // The connection failed
249 //
250 DEBUG (( DEBUG_CONNECT,
251 "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",
252 pPort,
253 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
254 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[1],
255 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[2],
256 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[3],
257 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[4],
258 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[5],
259 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[6],
260 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[7],
261 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[8],
262 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[9],
263 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[10],
264 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[11],
265 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[12],
266 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[13],
267 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[14],
268 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[15],
269 pTcp6->ConfigData.AccessPoint.RemotePort,
270 Status ));
271
272 //
273 // Close the current port
274 //
275 Status = EslSocketPortClose ( pPort );
276 if ( !EFI_ERROR ( Status )) {
277 DEBUG (( DEBUG_CONNECT,
278 "0x%08x: Port closed\r\n",
279 pPort ));
280 }
281 else {
282 DEBUG (( DEBUG_CONNECT,
283 "ERROR - Failed to close port 0x%08x, Status: %r\r\n",
284 pPort,
285 Status ));
286 }
287
288 //
289 // Try to connect using the next port
290 //
291 Status = EslTcp6ConnectStart ( pSocket );
292 if ( EFI_NOT_READY != Status ) {
293 pSocket->ConnectStatus = Status;
294 bRemoveFirstPort = TRUE;
295 }
296 }
297
298 //
299 // Remove the ports if necessary
300 //
301 if ( bRemoveFirstPort || bRemovePorts ) {
302 //
303 // Remove the first port if necessary
304 //
305 pPort = pSocket->pPortList;
306 if (( !bRemoveFirstPort ) && ( NULL != pPort )) {
307 pPort = pPort->pLinkSocket;
308 }
309
310 //
311 // Remove the rest of the list
312 //
313 while ( NULL != pPort ) {
314 pNextPort = pPort->pLinkSocket;
315 EslSocketPortClose ( pPort );
316 if ( !EFI_ERROR ( Status )) {
317 DEBUG (( DEBUG_CONNECT,
318 "0x%08x: Port closed\r\n",
319 pPort ));
320 }
321 else {
322 DEBUG (( DEBUG_CONNECT,
323 "ERROR - Failed to close port 0x%08x, Status: %r\r\n",
324 pPort,
325 Status ));
326 }
327 pPort = pNextPort;
328 }
329
330 //
331 // Notify the poll routine
332 //
333 pSocket->bConnected = TRUE;
334 }
335
336 DBG_EXIT ( );
337 }
338
339
340 /**
341 Poll for completion of the connection attempt.
342
343 This routine polls the ESL_SOCKET::bConnected flag to determine
344 when the connection attempt is complete.
345
346 This routine is called from ::EslSocketConnect to determine when
347 the connection is complete. The ESL_SOCKET::bConnected flag is
348 set by ::EslTcp6ConnectComplete when the TCPv6 layer establishes
349 a connection or runs out of local network adapters. This routine
350 gets the connection status from ESL_SOCKET::ConnectStatus.
351
352 @param [in] pSocket Address of an ::ESL_SOCKET structure.
353
354 @retval EFI_SUCCESS The connection was successfully established.
355 @retval EFI_NOT_READY The connection is in progress, call this routine again.
356 @retval Others The connection attempt failed.
357
358 **/
359 EFI_STATUS
360 EslTcp6ConnectPoll (
361 IN ESL_SOCKET * pSocket
362 )
363 {
364 EFI_STATUS Status;
365
366 DBG_ENTER ( );
367
368 //
369 // Determine if the connection is complete
370 //
371 if ( !pSocket->bConnected ) {
372 //
373 // Not connected
374 //
375 pSocket->errno = EAGAIN;
376 Status = EFI_NOT_READY;
377 }
378 else {
379 //
380 // The connection processing is complete
381 //
382 pSocket->bConnected = FALSE;
383
384 //
385 // Translate the connection status
386 //
387 Status = pSocket->ConnectStatus;
388 switch ( Status ) {
389 default:
390 case EFI_DEVICE_ERROR:
391 pSocket->errno = EIO;
392 break;
393
394 case EFI_ABORTED:
395 pSocket->errno = ECONNREFUSED;
396 break;
397
398 case EFI_INVALID_PARAMETER:
399 pSocket->errno = EINVAL;
400 break;
401
402 case EFI_NO_MAPPING:
403 case EFI_NO_RESPONSE:
404 pSocket->errno = EHOSTUNREACH;
405 break;
406
407 case EFI_NO_MEDIA:
408 pSocket->errno = ENETDOWN;
409 break;
410
411 case EFI_OUT_OF_RESOURCES:
412 pSocket->errno = ENOMEM;
413 break;
414
415 case EFI_SUCCESS:
416 pSocket->errno = 0;
417 pSocket->bConfigured = TRUE;
418 break;
419
420 case EFI_TIMEOUT:
421 pSocket->errno = ETIMEDOUT;
422 break;
423
424 case EFI_UNSUPPORTED:
425 pSocket->errno = ENOTSUP;
426 break;
427
428 case 0x80000069:
429 pSocket->errno = ECONNRESET;
430 break;
431 }
432 }
433
434 //
435 // Return the initialization status
436 //
437 DBG_EXIT_STATUS ( Status );
438 return Status;
439 }
440
441
442 /**
443 Attempt to connect to a remote TCP port
444
445 This routine starts the connection processing for a SOCK_STREAM
446 or SOCK_SEQPAKCET socket using the TCPv6 network layer. It
447 configures the local TCPv6 connection point and then attempts to
448 connect to a remote system. Upon completion, the
449 ::EslTcp6ConnectComplete routine gets called with the connection
450 status.
451
452 This routine is called by ::EslSocketConnect to initiate the TCPv6
453 network specific connect operations. The connection processing is
454 initiated by this routine and finished by ::EslTcp6ConnectComplete.
455 This pair of routines walks through the list of local TCPv6
456 connection points until a connection to the remote system is
457 made.
458
459 @param [in] pSocket Address of an ::ESL_SOCKET structure.
460
461 @retval EFI_SUCCESS The connection was successfully established.
462 @retval EFI_NOT_READY The connection is in progress, call this routine again.
463 @retval Others The connection attempt failed.
464
465 **/
466 EFI_STATUS
467 EslTcp6ConnectStart (
468 IN ESL_SOCKET * pSocket
469 )
470 {
471 ESL_PORT * pPort;
472 ESL_TCP6_CONTEXT * pTcp6;
473 EFI_TCP6_PROTOCOL * pTcp6Protocol;
474 EFI_STATUS Status;
475
476 DBG_ENTER ( );
477
478 //
479 // Determine if any more local adapters are available
480 //
481 pPort = pSocket->pPortList;
482 if ( NULL != pPort ) {
483 //
484 // Configure the port
485 //
486 pTcp6 = &pPort->Context.Tcp6;
487 pTcp6->ConfigData.AccessPoint.ActiveFlag = TRUE;
488 pTcp6->ConfigData.TrafficClass = 0;
489 pTcp6->ConfigData.HopLimit = 255;
490 pTcp6Protocol = pPort->pProtocol.TCPv6;
491 Status = pTcp6Protocol->Configure ( pTcp6Protocol,
492 &pTcp6->ConfigData );
493 if ( EFI_ERROR ( Status )) {
494 DEBUG (( DEBUG_CONNECT,
495 "ERROR - Failed to configure the Tcp6 port, Status: %r\r\n",
496 Status ));
497 switch ( Status ) {
498 case EFI_ACCESS_DENIED:
499 pSocket->errno = EACCES;
500 break;
501
502 default:
503 case EFI_DEVICE_ERROR:
504 pSocket->errno = EIO;
505 break;
506
507 case EFI_INVALID_PARAMETER:
508 pSocket->errno = EADDRNOTAVAIL;
509 break;
510
511 case EFI_NO_MAPPING:
512 pSocket->errno = EAFNOSUPPORT;
513 break;
514
515 case EFI_OUT_OF_RESOURCES:
516 pSocket->errno = ENOBUFS;
517 break;
518
519 case EFI_UNSUPPORTED:
520 pSocket->errno = EOPNOTSUPP;
521 break;
522 }
523 }
524 else {
525 DEBUG (( DEBUG_CONNECT,
526 "0x%08x: Port configured\r\n",
527 pPort ));
528 pPort->bConfigured = TRUE;
529
530 //
531 // Attempt the connection to the remote system
532 //
533 Status = pTcp6Protocol->Connect ( pTcp6Protocol,
534 &pTcp6->ConnectToken );
535 if ( !EFI_ERROR ( Status )) {
536 //
537 // Connection in progress
538 //
539 pSocket->errno = EINPROGRESS;
540 Status = EFI_NOT_READY;
541 DEBUG (( DEBUG_CONNECT,
542 "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",
543 pPort,
544 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
545 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[1],
546 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[2],
547 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[3],
548 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[4],
549 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[5],
550 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[6],
551 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[7],
552 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[8],
553 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[9],
554 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[10],
555 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[11],
556 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[12],
557 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[13],
558 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[14],
559 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[15],
560 pTcp6->ConfigData.AccessPoint.RemotePort ));
561 }
562 else {
563 //
564 // Connection error
565 //
566 DEBUG (( DEBUG_CONNECT,
567 "ERROR - Port 0x%08x not connected, Status: %r\r\n",
568 pPort,
569 Status ));
570 //
571 // Determine the errno value
572 //
573 switch ( Status ) {
574 default:
575 pSocket->errno = EIO;
576 break;
577
578 case EFI_OUT_OF_RESOURCES:
579 pSocket->errno = ENOBUFS;
580 break;
581
582 case EFI_TIMEOUT:
583 pSocket->errno = ETIMEDOUT;
584 break;
585
586 case EFI_NETWORK_UNREACHABLE:
587 pSocket->errno = ENETDOWN;
588 break;
589
590 case EFI_HOST_UNREACHABLE:
591 pSocket->errno = EHOSTUNREACH;
592 break;
593
594 case EFI_PORT_UNREACHABLE:
595 case EFI_PROTOCOL_UNREACHABLE:
596 case EFI_CONNECTION_REFUSED:
597 pSocket->errno = ECONNREFUSED;
598 break;
599
600 case EFI_CONNECTION_RESET:
601 pSocket->errno = ECONNRESET;
602 break;
603 }
604 }
605 }
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
825 //
826 // All done
827 //
828 DEBUG (( DEBUG_LISTEN,
829 "0x%08x: pSocket - Listen pending on socket\r\n",
830 pSocket ));
831 break;
832 }
833
834 //
835 // Return the operation status
836 //
837 DBG_EXIT_STATUS ( Status );
838 return Status;
839 }
840
841
842 /**
843 Process the connection attempt
844
845 A system has initiated a connection attempt with a socket in the
846 listen state. Attempt to complete the connection.
847
848 The TCPv6 layer calls this routine when a connection is made to
849 the socket in the listen state. See the
850 \ref ConnectionManagement section.
851
852 @param [in] Event The listen completion event
853
854 @param [in] pPort Address of an ::ESL_PORT structure.
855
856 **/
857 VOID
858 EslTcp6ListenComplete (
859 IN EFI_EVENT Event,
860 IN ESL_PORT * pPort
861 )
862 {
863 EFI_HANDLE ChildHandle;
864 struct sockaddr_in6 LocalAddress;
865 EFI_TCP6_CONFIG_DATA * pConfigData;
866 ESL_LAYER * pLayer;
867 ESL_PORT * pNewPort;
868 ESL_SOCKET * pNewSocket;
869 ESL_SOCKET * pSocket;
870 ESL_TCP6_CONTEXT * pTcp6;
871 EFI_TCP6_PROTOCOL * pTcp6Protocol;
872 EFI_STATUS Status;
873 EFI_HANDLE TcpPortHandle;
874 EFI_STATUS TempStatus;
875
876 DBG_ENTER ( );
877 VERIFY_AT_TPL ( TPL_SOCKETS );
878
879 //
880 // Assume success
881 //
882 Status = EFI_SUCCESS;
883
884 //
885 // Determine if this connection fits into the connection FIFO
886 //
887 pSocket = pPort->pSocket;
888 TcpPortHandle = pPort->Context.Tcp6.ListenToken.NewChildHandle;
889 if (( SOCKET_STATE_LISTENING == pSocket->State )
890 && ( pSocket->MaxFifoDepth > pSocket->FifoDepth )) {
891 //
892 // Allocate a socket for this connection
893 //
894 ChildHandle = NULL;
895 pLayer = &mEslLayer;
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
1214 //
1215 // Display the local address
1216 //
1217 DEBUG (( DEBUG_BIND,
1218 "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",
1219 pPort,
1220 pAccessPoint->StationAddress.Addr[0],
1221 pAccessPoint->StationAddress.Addr[1],
1222 pAccessPoint->StationAddress.Addr[2],
1223 pAccessPoint->StationAddress.Addr[3],
1224 pAccessPoint->StationAddress.Addr[4],
1225 pAccessPoint->StationAddress.Addr[5],
1226 pAccessPoint->StationAddress.Addr[6],
1227 pAccessPoint->StationAddress.Addr[7],
1228 pAccessPoint->StationAddress.Addr[8],
1229 pAccessPoint->StationAddress.Addr[9],
1230 pAccessPoint->StationAddress.Addr[10],
1231 pAccessPoint->StationAddress.Addr[11],
1232 pAccessPoint->StationAddress.Addr[12],
1233 pAccessPoint->StationAddress.Addr[13],
1234 pAccessPoint->StationAddress.Addr[14],
1235 pAccessPoint->StationAddress.Addr[15],
1236 pAccessPoint->StationPort ));
1237 }
1238 }
1239
1240 //
1241 // Return the operation status
1242 //
1243 DBG_EXIT_STATUS ( Status );
1244 return Status;
1245 }
1246
1247
1248 /**
1249 Free a receive packet
1250
1251 This routine performs the network specific operations necessary
1252 to free a receive packet.
1253
1254 This routine is called by ::EslSocketPortCloseTxDone to free a
1255 receive packet.
1256
1257 @param [in] pPacket Address of an ::ESL_PACKET structure.
1258 @param [in, out] pRxBytes Address of the count of RX bytes
1259
1260 **/
1261 VOID
1262 EslTcp6PacketFree (
1263 IN ESL_PACKET * pPacket,
1264 IN OUT size_t * pRxBytes
1265 )
1266 {
1267 DBG_ENTER ( );
1268
1269 //
1270 // Account for the receive bytes
1271 //
1272 *pRxBytes -= pPacket->Op.Tcp6Rx.RxData.DataLength;
1273 DBG_EXIT ( );
1274 }
1275
1276
1277 /**
1278 Initialize the network specific portions of an ::ESL_PORT structure.
1279
1280 This routine initializes the network specific portions of an
1281 ::ESL_PORT structure for use by the socket.
1282
1283 This support routine is called by ::EslSocketPortAllocate
1284 to connect the socket with the underlying network adapter
1285 running the TCPv6 protocol.
1286
1287 @param [in] pPort Address of an ESL_PORT structure
1288 @param [in] DebugFlags Flags for debug messages
1289
1290 @retval EFI_SUCCESS - Socket successfully created
1291
1292 **/
1293 EFI_STATUS
1294 EslTcp6PortAllocate (
1295 IN ESL_PORT * pPort,
1296 IN UINTN DebugFlags
1297 )
1298 {
1299 EFI_TCP6_ACCESS_POINT * pAccessPoint;
1300 ESL_SOCKET * pSocket;
1301 ESL_TCP6_CONTEXT * pTcp6;
1302 EFI_STATUS Status;
1303
1304 DBG_ENTER ( );
1305
1306 //
1307 // Use for/break instead of goto
1308 for ( ; ; ) {
1309 //
1310 // Allocate the close event
1311 //
1312 pSocket = pPort->pSocket;
1313 pTcp6 = &pPort->Context.Tcp6;
1314 Status = gBS->CreateEvent ( EVT_NOTIFY_SIGNAL,
1315 TPL_SOCKETS,
1316 (EFI_EVENT_NOTIFY)EslSocketPortCloseComplete,
1317 pPort,
1318 &pTcp6->CloseToken.CompletionToken.Event);
1319 if ( EFI_ERROR ( Status )) {
1320 DEBUG (( DEBUG_ERROR | DebugFlags,
1321 "ERROR - Failed to create the close event, Status: %r\r\n",
1322 Status ));
1323 pSocket->errno = ENOMEM;
1324 break;
1325 }
1326 DEBUG (( DEBUG_CLOSE | DEBUG_POOL,
1327 "0x%08x: Created close event\r\n",
1328 pTcp6->CloseToken.CompletionToken.Event ));
1329
1330 //
1331 // Allocate the connection event
1332 //
1333 Status = gBS->CreateEvent ( EVT_NOTIFY_SIGNAL,
1334 TPL_SOCKETS,
1335 (EFI_EVENT_NOTIFY)EslTcp6ConnectComplete,
1336 pPort,
1337 &pTcp6->ConnectToken.CompletionToken.Event);
1338 if ( EFI_ERROR ( Status )) {
1339 DEBUG (( DEBUG_ERROR | DebugFlags,
1340 "ERROR - Failed to create the connect event, Status: %r\r\n",
1341 Status ));
1342 pSocket->errno = ENOMEM;
1343 break;
1344 }
1345 DEBUG (( DEBUG_CLOSE | DEBUG_POOL,
1346 "0x%08x: Created connect event\r\n",
1347 pTcp6->ConnectToken.CompletionToken.Event ));
1348
1349 //
1350 // Initialize the port
1351 //
1352 pSocket->TxPacketOffset = OFFSET_OF ( ESL_PACKET, Op.Tcp6Tx.TxData );
1353 pSocket->TxTokenEventOffset = OFFSET_OF ( ESL_IO_MGMT, Token.Tcp6Tx.CompletionToken.Event );
1354 pSocket->TxTokenOffset = OFFSET_OF ( EFI_TCP6_IO_TOKEN, Packet.TxData );
1355
1356 //
1357 // Save the cancel, receive and transmit addresses
1358 // pPort->pfnRxCancel = NULL; since the UEFI implementation returns EFI_UNSUPPORTED
1359 //
1360 pPort->pfnConfigure = (PFN_NET_CONFIGURE)pPort->pProtocol.TCPv6->Configure;
1361 pPort->pfnRxPoll = (PFN_NET_POLL)pPort->pProtocol.TCPv6->Poll;
1362 pPort->pfnRxStart = (PFN_NET_IO_START)pPort->pProtocol.TCPv6->Receive;
1363 pPort->pfnTxStart = (PFN_NET_IO_START)pPort->pProtocol.TCPv6->Transmit;
1364
1365 //
1366 // Set the configuration flags
1367 //
1368 pAccessPoint = &pPort->Context.Tcp6.ConfigData.AccessPoint;
1369 pAccessPoint->ActiveFlag = FALSE;
1370 pTcp6->ConfigData.TrafficClass = 0;
1371 pTcp6->ConfigData.HopLimit = 255;
1372 break;
1373 }
1374
1375 //
1376 // Return the operation status
1377 //
1378 DBG_EXIT_STATUS ( Status );
1379 return Status;
1380 }
1381
1382
1383 /**
1384 Close a Tcp6 port.
1385
1386 This routine releases the network specific resources allocated by
1387 ::EslTcp6PortAllocate.
1388
1389 This routine is called by ::EslSocketPortClose.
1390 See the \ref PortCloseStateMachine section.
1391
1392 @param [in] pPort Address of an ::ESL_PORT structure.
1393
1394 @retval EFI_SUCCESS The port is closed
1395 @retval other Port close error
1396
1397 **/
1398 EFI_STATUS
1399 EslTcp6PortClose (
1400 IN ESL_PORT * pPort
1401 )
1402 {
1403 UINTN DebugFlags;
1404 ESL_TCP6_CONTEXT * pTcp6;
1405 EFI_STATUS Status;
1406
1407 DBG_ENTER ( );
1408
1409 //
1410 // Locate the port in the socket list
1411 //
1412 Status = EFI_SUCCESS;
1413 DebugFlags = pPort->DebugFlags;
1414 pTcp6 = &pPort->Context.Tcp6;
1415
1416 //
1417 // Done with the connect event
1418 //
1419 if ( NULL != pTcp6->ConnectToken.CompletionToken.Event ) {
1420 Status = gBS->CloseEvent ( pTcp6->ConnectToken.CompletionToken.Event );
1421 if ( !EFI_ERROR ( Status )) {
1422 DEBUG (( DebugFlags | DEBUG_POOL,
1423 "0x%08x: Closed connect event\r\n",
1424 pTcp6->ConnectToken.CompletionToken.Event ));
1425 }
1426 else {
1427 DEBUG (( DEBUG_ERROR | DebugFlags,
1428 "ERROR - Failed to close the connect event, Status: %r\r\n",
1429 Status ));
1430 ASSERT ( EFI_SUCCESS == Status );
1431 }
1432 }
1433
1434 //
1435 // Done with the close event
1436 //
1437 if ( NULL != pTcp6->CloseToken.CompletionToken.Event ) {
1438 Status = gBS->CloseEvent ( pTcp6->CloseToken.CompletionToken.Event );
1439 if ( !EFI_ERROR ( Status )) {
1440 DEBUG (( DebugFlags | DEBUG_POOL,
1441 "0x%08x: Closed close event\r\n",
1442 pTcp6->CloseToken.CompletionToken.Event ));
1443 }
1444 else {
1445 DEBUG (( DEBUG_ERROR | DebugFlags,
1446 "ERROR - Failed to close the close event, Status: %r\r\n",
1447 Status ));
1448 ASSERT ( EFI_SUCCESS == Status );
1449 }
1450 }
1451
1452 //
1453 // Done with the listen completion event
1454 //
1455 if ( NULL != pTcp6->ListenToken.CompletionToken.Event ) {
1456 Status = gBS->CloseEvent ( pTcp6->ListenToken.CompletionToken.Event );
1457 if ( !EFI_ERROR ( Status )) {
1458 DEBUG (( DebugFlags | DEBUG_POOL,
1459 "0x%08x: Closed listen completion event\r\n",
1460 pTcp6->ListenToken.CompletionToken.Event ));
1461 }
1462 else {
1463 DEBUG (( DEBUG_ERROR | DebugFlags,
1464 "ERROR - Failed to close the listen completion event, Status: %r\r\n",
1465 Status ));
1466 ASSERT ( EFI_SUCCESS == Status );
1467 }
1468 }
1469
1470 //
1471 // Return the operation status
1472 //
1473 DBG_EXIT_STATUS ( Status );
1474 return Status;
1475 }
1476
1477
1478 /**
1479 Perform the network specific close operation on the port.
1480
1481 This routine performs a cancel operations on the TCPv6 port to
1482 shutdown the receive operations on the port.
1483
1484 This routine is called by the ::EslSocketPortCloseTxDone
1485 routine after the port completes all of the transmission.
1486
1487 @param [in] pPort Address of an ::ESL_PORT structure.
1488
1489 @retval EFI_SUCCESS The port is closed, not normally returned
1490 @retval EFI_NOT_READY The port is still closing
1491 @retval EFI_ALREADY_STARTED Error, the port is in the wrong state,
1492 most likely the routine was called already.
1493
1494 **/
1495 EFI_STATUS
1496 EslTcp6PortCloseOp (
1497 IN ESL_PORT * pPort
1498 )
1499 {
1500 ESL_TCP6_CONTEXT * pTcp6;
1501 EFI_TCP6_PROTOCOL * pTcp6Protocol;
1502 EFI_STATUS Status;
1503
1504 DBG_ENTER ( );
1505
1506 //
1507 // Close the configured port
1508 //
1509 Status = EFI_SUCCESS;
1510 pTcp6 = &pPort->Context.Tcp6;
1511 pTcp6Protocol = pPort->pProtocol.TCPv6;
1512 pTcp6->CloseToken.AbortOnClose = pPort->bCloseNow;
1513 Status = pTcp6Protocol->Close ( pTcp6Protocol,
1514 &pTcp6->CloseToken );
1515 if ( !EFI_ERROR ( Status )) {
1516 DEBUG (( pPort->DebugFlags | DEBUG_CLOSE | DEBUG_INFO,
1517 "0x%08x: Port close started\r\n",
1518 pPort ));
1519 }
1520 else {
1521 DEBUG (( DEBUG_ERROR | pPort->DebugFlags | DEBUG_CLOSE | DEBUG_INFO,
1522 "ERROR - Close failed on port 0x%08x, Status: %r\r\n",
1523 pPort,
1524 Status ));
1525 }
1526
1527 //
1528 // Return the operation status
1529 //
1530 DBG_EXIT_STATUS ( Status );
1531 return Status;
1532 }
1533
1534
1535 /**
1536 Receive data from a network connection.
1537
1538 This routine attempts to return buffered data to the caller. The
1539 data is removed from the urgent queue if the message flag MSG_OOB
1540 is specified, otherwise data is removed from the normal queue.
1541 See the \ref ReceiveEngine section.
1542
1543 This routine is called by ::EslSocketReceive to handle the network
1544 specific receive operation to support SOCK_STREAM and SOCK_SEQPACKET
1545 sockets.
1546
1547 @param [in] pPort Address of an ::ESL_PORT structure.
1548
1549 @param [in] pPacket Address of an ::ESL_PACKET structure.
1550
1551 @param [in] pbConsumePacket Address of a BOOLEAN indicating if the packet is to be consumed
1552
1553 @param [in] BufferLength Length of the the buffer
1554
1555 @param [in] pBuffer Address of a buffer to receive the data.
1556
1557 @param [in] pDataLength Number of received data bytes in the buffer.
1558
1559 @param [out] pAddress Network address to receive the remote system address
1560
1561 @param [out] pSkipBytes Address to receive the number of bytes skipped
1562
1563 @return Returns the address of the next free byte in the buffer.
1564
1565 **/
1566 UINT8 *
1567 EslTcp6Receive (
1568 IN ESL_PORT * pPort,
1569 IN ESL_PACKET * pPacket,
1570 IN BOOLEAN * pbConsumePacket,
1571 IN size_t BufferLength,
1572 IN UINT8 * pBuffer,
1573 OUT size_t * pDataLength,
1574 OUT struct sockaddr * pAddress,
1575 OUT size_t * pSkipBytes
1576 )
1577 {
1578 size_t DataLength;
1579 struct sockaddr_in6 * pRemoteAddress;
1580 ESL_TCP6_CONTEXT * pTcp6;
1581
1582 DBG_ENTER ( );
1583
1584 //
1585 // Return the remote system address if requested
1586 //
1587 if ( NULL != pAddress ) {
1588 //
1589 // Build the remote address
1590 //
1591 pTcp6 = &pPort->Context.Tcp6;
1592 DEBUG (( DEBUG_RX,
1593 "Getting packet remote address: [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n",
1594 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
1595 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[1],
1596 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[2],
1597 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[3],
1598 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[4],
1599 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[5],
1600 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[6],
1601 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[7],
1602 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[8],
1603 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[9],
1604 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[10],
1605 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[11],
1606 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[12],
1607 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[13],
1608 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[14],
1609 pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[15],
1610 pTcp6->ConfigData.AccessPoint.RemotePort ));
1611 pRemoteAddress = (struct sockaddr_in6 *)pAddress;
1612 CopyMem ( &pRemoteAddress->sin6_addr,
1613 &pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
1614 sizeof ( pRemoteAddress->sin6_addr ));
1615 pRemoteAddress->sin6_port = SwapBytes16 ( pTcp6->ConfigData.AccessPoint.RemotePort );
1616 }
1617
1618 //
1619 // Determine the amount of received data
1620 //
1621 DataLength = pPacket->ValidBytes;
1622 if ( BufferLength < DataLength ) {
1623 DataLength = BufferLength;
1624 }
1625
1626 //
1627 // Move the data into the buffer
1628 //
1629 DEBUG (( DEBUG_RX,
1630 "0x%08x: Port copy packet 0x%08x data into 0x%08x, 0x%08x bytes\r\n",
1631 pPort,
1632 pPacket,
1633 pBuffer,
1634 DataLength ));
1635 CopyMem ( pBuffer, pPacket->pBuffer, DataLength );
1636
1637 //
1638 // Determine if the data is being read
1639 //
1640 if ( *pbConsumePacket ) {
1641 //
1642 // Account for the bytes consumed
1643 //
1644 pPacket->pBuffer += DataLength;
1645 pPacket->ValidBytes -= DataLength;
1646 DEBUG (( DEBUG_RX,
1647 "0x%08x: Port account for 0x%08x bytes\r\n",
1648 pPort,
1649 DataLength ));
1650
1651 //
1652 // Determine if the entire packet was consumed
1653 //
1654 if (( 0 == pPacket->ValidBytes )
1655 || ( SOCK_STREAM != pPort->pSocket->Type )) {
1656 //
1657 // All done with this packet
1658 // Account for any discarded data
1659 //
1660 *pSkipBytes = pPacket->ValidBytes;
1661 }
1662 else
1663 {
1664 //
1665 // More data to consume later
1666 //
1667 *pbConsumePacket = FALSE;
1668 }
1669 }
1670
1671 //
1672 // Return the data length and the buffer address
1673 //
1674 *pDataLength = DataLength;
1675 DBG_EXIT_HEX ( pBuffer );
1676 return pBuffer;
1677 }
1678
1679
1680 /**
1681 Get the remote socket address.
1682
1683 This routine returns the address of the remote connection point
1684 associated with the SOCK_STREAM or SOCK_SEQPACKET socket.
1685
1686 This routine is called by ::EslSocketGetPeerAddress to detemine
1687 the TCPv6 address and por number associated with the network adapter.
1688
1689 @param [in] pPort Address of an ::ESL_PORT structure.
1690
1691 @param [out] pAddress Network address to receive the remote system address
1692
1693 **/
1694 VOID
1695 EslTcp6RemoteAddressGet (
1696 IN ESL_PORT * pPort,
1697 OUT struct sockaddr * pAddress
1698 )
1699 {
1700 struct sockaddr_in6 * pRemoteAddress;
1701 ESL_TCP6_CONTEXT * pTcp6;
1702
1703 DBG_ENTER ( );
1704
1705 //
1706 // Return the remote address
1707 //
1708 pTcp6 = &pPort->Context.Tcp6;
1709 pRemoteAddress = (struct sockaddr_in6 *)pAddress;
1710 pRemoteAddress->sin6_family = AF_INET6;
1711 pRemoteAddress->sin6_port = SwapBytes16 ( pTcp6->ConfigData.AccessPoint.RemotePort );
1712 CopyMem ( &pRemoteAddress->sin6_addr,
1713 &pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr[0],
1714 sizeof ( pRemoteAddress->sin6_addr ));
1715
1716 DBG_EXIT ( );
1717 }
1718
1719
1720 /**
1721 Set the remote address
1722
1723 This routine sets the remote address in the port.
1724
1725 This routine is called by ::EslSocketConnect to specify the
1726 remote network address.
1727
1728 @param [in] pPort Address of an ::ESL_PORT structure.
1729
1730 @param [in] pSockAddr Network address of the remote system.
1731
1732 @param [in] SockAddrLength Length in bytes of the network address.
1733
1734 @retval EFI_SUCCESS The operation was successful
1735
1736 **/
1737 EFI_STATUS
1738 EslTcp6RemoteAddressSet (
1739 IN ESL_PORT * pPort,
1740 IN CONST struct sockaddr * pSockAddr,
1741 IN socklen_t SockAddrLength
1742 )
1743 {
1744 CONST struct sockaddr_in6 * pRemoteAddress;
1745 ESL_TCP6_CONTEXT * pTcp6;
1746 EFI_STATUS Status;
1747
1748 DBG_ENTER ( );
1749
1750 //
1751 // Set the remote address
1752 //
1753 pTcp6 = &pPort->Context.Tcp6;
1754 pRemoteAddress = (struct sockaddr_in6 *)pSockAddr;
1755 CopyMem ( &pTcp6->ConfigData.AccessPoint.RemoteAddress.Addr [ 0 ],
1756 &pRemoteAddress->sin6_addr.__u6_addr.__u6_addr8 [ 0 ],
1757 sizeof ( pRemoteAddress->sin6_addr.__u6_addr.__u6_addr8 ));
1758 pTcp6->ConfigData.AccessPoint.RemotePort = SwapBytes16 ( pRemoteAddress->sin6_port );
1759 Status = EFI_SUCCESS;
1760
1761 //
1762 // TODO: Fix the following check
1763 //
1764 /*
1765 if ( INADDR_BROADCAST == pRemoteAddress->sin6_addr.s_addr ) {
1766 DEBUG (( DEBUG_CONNECT,
1767 "ERROR - Invalid remote address\r\n" ));
1768 Status = EFI_INVALID_PARAMETER;
1769 pPort->pSocket->errno = EAFNOSUPPORT;
1770 }
1771 */
1772
1773 //
1774 // Return the operation status
1775 //
1776 DBG_EXIT_STATUS ( Status );
1777 return Status;
1778 }
1779
1780
1781 /**
1782 Process the receive completion
1783
1784 This routine queues the data in FIFO order in either the urgent
1785 or normal data queues depending upon the type of data received.
1786 See the \ref ReceiveEngine section.
1787
1788 This routine is called by the TCPv6 driver when some data is
1789 received.
1790
1791 Buffer the data that was just received.
1792
1793 @param [in] Event The receive completion event
1794
1795 @param [in] pIo Address of an ::ESL_IO_MGMT structure
1796
1797 **/
1798 VOID
1799 EslTcp6RxComplete (
1800 IN EFI_EVENT Event,
1801 IN ESL_IO_MGMT * pIo
1802 )
1803 {
1804 BOOLEAN bUrgent;
1805 size_t LengthInBytes;
1806 ESL_PACKET * pPacket;
1807 EFI_STATUS Status;
1808
1809 DBG_ENTER ( );
1810
1811 //
1812 // Get the operation status.
1813 //
1814 Status = pIo->Token.Tcp6Rx.CompletionToken.Status;
1815
1816 //
1817 // +--------------------+ +---------------------------+
1818 // | ESL_IO_MGMT | | ESL_PACKET |
1819 // | | | |
1820 // | +---------------+ +-----------------------+ |
1821 // | | Token | | EFI_Tcp6_RECEIVE_DATA | |
1822 // | | RxData --> | | |
1823 // | | | +-----------------------+---+
1824 // | | Event | | Data Buffer |
1825 // +----+---------------+ | |
1826 // | |
1827 // +---------------------------+
1828 //
1829 //
1830 // Duplicate the buffer address and length for use by the
1831 // buffer handling code in EslTcp6Receive. These fields are
1832 // used when a partial read is done of the data from the
1833 // packet.
1834 //
1835 pPacket = pIo->pPacket;
1836 pPacket->pBuffer = pPacket->Op.Tcp6Rx.RxData.FragmentTable[0].FragmentBuffer;
1837 LengthInBytes = pPacket->Op.Tcp6Rx.RxData.DataLength;
1838 pPacket->ValidBytes = LengthInBytes;
1839
1840 //
1841 // Get the data type so that it may be linked to the
1842 // correct receive buffer list on the ESL_SOCKET structure
1843 //
1844 bUrgent = pPacket->Op.Tcp6Rx.RxData.UrgentFlag;
1845
1846 //
1847 // Complete this request
1848 //
1849 EslSocketRxComplete ( pIo, Status, LengthInBytes, bUrgent );
1850 DBG_EXIT ( );
1851 }
1852
1853
1854 /**
1855 Start a receive operation
1856
1857 This routine posts a receive buffer to the TCPv6 driver.
1858 See the \ref ReceiveEngine section.
1859
1860 This support routine is called by EslSocketRxStart.
1861
1862 @param [in] pPort Address of an ::ESL_PORT structure.
1863 @param [in] pIo Address of an ::ESL_IO_MGMT structure.
1864
1865 **/
1866 VOID
1867 EslTcp6RxStart (
1868 IN ESL_PORT * pPort,
1869 IN ESL_IO_MGMT * pIo
1870 )
1871 {
1872 ESL_PACKET * pPacket;
1873
1874 DBG_ENTER ( );
1875
1876 //
1877 // Initialize the buffer for receive
1878 //
1879 pPacket = pIo->pPacket;
1880 pIo->Token.Tcp6Rx.Packet.RxData = &pPacket->Op.Tcp6Rx.RxData;
1881 pPacket->Op.Tcp6Rx.RxData.DataLength = sizeof ( pPacket->Op.Tcp6Rx.Buffer );
1882 pPacket->Op.Tcp6Rx.RxData.FragmentCount = 1;
1883 pPacket->Op.Tcp6Rx.RxData.FragmentTable[0].FragmentLength = pPacket->Op.Tcp6Rx.RxData.DataLength;
1884 pPacket->Op.Tcp6Rx.RxData.FragmentTable[0].FragmentBuffer = &pPacket->Op.Tcp6Rx.Buffer[0];
1885
1886 DBG_EXIT ( );
1887 }
1888
1889
1890 /**
1891 Determine if the socket is configured.
1892
1893 This routine uses the flag ESL_SOCKET::bConfigured to determine
1894 if the network layer's configuration routine has been called.
1895
1896 This routine is called by EslSocketIsConfigured to verify
1897 that the socket has been configured.
1898
1899 @param [in] pSocket Address of an ::ESL_SOCKET structure.
1900
1901 @retval EFI_SUCCESS - The port is connected
1902 @retval EFI_NOT_STARTED - The port is not connected
1903
1904 **/
1905 EFI_STATUS
1906 EslTcp6SocketIsConfigured (
1907 IN ESL_SOCKET * pSocket
1908 )
1909 {
1910 EFI_STATUS Status;
1911
1912 DBG_ENTER ( );
1913
1914 //
1915 // Determine the socket configuration status
1916 //
1917 Status = pSocket->bConfigured ? EFI_SUCCESS : EFI_NOT_STARTED;
1918
1919 //
1920 // Return the port connected state.
1921 //
1922 DBG_EXIT_STATUS ( Status );
1923 return Status;
1924 }
1925
1926
1927 /**
1928 Buffer data for transmission over a network connection.
1929
1930 This routine buffers data for the transmit engine in one of two
1931 queues, one for urgent (out-of-band) data and the other for normal
1932 data. The urgent data is provided to TCP as soon as it is available,
1933 allowing the TCP layer to schedule transmission of the urgent data
1934 between packets of normal data.
1935
1936 This routine is called by ::EslSocketTransmit to buffer
1937 data for transmission. When the \ref TransmitEngine has resources,
1938 this routine will start the transmission of the next buffer on
1939 the network connection.
1940
1941 Transmission errors are returned during the next transmission or
1942 during the close operation. Only buffering errors are returned
1943 during the current transmission attempt.
1944
1945 @param [in] pSocket Address of an ::ESL_SOCKET structure
1946
1947 @param [in] Flags Message control flags
1948
1949 @param [in] BufferLength Length of the the buffer
1950
1951 @param [in] pBuffer Address of a buffer to receive the data.
1952
1953 @param [in] pDataLength Number of received data bytes in the buffer.
1954
1955 @param [in] pAddress Network address of the remote system address
1956
1957 @param [in] AddressLength Length of the remote network address structure
1958
1959 @retval EFI_SUCCESS - Socket data successfully buffered
1960
1961 **/
1962 EFI_STATUS
1963 EslTcp6TxBuffer (
1964 IN ESL_SOCKET * pSocket,
1965 IN int Flags,
1966 IN size_t BufferLength,
1967 IN CONST UINT8 * pBuffer,
1968 OUT size_t * pDataLength,
1969 IN const struct sockaddr * pAddress,
1970 IN socklen_t AddressLength
1971 )
1972 {
1973 BOOLEAN bUrgent;
1974 BOOLEAN bUrgentQueue;
1975 ESL_PACKET * pPacket;
1976 ESL_IO_MGMT ** ppActive;
1977 ESL_IO_MGMT ** ppFree;
1978 ESL_PORT * pPort;
1979 ESL_PACKET ** ppQueueHead;
1980 ESL_PACKET ** ppQueueTail;
1981 ESL_PACKET * pPreviousPacket;
1982 ESL_TCP6_CONTEXT * pTcp6;
1983 size_t * pTxBytes;
1984 EFI_TCP6_TRANSMIT_DATA * pTxData;
1985 EFI_STATUS Status;
1986 EFI_TPL TplPrevious;
1987
1988 DBG_ENTER ( );
1989
1990 //
1991 // Assume failure
1992 //
1993 Status = EFI_UNSUPPORTED;
1994 pSocket->errno = ENOTCONN;
1995 *pDataLength = 0;
1996
1997 //
1998 // Verify that the socket is connected
1999 //
2000 if ( SOCKET_STATE_CONNECTED == pSocket->State ) {
2001 //
2002 // Locate the port
2003 //
2004 pPort = pSocket->pPortList;
2005 if ( NULL != pPort ) {
2006 //
2007 // Determine the queue head
2008 //
2009 pTcp6 = &pPort->Context.Tcp6;
2010 bUrgent = (BOOLEAN)( 0 != ( Flags & MSG_OOB ));
2011 bUrgentQueue = bUrgent
2012 && ( !pSocket->bOobInLine )
2013 && pSocket->pApi->bOobSupported;
2014 if ( bUrgentQueue ) {
2015 ppQueueHead = &pSocket->pTxOobPacketListHead;
2016 ppQueueTail = &pSocket->pTxOobPacketListTail;
2017 ppActive = &pPort->pTxOobActive;
2018 ppFree = &pPort->pTxOobFree;
2019 pTxBytes = &pSocket->TxOobBytes;
2020 }
2021 else {
2022 ppQueueHead = &pSocket->pTxPacketListHead;
2023 ppQueueTail = &pSocket->pTxPacketListTail;
2024 ppActive = &pPort->pTxActive;
2025 ppFree = &pPort->pTxFree;
2026 pTxBytes = &pSocket->TxBytes;
2027 }
2028
2029 //
2030 // Verify that there is enough room to buffer another
2031 // transmit operation
2032 //
2033 if ( pSocket->MaxTxBuf > *pTxBytes ) {
2034 if ( pPort->bTxFlowControl ) {
2035 DEBUG (( DEBUG_TX,
2036 "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\r\n0x%08x: pPort, TX flow control released, Max bytes: %d > %d bufferred bytes\r\n",
2037 pPort,
2038 pSocket->MaxTxBuf,
2039 *pTxBytes ));
2040 pPort->bTxFlowControl = FALSE;
2041 }
2042
2043 //
2044 // Attempt to allocate the packet
2045 //
2046 Status = EslSocketPacketAllocate ( &pPacket,
2047 sizeof ( pPacket->Op.Tcp6Tx )
2048 - sizeof ( pPacket->Op.Tcp6Tx.Buffer )
2049 + BufferLength,
2050 0,
2051 DEBUG_TX );
2052 if ( !EFI_ERROR ( Status )) {
2053 //
2054 // Initialize the transmit operation
2055 //
2056 pTxData = &pPacket->Op.Tcp6Tx.TxData;
2057 pTxData->Push = TRUE || bUrgent;
2058 pTxData->Urgent = bUrgent;
2059 pTxData->DataLength = (UINT32) BufferLength;
2060 pTxData->FragmentCount = 1;
2061 pTxData->FragmentTable[0].FragmentLength = (UINT32) BufferLength;
2062 pTxData->FragmentTable[0].FragmentBuffer = &pPacket->Op.Tcp6Tx.Buffer[0];
2063
2064 //
2065 // Copy the data into the buffer
2066 //
2067 CopyMem ( &pPacket->Op.Tcp6Tx.Buffer[0],
2068 pBuffer,
2069 BufferLength );
2070
2071 //
2072 // Synchronize with the socket layer
2073 //
2074 RAISE_TPL ( TplPrevious, TPL_SOCKETS );
2075
2076 //
2077 // Stop transmission after an error
2078 //
2079 if ( !EFI_ERROR ( pSocket->TxError )) {
2080 //
2081 // Display the request
2082 //
2083 DEBUG (( DEBUG_TX,
2084 "Send %d %s bytes from 0x%08x\r\n",
2085 BufferLength,
2086 bUrgent ? L"urgent" : L"normal",
2087 pBuffer ));
2088
2089 //
2090 // Queue the data for transmission
2091 //
2092 pPacket->pNext = NULL;
2093 pPreviousPacket = *ppQueueTail;
2094 if ( NULL == pPreviousPacket ) {
2095 *ppQueueHead = pPacket;
2096 }
2097 else {
2098 pPreviousPacket->pNext = pPacket;
2099 }
2100 *ppQueueTail = pPacket;
2101 DEBUG (( DEBUG_TX,
2102 "0x%08x: Packet on %s transmit list\r\n",
2103 pPacket,
2104 bUrgentQueue ? L"urgent" : L"normal" ));
2105
2106 //
2107 // Account for the buffered data
2108 //
2109 *pTxBytes += BufferLength;
2110 *pDataLength = BufferLength;
2111
2112 //
2113 // Start the transmit engine if it is idle
2114 //
2115 if ( NULL != *ppFree ) {
2116 EslSocketTxStart ( pPort,
2117 ppQueueHead,
2118 ppQueueTail,
2119 ppActive,
2120 ppFree );
2121 }
2122 }
2123 else {
2124 //
2125 // Previous transmit error
2126 // Stop transmission
2127 //
2128 Status = pSocket->TxError;
2129 pSocket->errno = EIO;
2130
2131 //
2132 // Free the packet
2133 //
2134 EslSocketPacketFree ( pPacket, DEBUG_TX );
2135 }
2136
2137 //
2138 // Release the socket layer synchronization
2139 //
2140 RESTORE_TPL ( TplPrevious );
2141 }
2142 else {
2143 //
2144 // Packet allocation failed
2145 //
2146 pSocket->errno = ENOMEM;
2147 }
2148 }
2149 else {
2150 if ( !pPort->bTxFlowControl ) {
2151 DEBUG (( DEBUG_TX,
2152 "0x%08x: pPort, TX flow control applied, Max bytes %d <= %d bufferred bytes\r\nTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\r\n",
2153 pPort,
2154 pSocket->MaxTxBuf,
2155 *pTxBytes ));
2156 pPort->bTxFlowControl = TRUE;
2157 }
2158 //
2159 // Not enough buffer space available
2160 //
2161 pSocket->errno = EAGAIN;
2162 Status = EFI_NOT_READY;
2163 }
2164 }
2165 }
2166
2167 //
2168 // Return the operation status
2169 //
2170 DBG_EXIT_STATUS ( Status );
2171 return Status;
2172 }
2173
2174
2175 /**
2176 Process the normal data transmit completion
2177
2178 This routine use ::EslSocketTxComplete to perform the transmit
2179 completion processing for normal data.
2180
2181 This routine is called by the TCPv6 network layer when a
2182 normal data transmit request completes.
2183
2184 @param [in] Event The normal transmit completion event
2185
2186 @param [in] pIo The ESL_IO_MGMT structure address
2187
2188 **/
2189 VOID
2190 EslTcp6TxComplete (
2191 IN EFI_EVENT Event,
2192 IN ESL_IO_MGMT * pIo
2193 )
2194 {
2195 UINT32 LengthInBytes;
2196 ESL_PACKET * pPacket;
2197 ESL_PORT * pPort;
2198 ESL_SOCKET * pSocket;
2199 EFI_STATUS Status;
2200
2201 DBG_ENTER ( );
2202
2203 //
2204 // Locate the active transmit packet
2205 //
2206 pPacket = pIo->pPacket;
2207 pPort = pIo->pPort;
2208 pSocket = pPort->pSocket;
2209
2210 //
2211 // Get the transmit length and status
2212 //
2213 LengthInBytes = pPacket->Op.Tcp6Tx.TxData.DataLength;
2214 pSocket->TxBytes -= LengthInBytes;
2215 Status = pIo->Token.Tcp6Tx.CompletionToken.Status;
2216
2217 //
2218 // Complete the transmit operation
2219 //
2220 EslSocketTxComplete ( pIo,
2221 LengthInBytes,
2222 Status,
2223 "Normal ",
2224 &pSocket->pTxPacketListHead,
2225 &pSocket->pTxPacketListTail,
2226 &pPort->pTxActive,
2227 &pPort->pTxFree );
2228 DBG_EXIT ( );
2229 }
2230
2231
2232 /**
2233 Process the urgent data transmit completion
2234
2235 This routine use ::EslSocketTxComplete to perform the transmit
2236 completion processing for urgent data.
2237
2238 This routine is called by the TCPv6 network layer when a
2239 urgent data transmit request completes.
2240
2241 @param [in] Event The urgent transmit completion event
2242
2243 @param [in] pIo The ESL_IO_MGMT structure address
2244
2245 **/
2246 VOID
2247 EslTcp6TxOobComplete (
2248 IN EFI_EVENT Event,
2249 IN ESL_IO_MGMT * pIo
2250 )
2251 {
2252 UINT32 LengthInBytes;
2253 ESL_PACKET * pPacket;
2254 ESL_PORT * pPort;
2255 ESL_SOCKET * pSocket;
2256 EFI_STATUS Status;
2257
2258 DBG_ENTER ( );
2259
2260 //
2261 // Locate the active transmit packet
2262 //
2263 pPacket = pIo->pPacket;
2264 pPort = pIo->pPort;
2265 pSocket = pPort->pSocket;
2266
2267 //
2268 // Get the transmit length and status
2269 //
2270 LengthInBytes = pPacket->Op.Tcp6Tx.TxData.DataLength;
2271 pSocket->TxOobBytes -= LengthInBytes;
2272 Status = pIo->Token.Tcp6Tx.CompletionToken.Status;
2273
2274 //
2275 // Complete the transmit operation
2276 //
2277 EslSocketTxComplete ( pIo,
2278 LengthInBytes,
2279 Status,
2280 "Urgent ",
2281 &pSocket->pTxOobPacketListHead,
2282 &pSocket->pTxOobPacketListTail,
2283 &pPort->pTxOobActive,
2284 &pPort->pTxOobFree );
2285 DBG_EXIT ( );
2286 }
2287
2288
2289 /**
2290 Interface between the socket layer and the network specific
2291 code that supports SOCK_STREAM and SOCK_SEQPACKET sockets
2292 over TCPv6.
2293 **/
2294 CONST ESL_PROTOCOL_API cEslTcp6Api = {
2295 "TCPv6",
2296 IPPROTO_TCP,
2297 OFFSET_OF ( ESL_PORT, Context.Tcp6.ConfigData ),
2298 OFFSET_OF ( ESL_LAYER, pTcp6List ),
2299 sizeof ( struct sockaddr_in6 ),
2300 sizeof ( struct sockaddr_in6 ),
2301 AF_INET6,
2302 sizeof (((ESL_PACKET *)0 )->Op.Tcp6Rx ),
2303 OFFSET_OF ( ESL_PACKET, Op.Tcp6Rx.Buffer ) - OFFSET_OF ( ESL_PACKET, Op ),
2304 OFFSET_OF ( ESL_IO_MGMT, Token.Tcp6Rx.Packet.RxData ),
2305 TRUE,
2306 EADDRINUSE,
2307 EslTcp6Accept,
2308 EslTcp6ConnectPoll,
2309 EslTcp6ConnectStart,
2310 EslTcp6SocketIsConfigured,
2311 EslTcp6LocalAddressGet,
2312 EslTcp6LocalAddressSet,
2313 EslTcp6Listen,
2314 NULL, // OptionGet
2315 NULL, // OptionSet
2316 EslTcp6PacketFree,
2317 EslTcp6PortAllocate,
2318 EslTcp6PortClose,
2319 EslTcp6PortCloseOp,
2320 FALSE,
2321 EslTcp6Receive,
2322 EslTcp6RemoteAddressGet,
2323 EslTcp6RemoteAddressSet,
2324 EslTcp6RxComplete,
2325 EslTcp6RxStart,
2326 EslTcp6TxBuffer,
2327 EslTcp6TxComplete,
2328 EslTcp6TxOobComplete
2329 };