2 Driver Binding functions and Service Binding functions
3 implementationfor for Dhcp6 Driver.
5 Copyright (c) 2009 - 2010, 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
;
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
->InDestory
= FALSE
;
158 Dhcp6Srv
->Controller
= Controller
;
159 Dhcp6Srv
->Image
= ImageHandle
;
160 Dhcp6Srv
->Xid
= (0xffffff & NET_RANDOM (NetRandomInitSeed ()));
163 &Dhcp6Srv
->ServiceBinding
,
164 &gDhcp6ServiceBindingTemplate
,
165 sizeof (EFI_SERVICE_BINDING_PROTOCOL
)
169 // Generate client Duid in the format of Duid-llt.
171 Dhcp6Srv
->ClientId
= Dhcp6GenerateClientId (Dhcp6Srv
->Snp
->Mode
);
173 if (Dhcp6Srv
->ClientId
== NULL
) {
175 return EFI_DEVICE_ERROR
;
179 // Create an Udp6Io for stateful transmit/receive of each Dhcp6 instance.
181 Dhcp6Srv
->UdpIo
= UdpIoCreateIo (
189 if (Dhcp6Srv
->UdpIo
== NULL
) {
190 FreePool (Dhcp6Srv
->ClientId
);
192 return EFI_DEVICE_ERROR
;
195 InitializeListHead (&Dhcp6Srv
->Child
);
204 Destroy the Dhcp6 instance and recycle the resources.
206 @param[in, out] Instance The pointer to the Dhcp6 instance.
210 Dhcp6DestroyInstance (
211 IN OUT DHCP6_INSTANCE
*Instance
215 // Clean up the retry list first.
217 Dhcp6CleanupRetry (Instance
, DHCP6_PACKET_ALL
);
218 gBS
->CloseEvent (Instance
->Timer
);
221 // Clean up the current configure data.
223 if (Instance
->Config
!= NULL
) {
224 Dhcp6CleanupConfigData (Instance
->Config
);
225 FreePool (Instance
->Config
);
229 // Clean up the current Ia.
231 if (Instance
->IaCb
.Ia
!= NULL
) {
232 if (Instance
->IaCb
.Ia
->ReplyPacket
!= NULL
) {
233 FreePool (Instance
->IaCb
.Ia
->ReplyPacket
);
235 FreePool (Instance
->IaCb
.Ia
);
238 if (Instance
->Unicast
!= NULL
) {
239 FreePool (Instance
->Unicast
);
242 if (Instance
->AdSelect
!= NULL
) {
243 FreePool (Instance
->AdSelect
);
251 Create the Dhcp6 instance and initialize it.
253 @param[in] Service The pointer to the Dhcp6 service.
254 @param[out] Instance The pointer to the Dhcp6 instance.
256 @retval EFI_SUCCESS The Dhcp6 instance is created.
257 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
261 Dhcp6CreateInstance (
262 IN DHCP6_SERVICE
*Service
,
263 OUT DHCP6_INSTANCE
**Instance
267 DHCP6_INSTANCE
*Dhcp6Ins
;
270 Dhcp6Ins
= AllocateZeroPool (sizeof (DHCP6_INSTANCE
));
272 if (Dhcp6Ins
== NULL
) {
273 return EFI_OUT_OF_RESOURCES
;
277 // Initialize the fields of the new Dhcp6 instance.
279 Dhcp6Ins
->Signature
= DHCP6_INSTANCE_SIGNATURE
;
280 Dhcp6Ins
->UdpSts
= EFI_ALREADY_STARTED
;
281 Dhcp6Ins
->Service
= Service
;
282 Dhcp6Ins
->InDestory
= FALSE
;
283 Dhcp6Ins
->MediaPresent
= TRUE
;
287 &gDhcp6ProtocolTemplate
,
288 sizeof (EFI_DHCP6_PROTOCOL
)
291 InitializeListHead (&Dhcp6Ins
->TxList
);
292 InitializeListHead (&Dhcp6Ins
->InfList
);
295 // There is a timer for each Dhcp6 instance, which is used to track the
296 // lease time of Ia and the retransmisson time of all sent packets.
298 Status
= gBS
->CreateEvent (
299 EVT_NOTIFY_SIGNAL
| EVT_TIMER
,
306 if (EFI_ERROR (Status
)) {
311 *Instance
= Dhcp6Ins
;
318 Entry point of the DHCP6 driver to install various protocols.
320 @param[in] ImageHandle The handle of the UEFI image file.
321 @param[in] SystemTable The pointer to the EFI System Table.
323 @retval EFI_SUCCESS The operation completed successfully.
324 @retval Others Unexpected error occurs.
329 Dhcp6DriverEntryPoint (
330 IN EFI_HANDLE ImageHandle
,
331 IN EFI_SYSTEM_TABLE
*SystemTable
334 return EfiLibInstallDriverBindingComponentName2 (
337 &gDhcp6DriverBinding
,
339 &gDhcp6ComponentName
,
340 &gDhcp6ComponentName2
346 Test to see if this driver supports ControllerHandle. This service
347 is called by the EFI boot service ConnectController(). In
348 order to make drivers as small as possible, there are a few calling
349 restrictions for this service. ConnectController() must
350 follow these calling restrictions. If any other agent wishes to call
351 Supported() it must also follow these calling restrictions.
353 @param[in] This The pointer to the driver binding protocol.
354 @param[in] ControllerHandle The handle of device to be tested.
355 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
356 device to be started.
358 @retval EFI_SUCCESS This driver supports this device.
359 @retval Others This driver does not support this device.
364 Dhcp6DriverBindingSupported (
365 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
366 IN EFI_HANDLE ControllerHandle
,
367 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
370 return gBS
->OpenProtocol (
372 &gEfiUdp6ServiceBindingProtocolGuid
,
374 This
->DriverBindingHandle
,
376 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
382 Start this driver on ControllerHandle. This service is called by the
383 EFI boot service ConnectController(). In order to make
384 drivers as small as possible, there are a few calling restrictions for
385 this service. ConnectController() must follow these
386 calling restrictions. If any other agent wishes to call Start() it
387 must also follow these calling restrictions.
389 @param[in] This The pointer to the driver binding protocol.
390 @param[in] ControllerHandle The handle of device to be started.
391 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
392 device to be started.
394 @retval EFI_SUCCESS This driver is installed to ControllerHandle.
395 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
396 @retval other This driver does not support this device.
401 Dhcp6DriverBindingStart (
402 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
403 IN EFI_HANDLE ControllerHandle
,
404 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
408 DHCP6_SERVICE
*Service
;
411 // Check the Dhcp6 serivce whether already started.
413 Status
= gBS
->OpenProtocol (
415 &gEfiDhcp6ServiceBindingProtocolGuid
,
417 This
->DriverBindingHandle
,
419 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
422 if (!EFI_ERROR (Status
)) {
423 return EFI_ALREADY_STARTED
;
427 // Create and initialize the Dhcp6 service.
429 Status
= Dhcp6CreateService (
431 This
->DriverBindingHandle
,
435 if (EFI_ERROR (Status
)) {
439 ASSERT (Service
!= NULL
);
441 Status
= gBS
->InstallMultipleProtocolInterfaces (
443 &gEfiDhcp6ServiceBindingProtocolGuid
,
444 &Service
->ServiceBinding
,
448 if (EFI_ERROR (Status
)) {
449 Dhcp6DestroyService (Service
);
458 Stop this driver on ControllerHandle. This service is called by the
459 EFI boot service DisconnectController(). In order to
460 make drivers as small as possible, there are a few calling
461 restrictions for this service. DisconnectController()
462 must follow these calling restrictions. If any other agent wishes
463 to call Stop() it must also follow these calling restrictions.
465 @param[in] This Protocol instance pointer.
466 @param[in] ControllerHandle Handle of device to stop driver on
467 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
468 children is zero stop the entire bus driver.
469 @param[in] ChildHandleBuffer List of Child Handles to Stop.
471 @retval EFI_SUCCESS This driver is removed ControllerHandle
472 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
473 @retval other This driver was not removed from this device
478 Dhcp6DriverBindingStop (
479 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
480 IN EFI_HANDLE ControllerHandle
,
481 IN UINTN NumberOfChildren
,
482 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
487 EFI_HANDLE NicHandle
;
488 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
489 DHCP6_SERVICE
*Service
;
490 DHCP6_INSTANCE
*Instance
;
493 // Find and check the Nic handle by the controller handle.
495 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiUdp6ProtocolGuid
);
497 if (NicHandle
== NULL
) {
498 return EFI_DEVICE_ERROR
;
501 Status
= gBS
->OpenProtocol (
503 &gEfiDhcp6ServiceBindingProtocolGuid
,
504 (VOID
**) &ServiceBinding
,
505 This
->DriverBindingHandle
,
507 EFI_OPEN_PROTOCOL_GET_PROTOCOL
510 if (EFI_ERROR (Status
)) {
514 Service
= DHCP6_SERVICE_FROM_THIS (ServiceBinding
);
516 if (Service
->InDestory
) {
520 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
522 if (NumberOfChildren
== 0) {
524 // Destory the service itself if no child instance left.
526 Service
->InDestory
= TRUE
;
528 Status
= gBS
->UninstallProtocolInterface (
530 &gEfiDhcp6ServiceBindingProtocolGuid
,
534 if (EFI_ERROR (Status
)) {
535 Service
->InDestory
= FALSE
;
539 Dhcp6DestroyService (Service
);
543 // Destory all the children instances before destory the service.
545 while (!IsListEmpty (&Service
->Child
)) {
546 Instance
= NET_LIST_HEAD (&Service
->Child
, DHCP6_INSTANCE
, Link
);
547 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
550 // Any of child failed to be destroyed.
552 if (Service
->NumOfChild
!= 0) {
553 Status
= EFI_DEVICE_ERROR
;
558 gBS
->RestoreTPL (OldTpl
);
564 Creates a child handle and installs a protocol.
566 The CreateChild() function installs a protocol on ChildHandle.
567 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
568 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
570 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
571 @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,
572 then a new handle is created. If it is a pointer to an existing
573 UEFI handle, then the protocol is added to the existing UEFI handle.
575 @retval EFI_SUCCES The protocol was added to ChildHandle.
576 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
577 @retval other The child handle was not created.
582 Dhcp6ServiceBindingCreateChild (
583 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
584 IN OUT EFI_HANDLE
*ChildHandle
589 DHCP6_SERVICE
*Service
;
590 DHCP6_INSTANCE
*Instance
;
593 if (This
== NULL
|| ChildHandle
== NULL
) {
594 return EFI_INVALID_PARAMETER
;
597 Service
= DHCP6_SERVICE_FROM_THIS (This
);
599 Status
= Dhcp6CreateInstance (Service
, &Instance
);
601 if (EFI_ERROR (Status
)) {
605 ASSERT (Instance
!= NULL
);
608 // Start the timer when the instance is ready to use.
610 Status
= gBS
->SetTimer (
616 if (EFI_ERROR (Status
)) {
621 // Install the DHCP6 protocol onto ChildHandle.
623 Status
= gBS
->InstallMultipleProtocolInterfaces (
625 &gEfiDhcp6ProtocolGuid
,
630 if (EFI_ERROR (Status
)) {
634 Instance
->Handle
= *ChildHandle
;
637 // Open the UDP6 protocol BY_CHILD.
639 Status
= gBS
->OpenProtocol (
640 Service
->UdpIo
->UdpHandle
,
641 &gEfiUdp6ProtocolGuid
,
643 gDhcp6DriverBinding
.DriverBindingHandle
,
645 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
648 if (EFI_ERROR (Status
)) {
650 gBS
->UninstallMultipleProtocolInterfaces (
652 &gEfiDhcp6ProtocolGuid
,
660 // Add into the children list of its parent service.
662 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
664 InsertTailList (&Service
->Child
, &Instance
->Link
);
665 Service
->NumOfChild
++;
667 gBS
->RestoreTPL (OldTpl
);
672 Dhcp6DestroyInstance (Instance
);
678 Destroys a child handle with a protocol installed on it.
680 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
681 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
682 last protocol on ChildHandle, then ChildHandle is destroyed.
684 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
685 @param[in] ChildHandle Handle of the child to destroy
687 @retval EFI_SUCCES The protocol was removed from ChildHandle.
688 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
689 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
690 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
691 because its services are being used.
692 @retval other The child handle was not destroyed
697 Dhcp6ServiceBindingDestroyChild (
698 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
699 IN EFI_HANDLE ChildHandle
704 EFI_DHCP6_PROTOCOL
*Dhcp6
;
705 DHCP6_SERVICE
*Service
;
706 DHCP6_INSTANCE
*Instance
;
708 if (This
== NULL
|| ChildHandle
== NULL
) {
709 return EFI_INVALID_PARAMETER
;
713 // Retrieve the private context data structures
715 Status
= gBS
->OpenProtocol (
717 &gEfiDhcp6ProtocolGuid
,
719 gDhcp6DriverBinding
.DriverBindingHandle
,
721 EFI_OPEN_PROTOCOL_GET_PROTOCOL
724 if (EFI_ERROR (Status
)) {
725 return EFI_UNSUPPORTED
;
728 Instance
= DHCP6_INSTANCE_FROM_THIS (Dhcp6
);
729 Service
= DHCP6_SERVICE_FROM_THIS (This
);
731 if (Instance
->Service
!= Service
) {
732 return EFI_INVALID_PARAMETER
;
735 if (Instance
->InDestory
) {
739 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
741 Instance
->InDestory
= TRUE
;
743 Status
= gBS
->CloseProtocol (
744 Service
->UdpIo
->UdpHandle
,
745 &gEfiUdp6ProtocolGuid
,
746 gDhcp6DriverBinding
.DriverBindingHandle
,
750 if (EFI_ERROR (Status
)) {
751 Instance
->InDestory
= FALSE
;
752 gBS
->RestoreTPL (OldTpl
);
757 // Uninstall the MTFTP6 protocol first to enable a top down destruction.
759 Status
= gBS
->UninstallProtocolInterface (
761 &gEfiDhcp6ProtocolGuid
,
765 if (EFI_ERROR (Status
)) {
766 Instance
->InDestory
= FALSE
;
767 gBS
->RestoreTPL (OldTpl
);
772 // Remove it from the children list of its parent service.
774 RemoveEntryList (&Instance
->Link
);
775 Service
->NumOfChild
--;
777 Dhcp6DestroyInstance (Instance
);
779 gBS
->RestoreTPL (OldTpl
);