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