]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip4Dxe/Ip4Driver.c
NetworkPkg: Apply uncrustify changes
[mirror_edk2.git] / NetworkPkg / 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 //
41 // Test if protocol was even found.
42 // Notification function will be called at least once.
43 //
44 Status = gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **)&mIpSec);
45 if ((Status == EFI_SUCCESS) && (mIpSec != NULL)) {
46 //
47 // Close the event so it does not get called again.
48 //
49 gBS->CloseEvent (Event);
50
51 mIpSec2Installed = TRUE;
52 }
53 }
54
55 /**
56 This is the declaration of an EFI image entry point. This entry point is
57 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
58 both device drivers and bus drivers.
59
60 The entry point for IP4 driver which install the driver
61 binding and component name protocol on its image.
62
63 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
64 @param[in] SystemTable A pointer to the EFI System Table.
65
66 @retval EFI_SUCCESS The operation completed successfully.
67 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
68
69 **/
70 EFI_STATUS
71 EFIAPI
72 Ip4DriverEntryPoint (
73 IN EFI_HANDLE ImageHandle,
74 IN EFI_SYSTEM_TABLE *SystemTable
75 )
76 {
77 VOID *Registration;
78
79 EfiCreateProtocolNotifyEvent (
80 &gEfiIpSec2ProtocolGuid,
81 TPL_CALLBACK,
82 IpSec2InstalledCallback,
83 NULL,
84 &Registration
85 );
86
87 return EfiLibInstallDriverBindingComponentName2 (
88 ImageHandle,
89 SystemTable,
90 &gIp4DriverBinding,
91 ImageHandle,
92 &gIp4ComponentName,
93 &gIp4ComponentName2
94 );
95 }
96
97 /**
98 Test to see if this driver supports ControllerHandle. This service
99 is called by the EFI boot service ConnectController(). In
100 order to make drivers as small as possible, there are a few calling
101 restrictions for this service. ConnectController() must
102 follow these calling restrictions. If any other agent wishes to call
103 Supported() it must also follow these calling restrictions.
104
105 @param[in] This Protocol instance pointer.
106 @param[in] ControllerHandle Handle of device to test
107 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
108 device to start.
109
110 @retval EFI_SUCCESS This driver supports this device
111 @retval EFI_ALREADY_STARTED This driver is already running on this device
112 @retval other This driver does not support this device
113
114 **/
115 EFI_STATUS
116 EFIAPI
117 Ip4DriverBindingSupported (
118 IN EFI_DRIVER_BINDING_PROTOCOL *This,
119 IN EFI_HANDLE ControllerHandle,
120 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
121 )
122 {
123 EFI_STATUS Status;
124
125 //
126 // Test for the MNP service binding Protocol
127 //
128 Status = gBS->OpenProtocol (
129 ControllerHandle,
130 &gEfiManagedNetworkServiceBindingProtocolGuid,
131 NULL,
132 This->DriverBindingHandle,
133 ControllerHandle,
134 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
135 );
136
137 if (EFI_ERROR (Status)) {
138 return Status;
139 }
140
141 //
142 // Test for the Arp service binding Protocol
143 //
144 Status = gBS->OpenProtocol (
145 ControllerHandle,
146 &gEfiArpServiceBindingProtocolGuid,
147 NULL,
148 This->DriverBindingHandle,
149 ControllerHandle,
150 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
151 );
152
153 return Status;
154 }
155
156 /**
157 Clean up a IP4 service binding instance. It will release all
158 the resource allocated by the instance. The instance may be
159 partly initialized, or partly destroyed. If a resource is
160 destroyed, it is marked as that in case the destroy failed and
161 being called again later.
162
163 @param[in] IpSb The IP4 service binding instance to clean up
164
165 @retval EFI_SUCCESS The resource used by the instance are cleaned up
166 @retval other Failed to clean up some of the resources.
167
168 **/
169 EFI_STATUS
170 Ip4CleanService (
171 IN IP4_SERVICE *IpSb
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
379 IpSb->OldMaxPacketSize = IpSb->MaxPacketSize;
380 *Service = IpSb;
381
382 return EFI_SUCCESS;
383
384 ON_ERROR:
385 Ip4CleanService (IpSb);
386 FreePool (IpSb);
387
388 return Status;
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 ControllerHandle
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 Stop this driver on ControllerHandle. This service is called by the
678 EFI boot service DisconnectController(). In order to
679 make drivers as small as possible, there are a few calling
680 restrictions for this service. DisconnectController()
681 must follow these calling restrictions. If any other agent wishes
682 to call Stop() it must also follow these calling restrictions.
683
684 @param[in] This Protocol instance pointer.
685 @param[in] ControllerHandle Handle of device to stop driver on
686 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
687 of children is zero stop the entire bus driver.
688 @param[in] ChildHandleBuffer List of Child Handles to Stop.
689
690 @retval EFI_SUCCESS This driver is removed ControllerHandle
691 @retval other This driver was not removed from this device
692
693 **/
694 EFI_STATUS
695 EFIAPI
696 Ip4DriverBindingStop (
697 IN EFI_DRIVER_BINDING_PROTOCOL *This,
698 IN EFI_HANDLE ControllerHandle,
699 IN UINTN NumberOfChildren,
700 IN EFI_HANDLE *ChildHandleBuffer
701 )
702 {
703 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
704 IP4_SERVICE *IpSb;
705 EFI_HANDLE NicHandle;
706 EFI_STATUS Status;
707 INTN State;
708 LIST_ENTRY *List;
709 IP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
710 IP4_INTERFACE *IpIf;
711 IP4_ROUTE_TABLE *RouteTable;
712
713 BOOLEAN IsDhcp4;
714
715 IsDhcp4 = FALSE;
716
717 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
718 if (NicHandle == NULL) {
719 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiArpProtocolGuid);
720 if (NicHandle == NULL) {
721 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp4ProtocolGuid);
722 if (NicHandle != NULL) {
723 IsDhcp4 = TRUE;
724 } else {
725 return EFI_SUCCESS;
726 }
727 }
728 }
729
730 Status = gBS->OpenProtocol (
731 NicHandle,
732 &gEfiIp4ServiceBindingProtocolGuid,
733 (VOID **)&ServiceBinding,
734 This->DriverBindingHandle,
735 NicHandle,
736 EFI_OPEN_PROTOCOL_GET_PROTOCOL
737 );
738 if (EFI_ERROR (Status)) {
739 return EFI_DEVICE_ERROR;
740 }
741
742 IpSb = IP4_SERVICE_FROM_PROTOCOL (ServiceBinding);
743
744 if (IsDhcp4) {
745 Status = Ip4Config2DestroyDhcp4 (&IpSb->Ip4Config2Instance);
746 gBS->CloseEvent (IpSb->Ip4Config2Instance.Dhcp4Event);
747 IpSb->Ip4Config2Instance.Dhcp4Event = NULL;
748 } else if (NumberOfChildren != 0) {
749 List = &IpSb->Children;
750 Context.ServiceBinding = ServiceBinding;
751 Context.NumberOfChildren = NumberOfChildren;
752 Context.ChildHandleBuffer = ChildHandleBuffer;
753 Status = NetDestroyLinkList (
754 List,
755 Ip4DestroyChildEntryInHandleBuffer,
756 &Context,
757 NULL
758 );
759 } else if (IpSb->DefaultInterface->ArpHandle == ControllerHandle) {
760 //
761 // The ARP protocol for the default interface is being uninstalled and all
762 // its IP child handles should have been destroyed before. So, release the
763 // default interface and route table, create a new one and mark it as not started.
764 //
765 Ip4CancelReceive (IpSb->DefaultInterface);
766 Ip4FreeInterface (IpSb->DefaultInterface, NULL);
767 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
768
769 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
770 if (IpIf == NULL) {
771 goto ON_ERROR;
772 }
773
774 RouteTable = Ip4CreateRouteTable ();
775 if (RouteTable == NULL) {
776 Ip4FreeInterface (IpIf, NULL);
777 goto ON_ERROR;
778 }
779
780 IpSb->DefaultInterface = IpIf;
781 InsertHeadList (&IpSb->Interfaces, &IpIf->Link);
782 IpSb->DefaultRouteTable = RouteTable;
783 Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);
784
785 IpSb->State = IP4_SERVICE_UNSTARTED;
786 } else if (IsListEmpty (&IpSb->Children)) {
787 State = IpSb->State;
788 //
789 // OK, clean other resources then uninstall the service binding protocol.
790 //
791 Status = Ip4CleanService (IpSb);
792 if (EFI_ERROR (Status)) {
793 IpSb->State = State;
794 goto ON_ERROR;
795 }
796
797 gBS->UninstallMultipleProtocolInterfaces (
798 NicHandle,
799 &gEfiIp4ServiceBindingProtocolGuid,
800 ServiceBinding,
801 &gEfiIp4Config2ProtocolGuid,
802 &IpSb->Ip4Config2Instance.Ip4Config2,
803 NULL
804 );
805
806 if (gIp4ControllerNameTable != NULL) {
807 FreeUnicodeStringTable (gIp4ControllerNameTable);
808 gIp4ControllerNameTable = NULL;
809 }
810
811 FreePool (IpSb);
812 }
813
814 ON_ERROR:
815 return Status;
816 }
817
818 /**
819 Creates a child handle and installs a protocol.
820
821 The CreateChild() function installs a protocol on ChildHandle.
822 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
823 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
824
825 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
826 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
827 then a new handle is created. If it is a pointer to an existing UEFI handle,
828 then the protocol is added to the existing UEFI handle.
829
830 @retval EFI_SUCCESS The protocol was added to ChildHandle.
831 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
832 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
833 the child
834 @retval other The child handle was not created
835
836 **/
837 EFI_STATUS
838 EFIAPI
839 Ip4ServiceBindingCreateChild (
840 IN EFI_SERVICE_BINDING_PROTOCOL *This,
841 IN OUT EFI_HANDLE *ChildHandle
842 )
843 {
844 IP4_SERVICE *IpSb;
845 IP4_PROTOCOL *IpInstance;
846 EFI_TPL OldTpl;
847 EFI_STATUS Status;
848 VOID *Mnp;
849
850 if ((This == NULL) || (ChildHandle == NULL)) {
851 return EFI_INVALID_PARAMETER;
852 }
853
854 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
855 IpInstance = AllocatePool (sizeof (IP4_PROTOCOL));
856
857 if (IpInstance == NULL) {
858 return EFI_OUT_OF_RESOURCES;
859 }
860
861 Ip4InitProtocol (IpSb, IpInstance);
862
863 //
864 // Install Ip4 onto ChildHandle
865 //
866 Status = gBS->InstallMultipleProtocolInterfaces (
867 ChildHandle,
868 &gEfiIp4ProtocolGuid,
869 &IpInstance->Ip4Proto,
870 NULL
871 );
872
873 if (EFI_ERROR (Status)) {
874 goto ON_ERROR;
875 }
876
877 IpInstance->Handle = *ChildHandle;
878
879 //
880 // Open the Managed Network protocol BY_CHILD.
881 //
882 Status = gBS->OpenProtocol (
883 IpSb->MnpChildHandle,
884 &gEfiManagedNetworkProtocolGuid,
885 (VOID **)&Mnp,
886 gIp4DriverBinding.DriverBindingHandle,
887 IpInstance->Handle,
888 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
889 );
890 if (EFI_ERROR (Status)) {
891 gBS->UninstallMultipleProtocolInterfaces (
892 *ChildHandle,
893 &gEfiIp4ProtocolGuid,
894 &IpInstance->Ip4Proto,
895 NULL
896 );
897
898 goto ON_ERROR;
899 }
900
901 //
902 // Insert it into the service binding instance.
903 //
904 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
905
906 InsertTailList (&IpSb->Children, &IpInstance->Link);
907 IpSb->NumChildren++;
908
909 gBS->RestoreTPL (OldTpl);
910
911 ON_ERROR:
912
913 if (EFI_ERROR (Status)) {
914 Ip4CleanProtocol (IpInstance);
915
916 FreePool (IpInstance);
917 }
918
919 return Status;
920 }
921
922 /**
923 Destroys a child handle with a protocol installed on it.
924
925 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
926 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
927 last protocol on ChildHandle, then ChildHandle is destroyed.
928
929 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
930 @param ChildHandle Handle of the child to destroy
931
932 @retval EFI_SUCCESS The protocol was removed from ChildHandle.
933 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
934 @retval EFI_INVALID_PARAMETER Child handle is NULL.
935 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
936 because its services are being used.
937 @retval other The child handle was not destroyed
938
939 **/
940 EFI_STATUS
941 EFIAPI
942 Ip4ServiceBindingDestroyChild (
943 IN EFI_SERVICE_BINDING_PROTOCOL *This,
944 IN EFI_HANDLE ChildHandle
945 )
946 {
947 EFI_STATUS Status;
948 IP4_SERVICE *IpSb;
949 IP4_PROTOCOL *IpInstance;
950 EFI_IP4_PROTOCOL *Ip4;
951 EFI_TPL OldTpl;
952
953 if ((This == NULL) || (ChildHandle == NULL)) {
954 return EFI_INVALID_PARAMETER;
955 }
956
957 //
958 // Retrieve the private context data structures
959 //
960 IpSb = IP4_SERVICE_FROM_PROTOCOL (This);
961
962 Status = gBS->OpenProtocol (
963 ChildHandle,
964 &gEfiIp4ProtocolGuid,
965 (VOID **)&Ip4,
966 gIp4DriverBinding.DriverBindingHandle,
967 ChildHandle,
968 EFI_OPEN_PROTOCOL_GET_PROTOCOL
969 );
970
971 if (EFI_ERROR (Status)) {
972 return EFI_UNSUPPORTED;
973 }
974
975 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (Ip4);
976
977 if (IpInstance->Service != IpSb) {
978 return EFI_INVALID_PARAMETER;
979 }
980
981 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
982
983 //
984 // A child can be destroyed more than once. For example,
985 // Ip4DriverBindingStop will destroy all of its children.
986 // when UDP driver is being stopped, it will destroy all
987 // the IP child it opens.
988 //
989 if (IpInstance->InDestroy) {
990 gBS->RestoreTPL (OldTpl);
991 return EFI_SUCCESS;
992 }
993
994 IpInstance->InDestroy = TRUE;
995
996 //
997 // Close the Managed Network protocol.
998 //
999 gBS->CloseProtocol (
1000 IpSb->MnpChildHandle,
1001 &gEfiManagedNetworkProtocolGuid,
1002 gIp4DriverBinding.DriverBindingHandle,
1003 ChildHandle
1004 );
1005
1006 if ((IpInstance->Interface != NULL) && (IpInstance->Interface->Arp != NULL)) {
1007 gBS->CloseProtocol (
1008 IpInstance->Interface->ArpHandle,
1009 &gEfiArpProtocolGuid,
1010 gIp4DriverBinding.DriverBindingHandle,
1011 ChildHandle
1012 );
1013 }
1014
1015 //
1016 // Uninstall the IP4 protocol first. Many thing happens during
1017 // this:
1018 // 1. The consumer of the IP4 protocol will be stopped if it
1019 // opens the protocol BY_DRIVER. For example, if MNP driver is
1020 // stopped, IP driver's stop function will be called, and uninstall
1021 // EFI_IP4_PROTOCOL will trigger the UDP's stop function. This
1022 // makes it possible to create the network stack bottom up, and
1023 // stop it top down.
1024 // 2. the upper layer will recycle the received packet. The recycle
1025 // event's TPL is higher than this function. The recycle events
1026 // will be called back before preceding. If any packets not recycled,
1027 // that means there is a resource leak.
1028 //
1029 gBS->RestoreTPL (OldTpl);
1030 Status = gBS->UninstallProtocolInterface (
1031 ChildHandle,
1032 &gEfiIp4ProtocolGuid,
1033 &IpInstance->Ip4Proto
1034 );
1035 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1036 if (EFI_ERROR (Status)) {
1037 IpInstance->InDestroy = FALSE;
1038 goto ON_ERROR;
1039 }
1040
1041 Status = Ip4CleanProtocol (IpInstance);
1042 if (EFI_ERROR (Status)) {
1043 gBS->InstallMultipleProtocolInterfaces (
1044 &ChildHandle,
1045 &gEfiIp4ProtocolGuid,
1046 Ip4,
1047 NULL
1048 );
1049
1050 goto ON_ERROR;
1051 }
1052
1053 RemoveEntryList (&IpInstance->Link);
1054 IpSb->NumChildren--;
1055
1056 gBS->RestoreTPL (OldTpl);
1057
1058 FreePool (IpInstance);
1059 return EFI_SUCCESS;
1060
1061 ON_ERROR:
1062 gBS->RestoreTPL (OldTpl);
1063
1064 return Status;
1065 }