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