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