]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.c
NetworkPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcDhcp6.c
index 6a08e9a2de68a56cebf1c71174ec6364af4119bc..1164fbbdefed7862a6cebff3d7432ec474337d76 100644 (file)
@@ -2,15 +2,9 @@
   Functions implementation related with DHCPv6 for UefiPxeBc Driver.\r
 \r
   (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
 \r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php.\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -93,11 +87,12 @@ PxeBcBuildDhcp6Options (
   // Append client option request option\r
   //\r
   OptList[Index]->OpCode     = HTONS (DHCP6_OPT_ORO);\r
-  OptList[Index]->OpLen      = HTONS (6);\r
+  OptList[Index]->OpLen      = HTONS (8);\r
   OptEnt.Oro                 = (PXEBC_DHCP6_OPTION_ORO *) OptList[Index]->Data;\r
   OptEnt.Oro->OpCode[0]      = HTONS(DHCP6_OPT_BOOT_FILE_URL);\r
   OptEnt.Oro->OpCode[1]      = HTONS(DHCP6_OPT_BOOT_FILE_PARAM);\r
   OptEnt.Oro->OpCode[2]      = HTONS(DHCP6_OPT_DNS_SERVERS);\r
+  OptEnt.Oro->OpCode[3]      = HTONS(DHCP6_OPT_VENDOR_CLASS);\r
   Index++;\r
   OptList[Index]             = GET_NEXT_DHCP6_OPTION (OptList[Index - 1]);\r
 \r
@@ -181,40 +176,24 @@ PxeBcBuildDhcp6Options (
   @param[in]  Dst          The pointer to the cache buffer for DHCPv6 packet.\r
   @param[in]  Src          The pointer to the DHCPv6 packet to be cached.\r
 \r
+  @retval     EFI_SUCCESS                Packet is copied.\r
+  @retval     EFI_BUFFER_TOO_SMALL       Cache buffer is not big enough to hold the packet.\r
+\r
 **/\r
-VOID\r
+EFI_STATUS\r
 PxeBcCacheDhcp6Packet (\r
   IN EFI_DHCP6_PACKET          *Dst,\r
   IN EFI_DHCP6_PACKET          *Src\r
   )\r
 {\r
-  ASSERT (Dst->Size >= Src->Length);\r
+  if (Dst->Size < Src->Length) {\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
 \r
   CopyMem (&Dst->Dhcp6, &Src->Dhcp6, Src->Length);\r
   Dst->Length = Src->Length;\r
-}\r
-\r
 \r
-/**\r
-  Free all the nodes in the list for boot file.\r
-\r
-  @param[in]  Head            The pointer to the head of list.\r
-\r
-**/\r
-VOID\r
-PxeBcFreeBootFileOption (\r
-  IN LIST_ENTRY               *Head\r
-  )\r
-{\r
-  LIST_ENTRY                  *Entry;\r
-  LIST_ENTRY                  *NextEntry;\r
-  PXEBC_DHCP6_OPTION_NODE     *Node;\r
-\r
-  NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, Head) {\r
-    Node = NET_LIST_USER_STRUCT (Entry, PXEBC_DHCP6_OPTION_NODE, Link);\r
-    RemoveEntryList (Entry);\r
-    FreePool (Node);\r
-  }\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -228,13 +207,13 @@ PxeBcFreeBootFileOption (
   @retval EFI_OUT_OF_RESOURCES    Failed to allocate needed resources.\r
   @retval EFI_DEVICE_ERROR        An unexpected network error occurred.\r
   @retval Others                  Other errors as indicated.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 PxeBcDns6 (\r
   IN PXEBC_PRIVATE_DATA           *Private,\r
   IN     CHAR16                   *HostName,\r
-     OUT EFI_IPv6_ADDRESS         *IpAddress                \r
+     OUT EFI_IPv6_ADDRESS         *IpAddress\r
   )\r
 {\r
   EFI_STATUS                      Status;\r
@@ -244,7 +223,7 @@ PxeBcDns6 (
   EFI_HANDLE                      Dns6Handle;\r
   EFI_IPv6_ADDRESS                *DnsServerList;\r
   BOOLEAN                         IsDone;\r
-  \r
+\r
   Dns6                = NULL;\r
   Dns6Handle          = NULL;\r
   DnsServerList       = Private->DnsServer;\r
@@ -261,8 +240,8 @@ PxeBcDns6 (
              );\r
   if (EFI_ERROR (Status)) {\r
     goto Exit;\r
-  } \r
-  \r
+  }\r
+\r
   Status = gBS->OpenProtocol (\r
                   Dns6Handle,\r
                   &gEfiDns6ProtocolGuid,\r
@@ -323,7 +302,7 @@ PxeBcDns6 (
   //\r
   // Name resolution is done, check result.\r
   //\r
-  Status = Token.Status;  \r
+  Status = Token.Status;\r
   if (!EFI_ERROR (Status)) {\r
     if (Token.RspData.H2AData == NULL) {\r
       Status = EFI_DEVICE_ERROR;\r
@@ -339,7 +318,7 @@ PxeBcDns6 (
     IP6_COPY_ADDRESS (IpAddress, Token.RspData.H2AData->IpList);\r
     Status = EFI_SUCCESS;\r
   }\r
-  \r
+\r
 Exit:\r
   FreePool (HostName);\r
 \r
@@ -355,7 +334,7 @@ Exit:
 \r
   if (Dns6 != NULL) {\r
     Dns6->Configure (Dns6, NULL);\r
-    \r
+\r
     gBS->CloseProtocol (\r
            Dns6Handle,\r
            &gEfiDns6ProtocolGuid,\r
@@ -376,8 +355,8 @@ Exit:
   if (DnsServerList != NULL) {\r
     FreePool (DnsServerList);\r
   }\r
-  \r
-  return Status;  \r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -466,14 +445,14 @@ PxeBcExtractBootFileUrl (
     while (*ServerAddress != '\0' && *ServerAddress != PXEBC_ADDR_END_DELIMITER) {\r
       ServerAddress++;\r
     }\r
-    \r
+\r
     if (*ServerAddress != PXEBC_ADDR_END_DELIMITER) {\r
       FreePool (TmpStr);\r
       return EFI_INVALID_PARAMETER;\r
     }\r
-    \r
+\r
     *ServerAddress = '\0';\r
-    \r
+\r
     //\r
     // Convert the string of server address to Ipv6 address format and store it.\r
     //\r
@@ -540,6 +519,7 @@ PxeBcExtractBootFileUrl (
     if (ModeStr != NULL && *(ModeStr + AsciiStrLen (";mode=octet")) == '\0') {\r
       *ModeStr = '\0';\r
     } else if (AsciiStrStr (BootFileNamePtr, ";mode=") != NULL) {\r
+      FreePool (TmpStr);\r
       return EFI_INVALID_PARAMETER;\r
     }\r
 \r
@@ -749,8 +729,11 @@ PxeBcParseDhcp6Packet (
   @param[in]  Ack                 The pointer to the DHCPv6 ack packet.\r
   @param[in]  Verified            If TRUE, parse the ACK packet and store info into mode data.\r
 \r
+  @retval     EFI_SUCCESS                Cache and parse the packet successfully.\r
+  @retval     EFI_BUFFER_TOO_SMALL       Cache buffer is not big enough to hold the packet.\r
+\r
 **/\r
-VOID\r
+EFI_STATUS\r
 PxeBcCopyDhcp6Ack (\r
   IN PXEBC_PRIVATE_DATA   *Private,\r
   IN EFI_DHCP6_PACKET     *Ack,\r
@@ -758,10 +741,14 @@ PxeBcCopyDhcp6Ack (
   )\r
 {\r
   EFI_PXE_BASE_CODE_MODE  *Mode;\r
+  EFI_STATUS              Status;\r
 \r
   Mode = Private->PxeBc.Mode;\r
 \r
-  PxeBcCacheDhcp6Packet (&Private->DhcpAck.Dhcp6.Packet.Ack, Ack);\r
+  Status = PxeBcCacheDhcp6Packet (&Private->DhcpAck.Dhcp6.Packet.Ack, Ack);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
   if (Verified) {\r
     //\r
@@ -771,6 +758,8 @@ PxeBcCopyDhcp6Ack (
     CopyMem (&Mode->DhcpAck.Dhcpv6, &Ack->Dhcp6, Ack->Length);\r
     Mode->DhcpAckReceived = TRUE;\r
   }\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 \r
@@ -780,8 +769,11 @@ PxeBcCopyDhcp6Ack (
   @param[in]  Private               The pointer to PxeBc private data.\r
   @param[in]  OfferIndex            The received order of offer packets.\r
 \r
+  @retval     EFI_SUCCESS                Cache and parse the packet successfully.\r
+  @retval     EFI_BUFFER_TOO_SMALL       Cache buffer is not big enough to hold the packet.\r
+\r
 **/\r
-VOID\r
+EFI_STATUS\r
 PxeBcCopyDhcp6Proxy (\r
   IN PXEBC_PRIVATE_DATA     *Private,\r
   IN UINT32                 OfferIndex\r
@@ -789,6 +781,7 @@ PxeBcCopyDhcp6Proxy (
 {\r
   EFI_PXE_BASE_CODE_MODE    *Mode;\r
   EFI_DHCP6_PACKET          *Offer;\r
+  EFI_STATUS              Status;\r
 \r
   ASSERT (OfferIndex < Private->OfferNum);\r
   ASSERT (OfferIndex < PXEBC_OFFER_MAX_NUM);\r
@@ -799,7 +792,10 @@ PxeBcCopyDhcp6Proxy (
   //\r
   // Cache the proxy offer packet and parse it.\r
   //\r
-  PxeBcCacheDhcp6Packet (&Private->ProxyOffer.Dhcp6.Packet.Offer, Offer);\r
+  Status = PxeBcCacheDhcp6Packet (&Private->ProxyOffer.Dhcp6.Packet.Offer, Offer);\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
   PxeBcParseDhcp6Packet (&Private->ProxyOffer.Dhcp6);\r
 \r
   //\r
@@ -807,6 +803,8 @@ PxeBcCopyDhcp6Proxy (
   //\r
   CopyMem (&Mode->ProxyOffer.Dhcpv6, &Offer->Dhcp6, Offer->Length);\r
   Mode->ProxyOfferReceived = TRUE;\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -882,12 +880,12 @@ PxeBcRequestBootService (
   UINT16                              OpCode;\r
   UINT16                              OpLen;\r
   EFI_STATUS                          Status;\r
-  EFI_DHCP6_PACKET                    *ProxyOffer;\r
+  EFI_DHCP6_PACKET                    *IndexOffer;\r
   UINT8                               *Option;\r
 \r
   PxeBc       = &Private->PxeBc;\r
   Request     = Private->Dhcp6Request;\r
-  ProxyOffer = &Private->OfferBuffer[Index].Dhcp6.Packet.Offer;\r
+  IndexOffer  = &Private->OfferBuffer[Index].Dhcp6.Packet.Offer;\r
   SrcPort     = PXEBC_BS_DISCOVER_PORT;\r
   DestPort    = PXEBC_BS_DISCOVER_PORT;\r
   OpFlags     = 0;\r
@@ -904,7 +902,7 @@ PxeBcRequestBootService (
   //\r
   // Build the request packet by the cached request packet before.\r
   //\r
-  Discover->TransactionId = ProxyOffer->Dhcp6.Header.TransactionId;\r
+  Discover->TransactionId = IndexOffer->Dhcp6.Header.TransactionId;\r
   Discover->MessageType   = Request->Dhcp6.Header.MessageType;\r
   RequestOpt              = Request->Dhcp6.Option;\r
   DiscoverOpt             = Discover->DhcpOptions;\r
@@ -914,22 +912,24 @@ PxeBcRequestBootService (
   //\r
   // Find Server ID Option from ProxyOffer.\r
   //\r
-  Option = PxeBcDhcp6SeekOption (\r
-             ProxyOffer->Dhcp6.Option,\r
-             ProxyOffer->Length - 4,\r
-             DHCP6_OPT_SERVER_ID\r
-             );\r
-  if (Option == NULL) {\r
-    return EFI_NOT_FOUND;\r
+  if (Private->OfferBuffer[Index].Dhcp6.OfferType == PxeOfferTypeProxyBinl) {\r
+    Option = PxeBcDhcp6SeekOption (\r
+               IndexOffer->Dhcp6.Option,\r
+               IndexOffer->Length - 4,\r
+               DHCP6_OPT_SERVER_ID\r
+               );\r
+    if (Option == NULL) {\r
+      return EFI_NOT_FOUND;\r
+    }\r
+\r
+    //\r
+    // Add Server ID Option.\r
+    //\r
+    OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) Option)->OpLen);\r
+    CopyMem (DiscoverOpt, Option, OpLen + 4);\r
+    DiscoverOpt += (OpLen + 4);\r
+    DiscoverLen += (OpLen + 4);\r
   }\r
-  \r
-  //\r
-  // Add Server ID Option.\r
-  //\r
-  OpLen = NTOHS (((EFI_DHCP6_PACKET_OPTION *) Option)->OpLen);\r
-  CopyMem (DiscoverOpt, Option, OpLen + 4);\r
-  DiscoverOpt += (OpLen + 4);\r
-  DiscoverLen += (OpLen + 4);\r
 \r
   while (RequestLen < Request->Length) {\r
     OpCode = NTOHS (((EFI_DHCP6_PACKET_OPTION *) RequestOpt)->OpCode);\r
@@ -950,7 +950,7 @@ PxeBcRequestBootService (
   }\r
 \r
   //\r
-  // Update Elapsed option in the package \r
+  // Update Elapsed option in the package\r
   //\r
   Option = PxeBcDhcp6SeekOption (\r
              Discover->DhcpOptions,\r
@@ -960,7 +960,7 @@ PxeBcRequestBootService (
   if (Option != NULL) {\r
     CalcElapsedTime (Private);\r
     WriteUnaligned16 ((UINT16*)(Option + 4), HTONS((UINT16) Private->ElapsedTime));\r
-  }  \r
+  }\r
 \r
   Status = PxeBc->UdpWrite (\r
                     PxeBc,\r
@@ -977,7 +977,7 @@ PxeBcRequestBootService (
                     );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto ON_ERROR;\r
   }\r
 \r
   //\r
@@ -992,9 +992,9 @@ PxeBcRequestBootService (
   //\r
   Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto ON_ERROR;\r
   }\r
-    \r
+\r
   Status = PxeBc->UdpRead (\r
                     PxeBc,\r
                     EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP,\r
@@ -1013,7 +1013,7 @@ PxeBcRequestBootService (
   Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto ON_ERROR;\r
   }\r
 \r
   //\r
@@ -1022,6 +1022,13 @@ PxeBcRequestBootService (
   Reply->Length = (UINT32) ReadSize;\r
 \r
   return EFI_SUCCESS;\r
+\r
+ON_ERROR:\r
+  if (Discover != NULL) {\r
+    FreePool (Discover);\r
+  }\r
+\r
+  return Status;\r
 }\r
 \r
 \r
@@ -1053,7 +1060,7 @@ PxeBcRetryDhcp6Binl (
   Mode                  = Private->PxeBc.Mode;\r
   Private->IsDoDiscover = FALSE;\r
   Offer                 = &Private->OfferBuffer[Index].Dhcp6;\r
-  if (Offer->OfferType == PxeOfferTypeDhcpBinl) {\r
+  if (Offer->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] == NULL) {\r
     //\r
     // There is no BootFileUrl option in dhcp6 offer, so use servers multi-cast address instead.\r
     //\r
@@ -1121,8 +1128,10 @@ PxeBcRetryDhcp6Binl (
   @param[in]  Private               The pointer to PXEBC_PRIVATE_DATA.\r
   @param[in]  RcvdOffer             The pointer to the received offer packet.\r
 \r
+  @retval     EFI_SUCCESS      Cache and parse the packet successfully.\r
+  @retval     Others           Operation failed.\r
 **/\r
-VOID\r
+EFI_STATUS\r
 PxeBcCacheDhcp6Offer (\r
   IN PXEBC_PRIVATE_DATA     *Private,\r
   IN EFI_DHCP6_PACKET       *RcvdOffer\r
@@ -1131,6 +1140,7 @@ PxeBcCacheDhcp6Offer (
   PXEBC_DHCP6_PACKET_CACHE  *Cache6;\r
   EFI_DHCP6_PACKET          *Offer;\r
   PXEBC_OFFER_TYPE          OfferType;\r
+  EFI_STATUS                Status;\r
 \r
   Cache6 = &Private->OfferBuffer[Private->OfferNum].Dhcp6;\r
   Offer  = &Cache6->Packet.Offer;\r
@@ -1138,13 +1148,16 @@ PxeBcCacheDhcp6Offer (
   //\r
   // Cache the content of DHCPv6 packet firstly.\r
   //\r
-  PxeBcCacheDhcp6Packet (Offer, RcvdOffer);\r
+  Status = PxeBcCacheDhcp6Packet (Offer, RcvdOffer);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
   //\r
   // Validate the DHCPv6 packet, and parse the options and offer type.\r
   //\r
   if (EFI_ERROR (PxeBcParseDhcp6Packet (Cache6))) {\r
-    return ;\r
+    return EFI_ABORTED;\r
   }\r
 \r
   //\r
@@ -1166,14 +1179,15 @@ PxeBcCacheDhcp6Offer (
       //\r
       Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;\r
       Private->OfferCount[OfferType]++;\r
-    } else if (Private->OfferCount[OfferType] > 0) {\r
+    } else if ((OfferType == PxeOfferTypeProxyPxe10 || OfferType == PxeOfferTypeProxyWfm11a) &&\r
+                 Private->OfferCount[OfferType] < 1) {\r
       //\r
       // Only cache the first PXE10/WFM11a offer, and discard the others.\r
       //\r
       Private->OfferIndex[OfferType][0] = Private->OfferNum;\r
       Private->OfferCount[OfferType]    = 1;\r
     } else {\r
-      return;\r
+      return EFI_ABORTED;\r
     }\r
   } else {\r
     //\r
@@ -1184,6 +1198,8 @@ PxeBcCacheDhcp6Offer (
   }\r
 \r
   Private->OfferNum++;\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 \r
@@ -1301,6 +1317,7 @@ PxeBcSelectDhcp6Offer (
   @retval     EFI_SUCCESS           Handled the DHCPv6 offer packet successfully.\r
   @retval     EFI_NO_RESPONSE       No response to the following request packet.\r
   @retval     EFI_OUT_OF_RESOURCES  Failed to allocate resources.\r
+  @retval     EFI_BUFFER_TOO_SMALL  Can't cache the offer pacet.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1410,7 +1427,7 @@ PxeBcHandleDhcp6Offer (
         //\r
         // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it.\r
         //\r
-        PxeBcCopyDhcp6Proxy (Private, ProxyIndex);\r
+        Status = PxeBcCopyDhcp6Proxy (Private, ProxyIndex);\r
       }\r
     } else {\r
       //\r
@@ -1424,7 +1441,7 @@ PxeBcHandleDhcp6Offer (
     //\r
     // All PXE boot information is ready by now.\r
     //\r
-    PxeBcCopyDhcp6Ack (Private, &Private->DhcpAck.Dhcp6.Packet.Ack, TRUE);\r
+    Status = PxeBcCopyDhcp6Ack (Private, &Private->DhcpAck.Dhcp6.Packet.Ack, TRUE);\r
     Private->PxeBc.Mode->DhcpDiscoverValid = TRUE;\r
   }\r
 \r
@@ -1453,7 +1470,7 @@ PxeBcUnregisterIp6Address (
 \r
 /**\r
   Check whether IP driver could route the message which will be sent to ServerIp address.\r
-  \r
+\r
   This function will check the IP6 route table every 1 seconds until specified timeout is expired, if a valid\r
   route is found in IP6 route table, the address will be filed in GatewayAddr and return.\r
 \r
@@ -1464,7 +1481,7 @@ PxeBcUnregisterIp6Address (
   @retval     EFI_SUCCESS         Found a valid gateway address successfully.\r
   @retval     EFI_TIMEOUT         The operation is time out.\r
   @retval     Other               Unexpect error happened.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 PxeBcCheckRouteTable (\r
@@ -1525,13 +1542,13 @@ PxeBcCheckRouteTable (
     if (Ip6ModeData.IcmpTypeList != NULL) {\r
       FreePool (Ip6ModeData.IcmpTypeList);\r
     }\r
-    \r
+\r
     if (GatewayIsFound || RetryCount == TimeOutInSecond) {\r
       break;\r
     }\r
-    \r
+\r
     RetryCount++;\r
-    \r
+\r
     //\r
     // Delay 1 second then recheck it again.\r
     //\r
@@ -1556,19 +1573,19 @@ PxeBcCheckRouteTable (
       Ip6->Poll (Ip6);\r
     }\r
   }\r
-  \r
+\r
 ON_EXIT:\r
   if (TimeOutEvt != NULL) {\r
     gBS->CloseEvent (TimeOutEvt);\r
   }\r
-  \r
+\r
   if (GatewayIsFound) {\r
     Status = EFI_SUCCESS;\r
   } else if (RetryCount == TimeOutInSecond) {\r
     Status = EFI_TIMEOUT;\r
   }\r
 \r
-  return Status; \r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -1622,7 +1639,7 @@ PxeBcRegisterIp6Address (
   if (EFI_ERROR (Status)) {\r
     NoGateway = TRUE;\r
   }\r
-  \r
+\r
   //\r
   // There is no channel between IP6 and PXE driver about address setting,\r
   // so it has to set the new address by Ip6ConfigProtocol manually.\r
@@ -1721,7 +1738,7 @@ PxeBcRegisterIp6Address (
       goto ON_EXIT;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Set the default gateway address back if needed.\r
   //\r
@@ -1808,7 +1825,7 @@ PxeBcSetIp6Policy (
 \r
 /**\r
   This function will register the station IP address and flush IP instance to start using the new IP address.\r
-  \r
+\r
   @param[in]  Private             The pointer to PXEBC_PRIVATE_DATA.\r
 \r
   @retval     EFI_SUCCESS         The new IP address has been configured successfully.\r
@@ -1822,7 +1839,7 @@ PxeBcSetIp6Address (
 {\r
   EFI_STATUS                  Status;\r
   EFI_DHCP6_PROTOCOL          *Dhcp6;\r
-    \r
+\r
   Dhcp6 = Private->Dhcp6;\r
 \r
   CopyMem (&Private->StationIp.v6, &Private->TmpStationIp.v6, sizeof (EFI_IPv6_ADDRESS));\r
@@ -1926,7 +1943,7 @@ PxeBcDhcp6CallBack (
       Status = EFI_ABORTED;\r
       break;\r
     }\r
-    \r
+\r
     //\r
     // Record the first Solicate msg time\r
     //\r
@@ -1965,7 +1982,7 @@ PxeBcDhcp6CallBack (
       Status = EFI_ABORTED;\r
       break;\r
     }\r
-    \r
+\r
     //\r
     // Store the request packet as seed packet for discover.\r
     //\r
@@ -1992,24 +2009,23 @@ PxeBcDhcp6CallBack (
       SelectAd   = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp6.Packet.Offer;\r
       *NewPacket = AllocateZeroPool (SelectAd->Size);\r
       ASSERT (*NewPacket != NULL);\r
+      if (*NewPacket == NULL) {\r
+        return EFI_ABORTED;\r
+      }\r
       CopyMem (*NewPacket, SelectAd, SelectAd->Size);\r
     }\r
     break;\r
 \r
   case Dhcp6RcvdReply:\r
-    if (Packet->Length > PXEBC_DHCP6_PACKET_MAX_SIZE) {\r
-      //\r
-      // Abort the DHCP if the Peply packet exceeds the maximum length.\r
-      //\r
-         Status = EFI_ABORTED;\r
-      break;\r
-    }\r
     //\r
     // Cache the dhcp ack to Private->Dhcp6Ack, but it's not the final ack in mode data\r
     // without verification.\r
     //\r
     ASSERT (Private->SelectIndex != 0);\r
-    PxeBcCopyDhcp6Ack (Private, Packet, FALSE);\r
+    Status = PxeBcCopyDhcp6Ack (Private, Packet, FALSE);\r
+    if (EFI_ERROR (Status)) {\r
+      Status = EFI_ABORTED;\r
+    }\r
     break;\r
 \r
   default:\r
@@ -2121,7 +2137,7 @@ PxeBcDhcp6Discover (
                     (VOID *) Discover\r
                     );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto ON_ERROR;\r
   }\r
 \r
   //\r
@@ -2141,9 +2157,9 @@ PxeBcDhcp6Discover (
   //\r
   Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto ON_ERROR;\r
   }\r
-  \r
+\r
   Status = PxeBc->UdpRead (\r
                     PxeBc,\r
                     EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP,\r
@@ -2161,10 +2177,17 @@ PxeBcDhcp6Discover (
   //\r
   Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto ON_ERROR;\r
   }\r
 \r
   return EFI_SUCCESS;\r
+\r
+ON_ERROR:\r
+  if (Discover != NULL) {\r
+    FreePool (Discover);\r
+  }\r
+\r
+  return Status;\r
 }\r
 \r
 \r
@@ -2293,13 +2316,13 @@ PxeBcDhcp6Sarr (
     }\r
 \r
     do {\r
-      \r
+\r
       TimerStatus = gBS->CheckEvent (Timer);\r
       if (!EFI_ERROR (TimerStatus)) {\r
         Status = Dhcp6->Start (Dhcp6);\r
       }\r
     } while (TimerStatus == EFI_NOT_READY);\r
-    \r
+\r
     gBS->CloseEvent (Timer);\r
   }\r
   if (EFI_ERROR (Status)) {\r
@@ -2324,7 +2347,7 @@ PxeBcDhcp6Sarr (
   // DHCP6 doesn't have an option to specify the router address on the subnet, the only way to get the\r
   // router address in IP6 is the router discovery mechanism (the RS and RA, which only be handled when\r
   // the IP policy is Automatic). So we just hold the station IP address here and leave the IP policy as\r
-  // Automatic, until we get the server IP address. This could let IP6 driver finish the router discovery \r
+  // Automatic, until we get the server IP address. This could let IP6 driver finish the router discovery\r
   // to find a valid router address.\r
   //\r
   CopyMem (&Private->TmpStationIp.v6, &Mode.Ia->IaAddress[0].IpAddress, sizeof (EFI_IPv6_ADDRESS));\r
@@ -2342,6 +2365,6 @@ PxeBcDhcp6Sarr (
     Dhcp6->Stop (Dhcp6);\r
     return Status;\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r