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.
340 Dhcp6DestroyChildEntry (
341 IN LIST_ENTRY
*Entry
,
345 DHCP6_INSTANCE
*Instance
;
346 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
348 if (Entry
== NULL
|| Context
== NULL
) {
349 return EFI_INVALID_PARAMETER
;
352 Instance
= NET_LIST_USER_STRUCT_S (Entry
, DHCP6_INSTANCE
, Link
, DHCP6_INSTANCE_SIGNATURE
);
353 ServiceBinding
= (EFI_SERVICE_BINDING_PROTOCOL
*) Context
;
355 return ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
360 Entry point of the DHCP6 driver to install various protocols.
362 @param[in] ImageHandle The handle of the UEFI image file.
363 @param[in] SystemTable The pointer to the EFI System Table.
365 @retval EFI_SUCCESS The operation completed successfully.
366 @retval Others Unexpected error occurs.
371 Dhcp6DriverEntryPoint (
372 IN EFI_HANDLE ImageHandle
,
373 IN EFI_SYSTEM_TABLE
*SystemTable
376 return EfiLibInstallDriverBindingComponentName2 (
379 &gDhcp6DriverBinding
,
381 &gDhcp6ComponentName
,
382 &gDhcp6ComponentName2
388 Test to see if this driver supports ControllerHandle. This service
389 is called by the EFI boot service ConnectController(). In
390 order to make drivers as small as possible, there are a few calling
391 restrictions for this service. ConnectController() must
392 follow these calling restrictions. If any other agent wishes to call
393 Supported() it must also follow these calling restrictions.
395 @param[in] This The pointer to the driver binding protocol.
396 @param[in] ControllerHandle The handle of device to be tested.
397 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
398 device to be started.
400 @retval EFI_SUCCESS This driver supports this device.
401 @retval Others This driver does not support this device.
406 Dhcp6DriverBindingSupported (
407 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
408 IN EFI_HANDLE ControllerHandle
,
409 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
412 return gBS
->OpenProtocol (
414 &gEfiUdp6ServiceBindingProtocolGuid
,
416 This
->DriverBindingHandle
,
418 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
424 Start this driver on ControllerHandle. This service is called by the
425 EFI boot service ConnectController(). In order to make
426 drivers as small as possible, there are a few calling restrictions for
427 this service. ConnectController() must follow these
428 calling restrictions. If any other agent wishes to call Start() it
429 must also follow these calling restrictions.
431 @param[in] This The pointer to the driver binding protocol.
432 @param[in] ControllerHandle The handle of device to be started.
433 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
434 device to be started.
436 @retval EFI_SUCCESS This driver is installed to ControllerHandle.
437 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
438 @retval other This driver does not support this device.
443 Dhcp6DriverBindingStart (
444 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
445 IN EFI_HANDLE ControllerHandle
,
446 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
450 DHCP6_SERVICE
*Service
;
453 // Check the Dhcp6 serivce whether already started.
455 Status
= gBS
->OpenProtocol (
457 &gEfiDhcp6ServiceBindingProtocolGuid
,
459 This
->DriverBindingHandle
,
461 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
464 if (!EFI_ERROR (Status
)) {
465 return EFI_ALREADY_STARTED
;
469 // Create and initialize the Dhcp6 service.
471 Status
= Dhcp6CreateService (
473 This
->DriverBindingHandle
,
477 if (EFI_ERROR (Status
)) {
481 ASSERT (Service
!= NULL
);
483 Status
= gBS
->InstallMultipleProtocolInterfaces (
485 &gEfiDhcp6ServiceBindingProtocolGuid
,
486 &Service
->ServiceBinding
,
490 if (EFI_ERROR (Status
)) {
491 Dhcp6DestroyService (Service
);
500 Stop this driver on ControllerHandle. This service is called by the
501 EFI boot service DisconnectController(). In order to
502 make drivers as small as possible, there are a few calling
503 restrictions for this service. DisconnectController()
504 must follow these calling restrictions. If any other agent wishes
505 to call Stop() it must also follow these calling restrictions.
507 @param[in] This Protocol instance pointer.
508 @param[in] ControllerHandle Handle of device to stop driver on
509 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
510 children is zero stop the entire bus driver.
511 @param[in] ChildHandleBuffer List of Child Handles to Stop.
513 @retval EFI_SUCCESS This driver is removed ControllerHandle
514 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
515 @retval other This driver was not removed from this device
520 Dhcp6DriverBindingStop (
521 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
522 IN EFI_HANDLE ControllerHandle
,
523 IN UINTN NumberOfChildren
,
524 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
528 EFI_HANDLE NicHandle
;
529 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
530 DHCP6_SERVICE
*Service
;
535 // Find and check the Nic handle by the controller handle.
537 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp6ProtocolGuid
);
539 if (NicHandle
== NULL
) {
543 Status
= gBS
->OpenProtocol (
545 &gEfiDhcp6ServiceBindingProtocolGuid
,
546 (VOID
**) &ServiceBinding
,
547 This
->DriverBindingHandle
,
549 EFI_OPEN_PROTOCOL_GET_PROTOCOL
552 if (EFI_ERROR (Status
)) {
556 Service
= DHCP6_SERVICE_FROM_THIS (ServiceBinding
);
557 if (!IsListEmpty (&Service
->Child
)) {
559 // Destroy all the children instances before destory the service.
561 List
= &Service
->Child
;
562 Status
= NetDestroyLinkList (
564 Dhcp6DestroyChildEntry
,
568 if (EFI_ERROR (Status
) || ListLength
!= 0) {
569 Status
= EFI_DEVICE_ERROR
;
573 if (NumberOfChildren
== 0 && !IsListEmpty (&Service
->Child
)) {
574 Status
= EFI_DEVICE_ERROR
;
577 if (NumberOfChildren
== 0 && IsListEmpty (&Service
->Child
)) {
579 // Destroy the service itself if no child instance left.
581 Status
= gBS
->UninstallProtocolInterface (
583 &gEfiDhcp6ServiceBindingProtocolGuid
,
586 if (EFI_ERROR (Status
)) {
590 Dhcp6DestroyService (Service
);
591 Status
= EFI_SUCCESS
;
600 Creates a child handle and installs a protocol.
602 The CreateChild() function installs a protocol on ChildHandle.
603 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
604 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
606 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
607 @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,
608 then a new handle is created. If it is a pointer to an existing
609 UEFI handle, then the protocol is added to the existing UEFI handle.
611 @retval EFI_SUCCES The protocol was added to ChildHandle.
612 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
613 @retval other The child handle was not created.
618 Dhcp6ServiceBindingCreateChild (
619 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
620 IN OUT EFI_HANDLE
*ChildHandle
625 DHCP6_SERVICE
*Service
;
626 DHCP6_INSTANCE
*Instance
;
629 if (This
== NULL
|| ChildHandle
== NULL
) {
630 return EFI_INVALID_PARAMETER
;
633 Service
= DHCP6_SERVICE_FROM_THIS (This
);
635 Status
= Dhcp6CreateInstance (Service
, &Instance
);
637 if (EFI_ERROR (Status
)) {
641 ASSERT (Instance
!= NULL
);
644 // Start the timer when the instance is ready to use.
646 Status
= gBS
->SetTimer (
652 if (EFI_ERROR (Status
)) {
657 // Install the DHCP6 protocol onto ChildHandle.
659 Status
= gBS
->InstallMultipleProtocolInterfaces (
661 &gEfiDhcp6ProtocolGuid
,
666 if (EFI_ERROR (Status
)) {
670 Instance
->Handle
= *ChildHandle
;
673 // Open the UDP6 protocol BY_CHILD.
675 Status
= gBS
->OpenProtocol (
676 Service
->UdpIo
->UdpHandle
,
677 &gEfiUdp6ProtocolGuid
,
679 gDhcp6DriverBinding
.DriverBindingHandle
,
681 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
684 if (EFI_ERROR (Status
)) {
686 gBS
->UninstallMultipleProtocolInterfaces (
688 &gEfiDhcp6ProtocolGuid
,
696 // Add into the children list of its parent service.
698 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
700 InsertTailList (&Service
->Child
, &Instance
->Link
);
701 Service
->NumOfChild
++;
703 gBS
->RestoreTPL (OldTpl
);
708 Dhcp6DestroyInstance (Instance
);
714 Destroys a child handle with a protocol installed on it.
716 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
717 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
718 last protocol on ChildHandle, then ChildHandle is destroyed.
720 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
721 @param[in] ChildHandle Handle of the child to destroy
723 @retval EFI_SUCCES The protocol was removed from ChildHandle.
724 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
725 @retval EFI_INVALID_PARAMETER Child handle is NULL.
726 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
727 because its services are being used.
728 @retval other The child handle was not destroyed
733 Dhcp6ServiceBindingDestroyChild (
734 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
735 IN EFI_HANDLE ChildHandle
740 EFI_DHCP6_PROTOCOL
*Dhcp6
;
741 DHCP6_SERVICE
*Service
;
742 DHCP6_INSTANCE
*Instance
;
744 if (This
== NULL
|| ChildHandle
== NULL
) {
745 return EFI_INVALID_PARAMETER
;
749 // Retrieve the private context data structures
751 Status
= gBS
->OpenProtocol (
753 &gEfiDhcp6ProtocolGuid
,
755 gDhcp6DriverBinding
.DriverBindingHandle
,
757 EFI_OPEN_PROTOCOL_GET_PROTOCOL
760 if (EFI_ERROR (Status
)) {
761 return EFI_UNSUPPORTED
;
764 Instance
= DHCP6_INSTANCE_FROM_THIS (Dhcp6
);
765 Service
= DHCP6_SERVICE_FROM_THIS (This
);
767 if (Instance
->Service
!= Service
) {
768 return EFI_INVALID_PARAMETER
;
771 if (Instance
->InDestroy
) {
775 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
777 Instance
->InDestroy
= TRUE
;
779 Status
= gBS
->CloseProtocol (
780 Service
->UdpIo
->UdpHandle
,
781 &gEfiUdp6ProtocolGuid
,
782 gDhcp6DriverBinding
.DriverBindingHandle
,
786 if (EFI_ERROR (Status
)) {
787 Instance
->InDestroy
= FALSE
;
788 gBS
->RestoreTPL (OldTpl
);
793 // Uninstall the MTFTP6 protocol first to enable a top down destruction.
795 gBS
->RestoreTPL (OldTpl
);
796 Status
= gBS
->UninstallProtocolInterface (
798 &gEfiDhcp6ProtocolGuid
,
801 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
802 if (EFI_ERROR (Status
)) {
803 Instance
->InDestroy
= FALSE
;
804 gBS
->RestoreTPL (OldTpl
);
809 // Remove it from the children list of its parent service.
811 RemoveEntryList (&Instance
->Link
);
812 Service
->NumOfChild
--;
814 gBS
->RestoreTPL (OldTpl
);
816 Dhcp6DestroyInstance (Instance
);