]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Driver.c
Add manual configured gateway address back after system reset.
[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 //
118 // Leave link-scope all-nodes multicast address (FF02::1)
119 //
120 Ip6SetToAllNodeMulticast (FALSE, IP6_LINK_LOCAL_SCOPE, &AllNodes);
121
122 Status = Ip6LeaveGroup (IpSb, &AllNodes);
123 if (EFI_ERROR (Status)) {
124 return Status;
125 }
126
127 if (IpSb->DefaultInterface != NULL) {
128 Ip6CleanInterface (IpSb->DefaultInterface, NULL);
129 IpSb->DefaultInterface = NULL;
130 }
131
132 Ip6CleanDefaultRouterList (IpSb);
133
134 Ip6CleanPrefixListTable (IpSb, &IpSb->OnlinkPrefix);
135 Ip6CleanPrefixListTable (IpSb, &IpSb->AutonomousPrefix);
136
137 if (IpSb->RouteTable != NULL) {
138 Ip6CleanRouteTable (IpSb->RouteTable);
139 IpSb->RouteTable = NULL;
140 }
141
142 if (IpSb->InterfaceId != NULL) {
143 FreePool (IpSb->InterfaceId);
144 }
145
146 IpSb->InterfaceId = NULL;
147
148 Ip6CleanAssembleTable (&IpSb->Assemble);
149
150 if (IpSb->MnpChildHandle != NULL) {
151 if (IpSb->Mnp != NULL) {
152 IpSb->Mnp->Cancel (IpSb->Mnp, NULL);
153 IpSb->Mnp->Configure (IpSb->Mnp, NULL);
154 gBS->CloseProtocol (
155 IpSb->MnpChildHandle,
156 &gEfiManagedNetworkProtocolGuid,
157 IpSb->Image,
158 IpSb->Controller
159 );
160
161 IpSb->Mnp = NULL;
162 }
163
164 NetLibDestroyServiceChild (
165 IpSb->Controller,
166 IpSb->Image,
167 &gEfiManagedNetworkServiceBindingProtocolGuid,
168 IpSb->MnpChildHandle
169 );
170
171 IpSb->MnpChildHandle = NULL;
172 }
173
174 if (IpSb->RecvRequest.MnpToken.Event != NULL) {
175 gBS->CloseEvent (IpSb->RecvRequest.MnpToken.Event);
176 }
177
178 if (IpSb->Timer != NULL) {
179 gBS->SetTimer (IpSb->Timer, TimerCancel, 0);
180 gBS->CloseEvent (IpSb->Timer);
181
182 IpSb->Timer = NULL;
183 }
184
185 if (IpSb->FasterTimer != NULL) {
186 gBS->SetTimer (IpSb->FasterTimer, TimerCancel, 0);
187 gBS->CloseEvent (IpSb->FasterTimer);
188
189 IpSb->FasterTimer = NULL;
190 }
191 //
192 // Free the Neighbor Discovery resources
193 //
194 while (!IsListEmpty (&IpSb->NeighborTable)) {
195 NeighborCache = NET_LIST_HEAD (&IpSb->NeighborTable, IP6_NEIGHBOR_ENTRY, Link);
196 Ip6FreeNeighborEntry (IpSb, NeighborCache, FALSE, TRUE, EFI_SUCCESS, NULL, NULL);
197 }
198
199 return EFI_SUCCESS;
200 }
201
202 /**
203 Create a new IP6 driver service binding protocol.
204
205 @param[in] Controller The controller that has MNP service binding
206 installed.
207 @param[in] ImageHandle The IP6 driver's image handle.
208 @param[out] Service The variable to receive the newly created IP6
209 service.
210
211 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
212 @retval EFI_SUCCESS A new IP6 service binding private is created.
213
214 **/
215 EFI_STATUS
216 Ip6CreateService (
217 IN EFI_HANDLE Controller,
218 IN EFI_HANDLE ImageHandle,
219 OUT IP6_SERVICE **Service
220 )
221 {
222 IP6_SERVICE *IpSb;
223 EFI_STATUS Status;
224 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken;
225 EFI_MANAGED_NETWORK_CONFIG_DATA *Config;
226 IP6_CONFIG_DATA_ITEM *DataItem;
227
228 ASSERT (Service != NULL);
229
230 *Service = NULL;
231
232 //
233 // allocate a service private data then initialize all the filed to
234 // empty resources, so if any thing goes wrong when allocating
235 // resources, Ip6CleanService can be called to clean it up.
236 //
237 IpSb = AllocateZeroPool (sizeof (IP6_SERVICE));
238
239 if (IpSb == NULL) {
240 return EFI_OUT_OF_RESOURCES;
241 }
242
243 IpSb->Signature = IP6_SERVICE_SIGNATURE;
244 IpSb->ServiceBinding.CreateChild = Ip6ServiceBindingCreateChild;
245 IpSb->ServiceBinding.DestroyChild = Ip6ServiceBindingDestroyChild;
246 IpSb->State = IP6_SERVICE_UNSTARTED;
247 IpSb->InDestroy = FALSE;
248
249 IpSb->NumChildren = 0;
250 InitializeListHead (&IpSb->Children);
251
252 InitializeListHead (&IpSb->Interfaces);
253 IpSb->DefaultInterface = NULL;
254 IpSb->RouteTable = NULL;
255
256 IpSb->RecvRequest.Signature = IP6_LINK_RX_SIGNATURE;
257 IpSb->RecvRequest.CallBack = NULL;
258 IpSb->RecvRequest.Context = NULL;
259 MnpToken = &IpSb->RecvRequest.MnpToken;
260 MnpToken->Event = NULL;
261 MnpToken->Status = EFI_NOT_READY;
262 MnpToken->Packet.RxData = NULL;
263
264 Ip6CreateAssembleTable (&IpSb->Assemble);
265
266 IpSb->MldCtrl.Mldv1QuerySeen = 0;
267 InitializeListHead (&IpSb->MldCtrl.Groups);
268
269 ZeroMem (&IpSb->LinkLocalAddr, sizeof (EFI_IPv6_ADDRESS));
270 IpSb->LinkLocalOk = FALSE;
271 IpSb->LinkLocalDadFail = FALSE;
272 IpSb->Dhcp6NeedStart = FALSE;
273 IpSb->Dhcp6NeedInfoRequest = FALSE;
274
275 IpSb->CurHopLimit = IP6_HOP_LIMIT;
276 IpSb->LinkMTU = IP6_MIN_LINK_MTU;
277 IpSb->BaseReachableTime = IP6_REACHABLE_TIME;
278 Ip6UpdateReachableTime (IpSb);
279 //
280 // RFC4861 RETRANS_TIMER: 1,000 milliseconds
281 //
282 IpSb->RetransTimer = IP6_RETRANS_TIMER;
283
284 IpSb->RoundRobin = 0;
285
286 InitializeListHead (&IpSb->NeighborTable);
287 InitializeListHead (&IpSb->DefaultRouterList);
288 InitializeListHead (&IpSb->OnlinkPrefix);
289 InitializeListHead (&IpSb->AutonomousPrefix);
290
291 IpSb->InterfaceIdLen = IP6_IF_ID_LEN;
292 IpSb->InterfaceId = NULL;
293
294 IpSb->RouterAdvertiseReceived = FALSE;
295 IpSb->SolicitTimer = IP6_MAX_RTR_SOLICITATIONS;
296 IpSb->Ticks = 0;
297
298 IpSb->Image = ImageHandle;
299 IpSb->Controller = Controller;
300
301 IpSb->MnpChildHandle = NULL;
302 IpSb->Mnp = NULL;
303
304 Config = &IpSb->MnpConfigData;
305 Config->ReceivedQueueTimeoutValue = 0;
306 Config->TransmitQueueTimeoutValue = 0;
307 Config->ProtocolTypeFilter = IP6_ETHER_PROTO;
308 Config->EnableUnicastReceive = TRUE;
309 Config->EnableMulticastReceive = TRUE;
310 Config->EnableBroadcastReceive = TRUE;
311 Config->EnablePromiscuousReceive = FALSE;
312 Config->FlushQueuesOnReset = TRUE;
313 Config->EnableReceiveTimestamps = FALSE;
314 Config->DisableBackgroundPolling = FALSE;
315
316 ZeroMem (&IpSb->SnpMode, sizeof (EFI_SIMPLE_NETWORK_MODE));
317
318 IpSb->Timer = NULL;
319 IpSb->FasterTimer = NULL;
320
321 ZeroMem (&IpSb->Ip6ConfigInstance, sizeof (IP6_CONFIG_INSTANCE));
322
323 IpSb->MacString = NULL;
324
325 //
326 // Create various resources. First create the route table, timer
327 // event, MNP token event and MNP child.
328 //
329
330 IpSb->RouteTable = Ip6CreateRouteTable ();
331 if (IpSb->RouteTable == NULL) {
332 Status = EFI_OUT_OF_RESOURCES;
333 goto ON_ERROR;
334 }
335
336 Status = gBS->CreateEvent (
337 EVT_NOTIFY_SIGNAL | EVT_TIMER,
338 TPL_CALLBACK,
339 Ip6TimerTicking,
340 IpSb,
341 &IpSb->Timer
342 );
343 if (EFI_ERROR (Status)) {
344 goto ON_ERROR;
345 }
346
347 Status = gBS->CreateEvent (
348 EVT_NOTIFY_SIGNAL | EVT_TIMER,
349 TPL_CALLBACK,
350 Ip6NdFasterTimerTicking,
351 IpSb,
352 &IpSb->FasterTimer
353 );
354 if (EFI_ERROR (Status)) {
355 goto ON_ERROR;
356 }
357
358 Status = NetLibCreateServiceChild (
359 Controller,
360 ImageHandle,
361 &gEfiManagedNetworkServiceBindingProtocolGuid,
362 &IpSb->MnpChildHandle
363 );
364 if (EFI_ERROR (Status)) {
365 goto ON_ERROR;
366 }
367
368 Status = gBS->OpenProtocol (
369 IpSb->MnpChildHandle,
370 &gEfiManagedNetworkProtocolGuid,
371 (VOID **) (&IpSb->Mnp),
372 ImageHandle,
373 Controller,
374 EFI_OPEN_PROTOCOL_BY_DRIVER
375 );
376 if (EFI_ERROR (Status)) {
377 goto ON_ERROR;
378 }
379
380 Status = Ip6ServiceConfigMnp (IpSb, TRUE);
381 if (EFI_ERROR (Status)) {
382 goto ON_ERROR;
383 }
384
385 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &IpSb->SnpMode);
386 if (EFI_ERROR (Status)) {
387 goto ON_ERROR;
388 }
389
390 IpSb->MaxPacketSize = IP6_MIN_LINK_MTU - sizeof (EFI_IP6_HEADER);
391 if (NetLibGetVlanId (IpSb->Controller) != 0) {
392 //
393 // This is a VLAN device, reduce MTU by VLAN tag length
394 //
395 IpSb->MaxPacketSize -= NET_VLAN_TAG_LEN;
396 }
397 IpSb->OldMaxPacketSize = IpSb->MaxPacketSize;
398
399 //
400 // Currently only ETHERNET is supported in IPv6 stack, since
401 // link local address requires an IEEE 802 48-bit MACs for
402 // EUI-64 format interface identifier mapping.
403 //
404 if (IpSb->SnpMode.IfType != NET_IFTYPE_ETHERNET) {
405 Status = EFI_UNSUPPORTED;
406 goto ON_ERROR;
407 }
408
409 Status = Ip6InitMld (IpSb);
410 if (EFI_ERROR (Status)) {
411 goto ON_ERROR;
412 }
413
414 //
415 // The timer expires every 100 (IP6_TIMER_INTERVAL_IN_MS) milliseconds.
416 //
417 Status = gBS->SetTimer (IpSb->FasterTimer, TimerPeriodic, TICKS_PER_MS * IP6_TIMER_INTERVAL_IN_MS);
418 if (EFI_ERROR (Status)) {
419 goto ON_ERROR;
420 }
421
422 //
423 // The timer expires every 1000 (IP6_ONE_SECOND_IN_MS) milliseconds.
424 //
425 Status = gBS->SetTimer (IpSb->Timer, TimerPeriodic, TICKS_PER_MS * IP6_ONE_SECOND_IN_MS);
426 if (EFI_ERROR (Status)) {
427 goto ON_ERROR;
428 }
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 Status = Ip6ReceiveFrame (Ip6AcceptFrame, IpSb);
443 if (EFI_ERROR (Status)) {
444 goto ON_ERROR;
445 }
446
447 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &IpSb->MacString);
448 if (EFI_ERROR (Status)) {
449 goto ON_ERROR;
450 }
451
452 Status = Ip6ConfigInitInstance (&IpSb->Ip6ConfigInstance);
453 if (EFI_ERROR (Status)) {
454 goto ON_ERROR;
455 }
456
457 IpSb->DefaultInterface = Ip6CreateInterface (IpSb, TRUE);
458 if (IpSb->DefaultInterface == NULL) {
459 Status = EFI_DEVICE_ERROR;
460 goto ON_ERROR;
461 }
462
463 //
464 // If there is any manual address, set it.
465 //
466 DataItem = &IpSb->Ip6ConfigInstance.DataItem[Ip6ConfigDataTypeManualAddress];
467 if (DataItem->Data.Ptr != NULL) {
468 DataItem->SetData (
469 &IpSb->Ip6ConfigInstance,
470 DataItem->DataSize,
471 DataItem->Data.Ptr
472 );
473 }
474
475 //
476 // If there is any gateway address, set it.
477 //
478 DataItem = &IpSb->Ip6ConfigInstance.DataItem[Ip6ConfigDataTypeGateway];
479 if (DataItem->Data.Ptr != NULL) {
480 DataItem->SetData (
481 &IpSb->Ip6ConfigInstance,
482 DataItem->DataSize,
483 DataItem->Data.Ptr
484 );
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
523 //
524 // Test for the Ip6 service binding protocol
525 //
526 Status = gBS->OpenProtocol (
527 ControllerHandle,
528 &gEfiIp6ServiceBindingProtocolGuid,
529 NULL,
530 This->DriverBindingHandle,
531 ControllerHandle,
532 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
533 );
534
535 if (Status == EFI_SUCCESS) {
536 return EFI_ALREADY_STARTED;
537 }
538
539 Status = Ip6CreateService (ControllerHandle, This->DriverBindingHandle, &IpSb);
540
541 if (EFI_ERROR (Status)) {
542 return Status;
543 }
544
545 ASSERT (IpSb != NULL);
546
547 //
548 // Install the Ip6ServiceBinding Protocol onto ControlerHandle
549 //
550 Status = gBS->InstallMultipleProtocolInterfaces (
551 &ControllerHandle,
552 &gEfiIp6ServiceBindingProtocolGuid,
553 &IpSb->ServiceBinding,
554 &gEfiIp6ConfigProtocolGuid,
555 &IpSb->Ip6ConfigInstance.Ip6Config,
556 NULL
557 );
558
559 if (EFI_ERROR (Status)) {
560
561 Ip6CleanService (IpSb);
562 FreePool (IpSb);
563 } else {
564 //
565 // Initialize the IP6 ID
566 //
567 mIp6Id = NET_RANDOM (NetRandomInitSeed ());
568
569 Ip6SetVariableData (IpSb);
570 }
571
572 return Status;
573 }
574
575 /**
576 Stop this driver on ControllerHandle.
577
578 @param[in] This Protocol instance pointer.
579 @param[in] ControllerHandle Handle of device to stop driver on.
580 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number
581 of children is zero, stop the entire bus driver.
582 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
583 if NumberOfChildren is 0.
584
585 @retval EFI_SUCCESS The device was stopped.
586 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
587
588 **/
589 EFI_STATUS
590 EFIAPI
591 Ip6DriverBindingStop (
592 IN EFI_DRIVER_BINDING_PROTOCOL *This,
593 IN EFI_HANDLE ControllerHandle,
594 IN UINTN NumberOfChildren,
595 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
596 )
597 {
598 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
599 IP6_SERVICE *IpSb;
600 IP6_PROTOCOL *IpInstance;
601 EFI_HANDLE NicHandle;
602 EFI_STATUS Status;
603 BOOLEAN IsDhcp6;
604 EFI_TPL OldTpl;
605 INTN State;
606
607 IsDhcp6 = FALSE;
608 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiDhcp6ProtocolGuid);
609
610 if (NicHandle != NULL) {
611 //
612 // DriverBindingStop is triggered by the uninstallation of the EFI DHCPv6
613 // Protocol used by Ip6Config.
614 //
615 IsDhcp6 = TRUE;
616 } else {
617
618 NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiManagedNetworkProtocolGuid);
619
620 if (NicHandle == NULL) {
621 return EFI_DEVICE_ERROR;
622 }
623 }
624
625 Status = gBS->OpenProtocol (
626 NicHandle,
627 &gEfiIp6ServiceBindingProtocolGuid,
628 (VOID **) &ServiceBinding,
629 This->DriverBindingHandle,
630 NicHandle,
631 EFI_OPEN_PROTOCOL_GET_PROTOCOL
632 );
633 if (EFI_ERROR (Status)) {
634 return EFI_DEVICE_ERROR;
635 }
636
637 IpSb = IP6_SERVICE_FROM_PROTOCOL (ServiceBinding);
638
639 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
640
641 if (IpSb->InDestroy) {
642 Status = EFI_SUCCESS;
643 goto Exit;
644 }
645
646 if (IsDhcp6) {
647
648 Status = Ip6ConfigDestroyDhcp6 (&IpSb->Ip6ConfigInstance);
649 gBS->CloseEvent (IpSb->Ip6ConfigInstance.Dhcp6Event);
650 IpSb->Ip6ConfigInstance.Dhcp6Event = NULL;
651 } else if (NumberOfChildren == 0) {
652
653 IpSb->InDestroy = TRUE;
654 State = IpSb->State;
655 IpSb->State = IP6_SERVICE_DESTROY;
656
657 //
658 // Clear the variable data.
659 //
660 Ip6ClearVariableData (IpSb);
661
662 Status = Ip6CleanService (IpSb);
663 if (EFI_ERROR (Status)) {
664 IpSb->State = State;
665 goto Exit;
666 }
667
668 Status = gBS->UninstallMultipleProtocolInterfaces (
669 NicHandle,
670 &gEfiIp6ServiceBindingProtocolGuid,
671 ServiceBinding,
672 &gEfiIp6ConfigProtocolGuid,
673 &IpSb->Ip6ConfigInstance.Ip6Config,
674 NULL
675 );
676 ASSERT_EFI_ERROR (Status);
677 FreePool (IpSb);
678 } else {
679 //
680 // NumberOfChildren is not zero, destroy all IP6 children instances.
681 //
682 while (!IsListEmpty (&IpSb->Children)) {
683 IpInstance = NET_LIST_HEAD (&IpSb->Children, IP6_PROTOCOL, Link);
684 ServiceBinding->DestroyChild (ServiceBinding, IpInstance->Handle);
685 }
686
687 if (IpSb->NumChildren != 0) {
688 Status = EFI_DEVICE_ERROR;
689 }
690 }
691
692 Exit:
693
694 gBS->RestoreTPL (OldTpl);
695
696 return Status;
697 }
698
699
700 /**
701 Creates a child handle with a set of I/O services.
702
703 @param[in] This Protocol instance pointer.
704 @param[in] ChildHandle Pointer to the handle of the child to create. If
705 it is NULL, then a new handle is created. If it
706 is not NULL, then the I/O services are added to
707 the existing child handle.
708
709 @retval EFI_SUCCES The child handle was created with the I/O services.
710 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
711 the child.
712 @retval other The child handle was not created.
713
714 **/
715 EFI_STATUS
716 EFIAPI
717 Ip6ServiceBindingCreateChild (
718 IN EFI_SERVICE_BINDING_PROTOCOL *This,
719 IN EFI_HANDLE *ChildHandle
720 )
721 {
722 IP6_SERVICE *IpSb;
723 IP6_PROTOCOL *IpInstance;
724 EFI_TPL OldTpl;
725 EFI_STATUS Status;
726 VOID *Mnp;
727
728 if ((This == NULL) || (ChildHandle == NULL)) {
729 return EFI_INVALID_PARAMETER;
730 }
731
732 IpSb = IP6_SERVICE_FROM_PROTOCOL (This);
733
734 if (IpSb->LinkLocalDadFail) {
735 return EFI_DEVICE_ERROR;
736 }
737
738 IpInstance = AllocatePool (sizeof (IP6_PROTOCOL));
739
740 if (IpInstance == NULL) {
741 return EFI_OUT_OF_RESOURCES;
742 }
743
744 Ip6InitProtocol (IpSb, IpInstance);
745
746 //
747 // Install Ip6 onto ChildHandle
748 //
749 Status = gBS->InstallMultipleProtocolInterfaces (
750 ChildHandle,
751 &gEfiIp6ProtocolGuid,
752 &IpInstance->Ip6Proto,
753 NULL
754 );
755 if (EFI_ERROR (Status)) {
756 goto ON_ERROR;
757 }
758
759 IpInstance->Handle = *ChildHandle;
760
761 //
762 // Open the Managed Network protocol BY_CHILD.
763 //
764 Status = gBS->OpenProtocol (
765 IpSb->MnpChildHandle,
766 &gEfiManagedNetworkProtocolGuid,
767 (VOID **) &Mnp,
768 gIp6DriverBinding.DriverBindingHandle,
769 IpInstance->Handle,
770 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
771 );
772 if (EFI_ERROR (Status)) {
773 gBS->UninstallMultipleProtocolInterfaces (
774 ChildHandle,
775 &gEfiIp6ProtocolGuid,
776 &IpInstance->Ip6Proto,
777 NULL
778 );
779
780 goto ON_ERROR;
781 }
782
783 //
784 // Insert it into the service binding instance.
785 //
786 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
787
788 InsertTailList (&IpSb->Children, &IpInstance->Link);
789 IpSb->NumChildren++;
790
791 gBS->RestoreTPL (OldTpl);
792
793 ON_ERROR:
794
795 if (EFI_ERROR (Status)) {
796
797 Ip6CleanProtocol (IpInstance);
798
799 FreePool (IpInstance);
800 }
801
802 return Status;
803 }
804
805 /**
806 Destroys a child handle with a set of I/O services.
807
808 @param[in] This Protocol instance pointer.
809 @param[in] ChildHandle Handle of the child to destroy.
810
811 @retval EFI_SUCCES The I/O services were removed from the child
812 handle.
813 @retval EFI_UNSUPPORTED The child handle does not support the I/O services
814 that are being removed.
815 @retval EFI_INVALID_PARAMETER Child handle is NULL.
816 @retval EFI_ACCESS_DENIED The child handle could not be destroyed because
817 its I/O services are being used.
818 @retval other The child handle was not destroyed.
819
820 **/
821 EFI_STATUS
822 EFIAPI
823 Ip6ServiceBindingDestroyChild (
824 IN EFI_SERVICE_BINDING_PROTOCOL *This,
825 IN EFI_HANDLE ChildHandle
826 )
827 {
828 EFI_STATUS Status;
829 IP6_SERVICE *IpSb;
830 IP6_PROTOCOL *IpInstance;
831 EFI_IP6_PROTOCOL *Ip6;
832 EFI_TPL OldTpl;
833 INTN State;
834
835 if ((This == NULL) || (ChildHandle == NULL)) {
836 return EFI_INVALID_PARAMETER;
837 }
838
839 //
840 // Retrieve the private context data structures
841 //
842 IpSb = IP6_SERVICE_FROM_PROTOCOL (This);
843
844 Status = gBS->OpenProtocol (
845 ChildHandle,
846 &gEfiIp6ProtocolGuid,
847 (VOID **) &Ip6,
848 gIp6DriverBinding.DriverBindingHandle,
849 ChildHandle,
850 EFI_OPEN_PROTOCOL_GET_PROTOCOL
851 );
852
853 if (EFI_ERROR (Status)) {
854 return EFI_UNSUPPORTED;
855 }
856
857 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (Ip6);
858
859 if (IpInstance->Service != IpSb) {
860 return EFI_INVALID_PARAMETER;
861 }
862
863 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
864
865 //
866 // A child can be destroyed more than once. For example,
867 // Ip6DriverBindingStop will destroy all of its children.
868 // when UDP driver is being stopped, it will destroy all
869 // the IP child it opens.
870 //
871 if (IpInstance->State == IP6_STATE_DESTROY) {
872 gBS->RestoreTPL (OldTpl);
873 return EFI_SUCCESS;
874 }
875
876 State = IpInstance->State;
877 IpInstance->State = IP6_STATE_DESTROY;
878
879 //
880 // Close the Managed Network protocol.
881 //
882 gBS->CloseProtocol (
883 IpSb->MnpChildHandle,
884 &gEfiManagedNetworkProtocolGuid,
885 gIp6DriverBinding.DriverBindingHandle,
886 ChildHandle
887 );
888
889 //
890 // Uninstall the IP6 protocol first. Many thing happens during
891 // this:
892 // 1. The consumer of the IP6 protocol will be stopped if it
893 // opens the protocol BY_DRIVER. For eaxmple, if MNP driver is
894 // stopped, IP driver's stop function will be called, and uninstall
895 // EFI_IP6_PROTOCOL will trigger the UDP's stop function. This
896 // makes it possible to create the network stack bottom up, and
897 // stop it top down.
898 // 2. the upper layer will recycle the received packet. The recycle
899 // event's TPL is higher than this function. The recycle events
900 // will be called back before preceeding. If any packets not recycled,
901 // that means there is a resource leak.
902 //
903 Status = gBS->UninstallProtocolInterface (
904 ChildHandle,
905 &gEfiIp6ProtocolGuid,
906 &IpInstance->Ip6Proto
907 );
908
909 if (EFI_ERROR (Status)) {
910 goto ON_ERROR;
911 }
912
913 Status = Ip6CleanProtocol (IpInstance);
914
915 Ip6SetVariableData (IpSb);
916
917 if (EFI_ERROR (Status)) {
918 gBS->InstallMultipleProtocolInterfaces (
919 &ChildHandle,
920 &gEfiIp6ProtocolGuid,
921 Ip6,
922 NULL
923 );
924
925 goto ON_ERROR;
926 }
927
928 RemoveEntryList (&IpInstance->Link);
929 ASSERT (IpSb->NumChildren > 0);
930 IpSb->NumChildren--;
931
932 gBS->RestoreTPL (OldTpl);
933
934 FreePool (IpInstance);
935 return EFI_SUCCESS;
936
937 ON_ERROR:
938 IpInstance->State = State;
939 gBS->RestoreTPL (OldTpl);
940
941 return Status;
942 }