]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Driver.c
4c607125a6ad49a935dfdf9138c0010258ba453b
[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 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 FREE_SERVICE;
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 UNINSTALL_PROTOCOL;
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 UNINSTALL_PROTOCOL;
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 UNINSTALL_PROTOCOL;
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 UNINSTALL_PROTOCOL;
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 UNINSTALL_PROTOCOL;
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 UNINSTALL_PROTOCOL;
641 }
642
643 //
644 // Initialize the IP6 ID
645 //
646 mIp6Id = NET_RANDOM (NetRandomInitSeed ());
647
648 return EFI_SUCCESS;
649
650 UNINSTALL_PROTOCOL:
651 gBS->UninstallMultipleProtocolInterfaces (
652 ControllerHandle,
653 &gEfiIp6ServiceBindingProtocolGuid,
654 &IpSb->ServiceBinding,
655 &gEfiIp6ConfigProtocolGuid,
656 Ip6Cfg,
657 NULL
658 );
659
660 FREE_SERVICE:
661 Ip6CleanService (IpSb);
662 FreePool (IpSb);
663 return Status;
664 }
665
666 /**
667 Callback function which provided by user to remove one node in NetDestroyLinkList process.
668
669 @param[in] Entry The entry to be removed.
670 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
671
672 @retval EFI_SUCCESS The entry has been removed successfully.
673 @retval Others Fail to remove the entry.
674
675 **/
676 EFI_STATUS
677 EFIAPI
678 Ip6DestroyChildEntryInHandleBuffer (
679 IN LIST_ENTRY *Entry,
680 IN VOID *Context
681 )
682 {
683 IP6_PROTOCOL *IpInstance;
684 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
685 UINTN NumberOfChildren;
686 EFI_HANDLE *ChildHandleBuffer;
687
688 if (Entry == NULL || Context == NULL) {
689 return EFI_INVALID_PARAMETER;
690 }
691
692 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
693 ServiceBinding = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding;
694 NumberOfChildren = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren;
695 ChildHandleBuffer = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer;
696
697 if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) {
698 return EFI_SUCCESS;
699 }
700
701 return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
702 }
703
704 /**
705 Stop this driver on ControllerHandle.
706
707 @param[in] This Protocol instance pointer.
708 @param[in] ControllerHandle Handle of device to stop driver on.
709 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
710 of children is zero, stop the entire bus driver.
711 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
712 if NumberOfChildren is 0.
713
714 @retval EFI_SUCCESS The device was stopped.
715 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
716
717 **/
718 EFI_STATUS
719 EFIAPI
720 Ip6DriverBindingStop (
721 IN EFI_DRIVER_BINDING_PROTOCOL *This,
722 IN EFI_HANDLE ControllerHandle,
723 IN UINTN NumberOfChildren,
724 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
725 )
726 {
727 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
728 IP6_SERVICE *IpSb;
729 EFI_HANDLE NicHandle;
730 EFI_STATUS Status;
731 LIST_ENTRY *List;
732 INTN State;
733 BOOLEAN IsDhcp6;
734 IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
735
736 IsDhcp6 = FALSE;
737 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
738 if (NicHandle == NULL) {
739 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp6ProtocolGuid);
740 if (NicHandle != NULL) {
741 IsDhcp6 = TRUE;
742 } else {
743 return EFI_SUCCESS;
744 }
745 }
746
747 Status = gBS->OpenProtocol (
748 NicHandle,
749 &gEfiIp6ServiceBindingProtocolGuid,
750 (VOID **) &ServiceBinding,
751 This->DriverBindingHandle,
752 NicHandle,
753 EFI_OPEN_PROTOCOL_GET_PROTOCOL
754 );
755 if (EFI_ERROR (Status)) {
756 return EFI_DEVICE_ERROR;
757 }
758
759 IpSb = IP6_SERVICE_FROM_PROTOCOL (ServiceBinding);
760
761 if (IsDhcp6) {
762 Status = Ip6ConfigDestroyDhcp6 (&IpSb->Ip6ConfigInstance);
763 gBS->CloseEvent (IpSb->Ip6ConfigInstance.Dhcp6Event);
764 IpSb->Ip6ConfigInstance.Dhcp6Event = NULL;
765 } else if (NumberOfChildren != 0) {
766 //
767 // NumberOfChildren is not zero, destroy the IP6 children instances in ChildHandleBuffer.
768 //
769 List = &IpSb->Children;
770 Context.ServiceBinding = ServiceBinding;
771 Context.NumberOfChildren = NumberOfChildren;
772 Context.ChildHandleBuffer = ChildHandleBuffer;
773 Status = NetDestroyLinkList (
774 List,
775 Ip6DestroyChildEntryInHandleBuffer,
776 &Context,
777 NULL
778 );
779 } else if (IsListEmpty (&IpSb->Children)) {
780 State = IpSb->State;
781 Status = Ip6CleanService (IpSb);
782 if (EFI_ERROR (Status)) {
783 IpSb->State = State;
784 goto Exit;
785 }
786
787 Status = gBS->UninstallMultipleProtocolInterfaces (
788 NicHandle,
789 &gEfiIp6ServiceBindingProtocolGuid,
790 ServiceBinding,
791 &gEfiIp6ConfigProtocolGuid,
792 &IpSb->Ip6ConfigInstance.Ip6Config,
793 NULL
794 );
795 ASSERT_EFI_ERROR (Status);
796 FreePool (IpSb);
797 Status = EFI_SUCCESS;
798 }
799
800 Exit:
801 return Status;
802 }
803
804
805 /**
806 Creates a child handle with a set of I/O services.
807
808 @param[in] This Protocol instance pointer.
809 @param[in] ChildHandle Pointer to the handle of the child to create. If
810 it is NULL, then a new handle is created. If it
811 is not NULL, then the I/O services are added to
812 the existing child handle.
813
814 @retval EFI_SUCCES The child handle was created with the I/O services.
815 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
816 the child.
817 @retval other The child handle was not created.
818
819 **/
820 EFI_STATUS
821 EFIAPI
822 Ip6ServiceBindingCreateChild (
823 IN EFI_SERVICE_BINDING_PROTOCOL *This,
824 IN EFI_HANDLE *ChildHandle
825 )
826 {
827 IP6_SERVICE *IpSb;
828 IP6_PROTOCOL *IpInstance;
829 EFI_TPL OldTpl;
830 EFI_STATUS Status;
831 VOID *Mnp;
832
833 if ((This == NULL) || (ChildHandle == NULL)) {
834 return EFI_INVALID_PARAMETER;
835 }
836
837 IpSb = IP6_SERVICE_FROM_PROTOCOL (This);
838
839 if (IpSb->LinkLocalDadFail) {
840 return EFI_DEVICE_ERROR;
841 }
842
843 IpInstance = AllocatePool (sizeof (IP6_PROTOCOL));
844
845 if (IpInstance == NULL) {
846 return EFI_OUT_OF_RESOURCES;
847 }
848
849 Ip6InitProtocol (IpSb, IpInstance);
850
851 //
852 // Install Ip6 onto ChildHandle
853 //
854 Status = gBS->InstallMultipleProtocolInterfaces (
855 ChildHandle,
856 &gEfiIp6ProtocolGuid,
857 &IpInstance->Ip6Proto,
858 NULL
859 );
860 if (EFI_ERROR (Status)) {
861 goto ON_ERROR;
862 }
863
864 IpInstance->Handle = *ChildHandle;
865
866 //
867 // Open the Managed Network protocol BY_CHILD.
868 //
869 Status = gBS->OpenProtocol (
870 IpSb->MnpChildHandle,
871 &gEfiManagedNetworkProtocolGuid,
872 (VOID **) &Mnp,
873 gIp6DriverBinding.DriverBindingHandle,
874 IpInstance->Handle,
875 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
876 );
877 if (EFI_ERROR (Status)) {
878 gBS->UninstallMultipleProtocolInterfaces (
879 ChildHandle,
880 &gEfiIp6ProtocolGuid,
881 &IpInstance->Ip6Proto,
882 NULL
883 );
884
885 goto ON_ERROR;
886 }
887
888 //
889 // Insert it into the service binding instance.
890 //
891 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
892
893 InsertTailList (&IpSb->Children, &IpInstance->Link);
894 IpSb->NumChildren++;
895
896 gBS->RestoreTPL (OldTpl);
897
898 ON_ERROR:
899
900 if (EFI_ERROR (Status)) {
901
902 Ip6CleanProtocol (IpInstance);
903
904 FreePool (IpInstance);
905 }
906
907 return Status;
908 }
909
910 /**
911 Destroys a child handle with a set of I/O services.
912
913 @param[in] This Protocol instance pointer.
914 @param[in] ChildHandle Handle of the child to destroy.
915
916 @retval EFI_SUCCES The I/O services were removed from the child
917 handle.
918 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
919 that are being removed.
920 @retval EFI_INVALID_PARAMETER Child handle is NULL.
921 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
922 its I/O services are being used.
923 @retval other The child handle was not destroyed.
924
925 **/
926 EFI_STATUS
927 EFIAPI
928 Ip6ServiceBindingDestroyChild (
929 IN EFI_SERVICE_BINDING_PROTOCOL *This,
930 IN EFI_HANDLE ChildHandle
931 )
932 {
933 EFI_STATUS Status;
934 IP6_SERVICE *IpSb;
935 IP6_PROTOCOL *IpInstance;
936 EFI_IP6_PROTOCOL *Ip6;
937 EFI_TPL OldTpl;
938
939 if ((This == NULL) || (ChildHandle == NULL)) {
940 return EFI_INVALID_PARAMETER;
941 }
942
943 //
944 // Retrieve the private context data structures
945 //
946 IpSb = IP6_SERVICE_FROM_PROTOCOL (This);
947
948 Status = gBS->OpenProtocol (
949 ChildHandle,
950 &gEfiIp6ProtocolGuid,
951 (VOID **) &Ip6,
952 gIp6DriverBinding.DriverBindingHandle,
953 ChildHandle,
954 EFI_OPEN_PROTOCOL_GET_PROTOCOL
955 );
956
957 if (EFI_ERROR (Status)) {
958 return EFI_UNSUPPORTED;
959 }
960
961 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (Ip6);
962
963 if (IpInstance->Service != IpSb) {
964 return EFI_INVALID_PARAMETER;
965 }
966
967 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
968
969 //
970 // A child can be destroyed more than once. For example,
971 // Ip6DriverBindingStop will destroy all of its children.
972 // when UDP driver is being stopped, it will destroy all
973 // the IP child it opens.
974 //
975 if (IpInstance->InDestroy) {
976 gBS->RestoreTPL (OldTpl);
977 return EFI_SUCCESS;
978 }
979
980 IpInstance->InDestroy = TRUE;
981
982 //
983 // Close the Managed Network protocol.
984 //
985 gBS->CloseProtocol (
986 IpSb->MnpChildHandle,
987 &gEfiManagedNetworkProtocolGuid,
988 gIp6DriverBinding.DriverBindingHandle,
989 ChildHandle
990 );
991
992 //
993 // Uninstall the IP6 protocol first. Many thing happens during
994 // this:
995 // 1. The consumer of the IP6 protocol will be stopped if it
996 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
997 // stopped, IP driver's stop function will be called, and uninstall
998 // EFI_IP6_PROTOCOL will trigger the UDP's stop function. This
999 // makes it possible to create the network stack bottom up, and
1000 // stop it top down.
1001 // 2. the upper layer will recycle the received packet. The recycle
1002 // event's TPL is higher than this function. The recycle events
1003 // will be called back before preceeding. If any packets not recycled,
1004 // that means there is a resource leak.
1005 //
1006 gBS->RestoreTPL (OldTpl);
1007 Status = gBS->UninstallProtocolInterface (
1008 ChildHandle,
1009 &gEfiIp6ProtocolGuid,
1010 &IpInstance->Ip6Proto
1011 );
1012 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1013 if (EFI_ERROR (Status)) {
1014 goto ON_ERROR;
1015 }
1016
1017 Status = Ip6CleanProtocol (IpInstance);
1018 if (EFI_ERROR (Status)) {
1019 gBS->InstallMultipleProtocolInterfaces (
1020 &ChildHandle,
1021 &gEfiIp6ProtocolGuid,
1022 Ip6,
1023 NULL
1024 );
1025
1026 goto ON_ERROR;
1027 }
1028
1029 RemoveEntryList (&IpInstance->Link);
1030 ASSERT (IpSb->NumChildren > 0);
1031 IpSb->NumChildren--;
1032
1033 gBS->RestoreTPL (OldTpl);
1034
1035 FreePool (IpInstance);
1036 return EFI_SUCCESS;
1037
1038 ON_ERROR:
1039 gBS->RestoreTPL (OldTpl);
1040
1041 return Status;
1042 }