Dhcp6Driver.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /** @file
  2. Driver Binding functions and Service Binding functions
  3. implementationfor for Dhcp6 Driver.
  4. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Dhcp6Impl.h"
  8. EFI_DRIVER_BINDING_PROTOCOL gDhcp6DriverBinding = {
  9. Dhcp6DriverBindingSupported,
  10. Dhcp6DriverBindingStart,
  11. Dhcp6DriverBindingStop,
  12. 0xa,
  13. NULL,
  14. NULL
  15. };
  16. EFI_SERVICE_BINDING_PROTOCOL gDhcp6ServiceBindingTemplate = {
  17. Dhcp6ServiceBindingCreateChild,
  18. Dhcp6ServiceBindingDestroyChild
  19. };
  20. /**
  21. Configure the default Udp6Io to receive all the DHCP6 traffic
  22. on this network interface.
  23. @param[in] UdpIo The pointer to Udp6Io to be configured.
  24. @param[in] Context The pointer to the context.
  25. @retval EFI_SUCCESS The Udp6Io is successfully configured.
  26. @retval Others Failed to configure the Udp6Io.
  27. **/
  28. EFI_STATUS
  29. EFIAPI
  30. Dhcp6ConfigureUdpIo (
  31. IN UDP_IO *UdpIo,
  32. IN VOID *Context
  33. )
  34. {
  35. EFI_UDP6_PROTOCOL *Udp6;
  36. EFI_UDP6_CONFIG_DATA *Config;
  37. Udp6 = UdpIo->Protocol.Udp6;
  38. Config = &(UdpIo->Config.Udp6);
  39. ZeroMem (Config, sizeof (EFI_UDP6_CONFIG_DATA));
  40. //
  41. // Set Udp6 configure data for the Dhcp6 instance.
  42. //
  43. Config->AcceptPromiscuous = FALSE;
  44. Config->AcceptAnyPort = FALSE;
  45. Config->AllowDuplicatePort = FALSE;
  46. Config->TrafficClass = 0;
  47. Config->HopLimit = 128;
  48. Config->ReceiveTimeout = 0;
  49. Config->TransmitTimeout = 0;
  50. //
  51. // Configure an endpoint of client(0, 546), server(0, 0), the addresses
  52. // will be overridden later. Note that we MUST not limit RemotePort.
  53. // More details, refer to RFC 3315 section 5.2.
  54. //
  55. Config->StationPort = DHCP6_PORT_CLIENT;
  56. Config->RemotePort = 0;
  57. return Udp6->Configure (Udp6, Config);;
  58. }
  59. /**
  60. Destroy the Dhcp6 service. The Dhcp6 service may be partly initialized,
  61. or partly destroyed. If a resource is destroyed, it is marked as such in
  62. case the destroy failed and being called again later.
  63. @param[in, out] Service The pointer to Dhcp6 service to be destroyed.
  64. **/
  65. VOID
  66. Dhcp6DestroyService (
  67. IN OUT DHCP6_SERVICE *Service
  68. )
  69. {
  70. //
  71. // All children instances should have been already destroyed here.
  72. //
  73. ASSERT (Service->NumOfChild == 0);
  74. if (Service->ClientId != NULL) {
  75. FreePool (Service->ClientId);
  76. }
  77. if (Service->UdpIo != NULL) {
  78. UdpIoFreeIo (Service->UdpIo);
  79. }
  80. FreePool (Service);
  81. }
  82. /**
  83. Create a new Dhcp6 service for the Nic controller.
  84. @param[in] Controller The controller to be installed DHCP6 service
  85. binding protocol.
  86. @param[in] ImageHandle The image handle of the Dhcp6 driver.
  87. @param[out] Service The return pointer of the new Dhcp6 service.
  88. @retval EFI_SUCCESS The Dhcp6 service is created successfully.
  89. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  90. @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
  91. **/
  92. EFI_STATUS
  93. Dhcp6CreateService (
  94. IN EFI_HANDLE Controller,
  95. IN EFI_HANDLE ImageHandle,
  96. OUT DHCP6_SERVICE **Service
  97. )
  98. {
  99. DHCP6_SERVICE *Dhcp6Srv;
  100. EFI_STATUS Status;
  101. *Service = NULL;
  102. Dhcp6Srv = AllocateZeroPool (sizeof (DHCP6_SERVICE));
  103. if (Dhcp6Srv == NULL) {
  104. return EFI_OUT_OF_RESOURCES;
  105. }
  106. //
  107. // Open the SNP protocol to get mode data later.
  108. //
  109. Dhcp6Srv->Snp = NULL;
  110. NetLibGetSnpHandle (Controller, &Dhcp6Srv->Snp);
  111. if (Dhcp6Srv->Snp == NULL) {
  112. FreePool (Dhcp6Srv);
  113. return EFI_DEVICE_ERROR;
  114. }
  115. //
  116. // Initialize the fields of the new Dhcp6 service.
  117. //
  118. Dhcp6Srv->Signature = DHCP6_SERVICE_SIGNATURE;
  119. Dhcp6Srv->Controller = Controller;
  120. Dhcp6Srv->Image = ImageHandle;
  121. Dhcp6Srv->Xid = (0xffffff & NET_RANDOM (NetRandomInitSeed ()));
  122. CopyMem (
  123. &Dhcp6Srv->ServiceBinding,
  124. &gDhcp6ServiceBindingTemplate,
  125. sizeof (EFI_SERVICE_BINDING_PROTOCOL)
  126. );
  127. //
  128. // Locate Ip6->Ip6Config and store it for get IP6 Duplicate Address Detection transmits.
  129. //
  130. Status = gBS->HandleProtocol (
  131. Controller,
  132. &gEfiIp6ConfigProtocolGuid,
  133. (VOID **) &Dhcp6Srv->Ip6Cfg
  134. );
  135. if (EFI_ERROR (Status)) {
  136. FreePool (Dhcp6Srv);
  137. return Status;
  138. }
  139. //
  140. // Generate client Duid: If SMBIOS system UUID is located, generate DUID in DUID-UUID format.
  141. // Otherwise, in DUID-LLT format.
  142. //
  143. Dhcp6Srv->ClientId = Dhcp6GenerateClientId (Dhcp6Srv->Snp->Mode);
  144. if (Dhcp6Srv->ClientId == NULL) {
  145. FreePool (Dhcp6Srv);
  146. return EFI_DEVICE_ERROR;
  147. }
  148. //
  149. // Create an Udp6Io for stateful transmit/receive of each Dhcp6 instance.
  150. //
  151. Dhcp6Srv->UdpIo = UdpIoCreateIo (
  152. Controller,
  153. ImageHandle,
  154. Dhcp6ConfigureUdpIo,
  155. UDP_IO_UDP6_VERSION,
  156. NULL
  157. );
  158. if (Dhcp6Srv->UdpIo == NULL) {
  159. FreePool (Dhcp6Srv->ClientId);
  160. FreePool (Dhcp6Srv);
  161. return EFI_DEVICE_ERROR;
  162. }
  163. InitializeListHead (&Dhcp6Srv->Child);
  164. *Service = Dhcp6Srv;
  165. return EFI_SUCCESS;
  166. }
  167. /**
  168. Destroy the Dhcp6 instance and recycle the resources.
  169. @param[in, out] Instance The pointer to the Dhcp6 instance.
  170. **/
  171. VOID
  172. Dhcp6DestroyInstance (
  173. IN OUT DHCP6_INSTANCE *Instance
  174. )
  175. {
  176. //
  177. // Clean up the retry list first.
  178. //
  179. Dhcp6CleanupRetry (Instance, DHCP6_PACKET_ALL);
  180. gBS->CloseEvent (Instance->Timer);
  181. //
  182. // Clean up the current configure data.
  183. //
  184. if (Instance->Config != NULL) {
  185. Dhcp6CleanupConfigData (Instance->Config);
  186. FreePool (Instance->Config);
  187. }
  188. //
  189. // Clean up the current Ia.
  190. //
  191. if (Instance->IaCb.Ia != NULL) {
  192. if (Instance->IaCb.Ia->ReplyPacket != NULL) {
  193. FreePool (Instance->IaCb.Ia->ReplyPacket);
  194. }
  195. FreePool (Instance->IaCb.Ia);
  196. }
  197. if (Instance->Unicast != NULL) {
  198. FreePool (Instance->Unicast);
  199. }
  200. if (Instance->AdSelect != NULL) {
  201. FreePool (Instance->AdSelect);
  202. }
  203. FreePool (Instance);
  204. }
  205. /**
  206. Create the Dhcp6 instance and initialize it.
  207. @param[in] Service The pointer to the Dhcp6 service.
  208. @param[out] Instance The pointer to the Dhcp6 instance.
  209. @retval EFI_SUCCESS The Dhcp6 instance is created.
  210. @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
  211. **/
  212. EFI_STATUS
  213. Dhcp6CreateInstance (
  214. IN DHCP6_SERVICE *Service,
  215. OUT DHCP6_INSTANCE **Instance
  216. )
  217. {
  218. EFI_STATUS Status;
  219. DHCP6_INSTANCE *Dhcp6Ins;
  220. *Instance = NULL;
  221. Dhcp6Ins = AllocateZeroPool (sizeof (DHCP6_INSTANCE));
  222. if (Dhcp6Ins == NULL) {
  223. return EFI_OUT_OF_RESOURCES;
  224. }
  225. //
  226. // Initialize the fields of the new Dhcp6 instance.
  227. //
  228. Dhcp6Ins->Signature = DHCP6_INSTANCE_SIGNATURE;
  229. Dhcp6Ins->UdpSts = EFI_ALREADY_STARTED;
  230. Dhcp6Ins->Service = Service;
  231. Dhcp6Ins->InDestroy = FALSE;
  232. Dhcp6Ins->MediaPresent = TRUE;
  233. CopyMem (
  234. &Dhcp6Ins->Dhcp6,
  235. &gDhcp6ProtocolTemplate,
  236. sizeof (EFI_DHCP6_PROTOCOL)
  237. );
  238. InitializeListHead (&Dhcp6Ins->TxList);
  239. InitializeListHead (&Dhcp6Ins->InfList);
  240. //
  241. // There is a timer for each Dhcp6 instance, which is used to track the
  242. // lease time of Ia and the retransmisson time of all sent packets.
  243. //
  244. Status = gBS->CreateEvent (
  245. EVT_NOTIFY_SIGNAL | EVT_TIMER,
  246. TPL_CALLBACK,
  247. Dhcp6OnTimerTick,
  248. Dhcp6Ins,
  249. &Dhcp6Ins->Timer
  250. );
  251. if (EFI_ERROR (Status)) {
  252. FreePool (Dhcp6Ins);
  253. return Status;
  254. }
  255. *Instance = Dhcp6Ins;
  256. return EFI_SUCCESS;
  257. }
  258. /**
  259. Callback function which provided by user to remove one node in NetDestroyLinkList process.
  260. @param[in] Entry The entry to be removed.
  261. @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
  262. @retval EFI_SUCCESS The entry has been removed successfully.
  263. @retval Others Fail to remove the entry.
  264. **/
  265. EFI_STATUS
  266. EFIAPI
  267. Dhcp6DestroyChildEntry (
  268. IN LIST_ENTRY *Entry,
  269. IN VOID *Context
  270. )
  271. {
  272. DHCP6_INSTANCE *Instance;
  273. EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
  274. if (Entry == NULL || Context == NULL) {
  275. return EFI_INVALID_PARAMETER;
  276. }
  277. Instance = NET_LIST_USER_STRUCT_S (Entry, DHCP6_INSTANCE, Link, DHCP6_INSTANCE_SIGNATURE);
  278. ServiceBinding = (EFI_SERVICE_BINDING_PROTOCOL *) Context;
  279. return ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
  280. }
  281. /**
  282. Entry point of the DHCP6 driver to install various protocols.
  283. @param[in] ImageHandle The handle of the UEFI image file.
  284. @param[in] SystemTable The pointer to the EFI System Table.
  285. @retval EFI_SUCCESS The operation completed successfully.
  286. @retval Others Unexpected error occurs.
  287. **/
  288. EFI_STATUS
  289. EFIAPI
  290. Dhcp6DriverEntryPoint (
  291. IN EFI_HANDLE ImageHandle,
  292. IN EFI_SYSTEM_TABLE *SystemTable
  293. )
  294. {
  295. return EfiLibInstallDriverBindingComponentName2 (
  296. ImageHandle,
  297. SystemTable,
  298. &gDhcp6DriverBinding,
  299. ImageHandle,
  300. &gDhcp6ComponentName,
  301. &gDhcp6ComponentName2
  302. );
  303. }
  304. /**
  305. Test to see if this driver supports ControllerHandle. This service
  306. is called by the EFI boot service ConnectController(). In
  307. order to make drivers as small as possible, there are a few calling
  308. restrictions for this service. ConnectController() must
  309. follow these calling restrictions. If any other agent wishes to call
  310. Supported() it must also follow these calling restrictions.
  311. @param[in] This The pointer to the driver binding protocol.
  312. @param[in] ControllerHandle The handle of device to be tested.
  313. @param[in] RemainingDevicePath Optional parameter use to pick a specific child
  314. device to be started.
  315. @retval EFI_SUCCESS This driver supports this device.
  316. @retval Others This driver does not support this device.
  317. **/
  318. EFI_STATUS
  319. EFIAPI
  320. Dhcp6DriverBindingSupported (
  321. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  322. IN EFI_HANDLE ControllerHandle,
  323. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  324. )
  325. {
  326. return gBS->OpenProtocol (
  327. ControllerHandle,
  328. &gEfiUdp6ServiceBindingProtocolGuid,
  329. NULL,
  330. This->DriverBindingHandle,
  331. ControllerHandle,
  332. EFI_OPEN_PROTOCOL_TEST_PROTOCOL
  333. );
  334. }
  335. /**
  336. Start this driver on ControllerHandle. This service is called by the
  337. EFI boot service ConnectController(). In order to make
  338. drivers as small as possible, there are a few calling restrictions for
  339. this service. ConnectController() must follow these
  340. calling restrictions. If any other agent wishes to call Start() it
  341. must also follow these calling restrictions.
  342. @param[in] This The pointer to the driver binding protocol.
  343. @param[in] ControllerHandle The handle of device to be started.
  344. @param[in] RemainingDevicePath Optional parameter use to pick a specific child
  345. device to be started.
  346. @retval EFI_SUCCESS This driver is installed to ControllerHandle.
  347. @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
  348. @retval other This driver does not support this device.
  349. **/
  350. EFI_STATUS
  351. EFIAPI
  352. Dhcp6DriverBindingStart (
  353. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  354. IN EFI_HANDLE ControllerHandle,
  355. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  356. )
  357. {
  358. EFI_STATUS Status;
  359. DHCP6_SERVICE *Service;
  360. //
  361. // Check the Dhcp6 serivce whether already started.
  362. //
  363. Status = gBS->OpenProtocol (
  364. ControllerHandle,
  365. &gEfiDhcp6ServiceBindingProtocolGuid,
  366. NULL,
  367. This->DriverBindingHandle,
  368. ControllerHandle,
  369. EFI_OPEN_PROTOCOL_TEST_PROTOCOL
  370. );
  371. if (!EFI_ERROR (Status)) {
  372. return EFI_ALREADY_STARTED;
  373. }
  374. //
  375. // Create and initialize the Dhcp6 service.
  376. //
  377. Status = Dhcp6CreateService (
  378. ControllerHandle,
  379. This->DriverBindingHandle,
  380. &Service
  381. );
  382. if (EFI_ERROR (Status)) {
  383. return Status;
  384. }
  385. ASSERT (Service != NULL);
  386. Status = gBS->InstallMultipleProtocolInterfaces (
  387. &ControllerHandle,
  388. &gEfiDhcp6ServiceBindingProtocolGuid,
  389. &Service->ServiceBinding,
  390. NULL
  391. );
  392. if (EFI_ERROR (Status)) {
  393. Dhcp6DestroyService (Service);
  394. return Status;
  395. }
  396. return EFI_SUCCESS;
  397. }
  398. /**
  399. Stop this driver on ControllerHandle. This service is called by the
  400. EFI boot service DisconnectController(). In order to
  401. make drivers as small as possible, there are a few calling
  402. restrictions for this service. DisconnectController()
  403. must follow these calling restrictions. If any other agent wishes
  404. to call Stop() it must also follow these calling restrictions.
  405. @param[in] This Protocol instance pointer.
  406. @param[in] ControllerHandle Handle of device to stop driver on
  407. @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
  408. children is zero stop the entire bus driver.
  409. @param[in] ChildHandleBuffer List of Child Handles to Stop.
  410. @retval EFI_SUCCESS This driver is removed ControllerHandle
  411. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  412. @retval other This driver was not removed from this device
  413. **/
  414. EFI_STATUS
  415. EFIAPI
  416. Dhcp6DriverBindingStop (
  417. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  418. IN EFI_HANDLE ControllerHandle,
  419. IN UINTN NumberOfChildren,
  420. IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
  421. )
  422. {
  423. EFI_STATUS Status;
  424. EFI_HANDLE NicHandle;
  425. EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
  426. DHCP6_SERVICE *Service;
  427. LIST_ENTRY *List;
  428. UINTN ListLength;
  429. //
  430. // Find and check the Nic handle by the controller handle.
  431. //
  432. NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiUdp6ProtocolGuid);
  433. if (NicHandle == NULL) {
  434. return EFI_SUCCESS;
  435. }
  436. Status = gBS->OpenProtocol (
  437. NicHandle,
  438. &gEfiDhcp6ServiceBindingProtocolGuid,
  439. (VOID **) &ServiceBinding,
  440. This->DriverBindingHandle,
  441. NicHandle,
  442. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  443. );
  444. if (EFI_ERROR (Status)) {
  445. return Status;
  446. }
  447. Service = DHCP6_SERVICE_FROM_THIS (ServiceBinding);
  448. if (!IsListEmpty (&Service->Child)) {
  449. //
  450. // Destroy all the children instances before destory the service.
  451. //
  452. List = &Service->Child;
  453. Status = NetDestroyLinkList (
  454. List,
  455. Dhcp6DestroyChildEntry,
  456. ServiceBinding,
  457. &ListLength
  458. );
  459. if (EFI_ERROR (Status) || ListLength != 0) {
  460. Status = EFI_DEVICE_ERROR;
  461. }
  462. }
  463. if (NumberOfChildren == 0 && !IsListEmpty (&Service->Child)) {
  464. Status = EFI_DEVICE_ERROR;
  465. }
  466. if (NumberOfChildren == 0 && IsListEmpty (&Service->Child)) {
  467. //
  468. // Destroy the service itself if no child instance left.
  469. //
  470. Status = gBS->UninstallProtocolInterface (
  471. NicHandle,
  472. &gEfiDhcp6ServiceBindingProtocolGuid,
  473. ServiceBinding
  474. );
  475. if (EFI_ERROR (Status)) {
  476. goto ON_EXIT;
  477. }
  478. Dhcp6DestroyService (Service);
  479. Status = EFI_SUCCESS;
  480. }
  481. ON_EXIT:
  482. return Status;
  483. }
  484. /**
  485. Creates a child handle and installs a protocol.
  486. The CreateChild() function installs a protocol on ChildHandle.
  487. If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
  488. If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
  489. @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  490. @param[in, out] ChildHandle Pointer to the handle of the child to create. If it is NULL,
  491. then a new handle is created. If it is a pointer to an existing
  492. UEFI handle, then the protocol is added to the existing UEFI handle.
  493. @retval EFI_SUCCES The protocol was added to ChildHandle.
  494. @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
  495. @retval other The child handle was not created.
  496. **/
  497. EFI_STATUS
  498. EFIAPI
  499. Dhcp6ServiceBindingCreateChild (
  500. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  501. IN OUT EFI_HANDLE *ChildHandle
  502. )
  503. {
  504. EFI_STATUS Status;
  505. EFI_TPL OldTpl;
  506. DHCP6_SERVICE *Service;
  507. DHCP6_INSTANCE *Instance;
  508. VOID *Udp6;
  509. if (This == NULL || ChildHandle == NULL) {
  510. return EFI_INVALID_PARAMETER;
  511. }
  512. Service = DHCP6_SERVICE_FROM_THIS (This);
  513. Status = Dhcp6CreateInstance (Service, &Instance);
  514. if (EFI_ERROR (Status)) {
  515. return Status;
  516. }
  517. ASSERT (Instance != NULL);
  518. //
  519. // Start the timer when the instance is ready to use.
  520. //
  521. Status = gBS->SetTimer (
  522. Instance->Timer,
  523. TimerPeriodic,
  524. TICKS_PER_SECOND
  525. );
  526. if (EFI_ERROR (Status)) {
  527. goto ON_ERROR;
  528. }
  529. //
  530. // Install the DHCP6 protocol onto ChildHandle.
  531. //
  532. Status = gBS->InstallMultipleProtocolInterfaces (
  533. ChildHandle,
  534. &gEfiDhcp6ProtocolGuid,
  535. &Instance->Dhcp6,
  536. NULL
  537. );
  538. if (EFI_ERROR (Status)) {
  539. goto ON_ERROR;
  540. }
  541. Instance->Handle = *ChildHandle;
  542. //
  543. // Open the UDP6 protocol BY_CHILD.
  544. //
  545. Status = gBS->OpenProtocol (
  546. Service->UdpIo->UdpHandle,
  547. &gEfiUdp6ProtocolGuid,
  548. (VOID **) &Udp6,
  549. gDhcp6DriverBinding.DriverBindingHandle,
  550. Instance->Handle,
  551. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  552. );
  553. if (EFI_ERROR (Status)) {
  554. gBS->UninstallMultipleProtocolInterfaces (
  555. Instance->Handle,
  556. &gEfiDhcp6ProtocolGuid,
  557. &Instance->Dhcp6,
  558. NULL
  559. );
  560. goto ON_ERROR;
  561. }
  562. //
  563. // Add into the children list of its parent service.
  564. //
  565. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  566. InsertTailList (&Service->Child, &Instance->Link);
  567. Service->NumOfChild++;
  568. gBS->RestoreTPL (OldTpl);
  569. return EFI_SUCCESS;
  570. ON_ERROR:
  571. Dhcp6DestroyInstance (Instance);
  572. return Status;
  573. }
  574. /**
  575. Destroys a child handle with a protocol installed on it.
  576. The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
  577. that was installed by CreateChild() from ChildHandle. If the removed protocol is the
  578. last protocol on ChildHandle, then ChildHandle is destroyed.
  579. @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  580. @param[in] ChildHandle Handle of the child to destroy
  581. @retval EFI_SUCCES The protocol was removed from ChildHandle.
  582. @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
  583. @retval EFI_INVALID_PARAMETER Child handle is NULL.
  584. @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
  585. because its services are being used.
  586. @retval other The child handle was not destroyed
  587. **/
  588. EFI_STATUS
  589. EFIAPI
  590. Dhcp6ServiceBindingDestroyChild (
  591. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  592. IN EFI_HANDLE ChildHandle
  593. )
  594. {
  595. EFI_STATUS Status;
  596. EFI_TPL OldTpl;
  597. EFI_DHCP6_PROTOCOL *Dhcp6;
  598. DHCP6_SERVICE *Service;
  599. DHCP6_INSTANCE *Instance;
  600. if (This == NULL || ChildHandle == NULL) {
  601. return EFI_INVALID_PARAMETER;
  602. }
  603. //
  604. // Retrieve the private context data structures
  605. //
  606. Status = gBS->OpenProtocol (
  607. ChildHandle,
  608. &gEfiDhcp6ProtocolGuid,
  609. (VOID **) &Dhcp6,
  610. gDhcp6DriverBinding.DriverBindingHandle,
  611. ChildHandle,
  612. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  613. );
  614. if (EFI_ERROR (Status)) {
  615. return EFI_UNSUPPORTED;
  616. }
  617. Instance = DHCP6_INSTANCE_FROM_THIS (Dhcp6);
  618. Service = DHCP6_SERVICE_FROM_THIS (This);
  619. if (Instance->Service != Service) {
  620. return EFI_INVALID_PARAMETER;
  621. }
  622. if (Instance->InDestroy) {
  623. return EFI_SUCCESS;
  624. }
  625. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  626. Instance->InDestroy = TRUE;
  627. Status = gBS->CloseProtocol (
  628. Service->UdpIo->UdpHandle,
  629. &gEfiUdp6ProtocolGuid,
  630. gDhcp6DriverBinding.DriverBindingHandle,
  631. ChildHandle
  632. );
  633. if (EFI_ERROR (Status)) {
  634. Instance->InDestroy = FALSE;
  635. gBS->RestoreTPL (OldTpl);
  636. return Status;
  637. }
  638. //
  639. // Uninstall the MTFTP6 protocol first to enable a top down destruction.
  640. //
  641. gBS->RestoreTPL (OldTpl);
  642. Status = gBS->UninstallProtocolInterface (
  643. ChildHandle,
  644. &gEfiDhcp6ProtocolGuid,
  645. Dhcp6
  646. );
  647. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  648. if (EFI_ERROR (Status)) {
  649. Instance->InDestroy = FALSE;
  650. gBS->RestoreTPL (OldTpl);
  651. return Status;
  652. }
  653. //
  654. // Remove it from the children list of its parent service.
  655. //
  656. RemoveEntryList (&Instance->Link);
  657. Service->NumOfChild--;
  658. gBS->RestoreTPL (OldTpl);
  659. Dhcp6DestroyInstance (Instance);
  660. return EFI_SUCCESS;
  661. }