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
36 Configure the default Udp6Io to receive all the DHCP6 traffic
37 on this network interface.
39 @param[in] UdpIo The pointer to Udp6Io to be configured.
40 @param[in] Context The pointer to the context.
42 @retval EFI_SUCCESS The Udp6Io is successfully configured.
43 @retval Others Failed to configure the Udp6Io.
53 EFI_UDP6_PROTOCOL
*Udp6
;
54 EFI_UDP6_CONFIG_DATA
*Config
;
56 Udp6
= UdpIo
->Protocol
.Udp6
;
57 Config
= &(UdpIo
->Config
.Udp6
);
59 ZeroMem (Config
, sizeof (EFI_UDP6_CONFIG_DATA
));
62 // Set Udp6 configure data for the Dhcp6 instance.
64 Config
->AcceptPromiscuous
= FALSE
;
65 Config
->AcceptAnyPort
= FALSE
;
66 Config
->AllowDuplicatePort
= FALSE
;
67 Config
->TrafficClass
= 0;
68 Config
->HopLimit
= 128;
69 Config
->ReceiveTimeout
= 0;
70 Config
->TransmitTimeout
= 0;
73 // Configure an endpoint of client(0, 546), server(0, 0), the addresses
74 // will be overridden later. Note that we MUST not limit RemotePort.
75 // More details, refer to RFC 3315 section 5.2.
77 Config
->StationPort
= DHCP6_PORT_CLIENT
;
78 Config
->RemotePort
= 0;
80 return Udp6
->Configure (Udp6
, Config
);;
85 Destory the Dhcp6 service. The Dhcp6 service may be partly initialized,
86 or partly destroyed. If a resource is destroyed, it is marked as such in
87 case the destroy failed and being called again later.
89 @param[in, out] Service The pointer to Dhcp6 service to be destroyed.
94 IN OUT DHCP6_SERVICE
*Service
98 // All children instances should have been already destoryed here.
100 ASSERT (Service
->NumOfChild
== 0);
102 if (Service
->ClientId
!= NULL
) {
103 FreePool (Service
->ClientId
);
106 if (Service
->UdpIo
!= NULL
) {
107 UdpIoFreeIo (Service
->UdpIo
);
115 Create a new Dhcp6 service for the Nic controller.
117 @param[in] Controller The controller to be installed DHCP6 service
119 @param[in] ImageHandle The image handle of the Dhcp6 driver.
120 @param[out] Service The return pointer of the new Dhcp6 service.
122 @retval EFI_SUCCESS The Dhcp6 service is created successfully.
123 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
124 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
129 IN EFI_HANDLE Controller
,
130 IN EFI_HANDLE ImageHandle
,
131 OUT DHCP6_SERVICE
**Service
134 DHCP6_SERVICE
*Dhcp6Srv
;
138 Dhcp6Srv
= AllocateZeroPool (sizeof (DHCP6_SERVICE
));
140 if (Dhcp6Srv
== NULL
) {
141 return EFI_OUT_OF_RESOURCES
;
145 // Open the SNP protocol to get mode data later.
147 Dhcp6Srv
->Snp
= NULL
;
148 NetLibGetSnpHandle (Controller
, &Dhcp6Srv
->Snp
);
149 if (Dhcp6Srv
->Snp
== NULL
) {
151 return EFI_DEVICE_ERROR
;
155 // Initialize the fields of the new Dhcp6 service.
157 Dhcp6Srv
->Signature
= DHCP6_SERVICE_SIGNATURE
;
158 Dhcp6Srv
->InDestory
= FALSE
;
159 Dhcp6Srv
->Controller
= Controller
;
160 Dhcp6Srv
->Image
= ImageHandle
;
161 Dhcp6Srv
->Xid
= (0xffffff & NET_RANDOM (NetRandomInitSeed ()));
164 &Dhcp6Srv
->ServiceBinding
,
165 &gDhcp6ServiceBindingTemplate
,
166 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
170 // Locate Ip6->Ip6Config and store it for get IP6 Duplicate Address Detection transmits.
172 Status
= gBS
->HandleProtocol (
174 &gEfiIp6ConfigProtocolGuid
,
175 (VOID
**) &Dhcp6Srv
->Ip6Cfg
177 if (EFI_ERROR (Status
)) {
183 // Generate client Duid: If SMBIOS system UUID is located, generate DUID in DUID-UUID format.
184 // Otherwise, in DUID-LLT format.
186 Dhcp6Srv
->ClientId
= Dhcp6GenerateClientId (Dhcp6Srv
->Snp
->Mode
);
188 if (Dhcp6Srv
->ClientId
== NULL
) {
190 return EFI_DEVICE_ERROR
;
194 // Create an Udp6Io for stateful transmit/receive of each Dhcp6 instance.
196 Dhcp6Srv
->UdpIo
= UdpIoCreateIo (
204 if (Dhcp6Srv
->UdpIo
== NULL
) {
205 FreePool (Dhcp6Srv
->ClientId
);
207 return EFI_DEVICE_ERROR
;
210 InitializeListHead (&Dhcp6Srv
->Child
);
219 Destroy the Dhcp6 instance and recycle the resources.
221 @param[in, out] Instance The pointer to the Dhcp6 instance.
225 Dhcp6DestroyInstance (
226 IN OUT DHCP6_INSTANCE
*Instance
230 // Clean up the retry list first.
232 Dhcp6CleanupRetry (Instance
, DHCP6_PACKET_ALL
);
233 gBS
->CloseEvent (Instance
->Timer
);
236 // Clean up the current configure data.
238 if (Instance
->Config
!= NULL
) {
239 Dhcp6CleanupConfigData (Instance
->Config
);
240 FreePool (Instance
->Config
);
244 // Clean up the current Ia.
246 if (Instance
->IaCb
.Ia
!= NULL
) {
247 if (Instance
->IaCb
.Ia
->ReplyPacket
!= NULL
) {
248 FreePool (Instance
->IaCb
.Ia
->ReplyPacket
);
250 FreePool (Instance
->IaCb
.Ia
);
253 if (Instance
->Unicast
!= NULL
) {
254 FreePool (Instance
->Unicast
);
257 if (Instance
->AdSelect
!= NULL
) {
258 FreePool (Instance
->AdSelect
);
266 Create the Dhcp6 instance and initialize it.
268 @param[in] Service The pointer to the Dhcp6 service.
269 @param[out] Instance The pointer to the Dhcp6 instance.
271 @retval EFI_SUCCESS The Dhcp6 instance is created.
272 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
276 Dhcp6CreateInstance (
277 IN DHCP6_SERVICE
*Service
,
278 OUT DHCP6_INSTANCE
**Instance
282 DHCP6_INSTANCE
*Dhcp6Ins
;
285 Dhcp6Ins
= AllocateZeroPool (sizeof (DHCP6_INSTANCE
));
287 if (Dhcp6Ins
== NULL
) {
288 return EFI_OUT_OF_RESOURCES
;
292 // Initialize the fields of the new Dhcp6 instance.
294 Dhcp6Ins
->Signature
= DHCP6_INSTANCE_SIGNATURE
;
295 Dhcp6Ins
->UdpSts
= EFI_ALREADY_STARTED
;
296 Dhcp6Ins
->Service
= Service
;
297 Dhcp6Ins
->InDestory
= FALSE
;
298 Dhcp6Ins
->MediaPresent
= TRUE
;
302 &gDhcp6ProtocolTemplate
,
303 sizeof (EFI_DHCP6_PROTOCOL
)
306 InitializeListHead (&Dhcp6Ins
->TxList
);
307 InitializeListHead (&Dhcp6Ins
->InfList
);
310 // There is a timer for each Dhcp6 instance, which is used to track the
311 // lease time of Ia and the retransmisson time of all sent packets.
313 Status
= gBS
->CreateEvent (
314 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
321 if (EFI_ERROR (Status
)) {
326 *Instance
= Dhcp6Ins
;
333 Entry point of the DHCP6 driver to install various protocols.
335 @param[in] ImageHandle The handle of the UEFI image file.
336 @param[in] SystemTable The pointer to the EFI System Table.
338 @retval EFI_SUCCESS The operation completed successfully.
339 @retval Others Unexpected error occurs.
344 Dhcp6DriverEntryPoint (
345 IN EFI_HANDLE ImageHandle
,
346 IN EFI_SYSTEM_TABLE
*SystemTable
349 return EfiLibInstallDriverBindingComponentName2 (
352 &gDhcp6DriverBinding
,
354 &gDhcp6ComponentName
,
355 &gDhcp6ComponentName2
361 Test to see if this driver supports ControllerHandle. This service
362 is called by the EFI boot service ConnectController(). In
363 order to make drivers as small as possible, there are a few calling
364 restrictions for this service. ConnectController() must
365 follow these calling restrictions. If any other agent wishes to call
366 Supported() it must also follow these calling restrictions.
368 @param[in] This The pointer to the driver binding protocol.
369 @param[in] ControllerHandle The handle of device to be tested.
370 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
371 device to be started.
373 @retval EFI_SUCCESS This driver supports this device.
374 @retval Others This driver does not support this device.
379 Dhcp6DriverBindingSupported (
380 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
381 IN EFI_HANDLE ControllerHandle
,
382 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
385 return gBS
->OpenProtocol (
387 &gEfiUdp6ServiceBindingProtocolGuid
,
389 This
->DriverBindingHandle
,
391 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
397 Start this driver on ControllerHandle. This service is called by the
398 EFI boot service ConnectController(). In order to make
399 drivers as small as possible, there are a few calling restrictions for
400 this service. ConnectController() must follow these
401 calling restrictions. If any other agent wishes to call Start() it
402 must also follow these calling restrictions.
404 @param[in] This The pointer to the driver binding protocol.
405 @param[in] ControllerHandle The handle of device to be started.
406 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
407 device to be started.
409 @retval EFI_SUCCESS This driver is installed to ControllerHandle.
410 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
411 @retval other This driver does not support this device.
416 Dhcp6DriverBindingStart (
417 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
418 IN EFI_HANDLE ControllerHandle
,
419 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
423 DHCP6_SERVICE
*Service
;
426 // Check the Dhcp6 serivce whether already started.
428 Status
= gBS
->OpenProtocol (
430 &gEfiDhcp6ServiceBindingProtocolGuid
,
432 This
->DriverBindingHandle
,
434 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
437 if (!EFI_ERROR (Status
)) {
438 return EFI_ALREADY_STARTED
;
442 // Create and initialize the Dhcp6 service.
444 Status
= Dhcp6CreateService (
446 This
->DriverBindingHandle
,
450 if (EFI_ERROR (Status
)) {
454 ASSERT (Service
!= NULL
);
456 Status
= gBS
->InstallMultipleProtocolInterfaces (
458 &gEfiDhcp6ServiceBindingProtocolGuid
,
459 &Service
->ServiceBinding
,
463 if (EFI_ERROR (Status
)) {
464 Dhcp6DestroyService (Service
);
473 Stop this driver on ControllerHandle. This service is called by the
474 EFI boot service DisconnectController(). In order to
475 make drivers as small as possible, there are a few calling
476 restrictions for this service. DisconnectController()
477 must follow these calling restrictions. If any other agent wishes
478 to call Stop() it must also follow these calling restrictions.
480 @param[in] This Protocol instance pointer.
481 @param[in] ControllerHandle Handle of device to stop driver on
482 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
483 children is zero stop the entire bus driver.
484 @param[in] ChildHandleBuffer List of Child Handles to Stop.
486 @retval EFI_SUCCESS This driver is removed ControllerHandle
487 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
488 @retval other This driver was not removed from this device
493 Dhcp6DriverBindingStop (
494 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
495 IN EFI_HANDLE ControllerHandle
,
496 IN UINTN NumberOfChildren
,
497 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
502 EFI_HANDLE NicHandle
;
503 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
504 DHCP6_SERVICE
*Service
;
505 DHCP6_INSTANCE
*Instance
;
508 // Find and check the Nic handle by the controller handle.
510 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp6ProtocolGuid
);
512 if (NicHandle
== NULL
) {
513 return EFI_DEVICE_ERROR
;
516 Status
= gBS
->OpenProtocol (
518 &gEfiDhcp6ServiceBindingProtocolGuid
,
519 (VOID
**) &ServiceBinding
,
520 This
->DriverBindingHandle
,
522 EFI_OPEN_PROTOCOL_GET_PROTOCOL
525 if (EFI_ERROR (Status
)) {
529 Service
= DHCP6_SERVICE_FROM_THIS (ServiceBinding
);
531 if (Service
->InDestory
) {
535 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
537 if (NumberOfChildren
== 0) {
539 // Destory the service itself if no child instance left.
541 Service
->InDestory
= TRUE
;
543 Status
= gBS
->UninstallProtocolInterface (
545 &gEfiDhcp6ServiceBindingProtocolGuid
,
549 if (EFI_ERROR (Status
)) {
550 Service
->InDestory
= FALSE
;
554 Dhcp6DestroyService (Service
);
558 // Destory all the children instances before destory the service.
560 while (!IsListEmpty (&Service
->Child
)) {
561 Instance
= NET_LIST_HEAD (&Service
->Child
, DHCP6_INSTANCE
, Link
);
562 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
565 // Any of child failed to be destroyed.
567 if (Service
->NumOfChild
!= 0) {
568 Status
= EFI_DEVICE_ERROR
;
573 gBS
->RestoreTPL (OldTpl
);
579 Creates a child handle and installs a protocol.
581 The CreateChild() function installs a protocol on ChildHandle.
582 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
583 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
585 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
586 @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,
587 then a new handle is created. If it is a pointer to an existing
588 UEFI handle, then the protocol is added to the existing UEFI handle.
590 @retval EFI_SUCCES The protocol was added to ChildHandle.
591 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
592 @retval other The child handle was not created.
597 Dhcp6ServiceBindingCreateChild (
598 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
599 IN OUT EFI_HANDLE
*ChildHandle
604 DHCP6_SERVICE
*Service
;
605 DHCP6_INSTANCE
*Instance
;
608 if (This
== NULL
|| ChildHandle
== NULL
) {
609 return EFI_INVALID_PARAMETER
;
612 Service
= DHCP6_SERVICE_FROM_THIS (This
);
614 Status
= Dhcp6CreateInstance (Service
, &Instance
);
616 if (EFI_ERROR (Status
)) {
620 ASSERT (Instance
!= NULL
);
623 // Start the timer when the instance is ready to use.
625 Status
= gBS
->SetTimer (
631 if (EFI_ERROR (Status
)) {
636 // Install the DHCP6 protocol onto ChildHandle.
638 Status
= gBS
->InstallMultipleProtocolInterfaces (
640 &gEfiDhcp6ProtocolGuid
,
645 if (EFI_ERROR (Status
)) {
649 Instance
->Handle
= *ChildHandle
;
652 // Open the UDP6 protocol BY_CHILD.
654 Status
= gBS
->OpenProtocol (
655 Service
->UdpIo
->UdpHandle
,
656 &gEfiUdp6ProtocolGuid
,
658 gDhcp6DriverBinding
.DriverBindingHandle
,
660 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
663 if (EFI_ERROR (Status
)) {
665 gBS
->UninstallMultipleProtocolInterfaces (
667 &gEfiDhcp6ProtocolGuid
,
675 // Add into the children list of its parent service.
677 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
679 InsertTailList (&Service
->Child
, &Instance
->Link
);
680 Service
->NumOfChild
++;
682 gBS
->RestoreTPL (OldTpl
);
687 Dhcp6DestroyInstance (Instance
);
693 Destroys a child handle with a protocol installed on it.
695 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
696 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
697 last protocol on ChildHandle, then ChildHandle is destroyed.
699 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
700 @param[in] ChildHandle Handle of the child to destroy
702 @retval EFI_SUCCES The protocol was removed from ChildHandle.
703 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
704 @retval EFI_INVALID_PARAMETER Child handle is NULL.
705 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
706 because its services are being used.
707 @retval other The child handle was not destroyed
712 Dhcp6ServiceBindingDestroyChild (
713 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
714 IN EFI_HANDLE ChildHandle
719 EFI_DHCP6_PROTOCOL
*Dhcp6
;
720 DHCP6_SERVICE
*Service
;
721 DHCP6_INSTANCE
*Instance
;
723 if (This
== NULL
|| ChildHandle
== NULL
) {
724 return EFI_INVALID_PARAMETER
;
728 // Retrieve the private context data structures
730 Status
= gBS
->OpenProtocol (
732 &gEfiDhcp6ProtocolGuid
,
734 gDhcp6DriverBinding
.DriverBindingHandle
,
736 EFI_OPEN_PROTOCOL_GET_PROTOCOL
739 if (EFI_ERROR (Status
)) {
740 return EFI_UNSUPPORTED
;
743 Instance
= DHCP6_INSTANCE_FROM_THIS (Dhcp6
);
744 Service
= DHCP6_SERVICE_FROM_THIS (This
);
746 if (Instance
->Service
!= Service
) {
747 return EFI_INVALID_PARAMETER
;
750 if (Instance
->InDestory
) {
754 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
756 Instance
->InDestory
= TRUE
;
758 Status
= gBS
->CloseProtocol (
759 Service
->UdpIo
->UdpHandle
,
760 &gEfiUdp6ProtocolGuid
,
761 gDhcp6DriverBinding
.DriverBindingHandle
,
765 if (EFI_ERROR (Status
)) {
766 Instance
->InDestory
= FALSE
;
767 gBS
->RestoreTPL (OldTpl
);
772 // Uninstall the MTFTP6 protocol first to enable a top down destruction.
774 Status
= gBS
->UninstallProtocolInterface (
776 &gEfiDhcp6ProtocolGuid
,
780 if (EFI_ERROR (Status
)) {
781 Instance
->InDestory
= FALSE
;
782 gBS
->RestoreTPL (OldTpl
);
787 // Remove it from the children list of its parent service.
789 RemoveEntryList (&Instance
->Link
);
790 Service
->NumOfChild
--;
792 Dhcp6DestroyInstance (Instance
);
794 gBS
->RestoreTPL (OldTpl
);