2 DnsDxe support functions implementation.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Remove TokenEntry from TokenMap.
20 @param[in] TokenMap All DNSv4 Token entrys.
21 @param[in] TokenEntry TokenEntry need to be removed.
23 @retval EFI_SUCCESS Remove TokenEntry from TokenMap sucessfully.
24 @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap.
28 Dns4RemoveTokenEntry (
30 IN DNS4_TOKEN_ENTRY
*TokenEntry
36 // Find the TokenEntry first.
38 Item
= NetMapFindKey (TokenMap
, (VOID
*) TokenEntry
);
42 // Remove the TokenEntry if it's found in the map.
44 NetMapRemoveItem (TokenMap
, Item
, NULL
);
53 Remove TokenEntry from TokenMap.
55 @param[in] TokenMap All DNSv6 Token entrys.
56 @param[in] TokenEntry TokenEntry need to be removed.
58 @retval EFI_SUCCESS Remove TokenEntry from TokenMap sucessfully.
59 @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap.
63 Dns6RemoveTokenEntry (
65 IN DNS6_TOKEN_ENTRY
*TokenEntry
71 // Find the TokenEntry first.
73 Item
= NetMapFindKey (TokenMap
, (VOID
*) TokenEntry
);
77 // Remove the TokenEntry if it's found in the map.
79 NetMapRemoveItem (TokenMap
, Item
, NULL
);
88 This function cancle the token specified by Arg in the Map.
90 @param[in] Map Pointer to the NET_MAP.
91 @param[in] Item Pointer to the NET_MAP_ITEM.
92 @param[in] Arg Pointer to the token to be cancelled. If NULL, all
93 the tokens in this Map will be cancelled.
94 This parameter is optional and may be NULL.
96 @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token
97 is not the same as that in the Item, if Arg is not
99 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
107 IN NET_MAP_ITEM
*Item
,
108 IN VOID
*Arg OPTIONAL
111 DNS4_TOKEN_ENTRY
*TokenEntry
;
115 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
119 if (Item
->Value
!= NULL
) {
121 // If the TokenEntry is a transmit TokenEntry, the corresponding Packet is recorded in
124 Packet
= (NET_BUF
*) (Item
->Value
);
125 UdpIo
= (UDP_IO
*) (*((UINTN
*) &Packet
->ProtoData
[0]));
127 UdpIoCancelSentDatagram (UdpIo
, Packet
);
131 // Remove TokenEntry from Dns4TxTokens.
133 TokenEntry
= (DNS4_TOKEN_ENTRY
*) Item
->Key
;
134 if (Dns4RemoveTokenEntry (Map
, TokenEntry
) == EFI_SUCCESS
) {
135 TokenEntry
->Token
->Status
= EFI_ABORTED
;
136 gBS
->SignalEvent (TokenEntry
->Token
->Event
);
148 This function cancle the token specified by Arg in the Map.
150 @param[in] Map Pointer to the NET_MAP.
151 @param[in] Item Pointer to the NET_MAP_ITEM.
152 @param[in] Arg Pointer to the token to be cancelled. If NULL, all
153 the tokens in this Map will be cancelled.
154 This parameter is optional and may be NULL.
156 @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token
157 is not the same as that in the Item, if Arg is not
159 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
167 IN NET_MAP_ITEM
*Item
,
168 IN VOID
*Arg OPTIONAL
171 DNS6_TOKEN_ENTRY
*TokenEntry
;
175 if ((Arg
!= NULL
) && (Item
->Key
!= Arg
)) {
179 if (Item
->Value
!= NULL
) {
181 // If the TokenEntry is a transmit TokenEntry, the corresponding Packet is recorded in
184 Packet
= (NET_BUF
*) (Item
->Value
);
185 UdpIo
= (UDP_IO
*) (*((UINTN
*) &Packet
->ProtoData
[0]));
187 UdpIoCancelSentDatagram (UdpIo
, Packet
);
191 // Remove TokenEntry from Dns6TxTokens.
193 TokenEntry
= (DNS6_TOKEN_ENTRY
*) Item
->Key
;
194 if (Dns6RemoveTokenEntry (Map
, TokenEntry
) == EFI_SUCCESS
) {
195 TokenEntry
->Token
->Status
= EFI_ABORTED
;
196 gBS
->SignalEvent (TokenEntry
->Token
->Event
);
208 Get the TokenEntry from the TokensMap.
210 @param[in] TokensMap All DNSv4 Token entrys
211 @param[in] Token Pointer to the token to be get.
212 @param[out] TokenEntry Pointer to TokenEntry corresponding Token.
214 @retval EFI_SUCCESS Get the TokenEntry from the TokensMap sucessfully.
215 @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap.
221 IN NET_MAP
*TokensMap
,
222 IN EFI_DNS4_COMPLETION_TOKEN
*Token
,
223 OUT DNS4_TOKEN_ENTRY
**TokenEntry
230 NET_LIST_FOR_EACH (Entry
, &TokensMap
->Used
) {
231 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
232 *TokenEntry
= (DNS4_TOKEN_ENTRY
*) (Item
->Key
);
233 if ((*TokenEntry
)->Token
== Token
) {
240 return EFI_NOT_FOUND
;
244 Get the TokenEntry from the TokensMap.
246 @param[in] TokensMap All DNSv6 Token entrys
247 @param[in] Token Pointer to the token to be get.
248 @param[out] TokenEntry Pointer to TokenEntry corresponding Token.
250 @retval EFI_SUCCESS Get the TokenEntry from the TokensMap sucessfully.
251 @retval EFI_NOT_FOUND TokenEntry is not found in TokenMap.
257 IN NET_MAP
*TokensMap
,
258 IN EFI_DNS6_COMPLETION_TOKEN
*Token
,
259 OUT DNS6_TOKEN_ENTRY
**TokenEntry
266 NET_LIST_FOR_EACH (Entry
, &TokensMap
->Used
) {
267 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
268 *TokenEntry
= (DNS6_TOKEN_ENTRY
*) (Item
->Key
);
269 if ((*TokenEntry
)->Token
== Token
) {
276 return EFI_NOT_FOUND
;
280 Cancel DNS4 tokens from the DNS4 instance.
282 @param[in] Instance Pointer to the DNS instance context data.
283 @param[in] Token Pointer to the token to be canceled. If NULL, all
284 tokens in this instance will be cancelled.
285 This parameter is optional and may be NULL.
287 @retval EFI_SUCCESS The Token is cancelled.
288 @retval EFI_NOT_FOUND The Token is not found.
292 Dns4InstanceCancelToken (
293 IN DNS_INSTANCE
*Instance
,
294 IN EFI_DNS4_COMPLETION_TOKEN
*Token
298 DNS4_TOKEN_ENTRY
*TokenEntry
;
303 Status
= GetDns4TokenEntry (&Instance
->Dns4TxTokens
, Token
, &TokenEntry
);
304 if (EFI_ERROR (Status
)) {
312 // Cancel this TokenEntry from the Dns4TxTokens map.
314 Status
= NetMapIterate (&Instance
->Dns4TxTokens
, Dns4CancelTokens
, TokenEntry
);
316 if ((TokenEntry
!= NULL
) && (Status
== EFI_ABORTED
)) {
318 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
319 // the Dns4TxTokens and returns success.
321 if (NetMapIsEmpty (&Instance
->Dns4TxTokens
)) {
322 Instance
->UdpIo
->Protocol
.Udp4
->Cancel (Instance
->UdpIo
->Protocol
.Udp4
, &Instance
->UdpIo
->RecvRequest
->Token
.Udp4
);
327 ASSERT ((TokenEntry
!= NULL
) || (0 == NetMapGetCount (&Instance
->Dns4TxTokens
)));
329 if (NetMapIsEmpty (&Instance
->Dns4TxTokens
)) {
330 Instance
->UdpIo
->Protocol
.Udp4
->Cancel (Instance
->UdpIo
->Protocol
.Udp4
, &Instance
->UdpIo
->RecvRequest
->Token
.Udp4
);
337 Cancel DNS6 tokens from the DNS6 instance.
339 @param[in] Instance Pointer to the DNS instance context data.
340 @param[in] Token Pointer to the token to be canceled. If NULL, all
341 tokens in this instance will be cancelled.
342 This parameter is optional and may be NULL.
344 @retval EFI_SUCCESS The Token is cancelled.
345 @retval EFI_NOT_FOUND The Token is not found.
349 Dns6InstanceCancelToken (
350 IN DNS_INSTANCE
*Instance
,
351 IN EFI_DNS6_COMPLETION_TOKEN
*Token
355 DNS6_TOKEN_ENTRY
*TokenEntry
;
360 Status
= GetDns6TokenEntry (&Instance
->Dns6TxTokens
, Token
, &TokenEntry
);
361 if (EFI_ERROR (Status
)) {
369 // Cancel this TokenEntry from the Dns6TxTokens map.
371 Status
= NetMapIterate (&Instance
->Dns6TxTokens
, Dns6CancelTokens
, TokenEntry
);
373 if ((TokenEntry
!= NULL
) && (Status
== EFI_ABORTED
)) {
375 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
376 // the Dns6TxTokens and returns success.
378 if (NetMapIsEmpty (&Instance
->Dns6TxTokens
)) {
379 Instance
->UdpIo
->Protocol
.Udp6
->Cancel (Instance
->UdpIo
->Protocol
.Udp6
, &Instance
->UdpIo
->RecvRequest
->Token
.Udp6
);
384 ASSERT ((TokenEntry
!= NULL
) || (0 == NetMapGetCount (&Instance
->Dns6TxTokens
)));
386 if (NetMapIsEmpty (&Instance
->Dns6TxTokens
)) {
387 Instance
->UdpIo
->Protocol
.Udp6
->Cancel (Instance
->UdpIo
->Protocol
.Udp6
, &Instance
->UdpIo
->RecvRequest
->Token
.Udp6
);
394 Free the resource related to the configure parameters.
396 @param Config The DNS configure data
401 IN OUT EFI_DNS4_CONFIG_DATA
*Config
404 if (Config
->DnsServerList
!= NULL
) {
405 FreePool (Config
->DnsServerList
);
408 ZeroMem (Config
, sizeof (EFI_DNS4_CONFIG_DATA
));
412 Free the resource related to the configure parameters.
414 @param Config The DNS configure data
419 IN OUT EFI_DNS6_CONFIG_DATA
*Config
422 if (Config
->DnsServerList
!= NULL
) {
423 FreePool (Config
->DnsServerList
);
426 ZeroMem (Config
, sizeof (EFI_DNS6_CONFIG_DATA
));
430 Allocate memory for configure parameter such as timeout value for Dst,
431 then copy the configure parameter from Src to Dst.
433 @param[out] Dst The destination DHCP configure data.
434 @param[in] Src The source DHCP configure data.
436 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
437 @retval EFI_SUCCESS The configure is copied.
442 OUT EFI_DNS4_CONFIG_DATA
*Dst
,
443 IN EFI_DNS4_CONFIG_DATA
*Src
449 CopyMem (Dst
, Src
, sizeof (*Dst
));
450 Dst
->DnsServerList
= NULL
;
453 // Allocate a memory then copy DnsServerList to it
455 if (Src
->DnsServerList
!= NULL
) {
456 Len
= Src
->DnsServerListCount
* sizeof (EFI_IPv4_ADDRESS
);
457 Dst
->DnsServerList
= AllocatePool (Len
);
458 if (Dst
->DnsServerList
== NULL
) {
459 Dns4CleanConfigure (Dst
);
460 return EFI_OUT_OF_RESOURCES
;
463 for (Index
= 0; Index
< Src
->DnsServerListCount
; Index
++) {
464 CopyMem (&Dst
->DnsServerList
[Index
], &Src
->DnsServerList
[Index
], sizeof (EFI_IPv4_ADDRESS
));
472 Allocate memory for configure parameter such as timeout value for Dst,
473 then copy the configure parameter from Src to Dst.
475 @param[out] Dst The destination DHCP configure data.
476 @param[in] Src The source DHCP configure data.
478 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
479 @retval EFI_SUCCESS The configure is copied.
484 OUT EFI_DNS6_CONFIG_DATA
*Dst
,
485 IN EFI_DNS6_CONFIG_DATA
*Src
491 CopyMem (Dst
, Src
, sizeof (*Dst
));
492 Dst
->DnsServerList
= NULL
;
495 // Allocate a memory then copy DnsServerList to it
497 if (Src
->DnsServerList
!= NULL
) {
498 Len
= Src
->DnsServerCount
* sizeof (EFI_IPv6_ADDRESS
);
499 Dst
->DnsServerList
= AllocatePool (Len
);
500 if (Dst
->DnsServerList
== NULL
) {
501 Dns6CleanConfigure (Dst
);
502 return EFI_OUT_OF_RESOURCES
;
505 for (Index
= 0; Index
< Src
->DnsServerCount
; Index
++) {
506 CopyMem (&Dst
->DnsServerList
[Index
], &Src
->DnsServerList
[Index
], sizeof (EFI_IPv6_ADDRESS
));
514 Callback of Dns packet. Does nothing.
516 @param Arg The context.
528 Poll the UDP to get the IP4 default address, which may be retrieved
531 The default time out value is 5 seconds. If IP has retrieved the default address,
532 the UDP is reconfigured.
534 @param Instance The DNS instance
535 @param UdpIo The UDP_IO to poll
536 @param UdpCfgData The UDP configure data to reconfigure the UDP_IO
538 @retval TRUE The default address is retrieved and UDP is reconfigured.
539 @retval FALSE Some error occured.
544 IN DNS_INSTANCE
*Instance
,
546 IN EFI_UDP4_CONFIG_DATA
*UdpCfgData
549 DNS_SERVICE
*Service
;
550 EFI_IP4_MODE_DATA Ip4Mode
;
551 EFI_UDP4_PROTOCOL
*Udp
;
554 ASSERT (Instance
->Dns4CfgData
.UseDefaultSetting
);
556 Service
= Instance
->Service
;
557 Udp
= UdpIo
->Protocol
.Udp4
;
559 Status
= gBS
->SetTimer (
560 Service
->TimerToGetMap
,
562 DNS_TIME_TO_GETMAP
* TICKS_PER_SECOND
564 if (EFI_ERROR (Status
)) {
568 while (EFI_ERROR (gBS
->CheckEvent (Service
->TimerToGetMap
))) {
571 if (!EFI_ERROR (Udp
->GetModeData (Udp
, NULL
, &Ip4Mode
, NULL
, NULL
)) &&
572 Ip4Mode
.IsConfigured
) {
574 Udp
->Configure (Udp
, NULL
);
575 return (BOOLEAN
) (Udp
->Configure (Udp
, UdpCfgData
) == EFI_SUCCESS
);
583 Configure the opened Udp6 instance until the corresponding Ip6 instance
586 @param Instance The DNS instance
587 @param UdpIo The UDP_IO to poll
588 @param UdpCfgData The UDP configure data to reconfigure the UDP_IO
590 @retval TRUE Configure the Udp6 instance successfully.
591 @retval FALSE Some error occured.
596 IN DNS_INSTANCE
*Instance
,
598 IN EFI_UDP6_CONFIG_DATA
*UdpCfgData
601 DNS_SERVICE
*Service
;
602 EFI_IP6_MODE_DATA Ip6Mode
;
603 EFI_UDP6_PROTOCOL
*Udp
;
606 Service
= Instance
->Service
;
607 Udp
= UdpIo
->Protocol
.Udp6
;
609 Status
= gBS
->SetTimer (
610 Service
->TimerToGetMap
,
612 DNS_TIME_TO_GETMAP
* TICKS_PER_SECOND
614 if (EFI_ERROR (Status
)) {
618 while (EFI_ERROR (gBS
->CheckEvent (Service
->TimerToGetMap
))) {
621 if (!EFI_ERROR (Udp
->GetModeData (Udp
, NULL
, &Ip6Mode
, NULL
, NULL
))) {
622 if (Ip6Mode
.AddressList
!= NULL
) {
623 FreePool (Ip6Mode
.AddressList
);
626 if (Ip6Mode
.GroupTable
!= NULL
) {
627 FreePool (Ip6Mode
.GroupTable
);
630 if (Ip6Mode
.RouteTable
!= NULL
) {
631 FreePool (Ip6Mode
.RouteTable
);
634 if (Ip6Mode
.NeighborCache
!= NULL
) {
635 FreePool (Ip6Mode
.NeighborCache
);
638 if (Ip6Mode
.PrefixTable
!= NULL
) {
639 FreePool (Ip6Mode
.PrefixTable
);
642 if (Ip6Mode
.IcmpTypeList
!= NULL
) {
643 FreePool (Ip6Mode
.IcmpTypeList
);
646 if (Ip6Mode
.IsConfigured
) {
647 Udp
->Configure (Udp
, NULL
);
648 return (BOOLEAN
) (Udp
->Configure (Udp
, UdpCfgData
) == EFI_SUCCESS
);
659 @param Instance The DNS session
660 @param UdpIo The UDP_IO instance
662 @retval EFI_SUCCESS The UDP is successfully configured for the
668 IN DNS_INSTANCE
*Instance
,
672 EFI_DNS4_CONFIG_DATA
*Config
;
673 EFI_UDP4_CONFIG_DATA UdpConfig
;
676 Config
= &Instance
->Dns4CfgData
;
678 UdpConfig
.AcceptBroadcast
= FALSE
;
679 UdpConfig
.AcceptPromiscuous
= FALSE
;
680 UdpConfig
.AcceptAnyPort
= FALSE
;
681 UdpConfig
.AllowDuplicatePort
= FALSE
;
682 UdpConfig
.TypeOfService
= 0;
683 UdpConfig
.TimeToLive
= 128;
684 UdpConfig
.DoNotFragment
= FALSE
;
685 UdpConfig
.ReceiveTimeout
= 0;
686 UdpConfig
.TransmitTimeout
= 0;
687 UdpConfig
.UseDefaultAddress
= Config
->UseDefaultSetting
;
688 UdpConfig
.SubnetMask
= Config
->SubnetMask
;
689 UdpConfig
.StationPort
= Config
->LocalPort
;
690 UdpConfig
.RemotePort
= DNS_SERVER_PORT
;
692 CopyMem (&UdpConfig
.StationAddress
, &Config
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
693 CopyMem (&UdpConfig
.RemoteAddress
, &Instance
->SessionDnsServer
.v4
, sizeof (EFI_IPv4_ADDRESS
));
695 Status
= UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, &UdpConfig
);
697 if ((Status
== EFI_NO_MAPPING
) && Dns4GetMapping (Instance
, UdpIo
, &UdpConfig
)) {
707 @param Instance The DNS session
708 @param UdpIo The UDP_IO instance
710 @retval EFI_SUCCESS The UDP is successfully configured for the
716 IN DNS_INSTANCE
*Instance
,
720 EFI_DNS6_CONFIG_DATA
*Config
;
721 EFI_UDP6_CONFIG_DATA UdpConfig
;
724 Config
= &Instance
->Dns6CfgData
;
726 UdpConfig
.AcceptPromiscuous
= FALSE
;
727 UdpConfig
.AcceptAnyPort
= FALSE
;
728 UdpConfig
.AllowDuplicatePort
= FALSE
;
729 UdpConfig
.TrafficClass
= 0;
730 UdpConfig
.HopLimit
= 128;
731 UdpConfig
.ReceiveTimeout
= 0;
732 UdpConfig
.TransmitTimeout
= 0;
733 UdpConfig
.StationPort
= Config
->LocalPort
;
734 UdpConfig
.RemotePort
= DNS_SERVER_PORT
;
735 CopyMem (&UdpConfig
.StationAddress
, &Config
->StationIp
, sizeof (EFI_IPv6_ADDRESS
));
736 CopyMem (&UdpConfig
.RemoteAddress
, &Instance
->SessionDnsServer
.v6
, sizeof (EFI_IPv6_ADDRESS
));
738 Status
= UdpIo
->Protocol
.Udp6
->Configure (UdpIo
->Protocol
.Udp6
, &UdpConfig
);
740 if ((Status
== EFI_NO_MAPPING
) && Dns6GetMapping (Instance
, UdpIo
, &UdpConfig
)) {
748 Update Dns4 cache to shared list of caches of all DNSv4 instances.
750 @param Dns4CacheList All Dns4 cache list.
751 @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache.
752 If TRUE, this function will delete matching DNS Cache entry.
753 @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter.
754 If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists.
755 @param DnsCacheEntry Entry Pointer to DNS Cache entry.
757 @retval EFI_SUCCESS Update Dns4 cache successfully.
758 @retval Others Failed to update Dns4 cache.
764 IN LIST_ENTRY
*Dns4CacheList
,
765 IN BOOLEAN DeleteFlag
,
767 IN EFI_DNS4_CACHE_ENTRY DnsCacheEntry
770 DNS4_CACHE
*NewDnsCache
;
779 // Search the database for the matching EFI_DNS_CACHE_ENTRY
781 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Dns4CacheList
) {
782 Item
= NET_LIST_USER_STRUCT (Entry
, DNS4_CACHE
, AllCacheLink
);
783 if (StrCmp (DnsCacheEntry
.HostName
, Item
->DnsCache
.HostName
) == 0 && \
784 CompareMem (DnsCacheEntry
.IpAddress
, Item
->DnsCache
.IpAddress
, sizeof (EFI_IPv4_ADDRESS
)) == 0) {
786 // This is the Dns cache entry
790 // Delete matching DNS Cache entry
792 RemoveEntryList (&Item
->AllCacheLink
);
795 } else if (Override
) {
799 Item
->DnsCache
.Timeout
= DnsCacheEntry
.Timeout
;
803 return EFI_ACCESS_DENIED
;
811 NewDnsCache
= AllocatePool (sizeof (DNS4_CACHE
));
812 if (NewDnsCache
== NULL
) {
813 return EFI_OUT_OF_RESOURCES
;
816 InitializeListHead (&NewDnsCache
->AllCacheLink
);
818 NewDnsCache
->DnsCache
.HostName
= AllocatePool (StrSize (DnsCacheEntry
.HostName
));
819 if (NewDnsCache
->DnsCache
.HostName
== NULL
) {
820 return EFI_OUT_OF_RESOURCES
;
823 CopyMem (NewDnsCache
->DnsCache
.HostName
, DnsCacheEntry
.HostName
, StrSize (DnsCacheEntry
.HostName
));
825 NewDnsCache
->DnsCache
.IpAddress
= AllocatePool (sizeof (EFI_IPv4_ADDRESS
));
826 if (NewDnsCache
->DnsCache
.IpAddress
== NULL
) {
827 return EFI_OUT_OF_RESOURCES
;
830 CopyMem (NewDnsCache
->DnsCache
.IpAddress
, DnsCacheEntry
.IpAddress
, sizeof (EFI_IPv4_ADDRESS
));
832 NewDnsCache
->DnsCache
.Timeout
= DnsCacheEntry
.Timeout
;
834 InsertTailList (Dns4CacheList
, &NewDnsCache
->AllCacheLink
);
840 Update Dns6 cache to shared list of caches of all DNSv6 instances.
842 @param Dns6CacheList All Dns6 cache list.
843 @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache.
844 If TRUE, this function will delete matching DNS Cache entry.
845 @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter.
846 If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists.
847 @param DnsCacheEntry Entry Pointer to DNS Cache entry.
849 @retval EFI_SUCCESS Update Dns6 cache successfully.
850 @retval Others Failed to update Dns6 cache.
855 IN LIST_ENTRY
*Dns6CacheList
,
856 IN BOOLEAN DeleteFlag
,
858 IN EFI_DNS6_CACHE_ENTRY DnsCacheEntry
861 DNS6_CACHE
*NewDnsCache
;
870 // Search the database for the matching EFI_DNS_CACHE_ENTRY
872 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Dns6CacheList
) {
873 Item
= NET_LIST_USER_STRUCT (Entry
, DNS6_CACHE
, AllCacheLink
);
874 if (StrCmp (DnsCacheEntry
.HostName
, Item
->DnsCache
.HostName
) == 0 && \
875 CompareMem (DnsCacheEntry
.IpAddress
, Item
->DnsCache
.IpAddress
, sizeof (EFI_IPv6_ADDRESS
)) == 0) {
877 // This is the Dns cache entry
881 // Delete matching DNS Cache entry
883 RemoveEntryList (&Item
->AllCacheLink
);
886 } else if (Override
) {
890 Item
->DnsCache
.Timeout
= DnsCacheEntry
.Timeout
;
894 return EFI_ACCESS_DENIED
;
902 NewDnsCache
= AllocatePool (sizeof (DNS6_CACHE
));
903 if (NewDnsCache
== NULL
) {
904 return EFI_OUT_OF_RESOURCES
;
907 InitializeListHead (&NewDnsCache
->AllCacheLink
);
909 NewDnsCache
->DnsCache
.HostName
= AllocatePool (StrSize (DnsCacheEntry
.HostName
));
910 if (NewDnsCache
->DnsCache
.HostName
== NULL
) {
911 return EFI_OUT_OF_RESOURCES
;
914 CopyMem (NewDnsCache
->DnsCache
.HostName
, DnsCacheEntry
.HostName
, StrSize (DnsCacheEntry
.HostName
));
916 NewDnsCache
->DnsCache
.IpAddress
= AllocatePool (sizeof (EFI_IPv6_ADDRESS
));
917 if (NewDnsCache
->DnsCache
.IpAddress
== NULL
) {
918 return EFI_OUT_OF_RESOURCES
;
921 CopyMem (NewDnsCache
->DnsCache
.IpAddress
, DnsCacheEntry
.IpAddress
, sizeof (EFI_IPv6_ADDRESS
));
923 NewDnsCache
->DnsCache
.Timeout
= DnsCacheEntry
.Timeout
;
925 InsertTailList (Dns6CacheList
, &NewDnsCache
->AllCacheLink
);
931 Add Dns4 ServerIp to common list of addresses of all configured DNSv4 server.
933 @param Dns4ServerList Common list of addresses of all configured DNSv4 server.
934 @param ServerIp DNS server Ip.
936 @retval EFI_SUCCESS Add Dns4 ServerIp to common list successfully.
937 @retval Others Failed to add Dns4 ServerIp to common list.
943 IN LIST_ENTRY
*Dns4ServerList
,
944 IN EFI_IPv4_ADDRESS ServerIp
947 DNS4_SERVER_IP
*NewServerIp
;
948 DNS4_SERVER_IP
*Item
;
956 // Search the database for the matching ServerIp
958 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Dns4ServerList
) {
959 Item
= NET_LIST_USER_STRUCT (Entry
, DNS4_SERVER_IP
, AllServerLink
);
960 if (CompareMem (&Item
->Dns4ServerIp
, &ServerIp
, sizeof (EFI_IPv4_ADDRESS
)) == 0) {
971 NewServerIp
= AllocatePool (sizeof (DNS4_SERVER_IP
));
972 if (NewServerIp
== NULL
) {
973 return EFI_OUT_OF_RESOURCES
;
976 InitializeListHead (&NewServerIp
->AllServerLink
);
978 CopyMem (&NewServerIp
->Dns4ServerIp
, &ServerIp
, sizeof (EFI_IPv4_ADDRESS
));
980 InsertTailList (Dns4ServerList
, &NewServerIp
->AllServerLink
);
986 Add Dns6 ServerIp to common list of addresses of all configured DNSv6 server.
988 @param Dns6ServerList Common list of addresses of all configured DNSv6 server.
989 @param ServerIp DNS server Ip.
991 @retval EFI_SUCCESS Add Dns6 ServerIp to common list successfully.
992 @retval Others Failed to add Dns6 ServerIp to common list.
998 IN LIST_ENTRY
*Dns6ServerList
,
999 IN EFI_IPv6_ADDRESS ServerIp
1002 DNS6_SERVER_IP
*NewServerIp
;
1003 DNS6_SERVER_IP
*Item
;
1011 // Search the database for the matching ServerIp
1013 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Dns6ServerList
) {
1014 Item
= NET_LIST_USER_STRUCT (Entry
, DNS6_SERVER_IP
, AllServerLink
);
1015 if (CompareMem (&Item
->Dns6ServerIp
, &ServerIp
, sizeof (EFI_IPv6_ADDRESS
)) == 0) {
1026 NewServerIp
= AllocatePool (sizeof (DNS6_SERVER_IP
));
1027 if (NewServerIp
== NULL
) {
1028 return EFI_OUT_OF_RESOURCES
;
1031 InitializeListHead (&NewServerIp
->AllServerLink
);
1033 CopyMem (&NewServerIp
->Dns6ServerIp
, &ServerIp
, sizeof (EFI_IPv6_ADDRESS
));
1035 InsertTailList (Dns6ServerList
, &NewServerIp
->AllServerLink
);
1041 Find out whether the response is valid or invalid.
1043 @param TokensMap All DNS transmittal Tokens entry.
1044 @param Identification Identification for queried packet.
1045 @param Type Type for queried packet.
1046 @param Class Class for queried packet.
1047 @param Item Return corresponding Token entry.
1049 @retval TRUE The response is valid.
1050 @retval FALSE The response is invalid.
1054 IsValidDnsResponse (
1055 IN NET_MAP
*TokensMap
,
1056 IN UINT16 Identification
,
1059 OUT NET_MAP_ITEM
**Item
1066 DNS_HEADER
*DnsHeader
;
1068 DNS_QUERY_SECTION
*QuerySection
;
1070 NET_LIST_FOR_EACH (Entry
, &TokensMap
->Used
) {
1071 *Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1072 Packet
= (NET_BUF
*) ((*Item
)->Value
);
1073 if (Packet
== NULL
){
1077 TxString
= NetbufGetByte (Packet
, 0, NULL
);
1078 ASSERT (TxString
!= NULL
);
1079 DnsHeader
= (DNS_HEADER
*) TxString
;
1080 QueryName
= (CHAR8
*) (TxString
+ sizeof (*DnsHeader
));
1081 QuerySection
= (DNS_QUERY_SECTION
*) (QueryName
+ AsciiStrLen (QueryName
) + 1);
1083 if (NTOHS (DnsHeader
->Identification
) == Identification
&&
1084 NTOHS (QuerySection
->Type
) == Type
&&
1085 NTOHS (QuerySection
->Class
) == Class
) {
1099 @param Instance The DNS instance
1100 @param RxString Received buffer.
1101 @param Completed Flag to indicate that Dns response is valid.
1103 @retval EFI_SUCCESS Parse Dns Response successfully.
1104 @retval Others Failed to parse Dns Response.
1109 IN OUT DNS_INSTANCE
*Instance
,
1111 OUT BOOLEAN
*Completed
1114 DNS_HEADER
*DnsHeader
;
1117 DNS_QUERY_SECTION
*QuerySection
;
1120 DNS_ANSWER_SECTION
*AnswerSection
;
1124 DNS4_TOKEN_ENTRY
*Dns4TokenEntry
;
1125 DNS6_TOKEN_ENTRY
*Dns6TokenEntry
;
1129 UINT32 AnswerSectionNum
;
1132 EFI_IPv4_ADDRESS
*HostAddr4
;
1133 EFI_IPv6_ADDRESS
*HostAddr6
;
1135 EFI_DNS4_CACHE_ENTRY
*Dns4CacheEntry
;
1136 EFI_DNS6_CACHE_ENTRY
*Dns6CacheEntry
;
1138 DNS_RESOURCE_RECORD
*Dns4RR
;
1139 DNS6_RESOURCE_RECORD
*Dns6RR
;
1146 Dns4TokenEntry
= NULL
;
1147 Dns6TokenEntry
= NULL
;
1151 AnswerSectionNum
= 0;
1157 Dns4CacheEntry
= NULL
;
1158 Dns6CacheEntry
= NULL
;
1164 Status
= EFI_SUCCESS
;
1169 DnsHeader
= (DNS_HEADER
*) RxString
;
1171 DnsHeader
->Identification
= NTOHS (DnsHeader
->Identification
);
1172 DnsHeader
->Flags
.Uint16
= NTOHS (DnsHeader
->Flags
.Uint16
);
1173 DnsHeader
->QuestionsNum
= NTOHS (DnsHeader
->QuestionsNum
);
1174 DnsHeader
->AnswersNum
= NTOHS (DnsHeader
->AnswersNum
);
1175 DnsHeader
->AuthorityNum
= NTOHS (DnsHeader
->AuthorityNum
);
1176 DnsHeader
->AditionalNum
= NTOHS (DnsHeader
->AditionalNum
);
1181 QueryName
= (CHAR8
*) (RxString
+ sizeof (*DnsHeader
));
1184 // Get query section
1186 QuerySection
= (DNS_QUERY_SECTION
*) (QueryName
+ AsciiStrLen (QueryName
) + 1);
1187 QuerySection
->Type
= NTOHS (QuerySection
->Type
);
1188 QuerySection
->Class
= NTOHS (QuerySection
->Class
);
1193 AnswerName
= (CHAR8
*) QuerySection
+ sizeof (*QuerySection
);
1195 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1198 // Check DnsResponse Validity, if so, also get a valid NET_MAP_ITEM.
1200 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1201 if (!IsValidDnsResponse (
1202 &Instance
->Dns4TxTokens
,
1203 DnsHeader
->Identification
,
1205 QuerySection
->Class
,
1209 Status
= EFI_ABORTED
;
1212 ASSERT (Item
!= NULL
);
1213 Dns4TokenEntry
= (DNS4_TOKEN_ENTRY
*) (Item
->Key
);
1215 if (!IsValidDnsResponse (
1216 &Instance
->Dns6TxTokens
,
1217 DnsHeader
->Identification
,
1219 QuerySection
->Class
,
1223 Status
= EFI_ABORTED
;
1226 ASSERT (Item
!= NULL
);
1227 Dns6TokenEntry
= (DNS6_TOKEN_ENTRY
*) (Item
->Key
);
1231 // Continue Check Some Errors.
1233 if (DnsHeader
->Flags
.Bits
.RCode
!= DNS_FLAGS_RCODE_NO_ERROR
|| DnsHeader
->AnswersNum
< 1 || \
1234 DnsHeader
->Flags
.Bits
.QR
!= DNS_FLAGS_QR_RESPONSE
) {
1236 // The domain name referenced in the query does not exist.
1238 if (DnsHeader
->Flags
.Bits
.RCode
== DNS_FLAGS_RCODE_NAME_ERROR
) {
1239 Status
= EFI_NOT_FOUND
;
1241 Status
= EFI_DEVICE_ERROR
;
1248 // Do some buffer allocations.
1250 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1251 ASSERT (Dns4TokenEntry
!= NULL
);
1253 if (Dns4TokenEntry
->GeneralLookUp
) {
1255 // It's the GeneralLookUp querying.
1257 Dns4TokenEntry
->Token
->RspData
.GLookupData
= AllocatePool (sizeof (DNS_RESOURCE_RECORD
));
1258 if (Dns4TokenEntry
->Token
->RspData
.GLookupData
== NULL
) {
1259 Status
= EFI_OUT_OF_RESOURCES
;
1262 Dns4TokenEntry
->Token
->RspData
.GLookupData
->RRList
= AllocatePool (DnsHeader
->AnswersNum
* sizeof (DNS_RESOURCE_RECORD
));
1263 if (Dns4TokenEntry
->Token
->RspData
.GLookupData
->RRList
== NULL
) {
1264 Status
= EFI_OUT_OF_RESOURCES
;
1269 // It's not the GeneralLookUp querying. Check the Query type.
1271 if (QuerySection
->Type
== DNS_TYPE_A
) {
1272 Dns4TokenEntry
->Token
->RspData
.H2AData
= AllocatePool (sizeof (DNS_HOST_TO_ADDR_DATA
));
1273 if (Dns4TokenEntry
->Token
->RspData
.H2AData
== NULL
) {
1274 Status
= EFI_OUT_OF_RESOURCES
;
1277 Dns4TokenEntry
->Token
->RspData
.H2AData
->IpList
= AllocatePool (DnsHeader
->AnswersNum
* sizeof (EFI_IPv4_ADDRESS
));
1278 if (Dns4TokenEntry
->Token
->RspData
.H2AData
->IpList
== NULL
) {
1279 Status
= EFI_OUT_OF_RESOURCES
;
1283 Status
= EFI_UNSUPPORTED
;
1288 ASSERT (Dns6TokenEntry
!= NULL
);
1290 if (Dns6TokenEntry
->GeneralLookUp
) {
1292 // It's the GeneralLookUp querying.
1294 Dns6TokenEntry
->Token
->RspData
.GLookupData
= AllocatePool (sizeof (DNS_RESOURCE_RECORD
));
1295 if (Dns6TokenEntry
->Token
->RspData
.GLookupData
== NULL
) {
1296 Status
= EFI_OUT_OF_RESOURCES
;
1299 Dns6TokenEntry
->Token
->RspData
.GLookupData
->RRList
= AllocatePool (DnsHeader
->AnswersNum
* sizeof (DNS_RESOURCE_RECORD
));
1300 if (Dns6TokenEntry
->Token
->RspData
.GLookupData
->RRList
== NULL
) {
1301 Status
= EFI_OUT_OF_RESOURCES
;
1306 // It's not the GeneralLookUp querying. Check the Query type.
1308 if (QuerySection
->Type
== DNS_TYPE_AAAA
) {
1309 Dns6TokenEntry
->Token
->RspData
.H2AData
= AllocatePool (sizeof (DNS6_HOST_TO_ADDR_DATA
));
1310 if (Dns6TokenEntry
->Token
->RspData
.H2AData
== NULL
) {
1311 Status
= EFI_OUT_OF_RESOURCES
;
1314 Dns6TokenEntry
->Token
->RspData
.H2AData
->IpList
= AllocatePool (DnsHeader
->AnswersNum
* sizeof (EFI_IPv6_ADDRESS
));
1315 if (Dns6TokenEntry
->Token
->RspData
.H2AData
->IpList
== NULL
) {
1316 Status
= EFI_OUT_OF_RESOURCES
;
1320 Status
= EFI_UNSUPPORTED
;
1326 Status
= EFI_NOT_FOUND
;
1329 // Processing AnswerSection.
1331 while (AnswerSectionNum
< DnsHeader
->AnswersNum
) {
1333 // Answer name should be PTR.
1335 ASSERT ((*(UINT8
*) AnswerName
& 0xC0) == 0xC0);
1338 // Get Answer section.
1340 AnswerSection
= (DNS_ANSWER_SECTION
*) (AnswerName
+ sizeof (UINT16
));
1341 AnswerSection
->Type
= NTOHS (AnswerSection
->Type
);
1342 AnswerSection
->Class
= NTOHS (AnswerSection
->Class
);
1343 AnswerSection
->Ttl
= NTOHL (AnswerSection
->Ttl
);
1344 AnswerSection
->DataLength
= NTOHS (AnswerSection
->DataLength
);
1347 // Check whether it's the GeneralLookUp querying.
1349 if (Instance
->Service
->IpVersion
== IP_VERSION_4
&& Dns4TokenEntry
->GeneralLookUp
) {
1350 Dns4RR
= Dns4TokenEntry
->Token
->RspData
.GLookupData
->RRList
;
1351 AnswerData
= (UINT8
*) AnswerSection
+ sizeof (*AnswerSection
);
1354 // Fill the ResourceRecord.
1356 Dns4RR
[RRCount
].QName
= AllocateZeroPool (AsciiStrLen (QueryName
) + 1);
1357 if (Dns4RR
[RRCount
].QName
== NULL
) {
1358 Status
= EFI_OUT_OF_RESOURCES
;
1361 CopyMem (Dns4RR
[RRCount
].QName
, QueryName
, AsciiStrLen (QueryName
));
1362 Dns4RR
[RRCount
].QType
= AnswerSection
->Type
;
1363 Dns4RR
[RRCount
].QClass
= AnswerSection
->Class
;
1364 Dns4RR
[RRCount
].TTL
= AnswerSection
->Ttl
;
1365 Dns4RR
[RRCount
].DataLength
= AnswerSection
->DataLength
;
1366 Dns4RR
[RRCount
].RData
= AllocateZeroPool (Dns4RR
[RRCount
].DataLength
);
1367 if (Dns4RR
[RRCount
].RData
== NULL
) {
1368 Status
= EFI_OUT_OF_RESOURCES
;
1371 CopyMem (Dns4RR
[RRCount
].RData
, AnswerData
, Dns4RR
[RRCount
].DataLength
);
1374 Status
= EFI_SUCCESS
;
1375 } else if (Instance
->Service
->IpVersion
== IP_VERSION_6
&& Dns6TokenEntry
->GeneralLookUp
) {
1376 Dns6RR
= Dns6TokenEntry
->Token
->RspData
.GLookupData
->RRList
;
1377 AnswerData
= (UINT8
*) AnswerSection
+ sizeof (*AnswerSection
);
1380 // Fill the ResourceRecord.
1382 Dns6RR
[RRCount
].QName
= AllocateZeroPool (AsciiStrLen (QueryName
) + 1);
1383 if (Dns6RR
[RRCount
].QName
== NULL
) {
1384 Status
= EFI_OUT_OF_RESOURCES
;
1387 CopyMem (Dns6RR
[RRCount
].QName
, QueryName
, AsciiStrLen (QueryName
));
1388 Dns6RR
[RRCount
].QType
= AnswerSection
->Type
;
1389 Dns6RR
[RRCount
].QClass
= AnswerSection
->Class
;
1390 Dns6RR
[RRCount
].TTL
= AnswerSection
->Ttl
;
1391 Dns6RR
[RRCount
].DataLength
= AnswerSection
->DataLength
;
1392 Dns6RR
[RRCount
].RData
= AllocateZeroPool (Dns6RR
[RRCount
].DataLength
);
1393 if (Dns6RR
[RRCount
].RData
== NULL
) {
1394 Status
= EFI_OUT_OF_RESOURCES
;
1397 CopyMem (Dns6RR
[RRCount
].RData
, AnswerData
, Dns6RR
[RRCount
].DataLength
);
1400 Status
= EFI_SUCCESS
;
1403 // It's not the GeneralLookUp querying.
1404 // Check the Query type, parse the response packet.
1406 switch (AnswerSection
->Type
) {
1409 // This is address entry, get Data.
1411 ASSERT (Dns4TokenEntry
!= NULL
&& AnswerSection
->DataLength
== 4);
1413 HostAddr4
= Dns4TokenEntry
->Token
->RspData
.H2AData
->IpList
;
1414 AnswerData
= (UINT8
*) AnswerSection
+ sizeof (*AnswerSection
);
1415 CopyMem (&HostAddr4
[IpCount
], AnswerData
, sizeof (EFI_IPv4_ADDRESS
));
1418 // Allocate new CacheEntry pool to update DNS cache dynamically.
1420 Dns4CacheEntry
= AllocateZeroPool (sizeof (EFI_DNS4_CACHE_ENTRY
));
1421 if (Dns4CacheEntry
== NULL
) {
1422 Status
= EFI_OUT_OF_RESOURCES
;
1425 Dns4CacheEntry
->HostName
= AllocateZeroPool (2 * (StrLen(Dns4TokenEntry
->QueryHostName
) + 1));
1426 if (Dns4CacheEntry
->HostName
== NULL
) {
1427 Status
= EFI_OUT_OF_RESOURCES
;
1430 CopyMem (Dns4CacheEntry
->HostName
, Dns4TokenEntry
->QueryHostName
, 2 * (StrLen(Dns4TokenEntry
->QueryHostName
) + 1));
1431 Dns4CacheEntry
->IpAddress
= AllocateZeroPool (sizeof (EFI_IPv4_ADDRESS
));
1432 if (Dns4CacheEntry
->IpAddress
== NULL
) {
1433 Status
= EFI_OUT_OF_RESOURCES
;
1436 CopyMem (Dns4CacheEntry
->IpAddress
, AnswerData
, sizeof (EFI_IPv4_ADDRESS
));
1438 if (CNameTtl
!= 0 && AnswerSection
->Ttl
!= 0) {
1439 Dns4CacheEntry
->Timeout
= MIN (CNameTtl
, AnswerSection
->Ttl
);
1441 Dns4CacheEntry
->Timeout
= MAX (CNameTtl
, AnswerSection
->Ttl
);
1444 UpdateDns4Cache (&mDriverData
->Dns4CacheList
, FALSE
, TRUE
, *Dns4CacheEntry
);
1447 // Free allocated CacheEntry pool.
1449 FreePool (Dns4CacheEntry
->HostName
);
1450 Dns4CacheEntry
->HostName
= NULL
;
1452 FreePool (Dns4CacheEntry
->IpAddress
);
1453 Dns4CacheEntry
->IpAddress
= NULL
;
1455 FreePool (Dns4CacheEntry
);
1456 Dns4CacheEntry
= NULL
;
1459 Status
= EFI_SUCCESS
;
1463 // This is address entry, get Data.
1465 ASSERT (Dns6TokenEntry
!= NULL
&& AnswerSection
->DataLength
== 16);
1467 HostAddr6
= Dns6TokenEntry
->Token
->RspData
.H2AData
->IpList
;
1468 AnswerData
= (UINT8
*) AnswerSection
+ sizeof (*AnswerSection
);
1469 CopyMem (&HostAddr6
[IpCount
], AnswerData
, sizeof (EFI_IPv6_ADDRESS
));
1472 // Allocate new CacheEntry pool to update DNS cache dynamically.
1474 Dns6CacheEntry
= AllocateZeroPool (sizeof (EFI_DNS6_CACHE_ENTRY
));
1475 if (Dns6CacheEntry
== NULL
) {
1476 Status
= EFI_OUT_OF_RESOURCES
;
1479 Dns6CacheEntry
->HostName
= AllocateZeroPool (2 * (StrLen(Dns6TokenEntry
->QueryHostName
) + 1));
1480 if (Dns6CacheEntry
->HostName
== NULL
) {
1481 Status
= EFI_OUT_OF_RESOURCES
;
1484 CopyMem (Dns6CacheEntry
->HostName
, Dns6TokenEntry
->QueryHostName
, 2 * (StrLen(Dns6TokenEntry
->QueryHostName
) + 1));
1485 Dns6CacheEntry
->IpAddress
= AllocateZeroPool (sizeof (EFI_IPv6_ADDRESS
));
1486 if (Dns6CacheEntry
->IpAddress
== NULL
) {
1487 Status
= EFI_OUT_OF_RESOURCES
;
1490 CopyMem (Dns6CacheEntry
->IpAddress
, AnswerData
, sizeof (EFI_IPv6_ADDRESS
));
1492 if (CNameTtl
!= 0 && AnswerSection
->Ttl
!= 0) {
1493 Dns6CacheEntry
->Timeout
= MIN (CNameTtl
, AnswerSection
->Ttl
);
1495 Dns6CacheEntry
->Timeout
= MAX (CNameTtl
, AnswerSection
->Ttl
);
1498 UpdateDns6Cache (&mDriverData
->Dns6CacheList
, FALSE
, TRUE
, *Dns6CacheEntry
);
1501 // Free allocated CacheEntry pool.
1503 FreePool (Dns6CacheEntry
->HostName
);
1504 Dns6CacheEntry
->HostName
= NULL
;
1506 FreePool (Dns6CacheEntry
->IpAddress
);
1507 Dns6CacheEntry
->IpAddress
= NULL
;
1509 FreePool (Dns6CacheEntry
);
1510 Dns6CacheEntry
= NULL
;
1513 Status
= EFI_SUCCESS
;
1515 case DNS_TYPE_CNAME
:
1517 // According RFC 1034 - 3.6.2, if the query name is an alias, the name server will include the CNAME
1518 // record in the response and restart the query at the domain name specified in the data field of the
1519 // CNAME record. So, just record the TTL value of the CNAME, then skip to parse the next record.
1521 CNameTtl
= AnswerSection
->Ttl
;
1524 Status
= EFI_UNSUPPORTED
;
1532 AnswerName
= (CHAR8
*) AnswerSection
+ sizeof (*AnswerSection
) + AnswerSection
->DataLength
;
1533 AnswerSectionNum
++;
1536 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1537 ASSERT (Dns4TokenEntry
!= NULL
);
1539 if (Dns4TokenEntry
->GeneralLookUp
) {
1540 Dns4TokenEntry
->Token
->RspData
.GLookupData
->RRCount
= RRCount
;
1542 if (QuerySection
->Type
== DNS_TYPE_A
) {
1543 Dns4TokenEntry
->Token
->RspData
.H2AData
->IpCount
= IpCount
;
1545 Status
= EFI_UNSUPPORTED
;
1550 ASSERT (Dns6TokenEntry
!= NULL
);
1552 if (Dns6TokenEntry
->GeneralLookUp
) {
1553 Dns6TokenEntry
->Token
->RspData
.GLookupData
->RRCount
= RRCount
;
1555 if (QuerySection
->Type
== DNS_TYPE_AAAA
) {
1556 Dns6TokenEntry
->Token
->RspData
.H2AData
->IpCount
= IpCount
;
1558 Status
= EFI_UNSUPPORTED
;
1566 // Parsing is complete, free the sending packet and signal Event here.
1568 if (Item
!= NULL
&& Item
->Value
!= NULL
) {
1569 NetbufFree ((NET_BUF
*) (Item
->Value
));
1572 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1573 ASSERT (Dns4TokenEntry
!= NULL
);
1574 Dns4RemoveTokenEntry (&Instance
->Dns4TxTokens
, Dns4TokenEntry
);
1575 Dns4TokenEntry
->Token
->Status
= Status
;
1576 if (Dns4TokenEntry
->Token
->Event
!= NULL
) {
1577 gBS
->SignalEvent (Dns4TokenEntry
->Token
->Event
);
1581 ASSERT (Dns6TokenEntry
!= NULL
);
1582 Dns6RemoveTokenEntry (&Instance
->Dns6TxTokens
, Dns6TokenEntry
);
1583 Dns6TokenEntry
->Token
->Status
= Status
;
1584 if (Dns6TokenEntry
->Token
->Event
!= NULL
) {
1585 gBS
->SignalEvent (Dns6TokenEntry
->Token
->Event
);
1591 gBS
->RestoreTPL (OldTpl
);
1596 Parse response packet.
1598 @param Packet The packets received.
1599 @param EndPoint The local/remote UDP access point
1600 @param IoStatus The status of the UDP receive
1601 @param Context The opaque parameter to the function.
1606 DnsOnPacketReceived (
1608 UDP_END_POINT
*EndPoint
,
1609 EFI_STATUS IoStatus
,
1613 DNS_INSTANCE
*Instance
;
1619 Instance
= (DNS_INSTANCE
*) Context
;
1620 NET_CHECK_SIGNATURE (Instance
, DNS_INSTANCE_SIGNATURE
);
1625 if (EFI_ERROR (IoStatus
)) {
1629 ASSERT (Packet
!= NULL
);
1631 if (Packet
->TotalSize
<= sizeof (DNS_HEADER
)) {
1635 RcvString
= NetbufGetByte (Packet
, 0, NULL
);
1636 ASSERT (RcvString
!= NULL
);
1639 // Parse Dns Response
1641 ParseDnsResponse (Instance
, RcvString
, &Completed
);
1645 if (Packet
!= NULL
) {
1646 NetbufFree (Packet
);
1650 UdpIoRecvDatagram (Instance
->UdpIo
, DnsOnPacketReceived
, Instance
, 0);
1655 Release the net buffer when packet is sent.
1657 @param Packet The packets received.
1658 @param EndPoint The local/remote UDP access point
1659 @param IoStatus The status of the UDP receive
1660 @param Context The opaque parameter to the function.
1667 UDP_END_POINT
*EndPoint
,
1668 EFI_STATUS IoStatus
,
1672 DNS_INSTANCE
*Instance
;
1675 DNS4_TOKEN_ENTRY
*Dns4TokenEntry
;
1676 DNS6_TOKEN_ENTRY
*Dns6TokenEntry
;
1678 Dns4TokenEntry
= NULL
;
1679 Dns6TokenEntry
= NULL
;
1681 Instance
= (DNS_INSTANCE
*) Context
;
1682 NET_CHECK_SIGNATURE (Instance
, DNS_INSTANCE_SIGNATURE
);
1684 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1685 NET_LIST_FOR_EACH (Entry
, &Instance
->Dns4TxTokens
.Used
) {
1686 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1687 if (Packet
== (NET_BUF
*)(Item
->Value
)) {
1688 Dns4TokenEntry
= ((DNS4_TOKEN_ENTRY
*)Item
->Key
);
1689 Dns4TokenEntry
->PacketToLive
= Dns4TokenEntry
->Token
->RetryInterval
;
1694 NET_LIST_FOR_EACH (Entry
, &Instance
->Dns6TxTokens
.Used
) {
1695 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1696 if (Packet
== (NET_BUF
*)(Item
->Value
)) {
1697 Dns6TokenEntry
= ((DNS6_TOKEN_ENTRY
*)Item
->Key
);
1698 Dns6TokenEntry
->PacketToLive
= Dns6TokenEntry
->Token
->RetryInterval
;
1704 NetbufFree (Packet
);
1708 Query request information.
1710 @param Instance The DNS instance
1711 @param Packet The packet for querying request information.
1713 @retval EFI_SUCCESS Query request information successfully.
1714 @retval Others Failed to query request information.
1719 IN DNS_INSTANCE
*Instance
,
1726 // Ready to receive the DNS response.
1728 if (Instance
->UdpIo
->RecvRequest
== NULL
) {
1729 Status
= UdpIoRecvDatagram (Instance
->UdpIo
, DnsOnPacketReceived
, Instance
, 0);
1730 if (EFI_ERROR (Status
)) {
1736 // Transmit the DNS packet.
1738 NET_GET_REF (Packet
);
1740 Status
= UdpIoSendDatagram (Instance
->UdpIo
, Packet
, NULL
, NULL
, DnsOnPacketSent
, Instance
);
1746 Construct the Packet according query section.
1748 @param Instance The DNS instance
1749 @param QueryName Queried Name
1750 @param Type Queried Type
1751 @param Class Queried Class
1752 @param Packet The packet for query
1754 @retval EFI_SUCCESS The packet is constructed.
1755 @retval Others Failed to construct the Packet.
1760 IN DNS_INSTANCE
*Instance
,
1761 IN CHAR8
*QueryName
,
1764 OUT NET_BUF
**Packet
1768 DNS_HEADER
*DnsHeader
;
1769 DNS_QUERY_SECTION
*DnsQuery
;
1772 // Messages carried by UDP are restricted to 512 bytes (not counting the IP
1775 Frag
.Bulk
= AllocatePool (DNS_MAX_MESSAGE_SIZE
* sizeof (UINT8
));
1776 if (Frag
.Bulk
== NULL
) {
1777 return EFI_OUT_OF_RESOURCES
;
1783 DnsHeader
= (DNS_HEADER
*) Frag
.Bulk
;
1784 DnsHeader
->Identification
= (UINT16
)NET_RANDOM (NetRandomInitSeed());
1785 DnsHeader
->Flags
.Uint16
= 0x0000;
1786 DnsHeader
->Flags
.Bits
.RD
= 1;
1787 DnsHeader
->Flags
.Bits
.OpCode
= DNS_FLAGS_OPCODE_STANDARD
;
1788 DnsHeader
->Flags
.Bits
.QR
= DNS_FLAGS_QR_QUERY
;
1789 DnsHeader
->QuestionsNum
= 1;
1790 DnsHeader
->AnswersNum
= 0;
1791 DnsHeader
->AuthorityNum
= 0;
1792 DnsHeader
->AditionalNum
= 0;
1794 DnsHeader
->Identification
= HTONS (DnsHeader
->Identification
);
1795 DnsHeader
->Flags
.Uint16
= HTONS (DnsHeader
->Flags
.Uint16
);
1796 DnsHeader
->QuestionsNum
= HTONS (DnsHeader
->QuestionsNum
);
1797 DnsHeader
->AnswersNum
= HTONS (DnsHeader
->AnswersNum
);
1798 DnsHeader
->AuthorityNum
= HTONS (DnsHeader
->AuthorityNum
);
1799 DnsHeader
->AditionalNum
= HTONS (DnsHeader
->AditionalNum
);
1801 Frag
.Len
= sizeof (*DnsHeader
);
1806 CopyMem (Frag
.Bulk
+ Frag
.Len
, QueryName
, AsciiStrLen (QueryName
));
1807 Frag
.Len
= (UINT32
) (Frag
.Len
+ AsciiStrLen (QueryName
));
1808 *(Frag
.Bulk
+ Frag
.Len
) = 0;
1812 // Rest query section
1814 DnsQuery
= (DNS_QUERY_SECTION
*) (Frag
.Bulk
+ Frag
.Len
);
1816 DnsQuery
->Type
= HTONS (Type
);
1817 DnsQuery
->Class
= HTONS (Class
);
1819 Frag
.Len
+= sizeof (*DnsQuery
);
1822 // Wrap the Frag in a net buffer.
1824 *Packet
= NetbufFromExt (&Frag
, 1, 0, 0, DnsDummyExtFree
, NULL
);
1825 if (*Packet
== NULL
) {
1826 FreePool (Frag
.Bulk
);
1827 return EFI_OUT_OF_RESOURCES
;
1831 // Store the UdpIo in ProtoData.
1833 *((UINTN
*) &((*Packet
)->ProtoData
[0])) = (UINTN
) (Instance
->UdpIo
);
1839 Retransmit the packet.
1841 @param Instance The DNS instance
1842 @param Packet Retransmit the packet
1844 @retval EFI_SUCCESS The packet is retransmitted.
1845 @retval Others Failed to retransmit.
1850 IN DNS_INSTANCE
*Instance
,
1858 ASSERT (Packet
!= NULL
);
1861 // Set the requests to the listening port, other packets to the connected port
1863 Buffer
= NetbufGetByte (Packet
, 0, NULL
);
1864 ASSERT (Buffer
!= NULL
);
1866 NET_GET_REF (Packet
);
1868 Status
= UdpIoSendDatagram (
1877 if (EFI_ERROR (Status
)) {
1878 NET_PUT_REF (Packet
);
1885 The timer ticking function for the DNS services.
1887 @param Event The ticking event
1888 @param Context The DNS service instance
1893 DnsOnTimerRetransmit (
1898 DNS_SERVICE
*Service
;
1903 DNS_INSTANCE
*Instance
;
1904 LIST_ENTRY
*EntryNetMap
;
1905 NET_MAP_ITEM
*ItemNetMap
;
1906 DNS4_TOKEN_ENTRY
*Dns4TokenEntry
;
1907 DNS6_TOKEN_ENTRY
*Dns6TokenEntry
;
1909 Dns4TokenEntry
= NULL
;
1910 Dns6TokenEntry
= NULL
;
1912 Service
= (DNS_SERVICE
*) Context
;
1915 if (Service
->IpVersion
== IP_VERSION_4
) {
1917 // Iterate through all the children of the DNS service instance. Time
1918 // out the packet. If maximum retries reached, clean the Token up.
1920 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Service
->Dns4ChildrenList
) {
1921 Instance
= NET_LIST_USER_STRUCT (Entry
, DNS_INSTANCE
, Link
);
1923 EntryNetMap
= Instance
->Dns4TxTokens
.Used
.ForwardLink
;
1924 while (EntryNetMap
!= &Instance
->Dns4TxTokens
.Used
) {
1925 ItemNetMap
= NET_LIST_USER_STRUCT (EntryNetMap
, NET_MAP_ITEM
, Link
);
1926 Dns4TokenEntry
= (DNS4_TOKEN_ENTRY
*)(ItemNetMap
->Key
);
1927 if (Dns4TokenEntry
->PacketToLive
== 0 || (--Dns4TokenEntry
->PacketToLive
> 0)) {
1928 EntryNetMap
= EntryNetMap
->ForwardLink
;
1933 // Retransmit the packet if haven't reach the maxmium retry count,
1934 // otherwise exit the transfer.
1936 if (++Dns4TokenEntry
->Token
->RetryCount
< Instance
->MaxRetry
) {
1937 DnsRetransmit (Instance
, (NET_BUF
*)ItemNetMap
->Value
);
1938 EntryNetMap
= EntryNetMap
->ForwardLink
;
1941 // Maximum retries reached, clean the Token up.
1943 Dns4RemoveTokenEntry (&Instance
->Dns4TxTokens
, Dns4TokenEntry
);
1944 Dns4TokenEntry
->Token
->Status
= EFI_TIMEOUT
;
1945 gBS
->SignalEvent (Dns4TokenEntry
->Token
->Event
);
1949 // Free the sending packet.
1951 if (ItemNetMap
->Value
!= NULL
) {
1952 NetbufFree ((NET_BUF
*)(ItemNetMap
->Value
));
1955 EntryNetMap
= Instance
->Dns4TxTokens
.Used
.ForwardLink
;
1961 // Iterate through all the children of the DNS service instance. Time
1962 // out the packet. If maximum retries reached, clean the Token up.
1964 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Service
->Dns6ChildrenList
) {
1965 Instance
= NET_LIST_USER_STRUCT (Entry
, DNS_INSTANCE
, Link
);
1967 EntryNetMap
= Instance
->Dns6TxTokens
.Used
.ForwardLink
;
1968 while (EntryNetMap
!= &Instance
->Dns6TxTokens
.Used
) {
1969 ItemNetMap
= NET_LIST_USER_STRUCT (EntryNetMap
, NET_MAP_ITEM
, Link
);
1970 Dns6TokenEntry
= (DNS6_TOKEN_ENTRY
*) (ItemNetMap
->Key
);
1971 if (Dns6TokenEntry
->PacketToLive
== 0 || (--Dns6TokenEntry
->PacketToLive
> 0)) {
1972 EntryNetMap
= EntryNetMap
->ForwardLink
;
1977 // Retransmit the packet if haven't reach the maxmium retry count,
1978 // otherwise exit the transfer.
1980 if (++Dns6TokenEntry
->Token
->RetryCount
< Instance
->MaxRetry
) {
1981 DnsRetransmit (Instance
, (NET_BUF
*) ItemNetMap
->Value
);
1982 EntryNetMap
= EntryNetMap
->ForwardLink
;
1985 // Maximum retries reached, clean the Token up.
1987 Dns6RemoveTokenEntry (&Instance
->Dns6TxTokens
, Dns6TokenEntry
);
1988 Dns6TokenEntry
->Token
->Status
= EFI_TIMEOUT
;
1989 gBS
->SignalEvent (Dns6TokenEntry
->Token
->Event
);
1993 // Free the sending packet.
1995 if (ItemNetMap
->Value
!= NULL
) {
1996 NetbufFree ((NET_BUF
*) (ItemNetMap
->Value
));
1999 EntryNetMap
= Instance
->Dns6TxTokens
.Used
.ForwardLink
;
2007 The timer ticking function for the DNS driver.
2009 @param Event The ticking event
2029 // Iterate through all the DNS4 cache list.
2031 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &mDriverData
->Dns4CacheList
) {
2032 Item4
= NET_LIST_USER_STRUCT (Entry
, DNS4_CACHE
, AllCacheLink
);
2033 Item4
->DnsCache
.Timeout
--;
2036 Entry
= mDriverData
->Dns4CacheList
.ForwardLink
;
2037 while (Entry
!= &mDriverData
->Dns4CacheList
) {
2038 Item4
= NET_LIST_USER_STRUCT (Entry
, DNS4_CACHE
, AllCacheLink
);
2039 if (Item4
->DnsCache
.Timeout
== 0) {
2040 RemoveEntryList (&Item4
->AllCacheLink
);
2041 Entry
= mDriverData
->Dns4CacheList
.ForwardLink
;
2043 Entry
= Entry
->ForwardLink
;
2048 // Iterate through all the DNS6 cache list.
2050 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &mDriverData
->Dns6CacheList
) {
2051 Item6
= NET_LIST_USER_STRUCT (Entry
, DNS6_CACHE
, AllCacheLink
);
2052 Item6
->DnsCache
.Timeout
--;
2055 Entry
= mDriverData
->Dns6CacheList
.ForwardLink
;
2056 while (Entry
!= &mDriverData
->Dns6CacheList
) {
2057 Item6
= NET_LIST_USER_STRUCT (Entry
, DNS6_CACHE
, AllCacheLink
);
2058 if (Item6
->DnsCache
.Timeout
== 0) {
2059 RemoveEntryList (&Item6
->AllCacheLink
);
2060 Entry
= mDriverData
->Dns6CacheList
.ForwardLink
;
2062 Entry
= Entry
->ForwardLink
;