2 DnsDxe support functions implementation.
4 Copyright (c) 2015, 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 Ip6Mode
.IsConfigured
) {
624 Udp
->Configure (Udp
, NULL
);
625 return (BOOLEAN
) (Udp
->Configure (Udp
, UdpCfgData
) == EFI_SUCCESS
);
635 @param Instance The DNS session
636 @param UdpIo The UDP_IO instance
638 @retval EFI_SUCCESS The UDP is successfully configured for the
644 IN DNS_INSTANCE
*Instance
,
648 EFI_DNS4_CONFIG_DATA
*Config
;
649 EFI_UDP4_CONFIG_DATA UdpConfig
;
652 Config
= &Instance
->Dns4CfgData
;
654 UdpConfig
.AcceptBroadcast
= FALSE
;
655 UdpConfig
.AcceptPromiscuous
= FALSE
;
656 UdpConfig
.AcceptAnyPort
= FALSE
;
657 UdpConfig
.AllowDuplicatePort
= FALSE
;
658 UdpConfig
.TypeOfService
= 0;
659 UdpConfig
.TimeToLive
= 128;
660 UdpConfig
.DoNotFragment
= FALSE
;
661 UdpConfig
.ReceiveTimeout
= 0;
662 UdpConfig
.TransmitTimeout
= 0;
663 UdpConfig
.UseDefaultAddress
= Config
->UseDefaultSetting
;
664 UdpConfig
.SubnetMask
= Config
->SubnetMask
;
665 UdpConfig
.StationPort
= Config
->LocalPort
;
666 UdpConfig
.RemotePort
= DNS_SERVER_PORT
;
668 CopyMem (&UdpConfig
.StationAddress
, &Config
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
669 CopyMem (&UdpConfig
.RemoteAddress
, &Instance
->SessionDnsServer
.v4
, sizeof (EFI_IPv4_ADDRESS
));
671 Status
= UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, &UdpConfig
);
673 if ((Status
== EFI_NO_MAPPING
) && Dns4GetMapping (Instance
, UdpIo
, &UdpConfig
)) {
683 @param Instance The DNS session
684 @param UdpIo The UDP_IO instance
686 @retval EFI_SUCCESS The UDP is successfully configured for the
692 IN DNS_INSTANCE
*Instance
,
696 EFI_DNS6_CONFIG_DATA
*Config
;
697 EFI_UDP6_CONFIG_DATA UdpConfig
;
700 Config
= &Instance
->Dns6CfgData
;
702 UdpConfig
.AcceptPromiscuous
= FALSE
;
703 UdpConfig
.AcceptAnyPort
= FALSE
;
704 UdpConfig
.AllowDuplicatePort
= FALSE
;
705 UdpConfig
.TrafficClass
= 0;
706 UdpConfig
.HopLimit
= 128;
707 UdpConfig
.ReceiveTimeout
= 0;
708 UdpConfig
.TransmitTimeout
= 0;
709 UdpConfig
.StationPort
= Config
->LocalPort
;
710 UdpConfig
.RemotePort
= DNS_SERVER_PORT
;
711 CopyMem (&UdpConfig
.StationAddress
, &Config
->StationIp
, sizeof (EFI_IPv6_ADDRESS
));
712 CopyMem (&UdpConfig
.RemoteAddress
, &Instance
->SessionDnsServer
.v6
, sizeof (EFI_IPv6_ADDRESS
));
714 Status
= UdpIo
->Protocol
.Udp6
->Configure (UdpIo
->Protocol
.Udp6
, &UdpConfig
);
716 if ((Status
== EFI_NO_MAPPING
) && Dns6GetMapping (Instance
, UdpIo
, &UdpConfig
)) {
724 Update Dns4 cache to shared list of caches of all DNSv4 instances.
726 @param Dns4CacheList All Dns4 cache list.
727 @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache.
728 If TRUE, this function will delete matching DNS Cache entry.
729 @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter.
730 If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists.
731 @param DnsCacheEntry Entry Pointer to DNS Cache entry.
733 @retval EFI_SUCCESS Update Dns4 cache successfully.
734 @retval Others Failed to update Dns4 cache.
740 IN LIST_ENTRY
*Dns4CacheList
,
741 IN BOOLEAN DeleteFlag
,
743 IN EFI_DNS4_CACHE_ENTRY DnsCacheEntry
746 DNS4_CACHE
*NewDnsCache
;
755 // Search the database for the matching EFI_DNS_CACHE_ENTRY
757 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Dns4CacheList
) {
758 Item
= NET_LIST_USER_STRUCT (Entry
, DNS4_CACHE
, AllCacheLink
);
759 if (StrCmp (DnsCacheEntry
.HostName
, Item
->DnsCache
.HostName
) == 0 && \
760 CompareMem (DnsCacheEntry
.IpAddress
, Item
->DnsCache
.IpAddress
, sizeof (EFI_IPv4_ADDRESS
)) == 0) {
762 // This is the Dns cache entry
766 // Delete matching DNS Cache entry
768 RemoveEntryList (&Item
->AllCacheLink
);
771 } else if (Override
) {
775 Item
->DnsCache
.Timeout
= DnsCacheEntry
.Timeout
;
779 return EFI_ACCESS_DENIED
;
787 NewDnsCache
= AllocatePool (sizeof (DNS4_CACHE
));
788 if (NewDnsCache
== NULL
) {
789 return EFI_OUT_OF_RESOURCES
;
792 InitializeListHead (&NewDnsCache
->AllCacheLink
);
794 NewDnsCache
->DnsCache
.HostName
= AllocatePool (StrSize (DnsCacheEntry
.HostName
));
795 if (NewDnsCache
->DnsCache
.HostName
== NULL
) {
796 return EFI_OUT_OF_RESOURCES
;
799 CopyMem (NewDnsCache
->DnsCache
.HostName
, DnsCacheEntry
.HostName
, StrSize (DnsCacheEntry
.HostName
));
801 NewDnsCache
->DnsCache
.IpAddress
= AllocatePool (sizeof (EFI_IPv4_ADDRESS
));
802 if (NewDnsCache
->DnsCache
.IpAddress
== NULL
) {
803 return EFI_OUT_OF_RESOURCES
;
806 CopyMem (NewDnsCache
->DnsCache
.IpAddress
, DnsCacheEntry
.IpAddress
, sizeof (EFI_IPv4_ADDRESS
));
808 NewDnsCache
->DnsCache
.Timeout
= DnsCacheEntry
.Timeout
;
810 InsertTailList (Dns4CacheList
, &NewDnsCache
->AllCacheLink
);
816 Update Dns6 cache to shared list of caches of all DNSv6 instances.
818 @param Dns6CacheList All Dns6 cache list.
819 @param DeleteFlag If FALSE, this function is to add one entry to the DNS Cache.
820 If TRUE, this function will delete matching DNS Cache entry.
821 @param Override If TRUE, the matching DNS cache entry will be overwritten with the supplied parameter.
822 If FALSE, EFI_ACCESS_DENIED will be returned if the entry to be added is already exists.
823 @param DnsCacheEntry Entry Pointer to DNS Cache entry.
825 @retval EFI_SUCCESS Update Dns6 cache successfully.
826 @retval Others Failed to update Dns6 cache.
831 IN LIST_ENTRY
*Dns6CacheList
,
832 IN BOOLEAN DeleteFlag
,
834 IN EFI_DNS6_CACHE_ENTRY DnsCacheEntry
837 DNS6_CACHE
*NewDnsCache
;
846 // Search the database for the matching EFI_DNS_CACHE_ENTRY
848 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Dns6CacheList
) {
849 Item
= NET_LIST_USER_STRUCT (Entry
, DNS6_CACHE
, AllCacheLink
);
850 if (StrCmp (DnsCacheEntry
.HostName
, Item
->DnsCache
.HostName
) == 0 && \
851 CompareMem (DnsCacheEntry
.IpAddress
, Item
->DnsCache
.IpAddress
, sizeof (EFI_IPv6_ADDRESS
)) == 0) {
853 // This is the Dns cache entry
857 // Delete matching DNS Cache entry
859 RemoveEntryList (&Item
->AllCacheLink
);
862 } else if (Override
) {
866 Item
->DnsCache
.Timeout
= DnsCacheEntry
.Timeout
;
870 return EFI_ACCESS_DENIED
;
878 NewDnsCache
= AllocatePool (sizeof (DNS6_CACHE
));
879 if (NewDnsCache
== NULL
) {
880 return EFI_OUT_OF_RESOURCES
;
883 InitializeListHead (&NewDnsCache
->AllCacheLink
);
885 NewDnsCache
->DnsCache
.HostName
= AllocatePool (StrSize (DnsCacheEntry
.HostName
));
886 if (NewDnsCache
->DnsCache
.HostName
== NULL
) {
887 return EFI_OUT_OF_RESOURCES
;
890 CopyMem (NewDnsCache
->DnsCache
.HostName
, DnsCacheEntry
.HostName
, StrSize (DnsCacheEntry
.HostName
));
892 NewDnsCache
->DnsCache
.IpAddress
= AllocatePool (sizeof (EFI_IPv6_ADDRESS
));
893 if (NewDnsCache
->DnsCache
.IpAddress
== NULL
) {
894 return EFI_OUT_OF_RESOURCES
;
897 CopyMem (NewDnsCache
->DnsCache
.IpAddress
, DnsCacheEntry
.IpAddress
, sizeof (EFI_IPv6_ADDRESS
));
899 NewDnsCache
->DnsCache
.Timeout
= DnsCacheEntry
.Timeout
;
901 InsertTailList (Dns6CacheList
, &NewDnsCache
->AllCacheLink
);
907 Add Dns4 ServerIp to common list of addresses of all configured DNSv4 server.
909 @param Dns4ServerList Common list of addresses of all configured DNSv4 server.
910 @param ServerIp DNS server Ip.
912 @retval EFI_SUCCESS Add Dns4 ServerIp to common list successfully.
913 @retval Others Failed to add Dns4 ServerIp to common list.
919 IN LIST_ENTRY
*Dns4ServerList
,
920 IN EFI_IPv4_ADDRESS ServerIp
923 DNS4_SERVER_IP
*NewServerIp
;
924 DNS4_SERVER_IP
*Item
;
932 // Search the database for the matching ServerIp
934 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Dns4ServerList
) {
935 Item
= NET_LIST_USER_STRUCT (Entry
, DNS4_SERVER_IP
, AllServerLink
);
936 if (CompareMem (&Item
->Dns4ServerIp
, &ServerIp
, sizeof (EFI_IPv4_ADDRESS
)) == 0) {
947 NewServerIp
= AllocatePool (sizeof (DNS4_SERVER_IP
));
948 if (NewServerIp
== NULL
) {
949 return EFI_OUT_OF_RESOURCES
;
952 InitializeListHead (&NewServerIp
->AllServerLink
);
954 CopyMem (&NewServerIp
->Dns4ServerIp
, &ServerIp
, sizeof (EFI_IPv4_ADDRESS
));
956 InsertTailList (Dns4ServerList
, &NewServerIp
->AllServerLink
);
962 Add Dns6 ServerIp to common list of addresses of all configured DNSv6 server.
964 @param Dns6ServerList Common list of addresses of all configured DNSv6 server.
965 @param ServerIp DNS server Ip.
967 @retval EFI_SUCCESS Add Dns6 ServerIp to common list successfully.
968 @retval Others Failed to add Dns6 ServerIp to common list.
974 IN LIST_ENTRY
*Dns6ServerList
,
975 IN EFI_IPv6_ADDRESS ServerIp
978 DNS6_SERVER_IP
*NewServerIp
;
979 DNS6_SERVER_IP
*Item
;
987 // Search the database for the matching ServerIp
989 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, Dns6ServerList
) {
990 Item
= NET_LIST_USER_STRUCT (Entry
, DNS6_SERVER_IP
, AllServerLink
);
991 if (CompareMem (&Item
->Dns6ServerIp
, &ServerIp
, sizeof (EFI_IPv6_ADDRESS
)) == 0) {
1002 NewServerIp
= AllocatePool (sizeof (DNS6_SERVER_IP
));
1003 if (NewServerIp
== NULL
) {
1004 return EFI_OUT_OF_RESOURCES
;
1007 InitializeListHead (&NewServerIp
->AllServerLink
);
1009 CopyMem (&NewServerIp
->Dns6ServerIp
, &ServerIp
, sizeof (EFI_IPv6_ADDRESS
));
1011 InsertTailList (Dns6ServerList
, &NewServerIp
->AllServerLink
);
1017 Find out whether the response is valid or invalid.
1019 @param TokensMap All DNS transmittal Tokens entry.
1020 @param Identification Identification for queried packet.
1021 @param Type Type for queried packet.
1022 @param Item Return corresponding Token entry.
1024 @retval TRUE The response is valid.
1025 @retval FALSE The response is invalid.
1029 IsValidDnsResponse (
1030 IN NET_MAP
*TokensMap
,
1031 IN UINT16 Identification
,
1033 OUT NET_MAP_ITEM
**Item
1040 DNS_HEADER
*DnsHeader
;
1042 DNS_QUERY_SECTION
*QuerySection
;
1044 NET_LIST_FOR_EACH (Entry
, &TokensMap
->Used
) {
1045 *Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1046 Packet
= (NET_BUF
*) ((*Item
)->Value
);
1047 if (Packet
== NULL
){
1051 TxString
= NetbufGetByte (Packet
, 0, NULL
);
1052 ASSERT (TxString
!= NULL
);
1053 DnsHeader
= (DNS_HEADER
*) TxString
;
1054 QueryName
= (CHAR8
*) (TxString
+ sizeof (*DnsHeader
));
1055 QuerySection
= (DNS_QUERY_SECTION
*) (QueryName
+ AsciiStrLen (QueryName
) + 1);
1057 DnsHeader
->Identification
= NTOHS (DnsHeader
->Identification
);
1058 QuerySection
->Type
= NTOHS (QuerySection
->Type
);
1060 if (DnsHeader
->Identification
== Identification
&& QuerySection
->Type
== Type
) {
1074 @param Instance The DNS instance
1075 @param RxString Received buffer.
1076 @param Completed Flag to indicate that Dns response is valid.
1078 @retval EFI_SUCCESS Parse Dns Response successfully.
1079 @retval Others Failed to parse Dns Response.
1084 IN OUT DNS_INSTANCE
*Instance
,
1086 OUT BOOLEAN
*Completed
1089 DNS_HEADER
*DnsHeader
;
1092 DNS_QUERY_SECTION
*QuerySection
;
1095 DNS_ANSWER_SECTION
*AnswerSection
;
1099 DNS4_TOKEN_ENTRY
*Dns4TokenEntry
;
1100 DNS6_TOKEN_ENTRY
*Dns6TokenEntry
;
1103 UINT32 AnswerSectionNum
;
1105 EFI_IPv4_ADDRESS
*HostAddr4
;
1106 EFI_IPv6_ADDRESS
*HostAddr6
;
1108 EFI_DNS4_CACHE_ENTRY
*Dns4CacheEntry
;
1109 EFI_DNS6_CACHE_ENTRY
*Dns6CacheEntry
;
1116 Dns4TokenEntry
= NULL
;
1117 Dns6TokenEntry
= NULL
;
1120 AnswerSectionNum
= 0;
1125 Dns4CacheEntry
= NULL
;
1126 Dns6CacheEntry
= NULL
;
1129 Status
= EFI_SUCCESS
;
1134 DnsHeader
= (DNS_HEADER
*) RxString
;
1136 DnsHeader
->Identification
= NTOHS (DnsHeader
->Identification
);
1137 DnsHeader
->Flags
.Uint16
= NTOHS (DnsHeader
->Flags
.Uint16
);
1138 DnsHeader
->QuestionsNum
= NTOHS (DnsHeader
->QuestionsNum
);
1139 DnsHeader
->AnswersNum
= NTOHS (DnsHeader
->AnswersNum
);
1140 DnsHeader
->AuthorityNum
= NTOHS (DnsHeader
->AuthorityNum
);
1141 DnsHeader
->AditionalNum
= NTOHS (DnsHeader
->AditionalNum
);
1146 QueryName
= (CHAR8
*) (RxString
+ sizeof (*DnsHeader
));
1149 // Get query section
1151 QuerySection
= (DNS_QUERY_SECTION
*) (QueryName
+ AsciiStrLen (QueryName
) + 1);
1152 QuerySection
->Type
= NTOHS (QuerySection
->Type
);
1153 QuerySection
->Class
= NTOHS (QuerySection
->Class
);
1158 AnswerName
= (CHAR8
*) QuerySection
+ sizeof (*QuerySection
);
1160 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1163 // Check DnsResponse Validity, if so, also get a valid NET_MAP_ITEM.
1165 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1166 if (!IsValidDnsResponse (&Instance
->Dns4TxTokens
, DnsHeader
->Identification
, QuerySection
->Type
, &Item
)) {
1168 Status
= EFI_ABORTED
;
1171 ASSERT (Item
!= NULL
);
1172 Dns4TokenEntry
= (DNS4_TOKEN_ENTRY
*) (Item
->Key
);
1174 if (!IsValidDnsResponse (&Instance
->Dns6TxTokens
, DnsHeader
->Identification
, QuerySection
->Type
, &Item
)) {
1176 Status
= EFI_ABORTED
;
1179 ASSERT (Item
!= NULL
);
1180 Dns6TokenEntry
= (DNS6_TOKEN_ENTRY
*) (Item
->Key
);
1184 // Continue Check Some Errors.
1186 if (DnsHeader
->Flags
.Bits
.RCode
!= DNS_FLAGS_RCODE_NO_ERROR
|| DnsHeader
->AnswersNum
< 1 || \
1187 DnsHeader
->Flags
.Bits
.QR
!= DNS_FLAGS_QR_RESPONSE
|| QuerySection
->Class
!= DNS_CLASS_INET
) {
1188 Status
= EFI_ABORTED
;
1193 // Free the sending packet.
1195 if (Item
->Value
!= NULL
) {
1196 NetbufFree ((NET_BUF
*) (Item
->Value
));
1200 // Check the Query type, do some buffer allocations.
1202 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1203 ASSERT (Dns4TokenEntry
!= NULL
);
1204 if (QuerySection
->Type
== DNS_TYPE_A
) {
1205 Dns4TokenEntry
->Token
->RspData
.H2AData
= AllocatePool (sizeof (DNS_HOST_TO_ADDR_DATA
));
1206 ASSERT (Dns4TokenEntry
->Token
->RspData
.H2AData
!= NULL
);
1207 Dns4TokenEntry
->Token
->RspData
.H2AData
->IpList
= AllocatePool (DnsHeader
->AnswersNum
* sizeof (EFI_IPv4_ADDRESS
));
1208 ASSERT (Dns4TokenEntry
->Token
->RspData
.H2AData
->IpList
!= NULL
);
1210 Status
= EFI_UNSUPPORTED
;
1214 ASSERT (Dns6TokenEntry
!= NULL
);
1215 if (QuerySection
->Type
== DNS_TYPE_AAAA
) {
1216 Dns6TokenEntry
->Token
->RspData
.H2AData
= AllocatePool (sizeof (DNS6_HOST_TO_ADDR_DATA
));
1217 ASSERT (Dns6TokenEntry
->Token
->RspData
.H2AData
!= NULL
);
1218 Dns6TokenEntry
->Token
->RspData
.H2AData
->IpList
= AllocatePool (DnsHeader
->AnswersNum
* sizeof (EFI_IPv6_ADDRESS
));
1219 ASSERT (Dns6TokenEntry
->Token
->RspData
.H2AData
->IpList
!= NULL
);
1221 Status
= EFI_UNSUPPORTED
;
1227 // Processing AnswerSection.
1229 while (AnswerSectionNum
< DnsHeader
->AnswersNum
) {
1231 // Answer name should be PTR.
1233 ASSERT ((*(UINT8
*) AnswerName
& 0xC0) == 0xC0);
1236 // Get Answer section.
1238 AnswerSection
= (DNS_ANSWER_SECTION
*) (AnswerName
+ sizeof (UINT16
));
1239 AnswerSection
->Type
= NTOHS (AnswerSection
->Type
);
1240 AnswerSection
->Class
= NTOHS (AnswerSection
->Class
);
1241 AnswerSection
->Ttl
= NTOHL (AnswerSection
->Ttl
);
1242 AnswerSection
->DataLength
= NTOHS (AnswerSection
->DataLength
);
1244 ASSERT (AnswerSection
->Class
== DNS_CLASS_INET
);
1246 if (AnswerSection
->Type
== QuerySection
->Type
) {
1247 switch (AnswerSection
->Type
) {
1250 // This is address entry, get Data.
1252 ASSERT (Dns4TokenEntry
!= NULL
&& AnswerSection
->DataLength
== 4);
1254 HostAddr4
= Dns4TokenEntry
->Token
->RspData
.H2AData
->IpList
;
1255 AnswerData
= (UINT8
*) AnswerSection
+ sizeof (*AnswerSection
);
1256 CopyMem (&HostAddr4
[IpCount
], AnswerData
, sizeof (EFI_IPv4_ADDRESS
));
1259 // Update DNS cache dynamically.
1261 if (Dns4CacheEntry
!= NULL
) {
1262 if (Dns4CacheEntry
->HostName
!= NULL
) {
1263 FreePool (Dns4CacheEntry
->HostName
);
1266 if (Dns4CacheEntry
->IpAddress
!= NULL
) {
1267 FreePool (Dns4CacheEntry
->IpAddress
);
1270 FreePool (Dns4CacheEntry
);
1274 // Allocate new CacheEntry pool.
1276 Dns4CacheEntry
= AllocateZeroPool (sizeof (EFI_DNS4_CACHE_ENTRY
));
1277 ASSERT (Dns4CacheEntry
!= NULL
);
1278 Dns4CacheEntry
->HostName
= AllocateZeroPool (2 * (StrLen(Dns4TokenEntry
->QueryHostName
) + 1));
1279 ASSERT (Dns4CacheEntry
->HostName
!= NULL
);
1280 CopyMem (Dns4CacheEntry
->HostName
, Dns4TokenEntry
->QueryHostName
, 2 * (StrLen(Dns4TokenEntry
->QueryHostName
) + 1));
1281 Dns4CacheEntry
->IpAddress
= AllocateZeroPool (sizeof (EFI_IPv4_ADDRESS
));
1282 ASSERT (Dns4CacheEntry
->IpAddress
!= NULL
);
1283 CopyMem (Dns4CacheEntry
->IpAddress
, AnswerData
, sizeof (EFI_IPv4_ADDRESS
));
1284 Dns4CacheEntry
->Timeout
= AnswerSection
->Ttl
;
1286 UpdateDns4Cache (&mDriverData
->Dns4CacheList
, FALSE
, TRUE
, *Dns4CacheEntry
);
1292 // This is address entry, get Data.
1294 ASSERT (Dns6TokenEntry
!= NULL
&& AnswerSection
->DataLength
== 16);
1296 HostAddr6
= Dns6TokenEntry
->Token
->RspData
.H2AData
->IpList
;
1297 AnswerData
= (UINT8
*) AnswerSection
+ sizeof (*AnswerSection
);
1298 CopyMem (&HostAddr6
[IpCount
], AnswerData
, sizeof (EFI_IPv6_ADDRESS
));
1301 // Update DNS cache dynamically.
1303 if (Dns6CacheEntry
!= NULL
) {
1304 if (Dns6CacheEntry
->HostName
!= NULL
) {
1305 FreePool (Dns6CacheEntry
->HostName
);
1308 if (Dns6CacheEntry
->IpAddress
!= NULL
) {
1309 FreePool (Dns6CacheEntry
->IpAddress
);
1312 FreePool (Dns6CacheEntry
);
1316 // Allocate new CacheEntry pool.
1318 Dns6CacheEntry
= AllocateZeroPool (sizeof (EFI_DNS6_CACHE_ENTRY
));
1319 ASSERT (Dns6CacheEntry
!= NULL
);
1320 Dns6CacheEntry
->HostName
= AllocateZeroPool (2 * (StrLen(Dns6TokenEntry
->QueryHostName
) + 1));
1321 ASSERT (Dns6CacheEntry
->HostName
!= NULL
);
1322 CopyMem (Dns6CacheEntry
->HostName
, Dns6TokenEntry
->QueryHostName
, 2 * (StrLen(Dns6TokenEntry
->QueryHostName
) + 1));
1323 Dns6CacheEntry
->IpAddress
= AllocateZeroPool (sizeof (EFI_IPv6_ADDRESS
));
1324 ASSERT (Dns6CacheEntry
->IpAddress
!= NULL
);
1325 CopyMem (Dns6CacheEntry
->IpAddress
, AnswerData
, sizeof (EFI_IPv6_ADDRESS
));
1326 Dns6CacheEntry
->Timeout
= AnswerSection
->Ttl
;
1328 UpdateDns6Cache (&mDriverData
->Dns6CacheList
, FALSE
, TRUE
, *Dns6CacheEntry
);
1333 Status
= EFI_UNSUPPORTED
;
1341 AnswerName
= (CHAR8
*) AnswerSection
+ sizeof (*AnswerSection
) + AnswerSection
->DataLength
;
1342 AnswerSectionNum
++;
1345 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1346 ASSERT (Dns4TokenEntry
!= NULL
);
1347 if (QuerySection
->Type
== DNS_TYPE_A
) {
1348 Dns4TokenEntry
->Token
->RspData
.H2AData
->IpCount
= IpCount
;
1350 Status
= EFI_UNSUPPORTED
;
1354 ASSERT (Dns6TokenEntry
!= NULL
);
1355 if (QuerySection
->Type
== DNS_TYPE_AAAA
) {
1356 Dns6TokenEntry
->Token
->RspData
.H2AData
->IpCount
= IpCount
;
1358 Status
= EFI_UNSUPPORTED
;
1364 // Parsing is complete, SignalEvent here.
1366 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1367 ASSERT (Dns4TokenEntry
!= NULL
);
1368 Dns4RemoveTokenEntry (&Instance
->Dns4TxTokens
, Dns4TokenEntry
);
1369 Dns4TokenEntry
->Token
->Status
= EFI_SUCCESS
;
1370 if (Dns4TokenEntry
->Token
->Event
!= NULL
) {
1371 gBS
->SignalEvent (Dns4TokenEntry
->Token
->Event
);
1375 ASSERT (Dns6TokenEntry
!= NULL
);
1376 Dns6RemoveTokenEntry (&Instance
->Dns6TxTokens
, Dns6TokenEntry
);
1377 Dns6TokenEntry
->Token
->Status
= EFI_SUCCESS
;
1378 if (Dns6TokenEntry
->Token
->Event
!= NULL
) {
1379 gBS
->SignalEvent (Dns6TokenEntry
->Token
->Event
);
1385 // Free allocated CacheEntry pool.
1387 if (Dns4CacheEntry
!= NULL
) {
1388 if (Dns4CacheEntry
->HostName
!= NULL
) {
1389 FreePool (Dns4CacheEntry
->HostName
);
1392 if (Dns4CacheEntry
->IpAddress
!= NULL
) {
1393 FreePool (Dns4CacheEntry
->IpAddress
);
1396 FreePool (Dns4CacheEntry
);
1399 if (Dns6CacheEntry
!= NULL
) {
1400 if (Dns6CacheEntry
->HostName
!= NULL
) {
1401 FreePool (Dns6CacheEntry
->HostName
);
1404 if (Dns6CacheEntry
->IpAddress
!= NULL
) {
1405 FreePool (Dns6CacheEntry
->IpAddress
);
1408 FreePool (Dns6CacheEntry
);
1412 gBS
->RestoreTPL (OldTpl
);
1417 Parse response packet.
1419 @param Packet The packets received.
1420 @param EndPoint The local/remote UDP access point
1421 @param IoStatus The status of the UDP receive
1422 @param Context The opaque parameter to the function.
1427 DnsOnPacketReceived (
1429 UDP_END_POINT
*EndPoint
,
1430 EFI_STATUS IoStatus
,
1434 DNS_INSTANCE
*Instance
;
1440 Instance
= (DNS_INSTANCE
*) Context
;
1441 NET_CHECK_SIGNATURE (Instance
, DNS_INSTANCE_SIGNATURE
);
1446 if (EFI_ERROR (IoStatus
)) {
1450 ASSERT (Packet
!= NULL
);
1452 RcvString
= NetbufGetByte (Packet
, 0, NULL
);
1453 ASSERT (RcvString
!= NULL
);
1456 // Parse Dns Response
1458 ParseDnsResponse (Instance
, RcvString
, &Completed
);
1462 if (Packet
!= NULL
) {
1463 NetbufFree (Packet
);
1467 UdpIoRecvDatagram (Instance
->UdpIo
, DnsOnPacketReceived
, Instance
, 0);
1472 Release the net buffer when packet is sent.
1474 @param Packet The packets received.
1475 @param EndPoint The local/remote UDP access point
1476 @param IoStatus The status of the UDP receive
1477 @param Context The opaque parameter to the function.
1484 UDP_END_POINT
*EndPoint
,
1485 EFI_STATUS IoStatus
,
1489 DNS_INSTANCE
*Instance
;
1492 DNS4_TOKEN_ENTRY
*Dns4TokenEntry
;
1493 DNS6_TOKEN_ENTRY
*Dns6TokenEntry
;
1495 Dns4TokenEntry
= NULL
;
1496 Dns6TokenEntry
= NULL
;
1498 Instance
= (DNS_INSTANCE
*) Context
;
1499 NET_CHECK_SIGNATURE (Instance
, DNS_INSTANCE_SIGNATURE
);
1501 if (Instance
->Service
->IpVersion
== IP_VERSION_4
) {
1502 NET_LIST_FOR_EACH (Entry
, &Instance
->Dns4TxTokens
.Used
) {
1503 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1504 if (Packet
== (NET_BUF
*)(Item
->Value
)) {
1505 Dns4TokenEntry
= ((DNS4_TOKEN_ENTRY
*)Item
->Key
);
1506 Dns4TokenEntry
->PacketToLive
= Dns4TokenEntry
->Token
->RetryInterval
;
1511 NET_LIST_FOR_EACH (Entry
, &Instance
->Dns6TxTokens
.Used
) {
1512 Item
= NET_LIST_USER_STRUCT (Entry
, NET_MAP_ITEM
, Link
);
1513 if (Packet
== (NET_BUF
*)(Item
->Value
)) {
1514 Dns6TokenEntry
= ((DNS6_TOKEN_ENTRY
*)Item
->Key
);
1515 Dns6TokenEntry
->PacketToLive
= Dns6TokenEntry
->Token
->RetryInterval
;
1521 NetbufFree (Packet
);
1525 Query request information.
1527 @param Instance The DNS instance
1528 @param Packet The packet for querying request information.
1530 @retval EFI_SUCCESS Query request information successfully.
1531 @retval Others Failed to query request information.
1536 IN DNS_INSTANCE
*Instance
,
1543 // Ready to receive the DNS response.
1545 if (Instance
->UdpIo
->RecvRequest
== NULL
) {
1546 Status
= UdpIoRecvDatagram (Instance
->UdpIo
, DnsOnPacketReceived
, Instance
, 0);
1547 if (EFI_ERROR (Status
)) {
1553 // Transmit the DNS packet.
1555 NET_GET_REF (Packet
);
1557 Status
= UdpIoSendDatagram (Instance
->UdpIo
, Packet
, NULL
, NULL
, DnsOnPacketSent
, Instance
);
1563 Construct the Packet to query Ip.
1565 @param Instance The DNS instance
1566 @param HostName Queried HostName
1567 @param Type DNS query Type
1568 @param Packet The packet for querying Ip
1570 @retval EFI_SUCCESS The packet is constructed.
1571 @retval Others Failed to construct the Packet.
1575 ConstructDNSQueryIp (
1576 IN DNS_INSTANCE
*Instance
,
1577 IN CHAR16
*HostName
,
1579 OUT NET_BUF
**Packet
1583 DNS_HEADER
*DnsHeader
;
1585 DNS_QUERY_SECTION
*QuerySection
;
1592 Frag
.Bulk
= AllocatePool (DNS_DEFAULT_BLKSIZE
* sizeof (UINT8
));
1593 if (Frag
.Bulk
== NULL
) {
1594 return EFI_OUT_OF_RESOURCES
;
1600 DnsHeader
= (DNS_HEADER
*) Frag
.Bulk
;
1601 DnsHeader
->Identification
= (UINT16
)NET_RANDOM (NetRandomInitSeed());
1602 DnsHeader
->Flags
.Uint16
= 0x0000;
1603 DnsHeader
->Flags
.Bits
.RD
= 1;
1604 DnsHeader
->Flags
.Bits
.OpCode
= DNS_FLAGS_OPCODE_STANDARD
;
1605 DnsHeader
->Flags
.Bits
.QR
= DNS_FLAGS_QR_QUERY
;
1606 DnsHeader
->QuestionsNum
= 1;
1607 DnsHeader
->AnswersNum
= 0;
1608 DnsHeader
->AuthorityNum
= 0;
1609 DnsHeader
->AditionalNum
= 0;
1611 DnsHeader
->Identification
= HTONS (DnsHeader
->Identification
);
1612 DnsHeader
->Flags
.Uint16
= HTONS (DnsHeader
->Flags
.Uint16
);
1613 DnsHeader
->QuestionsNum
= HTONS (DnsHeader
->QuestionsNum
);
1614 DnsHeader
->AnswersNum
= HTONS (DnsHeader
->AnswersNum
);
1615 DnsHeader
->AuthorityNum
= HTONS (DnsHeader
->AuthorityNum
);
1616 DnsHeader
->AditionalNum
= HTONS (DnsHeader
->AditionalNum
);
1618 Frag
.Len
= sizeof (*DnsHeader
);
1623 QueryName
= (CHAR8
*) (Frag
.Bulk
+ Frag
.Len
);
1627 for (Index
= 0; HostName
[Index
] != 0; Index
++) {
1628 *Tail
= (CHAR8
) HostName
[Index
];
1630 *Header
= (CHAR8
) Len
;
1639 *Header
= (CHAR8
) Len
;
1641 Frag
.Len
= (UINT32
) (Frag
.Len
+ StrLen (HostName
) + 2); /// 1 for header, 1 for tail.
1644 // Rest query section
1646 QuerySection
= (DNS_QUERY_SECTION
*) (Frag
.Bulk
+ Frag
.Len
);
1647 QuerySection
->Type
= Type
;
1648 QuerySection
->Class
= DNS_CLASS_INET
;
1650 QuerySection
->Type
= HTONS (QuerySection
->Type
);
1651 QuerySection
->Class
= HTONS (QuerySection
->Class
);
1653 Frag
.Len
+= sizeof (*QuerySection
);
1656 // Wrap the Frag in a net buffer.
1658 *Packet
= NetbufFromExt (&Frag
, 1, 0, 0, DnsDummyExtFree
, NULL
);
1659 if (*Packet
== NULL
) {
1660 FreePool (Frag
.Bulk
);
1661 return EFI_OUT_OF_RESOURCES
;
1665 // Store the UdpIo in ProtoData.
1667 *((UINTN
*) &((*Packet
)->ProtoData
[0])) = (UINTN
) (Instance
->UdpIo
);
1673 Retransmit the packet.
1675 @param Instance The DNS instance
1676 @param Packet Retransmit the packet
1678 @retval EFI_SUCCESS The packet is retransmitted.
1679 @retval Others Failed to retransmit.
1684 IN DNS_INSTANCE
*Instance
,
1692 ASSERT (Packet
!= NULL
);
1695 // Set the requests to the listening port, other packets to the connected port
1697 Buffer
= NetbufGetByte (Packet
, 0, NULL
);
1698 ASSERT (Buffer
!= NULL
);
1700 NET_GET_REF (Packet
);
1702 Status
= UdpIoSendDatagram (
1711 if (EFI_ERROR (Status
)) {
1712 NET_PUT_REF (Packet
);
1719 The timer ticking function for the DNS services.
1721 @param Event The ticking event
1722 @param Context The DNS service instance
1727 DnsOnTimerRetransmit (
1732 DNS_SERVICE
*Service
;
1737 DNS_INSTANCE
*Instance
;
1738 LIST_ENTRY
*EntryNetMap
;
1739 NET_MAP_ITEM
*ItemNetMap
;
1740 DNS4_TOKEN_ENTRY
*Dns4TokenEntry
;
1741 DNS6_TOKEN_ENTRY
*Dns6TokenEntry
;
1743 Dns4TokenEntry
= NULL
;
1744 Dns6TokenEntry
= NULL
;
1746 Service
= (DNS_SERVICE
*) Context
;
1749 if (Service
->IpVersion
== IP_VERSION_4
) {
1751 // Iterate through all the children of the DNS service instance. Time
1752 // out the packet. If maximum retries reached, clean the Token up.
1754 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Service
->Dns4ChildrenList
) {
1755 Instance
= NET_LIST_USER_STRUCT (Entry
, DNS_INSTANCE
, Link
);
1757 EntryNetMap
= Instance
->Dns4TxTokens
.Used
.ForwardLink
;
1758 while (EntryNetMap
!= &Instance
->Dns4TxTokens
.Used
) {
1759 ItemNetMap
= NET_LIST_USER_STRUCT (EntryNetMap
, NET_MAP_ITEM
, Link
);
1760 Dns4TokenEntry
= (DNS4_TOKEN_ENTRY
*)(ItemNetMap
->Key
);
1761 if (Dns4TokenEntry
->PacketToLive
== 0 || (--Dns4TokenEntry
->PacketToLive
> 0)) {
1762 EntryNetMap
= EntryNetMap
->ForwardLink
;
1767 // Retransmit the packet if haven't reach the maxmium retry count,
1768 // otherwise exit the transfer.
1770 if (++Dns4TokenEntry
->Token
->RetryCount
< Instance
->MaxRetry
) {
1771 DnsRetransmit (Instance
, (NET_BUF
*)ItemNetMap
->Value
);
1772 EntryNetMap
= EntryNetMap
->ForwardLink
;
1775 // Maximum retries reached, clean the Token up.
1777 Dns4RemoveTokenEntry (&Instance
->Dns4TxTokens
, Dns4TokenEntry
);
1778 Dns4TokenEntry
->Token
->Status
= EFI_TIMEOUT
;
1779 gBS
->SignalEvent (Dns4TokenEntry
->Token
->Event
);
1783 // Free the sending packet.
1785 if (ItemNetMap
->Value
!= NULL
) {
1786 NetbufFree ((NET_BUF
*)(ItemNetMap
->Value
));
1789 EntryNetMap
= Instance
->Dns4TxTokens
.Used
.ForwardLink
;
1795 // Iterate through all the children of the DNS service instance. Time
1796 // out the packet. If maximum retries reached, clean the Token up.
1798 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &Service
->Dns6ChildrenList
) {
1799 Instance
= NET_LIST_USER_STRUCT (Entry
, DNS_INSTANCE
, Link
);
1801 EntryNetMap
= Instance
->Dns6TxTokens
.Used
.ForwardLink
;
1802 while (EntryNetMap
!= &Instance
->Dns6TxTokens
.Used
) {
1803 ItemNetMap
= NET_LIST_USER_STRUCT (EntryNetMap
, NET_MAP_ITEM
, Link
);
1804 Dns6TokenEntry
= (DNS6_TOKEN_ENTRY
*) (ItemNetMap
->Key
);
1805 if (Dns6TokenEntry
->PacketToLive
== 0 || (--Dns6TokenEntry
->PacketToLive
> 0)) {
1806 EntryNetMap
= EntryNetMap
->ForwardLink
;
1811 // Retransmit the packet if haven't reach the maxmium retry count,
1812 // otherwise exit the transfer.
1814 if (++Dns6TokenEntry
->Token
->RetryCount
< Instance
->MaxRetry
) {
1815 DnsRetransmit (Instance
, (NET_BUF
*) ItemNetMap
->Value
);
1816 EntryNetMap
= EntryNetMap
->ForwardLink
;
1819 // Maximum retries reached, clean the Token up.
1821 Dns6RemoveTokenEntry (&Instance
->Dns6TxTokens
, Dns6TokenEntry
);
1822 Dns6TokenEntry
->Token
->Status
= EFI_TIMEOUT
;
1823 gBS
->SignalEvent (Dns6TokenEntry
->Token
->Event
);
1827 // Free the sending packet.
1829 if (ItemNetMap
->Value
!= NULL
) {
1830 NetbufFree ((NET_BUF
*) (ItemNetMap
->Value
));
1833 EntryNetMap
= Instance
->Dns6TxTokens
.Used
.ForwardLink
;
1841 The timer ticking function for the DNS driver.
1843 @param Event The ticking event
1863 // Iterate through all the DNS4 cache list.
1865 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &mDriverData
->Dns4CacheList
) {
1866 Item4
= NET_LIST_USER_STRUCT (Entry
, DNS4_CACHE
, AllCacheLink
);
1867 Item4
->DnsCache
.Timeout
--;
1870 Entry
= mDriverData
->Dns4CacheList
.ForwardLink
;
1871 while (Entry
!= &mDriverData
->Dns4CacheList
) {
1872 Item4
= NET_LIST_USER_STRUCT (Entry
, DNS4_CACHE
, AllCacheLink
);
1873 if (Item4
->DnsCache
.Timeout
<=0) {
1874 RemoveEntryList (&Item4
->AllCacheLink
);
1875 Entry
= mDriverData
->Dns4CacheList
.ForwardLink
;
1877 Entry
= Entry
->ForwardLink
;
1882 // Iterate through all the DNS6 cache list.
1884 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &mDriverData
->Dns6CacheList
) {
1885 Item6
= NET_LIST_USER_STRUCT (Entry
, DNS6_CACHE
, AllCacheLink
);
1886 Item6
->DnsCache
.Timeout
--;
1889 Entry
= mDriverData
->Dns6CacheList
.ForwardLink
;
1890 while (Entry
!= &mDriverData
->Dns6CacheList
) {
1891 Item6
= NET_LIST_USER_STRUCT (Entry
, DNS6_CACHE
, AllCacheLink
);
1892 if (Item6
->DnsCache
.Timeout
<=0) {
1893 RemoveEntryList (&Item6
->AllCacheLink
);
1894 Entry
= mDriverData
->Dns6CacheList
.ForwardLink
;
1896 Entry
= Entry
->ForwardLink
;