]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Driver.c
Fix GCC build fail issue for MdeModulePkg and NetworkPkg.
[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 - 2012, 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 //
416 // The timer expires every 100 (IP6_TIMER_INTERVAL_IN_MS) milliseconds.
417 //
418 Status = gBS->SetTimer (IpSb->FasterTimer, TimerPeriodic, TICKS_PER_MS * IP6_TIMER_INTERVAL_IN_MS);
419 if (EFI_ERROR (Status)) {
420 goto ON_ERROR;
421 }
422
423 //
424 // The timer expires every 1000 (IP6_ONE_SECOND_IN_MS) milliseconds.
425 //
426 Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_MS * IP6_ONE_SECOND_IN_MS);
427 if (EFI_ERROR (Status)) {
428 goto ON_ERROR;
429 }
430
431
432 Status = gBS->CreateEvent (
433 EVT_NOTIFY_SIGNAL,
434 TPL_NOTIFY,
435 Ip6OnFrameReceived,
436 &IpSb->RecvRequest,
437 &MnpToken->Event
438 );
439 if (EFI_ERROR (Status)) {
440 goto ON_ERROR;
441 }
442
443 Status = Ip6ReceiveFrame (Ip6AcceptFrame, IpSb);
444 if (EFI_ERROR (Status)) {
445 goto ON_ERROR;
446 }
447
448 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &IpSb->MacString);
449 if (EFI_ERROR (Status)) {
450 goto ON_ERROR;
451 }
452
453 Status = Ip6ConfigInitInstance (&IpSb->Ip6ConfigInstance);
454 if (EFI_ERROR (Status)) {
455 goto ON_ERROR;
456 }
457
458 IpSb->DefaultInterface = Ip6CreateInterface (IpSb, TRUE);
459 if (IpSb->DefaultInterface == NULL) {
460 Status = EFI_DEVICE_ERROR;
461 goto ON_ERROR;
462 }
463
464 //
465 // If there is any manual address, set it.
466 //
467 DataItem = &IpSb->Ip6ConfigInstance.DataItem[Ip6ConfigDataTypeManualAddress];
468 if (DataItem->Data.Ptr != NULL) {
469 DataItem->SetData (
470 &IpSb->Ip6ConfigInstance,
471 DataItem->DataSize,
472 DataItem->Data.Ptr
473 );
474 }
475
476 //
477 // If there is any gateway address, set it.
478 //
479 DataItem = &IpSb->Ip6ConfigInstance.DataItem[Ip6ConfigDataTypeGateway];
480 if (DataItem->Data.Ptr != NULL) {
481 DataItem->SetData (
482 &IpSb->Ip6ConfigInstance,
483 DataItem->DataSize,
484 DataItem->Data.Ptr
485 );
486 }
487
488 InsertHeadList (&IpSb->Interfaces, &IpSb->DefaultInterface->Link);
489
490 *Service = IpSb;
491 return EFI_SUCCESS;
492
493 ON_ERROR:
494 Ip6CleanService (IpSb);
495 FreePool (IpSb);
496 return Status;
497 }
498
499
500 /**
501 Start this driver on ControllerHandle.
502
503 @param[in] This Protocol instance pointer.
504 @param[in] ControllerHandle Handle of device to bind driver to.
505 @param[in] RemainingDevicePath Optional parameter used to pick a specific child
506 device to start.
507
508 @retval EFI_SUCCES This driver is added to ControllerHandle.
509 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
510 @retval other This driver does not support this device.
511
512 **/
513 EFI_STATUS
514 EFIAPI
515 Ip6DriverBindingStart (
516 IN EFI_DRIVER_BINDING_PROTOCOL *This,
517 IN EFI_HANDLE ControllerHandle,
518 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
519 )
520 {
521 IP6_SERVICE *IpSb;
522 EFI_STATUS Status;
523
524 //
525 // Test for the Ip6 service binding protocol
526 //
527 Status = gBS->OpenProtocol (
528 ControllerHandle,
529 &gEfiIp6ServiceBindingProtocolGuid,
530 NULL,
531 This->DriverBindingHandle,
532 ControllerHandle,
533 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
534 );
535
536 if (Status == EFI_SUCCESS) {
537 return EFI_ALREADY_STARTED;
538 }
539
540 Status = Ip6CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);
541
542 if (EFI_ERROR (Status)) {
543 return Status;
544 }
545
546 ASSERT (IpSb != NULL);
547
548 //
549 // Install the Ip6ServiceBinding Protocol onto ControlerHandle
550 //
551 Status = gBS->InstallMultipleProtocolInterfaces (
552 &ControllerHandle,
553 &gEfiIp6ServiceBindingProtocolGuid,
554 &IpSb->ServiceBinding,
555 &gEfiIp6ConfigProtocolGuid,
556 &IpSb->Ip6ConfigInstance.Ip6Config,
557 NULL
558 );
559
560 if (EFI_ERROR (Status)) {
561
562 Ip6CleanService (IpSb);
563 FreePool (IpSb);
564 } else {
565 //
566 // Initialize the IP6 ID
567 //
568 mIp6Id = NET_RANDOM (NetRandomInitSeed ());
569
570 Ip6SetVariableData (IpSb);
571 }
572
573 return Status;
574 }
575
576 /**
577 Callback function which provided by user to remove one node in NetDestroyLinkList process.
578
579 @param[in] Entry The entry to be removed.
580 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
581
582 @retval EFI_SUCCESS The entry has been removed successfully.
583 @retval Others Fail to remove the entry.
584
585 **/
586 EFI_STATUS
587 EFIAPI
588 Ip6DestroyChildEntryInHandleBuffer (
589 IN LIST_ENTRY *Entry,
590 IN VOID *Context
591 )
592 {
593 IP6_PROTOCOL *IpInstance;
594 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
595 UINTN NumberOfChildren;
596 EFI_HANDLE *ChildHandleBuffer;
597
598 if (Entry == NULL || Context == NULL) {
599 return EFI_INVALID_PARAMETER;
600 }
601
602 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
603 ServiceBinding = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ServiceBinding;
604 NumberOfChildren = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->NumberOfChildren;
605 ChildHandleBuffer = ((IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *) Context)->ChildHandleBuffer;
606
607 if (!NetIsInHandleBuffer (IpInstance->Handle, NumberOfChildren, ChildHandleBuffer)) {
608 return EFI_SUCCESS;
609 }
610
611 return ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
612 }
613
614 /**
615 Stop this driver on ControllerHandle.
616
617 @param[in] This Protocol instance pointer.
618 @param[in] ControllerHandle Handle of device to stop driver on.
619 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
620 of children is zero, stop the entire bus driver.
621 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
622 if NumberOfChildren is 0.
623
624 @retval EFI_SUCCESS The device was stopped.
625 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
626
627 **/
628 EFI_STATUS
629 EFIAPI
630 Ip6DriverBindingStop (
631 IN EFI_DRIVER_BINDING_PROTOCOL *This,
632 IN EFI_HANDLE ControllerHandle,
633 IN UINTN NumberOfChildren,
634 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
635 )
636 {
637 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
638 IP6_SERVICE *IpSb;
639 EFI_HANDLE NicHandle;
640 EFI_STATUS Status;
641 LIST_ENTRY *List;
642 INTN State;
643 BOOLEAN IsDhcp6;
644 IP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
645
646 IsDhcp6 = FALSE;
647 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
648 if (NicHandle == NULL) {
649 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp6ProtocolGuid);
650 if (NicHandle != NULL) {
651 IsDhcp6 = TRUE;
652 } else {
653 return EFI_SUCCESS;
654 }
655 }
656
657 Status = gBS->OpenProtocol (
658 NicHandle,
659 &gEfiIp6ServiceBindingProtocolGuid,
660 (VOID **) &ServiceBinding,
661 This->DriverBindingHandle,
662 NicHandle,
663 EFI_OPEN_PROTOCOL_GET_PROTOCOL
664 );
665 if (EFI_ERROR (Status)) {
666 return EFI_DEVICE_ERROR;
667 }
668
669 IpSb = IP6_SERVICE_FROM_PROTOCOL (ServiceBinding);
670
671 if (IsDhcp6) {
672 Status = Ip6ConfigDestroyDhcp6 (&IpSb->Ip6ConfigInstance);
673 gBS->CloseEvent (IpSb->Ip6ConfigInstance.Dhcp6Event);
674 IpSb->Ip6ConfigInstance.Dhcp6Event = NULL;
675 } else if (NumberOfChildren != 0) {
676 //
677 // NumberOfChildren is not zero, destroy the IP6 children instances in ChildHandleBuffer.
678 //
679 List = &IpSb->Children;
680 Context.ServiceBinding = ServiceBinding;
681 Context.NumberOfChildren = NumberOfChildren;
682 Context.ChildHandleBuffer = ChildHandleBuffer;
683 Status = NetDestroyLinkList (
684 List,
685 Ip6DestroyChildEntryInHandleBuffer,
686 &Context,
687 NULL
688 );
689 } else if (IsListEmpty (&IpSb->Children)) {
690 State = IpSb->State;
691 IpSb->State = IP6_SERVICE_DESTROY;
692
693 //
694 // Clear the variable data.
695 //
696 Ip6ClearVariableData (IpSb);
697
698 Status = Ip6CleanService (IpSb);
699 if (EFI_ERROR (Status)) {
700 IpSb->State = State;
701 goto Exit;
702 }
703
704 Status = gBS->UninstallMultipleProtocolInterfaces (
705 NicHandle,
706 &gEfiIp6ServiceBindingProtocolGuid,
707 ServiceBinding,
708 &gEfiIp6ConfigProtocolGuid,
709 &IpSb->Ip6ConfigInstance.Ip6Config,
710 NULL
711 );
712 ASSERT_EFI_ERROR (Status);
713 FreePool (IpSb);
714 Status = EFI_SUCCESS;
715 }
716
717 Exit:
718 return Status;
719 }
720
721
722 /**
723 Creates a child handle with a set of I/O services.
724
725 @param[in] This Protocol instance pointer.
726 @param[in] ChildHandle Pointer to the handle of the child to create. If
727 it is NULL, then a new handle is created. If it
728 is not NULL, then the I/O services are added to
729 the existing child handle.
730
731 @retval EFI_SUCCES The child handle was created with the I/O services.
732 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
733 the child.
734 @retval other The child handle was not created.
735
736 **/
737 EFI_STATUS
738 EFIAPI
739 Ip6ServiceBindingCreateChild (
740 IN EFI_SERVICE_BINDING_PROTOCOL *This,
741 IN EFI_HANDLE *ChildHandle
742 )
743 {
744 IP6_SERVICE *IpSb;
745 IP6_PROTOCOL *IpInstance;
746 EFI_TPL OldTpl;
747 EFI_STATUS Status;
748 VOID *Mnp;
749
750 if ((This == NULL) || (ChildHandle == NULL)) {
751 return EFI_INVALID_PARAMETER;
752 }
753
754 IpSb = IP6_SERVICE_FROM_PROTOCOL (This);
755
756 if (IpSb->LinkLocalDadFail) {
757 return EFI_DEVICE_ERROR;
758 }
759
760 IpInstance = AllocatePool (sizeof (IP6_PROTOCOL));
761
762 if (IpInstance == NULL) {
763 return EFI_OUT_OF_RESOURCES;
764 }
765
766 Ip6InitProtocol (IpSb, IpInstance);
767
768 //
769 // Install Ip6 onto ChildHandle
770 //
771 Status = gBS->InstallMultipleProtocolInterfaces (
772 ChildHandle,
773 &gEfiIp6ProtocolGuid,
774 &IpInstance->Ip6Proto,
775 NULL
776 );
777 if (EFI_ERROR (Status)) {
778 goto ON_ERROR;
779 }
780
781 IpInstance->Handle = *ChildHandle;
782
783 //
784 // Open the Managed Network protocol BY_CHILD.
785 //
786 Status = gBS->OpenProtocol (
787 IpSb->MnpChildHandle,
788 &gEfiManagedNetworkProtocolGuid,
789 (VOID **) &Mnp,
790 gIp6DriverBinding.DriverBindingHandle,
791 IpInstance->Handle,
792 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
793 );
794 if (EFI_ERROR (Status)) {
795 gBS->UninstallMultipleProtocolInterfaces (
796 ChildHandle,
797 &gEfiIp6ProtocolGuid,
798 &IpInstance->Ip6Proto,
799 NULL
800 );
801
802 goto ON_ERROR;
803 }
804
805 //
806 // Insert it into the service binding instance.
807 //
808 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
809
810 InsertTailList (&IpSb->Children, &IpInstance->Link);
811 IpSb->NumChildren++;
812
813 gBS->RestoreTPL (OldTpl);
814
815 ON_ERROR:
816
817 if (EFI_ERROR (Status)) {
818
819 Ip6CleanProtocol (IpInstance);
820
821 FreePool (IpInstance);
822 }
823
824 return Status;
825 }
826
827 /**
828 Destroys a child handle with a set of I/O services.
829
830 @param[in] This Protocol instance pointer.
831 @param[in] ChildHandle Handle of the child to destroy.
832
833 @retval EFI_SUCCES The I/O services were removed from the child
834 handle.
835 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
836 that are being removed.
837 @retval EFI_INVALID_PARAMETER Child handle is NULL.
838 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
839 its I/O services are being used.
840 @retval other The child handle was not destroyed.
841
842 **/
843 EFI_STATUS
844 EFIAPI
845 Ip6ServiceBindingDestroyChild (
846 IN EFI_SERVICE_BINDING_PROTOCOL *This,
847 IN EFI_HANDLE ChildHandle
848 )
849 {
850 EFI_STATUS Status;
851 IP6_SERVICE *IpSb;
852 IP6_PROTOCOL *IpInstance;
853 EFI_IP6_PROTOCOL *Ip6;
854 EFI_TPL OldTpl;
855
856 if ((This == NULL) || (ChildHandle == NULL)) {
857 return EFI_INVALID_PARAMETER;
858 }
859
860 //
861 // Retrieve the private context data structures
862 //
863 IpSb = IP6_SERVICE_FROM_PROTOCOL (This);
864
865 Status = gBS->OpenProtocol (
866 ChildHandle,
867 &gEfiIp6ProtocolGuid,
868 (VOID **) &Ip6,
869 gIp6DriverBinding.DriverBindingHandle,
870 ChildHandle,
871 EFI_OPEN_PROTOCOL_GET_PROTOCOL
872 );
873
874 if (EFI_ERROR (Status)) {
875 return EFI_UNSUPPORTED;
876 }
877
878 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (Ip6);
879
880 if (IpInstance->Service != IpSb) {
881 return EFI_INVALID_PARAMETER;
882 }
883
884 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
885
886 //
887 // A child can be destroyed more than once. For example,
888 // Ip6DriverBindingStop will destroy all of its children.
889 // when UDP driver is being stopped, it will destroy all
890 // the IP child it opens.
891 //
892 if (IpInstance->InDestroy) {
893 gBS->RestoreTPL (OldTpl);
894 return EFI_SUCCESS;
895 }
896
897 IpInstance->InDestroy = TRUE;
898
899 //
900 // Close the Managed Network protocol.
901 //
902 gBS->CloseProtocol (
903 IpSb->MnpChildHandle,
904 &gEfiManagedNetworkProtocolGuid,
905 gIp6DriverBinding.DriverBindingHandle,
906 ChildHandle
907 );
908
909 //
910 // Uninstall the IP6 protocol first. Many thing happens during
911 // this:
912 // 1. The consumer of the IP6 protocol will be stopped if it
913 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
914 // stopped, IP driver's stop function will be called, and uninstall
915 // EFI_IP6_PROTOCOL will trigger the UDP's stop function. This
916 // makes it possible to create the network stack bottom up, and
917 // stop it top down.
918 // 2. the upper layer will recycle the received packet. The recycle
919 // event's TPL is higher than this function. The recycle events
920 // will be called back before preceeding. If any packets not recycled,
921 // that means there is a resource leak.
922 //
923 gBS->RestoreTPL (OldTpl);
924 Status = gBS->UninstallProtocolInterface (
925 ChildHandle,
926 &gEfiIp6ProtocolGuid,
927 &IpInstance->Ip6Proto
928 );
929 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
930 if (EFI_ERROR (Status)) {
931 goto ON_ERROR;
932 }
933
934 Status = Ip6CleanProtocol (IpInstance);
935
936 Ip6SetVariableData (IpSb);
937
938 if (EFI_ERROR (Status)) {
939 gBS->InstallMultipleProtocolInterfaces (
940 &ChildHandle,
941 &gEfiIp6ProtocolGuid,
942 Ip6,
943 NULL
944 );
945
946 goto ON_ERROR;
947 }
948
949 RemoveEntryList (&IpInstance->Link);
950 ASSERT (IpSb->NumChildren > 0);
951 IpSb->NumChildren--;
952
953 gBS->RestoreTPL (OldTpl);
954
955 FreePool (IpInstance);
956 return EFI_SUCCESS;
957
958 ON_ERROR:
959 gBS->RestoreTPL (OldTpl);
960
961 return Status;
962 }