]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Tcp4Dxe/Tcp4Driver.c
Allocated one buffer to for option->description, since it may be released by BdsLibBo...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Tcp4Dxe / Tcp4Driver.c
CommitLineData
8a67d61d 1/** @file\r
dab714aa 2 Tcp driver function.\r
8a67d61d 3\r
dfc1f033 4Copyright (c) 2005 - 2007, Intel Corporation<BR>\r
8a67d61d 5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
dfc1f033 8http://opensource.org/licenses/bsd-license.php<BR>\r
8a67d61d 9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
8a67d61d 13**/\r
14\r
15#include "Tcp4Main.h"\r
16\r
17\r
120db52c 18UINT16 mTcp4RandomPort;\r
83cbd279 19extern EFI_COMPONENT_NAME_PROTOCOL gTcp4ComponentName;\r
20extern EFI_COMPONENT_NAME2_PROTOCOL gTcp4ComponentName2;\r
8a67d61d 21\r
22TCP4_HEARTBEAT_TIMER mTcp4Timer = {\r
23 NULL,\r
24 0\r
25};\r
26\r
27EFI_TCP4_PROTOCOL mTcp4ProtocolTemplate = {\r
28 Tcp4GetModeData,\r
29 Tcp4Configure,\r
30 Tcp4Routes,\r
31 Tcp4Connect,\r
32 Tcp4Accept,\r
33 Tcp4Transmit,\r
34 Tcp4Receive,\r
35 Tcp4Close,\r
36 Tcp4Cancel,\r
37 Tcp4Poll\r
38};\r
39\r
40SOCK_INIT_DATA mTcp4DefaultSockData = {\r
41 SOCK_STREAM,\r
4eb65aff 42 (SOCK_STATE) 0,\r
8a67d61d 43 NULL,\r
44 TCP_BACKLOG,\r
45 TCP_SND_BUF_SIZE,\r
46 TCP_RCV_BUF_SIZE,\r
47 &mTcp4ProtocolTemplate,\r
4f6e31e4 48 Tcp4CreateSocketCallback,\r
49 Tcp4DestroySocketCallback,\r
50 NULL,\r
51 NULL,\r
52 0,\r
8a67d61d 53 Tcp4Dispatcher,\r
54 NULL,\r
55};\r
56\r
57EFI_DRIVER_BINDING_PROTOCOL mTcp4DriverBinding = {\r
58 Tcp4DriverBindingSupported,\r
59 Tcp4DriverBindingStart,\r
60 Tcp4DriverBindingStop,\r
61 0xa,\r
62 NULL,\r
63 NULL\r
64};\r
65\r
66EFI_SERVICE_BINDING_PROTOCOL mTcp4ServiceBinding = {\r
67 Tcp4ServiceBindingCreateChild,\r
68 Tcp4ServiceBindingDestroyChild\r
69};\r
70\r
71\r
72/**\r
73 Create and start the heartbeat timer for TCP driver.\r
74\r
8a67d61d 75 @retval EFI_SUCCESS The timer is successfully created and started.\r
76 @retval other The timer is not created.\r
77\r
78**/\r
8a67d61d 79EFI_STATUS\r
80Tcp4CreateTimer (\r
81 VOID\r
82 )\r
83{\r
84 EFI_STATUS Status;\r
85\r
86 Status = EFI_SUCCESS;\r
87\r
88 if (mTcp4Timer.RefCnt == 0) {\r
89\r
90 Status = gBS->CreateEvent (\r
91 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
e48e37fc 92 TPL_NOTIFY,\r
8a67d61d 93 TcpTicking,\r
94 NULL,\r
95 &mTcp4Timer.TimerEvent\r
96 );\r
97 if (!EFI_ERROR (Status)) {\r
98\r
99 Status = gBS->SetTimer (\r
100 mTcp4Timer.TimerEvent,\r
101 TimerPeriodic,\r
102 (UINT64) (TICKS_PER_SECOND / TCP_TICK_HZ)\r
103 );\r
104 }\r
105 }\r
106\r
107 if (!EFI_ERROR (Status)) {\r
108\r
109 mTcp4Timer.RefCnt++;\r
110 }\r
111\r
112 return Status;\r
113}\r
114\r
115\r
116/**\r
117 Stop and destroy the heartbeat timer for TCP driver.\r
85511ddf 118 \r
8a67d61d 119**/\r
8a67d61d 120VOID\r
120db52c 121Tcp4DestroyTimer (\r
122 VOID\r
123 )\r
8a67d61d 124{\r
125 ASSERT (mTcp4Timer.RefCnt > 0);\r
126\r
127 mTcp4Timer.RefCnt--;\r
128\r
129 if (mTcp4Timer.RefCnt > 0) {\r
130 return;\r
131 }\r
132\r
133 gBS->SetTimer (mTcp4Timer.TimerEvent, TimerCancel, 0);\r
134 gBS->CloseEvent (mTcp4Timer.TimerEvent);\r
135 mTcp4Timer.TimerEvent = NULL;\r
136}\r
137\r
85511ddf 138/**\r
120db52c 139 The entry point for Tcp4 driver, used to install Tcp4 driver on the ImageHandle.\r
85511ddf 140\r
141 @param ImageHandle The firmware allocated handle for this\r
142 driver image.\r
143 @param SystemTable Pointer to the EFI system table.\r
144\r
145 @retval EFI_SUCCESS Driver loaded.\r
146 @retval other Driver not loaded.\r
8a67d61d 147\r
85511ddf 148**/\r
8a67d61d 149EFI_STATUS\r
150EFIAPI\r
151Tcp4DriverEntryPoint (\r
152 IN EFI_HANDLE ImageHandle,\r
153 IN EFI_SYSTEM_TABLE *SystemTable\r
154 )\r
8a67d61d 155{\r
156 EFI_STATUS Status;\r
157 UINT32 Seed;\r
158\r
159 //\r
160 // Install the TCP4 Driver Binding Protocol\r
161 //\r
83cbd279 162 Status = EfiLibInstallDriverBindingComponentName2 (\r
8a67d61d 163 ImageHandle,\r
164 SystemTable,\r
165 &mTcp4DriverBinding,\r
166 ImageHandle,\r
167 &gTcp4ComponentName,\r
83cbd279 168 &gTcp4ComponentName2\r
8a67d61d 169 );\r
da1d0201 170 ASSERT_EFI_ERROR (Status);\r
8a67d61d 171 //\r
172 // Initialize ISS and random port.\r
173 //\r
174 Seed = NetRandomInitSeed ();\r
175 mTcpGlobalIss = NET_RANDOM (Seed) % mTcpGlobalIss;\r
120db52c 176 mTcp4RandomPort = (UINT16) (TCP4_PORT_KNOWN +\r
4eb65aff 177 (UINT16) (NET_RANDOM(Seed) % TCP4_PORT_KNOWN));\r
8a67d61d 178\r
179 return Status;\r
180}\r
181\r
182\r
183/**\r
dfc1f033 184 Tests to see if this driver supports a given controller.\r
185 \r
186 If a child device is provided, it further tests to see if this driver supports \r
187 creating a handle for the specified child device.\r
188\r
189 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
190 @param ControllerHandle The handle of the controller to test. This handle \r
191 must support a protocol interface that supplies \r
192 an I/O abstraction to the driver.\r
193 @param RemainingDevicePath A pointer to the remaining portion of a device path. \r
194 This parameter is ignored by device drivers, and is optional for bus drivers.\r
195\r
196\r
197 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
198 RemainingDevicePath is supported by the driver \r
199 specified by This.\r
200 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
201 RemainingDevicePath is already being managed by \r
202 the driver specified by This.\r
203 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
204 RemainingDevicePath is already being managed by a \r
205 different driver or an application that requires \r
206 exclusive access.\r
207 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
208 RemainingDevicePath is not supported by the driver \r
209 specified by This.\r
210 \r
8a67d61d 211**/\r
212EFI_STATUS\r
213EFIAPI\r
214Tcp4DriverBindingSupported (\r
120db52c 215 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
8a67d61d 216 IN EFI_HANDLE ControllerHandle,\r
120db52c 217 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
8a67d61d 218 )\r
219{\r
220 EFI_STATUS Status;\r
221\r
222 //\r
223 // Test for the Tcp4ServiceBinding Protocol\r
224 //\r
225 Status = gBS->OpenProtocol (\r
226 ControllerHandle,\r
227 &gEfiTcp4ServiceBindingProtocolGuid,\r
228 NULL,\r
229 This->DriverBindingHandle,\r
230 ControllerHandle,\r
231 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
232 );\r
233 if (!EFI_ERROR (Status)) {\r
234 return EFI_ALREADY_STARTED;\r
235 }\r
236\r
237 //\r
238 // Test for the Ip4 Protocol\r
239 //\r
240 Status = gBS->OpenProtocol (\r
241 ControllerHandle,\r
242 &gEfiIp4ServiceBindingProtocolGuid,\r
243 NULL,\r
244 This->DriverBindingHandle,\r
245 ControllerHandle,\r
246 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
247 );\r
248\r
249 return Status;\r
250}\r
251\r
252\r
253/**\r
dfc1f033 254 Start this driver on ControllerHandle. \r
255 \r
256 The Start() function is designed to be invoked from the EFI boot service \r
257 ConnectController(). As a result, much of the error checking on the parameters \r
258 to Start() has been moved into this common boot service. It is legal to call \r
259 Start() from other locations, but the following calling restrictions must be \r
260 followed or the system behavior will not be deterministic.\r
261 1. ControllerHandle must be a valid EFI_HANDLE.\r
262 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally \r
263 aligned EFI_DEVICE_PATH_PROTOCOL.\r
264 3. Prior to calling Start(), the Supported() function for the driver specified \r
265 by This must have been called with the same calling parameters, and Supported() \r
266 must have returned EFI_SUCCESS.\r
267\r
268 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
269 @param ControllerHandle The handle of the controller to start. This handle \r
270 must support a protocol interface that supplies \r
271 an I/O abstraction to the driver.\r
272 @param RemainingDevicePath A pointer to the remaining portion of a device path. \r
273 This parameter is ignored by device drivers, and is \r
274 optional for bus drivers.\r
275\r
276 @retval EFI_SUCCESS The device was started.\r
277 @retval EFI_ALREADY_STARTED The device could not be started due to a device error.\r
278 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack \r
279 of resources.\r
8a67d61d 280\r
281**/\r
282EFI_STATUS\r
283EFIAPI\r
284Tcp4DriverBindingStart (\r
120db52c 285 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
8a67d61d 286 IN EFI_HANDLE ControllerHandle,\r
120db52c 287 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
8a67d61d 288 )\r
289{\r
290 EFI_STATUS Status;\r
291 TCP4_SERVICE_DATA *TcpServiceData;\r
292 IP_IO_OPEN_DATA OpenData;\r
293\r
e48e37fc 294 TcpServiceData = AllocateZeroPool (sizeof (TCP4_SERVICE_DATA));\r
8a67d61d 295\r
296 if (NULL == TcpServiceData) {\r
e48e37fc 297 DEBUG ((EFI_D_ERROR, "Tcp4DriverBindingStart: Have no enough"\r
dfc1f033 298 " resource to create a Tcp Servcie Data\n"));\r
8a67d61d 299\r
300 return EFI_OUT_OF_RESOURCES;\r
301 }\r
302\r
303 //\r
304 // Create a new IP IO to Consume it\r
305 //\r
306 TcpServiceData->IpIo = IpIoCreate (This->DriverBindingHandle, ControllerHandle);\r
307 if (NULL == TcpServiceData->IpIo) {\r
308\r
e48e37fc 309 DEBUG ((EFI_D_ERROR, "Tcp4DriverBindingStart: Have no enough"\r
dfc1f033 310 " resource to create an Ip Io\n"));\r
8a67d61d 311\r
312 Status = EFI_OUT_OF_RESOURCES;\r
c4a62a12 313 goto ON_ERROR;\r
8a67d61d 314 }\r
315\r
316 //\r
317 // Configure and start IpIo.\r
318 //\r
e48e37fc 319 ZeroMem (&OpenData, sizeof (IP_IO_OPEN_DATA));\r
8a67d61d 320\r
687a2e5f 321 CopyMem (&OpenData.IpConfigData, &mIpIoDefaultIpConfigData, sizeof (OpenData.IpConfigData));\r
8a67d61d 322 OpenData.IpConfigData.DefaultProtocol = EFI_IP_PROTO_TCP;\r
323\r
324 OpenData.PktRcvdNotify = Tcp4RxCallback;\r
325 Status = IpIoOpen (TcpServiceData->IpIo, &OpenData);\r
326\r
327 if (EFI_ERROR (Status)) {\r
c4a62a12 328 goto ON_ERROR;\r
8a67d61d 329 }\r
330\r
331 //\r
332 // Create the timer event used by TCP driver\r
333 //\r
334 Status = Tcp4CreateTimer ();\r
335 if (EFI_ERROR (Status)) {\r
336\r
e48e37fc 337 DEBUG ((EFI_D_ERROR, "Tcp4DriverBindingStart: Create TcpTimer"\r
8a67d61d 338 " Event failed with %r\n", Status));\r
339\r
c4a62a12 340 goto ON_ERROR;\r
8a67d61d 341 }\r
342\r
343 //\r
344 // Install the Tcp4ServiceBinding Protocol on the\r
345 // controller handle\r
346 //\r
347 TcpServiceData->Tcp4ServiceBinding = mTcp4ServiceBinding;\r
348\r
349 Status = gBS->InstallMultipleProtocolInterfaces (\r
350 &ControllerHandle,\r
351 &gEfiTcp4ServiceBindingProtocolGuid,\r
352 &TcpServiceData->Tcp4ServiceBinding,\r
353 NULL\r
354 );\r
355 if (EFI_ERROR (Status)) {\r
356\r
e48e37fc 357 DEBUG ((EFI_D_ERROR, "Tcp4DriverBindingStart: Install Tcp4 Service Binding"\r
8a67d61d 358 " Protocol failed for %r\n", Status));\r
359\r
c4a62a12 360 Tcp4DestroyTimer ();\r
361 goto ON_ERROR;\r
8a67d61d 362 }\r
363\r
364 //\r
365 // Initialize member in TcpServiceData\r
366 //\r
367 TcpServiceData->ControllerHandle = ControllerHandle;\r
368 TcpServiceData->Signature = TCP4_DRIVER_SIGNATURE;\r
369 TcpServiceData->DriverBindingHandle = This->DriverBindingHandle;\r
370\r
e48e37fc 371 InitializeListHead (&TcpServiceData->SocketList);\r
c4a62a12 372\r
8a67d61d 373 TcpSetVariableData (TcpServiceData);\r
374\r
375 return EFI_SUCCESS;\r
376\r
c4a62a12 377ON_ERROR:\r
8a67d61d 378\r
c4a62a12 379 if (TcpServiceData->IpIo != NULL) {\r
380 IpIoDestroy (TcpServiceData->IpIo);\r
381 }\r
8a67d61d 382\r
e48e37fc 383 gBS->FreePool (TcpServiceData);\r
8a67d61d 384\r
385 return Status;\r
386}\r
387\r
388\r
389/**\r
390 Stop this driver on ControllerHandle.\r
dfc1f033 391 \r
392 The Stop() function is designed to be invoked from the EFI boot service \r
393 DisconnectController(). As a result, much of the error checking on the parameters \r
394 to Stop() has been moved into this common boot service. It is legal to call Stop() \r
395 from other locations, but the following calling restrictions must be followed \r
396 or the system behavior will not be deterministic.\r
397 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call \r
398 to this same driver's Start() function.\r
399 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
400 EFI_HANDLE. In addition, all of these handles must have been created in this \r
401 driver's Start() function, and the Start() function must have called OpenProtocol() \r
402 on ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
403 \r
404 @param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
405 @param ControllerHandle A handle to the device being stopped. The handle must \r
406 support a bus specific I/O protocol for the driver \r
407 to use to stop the device.\r
408 @param NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
409 @param ChildHandleBuffer An array of child handles to be freed. May be NULL if \r
410 NumberOfChildren is 0.\r
8a67d61d 411\r
dfc1f033 412 @retval EFI_SUCCESS The device was stopped.\r
413 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
8a67d61d 414\r
415**/\r
416EFI_STATUS\r
417EFIAPI\r
418Tcp4DriverBindingStop (\r
419 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
420 IN EFI_HANDLE ControllerHandle,\r
421 IN UINTN NumberOfChildren,\r
422 IN EFI_HANDLE *ChildHandleBuffer\r
423 )\r
424{\r
425 EFI_STATUS Status;\r
426 EFI_HANDLE NicHandle;\r
c4a62a12 427 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;\r
8a67d61d 428 TCP4_SERVICE_DATA *TcpServiceData;\r
8a67d61d 429 SOCKET *Sock;\r
8a67d61d 430\r
431 // Find the NicHandle where Tcp4 ServiceBinding Protocol is installed.\r
432 //\r
433 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiIp4ProtocolGuid);\r
434 if (NicHandle == NULL) {\r
c4a62a12 435 return EFI_DEVICE_ERROR;\r
8a67d61d 436 }\r
437\r
438 //\r
439 // Retrieve the TCP driver Data Structure\r
440 //\r
441 Status = gBS->OpenProtocol (\r
442 NicHandle,\r
443 &gEfiTcp4ServiceBindingProtocolGuid,\r
c4a62a12 444 (VOID **) &ServiceBinding,\r
8a67d61d 445 This->DriverBindingHandle,\r
446 ControllerHandle,\r
447 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
448 );\r
449 if (EFI_ERROR (Status)) {\r
450\r
e48e37fc 451 DEBUG ((EFI_D_ERROR, "Tcp4DriverBindingStop: Locate Tcp4 Service "\r
8a67d61d 452 " Binding Protocol failed with %r\n", Status));\r
453\r
c4a62a12 454 return EFI_DEVICE_ERROR;\r
8a67d61d 455 }\r
456\r
c4a62a12 457 TcpServiceData = TCP4_FROM_THIS (ServiceBinding);\r
8a67d61d 458\r
c4a62a12 459 if (NumberOfChildren == 0) {\r
8a67d61d 460 //\r
c4a62a12 461 // Uninstall TCP servicebinding protocol\r
8a67d61d 462 //\r
c4a62a12 463 gBS->UninstallMultipleProtocolInterfaces (\r
464 NicHandle,\r
465 &gEfiTcp4ServiceBindingProtocolGuid,\r
466 ServiceBinding,\r
467 NULL\r
468 );\r
8a67d61d 469\r
c4a62a12 470 //\r
471 // Destroy the IpIO consumed by TCP driver\r
472 //\r
473 IpIoDestroy (TcpServiceData->IpIo);\r
8a67d61d 474\r
c4a62a12 475 //\r
476 // Destroy the heartbeat timer.\r
477 //\r
478 Tcp4DestroyTimer ();\r
8a67d61d 479\r
c4a62a12 480 //\r
481 // Clear the variable.\r
482 //\r
483 TcpClearVariableData (TcpServiceData);\r
8a67d61d 484\r
485 //\r
c4a62a12 486 // Release the TCP service data\r
8a67d61d 487 //\r
e48e37fc 488 gBS->FreePool (TcpServiceData);\r
c4a62a12 489 } else {\r
8a67d61d 490\r
e48e37fc 491 while (!IsListEmpty (&TcpServiceData->SocketList)) {\r
c4a62a12 492 Sock = NET_LIST_HEAD (&TcpServiceData->SocketList, SOCKET, Link);\r
8a67d61d 493\r
c4a62a12 494 ServiceBinding->DestroyChild (ServiceBinding, Sock->SockHandle);\r
8a67d61d 495 }\r
496 }\r
497\r
8a67d61d 498 return Status;\r
499}\r
500\r
120db52c 501/**\r
502 Open Ip4 and device path protocols for a created socket, and insert it in \r
503 socket list.\r
504 \r
505 @param This Pointer to the socket just created\r
506 @param Context Context of the socket\r
507 \r
508 @retval EFI_SUCCESS This protocol is installed successfully.\r
509 @retval other Some error occured.\r
510 \r
511**/\r
4f6e31e4 512EFI_STATUS\r
513Tcp4CreateSocketCallback (\r
514 IN SOCKET *This,\r
515 IN VOID *Context\r
516 )\r
517{\r
518 EFI_STATUS Status;\r
519 TCP4_SERVICE_DATA *TcpServiceData;\r
520 EFI_IP4_PROTOCOL *Ip4;\r
521\r
522 TcpServiceData = ((TCP4_PROTO_DATA *) This->ProtoReserved)->TcpService;\r
523\r
524 //\r
525 // Open the default Ip4 protocol of IP_IO BY_DRIVER.\r
526 //\r
527 Status = gBS->OpenProtocol (\r
528 TcpServiceData->IpIo->ChildHandle,\r
529 &gEfiIp4ProtocolGuid,\r
530 (VOID **) &Ip4,\r
531 TcpServiceData->DriverBindingHandle,\r
532 This->SockHandle,\r
533 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
534 );\r
535 if (EFI_ERROR (Status)) {\r
536 return Status;\r
537 }\r
538\r
539 //\r
540 // Open the device path on the handle where service binding resides on.\r
541 //\r
542 Status = gBS->OpenProtocol (\r
543 TcpServiceData->ControllerHandle,\r
544 &gEfiDevicePathProtocolGuid,\r
545 (VOID **) &This->ParentDevicePath,\r
546 TcpServiceData->DriverBindingHandle,\r
547 This->SockHandle,\r
548 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
549 );\r
550 if (EFI_ERROR (Status)) {\r
551 gBS->CloseProtocol (\r
552 TcpServiceData->IpIo->ChildHandle,\r
553 &gEfiIp4ProtocolGuid,\r
554 TcpServiceData->DriverBindingHandle,\r
555 This->SockHandle\r
556 );\r
557 } else {\r
558 //\r
559 // Insert this socket into the SocketList.\r
560 //\r
e48e37fc 561 InsertTailList (&TcpServiceData->SocketList, &This->Link);\r
4f6e31e4 562 }\r
563\r
564 return Status;\r
565}\r
566\r
120db52c 567/**\r
568 Close Ip4 and device path protocols for a socket, and remove it from socket list. \r
569 \r
570 @param This Pointer to the socket to be removed\r
571 @param Context Context of the socket\r
572 \r
573**/\r
4f6e31e4 574VOID\r
575Tcp4DestroySocketCallback (\r
576 IN SOCKET *This,\r
577 IN VOID *Context\r
578 )\r
579{\r
580 TCP4_SERVICE_DATA *TcpServiceData;\r
581\r
582 TcpServiceData = ((TCP4_PROTO_DATA *) This->ProtoReserved)->TcpService;\r
583\r
584 //\r
585 // Remove this node from the list.\r
586 //\r
e48e37fc 587 RemoveEntryList (&This->Link);\r
4f6e31e4 588\r
589 //\r
590 // Close the device path protocol\r
591 //\r
592 gBS->CloseProtocol (\r
593 TcpServiceData->ControllerHandle,\r
594 &gEfiDevicePathProtocolGuid,\r
595 TcpServiceData->DriverBindingHandle,\r
596 This->SockHandle\r
597 );\r
598\r
599 //\r
600 // Close the Ip4 protocol.\r
601 //\r
602 gBS->CloseProtocol (\r
603 TcpServiceData->IpIo->ChildHandle,\r
604 &gEfiIp4ProtocolGuid,\r
605 TcpServiceData->DriverBindingHandle,\r
606 This->SockHandle\r
607 );\r
608}\r
609\r
8a67d61d 610/**\r
dfc1f033 611 Creates a child handle and installs a protocol.\r
612 \r
613 The CreateChild() function installs a protocol on ChildHandle. If ChildHandle \r
614 is a pointer to NULL, then a new handle is created and returned in ChildHandle. \r
615 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing \r
616 ChildHandle.\r
617\r
618 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
619 @param ChildHandle Pointer to the handle of the child to create. If it is NULL, then \r
620 a new handle is created. If it is a pointer to an existing UEFI \r
621 handle, then the protocol is added to the existing UEFI handle.\r
622\r
623 @retval EFI_SUCCES The protocol was added to ChildHandle.\r
624 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r
625 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create\r
626 the child.\r
627 @retval other The child handle was not created.\r
8a67d61d 628\r
629**/\r
630EFI_STATUS\r
631EFIAPI\r
632Tcp4ServiceBindingCreateChild (\r
276dcc1b 633 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
634 IN OUT EFI_HANDLE *ChildHandle\r
8a67d61d 635 )\r
636{\r
637 SOCKET *Sock;\r
638 TCP4_SERVICE_DATA *TcpServiceData;\r
639 TCP4_PROTO_DATA TcpProto;\r
640 EFI_STATUS Status;\r
8a67d61d 641 EFI_TPL OldTpl;\r
642\r
643 if (NULL == This || NULL == ChildHandle) {\r
644 return EFI_INVALID_PARAMETER;\r
645 }\r
646\r
e48e37fc 647 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
4f6e31e4 648 Status = EFI_SUCCESS;\r
8a67d61d 649 TcpServiceData = TCP4_FROM_THIS (This);\r
650 TcpProto.TcpService = TcpServiceData;\r
651 TcpProto.TcpPcb = NULL;\r
652\r
653 //\r
654 // Create a tcp instance with defualt Tcp default\r
655 // sock init data and TcpProto\r
656 //\r
4f6e31e4 657 mTcp4DefaultSockData.ProtoData = &TcpProto;\r
658 mTcp4DefaultSockData.DataSize = sizeof (TCP4_PROTO_DATA);\r
8a67d61d 659 mTcp4DefaultSockData.DriverBinding = TcpServiceData->DriverBindingHandle;\r
e48e37fc 660\r
4f6e31e4 661 Sock = SockCreateChild (&mTcp4DefaultSockData);\r
8a67d61d 662 if (NULL == Sock) {\r
e48e37fc 663 DEBUG ((EFI_D_ERROR, "Tcp4DriverBindingCreateChild: "\r
8a67d61d 664 "No resource to create a Tcp Child\n"));\r
665\r
666 Status = EFI_OUT_OF_RESOURCES;\r
c4a62a12 667 } else {\r
4f6e31e4 668 *ChildHandle = Sock->SockHandle;\r
8a67d61d 669 }\r
670\r
e48e37fc 671 gBS->RestoreTPL (OldTpl);\r
8a67d61d 672 return Status;\r
673}\r
674\r
675\r
676/**\r
dfc1f033 677 Destroys a child handle with a protocol installed on it.\r
678 \r
679 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol \r
680 that was installed by CreateChild() from ChildHandle. If the removed protocol is the \r
681 last protocol on ChildHandle, then ChildHandle is destroyed.\r
682\r
683 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
684 @param ChildHandle Handle of the child to destroy\r
685\r
686 @retval EFI_SUCCES The protocol was removed from ChildHandle.\r
687 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is \r
688 being removed.\r
689 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.\r
690 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle\r
691 because its services are being used.\r
692 @retval other The child handle was not destroyed.\r
693 \r
8a67d61d 694**/\r
695EFI_STATUS\r
696EFIAPI\r
697Tcp4ServiceBindingDestroyChild (\r
698 IN EFI_SERVICE_BINDING_PROTOCOL *This,\r
699 IN EFI_HANDLE ChildHandle\r
700 )\r
701{\r
702 EFI_STATUS Status;\r
703 EFI_TCP4_PROTOCOL *Tcp4;\r
704 SOCKET *Sock;\r
8a67d61d 705 EFI_TPL OldTpl;\r
706\r
707 if (NULL == This || NULL == ChildHandle) {\r
708 return EFI_INVALID_PARAMETER;\r
709 }\r
710\r
e48e37fc 711 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 712\r
713 //\r
714 // retrieve the Tcp4 protocol from ChildHandle\r
715 //\r
716 Status = gBS->OpenProtocol (\r
717 ChildHandle,\r
718 &gEfiTcp4ProtocolGuid,\r
719 (VOID **) &Tcp4,\r
720 mTcp4DriverBinding.DriverBindingHandle,\r
721 ChildHandle,\r
722 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
723 );\r
724 if (EFI_ERROR (Status)) {\r
725 Status = EFI_UNSUPPORTED;\r
4f6e31e4 726 } else {\r
727 //\r
728 // destroy this sock and related Tcp protocol control\r
729 // block\r
730 //\r
731 Sock = SOCK_FROM_THIS (Tcp4);\r
e5e12de7 732\r
4f6e31e4 733 SockDestroyChild (Sock);\r
734 }\r
8a67d61d 735\r
e48e37fc 736 gBS->RestoreTPL (OldTpl);\r
8a67d61d 737 return Status;\r
738}\r
4f6e31e4 739\r