]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Driver.c
NetworkPkg: Apply uncrustify changes
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Driver.c
1 /** @file
2 The driver binding and service binding protocol for IP6 driver.
3
4 Copyright (c) 2009 - 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 "Ip6Impl.h"
12
13 EFI_DRIVER_BINDING_PROTOCOL gIp6DriverBinding = {
14 Ip6DriverBindingSupported,
15 Ip6DriverBindingStart,
16 Ip6DriverBindingStop,
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 IP6 driver which installs 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 Ip6DriverEntryPoint (
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 &gIp6DriverBinding,
91 ImageHandle,
92 &gIp6ComponentName,
93 &gIp6ComponentName2
94 );
95 }
96
97 /**
98 Test to see if this driver supports ControllerHandle.
99
100 @param[in] This Protocol instance pointer.
101 @param[in] ControllerHandle Handle of device to test.
102 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
103 device to start.
104
105 @retval EFI_SUCCESS This driver supports this device.
106 @retval EFI_ALREADY_STARTED This driver is already running on this device.
107 @retval other This driver does not support this device.
108
109 **/
110 EFI_STATUS
111 EFIAPI
112 Ip6DriverBindingSupported (
113 IN EFI_DRIVER_BINDING_PROTOCOL *This,
114 IN EFI_HANDLE ControllerHandle,
115 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
116 )
117 {
118 //
119 // Test for the MNP service binding Protocol
120 //
121 return gBS->OpenProtocol (
122 ControllerHandle,
123 &gEfiManagedNetworkServiceBindingProtocolGuid,
124 NULL,
125 This->DriverBindingHandle,
126 ControllerHandle,
127 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
128 );
129 }
130
131 /**
132 Clean up an IP6 service binding instance. It releases all
133 the resource allocated by the instance. The instance may be
134 partly initialized, or partly destroyed. If a resource is
135 destroyed, it is marked as that in case the destroy failed and
136 being called again later.
137
138 @param[in] IpSb The IP6 service binding instance to clean up.
139
140 @retval EFI_SUCCESS The resource used by the instance are cleaned up.
141 @retval Others Failed to clean up some of the resources.
142
143 **/
144 EFI_STATUS
145 Ip6CleanService (
146 IN IP6_SERVICE *IpSb
147 )
148 {
149 EFI_STATUS Status;
150 EFI_IPv6_ADDRESS AllNodes;
151 IP6_NEIGHBOR_ENTRY *NeighborCache;
152
153 IpSb->State = IP6_SERVICE_DESTROY;
154
155 if (IpSb->Timer != NULL) {
156 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
157 gBS->CloseEvent (IpSb->Timer);
158
159 IpSb->Timer = NULL;
160 }
161
162 if (IpSb->FasterTimer != NULL) {
163 gBS->SetTimer (IpSb->FasterTimer, TimerCancel, 0);
164 gBS->CloseEvent (IpSb->FasterTimer);
165
166 IpSb->FasterTimer = NULL;
167 }
168
169 Ip6ConfigCleanInstance (&IpSb->Ip6ConfigInstance);
170
171 if (!IpSb->LinkLocalDadFail) {
172 //
173 // Leave link-scope all-nodes multicast address (FF02::1)
174 //
175 Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &AllNodes);
176
177 Status = Ip6LeaveGroup (IpSb, &AllNodes);
178 if (EFI_ERROR (Status)) {
179 return Status;
180 }
181 }
182
183 if (IpSb->DefaultInterface != NULL) {
184 Ip6CleanInterface (IpSb->DefaultInterface, NULL);
185 IpSb->DefaultInterface = NULL;
186 }
187
188 Ip6CleanDefaultRouterList (IpSb);
189
190 Ip6CleanPrefixListTable (IpSb, &IpSb->OnlinkPrefix);
191 Ip6CleanPrefixListTable (IpSb, &IpSb->AutonomousPrefix);
192
193 if (IpSb->RouteTable != NULL) {
194 Ip6CleanRouteTable (IpSb->RouteTable);
195 IpSb->RouteTable = NULL;
196 }
197
198 if (IpSb->InterfaceId != NULL) {
199 FreePool (IpSb->InterfaceId);
200 }
201
202 IpSb->InterfaceId = NULL;
203
204 Ip6CleanAssembleTable (&IpSb->Assemble);
205
206 if (IpSb->MnpChildHandle != NULL) {
207 if (IpSb->Mnp != NULL) {
208 IpSb->Mnp->Cancel (IpSb->Mnp, NULL);
209 IpSb->Mnp->Configure (IpSb->Mnp, NULL);
210 gBS->CloseProtocol (
211 IpSb->MnpChildHandle,
212 &gEfiManagedNetworkProtocolGuid,
213 IpSb->Image,
214 IpSb->Controller
215 );
216
217 IpSb->Mnp = NULL;
218 }
219
220 NetLibDestroyServiceChild (
221 IpSb->Controller,
222 IpSb->Image,
223 &gEfiManagedNetworkServiceBindingProtocolGuid,
224 IpSb->MnpChildHandle
225 );
226
227 IpSb->MnpChildHandle = NULL;
228 }
229
230 if (IpSb->RecvRequest.MnpToken.Event != NULL) {
231 gBS->CloseEvent (IpSb->RecvRequest.MnpToken.Event);
232 }
233
234 //
235 // Free the Neighbor Discovery resources
236 //
237 while (!IsListEmpty (&IpSb->NeighborTable)) {
238 NeighborCache = NET_LIST_HEAD (&IpSb->NeighborTable, IP6_NEIGHBOR_ENTRY, Link);
239 Ip6FreeNeighborEntry (IpSb, NeighborCache, FALSE, TRUE, EFI_SUCCESS, NULL, NULL);
240 }
241
242 return EFI_SUCCESS;
243 }
244
245 /**
246 Create a new IP6 driver service binding protocol.
247
248 @param[in] Controller The controller that has MNP service binding
249 installed.
250 @param[in] ImageHandle The IP6 driver's image handle.
251 @param[out] Service The variable to receive the newly created IP6
252 service.
253
254 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
255 @retval EFI_SUCCESS A new IP6 service binding private is created.
256
257 **/
258 EFI_STATUS
259 Ip6CreateService (
260 IN EFI_HANDLE Controller,
261 IN EFI_HANDLE ImageHandle,
262 OUT IP6_SERVICE **Service
263 )
264 {
265 IP6_SERVICE *IpSb;
266 EFI_STATUS Status;
267 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken;
268 EFI_MANAGED_NETWORK_CONFIG_DATA *Config;
269
270 ASSERT (Service != NULL);
271
272 *Service = NULL;
273
274 //
275 // allocate a service private data then initialize all the filed to
276 // empty resources, so if any thing goes wrong when allocating
277 // resources, Ip6CleanService can be called to clean it up.
278 //
279 IpSb = AllocateZeroPool (sizeof (IP6_SERVICE));
280
281 if (IpSb == NULL) {
282 return EFI_OUT_OF_RESOURCES;
283 }
284
285 IpSb->Signature = IP6_SERVICE_SIGNATURE;
286 IpSb->ServiceBinding.CreateChild = Ip6ServiceBindingCreateChild;
287 IpSb->ServiceBinding.DestroyChild = Ip6ServiceBindingDestroyChild;
288 IpSb->State = IP6_SERVICE_UNSTARTED;
289
290 IpSb->NumChildren = 0;
291 InitializeListHead (&IpSb->Children);
292
293 InitializeListHead (&IpSb->Interfaces);
294 IpSb->DefaultInterface = NULL;
295 IpSb->RouteTable = NULL;
296
297 IpSb->RecvRequest.Signature = IP6_LINK_RX_SIGNATURE;
298 IpSb->RecvRequest.CallBack = NULL;
299 IpSb->RecvRequest.Context = NULL;
300 MnpToken = &IpSb->RecvRequest.MnpToken;
301 MnpToken->Event = NULL;
302 MnpToken->Status = EFI_NOT_READY;
303 MnpToken->Packet.RxData = NULL;
304
305 Ip6CreateAssembleTable (&IpSb->Assemble);
306
307 IpSb->MldCtrl.Mldv1QuerySeen = 0;
308 InitializeListHead (&IpSb->MldCtrl.Groups);
309
310 ZeroMem (&IpSb->LinkLocalAddr, sizeof (EFI_IPv6_ADDRESS));
311 IpSb->LinkLocalOk = FALSE;
312 IpSb->LinkLocalDadFail = FALSE;
313 IpSb->Dhcp6NeedStart = FALSE;
314 IpSb->Dhcp6NeedInfoRequest = FALSE;
315
316 IpSb->CurHopLimit = IP6_HOP_LIMIT;
317 IpSb->LinkMTU = IP6_MIN_LINK_MTU;
318 IpSb->BaseReachableTime = IP6_REACHABLE_TIME;
319 Ip6UpdateReachableTime (IpSb);
320 //
321 // RFC4861 RETRANS_TIMER: 1,000 milliseconds
322 //
323 IpSb->RetransTimer = IP6_RETRANS_TIMER;
324
325 IpSb->RoundRobin = 0;
326
327 InitializeListHead (&IpSb->NeighborTable);
328 InitializeListHead (&IpSb->DefaultRouterList);
329 InitializeListHead (&IpSb->OnlinkPrefix);
330 InitializeListHead (&IpSb->AutonomousPrefix);
331
332 IpSb->InterfaceIdLen = IP6_IF_ID_LEN;
333 IpSb->InterfaceId = NULL;
334
335 IpSb->RouterAdvertiseReceived = FALSE;
336 IpSb->SolicitTimer = IP6_MAX_RTR_SOLICITATIONS;
337 IpSb->Ticks = 0;
338
339 IpSb->Image = ImageHandle;
340 IpSb->Controller = Controller;
341
342 IpSb->MnpChildHandle = NULL;
343 IpSb->Mnp = NULL;
344
345 Config = &IpSb->MnpConfigData;
346 Config->ReceivedQueueTimeoutValue = 0;
347 Config->TransmitQueueTimeoutValue = 0;
348 Config->ProtocolTypeFilter = IP6_ETHER_PROTO;
349 Config->EnableUnicastReceive = TRUE;
350 Config->EnableMulticastReceive = TRUE;
351 Config->EnableBroadcastReceive = TRUE;
352 Config->EnablePromiscuousReceive = FALSE;
353 Config->FlushQueuesOnReset = TRUE;
354 Config->EnableReceiveTimestamps = FALSE;
355 Config->DisableBackgroundPolling = FALSE;
356
357 ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE));
358
359 IpSb->Timer = NULL;
360 IpSb->FasterTimer = NULL;
361
362 ZeroMem (&IpSb->Ip6ConfigInstance, sizeof (IP6_CONFIG_INSTANCE));
363
364 IpSb->MacString = NULL;
365
366 //
367 // Create various resources. First create the route table, timer
368 // event, MNP token event and MNP child.
369 //
370
371 IpSb->RouteTable = Ip6CreateRouteTable ();
372 if (IpSb->RouteTable == NULL) {
373 Status = EFI_OUT_OF_RESOURCES;
374 goto ON_ERROR;
375 }
376
377 Status = gBS->CreateEvent (
378 EVT_NOTIFY_SIGNAL | EVT_TIMER,
379 TPL_CALLBACK,
380 Ip6TimerTicking,
381 IpSb,
382 &IpSb->Timer
383 );
384 if (EFI_ERROR (Status)) {
385 goto ON_ERROR;
386 }
387
388 Status = gBS->CreateEvent (
389 EVT_NOTIFY_SIGNAL | EVT_TIMER,
390 TPL_CALLBACK,
391 Ip6NdFasterTimerTicking,
392 IpSb,
393 &IpSb->FasterTimer
394 );
395 if (EFI_ERROR (Status)) {
396 goto ON_ERROR;
397 }
398
399 Status = NetLibCreateServiceChild (
400 Controller,
401 ImageHandle,
402 &gEfiManagedNetworkServiceBindingProtocolGuid,
403 &IpSb->MnpChildHandle
404 );
405 if (EFI_ERROR (Status)) {
406 goto ON_ERROR;
407 }
408
409 Status = gBS->OpenProtocol (
410 IpSb->MnpChildHandle,
411 &gEfiManagedNetworkProtocolGuid,
412 (VOID **)(&IpSb->Mnp),
413 ImageHandle,
414 Controller,
415 EFI_OPEN_PROTOCOL_BY_DRIVER
416 );
417 if (EFI_ERROR (Status)) {
418 goto ON_ERROR;
419 }
420
421 Status = Ip6ServiceConfigMnp (IpSb, TRUE);
422 if (EFI_ERROR (Status)) {
423 goto ON_ERROR;
424 }
425
426 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode);
427 if (EFI_ERROR (Status)) {
428 goto ON_ERROR;
429 }
430
431 IpSb->MaxPacketSize = IP6_MIN_LINK_MTU - sizeof (EFI_IP6_HEADER);
432 if (NetLibGetVlanId (IpSb->Controller) != 0) {
433 //
434 // This is a VLAN device, reduce MTU by VLAN tag length
435 //
436 IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN;
437 }
438
439 IpSb->OldMaxPacketSize = IpSb->MaxPacketSize;
440
441 //
442 // Currently only ETHERNET is supported in IPv6 stack, since
443 // link local address requires an IEEE 802 48-bit MACs for
444 // EUI-64 format interface identifier mapping.
445 //
446 if (IpSb->SnpMode.IfType != NET_IFTYPE_ETHERNET) {
447 Status = EFI_UNSUPPORTED;
448 goto ON_ERROR;
449 }
450
451 Status = Ip6InitMld (IpSb);
452 if (EFI_ERROR (Status)) {
453 goto ON_ERROR;
454 }
455
456 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &IpSb->MacString);
457 if (EFI_ERROR (Status)) {
458 goto ON_ERROR;
459 }
460
461 Status = Ip6ConfigInitInstance (&IpSb->Ip6ConfigInstance);
462 if (EFI_ERROR (Status)) {
463 goto ON_ERROR;
464 }
465
466 IpSb->DefaultInterface = Ip6CreateInterface (IpSb, TRUE);
467 if (IpSb->DefaultInterface == NULL) {
468 Status = EFI_DEVICE_ERROR;
469 goto ON_ERROR;
470 }
471
472 Status = gBS->CreateEvent (
473 EVT_NOTIFY_SIGNAL,
474 TPL_NOTIFY,
475 Ip6OnFrameReceived,
476 &IpSb->RecvRequest,
477 &MnpToken->Event
478 );
479 if (EFI_ERROR (Status)) {
480 goto ON_ERROR;
481 }
482
483 InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);
484
485 *Service = IpSb;
486 return EFI_SUCCESS;
487
488 ON_ERROR:
489 Ip6CleanService (IpSb);
490 FreePool (IpSb);
491 return Status;
492 }
493
494 /**
495 Start this driver on ControllerHandle.
496
497 @param[in] This Protocol instance pointer.
498 @param[in] ControllerHandle Handle of device to bind driver to.
499 @param[in] RemainingDevicePath Optional parameter used to pick a specific child
500 device to start.
501
502 @retval EFI_SUCCESS This driver is added to ControllerHandle.
503 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
504 @retval other This driver does not support this device.
505
506 **/
507 EFI_STATUS
508 EFIAPI
509 Ip6DriverBindingStart (
510 IN EFI_DRIVER_BINDING_PROTOCOL *This,
511 IN EFI_HANDLE ControllerHandle,
512 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
513 )
514 {
515 IP6_SERVICE *IpSb;
516 EFI_STATUS Status;
517 EFI_IP6_CONFIG_PROTOCOL *Ip6Cfg;
518 IP6_CONFIG_DATA_ITEM *DataItem;
519
520 IpSb = NULL;
521 Ip6Cfg = NULL;
522 DataItem = NULL;
523
524 //
525 // Test for the Ip6 service binding protocol
526 //
527 Status = gBS->OpenProtocol (
528 ControllerHandle,
529 &gEfiIp6ServiceBindingProtocolGuid,
530 NULL,
531 This->DriverBindingHandle,
532 ControllerHandle,
533 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
534 );
535
536 if (Status == EFI_SUCCESS) {
537 return EFI_ALREADY_STARTED;
538 }
539
540 Status = Ip6CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);
541
542 if (EFI_ERROR (Status)) {
543 return Status;
544 }
545
546 ASSERT (IpSb != NULL);
547
548 Ip6Cfg = &IpSb->Ip6ConfigInstance.Ip6Config;
549
550 //
551 // Install the Ip6ServiceBinding Protocol onto ControllerHandle
552 //
553 Status = gBS->InstallMultipleProtocolInterfaces (
554 &ControllerHandle,
555 &gEfiIp6ServiceBindingProtocolGuid,
556 &IpSb->ServiceBinding,
557 &gEfiIp6ConfigProtocolGuid,
558 Ip6Cfg,
559 NULL
560 );
561 if (EFI_ERROR (Status)) {
562 goto FREE_SERVICE;
563 }
564
565 //
566 // Read the config data from NV variable again.
567 // The default data can be changed by other drivers.
568 //
569 Status = Ip6ConfigReadConfigData (IpSb->MacString, &IpSb->Ip6ConfigInstance);
570 if (EFI_ERROR (Status)) {
571 goto UNINSTALL_PROTOCOL;
572 }
573
574 //
575 // If there is any default manual address, set it.
576 //
577 DataItem = &IpSb->Ip6ConfigInstance.DataItem[Ip6ConfigDataTypeManualAddress];
578 if (DataItem->Data.Ptr != NULL) {
579 Status = Ip6Cfg->SetData (
580 Ip6Cfg,
581 Ip6ConfigDataTypeManualAddress,
582 DataItem->DataSize,
583 DataItem->Data.Ptr
584 );
585 if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_BAD_BUFFER_SIZE)) {
586 //
587 // Clean the invalid ManualAddress configuration.
588 //
589 Status = Ip6Cfg->SetData (
590 Ip6Cfg,
591 Ip6ConfigDataTypeManualAddress,
592 0,
593 NULL
594 );
595 DEBUG ((DEBUG_WARN, "Ip6DriverBindingStart: Clean the invalid ManualAddress configuration.\n"));
596 }
597 }
598
599 //
600 // If there is any default gateway address, set it.
601 //
602 DataItem = &IpSb->Ip6ConfigInstance.DataItem[Ip6ConfigDataTypeGateway];
603 if (DataItem->Data.Ptr != NULL) {
604 Status = Ip6Cfg->SetData (
605 Ip6Cfg,
606 Ip6ConfigDataTypeGateway,
607 DataItem->DataSize,
608 DataItem->Data.Ptr
609 );
610 if ((Status == EFI_INVALID_PARAMETER) || (Status == EFI_BAD_BUFFER_SIZE)) {
611 //
612 // Clean the invalid Gateway configuration.
613 //
614 Status = Ip6Cfg->SetData (
615 Ip6Cfg,
616 Ip6ConfigDataTypeGateway,
617 0,
618 NULL
619 );
620 DEBUG ((DEBUG_WARN, "Ip6DriverBindingStart: Clean the invalid Gateway configuration.\n"));
621 }
622 }
623
624 //
625 // ready to go: start the receiving and timer
626 //
627 Status = Ip6ReceiveFrame (Ip6AcceptFrame, IpSb);
628 if (EFI_ERROR (Status)) {
629 goto UNINSTALL_PROTOCOL;
630 }
631
632 //
633 // The timer expires every 100 (IP6_TIMER_INTERVAL_IN_MS) milliseconds.
634 //
635 Status = gBS->SetTimer (
636 IpSb->FasterTimer,
637 TimerPeriodic,
638 TICKS_PER_MS * IP6_TIMER_INTERVAL_IN_MS
639 );
640 if (EFI_ERROR (Status)) {
641 goto UNINSTALL_PROTOCOL;
642 }
643
644 //
645 // The timer expires every 1000 (IP6_ONE_SECOND_IN_MS) milliseconds.
646 //
647 Status = gBS->SetTimer (
648 IpSb->Timer,
649 TimerPeriodic,
650 TICKS_PER_MS * IP6_ONE_SECOND_IN_MS
651 );
652 if (EFI_ERROR (Status)) {
653 goto UNINSTALL_PROTOCOL;
654 }
655
656 //
657 // Initialize the IP6 ID
658 //
659 mIp6Id = NET_RANDOM (NetRandomInitSeed ());
660
661 return EFI_SUCCESS;
662
663 UNINSTALL_PROTOCOL:
664 gBS->UninstallMultipleProtocolInterfaces (
665 ControllerHandle,
666 &gEfiIp6ServiceBindingProtocolGuid,
667 &IpSb->ServiceBinding,
668 &gEfiIp6ConfigProtocolGuid,
669 Ip6Cfg,
670 NULL
671 );
672
673 FREE_SERVICE:
674 Ip6CleanService (IpSb);
675 FreePool (IpSb);
676 return Status;
677 }
678
679 /**
680 Callback function which provided by user to remove one node in NetDestroyLinkList process.
681
682 @param[in] Entry The entry to be removed.
683 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
684
685 @retval EFI_SUCCESS The entry has been removed successfully.
686 @retval Others Fail to remove the entry.
687
688 **/
689 EFI_STATUS
690 EFIAPI
691 Ip6DestroyChildEntryInHandleBuffer (
692 IN LIST_ENTRY *Entry,
693 IN VOID *Context
694 )
695 {
696 IP6_PROTOCOL *IpInstance;
697 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
698 UINTN NumberOfChildren;
699 EFI_HANDLE *ChildHandleBuffer;
700
701 if ((Entry == NULL) || (Context == NULL)) {
702 return EFI_INVALID_PARAMETER;
703 }
704
705 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
706 ServiceBinding = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ServiceBinding;
707 NumberOfChildren = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->NumberOfChildren;
708 ChildHandleBuffer = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ChildHandleBuffer;
709
710 if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) {
711 return EFI_SUCCESS;
712 }
713
714 return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
715 }
716
717 /**
718 Stop this driver on ControllerHandle.
719
720 @param[in] This Protocol instance pointer.
721 @param[in] ControllerHandle Handle of device to stop driver on.
722 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
723 of children is zero, stop the entire bus driver.
724 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
725 if NumberOfChildren is 0.
726
727 @retval EFI_SUCCESS The device was stopped.
728 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
729
730 **/
731 EFI_STATUS
732 EFIAPI
733 Ip6DriverBindingStop (
734 IN EFI_DRIVER_BINDING_PROTOCOL *This,
735 IN EFI_HANDLE ControllerHandle,
736 IN UINTN NumberOfChildren,
737 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
738 )
739 {
740 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
741 IP6_SERVICE *IpSb;
742 EFI_HANDLE NicHandle;
743 EFI_STATUS Status;
744 LIST_ENTRY *List;
745 INTN State;
746 BOOLEAN IsDhcp6;
747 IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
748
749 IsDhcp6 = FALSE;
750 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
751 if (NicHandle == NULL) {
752 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp6ProtocolGuid);
753 if (NicHandle != NULL) {
754 IsDhcp6 = TRUE;
755 } else {
756 return EFI_SUCCESS;
757 }
758 }
759
760 Status = gBS->OpenProtocol (
761 NicHandle,
762 &gEfiIp6ServiceBindingProtocolGuid,
763 (VOID **)&ServiceBinding,
764 This->DriverBindingHandle,
765 NicHandle,
766 EFI_OPEN_PROTOCOL_GET_PROTOCOL
767 );
768 if (EFI_ERROR (Status)) {
769 return EFI_DEVICE_ERROR;
770 }
771
772 IpSb = IP6_SERVICE_FROM_PROTOCOL (ServiceBinding);
773
774 if (IsDhcp6) {
775 Status = Ip6ConfigDestroyDhcp6 (&IpSb->Ip6ConfigInstance);
776 gBS->CloseEvent (IpSb->Ip6ConfigInstance.Dhcp6Event);
777 IpSb->Ip6ConfigInstance.Dhcp6Event = NULL;
778 } else if (NumberOfChildren != 0) {
779 //
780 // NumberOfChildren is not zero, destroy the IP6 children instances in ChildHandleBuffer.
781 //
782 List = &IpSb->Children;
783 Context.ServiceBinding = ServiceBinding;
784 Context.NumberOfChildren = NumberOfChildren;
785 Context.ChildHandleBuffer = ChildHandleBuffer;
786 Status = NetDestroyLinkList (
787 List,
788 Ip6DestroyChildEntryInHandleBuffer,
789 &Context,
790 NULL
791 );
792 } else if (IsListEmpty (&IpSb->Children)) {
793 State = IpSb->State;
794 Status = Ip6CleanService (IpSb);
795 if (EFI_ERROR (Status)) {
796 IpSb->State = State;
797 goto Exit;
798 }
799
800 Status = gBS->UninstallMultipleProtocolInterfaces (
801 NicHandle,
802 &gEfiIp6ServiceBindingProtocolGuid,
803 ServiceBinding,
804 &gEfiIp6ConfigProtocolGuid,
805 &IpSb->Ip6ConfigInstance.Ip6Config,
806 NULL
807 );
808 ASSERT_EFI_ERROR (Status);
809 FreePool (IpSb);
810 Status = EFI_SUCCESS;
811 }
812
813 Exit:
814 return Status;
815 }
816
817 /**
818 Creates a child handle with a set of I/O services.
819
820 @param[in] This Protocol instance pointer.
821 @param[in] ChildHandle Pointer to the handle of the child to create. If
822 it is NULL, then a new handle is created. If it
823 is not NULL, then the I/O services are added to
824 the existing child handle.
825
826 @retval EFI_SUCCESS The child handle was created with the I/O services.
827 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
828 the child.
829 @retval other The child handle was not created.
830
831 **/
832 EFI_STATUS
833 EFIAPI
834 Ip6ServiceBindingCreateChild (
835 IN EFI_SERVICE_BINDING_PROTOCOL *This,
836 IN EFI_HANDLE *ChildHandle
837 )
838 {
839 IP6_SERVICE *IpSb;
840 IP6_PROTOCOL *IpInstance;
841 EFI_TPL OldTpl;
842 EFI_STATUS Status;
843 VOID *Mnp;
844
845 if ((This == NULL) || (ChildHandle == NULL)) {
846 return EFI_INVALID_PARAMETER;
847 }
848
849 IpSb = IP6_SERVICE_FROM_PROTOCOL (This);
850
851 if (IpSb->LinkLocalDadFail) {
852 return EFI_DEVICE_ERROR;
853 }
854
855 IpInstance = AllocatePool (sizeof (IP6_PROTOCOL));
856
857 if (IpInstance == NULL) {
858 return EFI_OUT_OF_RESOURCES;
859 }
860
861 Ip6InitProtocol (IpSb, IpInstance);
862
863 //
864 // Install Ip6 onto ChildHandle
865 //
866 Status = gBS->InstallMultipleProtocolInterfaces (
867 ChildHandle,
868 &gEfiIp6ProtocolGuid,
869 &IpInstance->Ip6Proto,
870 NULL
871 );
872 if (EFI_ERROR (Status)) {
873 goto ON_ERROR;
874 }
875
876 IpInstance->Handle = *ChildHandle;
877
878 //
879 // Open the Managed Network protocol BY_CHILD.
880 //
881 Status = gBS->OpenProtocol (
882 IpSb->MnpChildHandle,
883 &gEfiManagedNetworkProtocolGuid,
884 (VOID **)&Mnp,
885 gIp6DriverBinding.DriverBindingHandle,
886 IpInstance->Handle,
887 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
888 );
889 if (EFI_ERROR (Status)) {
890 gBS->UninstallMultipleProtocolInterfaces (
891 *ChildHandle,
892 &gEfiIp6ProtocolGuid,
893 &IpInstance->Ip6Proto,
894 NULL
895 );
896
897 goto ON_ERROR;
898 }
899
900 //
901 // Insert it into the service binding instance.
902 //
903 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
904
905 InsertTailList (&IpSb->Children, &IpInstance->Link);
906 IpSb->NumChildren++;
907
908 gBS->RestoreTPL (OldTpl);
909
910 ON_ERROR:
911
912 if (EFI_ERROR (Status)) {
913 Ip6CleanProtocol (IpInstance);
914
915 FreePool (IpInstance);
916 }
917
918 return Status;
919 }
920
921 /**
922 Destroys a child handle with a set of I/O services.
923
924 @param[in] This Protocol instance pointer.
925 @param[in] ChildHandle Handle of the child to destroy.
926
927 @retval EFI_SUCCESS The I/O services were removed from the child
928 handle.
929 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
930 that are being removed.
931 @retval EFI_INVALID_PARAMETER Child handle is NULL.
932 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
933 its I/O services are being used.
934 @retval other The child handle was not destroyed.
935
936 **/
937 EFI_STATUS
938 EFIAPI
939 Ip6ServiceBindingDestroyChild (
940 IN EFI_SERVICE_BINDING_PROTOCOL *This,
941 IN EFI_HANDLE ChildHandle
942 )
943 {
944 EFI_STATUS Status;
945 IP6_SERVICE *IpSb;
946 IP6_PROTOCOL *IpInstance;
947 EFI_IP6_PROTOCOL *Ip6;
948 EFI_TPL OldTpl;
949
950 if ((This == NULL) || (ChildHandle == NULL)) {
951 return EFI_INVALID_PARAMETER;
952 }
953
954 //
955 // Retrieve the private context data structures
956 //
957 IpSb = IP6_SERVICE_FROM_PROTOCOL (This);
958
959 Status = gBS->OpenProtocol (
960 ChildHandle,
961 &gEfiIp6ProtocolGuid,
962 (VOID **)&Ip6,
963 gIp6DriverBinding.DriverBindingHandle,
964 ChildHandle,
965 EFI_OPEN_PROTOCOL_GET_PROTOCOL
966 );
967
968 if (EFI_ERROR (Status)) {
969 return EFI_UNSUPPORTED;
970 }
971
972 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (Ip6);
973
974 if (IpInstance->Service != IpSb) {
975 return EFI_INVALID_PARAMETER;
976 }
977
978 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
979
980 //
981 // A child can be destroyed more than once. For example,
982 // Ip6DriverBindingStop will destroy all of its children.
983 // when UDP driver is being stopped, it will destroy all
984 // the IP child it opens.
985 //
986 if (IpInstance->InDestroy) {
987 gBS->RestoreTPL (OldTpl);
988 return EFI_SUCCESS;
989 }
990
991 IpInstance->InDestroy = TRUE;
992
993 //
994 // Close the Managed Network protocol.
995 //
996 gBS->CloseProtocol (
997 IpSb->MnpChildHandle,
998 &gEfiManagedNetworkProtocolGuid,
999 gIp6DriverBinding.DriverBindingHandle,
1000 ChildHandle
1001 );
1002
1003 //
1004 // Uninstall the IP6 protocol first. Many thing happens during
1005 // this:
1006 // 1. The consumer of the IP6 protocol will be stopped if it
1007 // opens the protocol BY_DRIVER. For example, if MNP driver is
1008 // stopped, IP driver's stop function will be called, and uninstall
1009 // EFI_IP6_PROTOCOL will trigger the UDP's stop function. This
1010 // makes it possible to create the network stack bottom up, and
1011 // stop it top down.
1012 // 2. the upper layer will recycle the received packet. The recycle
1013 // event's TPL is higher than this function. The recycle events
1014 // will be called back before preceding. If any packets not recycled,
1015 // that means there is a resource leak.
1016 //
1017 gBS->RestoreTPL (OldTpl);
1018 Status = gBS->UninstallProtocolInterface (
1019 ChildHandle,
1020 &gEfiIp6ProtocolGuid,
1021 &IpInstance->Ip6Proto
1022 );
1023 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1024 if (EFI_ERROR (Status)) {
1025 goto ON_ERROR;
1026 }
1027
1028 Status = Ip6CleanProtocol (IpInstance);
1029 if (EFI_ERROR (Status)) {
1030 gBS->InstallMultipleProtocolInterfaces (
1031 &ChildHandle,
1032 &gEfiIp6ProtocolGuid,
1033 Ip6,
1034 NULL
1035 );
1036
1037 goto ON_ERROR;
1038 }
1039
1040 RemoveEntryList (&IpInstance->Link);
1041 ASSERT (IpSb->NumChildren > 0);
1042 IpSb->NumChildren--;
1043
1044 gBS->RestoreTPL (OldTpl);
1045
1046 FreePool (IpInstance);
1047 return EFI_SUCCESS;
1048
1049 ON_ERROR:
1050 gBS->RestoreTPL (OldTpl);
1051
1052 return Status;
1053 }