]> git.proxmox.com Git - mirror_edk2.git/commitdiff
NetworkPkg/DnsDxe: Handle CNAME type responded from the name server
authorJiaxin Wu <jiaxin.wu@intel.com>
Tue, 6 Sep 2016 03:23:38 +0000 (11:23 +0800)
committerJiaxin Wu <jiaxin.wu@intel.com>
Wed, 7 Sep 2016 06:28:17 +0000 (14:28 +0800)
v2:
* Code refine.
* For DnsCache, the minimum value of TTL is selected between CNAME and A/AAAA record.

According RFC 1034 - 3.6.2, if the query name is an alias, the name server
will include the CNAME record in the response and restart the query at the
domain name specified in the data field of the CNAME record. RFC also provides
one example server action when A query received:

Suppose a name server was processing a query with for USCISIC.ARPA, asking for
type A information, and had the following resource records:
USC-ISIC.ARPA IN CNAME C.ISI.EDU
C.ISI.EDU     IN A     10.0.0.52
Both of these RRs would be returned in the response to the type A query.

Currently, DnsDxe driver doesn't handle the CNAME type response, which will cause
any exception result. The driver need continue the packet parsing while CNAME type
record parsed. So, this patch is used to handle it correctly.

Cc: Hegde Nagaraj P <nagaraj-p.hegde@hpe.com>
Cc: Fu Siyuan <siyuan.fu@intel.com>
Cc: Ye Ting <ting.ye@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiaxin Wu <jiaxin.wu@intel.com>
Reviewed-by: Fu Siyuan <siyuan.fu@intel.com>
Tested-by: Hegde Nagaraj P <nagaraj-p.hegde@hpe.com>
NetworkPkg/DnsDxe/DnsImpl.c

index 360f68e1a4941a73b6db36cb59b255d416b0bcc9..cfaa4c78a936cb9aac9e4cfea577c9be7d266e39 100644 (file)
@@ -1127,6 +1127,7 @@ ParseDnsResponse (
   UINT32                IpCount;\r
   UINT32                RRCount;\r
   UINT32                AnswerSectionNum;\r
+  UINT32                CNameTtl;\r
   \r
   EFI_IPv4_ADDRESS      *HostAddr4;\r
   EFI_IPv6_ADDRESS      *HostAddr6;\r
@@ -1148,6 +1149,7 @@ ParseDnsResponse (
   IpCount          = 0;\r
   RRCount          = 0;\r
   AnswerSectionNum = 0;\r
+  CNameTtl         = 0;\r
   \r
   HostAddr4        = NULL;\r
   HostAddr6        = NULL;\r
@@ -1233,13 +1235,6 @@ ParseDnsResponse (
       Status = EFI_ABORTED;\r
       goto ON_EXIT;\r
   }\r
-\r
-  //\r
-  // Free the sending packet.\r
-  //\r
-  if (Item->Value != NULL) {\r
-    NetbufFree ((NET_BUF *) (Item->Value));\r
-  }\r
   \r
   //\r
   // Do some buffer allocations.\r
@@ -1290,12 +1285,12 @@ ParseDnsResponse (
       //\r
       Dns6TokenEntry->Token->RspData.GLookupData = AllocatePool (sizeof (DNS_RESOURCE_RECORD));\r
       if (Dns6TokenEntry->Token->RspData.GLookupData == NULL) {\r
-        Status = EFI_UNSUPPORTED;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
         goto ON_EXIT;\r
       }\r
       Dns6TokenEntry->Token->RspData.GLookupData->RRList = AllocatePool (DnsHeader->AnswersNum * sizeof (DNS_RESOURCE_RECORD));\r
       if (Dns6TokenEntry->Token->RspData.GLookupData->RRList == NULL) {\r
-        Status = EFI_UNSUPPORTED;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
         goto ON_EXIT;\r
       }\r
     } else {\r
@@ -1305,12 +1300,12 @@ ParseDnsResponse (
       if (QuerySection->Type == DNS_TYPE_AAAA) {\r
         Dns6TokenEntry->Token->RspData.H2AData = AllocatePool (sizeof (DNS6_HOST_TO_ADDR_DATA));\r
         if (Dns6TokenEntry->Token->RspData.H2AData == NULL) {\r
-          Status = EFI_UNSUPPORTED;\r
+          Status = EFI_OUT_OF_RESOURCES;\r
           goto ON_EXIT;\r
         }\r
         Dns6TokenEntry->Token->RspData.H2AData->IpList = AllocatePool (DnsHeader->AnswersNum * sizeof (EFI_IPv6_ADDRESS));\r
         if (Dns6TokenEntry->Token->RspData.H2AData->IpList == NULL) {\r
-          Status = EFI_UNSUPPORTED;\r
+          Status = EFI_OUT_OF_RESOURCES;\r
           goto ON_EXIT;\r
         }\r
       } else {\r
@@ -1320,6 +1315,8 @@ ParseDnsResponse (
     }\r
   }\r
 \r
+  Status = EFI_NOT_FOUND;\r
+\r
   //\r
   // Processing AnswerSection.\r
   //\r
@@ -1350,7 +1347,7 @@ ParseDnsResponse (
       //\r
       Dns4RR[RRCount].QName = AllocateZeroPool (AsciiStrLen (QueryName) + 1);\r
       if (Dns4RR[RRCount].QName == NULL) {\r
-        Status = EFI_UNSUPPORTED;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
         goto ON_EXIT;\r
       }\r
       CopyMem (Dns4RR[RRCount].QName, QueryName, AsciiStrLen (QueryName));\r
@@ -1360,12 +1357,13 @@ ParseDnsResponse (
       Dns4RR[RRCount].DataLength = AnswerSection->DataLength;\r
       Dns4RR[RRCount].RData = AllocateZeroPool (Dns4RR[RRCount].DataLength);\r
       if (Dns4RR[RRCount].RData == NULL) {\r
-        Status = EFI_UNSUPPORTED;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
         goto ON_EXIT;\r
       }\r
       CopyMem (Dns4RR[RRCount].RData, AnswerData, Dns4RR[RRCount].DataLength);\r
       \r
       RRCount ++;\r
+      Status = EFI_SUCCESS;\r
     } else if (Instance->Service->IpVersion == IP_VERSION_6 && Dns6TokenEntry->GeneralLookUp) {\r
       Dns6RR = Dns6TokenEntry->Token->RspData.GLookupData->RRList;\r
       AnswerData = (UINT8 *) AnswerSection + sizeof (*AnswerSection);\r
@@ -1375,7 +1373,7 @@ ParseDnsResponse (
       //\r
       Dns6RR[RRCount].QName = AllocateZeroPool (AsciiStrLen (QueryName) + 1);\r
       if (Dns6RR[RRCount].QName == NULL) {\r
-        Status = EFI_UNSUPPORTED;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
         goto ON_EXIT;\r
       }\r
       CopyMem (Dns6RR[RRCount].QName, QueryName, AsciiStrLen (QueryName));\r
@@ -1385,12 +1383,13 @@ ParseDnsResponse (
       Dns6RR[RRCount].DataLength = AnswerSection->DataLength;\r
       Dns6RR[RRCount].RData = AllocateZeroPool (Dns6RR[RRCount].DataLength);\r
       if (Dns6RR[RRCount].RData == NULL) {\r
-        Status = EFI_UNSUPPORTED;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
         goto ON_EXIT;\r
       }\r
       CopyMem (Dns6RR[RRCount].RData, AnswerData, Dns6RR[RRCount].DataLength);\r
       \r
       RRCount ++;\r
+      Status = EFI_SUCCESS;\r
     } else {\r
       //\r
       // It's not the GeneralLookUp querying. \r
@@ -1427,26 +1426,32 @@ ParseDnsResponse (
         //\r
         Dns4CacheEntry = AllocateZeroPool (sizeof (EFI_DNS4_CACHE_ENTRY));\r
         if (Dns4CacheEntry == NULL) {\r
-          Status = EFI_UNSUPPORTED;\r
+          Status = EFI_OUT_OF_RESOURCES;\r
           goto ON_EXIT;\r
         }\r
         Dns4CacheEntry->HostName = AllocateZeroPool (2 * (StrLen(Dns4TokenEntry->QueryHostName) + 1));\r
         if (Dns4CacheEntry->HostName == NULL) {\r
-          Status = EFI_UNSUPPORTED;\r
+          Status = EFI_OUT_OF_RESOURCES;\r
           goto ON_EXIT;\r
         }\r
         CopyMem (Dns4CacheEntry->HostName, Dns4TokenEntry->QueryHostName, 2 * (StrLen(Dns4TokenEntry->QueryHostName) + 1));\r
         Dns4CacheEntry->IpAddress = AllocateZeroPool (sizeof (EFI_IPv4_ADDRESS));\r
         if (Dns4CacheEntry->IpAddress == NULL) {\r
-          Status = EFI_UNSUPPORTED;\r
+          Status = EFI_OUT_OF_RESOURCES;\r
           goto ON_EXIT;\r
         }\r
         CopyMem (Dns4CacheEntry->IpAddress, AnswerData, sizeof (EFI_IPv4_ADDRESS));\r
-        Dns4CacheEntry->Timeout = AnswerSection->Ttl;\r
+\r
+        if (CNameTtl != 0 && AnswerSection->Ttl != 0) {\r
+          Dns4CacheEntry->Timeout = MIN (CNameTtl, AnswerSection->Ttl);\r
+        } else {\r
+          Dns4CacheEntry->Timeout = MAX (CNameTtl, AnswerSection->Ttl);\r
+        }\r
         \r
         UpdateDns4Cache (&mDriverData->Dns4CacheList, FALSE, TRUE, *Dns4CacheEntry);  \r
 \r
-        IpCount ++; \r
+        IpCount ++;\r
+        Status = EFI_SUCCESS;\r
         break;\r
       case DNS_TYPE_AAAA:\r
         //\r
@@ -1478,26 +1483,40 @@ ParseDnsResponse (
         //\r
         Dns6CacheEntry = AllocateZeroPool (sizeof (EFI_DNS6_CACHE_ENTRY));\r
         if (Dns6CacheEntry == NULL) {\r
-          Status = EFI_UNSUPPORTED;\r
+          Status = EFI_OUT_OF_RESOURCES;\r
           goto ON_EXIT;\r
         }\r
         Dns6CacheEntry->HostName = AllocateZeroPool (2 * (StrLen(Dns6TokenEntry->QueryHostName) + 1));\r
         if (Dns6CacheEntry->HostName == NULL) {\r
-          Status = EFI_UNSUPPORTED;\r
+          Status = EFI_OUT_OF_RESOURCES;\r
           goto ON_EXIT;\r
         }\r
         CopyMem (Dns6CacheEntry->HostName, Dns6TokenEntry->QueryHostName, 2 * (StrLen(Dns6TokenEntry->QueryHostName) + 1));\r
         Dns6CacheEntry->IpAddress = AllocateZeroPool (sizeof (EFI_IPv6_ADDRESS));\r
         if (Dns6CacheEntry->IpAddress == NULL) {\r
-          Status = EFI_UNSUPPORTED;\r
+          Status = EFI_OUT_OF_RESOURCES;\r
           goto ON_EXIT;\r
         }\r
         CopyMem (Dns6CacheEntry->IpAddress, AnswerData, sizeof (EFI_IPv6_ADDRESS));\r
-        Dns6CacheEntry->Timeout = AnswerSection->Ttl;\r
+\r
+        if (CNameTtl != 0 && AnswerSection->Ttl != 0) {\r
+          Dns6CacheEntry->Timeout = MIN (CNameTtl, AnswerSection->Ttl);\r
+        } else {\r
+          Dns6CacheEntry->Timeout = MAX (CNameTtl, AnswerSection->Ttl);\r
+        }\r
         \r
         UpdateDns6Cache (&mDriverData->Dns6CacheList, FALSE, TRUE, *Dns6CacheEntry);  \r
         \r
         IpCount ++;\r
+        Status = EFI_SUCCESS;\r
+        break;\r
+      case DNS_TYPE_CNAME:\r
+        //\r
+        // According RFC 1034 - 3.6.2, if the query name is an alias, the name server will include the CNAME \r
+        // record in the response and restart the query at the domain name specified in the data field of the \r
+        // CNAME record. So, just record the TTL value of the CNAME, then skip to parse the next record.\r
+        //\r
+        CNameTtl = AnswerSection->Ttl;\r
         break;\r
       default:\r
         Status = EFI_UNSUPPORTED;\r
@@ -1541,12 +1560,16 @@ ParseDnsResponse (
   }\r
 \r
   //\r
-  // Parsing is complete, SignalEvent here.\r
+  // Parsing is complete, free the sending packet and signal Event here.\r
   //\r
+  if (Item != NULL && Item->Value != NULL) {\r
+    NetbufFree ((NET_BUF *) (Item->Value));\r
+  }\r
+  \r
   if (Instance->Service->IpVersion == IP_VERSION_4) {\r
     ASSERT (Dns4TokenEntry != NULL);\r
     Dns4RemoveTokenEntry (&Instance->Dns4TxTokens, Dns4TokenEntry);\r
-    Dns4TokenEntry->Token->Status = EFI_SUCCESS;\r
+    Dns4TokenEntry->Token->Status = Status;\r
     if (Dns4TokenEntry->Token->Event != NULL) {\r
       gBS->SignalEvent (Dns4TokenEntry->Token->Event);\r
       DispatchDpc ();\r
@@ -1554,7 +1577,7 @@ ParseDnsResponse (
   } else {\r
     ASSERT (Dns6TokenEntry != NULL);\r
     Dns6RemoveTokenEntry (&Instance->Dns6TxTokens, Dns6TokenEntry);\r
-    Dns6TokenEntry->Token->Status = EFI_SUCCESS;\r
+    Dns6TokenEntry->Token->Status = Status;\r
     if (Dns6TokenEntry->Token->Event != NULL) {\r
       gBS->SignalEvent (Dns6TokenEntry->Token->Event);\r
       DispatchDpc ();\r
@@ -2037,7 +2060,7 @@ DnsOnTimerUpdate (
   Entry = mDriverData->Dns4CacheList.ForwardLink;\r
   while (Entry != &mDriverData->Dns4CacheList) {\r
     Item4 = NET_LIST_USER_STRUCT (Entry, DNS4_CACHE, AllCacheLink);\r
-    if (Item4->DnsCache.Timeout<=0) {\r
+    if (Item4->DnsCache.Timeout == 0) {\r
       RemoveEntryList (&Item4->AllCacheLink);\r
       Entry = mDriverData->Dns4CacheList.ForwardLink;\r
     } else {\r
@@ -2056,7 +2079,7 @@ DnsOnTimerUpdate (
   Entry = mDriverData->Dns6CacheList.ForwardLink;\r
   while (Entry != &mDriverData->Dns6CacheList) {\r
     Item6 = NET_LIST_USER_STRUCT (Entry, DNS6_CACHE, AllCacheLink);\r
-    if (Item6->DnsCache.Timeout<=0) {\r
+    if (Item6->DnsCache.Timeout == 0) {\r
       RemoveEntryList (&Item6->AllCacheLink);\r
       Entry = mDriverData->Dns6CacheList.ForwardLink;\r
     } else {\r