]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Driver.c
BaseTools:Change the path of the file that Binary Cache
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Driver.c
1 /** @file
2 The driver binding and service binding protocol for IP4 driver.
3
4 Copyright (c) 2005 - 2019, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "Ip4Impl.h"
12
13 EFI_DRIVER_BINDING_PROTOCOL gIp4DriverBinding = {
14 Ip4DriverBindingSupported,
15 Ip4DriverBindingStart,
16 Ip4DriverBindingStop,
17 0xa,
18 NULL,
19 NULL
20 };
21
22 BOOLEAN mIpSec2Installed = FALSE;
23
24 /**
25 Callback function for IpSec2 Protocol install.
26
27 @param[in] Event Event whose notification function is being invoked
28 @param[in] Context Pointer to the notification function's context
29
30 **/
31 VOID
32 EFIAPI
33 IpSec2InstalledCallback (
34 IN EFI_EVENT Event,
35 IN VOID *Context
36 )
37 {
38 EFI_STATUS Status;
39 //
40 // Test if protocol was even found.
41 // Notification function will be called at least once.
42 //
43 Status = gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **)&mIpSec);
44 if (Status == EFI_SUCCESS && mIpSec != NULL) {
45 //
46 // Close the event so it does not get called again.
47 //
48 gBS->CloseEvent (Event);
49
50 mIpSec2Installed = TRUE;
51 }
52 }
53
54 /**
55 This is the declaration of an EFI image entry point. This entry point is
56 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
57 both device drivers and bus drivers.
58
59 The entry point for IP4 driver which install the driver
60 binding and component name protocol on its image.
61
62 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
63 @param[in] SystemTable A pointer to the EFI System Table.
64
65 @retval EFI_SUCCESS The operation completed successfully.
66 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
67
68 **/
69 EFI_STATUS
70 EFIAPI
71 Ip4DriverEntryPoint (
72 IN EFI_HANDLE ImageHandle,
73 IN EFI_SYSTEM_TABLE *SystemTable
74 )
75 {
76 VOID *Registration;
77
78 EfiCreateProtocolNotifyEvent (
79 &gEfiIpSec2ProtocolGuid,
80 TPL_CALLBACK,
81 IpSec2InstalledCallback,
82 NULL,
83 &Registration
84 );
85
86 return EfiLibInstallDriverBindingComponentName2 (
87 ImageHandle,
88 SystemTable,
89 &gIp4DriverBinding,
90 ImageHandle,
91 &gIp4ComponentName,
92 &gIp4ComponentName2
93 );
94 }
95
96 /**
97 Test to see if this driver supports ControllerHandle. This service
98 is called by the EFI boot service ConnectController(). In
99 order to make drivers as small as possible, there are a few calling
100 restrictions for this service. ConnectController() must
101 follow these calling restrictions. If any other agent wishes to call
102 Supported() it must also follow these calling restrictions.
103
104 @param[in] This Protocol instance pointer.
105 @param[in] ControllerHandle Handle of device to test
106 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
107 device to start.
108
109 @retval EFI_SUCCESS This driver supports this device
110 @retval EFI_ALREADY_STARTED This driver is already running on this device
111 @retval other This driver does not support this device
112
113 **/
114 EFI_STATUS
115 EFIAPI
116 Ip4DriverBindingSupported (
117 IN EFI_DRIVER_BINDING_PROTOCOL * This,
118 IN EFI_HANDLE ControllerHandle,
119 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
120 )
121 {
122 EFI_STATUS Status;
123
124 //
125 // Test for the MNP service binding Protocol
126 //
127 Status = gBS->OpenProtocol (
128 ControllerHandle,
129 &gEfiManagedNetworkServiceBindingProtocolGuid,
130 NULL,
131 This->DriverBindingHandle,
132 ControllerHandle,
133 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
134 );
135
136 if (EFI_ERROR (Status)) {
137 return Status;
138 }
139
140 //
141 // Test for the Arp service binding Protocol
142 //
143 Status = gBS->OpenProtocol (
144 ControllerHandle,
145 &gEfiArpServiceBindingProtocolGuid,
146 NULL,
147 This->DriverBindingHandle,
148 ControllerHandle,
149 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
150 );
151
152 return Status;
153 }
154
155 /**
156 Clean up a IP4 service binding instance. It will release all
157 the resource allocated by the instance. The instance may be
158 partly initialized, or partly destroyed. If a resource is
159 destroyed, it is marked as that in case the destroy failed and
160 being called again later.
161
162 @param[in] IpSb The IP4 service binding instance to clean up
163
164 @retval EFI_SUCCESS The resource used by the instance are cleaned up
165 @retval other Failed to clean up some of the resources.
166
167 **/
168 EFI_STATUS
169 Ip4CleanService (
170 IN IP4_SERVICE *IpSb
171 );
172
173
174 /**
175 Create a new IP4 driver service binding private instance.
176
177 @param Controller The controller that has MNP service binding
178 installed
179 @param ImageHandle The IP4 driver's image handle
180 @param Service The variable to receive the newly created IP4
181 service.
182
183 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource
184 @retval EFI_SUCCESS A new IP4 service binding private is created.
185 @retval other Other error occurs.
186
187 **/
188 EFI_STATUS
189 Ip4CreateService (
190 IN EFI_HANDLE Controller,
191 IN EFI_HANDLE ImageHandle,
192 OUT IP4_SERVICE **Service
193 )
194 {
195 IP4_SERVICE *IpSb;
196 EFI_STATUS Status;
197
198 ASSERT (Service != NULL);
199
200 *Service = NULL;
201
202 //
203 // allocate a service private data then initialize all the filed to
204 // empty resources, so if any thing goes wrong when allocating
205 // resources, Ip4CleanService can be called to clean it up.
206 //
207 IpSb = AllocateZeroPool (sizeof (IP4_SERVICE));
208
209 if (IpSb == NULL) {
210 return EFI_OUT_OF_RESOURCES;
211 }
212
213 IpSb->Signature = IP4_SERVICE_SIGNATURE;
214 IpSb->ServiceBinding.CreateChild = Ip4ServiceBindingCreateChild;
215 IpSb->ServiceBinding.DestroyChild = Ip4ServiceBindingDestroyChild;
216 IpSb->State = IP4_SERVICE_UNSTARTED;
217
218 IpSb->NumChildren = 0;
219 InitializeListHead (&IpSb->Children);
220
221 InitializeListHead (&IpSb->Interfaces);
222 IpSb->DefaultInterface = NULL;
223 IpSb->DefaultRouteTable = NULL;
224
225 Ip4InitAssembleTable (&IpSb->Assemble);
226
227 IpSb->IgmpCtrl.Igmpv1QuerySeen = 0;
228 InitializeListHead (&IpSb->IgmpCtrl.Groups);
229
230 IpSb->Image = ImageHandle;
231 IpSb->Controller = Controller;
232
233 IpSb->MnpChildHandle = NULL;
234 IpSb->Mnp = NULL;
235
236 IpSb->MnpConfigData.ReceivedQueueTimeoutValue = 0;
237 IpSb->MnpConfigData.TransmitQueueTimeoutValue = 0;
238 IpSb->MnpConfigData.ProtocolTypeFilter = IP4_ETHER_PROTO;
239 IpSb->MnpConfigData.EnableUnicastReceive = TRUE;
240 IpSb->MnpConfigData.EnableMulticastReceive = TRUE;
241 IpSb->MnpConfigData.EnableBroadcastReceive = TRUE;
242 IpSb->MnpConfigData.EnablePromiscuousReceive = FALSE;
243 IpSb->MnpConfigData.FlushQueuesOnReset = TRUE;
244 IpSb->MnpConfigData.EnableReceiveTimestamps = FALSE;
245 IpSb->MnpConfigData.DisableBackgroundPolling = FALSE;
246
247 ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE));
248
249 IpSb->Timer = NULL;
250 IpSb->ReconfigCheckTimer = NULL;
251
252 IpSb->ReconfigEvent = NULL;
253
254 IpSb->Reconfig = FALSE;
255
256 IpSb->MediaPresent = TRUE;
257
258 //
259 // Create various resources. First create the route table, timer
260 // event, ReconfigEvent and MNP child. IGMP, interface's initialization depend
261 // on the MNP child.
262 //
263 IpSb->DefaultRouteTable = Ip4CreateRouteTable ();
264
265 if (IpSb->DefaultRouteTable == NULL) {
266 Status = EFI_OUT_OF_RESOURCES;
267 goto ON_ERROR;
268 }
269
270 Status = gBS->CreateEvent (
271 EVT_NOTIFY_SIGNAL | EVT_TIMER,
272 TPL_CALLBACK,
273 Ip4TimerTicking,
274 IpSb,
275 &IpSb->Timer
276 );
277
278 if (EFI_ERROR (Status)) {
279 goto ON_ERROR;
280 }
281
282 Status = gBS->CreateEvent (
283 EVT_NOTIFY_SIGNAL | EVT_TIMER,
284 TPL_CALLBACK,
285 Ip4TimerReconfigChecking,
286 IpSb,
287 &IpSb->ReconfigCheckTimer
288 );
289
290 if (EFI_ERROR (Status)) {
291 goto ON_ERROR;
292 }
293
294 Status = gBS->CreateEvent (
295 EVT_NOTIFY_SIGNAL,
296 TPL_NOTIFY,
297 Ip4AutoReconfigCallBack,
298 IpSb,
299 &IpSb->ReconfigEvent
300 );
301 if (EFI_ERROR (Status)) {
302 goto ON_ERROR;
303 }
304
305 Status = NetLibCreateServiceChild (
306 Controller,
307 ImageHandle,
308 &gEfiManagedNetworkServiceBindingProtocolGuid,
309 &IpSb->MnpChildHandle
310 );
311
312 if (EFI_ERROR (Status)) {
313 goto ON_ERROR;
314 }
315
316 Status = gBS->OpenProtocol (
317 IpSb->MnpChildHandle,
318 &gEfiManagedNetworkProtocolGuid,
319 (VOID **) &IpSb->Mnp,
320 ImageHandle,
321 Controller,
322 EFI_OPEN_PROTOCOL_BY_DRIVER
323 );
324
325 if (EFI_ERROR (Status)) {
326 goto ON_ERROR;
327 }
328
329 Status = Ip4ServiceConfigMnp (IpSb, TRUE);
330
331 if (EFI_ERROR (Status)) {
332 goto ON_ERROR;
333 }
334
335 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode);
336
337 if (EFI_ERROR (Status)) {
338 goto ON_ERROR;
339 }
340
341 Status = Ip4InitIgmp (IpSb);
342
343 if (EFI_ERROR (Status)) {
344 goto ON_ERROR;
345 }
346
347 IpSb->MacString = NULL;
348 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &IpSb->MacString);
349
350 if (EFI_ERROR (Status)) {
351 goto ON_ERROR;
352 }
353
354 IpSb->DefaultInterface = Ip4CreateInterface (IpSb->Mnp, Controller, ImageHandle);
355
356 if (IpSb->DefaultInterface == NULL) {
357 Status = EFI_OUT_OF_RESOURCES;
358 goto ON_ERROR;
359 }
360
361 InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);
362
363 ZeroMem (&IpSb->Ip4Config2Instance, sizeof (IP4_CONFIG2_INSTANCE));
364
365 Status = Ip4Config2InitInstance (&IpSb->Ip4Config2Instance);
366
367 if (EFI_ERROR (Status)) {
368 goto ON_ERROR;
369 }
370
371 IpSb->MaxPacketSize = IpSb->SnpMode.MaxPacketSize - sizeof (IP4_HEAD);
372 if (NetLibGetVlanId (IpSb->Controller) != 0) {
373 //
374 // This is a VLAN device, reduce MTU by VLAN tag length
375 //
376 IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN;
377 }
378 IpSb->OldMaxPacketSize = IpSb->MaxPacketSize;
379 *Service = IpSb;
380
381 return EFI_SUCCESS;
382
383 ON_ERROR:
384 Ip4CleanService (IpSb);
385 FreePool (IpSb);
386
387 return Status;
388 }
389
390
391 /**
392 Clean up a IP4 service binding instance. It will release all
393 the resource allocated by the instance. The instance may be
394 partly initialized, or partly destroyed. If a resource is
395 destroyed, it is marked as that in case the destroy failed and
396 being called again later.
397
398 @param[in] IpSb The IP4 service binding instance to clean up
399
400 @retval EFI_SUCCESS The resource used by the instance are cleaned up
401 @retval other Failed to clean up some of the resources.
402
403 **/
404 EFI_STATUS
405 Ip4CleanService (
406 IN IP4_SERVICE *IpSb
407 )
408 {
409 EFI_STATUS Status;
410
411 IpSb->State = IP4_SERVICE_DESTROY;
412
413 if (IpSb->Timer != NULL) {
414 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
415 gBS->CloseEvent (IpSb->Timer);
416
417 IpSb->Timer = NULL;
418 }
419
420 if (IpSb->ReconfigCheckTimer != NULL) {
421 gBS->SetTimer (IpSb->ReconfigCheckTimer, TimerCancel, 0);
422 gBS->CloseEvent (IpSb->ReconfigCheckTimer);
423
424 IpSb->ReconfigCheckTimer = NULL;
425 }
426
427 if (IpSb->DefaultInterface != NULL) {
428 Status = Ip4FreeInterface (IpSb->DefaultInterface, NULL);
429
430 if (EFI_ERROR (Status)) {
431 return Status;
432 }
433
434 IpSb->DefaultInterface = NULL;
435 }
436
437 if (IpSb->DefaultRouteTable != NULL) {
438 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
439 IpSb->DefaultRouteTable = NULL;
440 }
441
442 Ip4CleanAssembleTable (&IpSb->Assemble);
443
444 if (IpSb->MnpChildHandle != NULL) {
445 if (IpSb->Mnp != NULL) {
446 gBS->CloseProtocol (
447 IpSb->MnpChildHandle,
448 &gEfiManagedNetworkProtocolGuid,
449 IpSb->Image,
450 IpSb->Controller
451 );
452
453 IpSb->Mnp = NULL;
454 }
455
456 NetLibDestroyServiceChild (
457 IpSb->Controller,
458 IpSb->Image,
459 &gEfiManagedNetworkServiceBindingProtocolGuid,
460 IpSb->MnpChildHandle
461 );
462
463 IpSb->MnpChildHandle = NULL;
464 }
465
466 if (IpSb->ReconfigEvent != NULL) {
467 gBS->CloseEvent (IpSb->ReconfigEvent);
468
469 IpSb->ReconfigEvent = NULL;
470 }
471
472 IpSb->Reconfig = FALSE;
473
474 if (IpSb->MacString != NULL) {
475 FreePool (IpSb->MacString);
476 }
477
478 Ip4Config2CleanInstance (&IpSb->Ip4Config2Instance);
479
480 return EFI_SUCCESS;
481 }
482
483 /**
484 Callback function which provided by user to remove one node in NetDestroyLinkList process.
485
486 @param[in] Entry The entry to be removed.
487 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
488
489 @retval EFI_SUCCESS The entry has been removed successfully.
490 @retval Others Fail to remove the entry.
491
492 **/
493 EFI_STATUS
494 EFIAPI
495 Ip4DestroyChildEntryInHandleBuffer (
496 IN LIST_ENTRY *Entry,
497 IN VOID *Context
498 )
499 {
500 IP4_PROTOCOL *IpInstance;
501 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
502 UINTN NumberOfChildren;
503 EFI_HANDLE *ChildHandleBuffer;
504
505 if (Entry == NULL || Context == NULL) {
506 return EFI_INVALID_PARAMETER;
507 }
508
509 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);
510 ServiceBinding = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding;
511 NumberOfChildren = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren;
512 ChildHandleBuffer = ((IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer;
513
514 if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) {
515 return EFI_SUCCESS;
516 }
517
518 return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
519 }
520
521 /**
522 Start this driver on ControllerHandle. This service is called by the
523 EFI boot service ConnectController(). In order to make
524 drivers as small as possible, there are a few calling restrictions for
525 this service. ConnectController() must follow these
526 calling restrictions. If any other agent wishes to call Start() it
527 must also follow these calling restrictions.
528
529 @param[in] This Protocol instance pointer.
530 @param[in] ControllerHandle Handle of device to bind driver to
531 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
532 device to start.
533
534 @retval EFI_SUCCESS This driver is added to ControllerHandle
535 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
536 @retval other This driver does not support this device
537
538 **/
539 EFI_STATUS
540 EFIAPI
541 Ip4DriverBindingStart (
542 IN EFI_DRIVER_BINDING_PROTOCOL *This,
543 IN EFI_HANDLE ControllerHandle,
544 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
545 )
546 {
547 EFI_STATUS Status;
548 IP4_SERVICE *IpSb;
549 EFI_IP4_CONFIG2_PROTOCOL *Ip4Cfg2;
550 UINTN Index;
551 IP4_CONFIG2_DATA_ITEM *DataItem;
552
553 IpSb = NULL;
554 Ip4Cfg2 = NULL;
555 DataItem = NULL;
556
557 //
558 // Test for the Ip4 service binding protocol
559 //
560 Status = gBS->OpenProtocol (
561 ControllerHandle,
562 &gEfiIp4ServiceBindingProtocolGuid,
563 NULL,
564 This->DriverBindingHandle,
565 ControllerHandle,
566 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
567 );
568
569 if (Status == EFI_SUCCESS) {
570 return EFI_ALREADY_STARTED;
571 }
572
573 Status = Ip4CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);
574
575 if (EFI_ERROR (Status)) {
576 return Status;
577 }
578
579 ASSERT (IpSb != NULL);
580
581 Ip4Cfg2 = &IpSb->Ip4Config2Instance.Ip4Config2;
582
583 //
584 // Install the Ip4ServiceBinding Protocol onto ControlerHandle
585 //
586 Status = gBS->InstallMultipleProtocolInterfaces (
587 &ControllerHandle,
588 &gEfiIp4ServiceBindingProtocolGuid,
589 &IpSb->ServiceBinding,
590 &gEfiIp4Config2ProtocolGuid,
591 Ip4Cfg2,
592 NULL
593 );
594
595 if (EFI_ERROR (Status)) {
596 goto FREE_SERVICE;
597 }
598
599 //
600 // Read the config data from NV variable again.
601 // The default data can be changed by other drivers.
602 //
603 Status = Ip4Config2ReadConfigData (IpSb->MacString, &IpSb->Ip4Config2Instance);
604 if (EFI_ERROR (Status)) {
605 goto UNINSTALL_PROTOCOL;
606 }
607
608 //
609 // Consume the installed EFI_IP4_CONFIG2_PROTOCOL to set the default data items.
610 //
611 for (Index = Ip4Config2DataTypePolicy; Index < Ip4Config2DataTypeMaximum; Index++) {
612 DataItem = &IpSb->Ip4Config2Instance.DataItem[Index];
613 if (DataItem->Data.Ptr != NULL) {
614 Status = Ip4Cfg2->SetData (
615 Ip4Cfg2,
616 Index,
617 DataItem->DataSize,
618 DataItem->Data.Ptr
619 );
620 if (EFI_ERROR(Status)) {
621 goto UNINSTALL_PROTOCOL;
622 }
623
624 if (Index == Ip4Config2DataTypePolicy && (*(DataItem->Data.Policy) == Ip4Config2PolicyDhcp)) {
625 break;
626 }
627 }
628 }
629
630 //
631 // Ready to go: start the receiving and timer.
632 // Ip4Config2SetPolicy maybe call Ip4ReceiveFrame() to set the default interface's RecvRequest first after
633 // Ip4Config2 instance is initialized. So, EFI_ALREADY_STARTED is the allowed return status.
634 //
635 Status = Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);
636
637 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
638 goto UNINSTALL_PROTOCOL;
639 }
640
641 Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_SECOND);
642
643 if (EFI_ERROR (Status)) {
644 goto UNINSTALL_PROTOCOL;
645 }
646
647 Status = gBS->SetTimer (IpSb->ReconfigCheckTimer, TimerPeriodic, 500 * TICKS_PER_MS);
648
649 if (EFI_ERROR (Status)) {
650 goto UNINSTALL_PROTOCOL;
651 }
652
653 //
654 // Initialize the IP4 ID
655 //
656 mIp4Id = (UINT16)NET_RANDOM (NetRandomInitSeed ());
657
658 return Status;
659
660 UNINSTALL_PROTOCOL:
661 gBS->UninstallMultipleProtocolInterfaces (
662 ControllerHandle,
663 &gEfiIp4ServiceBindingProtocolGuid,
664 &IpSb->ServiceBinding,
665 &gEfiIp4Config2ProtocolGuid,
666 Ip4Cfg2,
667 NULL
668 );
669
670 FREE_SERVICE:
671 Ip4CleanService (IpSb);
672 FreePool (IpSb);
673 return Status;
674 }
675
676
677 /**
678 Stop this driver on ControllerHandle. This service is called by the
679 EFI boot service DisconnectController(). In order to
680 make drivers as small as possible, there are a few calling
681 restrictions for this service. DisconnectController()
682 must follow these calling restrictions. If any other agent wishes
683 to call Stop() it must also follow these calling restrictions.
684
685 @param[in] This Protocol instance pointer.
686 @param[in] ControllerHandle Handle of device to stop driver on
687 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
688 of children is zero stop the entire bus driver.
689 @param[in] ChildHandleBuffer List of Child Handles to Stop.
690
691 @retval EFI_SUCCESS This driver is removed ControllerHandle
692 @retval other This driver was not removed from this device
693
694 **/
695 EFI_STATUS
696 EFIAPI
697 Ip4DriverBindingStop (
698 IN EFI_DRIVER_BINDING_PROTOCOL *This,
699 IN EFI_HANDLE ControllerHandle,
700 IN UINTN NumberOfChildren,
701 IN EFI_HANDLE *ChildHandleBuffer
702 )
703 {
704 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
705 IP4_SERVICE *IpSb;
706 EFI_HANDLE NicHandle;
707 EFI_STATUS Status;
708 INTN State;
709 LIST_ENTRY *List;
710 IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
711 IP4_INTERFACE *IpIf;
712 IP4_ROUTE_TABLE *RouteTable;
713
714 BOOLEAN IsDhcp4;
715
716 IsDhcp4 = FALSE;
717
718 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
719 if (NicHandle == NULL) {
720 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);
721 if (NicHandle == NULL) {
722 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid);
723 if (NicHandle != NULL) {
724 IsDhcp4 = TRUE;
725 } else {
726 return EFI_SUCCESS;
727 }
728 }
729 }
730
731 Status = gBS->OpenProtocol (
732 NicHandle,
733 &gEfiIp4ServiceBindingProtocolGuid,
734 (VOID **) &ServiceBinding,
735 This->DriverBindingHandle,
736 NicHandle,
737 EFI_OPEN_PROTOCOL_GET_PROTOCOL
738 );
739 if (EFI_ERROR (Status)) {
740 return EFI_DEVICE_ERROR;
741 }
742
743 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
744
745 if (IsDhcp4) {
746 Status = Ip4Config2DestroyDhcp4 (&IpSb->Ip4Config2Instance);
747 gBS->CloseEvent (IpSb->Ip4Config2Instance.Dhcp4Event);
748 IpSb->Ip4Config2Instance.Dhcp4Event = NULL;
749 } else if (NumberOfChildren != 0) {
750 List = &IpSb->Children;
751 Context.ServiceBinding = ServiceBinding;
752 Context.NumberOfChildren = NumberOfChildren;
753 Context.ChildHandleBuffer = ChildHandleBuffer;
754 Status = NetDestroyLinkList (
755 List,
756 Ip4DestroyChildEntryInHandleBuffer,
757 &Context,
758 NULL
759 );
760 } else if (IpSb->DefaultInterface->ArpHandle == ControllerHandle) {
761
762 //
763 // The ARP protocol for the default interface is being uninstalled and all
764 // its IP child handles should have been destroyed before. So, release the
765 // default interface and route table, create a new one and mark it as not started.
766 //
767 Ip4CancelReceive (IpSb->DefaultInterface);
768 Ip4FreeInterface (IpSb->DefaultInterface, NULL);
769 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
770
771 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
772 if (IpIf == NULL) {
773 goto ON_ERROR;
774 }
775 RouteTable = Ip4CreateRouteTable ();
776 if (RouteTable == NULL) {
777 Ip4FreeInterface (IpIf, NULL);
778 goto ON_ERROR;;
779 }
780
781 IpSb->DefaultInterface = IpIf;
782 InsertHeadList (&IpSb->Interfaces, &IpIf->Link);
783 IpSb->DefaultRouteTable = RouteTable;
784 Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);
785
786 IpSb->State = IP4_SERVICE_UNSTARTED;
787
788 } else if (IsListEmpty (&IpSb->Children)) {
789 State = IpSb->State;
790 //
791 // OK, clean other resources then uninstall the service binding protocol.
792 //
793 Status = Ip4CleanService (IpSb);
794 if (EFI_ERROR (Status)) {
795 IpSb->State = State;
796 goto ON_ERROR;
797 }
798
799 gBS->UninstallMultipleProtocolInterfaces (
800 NicHandle,
801 &gEfiIp4ServiceBindingProtocolGuid,
802 ServiceBinding,
803 &gEfiIp4Config2ProtocolGuid,
804 &IpSb->Ip4Config2Instance.Ip4Config2,
805 NULL
806 );
807
808 if (gIp4ControllerNameTable != NULL) {
809 FreeUnicodeStringTable (gIp4ControllerNameTable);
810 gIp4ControllerNameTable = NULL;
811 }
812 FreePool (IpSb);
813 }
814
815 ON_ERROR:
816 return Status;
817 }
818
819
820 /**
821 Creates a child handle and installs a protocol.
822
823 The CreateChild() function installs a protocol on ChildHandle.
824 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
825 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
826
827 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
828 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
829 then a new handle is created. If it is a pointer to an existing UEFI handle,
830 then the protocol is added to the existing UEFI handle.
831
832 @retval EFI_SUCCES The protocol was added to ChildHandle.
833 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
834 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
835 the child
836 @retval other The child handle was not created
837
838 **/
839 EFI_STATUS
840 EFIAPI
841 Ip4ServiceBindingCreateChild (
842 IN EFI_SERVICE_BINDING_PROTOCOL *This,
843 IN OUT EFI_HANDLE *ChildHandle
844 )
845 {
846 IP4_SERVICE *IpSb;
847 IP4_PROTOCOL *IpInstance;
848 EFI_TPL OldTpl;
849 EFI_STATUS Status;
850 VOID *Mnp;
851
852 if ((This == NULL) || (ChildHandle == NULL)) {
853 return EFI_INVALID_PARAMETER;
854 }
855
856 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
857 IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));
858
859 if (IpInstance == NULL) {
860 return EFI_OUT_OF_RESOURCES;
861 }
862
863 Ip4InitProtocol (IpSb, IpInstance);
864
865 //
866 // Install Ip4 onto ChildHandle
867 //
868 Status = gBS->InstallMultipleProtocolInterfaces (
869 ChildHandle,
870 &gEfiIp4ProtocolGuid,
871 &IpInstance->Ip4Proto,
872 NULL
873 );
874
875 if (EFI_ERROR (Status)) {
876 goto ON_ERROR;
877 }
878
879 IpInstance->Handle = *ChildHandle;
880
881 //
882 // Open the Managed Network protocol BY_CHILD.
883 //
884 Status = gBS->OpenProtocol (
885 IpSb->MnpChildHandle,
886 &gEfiManagedNetworkProtocolGuid,
887 (VOID **) &Mnp,
888 gIp4DriverBinding.DriverBindingHandle,
889 IpInstance->Handle,
890 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
891 );
892 if (EFI_ERROR (Status)) {
893 gBS->UninstallMultipleProtocolInterfaces (
894 ChildHandle,
895 &gEfiIp4ProtocolGuid,
896 &IpInstance->Ip4Proto,
897 NULL
898 );
899
900 goto ON_ERROR;
901 }
902
903 //
904 // Insert it into the service binding instance.
905 //
906 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
907
908 InsertTailList (&IpSb->Children, &IpInstance->Link);
909 IpSb->NumChildren++;
910
911 gBS->RestoreTPL (OldTpl);
912
913 ON_ERROR:
914
915 if (EFI_ERROR (Status)) {
916
917 Ip4CleanProtocol (IpInstance);
918
919 FreePool (IpInstance);
920 }
921
922 return Status;
923 }
924
925
926 /**
927 Destroys a child handle with a protocol installed on it.
928
929 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
930 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
931 last protocol on ChildHandle, then ChildHandle is destroyed.
932
933 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
934 @param ChildHandle Handle of the child to destroy
935
936 @retval EFI_SUCCES The protocol was removed from ChildHandle.
937 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
938 @retval EFI_INVALID_PARAMETER Child handle is NULL.
939 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
940 because its services are being used.
941 @retval other The child handle was not destroyed
942
943 **/
944 EFI_STATUS
945 EFIAPI
946 Ip4ServiceBindingDestroyChild (
947 IN EFI_SERVICE_BINDING_PROTOCOL *This,
948 IN EFI_HANDLE ChildHandle
949 )
950 {
951 EFI_STATUS Status;
952 IP4_SERVICE *IpSb;
953 IP4_PROTOCOL *IpInstance;
954 EFI_IP4_PROTOCOL *Ip4;
955 EFI_TPL OldTpl;
956
957 if ((This == NULL) || (ChildHandle == NULL)) {
958 return EFI_INVALID_PARAMETER;
959 }
960
961 //
962 // Retrieve the private context data structures
963 //
964 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
965
966 Status = gBS->OpenProtocol (
967 ChildHandle,
968 &gEfiIp4ProtocolGuid,
969 (VOID **) &Ip4,
970 gIp4DriverBinding.DriverBindingHandle,
971 ChildHandle,
972 EFI_OPEN_PROTOCOL_GET_PROTOCOL
973 );
974
975 if (EFI_ERROR (Status)) {
976 return EFI_UNSUPPORTED;
977 }
978
979 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);
980
981 if (IpInstance->Service != IpSb) {
982 return EFI_INVALID_PARAMETER;
983 }
984
985 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
986
987 //
988 // A child can be destroyed more than once. For example,
989 // Ip4DriverBindingStop will destroy all of its children.
990 // when UDP driver is being stopped, it will destroy all
991 // the IP child it opens.
992 //
993 if (IpInstance->InDestroy) {
994 gBS->RestoreTPL (OldTpl);
995 return EFI_SUCCESS;
996 }
997
998 IpInstance->InDestroy = TRUE;
999
1000 //
1001 // Close the Managed Network protocol.
1002 //
1003 gBS->CloseProtocol (
1004 IpSb->MnpChildHandle,
1005 &gEfiManagedNetworkProtocolGuid,
1006 gIp4DriverBinding.DriverBindingHandle,
1007 ChildHandle
1008 );
1009
1010 if (IpInstance->Interface != NULL && IpInstance->Interface->Arp != NULL) {
1011 gBS->CloseProtocol (
1012 IpInstance->Interface->ArpHandle,
1013 &gEfiArpProtocolGuid,
1014 gIp4DriverBinding.DriverBindingHandle,
1015 ChildHandle
1016 );
1017 }
1018
1019 //
1020 // Uninstall the IP4 protocol first. Many thing happens during
1021 // this:
1022 // 1. The consumer of the IP4 protocol will be stopped if it
1023 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
1024 // stopped, IP driver's stop function will be called, and uninstall
1025 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
1026 // makes it possible to create the network stack bottom up, and
1027 // stop it top down.
1028 // 2. the upper layer will recycle the received packet. The recycle
1029 // event's TPL is higher than this function. The recycle events
1030 // will be called back before preceeding. If any packets not recycled,
1031 // that means there is a resource leak.
1032 //
1033 gBS->RestoreTPL (OldTpl);
1034 Status = gBS->UninstallProtocolInterface (
1035 ChildHandle,
1036 &gEfiIp4ProtocolGuid,
1037 &IpInstance->Ip4Proto
1038 );
1039 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1040 if (EFI_ERROR (Status)) {
1041 IpInstance->InDestroy = FALSE;
1042 goto ON_ERROR;
1043 }
1044
1045 Status = Ip4CleanProtocol (IpInstance);
1046 if (EFI_ERROR (Status)) {
1047 gBS->InstallMultipleProtocolInterfaces (
1048 &ChildHandle,
1049 &gEfiIp4ProtocolGuid,
1050 Ip4,
1051 NULL
1052 );
1053
1054 goto ON_ERROR;
1055 }
1056
1057 RemoveEntryList (&IpInstance->Link);
1058 IpSb->NumChildren--;
1059
1060 gBS->RestoreTPL (OldTpl);
1061
1062 FreePool (IpInstance);
1063 return EFI_SUCCESS;
1064
1065 ON_ERROR:
1066 gBS->RestoreTPL (OldTpl);
1067
1068 return Status;
1069 }