2 Driver Binding functions and Service Binding functions
3 implementation for Dhcp6 Driver.
5 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "Dhcp6Impl.h"
13 EFI_DRIVER_BINDING_PROTOCOL gDhcp6DriverBinding
= {
14 Dhcp6DriverBindingSupported
,
15 Dhcp6DriverBindingStart
,
16 Dhcp6DriverBindingStop
,
22 EFI_SERVICE_BINDING_PROTOCOL gDhcp6ServiceBindingTemplate
= {
23 Dhcp6ServiceBindingCreateChild
,
24 Dhcp6ServiceBindingDestroyChild
28 Configure the default Udp6Io to receive all the DHCP6 traffic
29 on this network interface.
31 @param[in] UdpIo The pointer to Udp6Io to be configured.
32 @param[in] Context The pointer to the context.
34 @retval EFI_SUCCESS The Udp6Io is successfully configured.
35 @retval Others Failed to configure the Udp6Io.
45 EFI_UDP6_PROTOCOL
*Udp6
;
46 EFI_UDP6_CONFIG_DATA
*Config
;
48 Udp6
= UdpIo
->Protocol
.Udp6
;
49 Config
= &(UdpIo
->Config
.Udp6
);
51 ZeroMem (Config
, sizeof (EFI_UDP6_CONFIG_DATA
));
54 // Set Udp6 configure data for the Dhcp6 instance.
56 Config
->AcceptPromiscuous
= FALSE
;
57 Config
->AcceptAnyPort
= FALSE
;
58 Config
->AllowDuplicatePort
= FALSE
;
59 Config
->TrafficClass
= 0;
60 Config
->HopLimit
= 128;
61 Config
->ReceiveTimeout
= 0;
62 Config
->TransmitTimeout
= 0;
65 // Configure an endpoint of client(0, 546), server(0, 0), the addresses
66 // will be overridden later. Note that we MUST not limit RemotePort.
67 // More details, refer to RFC 3315 section 5.2.
69 Config
->StationPort
= DHCP6_PORT_CLIENT
;
70 Config
->RemotePort
= 0;
72 return Udp6
->Configure (Udp6
, Config
);
76 Destroy the Dhcp6 service. The Dhcp6 service may be partly initialized,
77 or partly destroyed. If a resource is destroyed, it is marked as such in
78 case the destroy failed and being called again later.
80 @param[in, out] Service The pointer to Dhcp6 service to be destroyed.
85 IN OUT DHCP6_SERVICE
*Service
89 // All children instances should have been already destroyed here.
91 ASSERT (Service
->NumOfChild
== 0);
93 if (Service
->ClientId
!= NULL
) {
94 FreePool (Service
->ClientId
);
97 if (Service
->UdpIo
!= NULL
) {
98 UdpIoFreeIo (Service
->UdpIo
);
105 Create a new Dhcp6 service for the Nic controller.
107 @param[in] Controller The controller to be installed DHCP6 service
109 @param[in] ImageHandle The image handle of the Dhcp6 driver.
110 @param[out] Service The return pointer of the new Dhcp6 service.
112 @retval EFI_SUCCESS The Dhcp6 service is created successfully.
113 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
114 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
119 IN EFI_HANDLE Controller
,
120 IN EFI_HANDLE ImageHandle
,
121 OUT DHCP6_SERVICE
**Service
124 DHCP6_SERVICE
*Dhcp6Srv
;
128 Dhcp6Srv
= AllocateZeroPool (sizeof (DHCP6_SERVICE
));
130 if (Dhcp6Srv
== NULL
) {
131 return EFI_OUT_OF_RESOURCES
;
135 // Open the SNP protocol to get mode data later.
137 Dhcp6Srv
->Snp
= NULL
;
138 NetLibGetSnpHandle (Controller
, &Dhcp6Srv
->Snp
);
139 if (Dhcp6Srv
->Snp
== NULL
) {
141 return EFI_DEVICE_ERROR
;
145 // Initialize the fields of the new Dhcp6 service.
147 Dhcp6Srv
->Signature
= DHCP6_SERVICE_SIGNATURE
;
148 Dhcp6Srv
->Controller
= Controller
;
149 Dhcp6Srv
->Image
= ImageHandle
;
150 Dhcp6Srv
->Xid
= (0xffffff & NET_RANDOM (NetRandomInitSeed ()));
153 &Dhcp6Srv
->ServiceBinding
,
154 &gDhcp6ServiceBindingTemplate
,
155 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
159 // Locate Ip6->Ip6Config and store it for get IP6 Duplicate Address Detection transmits.
161 Status
= gBS
->HandleProtocol (
163 &gEfiIp6ConfigProtocolGuid
,
164 (VOID
**)&Dhcp6Srv
->Ip6Cfg
166 if (EFI_ERROR (Status
)) {
172 // Generate client Duid: If SMBIOS system UUID is located, generate DUID in DUID-UUID format.
173 // Otherwise, in DUID-LLT format.
175 Dhcp6Srv
->ClientId
= Dhcp6GenerateClientId (Dhcp6Srv
->Snp
->Mode
);
177 if (Dhcp6Srv
->ClientId
== NULL
) {
179 return EFI_DEVICE_ERROR
;
183 // Create an Udp6Io for stateful transmit/receive of each Dhcp6 instance.
185 Dhcp6Srv
->UdpIo
= UdpIoCreateIo (
193 if (Dhcp6Srv
->UdpIo
== NULL
) {
194 FreePool (Dhcp6Srv
->ClientId
);
196 return EFI_DEVICE_ERROR
;
199 InitializeListHead (&Dhcp6Srv
->Child
);
207 Destroy the Dhcp6 instance and recycle the resources.
209 @param[in, out] Instance The pointer to the Dhcp6 instance.
213 Dhcp6DestroyInstance (
214 IN OUT DHCP6_INSTANCE
*Instance
218 // Clean up the retry list first.
220 Dhcp6CleanupRetry (Instance
, DHCP6_PACKET_ALL
);
221 gBS
->CloseEvent (Instance
->Timer
);
224 // Clean up the current configure data.
226 if (Instance
->Config
!= NULL
) {
227 Dhcp6CleanupConfigData (Instance
->Config
);
228 FreePool (Instance
->Config
);
232 // Clean up the current Ia.
234 if (Instance
->IaCb
.Ia
!= NULL
) {
235 if (Instance
->IaCb
.Ia
->ReplyPacket
!= NULL
) {
236 FreePool (Instance
->IaCb
.Ia
->ReplyPacket
);
239 FreePool (Instance
->IaCb
.Ia
);
242 if (Instance
->Unicast
!= NULL
) {
243 FreePool (Instance
->Unicast
);
246 if (Instance
->AdSelect
!= NULL
) {
247 FreePool (Instance
->AdSelect
);
254 Create the Dhcp6 instance and initialize it.
256 @param[in] Service The pointer to the Dhcp6 service.
257 @param[out] Instance The pointer to the Dhcp6 instance.
259 @retval EFI_SUCCESS The Dhcp6 instance is created.
260 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
264 Dhcp6CreateInstance (
265 IN DHCP6_SERVICE
*Service
,
266 OUT DHCP6_INSTANCE
**Instance
270 DHCP6_INSTANCE
*Dhcp6Ins
;
273 Dhcp6Ins
= AllocateZeroPool (sizeof (DHCP6_INSTANCE
));
275 if (Dhcp6Ins
== NULL
) {
276 return EFI_OUT_OF_RESOURCES
;
280 // Initialize the fields of the new Dhcp6 instance.
282 Dhcp6Ins
->Signature
= DHCP6_INSTANCE_SIGNATURE
;
283 Dhcp6Ins
->UdpSts
= EFI_ALREADY_STARTED
;
284 Dhcp6Ins
->Service
= Service
;
285 Dhcp6Ins
->InDestroy
= FALSE
;
286 Dhcp6Ins
->MediaPresent
= TRUE
;
290 &gDhcp6ProtocolTemplate
,
291 sizeof (EFI_DHCP6_PROTOCOL
)
294 InitializeListHead (&Dhcp6Ins
->TxList
);
295 InitializeListHead (&Dhcp6Ins
->InfList
);
298 // There is a timer for each Dhcp6 instance, which is used to track the
299 // lease time of Ia and the retransmission time of all sent packets.
301 Status
= gBS
->CreateEvent (
302 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
309 if (EFI_ERROR (Status
)) {
314 *Instance
= Dhcp6Ins
;
320 Callback function which provided by user to remove one node in NetDestroyLinkList process.
322 @param[in] Entry The entry to be removed.
323 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
325 @retval EFI_SUCCESS The entry has been removed successfully.
326 @retval Others Fail to remove the entry.
331 Dhcp6DestroyChildEntry (
332 IN LIST_ENTRY
*Entry
,
336 DHCP6_INSTANCE
*Instance
;
337 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
339 if ((Entry
== NULL
) || (Context
== NULL
)) {
340 return EFI_INVALID_PARAMETER
;
343 Instance
= NET_LIST_USER_STRUCT_S (Entry
, DHCP6_INSTANCE
, Link
, DHCP6_INSTANCE_SIGNATURE
);
344 ServiceBinding
= (EFI_SERVICE_BINDING_PROTOCOL
*)Context
;
346 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
350 Entry point of the DHCP6 driver to install various protocols.
352 @param[in] ImageHandle The handle of the UEFI image file.
353 @param[in] SystemTable The pointer to the EFI System Table.
355 @retval EFI_SUCCESS The operation completed successfully.
356 @retval Others Unexpected error occurs.
361 Dhcp6DriverEntryPoint (
362 IN EFI_HANDLE ImageHandle
,
363 IN EFI_SYSTEM_TABLE
*SystemTable
366 return EfiLibInstallDriverBindingComponentName2 (
369 &gDhcp6DriverBinding
,
371 &gDhcp6ComponentName
,
372 &gDhcp6ComponentName2
377 Test to see if this driver supports ControllerHandle. This service
378 is called by the EFI boot service ConnectController(). In
379 order to make drivers as small as possible, there are a few calling
380 restrictions for this service. ConnectController() must
381 follow these calling restrictions. If any other agent wishes to call
382 Supported() it must also follow these calling restrictions.
384 @param[in] This The pointer to the driver binding protocol.
385 @param[in] ControllerHandle The handle of device to be tested.
386 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
387 device to be started.
389 @retval EFI_SUCCESS This driver supports this device.
390 @retval Others This driver does not support this device.
395 Dhcp6DriverBindingSupported (
396 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
397 IN EFI_HANDLE ControllerHandle
,
398 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
401 return gBS
->OpenProtocol (
403 &gEfiUdp6ServiceBindingProtocolGuid
,
405 This
->DriverBindingHandle
,
407 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
412 Start this driver on ControllerHandle. This service is called by the
413 EFI boot service ConnectController(). In order to make
414 drivers as small as possible, there are a few calling restrictions for
415 this service. ConnectController() must follow these
416 calling restrictions. If any other agent wishes to call Start() it
417 must also follow these calling restrictions.
419 @param[in] This The pointer to the driver binding protocol.
420 @param[in] ControllerHandle The handle of device to be started.
421 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
422 device to be started.
424 @retval EFI_SUCCESS This driver is installed to ControllerHandle.
425 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
426 @retval other This driver does not support this device.
431 Dhcp6DriverBindingStart (
432 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
433 IN EFI_HANDLE ControllerHandle
,
434 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
438 DHCP6_SERVICE
*Service
;
441 // Check the Dhcp6 service whether already started.
443 Status
= gBS
->OpenProtocol (
445 &gEfiDhcp6ServiceBindingProtocolGuid
,
447 This
->DriverBindingHandle
,
449 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
452 if (!EFI_ERROR (Status
)) {
453 return EFI_ALREADY_STARTED
;
457 // Create and initialize the Dhcp6 service.
459 Status
= Dhcp6CreateService (
461 This
->DriverBindingHandle
,
465 if (EFI_ERROR (Status
)) {
469 ASSERT (Service
!= NULL
);
471 Status
= gBS
->InstallMultipleProtocolInterfaces (
473 &gEfiDhcp6ServiceBindingProtocolGuid
,
474 &Service
->ServiceBinding
,
478 if (EFI_ERROR (Status
)) {
479 Dhcp6DestroyService (Service
);
487 Stop this driver on ControllerHandle. This service is called by the
488 EFI boot service DisconnectController(). In order to
489 make drivers as small as possible, there are a few calling
490 restrictions for this service. DisconnectController()
491 must follow these calling restrictions. If any other agent wishes
492 to call Stop() it must also follow these calling restrictions.
494 @param[in] This Protocol instance pointer.
495 @param[in] ControllerHandle Handle of device to stop driver on
496 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
497 children is zero stop the entire bus driver.
498 @param[in] ChildHandleBuffer List of Child Handles to Stop.
500 @retval EFI_SUCCESS This driver is removed ControllerHandle
501 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
502 @retval other This driver was not removed from this device
507 Dhcp6DriverBindingStop (
508 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
509 IN EFI_HANDLE ControllerHandle
,
510 IN UINTN NumberOfChildren
,
511 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
515 EFI_HANDLE NicHandle
;
516 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
517 DHCP6_SERVICE
*Service
;
522 // Find and check the Nic handle by the controller handle.
524 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp6ProtocolGuid
);
526 if (NicHandle
== NULL
) {
530 Status
= gBS
->OpenProtocol (
532 &gEfiDhcp6ServiceBindingProtocolGuid
,
533 (VOID
**)&ServiceBinding
,
534 This
->DriverBindingHandle
,
536 EFI_OPEN_PROTOCOL_GET_PROTOCOL
539 if (EFI_ERROR (Status
)) {
543 Service
= DHCP6_SERVICE_FROM_THIS (ServiceBinding
);
544 if (!IsListEmpty (&Service
->Child
)) {
546 // Destroy all the children instances before destroy the service.
548 List
= &Service
->Child
;
549 Status
= NetDestroyLinkList (
551 Dhcp6DestroyChildEntry
,
555 if (EFI_ERROR (Status
) || (ListLength
!= 0)) {
556 Status
= EFI_DEVICE_ERROR
;
560 if ((NumberOfChildren
== 0) && !IsListEmpty (&Service
->Child
)) {
561 Status
= EFI_DEVICE_ERROR
;
564 if ((NumberOfChildren
== 0) && IsListEmpty (&Service
->Child
)) {
566 // Destroy the service itself if no child instance left.
568 Status
= gBS
->UninstallProtocolInterface (
570 &gEfiDhcp6ServiceBindingProtocolGuid
,
573 if (EFI_ERROR (Status
)) {
577 Dhcp6DestroyService (Service
);
578 Status
= EFI_SUCCESS
;
586 Creates a child handle and installs a protocol.
588 The CreateChild() function installs a protocol on ChildHandle.
589 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
590 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
592 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
593 @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,
594 then a new handle is created. If it is a pointer to an existing
595 UEFI handle, then the protocol is added to the existing UEFI handle.
597 @retval EFI_SUCCESS The protocol was added to ChildHandle.
598 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
599 @retval other The child handle was not created.
604 Dhcp6ServiceBindingCreateChild (
605 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
606 IN OUT EFI_HANDLE
*ChildHandle
611 DHCP6_SERVICE
*Service
;
612 DHCP6_INSTANCE
*Instance
;
615 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
616 return EFI_INVALID_PARAMETER
;
619 Service
= DHCP6_SERVICE_FROM_THIS (This
);
621 Status
= Dhcp6CreateInstance (Service
, &Instance
);
623 if (EFI_ERROR (Status
)) {
627 ASSERT (Instance
!= NULL
);
630 // Start the timer when the instance is ready to use.
632 Status
= gBS
->SetTimer (
638 if (EFI_ERROR (Status
)) {
643 // Install the DHCP6 protocol onto ChildHandle.
645 Status
= gBS
->InstallMultipleProtocolInterfaces (
647 &gEfiDhcp6ProtocolGuid
,
652 if (EFI_ERROR (Status
)) {
656 Instance
->Handle
= *ChildHandle
;
659 // Open the UDP6 protocol BY_CHILD.
661 Status
= gBS
->OpenProtocol (
662 Service
->UdpIo
->UdpHandle
,
663 &gEfiUdp6ProtocolGuid
,
665 gDhcp6DriverBinding
.DriverBindingHandle
,
667 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
670 if (EFI_ERROR (Status
)) {
671 gBS
->UninstallMultipleProtocolInterfaces (
673 &gEfiDhcp6ProtocolGuid
,
681 // Add into the children list of its parent service.
683 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
685 InsertTailList (&Service
->Child
, &Instance
->Link
);
686 Service
->NumOfChild
++;
688 gBS
->RestoreTPL (OldTpl
);
693 Dhcp6DestroyInstance (Instance
);
698 Destroys a child handle with a protocol installed on it.
700 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
701 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
702 last protocol on ChildHandle, then ChildHandle is destroyed.
704 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
705 @param[in] ChildHandle Handle of the child to destroy
707 @retval EFI_SUCCESS The protocol was removed from ChildHandle.
708 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
709 @retval EFI_INVALID_PARAMETER Child handle is NULL.
710 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
711 because its services are being used.
712 @retval other The child handle was not destroyed
717 Dhcp6ServiceBindingDestroyChild (
718 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
719 IN EFI_HANDLE ChildHandle
724 EFI_DHCP6_PROTOCOL
*Dhcp6
;
725 DHCP6_SERVICE
*Service
;
726 DHCP6_INSTANCE
*Instance
;
728 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
729 return EFI_INVALID_PARAMETER
;
733 // Retrieve the private context data structures
735 Status
= gBS
->OpenProtocol (
737 &gEfiDhcp6ProtocolGuid
,
739 gDhcp6DriverBinding
.DriverBindingHandle
,
741 EFI_OPEN_PROTOCOL_GET_PROTOCOL
744 if (EFI_ERROR (Status
)) {
745 return EFI_UNSUPPORTED
;
748 Instance
= DHCP6_INSTANCE_FROM_THIS (Dhcp6
);
749 Service
= DHCP6_SERVICE_FROM_THIS (This
);
751 if (Instance
->Service
!= Service
) {
752 return EFI_INVALID_PARAMETER
;
755 if (Instance
->InDestroy
) {
759 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
761 Instance
->InDestroy
= TRUE
;
763 Status
= gBS
->CloseProtocol (
764 Service
->UdpIo
->UdpHandle
,
765 &gEfiUdp6ProtocolGuid
,
766 gDhcp6DriverBinding
.DriverBindingHandle
,
770 if (EFI_ERROR (Status
)) {
771 Instance
->InDestroy
= FALSE
;
772 gBS
->RestoreTPL (OldTpl
);
777 // Uninstall the MTFTP6 protocol first to enable a top down destruction.
779 gBS
->RestoreTPL (OldTpl
);
780 Status
= gBS
->UninstallProtocolInterface (
782 &gEfiDhcp6ProtocolGuid
,
785 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
786 if (EFI_ERROR (Status
)) {
787 Instance
->InDestroy
= FALSE
;
788 gBS
->RestoreTPL (OldTpl
);
793 // Remove it from the children list of its parent service.
795 RemoveEntryList (&Instance
->Link
);
796 Service
->NumOfChild
--;
798 gBS
->RestoreTPL (OldTpl
);
800 Dhcp6DestroyInstance (Instance
);