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"
14 EFI_DRIVER_BINDING_PROTOCOL gDhcp6DriverBinding
= {
15 Dhcp6DriverBindingSupported
,
16 Dhcp6DriverBindingStart
,
17 Dhcp6DriverBindingStop
,
23 EFI_SERVICE_BINDING_PROTOCOL gDhcp6ServiceBindingTemplate
= {
24 Dhcp6ServiceBindingCreateChild
,
25 Dhcp6ServiceBindingDestroyChild
29 Configure the default Udp6Io to receive all the DHCP6 traffic
30 on this network interface.
32 @param[in] UdpIo The pointer to Udp6Io to be configured.
33 @param[in] Context The pointer to the context.
35 @retval EFI_SUCCESS The Udp6Io is successfully configured.
36 @retval Others Failed to configure the Udp6Io.
46 EFI_UDP6_PROTOCOL
*Udp6
;
47 EFI_UDP6_CONFIG_DATA
*Config
;
49 Udp6
= UdpIo
->Protocol
.Udp6
;
50 Config
= &(UdpIo
->Config
.Udp6
);
52 ZeroMem (Config
, sizeof (EFI_UDP6_CONFIG_DATA
));
55 // Set Udp6 configure data for the Dhcp6 instance.
57 Config
->AcceptPromiscuous
= FALSE
;
58 Config
->AcceptAnyPort
= FALSE
;
59 Config
->AllowDuplicatePort
= FALSE
;
60 Config
->TrafficClass
= 0;
61 Config
->HopLimit
= 128;
62 Config
->ReceiveTimeout
= 0;
63 Config
->TransmitTimeout
= 0;
66 // Configure an endpoint of client(0, 546), server(0, 0), the addresses
67 // will be overridden later. Note that we MUST not limit RemotePort.
68 // More details, refer to RFC 3315 section 5.2.
70 Config
->StationPort
= DHCP6_PORT_CLIENT
;
71 Config
->RemotePort
= 0;
73 return Udp6
->Configure (Udp6
, Config
);;
78 Destroy the Dhcp6 service. The Dhcp6 service may be partly initialized,
79 or partly destroyed. If a resource is destroyed, it is marked as such in
80 case the destroy failed and being called again later.
82 @param[in, out] Service The pointer to Dhcp6 service to be destroyed.
87 IN OUT DHCP6_SERVICE
*Service
91 // All children instances should have been already destroyed here.
93 ASSERT (Service
->NumOfChild
== 0);
95 if (Service
->ClientId
!= NULL
) {
96 FreePool (Service
->ClientId
);
99 if (Service
->UdpIo
!= NULL
) {
100 UdpIoFreeIo (Service
->UdpIo
);
108 Create a new Dhcp6 service for the Nic controller.
110 @param[in] Controller The controller to be installed DHCP6 service
112 @param[in] ImageHandle The image handle of the Dhcp6 driver.
113 @param[out] Service The return pointer of the new Dhcp6 service.
115 @retval EFI_SUCCESS The Dhcp6 service is created successfully.
116 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
117 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
122 IN EFI_HANDLE Controller
,
123 IN EFI_HANDLE ImageHandle
,
124 OUT DHCP6_SERVICE
**Service
127 DHCP6_SERVICE
*Dhcp6Srv
;
131 Dhcp6Srv
= AllocateZeroPool (sizeof (DHCP6_SERVICE
));
133 if (Dhcp6Srv
== NULL
) {
134 return EFI_OUT_OF_RESOURCES
;
138 // Open the SNP protocol to get mode data later.
140 Dhcp6Srv
->Snp
= NULL
;
141 NetLibGetSnpHandle (Controller
, &Dhcp6Srv
->Snp
);
142 if (Dhcp6Srv
->Snp
== NULL
) {
144 return EFI_DEVICE_ERROR
;
148 // Initialize the fields of the new Dhcp6 service.
150 Dhcp6Srv
->Signature
= DHCP6_SERVICE_SIGNATURE
;
151 Dhcp6Srv
->Controller
= Controller
;
152 Dhcp6Srv
->Image
= ImageHandle
;
153 Dhcp6Srv
->Xid
= (0xffffff & NET_RANDOM (NetRandomInitSeed ()));
156 &Dhcp6Srv
->ServiceBinding
,
157 &gDhcp6ServiceBindingTemplate
,
158 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
162 // Locate Ip6->Ip6Config and store it for get IP6 Duplicate Address Detection transmits.
164 Status
= gBS
->HandleProtocol (
166 &gEfiIp6ConfigProtocolGuid
,
167 (VOID
**) &Dhcp6Srv
->Ip6Cfg
169 if (EFI_ERROR (Status
)) {
175 // Generate client Duid: If SMBIOS system UUID is located, generate DUID in DUID-UUID format.
176 // Otherwise, in DUID-LLT format.
178 Dhcp6Srv
->ClientId
= Dhcp6GenerateClientId (Dhcp6Srv
->Snp
->Mode
);
180 if (Dhcp6Srv
->ClientId
== NULL
) {
182 return EFI_DEVICE_ERROR
;
186 // Create an Udp6Io for stateful transmit/receive of each Dhcp6 instance.
188 Dhcp6Srv
->UdpIo
= UdpIoCreateIo (
196 if (Dhcp6Srv
->UdpIo
== NULL
) {
197 FreePool (Dhcp6Srv
->ClientId
);
199 return EFI_DEVICE_ERROR
;
202 InitializeListHead (&Dhcp6Srv
->Child
);
211 Destroy the Dhcp6 instance and recycle the resources.
213 @param[in, out] Instance The pointer to the Dhcp6 instance.
217 Dhcp6DestroyInstance (
218 IN OUT DHCP6_INSTANCE
*Instance
222 // Clean up the retry list first.
224 Dhcp6CleanupRetry (Instance
, DHCP6_PACKET_ALL
);
225 gBS
->CloseEvent (Instance
->Timer
);
228 // Clean up the current configure data.
230 if (Instance
->Config
!= NULL
) {
231 Dhcp6CleanupConfigData (Instance
->Config
);
232 FreePool (Instance
->Config
);
236 // Clean up the current Ia.
238 if (Instance
->IaCb
.Ia
!= NULL
) {
239 if (Instance
->IaCb
.Ia
->ReplyPacket
!= NULL
) {
240 FreePool (Instance
->IaCb
.Ia
->ReplyPacket
);
242 FreePool (Instance
->IaCb
.Ia
);
245 if (Instance
->Unicast
!= NULL
) {
246 FreePool (Instance
->Unicast
);
249 if (Instance
->AdSelect
!= NULL
) {
250 FreePool (Instance
->AdSelect
);
258 Create the Dhcp6 instance and initialize it.
260 @param[in] Service The pointer to the Dhcp6 service.
261 @param[out] Instance The pointer to the Dhcp6 instance.
263 @retval EFI_SUCCESS The Dhcp6 instance is created.
264 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
268 Dhcp6CreateInstance (
269 IN DHCP6_SERVICE
*Service
,
270 OUT DHCP6_INSTANCE
**Instance
274 DHCP6_INSTANCE
*Dhcp6Ins
;
277 Dhcp6Ins
= AllocateZeroPool (sizeof (DHCP6_INSTANCE
));
279 if (Dhcp6Ins
== NULL
) {
280 return EFI_OUT_OF_RESOURCES
;
284 // Initialize the fields of the new Dhcp6 instance.
286 Dhcp6Ins
->Signature
= DHCP6_INSTANCE_SIGNATURE
;
287 Dhcp6Ins
->UdpSts
= EFI_ALREADY_STARTED
;
288 Dhcp6Ins
->Service
= Service
;
289 Dhcp6Ins
->InDestroy
= FALSE
;
290 Dhcp6Ins
->MediaPresent
= TRUE
;
294 &gDhcp6ProtocolTemplate
,
295 sizeof (EFI_DHCP6_PROTOCOL
)
298 InitializeListHead (&Dhcp6Ins
->TxList
);
299 InitializeListHead (&Dhcp6Ins
->InfList
);
302 // There is a timer for each Dhcp6 instance, which is used to track the
303 // lease time of Ia and the retransmission time of all sent packets.
305 Status
= gBS
->CreateEvent (
306 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
313 if (EFI_ERROR (Status
)) {
318 *Instance
= Dhcp6Ins
;
324 Callback function which provided by user to remove one node in NetDestroyLinkList process.
326 @param[in] Entry The entry to be removed.
327 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
329 @retval EFI_SUCCESS The entry has been removed successfully.
330 @retval Others Fail to remove the entry.
335 Dhcp6DestroyChildEntry (
336 IN LIST_ENTRY
*Entry
,
340 DHCP6_INSTANCE
*Instance
;
341 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
343 if (Entry
== NULL
|| Context
== NULL
) {
344 return EFI_INVALID_PARAMETER
;
347 Instance
= NET_LIST_USER_STRUCT_S (Entry
, DHCP6_INSTANCE
, Link
, DHCP6_INSTANCE_SIGNATURE
);
348 ServiceBinding
= (EFI_SERVICE_BINDING_PROTOCOL
*) Context
;
350 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
355 Entry point of the DHCP6 driver to install various protocols.
357 @param[in] ImageHandle The handle of the UEFI image file.
358 @param[in] SystemTable The pointer to the EFI System Table.
360 @retval EFI_SUCCESS The operation completed successfully.
361 @retval Others Unexpected error occurs.
366 Dhcp6DriverEntryPoint (
367 IN EFI_HANDLE ImageHandle
,
368 IN EFI_SYSTEM_TABLE
*SystemTable
371 return EfiLibInstallDriverBindingComponentName2 (
374 &gDhcp6DriverBinding
,
376 &gDhcp6ComponentName
,
377 &gDhcp6ComponentName2
383 Test to see if this driver supports ControllerHandle. This service
384 is called by the EFI boot service ConnectController(). In
385 order to make drivers as small as possible, there are a few calling
386 restrictions for this service. ConnectController() must
387 follow these calling restrictions. If any other agent wishes to call
388 Supported() it must also follow these calling restrictions.
390 @param[in] This The pointer to the driver binding protocol.
391 @param[in] ControllerHandle The handle of device to be tested.
392 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
393 device to be started.
395 @retval EFI_SUCCESS This driver supports this device.
396 @retval Others This driver does not support this device.
401 Dhcp6DriverBindingSupported (
402 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
403 IN EFI_HANDLE ControllerHandle
,
404 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
407 return gBS
->OpenProtocol (
409 &gEfiUdp6ServiceBindingProtocolGuid
,
411 This
->DriverBindingHandle
,
413 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
419 Start this driver on ControllerHandle. This service is called by the
420 EFI boot service ConnectController(). In order to make
421 drivers as small as possible, there are a few calling restrictions for
422 this service. ConnectController() must follow these
423 calling restrictions. If any other agent wishes to call Start() it
424 must also follow these calling restrictions.
426 @param[in] This The pointer to the driver binding protocol.
427 @param[in] ControllerHandle The handle of device to be started.
428 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
429 device to be started.
431 @retval EFI_SUCCESS This driver is installed to ControllerHandle.
432 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
433 @retval other This driver does not support this device.
438 Dhcp6DriverBindingStart (
439 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
440 IN EFI_HANDLE ControllerHandle
,
441 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
445 DHCP6_SERVICE
*Service
;
448 // Check the Dhcp6 service whether already started.
450 Status
= gBS
->OpenProtocol (
452 &gEfiDhcp6ServiceBindingProtocolGuid
,
454 This
->DriverBindingHandle
,
456 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
459 if (!EFI_ERROR (Status
)) {
460 return EFI_ALREADY_STARTED
;
464 // Create and initialize the Dhcp6 service.
466 Status
= Dhcp6CreateService (
468 This
->DriverBindingHandle
,
472 if (EFI_ERROR (Status
)) {
476 ASSERT (Service
!= NULL
);
478 Status
= gBS
->InstallMultipleProtocolInterfaces (
480 &gEfiDhcp6ServiceBindingProtocolGuid
,
481 &Service
->ServiceBinding
,
485 if (EFI_ERROR (Status
)) {
486 Dhcp6DestroyService (Service
);
495 Stop this driver on ControllerHandle. This service is called by the
496 EFI boot service DisconnectController(). In order to
497 make drivers as small as possible, there are a few calling
498 restrictions for this service. DisconnectController()
499 must follow these calling restrictions. If any other agent wishes
500 to call Stop() it must also follow these calling restrictions.
502 @param[in] This Protocol instance pointer.
503 @param[in] ControllerHandle Handle of device to stop driver on
504 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
505 children is zero stop the entire bus driver.
506 @param[in] ChildHandleBuffer List of Child Handles to Stop.
508 @retval EFI_SUCCESS This driver is removed ControllerHandle
509 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
510 @retval other This driver was not removed from this device
515 Dhcp6DriverBindingStop (
516 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
517 IN EFI_HANDLE ControllerHandle
,
518 IN UINTN NumberOfChildren
,
519 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
523 EFI_HANDLE NicHandle
;
524 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
525 DHCP6_SERVICE
*Service
;
530 // Find and check the Nic handle by the controller handle.
532 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp6ProtocolGuid
);
534 if (NicHandle
== NULL
) {
538 Status
= gBS
->OpenProtocol (
540 &gEfiDhcp6ServiceBindingProtocolGuid
,
541 (VOID
**) &ServiceBinding
,
542 This
->DriverBindingHandle
,
544 EFI_OPEN_PROTOCOL_GET_PROTOCOL
547 if (EFI_ERROR (Status
)) {
551 Service
= DHCP6_SERVICE_FROM_THIS (ServiceBinding
);
552 if (!IsListEmpty (&Service
->Child
)) {
554 // Destroy all the children instances before destroy the service.
556 List
= &Service
->Child
;
557 Status
= NetDestroyLinkList (
559 Dhcp6DestroyChildEntry
,
563 if (EFI_ERROR (Status
) || ListLength
!= 0) {
564 Status
= EFI_DEVICE_ERROR
;
568 if (NumberOfChildren
== 0 && !IsListEmpty (&Service
->Child
)) {
569 Status
= EFI_DEVICE_ERROR
;
572 if (NumberOfChildren
== 0 && IsListEmpty (&Service
->Child
)) {
574 // Destroy the service itself if no child instance left.
576 Status
= gBS
->UninstallProtocolInterface (
578 &gEfiDhcp6ServiceBindingProtocolGuid
,
581 if (EFI_ERROR (Status
)) {
585 Dhcp6DestroyService (Service
);
586 Status
= EFI_SUCCESS
;
595 Creates a child handle and installs a protocol.
597 The CreateChild() function installs a protocol on ChildHandle.
598 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
599 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
601 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
602 @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,
603 then a new handle is created. If it is a pointer to an existing
604 UEFI handle, then the protocol is added to the existing UEFI handle.
606 @retval EFI_SUCCESS The protocol was added to ChildHandle.
607 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
608 @retval other The child handle was not created.
613 Dhcp6ServiceBindingCreateChild (
614 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
615 IN OUT EFI_HANDLE
*ChildHandle
620 DHCP6_SERVICE
*Service
;
621 DHCP6_INSTANCE
*Instance
;
624 if (This
== NULL
|| ChildHandle
== NULL
) {
625 return EFI_INVALID_PARAMETER
;
628 Service
= DHCP6_SERVICE_FROM_THIS (This
);
630 Status
= Dhcp6CreateInstance (Service
, &Instance
);
632 if (EFI_ERROR (Status
)) {
636 ASSERT (Instance
!= NULL
);
639 // Start the timer when the instance is ready to use.
641 Status
= gBS
->SetTimer (
647 if (EFI_ERROR (Status
)) {
652 // Install the DHCP6 protocol onto ChildHandle.
654 Status
= gBS
->InstallMultipleProtocolInterfaces (
656 &gEfiDhcp6ProtocolGuid
,
661 if (EFI_ERROR (Status
)) {
665 Instance
->Handle
= *ChildHandle
;
668 // Open the UDP6 protocol BY_CHILD.
670 Status
= gBS
->OpenProtocol (
671 Service
->UdpIo
->UdpHandle
,
672 &gEfiUdp6ProtocolGuid
,
674 gDhcp6DriverBinding
.DriverBindingHandle
,
676 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
679 if (EFI_ERROR (Status
)) {
681 gBS
->UninstallMultipleProtocolInterfaces (
683 &gEfiDhcp6ProtocolGuid
,
691 // Add into the children list of its parent service.
693 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
695 InsertTailList (&Service
->Child
, &Instance
->Link
);
696 Service
->NumOfChild
++;
698 gBS
->RestoreTPL (OldTpl
);
703 Dhcp6DestroyInstance (Instance
);
709 Destroys a child handle with a protocol installed on it.
711 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
712 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
713 last protocol on ChildHandle, then ChildHandle is destroyed.
715 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
716 @param[in] ChildHandle Handle of the child to destroy
718 @retval EFI_SUCCESS The protocol was removed from ChildHandle.
719 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
720 @retval EFI_INVALID_PARAMETER Child handle is NULL.
721 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
722 because its services are being used.
723 @retval other The child handle was not destroyed
728 Dhcp6ServiceBindingDestroyChild (
729 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
730 IN EFI_HANDLE ChildHandle
735 EFI_DHCP6_PROTOCOL
*Dhcp6
;
736 DHCP6_SERVICE
*Service
;
737 DHCP6_INSTANCE
*Instance
;
739 if (This
== NULL
|| ChildHandle
== NULL
) {
740 return EFI_INVALID_PARAMETER
;
744 // Retrieve the private context data structures
746 Status
= gBS
->OpenProtocol (
748 &gEfiDhcp6ProtocolGuid
,
750 gDhcp6DriverBinding
.DriverBindingHandle
,
752 EFI_OPEN_PROTOCOL_GET_PROTOCOL
755 if (EFI_ERROR (Status
)) {
756 return EFI_UNSUPPORTED
;
759 Instance
= DHCP6_INSTANCE_FROM_THIS (Dhcp6
);
760 Service
= DHCP6_SERVICE_FROM_THIS (This
);
762 if (Instance
->Service
!= Service
) {
763 return EFI_INVALID_PARAMETER
;
766 if (Instance
->InDestroy
) {
770 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
772 Instance
->InDestroy
= TRUE
;
774 Status
= gBS
->CloseProtocol (
775 Service
->UdpIo
->UdpHandle
,
776 &gEfiUdp6ProtocolGuid
,
777 gDhcp6DriverBinding
.DriverBindingHandle
,
781 if (EFI_ERROR (Status
)) {
782 Instance
->InDestroy
= FALSE
;
783 gBS
->RestoreTPL (OldTpl
);
788 // Uninstall the MTFTP6 protocol first to enable a top down destruction.
790 gBS
->RestoreTPL (OldTpl
);
791 Status
= gBS
->UninstallProtocolInterface (
793 &gEfiDhcp6ProtocolGuid
,
796 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
797 if (EFI_ERROR (Status
)) {
798 Instance
->InDestroy
= FALSE
;
799 gBS
->RestoreTPL (OldTpl
);
804 // Remove it from the children list of its parent service.
806 RemoveEntryList (&Instance
->Link
);
807 Service
->NumOfChild
--;
809 gBS
->RestoreTPL (OldTpl
);
811 Dhcp6DestroyInstance (Instance
);