2 Perform DNS resolution based on UEFI DNS protocols.
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 Notify the callback function when an event is triggered.
14 @param[in] Event The triggered event.
15 @param[in] Context The opaque parameter to the function.
25 *((BOOLEAN
*)Context
) = TRUE
;
29 Retrieve the host address using the EFI_DNS4_PROTOCOL.
31 @param[in] Image The handle of the driver image.
32 @param[in] Controller The handle of the controller.
33 @param[in, out] NvData The Session config data structure.
35 @retval EFI_SUCCESS Operation succeeded.
36 @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
37 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
38 @retval Others Other errors as indicated.
44 IN EFI_HANDLE Controller
,
45 IN OUT ISCSI_SESSION_CONFIG_NVDATA
*NvData
49 EFI_DNS4_PROTOCOL
*Dns4
;
50 EFI_DNS4_CONFIG_DATA Dns4CfgData
;
51 EFI_DNS4_COMPLETION_TOKEN Token
;
53 EFI_HANDLE Dns4Handle
;
54 EFI_IP4_CONFIG2_PROTOCOL
*Ip4Config2
;
55 EFI_IPv4_ADDRESS
*DnsServerList
;
56 UINTN DnsServerListCount
;
61 DnsServerListCount
= 0;
64 ZeroMem (&Token
, sizeof (EFI_DNS4_COMPLETION_TOKEN
));
67 // Get DNS server list from EFI IPv4 Configuration II protocol.
69 Status
= gBS
->HandleProtocol (Controller
, &gEfiIp4Config2ProtocolGuid
, (VOID
**)&Ip4Config2
);
70 if (!EFI_ERROR (Status
)) {
72 // Get the required size.
75 Status
= Ip4Config2
->GetData (Ip4Config2
, Ip4Config2DataTypeDnsServer
, &DataSize
, NULL
);
76 if (Status
== EFI_BUFFER_TOO_SMALL
) {
77 DnsServerList
= AllocatePool (DataSize
);
78 if (DnsServerList
== NULL
) {
79 return EFI_OUT_OF_RESOURCES
;
82 Status
= Ip4Config2
->GetData (Ip4Config2
, Ip4Config2DataTypeDnsServer
, &DataSize
, DnsServerList
);
83 if (EFI_ERROR (Status
)) {
84 FreePool (DnsServerList
);
87 DnsServerListCount
= DataSize
/ sizeof (EFI_IPv4_ADDRESS
);
93 // Create a DNS child instance and get the protocol.
95 Status
= NetLibCreateServiceChild (
98 &gEfiDns4ServiceBindingProtocolGuid
,
101 if (EFI_ERROR (Status
)) {
105 Status
= gBS
->OpenProtocol (
107 &gEfiDns4ProtocolGuid
,
111 EFI_OPEN_PROTOCOL_BY_DRIVER
113 if (EFI_ERROR (Status
)) {
118 // Configure DNS4 instance for the DNS server address and protocol.
120 ZeroMem (&Dns4CfgData
, sizeof (Dns4CfgData
));
121 Dns4CfgData
.DnsServerListCount
= DnsServerListCount
;
122 Dns4CfgData
.DnsServerList
= DnsServerList
;
123 Dns4CfgData
.EnableDnsCache
= TRUE
;
124 IP4_COPY_ADDRESS (&Dns4CfgData
.StationIp
, &NvData
->LocalIp
);
125 IP4_COPY_ADDRESS (&Dns4CfgData
.SubnetMask
, &NvData
->SubnetMask
);
126 Dns4CfgData
.Protocol
= EFI_IP_PROTO_UDP
;
127 Status
= Dns4
->Configure (
131 if (EFI_ERROR (Status
)) {
136 // Create event to set the is done flag when name resolution is finished.
138 ZeroMem (&Token
, sizeof (Token
));
139 Status
= gBS
->CreateEvent (
146 if (EFI_ERROR (Status
)) {
151 // Start asynchronous name resolution.
153 Token
.Status
= EFI_NOT_READY
;
156 HostName
= (CHAR16
*)AllocateZeroPool (ISCSI_NAME_MAX_SIZE
);
157 if (HostName
== NULL
) {
158 return EFI_OUT_OF_RESOURCES
;
161 AsciiStrToUnicodeStrS (
167 Status
= Dns4
->HostNameToIp (Dns4
, HostName
, &Token
);
168 if (EFI_ERROR (Status
)) {
177 // Name resolution is done, check result.
179 Status
= Token
.Status
;
180 if (!EFI_ERROR (Status
)) {
181 if (Token
.RspData
.H2AData
== NULL
) {
182 Status
= EFI_DEVICE_ERROR
;
186 if ((Token
.RspData
.H2AData
->IpCount
== 0) || (Token
.RspData
.H2AData
->IpList
== NULL
)) {
187 Status
= EFI_DEVICE_ERROR
;
192 // We just return the first IP address from DNS protocol.
194 IP4_COPY_ADDRESS (&NvData
->TargetIp
.v4
, Token
.RspData
.H2AData
->IpList
);
195 Status
= EFI_SUCCESS
;
200 if (Token
.Event
!= NULL
) {
201 gBS
->CloseEvent (Token
.Event
);
204 if (Token
.RspData
.H2AData
!= NULL
) {
205 if (Token
.RspData
.H2AData
->IpList
!= NULL
) {
206 FreePool (Token
.RspData
.H2AData
->IpList
);
209 FreePool (Token
.RspData
.H2AData
);
213 Dns4
->Configure (Dns4
, NULL
);
217 &gEfiDns4ProtocolGuid
,
223 if (Dns4Handle
!= NULL
) {
224 NetLibDestroyServiceChild (
227 &gEfiDns4ServiceBindingProtocolGuid
,
236 Retrieve the host address using the EFI_DNS6_PROTOCOL.
238 @param[in] Image The handle of the driver image.
239 @param[in] Controller The handle of the controller.
240 @param[in, out] NvData The Session config data structure.
242 @retval EFI_SUCCESS Operation succeeded.
243 @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
244 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
245 @retval Others Other errors as indicated.
251 IN EFI_HANDLE Controller
,
252 IN OUT ISCSI_SESSION_CONFIG_NVDATA
*NvData
256 EFI_DNS6_PROTOCOL
*Dns6
;
257 EFI_DNS6_CONFIG_DATA Dns6ConfigData
;
258 EFI_DNS6_COMPLETION_TOKEN Token
;
259 EFI_HANDLE Dns6Handle
;
260 EFI_IP6_CONFIG_PROTOCOL
*Ip6Config
;
261 EFI_IPv6_ADDRESS
*DnsServerList
;
262 UINTN DnsServerListCount
;
267 DnsServerList
= NULL
;
268 DnsServerListCount
= 0;
271 ZeroMem (&Token
, sizeof (EFI_DNS6_COMPLETION_TOKEN
));
274 // Get DNS server list from EFI IPv6 Configuration protocol.
276 Status
= gBS
->HandleProtocol (Controller
, &gEfiIp6ConfigProtocolGuid
, (VOID
**)&Ip6Config
);
277 if (!EFI_ERROR (Status
)) {
279 // Get the required size.
282 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, NULL
);
283 if (Status
== EFI_BUFFER_TOO_SMALL
) {
284 DnsServerList
= AllocatePool (DataSize
);
285 if (DnsServerList
== NULL
) {
286 return EFI_OUT_OF_RESOURCES
;
289 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, DnsServerList
);
290 if (EFI_ERROR (Status
)) {
291 FreePool (DnsServerList
);
292 DnsServerList
= NULL
;
294 DnsServerListCount
= DataSize
/ sizeof (EFI_IPv6_ADDRESS
);
300 // Create a DNSv6 child instance and get the protocol.
302 Status
= NetLibCreateServiceChild (
305 &gEfiDns6ServiceBindingProtocolGuid
,
308 if (EFI_ERROR (Status
)) {
312 Status
= gBS
->OpenProtocol (
314 &gEfiDns6ProtocolGuid
,
318 EFI_OPEN_PROTOCOL_BY_DRIVER
320 if (EFI_ERROR (Status
)) {
325 // Configure DNS6 instance for the DNS server address and protocol.
327 ZeroMem (&Dns6ConfigData
, sizeof (EFI_DNS6_CONFIG_DATA
));
328 Dns6ConfigData
.DnsServerCount
= (UINT32
)DnsServerListCount
;
329 Dns6ConfigData
.DnsServerList
= DnsServerList
;
330 Dns6ConfigData
.EnableDnsCache
= TRUE
;
331 Dns6ConfigData
.Protocol
= EFI_IP_PROTO_UDP
;
332 Status
= Dns6
->Configure (
336 if (EFI_ERROR (Status
)) {
340 Token
.Status
= EFI_NOT_READY
;
343 // Create event to set the IsDone flag when name resolution is finished.
345 Status
= gBS
->CreateEvent (
352 if (EFI_ERROR (Status
)) {
357 // Start asynchronous name resolution.
359 HostName
= (CHAR16
*)AllocateZeroPool (ISCSI_NAME_MAX_SIZE
);
360 if (HostName
== NULL
) {
361 return EFI_OUT_OF_RESOURCES
;
364 AsciiStrToUnicodeStrS (
369 Status
= Dns6
->HostNameToIp (Dns6
, HostName
, &Token
);
370 if (EFI_ERROR (Status
)) {
379 // Name resolution is done, check result.
381 Status
= Token
.Status
;
382 if (!EFI_ERROR (Status
)) {
383 if (Token
.RspData
.H2AData
== NULL
) {
384 Status
= EFI_DEVICE_ERROR
;
388 if ((Token
.RspData
.H2AData
->IpCount
== 0) || (Token
.RspData
.H2AData
->IpList
== NULL
)) {
389 Status
= EFI_DEVICE_ERROR
;
394 // We just return the first IPv6 address from DNS protocol.
396 IP6_COPY_ADDRESS (&NvData
->TargetIp
.v6
, Token
.RspData
.H2AData
->IpList
);
397 Status
= EFI_SUCCESS
;
402 if (Token
.Event
!= NULL
) {
403 gBS
->CloseEvent (Token
.Event
);
406 if (Token
.RspData
.H2AData
!= NULL
) {
407 if (Token
.RspData
.H2AData
->IpList
!= NULL
) {
408 FreePool (Token
.RspData
.H2AData
->IpList
);
411 FreePool (Token
.RspData
.H2AData
);
415 Dns6
->Configure (Dns6
, NULL
);
419 &gEfiDns6ProtocolGuid
,
425 if (Dns6Handle
!= NULL
) {
426 NetLibDestroyServiceChild (
429 &gEfiDns6ServiceBindingProtocolGuid
,