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