HttpDns.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /** @file
  2. Routines for HttpDxe driver to perform DNS resolution based on UEFI DNS protocols.
  3. Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "HttpDriver.h"
  7. /**
  8. Retrieve the host address using the EFI_DNS4_PROTOCOL.
  9. @param[in] HttpInstance Pointer to HTTP_PROTOCOL instance.
  10. @param[in] HostName Pointer to buffer containing hostname.
  11. @param[out] IpAddress On output, pointer to buffer containing IPv4 address.
  12. @retval EFI_SUCCESS Operation succeeded.
  13. @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
  14. @retval EFI_DEVICE_ERROR An unexpected network error occurred.
  15. @retval Others Other errors as indicated.
  16. **/
  17. EFI_STATUS
  18. HttpDns4 (
  19. IN HTTP_PROTOCOL *HttpInstance,
  20. IN CHAR16 *HostName,
  21. OUT EFI_IPv4_ADDRESS *IpAddress
  22. )
  23. {
  24. EFI_STATUS Status;
  25. EFI_DNS4_PROTOCOL *Dns4;
  26. EFI_DNS4_CONFIG_DATA Dns4CfgData;
  27. EFI_DNS4_COMPLETION_TOKEN Token;
  28. BOOLEAN IsDone;
  29. HTTP_SERVICE *Service;
  30. EFI_HANDLE Dns4Handle;
  31. EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
  32. UINTN DnsServerListCount;
  33. EFI_IPv4_ADDRESS *DnsServerList;
  34. UINTN DataSize;
  35. Service = HttpInstance->Service;
  36. ASSERT (Service != NULL);
  37. DnsServerList = NULL;
  38. DnsServerListCount = 0;
  39. ZeroMem (&Token, sizeof (EFI_DNS4_COMPLETION_TOKEN));
  40. //
  41. // Get DNS server list from EFI IPv4 Configuration II protocol.
  42. //
  43. Status = gBS->HandleProtocol (Service->ControllerHandle, &gEfiIp4Config2ProtocolGuid, (VOID **)&Ip4Config2);
  44. if (!EFI_ERROR (Status)) {
  45. //
  46. // Get the required size.
  47. //
  48. DataSize = 0;
  49. Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypeDnsServer, &DataSize, NULL);
  50. if (Status == EFI_BUFFER_TOO_SMALL) {
  51. DnsServerList = AllocatePool (DataSize);
  52. if (DnsServerList == NULL) {
  53. return EFI_OUT_OF_RESOURCES;
  54. }
  55. Status = Ip4Config2->GetData (Ip4Config2, Ip4Config2DataTypeDnsServer, &DataSize, DnsServerList);
  56. if (EFI_ERROR (Status)) {
  57. FreePool (DnsServerList);
  58. DnsServerList = NULL;
  59. } else {
  60. DnsServerListCount = DataSize / sizeof (EFI_IPv4_ADDRESS);
  61. }
  62. }
  63. }
  64. Dns4Handle = NULL;
  65. Dns4 = NULL;
  66. //
  67. // Create a DNS child instance and get the protocol.
  68. //
  69. Status = NetLibCreateServiceChild (
  70. Service->ControllerHandle,
  71. Service->Ip4DriverBindingHandle,
  72. &gEfiDns4ServiceBindingProtocolGuid,
  73. &Dns4Handle
  74. );
  75. if (EFI_ERROR (Status)) {
  76. goto Exit;
  77. }
  78. Status = gBS->OpenProtocol (
  79. Dns4Handle,
  80. &gEfiDns4ProtocolGuid,
  81. (VOID **)&Dns4,
  82. Service->Ip4DriverBindingHandle,
  83. Service->ControllerHandle,
  84. EFI_OPEN_PROTOCOL_BY_DRIVER
  85. );
  86. if (EFI_ERROR (Status)) {
  87. goto Exit;
  88. }
  89. //
  90. // Configure DNS4 instance for the DNS server address and protocol.
  91. //
  92. ZeroMem (&Dns4CfgData, sizeof (Dns4CfgData));
  93. Dns4CfgData.DnsServerListCount = DnsServerListCount;
  94. Dns4CfgData.DnsServerList = DnsServerList;
  95. Dns4CfgData.UseDefaultSetting = HttpInstance->IPv4Node.UseDefaultAddress;
  96. Dns4CfgData.RetryInterval = PcdGet32 (PcdHttpDnsRetryInterval);
  97. Dns4CfgData.RetryCount = PcdGet32 (PcdHttpDnsRetryCount);
  98. if (!Dns4CfgData.UseDefaultSetting) {
  99. IP4_COPY_ADDRESS (&Dns4CfgData.StationIp, &HttpInstance->IPv4Node.LocalAddress);
  100. IP4_COPY_ADDRESS (&Dns4CfgData.SubnetMask, &HttpInstance->IPv4Node.LocalSubnet);
  101. }
  102. Dns4CfgData.EnableDnsCache = TRUE;
  103. Dns4CfgData.Protocol = EFI_IP_PROTO_UDP;
  104. Status = Dns4->Configure (
  105. Dns4,
  106. &Dns4CfgData
  107. );
  108. if (EFI_ERROR (Status)) {
  109. goto Exit;
  110. }
  111. //
  112. // Create event to set the is done flag when name resolution is finished.
  113. //
  114. ZeroMem (&Token, sizeof (Token));
  115. Status = gBS->CreateEvent (
  116. EVT_NOTIFY_SIGNAL,
  117. TPL_NOTIFY,
  118. HttpCommonNotify,
  119. &IsDone,
  120. &Token.Event
  121. );
  122. if (EFI_ERROR (Status)) {
  123. goto Exit;
  124. }
  125. //
  126. // Start asynchronous name resolution.
  127. //
  128. Token.Status = EFI_NOT_READY;
  129. IsDone = FALSE;
  130. Status = Dns4->HostNameToIp (Dns4, HostName, &Token);
  131. if (EFI_ERROR (Status)) {
  132. goto Exit;
  133. }
  134. while (!IsDone) {
  135. Dns4->Poll (Dns4);
  136. }
  137. //
  138. // Name resolution is done, check result.
  139. //
  140. Status = Token.Status;
  141. if (!EFI_ERROR (Status)) {
  142. if (Token.RspData.H2AData == NULL) {
  143. Status = EFI_DEVICE_ERROR;
  144. goto Exit;
  145. }
  146. if ((Token.RspData.H2AData->IpCount == 0) || (Token.RspData.H2AData->IpList == NULL)) {
  147. Status = EFI_DEVICE_ERROR;
  148. goto Exit;
  149. }
  150. //
  151. // We just return the first IP address from DNS protocol.
  152. //
  153. IP4_COPY_ADDRESS (IpAddress, Token.RspData.H2AData->IpList);
  154. Status = EFI_SUCCESS;
  155. }
  156. Exit:
  157. if (Token.Event != NULL) {
  158. gBS->CloseEvent (Token.Event);
  159. }
  160. if (Token.RspData.H2AData != NULL) {
  161. if (Token.RspData.H2AData->IpList != NULL) {
  162. FreePool (Token.RspData.H2AData->IpList);
  163. }
  164. FreePool (Token.RspData.H2AData);
  165. }
  166. if (Dns4 != NULL) {
  167. Dns4->Configure (Dns4, NULL);
  168. gBS->CloseProtocol (
  169. Dns4Handle,
  170. &gEfiDns4ProtocolGuid,
  171. Service->Ip4DriverBindingHandle,
  172. Service->ControllerHandle
  173. );
  174. }
  175. if (Dns4Handle != NULL) {
  176. NetLibDestroyServiceChild (
  177. Service->ControllerHandle,
  178. Service->Ip4DriverBindingHandle,
  179. &gEfiDns4ServiceBindingProtocolGuid,
  180. Dns4Handle
  181. );
  182. }
  183. if (DnsServerList != NULL) {
  184. FreePool (DnsServerList);
  185. }
  186. return Status;
  187. }
  188. /**
  189. Retrieve the host address using the EFI_DNS6_PROTOCOL.
  190. @param[in] HttpInstance Pointer to HTTP_PROTOCOL instance.
  191. @param[in] HostName Pointer to buffer containing hostname.
  192. @param[out] IpAddress On output, pointer to buffer containing IPv6 address.
  193. @retval EFI_SUCCESS Operation succeeded.
  194. @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
  195. @retval EFI_DEVICE_ERROR An unexpected network error occurred.
  196. @retval Others Other errors as indicated.
  197. **/
  198. EFI_STATUS
  199. HttpDns6 (
  200. IN HTTP_PROTOCOL *HttpInstance,
  201. IN CHAR16 *HostName,
  202. OUT EFI_IPv6_ADDRESS *IpAddress
  203. )
  204. {
  205. EFI_STATUS Status;
  206. HTTP_SERVICE *Service;
  207. EFI_DNS6_PROTOCOL *Dns6;
  208. EFI_DNS6_CONFIG_DATA Dns6ConfigData;
  209. EFI_DNS6_COMPLETION_TOKEN Token;
  210. EFI_HANDLE Dns6Handle;
  211. EFI_IP6_CONFIG_PROTOCOL *Ip6Config;
  212. EFI_IPv6_ADDRESS *DnsServerList;
  213. UINTN DnsServerListCount;
  214. UINTN DataSize;
  215. BOOLEAN IsDone;
  216. Service = HttpInstance->Service;
  217. ASSERT (Service != NULL);
  218. DnsServerList = NULL;
  219. DnsServerListCount = 0;
  220. Dns6 = NULL;
  221. Dns6Handle = NULL;
  222. ZeroMem (&Token, sizeof (EFI_DNS6_COMPLETION_TOKEN));
  223. //
  224. // Get DNS server list from EFI IPv6 Configuration protocol.
  225. //
  226. Status = gBS->HandleProtocol (Service->ControllerHandle, &gEfiIp6ConfigProtocolGuid, (VOID **)&Ip6Config);
  227. if (!EFI_ERROR (Status)) {
  228. //
  229. // Get the required size.
  230. //
  231. DataSize = 0;
  232. Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, NULL);
  233. if (Status == EFI_BUFFER_TOO_SMALL) {
  234. DnsServerList = AllocatePool (DataSize);
  235. if (DnsServerList == NULL) {
  236. return EFI_OUT_OF_RESOURCES;
  237. }
  238. Status = Ip6Config->GetData (Ip6Config, Ip6ConfigDataTypeDnsServer, &DataSize, DnsServerList);
  239. if (EFI_ERROR (Status)) {
  240. FreePool (DnsServerList);
  241. DnsServerList = NULL;
  242. } else {
  243. DnsServerListCount = DataSize / sizeof (EFI_IPv6_ADDRESS);
  244. }
  245. }
  246. }
  247. //
  248. // Create a DNSv6 child instance and get the protocol.
  249. //
  250. Status = NetLibCreateServiceChild (
  251. Service->ControllerHandle,
  252. Service->Ip6DriverBindingHandle,
  253. &gEfiDns6ServiceBindingProtocolGuid,
  254. &Dns6Handle
  255. );
  256. if (EFI_ERROR (Status)) {
  257. goto Exit;
  258. }
  259. Status = gBS->OpenProtocol (
  260. Dns6Handle,
  261. &gEfiDns6ProtocolGuid,
  262. (VOID **)&Dns6,
  263. Service->Ip6DriverBindingHandle,
  264. Service->ControllerHandle,
  265. EFI_OPEN_PROTOCOL_BY_DRIVER
  266. );
  267. if (EFI_ERROR (Status)) {
  268. goto Exit;
  269. }
  270. //
  271. // Configure DNS6 instance for the DNS server address and protocol.
  272. //
  273. ZeroMem (&Dns6ConfigData, sizeof (EFI_DNS6_CONFIG_DATA));
  274. Dns6ConfigData.DnsServerCount = (UINT32)DnsServerListCount;
  275. Dns6ConfigData.DnsServerList = DnsServerList;
  276. Dns6ConfigData.EnableDnsCache = TRUE;
  277. Dns6ConfigData.Protocol = EFI_IP_PROTO_UDP;
  278. Dns6ConfigData.RetryInterval = PcdGet32 (PcdHttpDnsRetryInterval);
  279. Dns6ConfigData.RetryCount = PcdGet32 (PcdHttpDnsRetryCount);
  280. IP6_COPY_ADDRESS (&Dns6ConfigData.StationIp, &HttpInstance->Ipv6Node.LocalAddress);
  281. Status = Dns6->Configure (
  282. Dns6,
  283. &Dns6ConfigData
  284. );
  285. if (EFI_ERROR (Status)) {
  286. goto Exit;
  287. }
  288. Token.Status = EFI_NOT_READY;
  289. IsDone = FALSE;
  290. //
  291. // Create event to set the IsDone flag when name resolution is finished.
  292. //
  293. Status = gBS->CreateEvent (
  294. EVT_NOTIFY_SIGNAL,
  295. TPL_NOTIFY,
  296. HttpCommonNotify,
  297. &IsDone,
  298. &Token.Event
  299. );
  300. if (EFI_ERROR (Status)) {
  301. goto Exit;
  302. }
  303. //
  304. // Start asynchronous name resolution.
  305. //
  306. Status = Dns6->HostNameToIp (Dns6, HostName, &Token);
  307. if (EFI_ERROR (Status)) {
  308. goto Exit;
  309. }
  310. while (!IsDone) {
  311. Dns6->Poll (Dns6);
  312. }
  313. //
  314. // Name resolution is done, check result.
  315. //
  316. Status = Token.Status;
  317. if (!EFI_ERROR (Status)) {
  318. if (Token.RspData.H2AData == NULL) {
  319. Status = EFI_DEVICE_ERROR;
  320. goto Exit;
  321. }
  322. if ((Token.RspData.H2AData->IpCount == 0) || (Token.RspData.H2AData->IpList == NULL)) {
  323. Status = EFI_DEVICE_ERROR;
  324. goto Exit;
  325. }
  326. //
  327. // We just return the first IPv6 address from DNS protocol.
  328. //
  329. IP6_COPY_ADDRESS (IpAddress, Token.RspData.H2AData->IpList);
  330. Status = EFI_SUCCESS;
  331. }
  332. Exit:
  333. if (Token.Event != NULL) {
  334. gBS->CloseEvent (Token.Event);
  335. }
  336. if (Token.RspData.H2AData != NULL) {
  337. if (Token.RspData.H2AData->IpList != NULL) {
  338. FreePool (Token.RspData.H2AData->IpList);
  339. }
  340. FreePool (Token.RspData.H2AData);
  341. }
  342. if (Dns6 != NULL) {
  343. Dns6->Configure (Dns6, NULL);
  344. gBS->CloseProtocol (
  345. Dns6Handle,
  346. &gEfiDns6ProtocolGuid,
  347. Service->Ip6DriverBindingHandle,
  348. Service->ControllerHandle
  349. );
  350. }
  351. if (Dns6Handle != NULL) {
  352. NetLibDestroyServiceChild (
  353. Service->ControllerHandle,
  354. Service->Ip6DriverBindingHandle,
  355. &gEfiDns6ServiceBindingProtocolGuid,
  356. Dns6Handle
  357. );
  358. }
  359. if (DnsServerList != NULL) {
  360. FreePool (DnsServerList);
  361. }
  362. return Status;
  363. }