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
);
94 // Create a DNS child instance and get the protocol.
96 Status
= NetLibCreateServiceChild (
99 &gEfiDns4ServiceBindingProtocolGuid
,
102 if (EFI_ERROR (Status
)) {
106 Status
= gBS
->OpenProtocol (
108 &gEfiDns4ProtocolGuid
,
112 EFI_OPEN_PROTOCOL_BY_DRIVER
114 if (EFI_ERROR (Status
)) {
119 // Configure DNS4 instance for the DNS server address and protocol.
121 ZeroMem (&Dns4CfgData
, sizeof (Dns4CfgData
));
122 Dns4CfgData
.DnsServerListCount
= DnsServerListCount
;
123 Dns4CfgData
.DnsServerList
= DnsServerList
;
124 Dns4CfgData
.EnableDnsCache
= TRUE
;
125 IP4_COPY_ADDRESS (&Dns4CfgData
.StationIp
, &NvData
->LocalIp
);
126 IP4_COPY_ADDRESS (&Dns4CfgData
.SubnetMask
, &NvData
->SubnetMask
);
127 Dns4CfgData
.Protocol
= EFI_IP_PROTO_UDP
;
128 Status
= Dns4
->Configure (
132 if (EFI_ERROR (Status
)) {
137 // Create event to set the is done flag when name resolution is finished.
139 ZeroMem (&Token
, sizeof (Token
));
140 Status
= gBS
->CreateEvent (
147 if (EFI_ERROR (Status
)) {
152 // Start asynchronous name resolution.
154 Token
.Status
= EFI_NOT_READY
;
157 HostName
= (CHAR16
*) AllocateZeroPool (ISCSI_NAME_MAX_SIZE
);
158 if (HostName
== NULL
) {
159 return EFI_OUT_OF_RESOURCES
;
162 AsciiStrToUnicodeStrS (
168 Status
= Dns4
->HostNameToIp (Dns4
, HostName
, &Token
);
169 if (EFI_ERROR (Status
)) {
178 // Name resolution is done, check result.
180 Status
= Token
.Status
;
181 if (!EFI_ERROR (Status
)) {
182 if (Token
.RspData
.H2AData
== NULL
) {
183 Status
= EFI_DEVICE_ERROR
;
186 if (Token
.RspData
.H2AData
->IpCount
== 0 || Token
.RspData
.H2AData
->IpList
== NULL
) {
187 Status
= EFI_DEVICE_ERROR
;
191 // We just return the first IP address from DNS protocol.
193 IP4_COPY_ADDRESS (&NvData
->TargetIp
.v4
, Token
.RspData
.H2AData
->IpList
);
194 Status
= EFI_SUCCESS
;
199 if (Token
.Event
!= NULL
) {
200 gBS
->CloseEvent (Token
.Event
);
202 if (Token
.RspData
.H2AData
!= NULL
) {
203 if (Token
.RspData
.H2AData
->IpList
!= NULL
) {
204 FreePool (Token
.RspData
.H2AData
->IpList
);
206 FreePool (Token
.RspData
.H2AData
);
210 Dns4
->Configure (Dns4
, NULL
);
214 &gEfiDns4ProtocolGuid
,
220 if (Dns4Handle
!= NULL
) {
221 NetLibDestroyServiceChild (
224 &gEfiDns4ServiceBindingProtocolGuid
,
233 Retrieve the host address using the EFI_DNS6_PROTOCOL.
235 @param[in] Image The handle of the driver image.
236 @param[in] Controller The handle of the controller.
237 @param[in, out] NvData The Session config data structure.
239 @retval EFI_SUCCESS Operation succeeded.
240 @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
241 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
242 @retval Others Other errors as indicated.
248 IN EFI_HANDLE Controller
,
249 IN OUT ISCSI_SESSION_CONFIG_NVDATA
*NvData
253 EFI_DNS6_PROTOCOL
*Dns6
;
254 EFI_DNS6_CONFIG_DATA Dns6ConfigData
;
255 EFI_DNS6_COMPLETION_TOKEN Token
;
256 EFI_HANDLE Dns6Handle
;
257 EFI_IP6_CONFIG_PROTOCOL
*Ip6Config
;
258 EFI_IPv6_ADDRESS
*DnsServerList
;
259 UINTN DnsServerListCount
;
264 DnsServerList
= NULL
;
265 DnsServerListCount
= 0;
268 ZeroMem (&Token
, sizeof (EFI_DNS6_COMPLETION_TOKEN
));
271 // Get DNS server list from EFI IPv6 Configuration protocol.
273 Status
= gBS
->HandleProtocol (Controller
, &gEfiIp6ConfigProtocolGuid
, (VOID
**) &Ip6Config
);
274 if (!EFI_ERROR (Status
)) {
276 // Get the required size.
279 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, NULL
);
280 if (Status
== EFI_BUFFER_TOO_SMALL
) {
281 DnsServerList
= AllocatePool (DataSize
);
282 if (DnsServerList
== NULL
) {
283 return EFI_OUT_OF_RESOURCES
;
286 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, DnsServerList
);
287 if (EFI_ERROR (Status
)) {
288 FreePool (DnsServerList
);
289 DnsServerList
= NULL
;
291 DnsServerListCount
= DataSize
/ sizeof (EFI_IPv6_ADDRESS
);
297 // Create a DNSv6 child instance and get the protocol.
299 Status
= NetLibCreateServiceChild (
302 &gEfiDns6ServiceBindingProtocolGuid
,
305 if (EFI_ERROR (Status
)) {
309 Status
= gBS
->OpenProtocol (
311 &gEfiDns6ProtocolGuid
,
315 EFI_OPEN_PROTOCOL_BY_DRIVER
317 if (EFI_ERROR (Status
)) {
322 // Configure DNS6 instance for the DNS server address and protocol.
324 ZeroMem (&Dns6ConfigData
, sizeof (EFI_DNS6_CONFIG_DATA
));
325 Dns6ConfigData
.DnsServerCount
= (UINT32
)DnsServerListCount
;
326 Dns6ConfigData
.DnsServerList
= DnsServerList
;
327 Dns6ConfigData
.EnableDnsCache
= TRUE
;
328 Dns6ConfigData
.Protocol
= EFI_IP_PROTO_UDP
;
329 Status
= Dns6
->Configure (
333 if (EFI_ERROR (Status
)) {
337 Token
.Status
= EFI_NOT_READY
;
340 // Create event to set the IsDone flag when name resolution is finished.
342 Status
= gBS
->CreateEvent (
349 if (EFI_ERROR (Status
)) {
354 // Start asynchronous name resolution.
356 HostName
= (CHAR16
*) AllocateZeroPool (ISCSI_NAME_MAX_SIZE
);
357 if (HostName
== NULL
) {
358 return EFI_OUT_OF_RESOURCES
;
361 AsciiStrToUnicodeStrS (
366 Status
= Dns6
->HostNameToIp (Dns6
, HostName
, &Token
);
367 if (EFI_ERROR (Status
)) {
376 // Name resolution is done, check result.
378 Status
= Token
.Status
;
379 if (!EFI_ERROR (Status
)) {
380 if (Token
.RspData
.H2AData
== NULL
) {
381 Status
= EFI_DEVICE_ERROR
;
384 if (Token
.RspData
.H2AData
->IpCount
== 0 || Token
.RspData
.H2AData
->IpList
== NULL
) {
385 Status
= EFI_DEVICE_ERROR
;
389 // We just return the first IPv6 address from DNS protocol.
391 IP6_COPY_ADDRESS (&NvData
->TargetIp
.v6
, Token
.RspData
.H2AData
->IpList
);
392 Status
= EFI_SUCCESS
;
397 if (Token
.Event
!= NULL
) {
398 gBS
->CloseEvent (Token
.Event
);
400 if (Token
.RspData
.H2AData
!= NULL
) {
401 if (Token
.RspData
.H2AData
->IpList
!= NULL
) {
402 FreePool (Token
.RspData
.H2AData
->IpList
);
404 FreePool (Token
.RspData
.H2AData
);
408 Dns6
->Configure (Dns6
, NULL
);
412 &gEfiDns6ProtocolGuid
,
418 if (Dns6Handle
!= NULL
) {
419 NetLibDestroyServiceChild (
422 &gEfiDns6ServiceBindingProtocolGuid
,