2 Driver Binding functions and Service Binding functions
3 implementationfor for Dhcp6 Driver.
5 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
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.
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.
17 #include "Dhcp6Impl.h"
20 EFI_DRIVER_BINDING_PROTOCOL gDhcp6DriverBinding
= {
21 Dhcp6DriverBindingSupported
,
22 Dhcp6DriverBindingStart
,
23 Dhcp6DriverBindingStop
,
29 EFI_SERVICE_BINDING_PROTOCOL gDhcp6ServiceBindingTemplate
= {
30 Dhcp6ServiceBindingCreateChild
,
31 Dhcp6ServiceBindingDestroyChild
35 Configure the default Udp6Io to receive all the DHCP6 traffic
36 on this network interface.
38 @param[in] UdpIo The pointer to Udp6Io to be configured.
39 @param[in] Context The pointer to the context.
41 @retval EFI_SUCCESS The Udp6Io is successfully configured.
42 @retval Others Failed to configure the Udp6Io.
52 EFI_UDP6_PROTOCOL
*Udp6
;
53 EFI_UDP6_CONFIG_DATA
*Config
;
55 Udp6
= UdpIo
->Protocol
.Udp6
;
56 Config
= &(UdpIo
->Config
.Udp6
);
58 ZeroMem (Config
, sizeof (EFI_UDP6_CONFIG_DATA
));
61 // Set Udp6 configure data for the Dhcp6 instance.
63 Config
->AcceptPromiscuous
= FALSE
;
64 Config
->AcceptAnyPort
= FALSE
;
65 Config
->AllowDuplicatePort
= FALSE
;
66 Config
->TrafficClass
= 0;
67 Config
->HopLimit
= 128;
68 Config
->ReceiveTimeout
= 0;
69 Config
->TransmitTimeout
= 0;
72 // Configure an endpoint of client(0, 546), server(0, 0), the addresses
73 // will be overridden later. Note that we MUST not limit RemotePort.
74 // More details, refer to RFC 3315 section 5.2.
76 Config
->StationPort
= DHCP6_PORT_CLIENT
;
77 Config
->RemotePort
= 0;
79 return Udp6
->Configure (Udp6
, Config
);;
84 Destroy the Dhcp6 service. The Dhcp6 service may be partly initialized,
85 or partly destroyed. If a resource is destroyed, it is marked as such in
86 case the destroy failed and being called again later.
88 @param[in, out] Service The pointer to Dhcp6 service to be destroyed.
93 IN OUT DHCP6_SERVICE
*Service
97 // All children instances should have been already destroyed here.
99 ASSERT (Service
->NumOfChild
== 0);
101 if (Service
->ClientId
!= NULL
) {
102 FreePool (Service
->ClientId
);
105 if (Service
->UdpIo
!= NULL
) {
106 UdpIoFreeIo (Service
->UdpIo
);
114 Create a new Dhcp6 service for the Nic controller.
116 @param[in] Controller The controller to be installed DHCP6 service
118 @param[in] ImageHandle The image handle of the Dhcp6 driver.
119 @param[out] Service The return pointer of the new Dhcp6 service.
121 @retval EFI_SUCCESS The Dhcp6 service is created successfully.
122 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
123 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
128 IN EFI_HANDLE Controller
,
129 IN EFI_HANDLE ImageHandle
,
130 OUT DHCP6_SERVICE
**Service
133 DHCP6_SERVICE
*Dhcp6Srv
;
137 Dhcp6Srv
= AllocateZeroPool (sizeof (DHCP6_SERVICE
));
139 if (Dhcp6Srv
== NULL
) {
140 return EFI_OUT_OF_RESOURCES
;
144 // Open the SNP protocol to get mode data later.
146 Dhcp6Srv
->Snp
= NULL
;
147 NetLibGetSnpHandle (Controller
, &Dhcp6Srv
->Snp
);
148 if (Dhcp6Srv
->Snp
== NULL
) {
150 return EFI_DEVICE_ERROR
;
154 // Initialize the fields of the new Dhcp6 service.
156 Dhcp6Srv
->Signature
= DHCP6_SERVICE_SIGNATURE
;
157 Dhcp6Srv
->Controller
= Controller
;
158 Dhcp6Srv
->Image
= ImageHandle
;
159 Dhcp6Srv
->Xid
= (0xffffff & NET_RANDOM (NetRandomInitSeed ()));
162 &Dhcp6Srv
->ServiceBinding
,
163 &gDhcp6ServiceBindingTemplate
,
164 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
168 // Locate Ip6->Ip6Config and store it for get IP6 Duplicate Address Detection transmits.
170 Status
= gBS
->HandleProtocol (
172 &gEfiIp6ConfigProtocolGuid
,
173 (VOID
**) &Dhcp6Srv
->Ip6Cfg
175 if (EFI_ERROR (Status
)) {
181 // Generate client Duid: If SMBIOS system UUID is located, generate DUID in DUID-UUID format.
182 // Otherwise, in DUID-LLT format.
184 Dhcp6Srv
->ClientId
= Dhcp6GenerateClientId (Dhcp6Srv
->Snp
->Mode
);
186 if (Dhcp6Srv
->ClientId
== NULL
) {
188 return EFI_DEVICE_ERROR
;
192 // Create an Udp6Io for stateful transmit/receive of each Dhcp6 instance.
194 Dhcp6Srv
->UdpIo
= UdpIoCreateIo (
202 if (Dhcp6Srv
->UdpIo
== NULL
) {
203 FreePool (Dhcp6Srv
->ClientId
);
205 return EFI_DEVICE_ERROR
;
208 InitializeListHead (&Dhcp6Srv
->Child
);
217 Destroy the Dhcp6 instance and recycle the resources.
219 @param[in, out] Instance The pointer to the Dhcp6 instance.
223 Dhcp6DestroyInstance (
224 IN OUT DHCP6_INSTANCE
*Instance
228 // Clean up the retry list first.
230 Dhcp6CleanupRetry (Instance
, DHCP6_PACKET_ALL
);
231 gBS
->CloseEvent (Instance
->Timer
);
234 // Clean up the current configure data.
236 if (Instance
->Config
!= NULL
) {
237 Dhcp6CleanupConfigData (Instance
->Config
);
238 FreePool (Instance
->Config
);
242 // Clean up the current Ia.
244 if (Instance
->IaCb
.Ia
!= NULL
) {
245 if (Instance
->IaCb
.Ia
->ReplyPacket
!= NULL
) {
246 FreePool (Instance
->IaCb
.Ia
->ReplyPacket
);
248 FreePool (Instance
->IaCb
.Ia
);
251 if (Instance
->Unicast
!= NULL
) {
252 FreePool (Instance
->Unicast
);
255 if (Instance
->AdSelect
!= NULL
) {
256 FreePool (Instance
->AdSelect
);
264 Create the Dhcp6 instance and initialize it.
266 @param[in] Service The pointer to the Dhcp6 service.
267 @param[out] Instance The pointer to the Dhcp6 instance.
269 @retval EFI_SUCCESS The Dhcp6 instance is created.
270 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
274 Dhcp6CreateInstance (
275 IN DHCP6_SERVICE
*Service
,
276 OUT DHCP6_INSTANCE
**Instance
280 DHCP6_INSTANCE
*Dhcp6Ins
;
283 Dhcp6Ins
= AllocateZeroPool (sizeof (DHCP6_INSTANCE
));
285 if (Dhcp6Ins
== NULL
) {
286 return EFI_OUT_OF_RESOURCES
;
290 // Initialize the fields of the new Dhcp6 instance.
292 Dhcp6Ins
->Signature
= DHCP6_INSTANCE_SIGNATURE
;
293 Dhcp6Ins
->UdpSts
= EFI_ALREADY_STARTED
;
294 Dhcp6Ins
->Service
= Service
;
295 Dhcp6Ins
->InDestroy
= FALSE
;
296 Dhcp6Ins
->MediaPresent
= TRUE
;
300 &gDhcp6ProtocolTemplate
,
301 sizeof (EFI_DHCP6_PROTOCOL
)
304 InitializeListHead (&Dhcp6Ins
->TxList
);
305 InitializeListHead (&Dhcp6Ins
->InfList
);
308 // There is a timer for each Dhcp6 instance, which is used to track the
309 // lease time of Ia and the retransmisson time of all sent packets.
311 Status
= gBS
->CreateEvent (
312 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
319 if (EFI_ERROR (Status
)) {
324 *Instance
= Dhcp6Ins
;
330 Callback function which provided by user to remove one node in NetDestroyLinkList process.
332 @param[in] Entry The entry to be removed.
333 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
335 @retval EFI_SUCCESS The entry has been removed successfully.
336 @retval Others Fail to remove the entry.
341 Dhcp6DestroyChildEntry (
342 IN LIST_ENTRY
*Entry
,
346 DHCP6_INSTANCE
*Instance
;
347 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
349 if (Entry
== NULL
|| Context
== NULL
) {
350 return EFI_INVALID_PARAMETER
;
353 Instance
= NET_LIST_USER_STRUCT_S (Entry
, DHCP6_INSTANCE
, Link
, DHCP6_INSTANCE_SIGNATURE
);
354 ServiceBinding
= (EFI_SERVICE_BINDING_PROTOCOL
*) Context
;
356 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
361 Entry point of the DHCP6 driver to install various protocols.
363 @param[in] ImageHandle The handle of the UEFI image file.
364 @param[in] SystemTable The pointer to the EFI System Table.
366 @retval EFI_SUCCESS The operation completed successfully.
367 @retval Others Unexpected error occurs.
372 Dhcp6DriverEntryPoint (
373 IN EFI_HANDLE ImageHandle
,
374 IN EFI_SYSTEM_TABLE
*SystemTable
377 return EfiLibInstallDriverBindingComponentName2 (
380 &gDhcp6DriverBinding
,
382 &gDhcp6ComponentName
,
383 &gDhcp6ComponentName2
389 Test to see if this driver supports ControllerHandle. This service
390 is called by the EFI boot service ConnectController(). In
391 order to make drivers as small as possible, there are a few calling
392 restrictions for this service. ConnectController() must
393 follow these calling restrictions. If any other agent wishes to call
394 Supported() it must also follow these calling restrictions.
396 @param[in] This The pointer to the driver binding protocol.
397 @param[in] ControllerHandle The handle of device to be tested.
398 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
399 device to be started.
401 @retval EFI_SUCCESS This driver supports this device.
402 @retval Others This driver does not support this device.
407 Dhcp6DriverBindingSupported (
408 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
409 IN EFI_HANDLE ControllerHandle
,
410 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
413 return gBS
->OpenProtocol (
415 &gEfiUdp6ServiceBindingProtocolGuid
,
417 This
->DriverBindingHandle
,
419 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
425 Start this driver on ControllerHandle. This service is called by the
426 EFI boot service ConnectController(). In order to make
427 drivers as small as possible, there are a few calling restrictions for
428 this service. ConnectController() must follow these
429 calling restrictions. If any other agent wishes to call Start() it
430 must also follow these calling restrictions.
432 @param[in] This The pointer to the driver binding protocol.
433 @param[in] ControllerHandle The handle of device to be started.
434 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
435 device to be started.
437 @retval EFI_SUCCESS This driver is installed to ControllerHandle.
438 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
439 @retval other This driver does not support this device.
444 Dhcp6DriverBindingStart (
445 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
446 IN EFI_HANDLE ControllerHandle
,
447 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
451 DHCP6_SERVICE
*Service
;
454 // Check the Dhcp6 serivce whether already started.
456 Status
= gBS
->OpenProtocol (
458 &gEfiDhcp6ServiceBindingProtocolGuid
,
460 This
->DriverBindingHandle
,
462 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
465 if (!EFI_ERROR (Status
)) {
466 return EFI_ALREADY_STARTED
;
470 // Create and initialize the Dhcp6 service.
472 Status
= Dhcp6CreateService (
474 This
->DriverBindingHandle
,
478 if (EFI_ERROR (Status
)) {
482 ASSERT (Service
!= NULL
);
484 Status
= gBS
->InstallMultipleProtocolInterfaces (
486 &gEfiDhcp6ServiceBindingProtocolGuid
,
487 &Service
->ServiceBinding
,
491 if (EFI_ERROR (Status
)) {
492 Dhcp6DestroyService (Service
);
501 Stop this driver on ControllerHandle. This service is called by the
502 EFI boot service DisconnectController(). In order to
503 make drivers as small as possible, there are a few calling
504 restrictions for this service. DisconnectController()
505 must follow these calling restrictions. If any other agent wishes
506 to call Stop() it must also follow these calling restrictions.
508 @param[in] This Protocol instance pointer.
509 @param[in] ControllerHandle Handle of device to stop driver on
510 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
511 children is zero stop the entire bus driver.
512 @param[in] ChildHandleBuffer List of Child Handles to Stop.
514 @retval EFI_SUCCESS This driver is removed ControllerHandle
515 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
516 @retval other This driver was not removed from this device
521 Dhcp6DriverBindingStop (
522 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
523 IN EFI_HANDLE ControllerHandle
,
524 IN UINTN NumberOfChildren
,
525 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
529 EFI_HANDLE NicHandle
;
530 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
531 DHCP6_SERVICE
*Service
;
536 // Find and check the Nic handle by the controller handle.
538 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp6ProtocolGuid
);
540 if (NicHandle
== NULL
) {
544 Status
= gBS
->OpenProtocol (
546 &gEfiDhcp6ServiceBindingProtocolGuid
,
547 (VOID
**) &ServiceBinding
,
548 This
->DriverBindingHandle
,
550 EFI_OPEN_PROTOCOL_GET_PROTOCOL
553 if (EFI_ERROR (Status
)) {
557 Service
= DHCP6_SERVICE_FROM_THIS (ServiceBinding
);
558 if (!IsListEmpty (&Service
->Child
)) {
560 // Destroy all the children instances before destory the service.
562 List
= &Service
->Child
;
563 Status
= NetDestroyLinkList (
565 Dhcp6DestroyChildEntry
,
569 if (EFI_ERROR (Status
) || ListLength
!= 0) {
570 Status
= EFI_DEVICE_ERROR
;
574 if (NumberOfChildren
== 0 && !IsListEmpty (&Service
->Child
)) {
575 Status
= EFI_DEVICE_ERROR
;
578 if (NumberOfChildren
== 0 && IsListEmpty (&Service
->Child
)) {
580 // Destroy the service itself if no child instance left.
582 Status
= gBS
->UninstallProtocolInterface (
584 &gEfiDhcp6ServiceBindingProtocolGuid
,
587 if (EFI_ERROR (Status
)) {
591 Dhcp6DestroyService (Service
);
592 Status
= EFI_SUCCESS
;
601 Creates a child handle and installs a protocol.
603 The CreateChild() function installs a protocol on ChildHandle.
604 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
605 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
607 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
608 @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,
609 then a new handle is created. If it is a pointer to an existing
610 UEFI handle, then the protocol is added to the existing UEFI handle.
612 @retval EFI_SUCCES The protocol was added to ChildHandle.
613 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
614 @retval other The child handle was not created.
619 Dhcp6ServiceBindingCreateChild (
620 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
621 IN OUT EFI_HANDLE
*ChildHandle
626 DHCP6_SERVICE
*Service
;
627 DHCP6_INSTANCE
*Instance
;
630 if (This
== NULL
|| ChildHandle
== NULL
) {
631 return EFI_INVALID_PARAMETER
;
634 Service
= DHCP6_SERVICE_FROM_THIS (This
);
636 Status
= Dhcp6CreateInstance (Service
, &Instance
);
638 if (EFI_ERROR (Status
)) {
642 ASSERT (Instance
!= NULL
);
645 // Start the timer when the instance is ready to use.
647 Status
= gBS
->SetTimer (
653 if (EFI_ERROR (Status
)) {
658 // Install the DHCP6 protocol onto ChildHandle.
660 Status
= gBS
->InstallMultipleProtocolInterfaces (
662 &gEfiDhcp6ProtocolGuid
,
667 if (EFI_ERROR (Status
)) {
671 Instance
->Handle
= *ChildHandle
;
674 // Open the UDP6 protocol BY_CHILD.
676 Status
= gBS
->OpenProtocol (
677 Service
->UdpIo
->UdpHandle
,
678 &gEfiUdp6ProtocolGuid
,
680 gDhcp6DriverBinding
.DriverBindingHandle
,
682 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
685 if (EFI_ERROR (Status
)) {
687 gBS
->UninstallMultipleProtocolInterfaces (
689 &gEfiDhcp6ProtocolGuid
,
697 // Add into the children list of its parent service.
699 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
701 InsertTailList (&Service
->Child
, &Instance
->Link
);
702 Service
->NumOfChild
++;
704 gBS
->RestoreTPL (OldTpl
);
709 Dhcp6DestroyInstance (Instance
);
715 Destroys a child handle with a protocol installed on it.
717 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
718 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
719 last protocol on ChildHandle, then ChildHandle is destroyed.
721 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
722 @param[in] ChildHandle Handle of the child to destroy
724 @retval EFI_SUCCES The protocol was removed from ChildHandle.
725 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
726 @retval EFI_INVALID_PARAMETER Child handle is NULL.
727 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
728 because its services are being used.
729 @retval other The child handle was not destroyed
734 Dhcp6ServiceBindingDestroyChild (
735 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
736 IN EFI_HANDLE ChildHandle
741 EFI_DHCP6_PROTOCOL
*Dhcp6
;
742 DHCP6_SERVICE
*Service
;
743 DHCP6_INSTANCE
*Instance
;
745 if (This
== NULL
|| ChildHandle
== NULL
) {
746 return EFI_INVALID_PARAMETER
;
750 // Retrieve the private context data structures
752 Status
= gBS
->OpenProtocol (
754 &gEfiDhcp6ProtocolGuid
,
756 gDhcp6DriverBinding
.DriverBindingHandle
,
758 EFI_OPEN_PROTOCOL_GET_PROTOCOL
761 if (EFI_ERROR (Status
)) {
762 return EFI_UNSUPPORTED
;
765 Instance
= DHCP6_INSTANCE_FROM_THIS (Dhcp6
);
766 Service
= DHCP6_SERVICE_FROM_THIS (This
);
768 if (Instance
->Service
!= Service
) {
769 return EFI_INVALID_PARAMETER
;
772 if (Instance
->InDestroy
) {
776 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
778 Instance
->InDestroy
= TRUE
;
780 Status
= gBS
->CloseProtocol (
781 Service
->UdpIo
->UdpHandle
,
782 &gEfiUdp6ProtocolGuid
,
783 gDhcp6DriverBinding
.DriverBindingHandle
,
787 if (EFI_ERROR (Status
)) {
788 Instance
->InDestroy
= FALSE
;
789 gBS
->RestoreTPL (OldTpl
);
794 // Uninstall the MTFTP6 protocol first to enable a top down destruction.
796 gBS
->RestoreTPL (OldTpl
);
797 Status
= gBS
->UninstallProtocolInterface (
799 &gEfiDhcp6ProtocolGuid
,
802 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
803 if (EFI_ERROR (Status
)) {
804 Instance
->InDestroy
= FALSE
;
805 gBS
->RestoreTPL (OldTpl
);
810 // Remove it from the children list of its parent service.
812 RemoveEntryList (&Instance
->Link
);
813 Service
->NumOfChild
--;
815 gBS
->RestoreTPL (OldTpl
);
817 Dhcp6DestroyInstance (Instance
);