]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/IpSecDxe/IpSecConfigImpl.c
NetworkPkg: Fix IpSec run into infinite loop issue in some case
[mirror_edk2.git] / NetworkPkg / IpSecDxe / IpSecConfigImpl.c
index e671e42e27af1230f804c4eb625d87047c846d9d..bd49245190bd330e8221eea8cec73daa1b5e0c12 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   The implementation of IPSEC_CONFIG_PROTOCOL.\r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2015, 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
@@ -20,7 +20,7 @@ LIST_ENTRY                mConfigData[IPsecConfigDataTypeMaximum];
 BOOLEAN                   mSetBySelf = FALSE;\r
 \r
 //\r
-// Common CompareSelector routine entry for spd/sad/pad.\r
+// Common CompareSelector routine entry for SPD/SAD/PAD.\r
 //\r
 IPSEC_COMPARE_SELECTOR    mCompareSelector[] = {\r
   (IPSEC_COMPARE_SELECTOR) CompareSpdSelector,\r
@@ -29,7 +29,7 @@ IPSEC_COMPARE_SELECTOR    mCompareSelector[] = {
 };\r
 \r
 //\r
-// Common IsZeroSelector routine entry for spd/sad/pad.\r
+// Common IsZeroSelector routine entry for SPD/SAD/PAD.\r
 //\r
 IPSEC_IS_ZERO_SELECTOR    mIsZeroSelector[] = {\r
   (IPSEC_IS_ZERO_SELECTOR) IsZeroSpdSelector,\r
@@ -38,7 +38,7 @@ IPSEC_IS_ZERO_SELECTOR    mIsZeroSelector[] = {
 };\r
 \r
 //\r
-// Common DuplicateSelector routine entry for spd/sad/pad.\r
+// Common DuplicateSelector routine entry for SPD/SAD/PAD.\r
 //\r
 IPSEC_DUPLICATE_SELECTOR  mDuplicateSelector[] = {\r
   (IPSEC_DUPLICATE_SELECTOR) DuplicateSpdSelector,\r
@@ -47,7 +47,7 @@ IPSEC_DUPLICATE_SELECTOR  mDuplicateSelector[] = {
 };\r
 \r
 //\r
-// Common FixPolicyEntry routine entry for spd/sad/pad.\r
+// Common FixPolicyEntry routine entry for SPD/SAD/PAD.\r
 //\r
 IPSEC_FIX_POLICY_ENTRY    mFixPolicyEntry[] = {\r
   (IPSEC_FIX_POLICY_ENTRY) FixSpdEntry,\r
@@ -56,7 +56,7 @@ IPSEC_FIX_POLICY_ENTRY    mFixPolicyEntry[] = {
 };\r
 \r
 //\r
-// Common UnfixPolicyEntry routine entry for spd/sad/pad.\r
+// Common UnfixPolicyEntry routine entry for SPD/SAD/PAD.\r
 //\r
 IPSEC_FIX_POLICY_ENTRY    mUnfixPolicyEntry[] = {\r
   (IPSEC_FIX_POLICY_ENTRY) UnfixSpdEntry,\r
@@ -65,7 +65,7 @@ IPSEC_FIX_POLICY_ENTRY    mUnfixPolicyEntry[] = {
 };\r
 \r
 //\r
-// Common SetPolicyEntry routine entry for spd/sad/pad.\r
+// Common SetPolicyEntry routine entry for SPD/SAD/PAD.\r
 //\r
 IPSEC_SET_POLICY_ENTRY    mSetPolicyEntry[] = {\r
   (IPSEC_SET_POLICY_ENTRY) SetSpdEntry,\r
@@ -74,7 +74,7 @@ IPSEC_SET_POLICY_ENTRY    mSetPolicyEntry[] = {
 };\r
 \r
 //\r
-// Common GetPolicyEntry routine entry for spd/sad/pad.\r
+// Common GetPolicyEntry routine entry for SPD/SAD/PAD.\r
 //\r
 IPSEC_GET_POLICY_ENTRY    mGetPolicyEntry[] = {\r
   (IPSEC_GET_POLICY_ENTRY) GetSpdEntry,\r
@@ -97,7 +97,7 @@ EFI_IPSEC_CONFIG_PROTOCOL mIpSecConfigInstance = {
   Get the all IPSec configuration variables and store those variables\r
   to the internal data structure.\r
 \r
-  This founction is called by IpSecConfigInitialize() that is to intialize the\r
+  This founction is called by IpSecConfigInitialize() that is to intialize the \r
   IPsecConfiguration Protocol.\r
 \r
   @param[in]  Private            Point to IPSEC_PRIVATE_DATA.\r
@@ -121,7 +121,7 @@ IpSecConfigRestore (
 \r
   @retval  TRUE    The specified AddressInfo is in the AddressInfoList.\r
   @retval  FALSE   The specified AddressInfo is not in the AddressInfoList.\r
-\r
+  \r
 **/\r
 BOOLEAN\r
 IsInAddressInfoList(\r
@@ -130,27 +130,42 @@ IsInAddressInfoList(
   IN UINT32                           AddressCount\r
   )\r
 {\r
-  UINT8  Index;\r
+  UINT8           Index;\r
+  EFI_IP_ADDRESS  ZeroAddress;\r
+\r
+  ZeroMem(&ZeroAddress, sizeof (EFI_IP_ADDRESS));\r
 \r
+  //\r
+  // Zero Address means any address is matched.\r
+  //\r
+  if (AddressCount == 1) {\r
+    if (CompareMem (\r
+          &AddressInfoList[0].Address,\r
+          &ZeroAddress,\r
+          sizeof (EFI_IP_ADDRESS)\r
+          ) == 0) {\r
+      return TRUE;\r
+    }\r
+  }\r
   for (Index = 0; Index < AddressCount ; Index++) {\r
     if (CompareMem (\r
           AddressInfo,\r
           &AddressInfoList[Index].Address,\r
           sizeof (EFI_IP_ADDRESS)\r
-          ) == 0 &&\r
+          ) == 0 && \r
           AddressInfo->PrefixLength == AddressInfoList[Index].PrefixLength\r
-          ) {\r
+          ) { \r
        return TRUE;\r
      }\r
   }\r
   return FALSE;\r
 }\r
-\r
\r
 /**\r
   Compare two SPD Selectors.\r
 \r
   Compare two SPD Selector by the fields of LocalAddressCount/RemoteAddressCount/\r
-  NextLayerProtocol/LocalPort/LocalPortRange/RemotePort/RemotePortRange and the\r
+  NextLayerProtocol/LocalPort/LocalPortRange/RemotePort/RemotePortRange and the \r
   Local Addresses and remote Addresses.\r
 \r
   @param[in]   Selector1           Pointer of first SPD Selector.\r
@@ -158,7 +173,7 @@ IsInAddressInfoList(
 \r
   @retval  TRUE    This two Selector have the same value in above fields.\r
   @retval  FALSE   Not all above fields have the same value in these two Selectors.\r
-\r
+  \r
 **/\r
 BOOLEAN\r
 CompareSpdSelector (\r
@@ -178,7 +193,7 @@ CompareSpdSelector (
   //\r
   // Compare the LocalAddressCount/RemoteAddressCount/NextLayerProtocol/\r
   // LocalPort/LocalPortRange/RemotePort/RemotePortRange fields in the\r
-  // two Spdselectors. Since the SPD supports two directions, it needs to\r
+  // two Spdselectors. Since the SPD supports two directions, it needs to \r
   // compare two directions.\r
   //\r
   if ((SpdSel1->LocalAddressCount != SpdSel2->LocalAddressCount &&\r
@@ -194,10 +209,10 @@ CompareSpdSelector (
     IsMatch = FALSE;\r
     return IsMatch;\r
   }\r
-\r
+  \r
   //\r
   // Compare the all LocalAddress fields in the two Spdselectors.\r
-  // First, SpdSel1->LocalAddress to SpdSel2->LocalAddress && Compare\r
+  // First, SpdSel1->LocalAddress to SpdSel2->LocalAddress && Compare \r
   // SpdSel1->RemoteAddress to SpdSel2->RemoteAddress. If all match, return\r
   // TRUE.\r
   //\r
@@ -248,14 +263,14 @@ CompareSpdSelector (
     }\r
   }\r
   //\r
-  // Finish the one direction compare. If it is matched, return; otherwise,\r
+  // Finish the one direction compare. If it is matched, return; otherwise, \r
   // compare the other direction.\r
   //\r
   if (IsMatch) {\r
     return IsMatch;\r
   }\r
   //\r
-  // Secondly, the SpdSel1->LocalAddress doesn't equal to  SpdSel2->LocalAddress and\r
+  // Secondly, the SpdSel1->LocalAddress doesn't equal to  SpdSel2->LocalAddress and \r
   // SpdSel1->RemoteAddress doesn't equal to SpdSel2->RemoteAddress. Try to compare\r
   // the RemoteAddress to LocalAddress.\r
   //\r
@@ -309,6 +324,143 @@ CompareSpdSelector (
   return IsMatch;\r
 }\r
 \r
+/**\r
+  Find if the two SPD Selectors has subordinative.\r
+\r
+  Compare two SPD Selector by the fields of LocalAddressCount/RemoteAddressCount/\r
+  NextLayerProtocol/LocalPort/LocalPortRange/RemotePort/RemotePortRange and the \r
+  Local Addresses and remote Addresses.\r
+\r
+  @param[in]   Selector1           Pointer of first SPD Selector.\r
+  @param[in]   Selector2           Pointer of second SPD Selector.\r
+\r
+  @retval  TRUE    The first SPD Selector is subordinate Selector of second SPD Selector.\r
+  @retval  FALSE   The first SPD Selector is not subordinate Selector of second \r
+                   SPD Selector.\r
+  \r
+**/\r
+BOOLEAN\r
+IsSubSpdSelector (\r
+  IN EFI_IPSEC_CONFIG_SELECTOR        *Selector1,\r
+  IN EFI_IPSEC_CONFIG_SELECTOR        *Selector2\r
+  )\r
+{\r
+  EFI_IPSEC_SPD_SELECTOR  *SpdSel1;\r
+  EFI_IPSEC_SPD_SELECTOR  *SpdSel2;\r
+  BOOLEAN                 IsMatch;\r
+  UINTN                   Index;\r
+\r
+  SpdSel1 = &Selector1->SpdSelector;\r
+  SpdSel2 = &Selector2->SpdSelector;\r
+  IsMatch = TRUE;\r
+\r
+  //\r
+  // Compare the LocalAddressCount/RemoteAddressCount/NextLayerProtocol/\r
+  // LocalPort/LocalPortRange/RemotePort/RemotePortRange fields in the\r
+  // two Spdselectors. Since the SPD supports two directions, it needs to \r
+  // compare two directions.\r
+  //\r
+  if (SpdSel1->LocalAddressCount > SpdSel2->LocalAddressCount ||\r
+      SpdSel1->RemoteAddressCount > SpdSel2->RemoteAddressCount ||\r
+      (SpdSel1->NextLayerProtocol != SpdSel2->NextLayerProtocol && SpdSel2->NextLayerProtocol != 0xffff) ||\r
+      (SpdSel1->LocalPort > SpdSel2->LocalPort && SpdSel2->LocalPort != 0)||\r
+      (SpdSel1->LocalPortRange > SpdSel2->LocalPortRange && SpdSel1->LocalPort != 0)||\r
+      (SpdSel1->RemotePort > SpdSel2->RemotePort && SpdSel2->RemotePort != 0) ||\r
+      (SpdSel1->RemotePortRange > SpdSel2->RemotePortRange && SpdSel2->RemotePort != 0)\r
+      ) {\r
+    IsMatch = FALSE;\r
+  }\r
+  \r
+  //\r
+  // Compare the all LocalAddress fields in the two Spdselectors.\r
+  // First, SpdSel1->LocalAddress to SpdSel2->LocalAddress && Compare \r
+  // SpdSel1->RemoteAddress to SpdSel2->RemoteAddress. If all match, return\r
+  // TRUE.\r
+  //\r
+  if (IsMatch) {\r
+    for (Index = 0; Index < SpdSel1->LocalAddressCount; Index++) {\r
+      if (!IsInAddressInfoList (\r
+            &SpdSel1->LocalAddress[Index],\r
+            SpdSel2->LocalAddress,\r
+            SpdSel2->LocalAddressCount\r
+            )) {\r
+        IsMatch = FALSE;\r
+        break;\r
+      }\r
+    }\r
+\r
+    if (IsMatch) {\r
+      for (Index = 0; Index < SpdSel1->RemoteAddressCount; Index++) {\r
+        if (!IsInAddressInfoList (\r
+              &SpdSel1->RemoteAddress[Index],\r
+              SpdSel2->RemoteAddress,\r
+              SpdSel2->RemoteAddressCount\r
+              )) {\r
+          IsMatch = FALSE;\r
+          break;\r
+        }\r
+      }\r
+    }\r
+  }\r
+  if (IsMatch) {\r
+    return IsMatch;\r
+  }\r
+  \r
+  //\r
+  //\r
+  // The SPD selector in SPD entry is two way.\r
+  //\r
+  // Compare the LocalAddressCount/RemoteAddressCount/NextLayerProtocol/\r
+  // LocalPort/LocalPortRange/RemotePort/RemotePortRange fields in the\r
+  // two Spdselectors. Since the SPD supports two directions, it needs to \r
+  // compare two directions.\r
+  //\r
+  IsMatch = TRUE;\r
+  if (SpdSel1->LocalAddressCount > SpdSel2->RemoteAddressCount ||\r
+      SpdSel1->RemoteAddressCount > SpdSel2->LocalAddressCount ||\r
+      (SpdSel1->NextLayerProtocol != SpdSel2->NextLayerProtocol && SpdSel2->NextLayerProtocol != 0xffff) ||\r
+      (SpdSel1->LocalPort > SpdSel2->RemotePort && SpdSel2->RemotePort != 0)||\r
+      (SpdSel1->LocalPortRange > SpdSel2->RemotePortRange && SpdSel1->RemotePort != 0)||\r
+      (SpdSel1->RemotePort > SpdSel2->LocalPort && SpdSel2->LocalPort != 0) ||\r
+      (SpdSel1->RemotePortRange > SpdSel2->LocalPortRange && SpdSel2->LocalPort != 0)\r
+      ) {\r
+    IsMatch = FALSE;\r
+    return IsMatch;\r
+  }\r
+  \r
+  //\r
+  // Compare the all LocalAddress fields in the two Spdselectors.\r
+  // First, SpdSel1->LocalAddress to SpdSel2->LocalAddress && Compare \r
+  // SpdSel1->RemoteAddress to SpdSel2->RemoteAddress. If all match, return\r
+  // TRUE.\r
+  //\r
+  for (Index = 0; Index < SpdSel1->LocalAddressCount; Index++) {\r
+    if (!IsInAddressInfoList (\r
+          &SpdSel1->LocalAddress[Index],\r
+          SpdSel2->RemoteAddress,\r
+          SpdSel2->RemoteAddressCount\r
+          )) {\r
+      IsMatch = FALSE;\r
+      break;\r
+    }\r
+  }\r
+\r
+  if (IsMatch) {\r
+    for (Index = 0; Index < SpdSel1->RemoteAddressCount; Index++) {\r
+      if (!IsInAddressInfoList (\r
+            &SpdSel1->RemoteAddress[Index],\r
+            SpdSel2->LocalAddress,\r
+            SpdSel2->LocalAddressCount\r
+            )) {\r
+        IsMatch = FALSE;\r
+        break;\r
+      }\r
+    }\r
+  }\r
+  return IsMatch;\r
+  \r
+}\r
+\r
 /**\r
   Compare two SA IDs.\r
 \r
@@ -317,7 +469,7 @@ CompareSpdSelector (
 \r
   @retval  TRUE    This two Selectors have the same SA ID.\r
   @retval  FALSE   This two Selecotrs don't have the same SA ID.\r
-\r
+  \r
 **/\r
 BOOLEAN\r
 CompareSaId (\r
@@ -348,7 +500,7 @@ CompareSaId (
 \r
   @retval  TRUE    This two Selectors have the same PAD ID.\r
   @retval  FALSE   This two Selecotrs don't have the same PAD ID.\r
-\r
+  \r
 **/\r
 BOOLEAN\r
 ComparePadId (\r
@@ -435,16 +587,14 @@ IsZeroSaId (
   IN EFI_IPSEC_CONFIG_SELECTOR        *Selector\r
   )\r
 {\r
-  EFI_IP_ADDRESS  *DestAddr;\r
-  EFI_IP_ADDRESS  ZeroAddr;\r
-  BOOLEAN         IsZero;\r
-\r
-  DestAddr  = &Selector->SaId.DestAddress;\r
+  BOOLEAN                   IsZero;\r
+  EFI_IPSEC_CONFIG_SELECTOR ZeroSelector;\r
+  \r
   IsZero    = FALSE;\r
 \r
-  ZeroMem (&ZeroAddr, sizeof (EFI_IP_ADDRESS));\r
+  ZeroMem (&ZeroSelector, sizeof (EFI_IPSEC_CONFIG_SELECTOR));\r
 \r
-  if (CompareMem (DestAddr, &ZeroAddr, sizeof (EFI_IP_ADDRESS)) == 0) {\r
+  if (CompareMem (&ZeroSelector, Selector, sizeof (EFI_IPSEC_CONFIG_SELECTOR)) == 0) {\r
     IsZero = TRUE;\r
   }\r
 \r
@@ -486,14 +636,14 @@ IsZeroPadId (
 \r
   @param[in, out] DstSel             Pointer of Destination SPD Selector.\r
   @param[in]      SrcSel             Pointer of Source SPD Selector.\r
-  @param[in, out] Size               The size of the Destination SPD Selector. If it\r
-                                     not NULL and its value less than the size of\r
-                                     Source SPD Selector, the value of Source SPD\r
+  @param[in, out] Size               The size of the Destination SPD Selector. If it \r
+                                     not NULL and its value less than the size of \r
+                                     Source SPD Selector, the value of Source SPD \r
                                      Selector's size will be passed to caller by this\r
                                      parameter.\r
 \r
   @retval EFI_INVALID_PARAMETER  If the Destination or Source SPD Selector is NULL\r
-  @retval EFI_BUFFER_TOO_SMALL   If the input Size is less than size of the Source SPD Selector.\r
+  @retval EFI_BUFFER_TOO_SMALL   If the input Size is less than size of the Source SPD Selector. \r
   @retval EFI_SUCCESS            Copy Source SPD Selector to the Destination SPD\r
                                  Selector successfully.\r
 \r
@@ -520,12 +670,12 @@ DuplicateSpdSelector (
     return EFI_BUFFER_TOO_SMALL;\r
   }\r
   //\r
-  // Copy the base structure of spd selector.\r
+  // Copy the base structure of SPD selector.\r
   //\r
   CopyMem (Dst, Src, sizeof (EFI_IPSEC_SPD_SELECTOR));\r
 \r
   //\r
-  // Copy the local address array of spd selector.\r
+  // Copy the local address array of SPD selector.\r
   //\r
   Dst->LocalAddress = (EFI_IP_ADDRESS_INFO *) (Dst + 1);\r
   CopyMem (\r
@@ -535,7 +685,7 @@ DuplicateSpdSelector (
     );\r
 \r
   //\r
-  // Copy the remote address array of spd selector.\r
+  // Copy the remote address array of SPD selector.\r
   //\r
   Dst->RemoteAddress = Dst->LocalAddress + Dst->LocalAddressCount;\r
   CopyMem (\r
@@ -552,13 +702,13 @@ DuplicateSpdSelector (
 \r
   @param[in, out] DstSel             Pointer of Destination SA ID.\r
   @param[in]      SrcSel             Pointer of Source SA ID.\r
-  @param[in, out] Size               The size of the Destination SA ID. If it\r
-                                     not NULL and its value less than the size of\r
-                                     Source SA ID, the value of Source SA ID's size\r
+  @param[in, out] Size               The size of the Destination SA ID. If it \r
+                                     not NULL and its value less than the size of \r
+                                     Source SA ID, the value of Source SA ID's size \r
                                      will be passed to caller by this parameter.\r
 \r
   @retval EFI_INVALID_PARAMETER  If the Destination or Source SA ID is NULL.\r
-  @retval EFI_BUFFER_TOO_SMALL   If the input Size less than size of source SA ID.\r
+  @retval EFI_BUFFER_TOO_SMALL   If the input Size less than size of source SA ID. \r
   @retval EFI_SUCCESS            Copy Source SA ID  to the Destination SA ID successfully.\r
 \r
 **/\r
@@ -594,9 +744,9 @@ DuplicateSaId (
 \r
   @param[in, out] DstSel             Pointer of Destination PAD ID.\r
   @param[in]      SrcSel             Pointer of Source PAD ID.\r
-  @param[in, out] Size               The size of the Destination PAD ID. If it\r
-                                     not NULL and its value less than the size of\r
-                                     Source PAD ID, the value of Source PAD ID's size\r
+  @param[in, out] Size               The size of the Destination PAD ID. If it \r
+                                     not NULL and its value less than the size of \r
+                                     Source PAD ID, the value of Source PAD ID's size \r
                                      will be passed to caller by this parameter.\r
 \r
   @retval EFI_INVALID_PARAMETER  If the Destination or Source PAD ID is NULL.\r
@@ -632,11 +782,11 @@ DuplicatePadId (
 }\r
 \r
 /**\r
-  Fix the value of some members of SPD Selector.\r
+  Fix the value of some members of SPD Selector. \r
 \r
-  This function is called by IpSecCopyPolicyEntry()which copy the Policy\r
-  Entry into the Variable. Since some members in SPD Selector are pointers,\r
-  a physical address to relative address convertion is required before copying\r
+  This function is called by IpSecCopyPolicyEntry()which copy the Policy \r
+  Entry into the Variable. Since some members in SPD Selector are pointers, \r
+  a physical address to relative address convertion is required before copying \r
   this SPD entry into the variable.\r
 \r
   @param[in]       Selector              Pointer of SPD Selector.\r
@@ -650,7 +800,7 @@ FixSpdEntry (
   )\r
 {\r
   //\r
-  // It assumes that all ref buffers in spd selector and data are\r
+  // It assumes that all ref buffers in SPD selector and data are\r
   // stored in the continous memory and close to the base structure.\r
   //\r
   FIX_REF_BUF_ADDR (Selector->LocalAddress, Selector);\r
@@ -667,11 +817,11 @@ FixSpdEntry (
 }\r
 \r
 /**\r
-  Fix the value of some members of SA ID.\r
+  Fix the value of some members of SA ID. \r
 \r
-  This function is called by IpSecCopyPolicyEntry()which copy the Policy\r
-  Entry into the Variable. Since some members in SA ID are pointers,\r
-  a physical address to relative address conversion is required before copying\r
+  This function is called by IpSecCopyPolicyEntry()which copy the Policy \r
+  Entry into the Variable. Since some members in SA ID are pointers, \r
+  a physical address to relative address conversion is required before copying \r
   this SAD into the variable.\r
 \r
   @param[in]       SaId                  Pointer of SA ID\r
@@ -681,11 +831,11 @@ FixSpdEntry (
 VOID\r
 FixSadEntry (\r
   IN     EFI_IPSEC_SA_ID                  *SaId,\r
-  IN OUT EFI_IPSEC_SA_DATA                *Data\r
+  IN OUT EFI_IPSEC_SA_DATA2                *Data\r
   )\r
 {\r
   //\r
-  // It assumes that all ref buffers in sad selector and data are\r
+  // It assumes that all ref buffers in SAD selector and data are\r
   // stored in the continous memory and close to the base structure.\r
   //\r
   if (Data->AlgoInfo.EspAlgoInfo.AuthKey != NULL) {\r
@@ -708,10 +858,10 @@ FixSadEntry (
 }\r
 \r
 /**\r
-  Fix the value of some members of PAD ID.\r
+  Fix the value of some members of PAD ID. \r
 \r
-  This function is called by IpSecCopyPolicyEntry()which copy the Policy\r
-  Entry into the Variable. Since some members in PAD ID are pointers,\r
+  This function is called by IpSecCopyPolicyEntry()which copy the Policy \r
+  Entry into the Variable. Since some members in PAD ID are pointers, \r
   a physical address to relative address conversion is required before copying\r
   this PAD into the variable.\r
 \r
@@ -740,7 +890,7 @@ FixPadEntry (
 }\r
 \r
 /**\r
-  Recover the value of some members of SPD Selector.\r
+  Recover the value of some members of SPD Selector. \r
 \r
   This function is corresponding to FixSpdEntry(). It recovers the value of members\r
   of SPD Selector that are fixed by FixSpdEntry().\r
@@ -756,7 +906,7 @@ UnfixSpdEntry (
   )\r
 {\r
   //\r
-  // It assumes that all ref buffers in spd selector and data are\r
+  // It assumes that all ref buffers in SPD selector and data are\r
   // stored in the continous memory and close to the base structure.\r
   //\r
   UNFIX_REF_BUF_ADDR (Selector->LocalAddress, Selector);\r
@@ -768,11 +918,11 @@ UnfixSpdEntry (
       UNFIX_REF_BUF_ADDR (Data->ProcessingPolicy->TunnelOption, Data);\r
     }\r
   }\r
-\r
+  \r
 }\r
 \r
 /**\r
-  Recover the value of some members of SA ID.\r
+  Recover the value of some members of SA ID. \r
 \r
   This function is corresponding to FixSadEntry(). It recovers the value of members\r
   of SAD ID that are fixed by FixSadEntry().\r
@@ -784,11 +934,11 @@ UnfixSpdEntry (
 VOID\r
 UnfixSadEntry (\r
   IN OUT EFI_IPSEC_SA_ID                     *SaId,\r
-  IN OUT EFI_IPSEC_SA_DATA                   *Data\r
+  IN OUT EFI_IPSEC_SA_DATA2                   *Data\r
   )\r
 {\r
   //\r
-  // It assumes that all ref buffers in sad selector and data are\r
+  // It assumes that all ref buffers in SAD selector and data are\r
   // stored in the continous memory and close to the base structure.\r
   //\r
   if (Data->AlgoInfo.EspAlgoInfo.AuthKey != NULL) {\r
@@ -811,7 +961,7 @@ UnfixSadEntry (
 }\r
 \r
 /**\r
-  Recover the value of some members of PAD ID.\r
+  Recover the value of some members of PAD ID. \r
 \r
   This function is corresponding to FixPadEntry(). It recovers the value of members\r
   of PAD ID that are fixed by FixPadEntry().\r
@@ -843,30 +993,30 @@ UnfixPadEntry (
 /**\r
   Set the security policy information for the EFI IPsec driver.\r
 \r
-  The IPsec configuration data has a unique selector/identifier separately to\r
+  The IPsec configuration data has a unique selector/identifier separately to \r
   identify a data entry.\r
 \r
-  @param[in]  Selector           Pointer to an entry selector on operated\r
-                                 configuration data specified by DataType.\r
-                                 A NULL Selector causes the entire specified-type\r
+  @param[in]  Selector           Pointer to an entry selector on operated \r
+                                 configuration data specified by DataType. \r
+                                 A NULL Selector causes the entire specified-type \r
                                  configuration information to be flushed.\r
-  @param[in]  Data               The data buffer to be set. The structure\r
+  @param[in]  Data               The data buffer to be set. The structure \r
                                  of the data buffer should be EFI_IPSEC_SPD_DATA.\r
-  @param[in]  Context            Pointer to one entry selector that describes\r
-                                 the expected position the new data entry will\r
+  @param[in]  Context            Pointer to one entry selector that describes \r
+                                 the expected position the new data entry will \r
                                  be added. If Context is NULL, the new entry will\r
                                  be appended the end of database.\r
 \r
   @retval EFI_INVALID_PARAMETER  One or more of the following are TRUE:\r
-                                   - Selector is not NULL and its LocalAddress\r
+                                   - Selector is not NULL and its LocalAddress \r
                                      is NULL or its RemoteAddress is NULL.\r
-                                   - Data is not NULL and its Action is Protected\r
+                                   - Data is not NULL and its Action is Protected \r
                                      and its plolicy is NULL.\r
                                    - Data is not NULL, its Action is not protected,\r
                                      and its policy is not NULL.\r
-                                   - The Action of Data is Protected, its policy\r
+                                   - The Action of Data is Protected, its policy \r
                                      mode is Tunnel, and its tunnel option is NULL.\r
-                                   - The Action of Data is protected and its policy\r
+                                   - The Action of Data is protected and its policy \r
                                      mode is not Tunnel and it tunnel option is not NULL.\r
   @retval EFI_OUT_OF_RESOURCED  The required system resource could not be allocated.\r
   @retval EFI_SUCCESS           The specified configuration data was obtained successfully.\r
@@ -887,8 +1037,8 @@ SetSpdEntry (
   LIST_ENTRY              *SpdSas;\r
   LIST_ENTRY              *EntryInsertBefore;\r
   LIST_ENTRY              *Entry;\r
-  LIST_ENTRY              *NextEntry;\r
   LIST_ENTRY              *Entry2;\r
+  LIST_ENTRY              *NextEntry;\r
   IPSEC_SPD_ENTRY         *SpdEntry;\r
   IPSEC_SAD_ENTRY         *SadEntry;\r
   UINTN                   SpdEntrySize;\r
@@ -926,13 +1076,13 @@ SetSpdEntry (
   EntryInsertBefore = SpdList;\r
 \r
   //\r
-  // Remove the existed spd entry.\r
+  // Remove the existed SPD entry.\r
   //\r
   NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, SpdList) {\r
 \r
     SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
 \r
-    if (SpdSel == NULL ||\r
+    if (SpdSel == NULL || \r
         CompareSpdSelector ((EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector, (EFI_IPSEC_CONFIG_SELECTOR *) SpdSel)\r
         ) {\r
       //\r
@@ -942,21 +1092,26 @@ SetSpdEntry (
       RemoveEntryList (&SpdEntry->List);\r
 \r
       //\r
-      // Update the reverse ref of sad entry in the spd.sas list.\r
+      // Update the reverse ref of SAD entry in the SPD.sas list.\r
       //\r
       SpdSas = &SpdEntry->Data->Sas;\r
+      \r
+      //\r
+      // TODO: Deleted the related SAs.\r
+      //\r
       NET_LIST_FOR_EACH (Entry2, SpdSas) {\r
         SadEntry                  = IPSEC_SAD_ENTRY_FROM_SPD (Entry2);\r
         SadEntry->Data->SpdEntry  = NULL;\r
       }\r
+      \r
       //\r
-      // Free the existed spd entry\r
+      // Free the existed SPD entry\r
       //\r
       FreePool (SpdEntry);\r
     }\r
   }\r
   //\r
-  // Return success here if only want to remove the spd entry.\r
+  // Return success here if only want to remove the SPD entry.\r
   //\r
   if (SpdData == NULL || SpdSel == NULL) {\r
     return EFI_SUCCESS;\r
@@ -993,7 +1148,7 @@ SetSpdEntry (
   }\r
   //\r
   // Fix the address of Selector and Data buffer and copy them, which is\r
-  // continous memory and close to the base structure of spd entry.\r
+  // continous memory and close to the base structure of SPD entry.\r
   //\r
   SpdEntry->Selector  = (EFI_IPSEC_SPD_SELECTOR *) ALIGN_POINTER ((SpdEntry + 1), sizeof (UINTN));\r
   SpdEntry->Data      = (IPSEC_SPD_DATA *) ALIGN_POINTER (\r
@@ -1017,7 +1172,7 @@ SetSpdEntry (
 \r
   //\r
   // Fix the address of ProcessingPolicy and copy it if need, which is continous\r
-  // memory and close to the base structure of sad data.\r
+  // memory and close to the base structure of SAD data.\r
   //\r
   if (SpdData->Action != EfiIPsecActionProtect) {\r
     SpdEntry->Data->ProcessingPolicy = NULL;\r
@@ -1029,7 +1184,7 @@ SetSpdEntry (
     IpSecDuplicateProcessPolicy (SpdEntry->Data->ProcessingPolicy, SpdData->ProcessingPolicy);\r
   }\r
   //\r
-  // Update the sas list of the new spd entry.\r
+  // Update the sas list of the new SPD entry.\r
   //\r
   InitializeListHead (&SpdEntry->Data->Sas);\r
 \r
@@ -1044,13 +1199,21 @@ SetSpdEntry (
             (EFI_IPSEC_CONFIG_SELECTOR *) &SpdData->SaId[Index],\r
             (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id\r
             )) {\r
+        if (SadEntry->Data->SpdEntry != NULL) {  \r
+          RemoveEntryList (&SadEntry->BySpd);\r
+        }\r
         InsertTailList (&SpdEntry->Data->Sas, &SadEntry->BySpd);\r
         SadEntry->Data->SpdEntry = SpdEntry;\r
+        DuplicateSpdSelector (\r
+          (EFI_IPSEC_CONFIG_SELECTOR *)SadEntry->Data->SpdSelector,\r
+          (EFI_IPSEC_CONFIG_SELECTOR *)SpdEntry->Selector,\r
+          NULL\r
+          );             \r
       }\r
     }\r
   }\r
   //\r
-  // Insert the new spd entry.\r
+  // Insert the new SPD entry.\r
   //\r
   InsertTailList (EntryInsertBefore, &SpdEntry->List);\r
 \r
@@ -1060,17 +1223,17 @@ SetSpdEntry (
 /**\r
   Set the security association information for the EFI IPsec driver.\r
 \r
-  The IPsec configuration data has a unique selector/identifier separately to\r
+  The IPsec configuration data has a unique selector/identifier separately to \r
   identify a data entry.\r
 \r
-  @param[in]  Selector           Pointer to an entry selector on operated\r
-                                 configuration data specified by DataType.\r
-                                 A NULL Selector causes the entire specified-type\r
+  @param[in]  Selector           Pointer to an entry selector on operated \r
+                                 configuration data specified by DataType. \r
+                                 A NULL Selector causes the entire specified-type \r
                                  configuration information to be flushed.\r
-  @param[in]  Data               The data buffer to be set. The structure\r
+  @param[in]  Data               The data buffer to be set. The structure \r
                                  of the data buffer should be EFI_IPSEC_SA_DATA.\r
-  @param[in]  Context            Pointer to one entry selector which describes\r
-                                 the expected position the new data entry will\r
+  @param[in]  Context            Pointer to one entry selector which describes \r
+                                 the expected position the new data entry will \r
                                  be added. If Context is NULL,the new entry will\r
                                  be appended the end of database.\r
 \r
@@ -1092,13 +1255,13 @@ SetSadEntry (
   LIST_ENTRY        *SadList;\r
   LIST_ENTRY        *SpdList;\r
   EFI_IPSEC_SA_ID   *SaId;\r
-  EFI_IPSEC_SA_DATA *SaData;\r
+  EFI_IPSEC_SA_DATA2 *SaData;\r
   EFI_IPSEC_SA_ID   *InsertBefore;\r
   LIST_ENTRY        *EntryInsertBefore;\r
   UINTN             SadEntrySize;\r
-\r
+  \r
   SaId          = (Selector == NULL) ? NULL : &Selector->SaId;\r
-  SaData        = (Data == NULL) ? NULL : (EFI_IPSEC_SA_DATA *) Data;\r
+  SaData        = (Data == NULL) ? NULL : (EFI_IPSEC_SA_DATA2 *) Data;\r
   InsertBefore  = (Context == NULL) ? NULL : &((EFI_IPSEC_CONFIG_SELECTOR *) Context)->SaId;\r
   SadList       = &mConfigData[IPsecConfigDataTypeSad];\r
 \r
@@ -1108,13 +1271,13 @@ SetSadEntry (
   EntryInsertBefore = SadList;\r
 \r
   //\r
-  // Remove the existed sad entry.\r
+  // Remove the existed SAD entry.\r
   //\r
   NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, SadList) {\r
 \r
     SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
 \r
-    if (SaId == NULL ||\r
+    if (SaId == NULL || \r
         CompareSaId (\r
           (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id,\r
           (EFI_IPSEC_CONFIG_SELECTOR *) SaId\r
@@ -1125,7 +1288,7 @@ SetSadEntry (
       EntryInsertBefore = SadEntry->List.ForwardLink;\r
 \r
       //\r
-      // Update the related sad.byspd field.\r
+      // Update the related SAD.byspd field.\r
       //\r
       if (SadEntry->Data->SpdEntry != NULL) {\r
         RemoveEntryList (&SadEntry->BySpd);\r
@@ -1136,7 +1299,7 @@ SetSadEntry (
     }\r
   }\r
   //\r
-  // Return success here if only want to remove the sad entry\r
+  // Return success here if only want to remove the SAD entry\r
   //\r
   if (SaData == NULL || SaId == NULL) {\r
     return EFI_SUCCESS;\r
@@ -1163,16 +1326,19 @@ SetSadEntry (
   // Do Padding for different Arch.\r
   //\r
   SadEntrySize  = ALIGN_VARIABLE (sizeof (IPSEC_SAD_ENTRY));\r
-  SadEntrySize  = ALIGN_VARIABLE (SadEntrySize + sizeof (EFI_IPSEC_SA_DATA));\r
+  SadEntrySize  = ALIGN_VARIABLE (SadEntrySize + sizeof (EFI_IPSEC_SA_ID));\r
   SadEntrySize  = ALIGN_VARIABLE (SadEntrySize + sizeof (IPSEC_SAD_DATA));\r
-\r
+  \r
   if (SaId->Proto == EfiIPsecAH) {\r
     SadEntrySize += SaData->AlgoInfo.AhAlgoInfo.AuthKeyLength;\r
   } else {\r
     SadEntrySize  = ALIGN_VARIABLE (SadEntrySize + SaData->AlgoInfo.EspAlgoInfo.AuthKeyLength);\r
-    SadEntrySize += SaData->AlgoInfo.EspAlgoInfo.EncKeyLength;\r
+    SadEntrySize += ALIGN_VARIABLE (SaData->AlgoInfo.EspAlgoInfo.EncKeyLength);\r
   }\r
 \r
+  if (SaData->SpdSelector != NULL) {\r
+    SadEntrySize += SadEntrySize + (UINTN)SIZE_OF_SPD_SELECTOR (SaData->SpdSelector);\r
+  }\r
   SadEntry      = AllocateZeroPool (SadEntrySize);\r
 \r
   if (SadEntry == NULL) {\r
@@ -1180,7 +1346,7 @@ SetSadEntry (
   }\r
   //\r
   // Fix the address of Id and Data buffer and copy them, which is\r
-  // continous memory and close to the base structure of sad entry.\r
+  // continous memory and close to the base structure of SAD entry.\r
   //\r
   SadEntry->Id    = (EFI_IPSEC_SA_ID *) ALIGN_POINTER ((SadEntry + 1), sizeof (UINTN));\r
   SadEntry->Data  = (IPSEC_SAD_DATA *) ALIGN_POINTER ((SadEntry->Id + 1), sizeof (UINTN));\r
@@ -1219,7 +1385,7 @@ SetSadEntry (
 \r
     if (SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength != 0) {\r
       SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKey = (VOID *) ALIGN_POINTER (\r
-                                                               ((UINT8 *) (SadEntry->Data + 1) +\r
+                                                               ((UINT8 *) (SadEntry->Data + 1) + \r
                                                                  SadEntry->Data->AlgoInfo.EspAlgoInfo.AuthKeyLength),\r
                                                                  sizeof (UINTN)\r
                                                                  );\r
@@ -1238,28 +1404,52 @@ SetSadEntry (
     );\r
 \r
   SadEntry->Data->PathMTU     = SaData->PathMTU;\r
-  SadEntry->Data->SpdEntry    = NULL;\r
+  SadEntry->Data->SpdSelector = NULL;\r
   SadEntry->Data->ESNEnabled  = FALSE;\r
   SadEntry->Data->ManualSet   = SaData->ManualSet;\r
 \r
   //\r
-  // Update the spd.sas list of the spd entry specified by sad.selector\r
+  // Copy Tunnel Source/Destination Address\r
+  //\r
+  if (SaData->Mode == EfiIPsecTunnel) {\r
+    CopyMem (\r
+      &SadEntry->Data->TunnelDestAddress,\r
+      &SaData->TunnelDestinationAddress,\r
+      sizeof (EFI_IP_ADDRESS)\r
+      );\r
+    CopyMem (\r
+      &SadEntry->Data->TunnelSourceAddress,\r
+      &SaData->TunnelSourceAddress,\r
+      sizeof (EFI_IP_ADDRESS)\r
+      );\r
+  }\r
+  //\r
+  // Update the spd.sas list of the spd entry specified by SAD selector\r
   //\r
   SpdList = &mConfigData[IPsecConfigDataTypeSpd];\r
 \r
   for (Entry = SpdList->ForwardLink; Entry != SpdList && SaData->SpdSelector != NULL; Entry = Entry->ForwardLink) {\r
 \r
     SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
-    if (CompareSpdSelector (\r
-          (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector,\r
-          (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector\r
+    if (IsSubSpdSelector (\r
+          (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
+          (EFI_IPSEC_CONFIG_SELECTOR *) SpdEntry->Selector\r
           ) && SpdEntry->Data->Action == EfiIPsecActionProtect) {\r
       SadEntry->Data->SpdEntry = SpdEntry;\r
+      SadEntry->Data->SpdSelector = (EFI_IPSEC_SPD_SELECTOR *)((UINT8 *)SadEntry +\r
+                                                                SadEntrySize -\r
+                                                                (UINTN)SIZE_OF_SPD_SELECTOR (SaData->SpdSelector)\r
+                                                                );\r
+      DuplicateSpdSelector (\r
+       (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Data->SpdSelector,\r
+       (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
+       NULL\r
+       );\r
       InsertTailList (&SpdEntry->Data->Sas, &SadEntry->BySpd);\r
     }\r
   }\r
   //\r
-  // Insert the new sad entry.\r
+  // Insert the new SAD entry.\r
   //\r
   InsertTailList (EntryInsertBefore, &SadEntry->List);\r
 \r
@@ -1269,17 +1459,17 @@ SetSadEntry (
 /**\r
   Set the peer authorization configuration information for the EFI IPsec driver.\r
 \r
-  The IPsec configuration data has a unique selector/identifier separately to\r
+  The IPsec configuration data has a unique selector/identifier separately to \r
   identify a data entry.\r
 \r
-  @param[in]  Selector           Pointer to an entry selector on operated\r
-                                 configuration data specified by DataType.\r
-                                 A NULL Selector causes the entire specified-type\r
+  @param[in]  Selector           Pointer to an entry selector on operated \r
+                                 configuration data specified by DataType. \r
+                                 A NULL Selector causes the entire specified-type \r
                                  configuration information to be flushed.\r
-  @param[in]  Data               The data buffer to be set. The structure\r
+  @param[in]  Data               The data buffer to be set. The structure \r
                                  of the data buffer should be EFI_IPSEC_PAD_DATA.\r
-  @param[in]  Context            Pointer to one entry selector that describes\r
-                                 the expected position the new data entry will\r
+  @param[in]  Context            Pointer to one entry selector that describes \r
+                                 the expected position the new data entry will \r
                                  be added. If Context is NULL, the new entry will\r
                                  be appended the end of database.\r
 \r
@@ -1303,7 +1493,7 @@ SetPadEntry (
   EFI_IPSEC_PAD_ID    *InsertBefore;\r
   LIST_ENTRY          *EntryInsertBefore;\r
   UINTN               PadEntrySize;\r
-\r
+   \r
   PadId         = (Selector == NULL) ? NULL : &Selector->PadId;\r
   PadData       = (Data == NULL) ? NULL : (EFI_IPSEC_PAD_DATA *) Data;\r
   InsertBefore  = (Context == NULL) ? NULL : &((EFI_IPSEC_CONFIG_SELECTOR *) Context)->PadId;\r
@@ -1321,7 +1511,7 @@ SetPadEntry (
 \r
     PadEntry = IPSEC_PAD_ENTRY_FROM_LIST (Entry);\r
 \r
-    if (PadId == NULL ||\r
+    if (PadId == NULL || \r
         ComparePadId ((EFI_IPSEC_CONFIG_SELECTOR *) PadEntry->Id, (EFI_IPSEC_CONFIG_SELECTOR *) PadId)\r
         ) {\r
       //\r
@@ -1421,16 +1611,16 @@ SetPadEntry (
 }\r
 \r
 /**\r
-  This function lookup the data entry from IPsec SPD. Return the configuration\r
+  This function lookup the data entry from IPsec SPD. Return the configuration \r
   value of the specified SPD Entry.\r
 \r
-  @param[in]      Selector      Pointer to an entry selector which is an identifier\r
+  @param[in]      Selector      Pointer to an entry selector which is an identifier \r
                                 of the SPD entry.\r
   @param[in, out] DataSize      On output the size of data returned in Data.\r
-  @param[out]     Data          The buffer to return the contents of the IPsec\r
-                                configuration data. The type of the data buffer\r
-                                is associated with the DataType.\r
-\r
+  @param[out]     Data          The buffer to return the contents of the IPsec \r
+                                configuration data. The type of the data buffer \r
+                                is associated with the DataType. \r
\r
   @retval EFI_SUCCESS           The specified configuration data was obtained successfully.\r
   @retval EFI_INVALID_PARAMETER Data is NULL and *DataSize is not zero.\r
   @retval EFI_NOT_FOUND         The configuration data specified by Selector is not found.\r
@@ -1462,7 +1652,7 @@ GetSpdEntry (
     SpdEntry = IPSEC_SPD_ENTRY_FROM_LIST (Entry);\r
 \r
     //\r
-    // Find the required spd entry\r
+    // Find the required SPD entry\r
     //\r
     if (CompareSpdSelector (\r
           (EFI_IPSEC_CONFIG_SELECTOR *) SpdSel,\r
@@ -1482,7 +1672,7 @@ GetSpdEntry (
       *DataSize = RequiredSize;\r
 \r
       //\r
-      // Extract and fill all SaId array from the spd.sas list\r
+      // Extract and fill all SaId array from the SPD.sas list\r
       //\r
       SpdSas              = &SpdEntry->Data->Sas;\r
       SpdData->SaIdCount  = 0;\r
@@ -1496,7 +1686,7 @@ GetSpdEntry (
           );\r
       }\r
       //\r
-      // Fill the other fields in spd data.\r
+      // Fill the other fields in SPD data.\r
       //\r
       CopyMem (SpdData->Name, SpdEntry->Data->Name, sizeof (SpdData->Name));\r
 \r
@@ -1522,16 +1712,16 @@ GetSpdEntry (
 }\r
 \r
 /**\r
-  This function lookup the data entry from IPsec SAD. Return the configuration\r
+  This function lookup the data entry from IPsec SAD. Return the configuration \r
   value of the specified SAD Entry.\r
 \r
-  @param[in]      Selector      Pointer to an entry selector which is an identifier\r
+  @param[in]      Selector      Pointer to an entry selector which is an identifier \r
                                 of the SAD entry.\r
   @param[in, out] DataSize      On output, the size of data returned in Data.\r
-  @param[out]     Data          The buffer to return the contents of the IPsec\r
-                                configuration data. The type of the data buffer\r
-                                is associated with the DataType.\r
-\r
+  @param[out]     Data          The buffer to return the contents of the IPsec \r
+                                configuration data. The type of the data buffer \r
+                                is associated with the DataType. \r
\r
   @retval EFI_SUCCESS           The specified configuration data was obtained successfully.\r
   @retval EFI_NOT_FOUND         The configuration data specified by Selector is not found.\r
   @retval EFI_BUFFER_TOO_SMALL  The DataSize is too small for the result. DataSize has been\r
@@ -1549,32 +1739,32 @@ GetSadEntry (
   LIST_ENTRY        *Entry;\r
   LIST_ENTRY        *SadList;\r
   EFI_IPSEC_SA_ID   *SaId;\r
-  EFI_IPSEC_SA_DATA *SaData;\r
+  EFI_IPSEC_SA_DATA2 *SaData;\r
   UINTN             RequiredSize;\r
 \r
   SaId    = &Selector->SaId;\r
-  SaData  = (EFI_IPSEC_SA_DATA *) Data;\r
+  SaData  = (EFI_IPSEC_SA_DATA2 *) Data;\r
   SadList = &mConfigData[IPsecConfigDataTypeSad];\r
 \r
   NET_LIST_FOR_EACH (Entry, SadList) {\r
     SadEntry = IPSEC_SAD_ENTRY_FROM_LIST (Entry);\r
 \r
     //\r
-    // Find the required sad entry.\r
+    // Find the required SAD entry.\r
     //\r
     if (CompareSaId (\r
          (EFI_IPSEC_CONFIG_SELECTOR *) SaId,\r
          (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Id\r
          )) {\r
       //\r
-      // Calculate the required size of the sad entry.\r
+      // Calculate the required size of the SAD entry.\r
       // Data Layout is follows:\r
       // |EFI_IPSEC_SA_DATA\r
       // |AuthKey\r
       // |EncryptKey  (Optional)\r
-      // |SpdSelector (Optional)\r
-      //\r
-      RequiredSize  = ALIGN_VARIABLE (sizeof (EFI_IPSEC_SA_DATA));\r
+      // |SpdSelector (Optional)     \r
+      // \r
+      RequiredSize  = ALIGN_VARIABLE (sizeof (EFI_IPSEC_SA_DATA2));\r
 \r
       if (SaId->Proto == EfiIPsecAH) {\r
         RequiredSize  = ALIGN_VARIABLE (RequiredSize + SadEntry->Data->AlgoInfo.AhAlgoInfo.AuthKeyLength);\r
@@ -1583,18 +1773,17 @@ GetSadEntry (
         RequiredSize  = ALIGN_VARIABLE (RequiredSize + SadEntry->Data->AlgoInfo.EspAlgoInfo.EncKeyLength);\r
       }\r
 \r
-      if (SadEntry->Data->SpdEntry != NULL) {\r
-        RequiredSize += SIZE_OF_SPD_SELECTOR (SadEntry->Data->SpdEntry->Selector);\r
+      if (SadEntry->Data->SpdSelector != NULL) {\r
+        RequiredSize += SIZE_OF_SPD_SELECTOR (SadEntry->Data->SpdSelector);\r
       }\r
-\r
-\r
-\r
+      \r
       if (*DataSize < RequiredSize) {\r
         *DataSize = RequiredSize;\r
         return EFI_BUFFER_TOO_SMALL;\r
       }\r
+      \r
       //\r
-      // Fill the data fields of sad entry.\r
+      // Fill the data fields of SAD entry.\r
       //\r
       *DataSize                 = RequiredSize;\r
       SaData->Mode              = SadEntry->Data->Mode;\r
@@ -1661,19 +1850,34 @@ GetSadEntry (
       SaData->PathMTU = SadEntry->Data->PathMTU;\r
 \r
       //\r
-      // Fill the spd selector field of sad data\r
+      // Fill Tunnel Address if it is Tunnel Mode\r
       //\r
-      if (SadEntry->Data->SpdEntry != NULL) {\r
+      if (SadEntry->Data->Mode == EfiIPsecTunnel) {\r
+        CopyMem (\r
+          &SaData->TunnelDestinationAddress,\r
+          &SadEntry->Data->TunnelDestAddress,\r
+          sizeof (EFI_IP_ADDRESS)\r
+          );\r
+        CopyMem (\r
+          &SaData->TunnelSourceAddress,\r
+          &SadEntry->Data->TunnelSourceAddress,\r
+          sizeof (EFI_IP_ADDRESS)\r
+          );\r
+      }\r
+      //\r
+      // Fill the spd selector field of SAD data\r
+      //\r
+      if (SadEntry->Data->SpdSelector != NULL) {\r
 \r
         SaData->SpdSelector = (EFI_IPSEC_SPD_SELECTOR *) (\r
                                 (UINT8 *)SaData +\r
                                 RequiredSize -\r
-                                SIZE_OF_SPD_SELECTOR (SadEntry->Data->SpdEntry->Selector)\r
+                                SIZE_OF_SPD_SELECTOR (SadEntry->Data->SpdSelector)\r
                                 );\r
-\r
+       \r
         DuplicateSpdSelector (\r
           (EFI_IPSEC_CONFIG_SELECTOR *) SaData->SpdSelector,\r
-          (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Data->SpdEntry->Selector,\r
+          (EFI_IPSEC_CONFIG_SELECTOR *) SadEntry->Data->SpdSelector,\r
           NULL\r
           );\r
 \r
@@ -1692,16 +1896,16 @@ GetSadEntry (
 }\r
 \r
 /**\r
-  This function lookup the data entry from IPsec PAD. Return the configuration\r
+  This function lookup the data entry from IPsec PAD. Return the configuration \r
   value of the specified PAD Entry.\r
 \r
-  @param[in]      Selector      Pointer to an entry selector which is an identifier\r
+  @param[in]      Selector      Pointer to an entry selector which is an identifier \r
                                 of the PAD entry.\r
   @param[in, out] DataSize      On output the size of data returned in Data.\r
-  @param[out]     Data          The buffer to return the contents of the IPsec\r
-                                configuration data. The type of the data buffer\r
-                                is associated with the DataType.\r
-\r
+  @param[out]     Data          The buffer to return the contents of the IPsec \r
+                                configuration data. The type of the data buffer \r
+                                is associated with the DataType. \r
\r
   @retval EFI_SUCCESS           The specified configuration data was obtained successfully.\r
   @retval EFI_NOT_FOUND         The configuration data specified by Selector is not found.\r
   @retval EFI_BUFFER_TOO_SMALL  The DataSize is too small for the result. DataSize has been\r
@@ -1864,7 +2068,7 @@ IpSecGetSizeOfEfiSpdData (
 \r
 /**\r
   Calculate the a whole size of IPSEC_SPD_DATA which includes the buffer size pointed\r
-  to by the pointer members and the buffer size used by the Sa List.\r
+  to by the pointer members and the buffer size used by the Sa List. \r
 \r
   @param[in]  SpdData       Pointer to the specified IPSEC_SPD_DATA.\r
 \r
@@ -1900,25 +2104,25 @@ IpSecGetSizeOfSpdData (
   Get the IPsec Variable.\r
 \r
   Get the all variables which start with the string contained in VaraiableName.\r
-  Since all IPsec related variable store in continual space, those kinds of\r
-  variable can be searched by the EfiGetNextVariableName. Those variables also are\r
+  Since all IPsec related variable store in continual space, those kinds of \r
+  variable can be searched by the EfiGetNextVariableName. Those variables also are \r
   returned in a continual buffer.\r
-\r
+  \r
   @param[in]      VariableName          Pointer to a specified Variable Name.\r
   @param[in]      VendorGuid            Pointer to a specified Vendor Guid.\r
-  @param[in]      Attributes            Point to memory location to return the attributes\r
-                                        of variable. If the point is NULL, the parameter\r
+  @param[in]      Attributes            Point to memory location to return the attributes \r
+                                        of variable. If the point is NULL, the parameter \r
                                         would be ignored.\r
-  @param[in, out] DataSize              As input, point to the maximum size of return\r
-                                        Data-Buffer. As output, point to the actual\r
+  @param[in, out] DataSize              As input, point to the maximum size of return \r
+                                        Data-Buffer. As output, point to the actual \r
                                         size of the returned Data-Buffer.\r
   @param[in]      Data                  Point to return Data-Buffer.\r
-\r
+        \r
   @retval  EFI_ABORTED           If the Variable size which contained in the variable\r
-                                 structure doesn't match the variable size obtained\r
+                                 structure doesn't match the variable size obtained \r
                                  from the EFIGetVariable.\r
   @retval  EFI_BUFFER_TOO_SMALL  The DataSize is too small for the result. DataSize has\r
-                                 been updated with the size needed to complete the request.\r
+                                 been updated with the size needed to complete the request.   \r
   @retval  EFI_SUCCESS           The function completed successfully.\r
   @retval  others                Other errors found during the variable getting.\r
 **/\r
@@ -1951,7 +2155,7 @@ IpSecGetVariable (
   VariableNameISize   = (VariableNameLength + 5) * sizeof (CHAR16);\r
   VariableNameI       = AllocateZeroPool (VariableNameISize);\r
   ASSERT (VariableNameI != NULL);\r
-\r
+  \r
   //\r
   // Construct the varible name of ipsecconfig meta data.\r
   //\r
@@ -1995,6 +2199,10 @@ IpSecGetVariable (
                         VariableNameISizeNew,\r
                         VariableNameI\r
                         );\r
+      if (VariableNameI == NULL) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        break;\r
+      }\r
       VariableNameISize = VariableNameISizeNew;\r
 \r
       Status = gRT->GetNextVariableName (\r
@@ -2071,7 +2279,9 @@ IpSecGetVariable (
   }\r
 \r
 ON_EXIT:\r
-  FreePool (VariableNameI);\r
+  if (VariableNameI != NULL) {\r
+    FreePool (VariableNameI);\r
+  }\r
   return Status;\r
 }\r
 \r
@@ -2084,14 +2294,14 @@ ON_EXIT:
   @param[in]  VariableName  The name of the vendor's variable. It is a\r
                             Null-Terminated Unicode String.\r
   @param[in]  VendorGuid    Unify identifier for vendor.\r
-  @param[in]  Attributes    Point to memory location to return the attributes of\r
+  @param[in]  Attributes    Point to memory location to return the attributes of \r
                             variable. If the point is NULL, the parameter would be ignored.\r
   @param[in]  DataSize      The size in bytes of Data-Buffer.\r
   @param[in]  Data          Points to the content of the variable.\r
 \r
   @retval  EFI_SUCCESS      The firmware successfully stored the variable and its data, as\r
                             defined by the Attributes.\r
-  @retval  others           Storing the variables failed.\r
+  @retval  others           Storing the variables failed.      \r
 \r
 **/\r
 EFI_STATUS\r
@@ -2121,7 +2331,7 @@ IpSecSetVariable (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-\r
+  \r
   //\r
   // "VariableName + Info/0001/0002/... + NULL"\r
   //\r
@@ -2138,7 +2348,7 @@ IpSecSetVariable (
   //\r
   UnicodeSPrint (VariableNameI, VariableNameSize, L"%s%s", VariableName, L"Info");\r
   MaximumVariableSize -= VariableNameSize;\r
-\r
+  \r
   IpSecVariableInfo.VariableCount       = (UINT32) ((DataSize + (UINTN) MaximumVariableSize - 1) / (UINTN) MaximumVariableSize);\r
   IpSecVariableInfo.VariableSize        = (UINT32) DataSize;\r
   IpSecVariableInfo.SingleVariableSize  = (UINT32) MaximumVariableSize;\r
@@ -2163,7 +2373,7 @@ IpSecSetVariable (
     // Construct and set the variable of ipsecconfig data one by one.\r
     // The index of variable name begin from 0001, and the varaible name\r
     // likes "VariableName0001", "VaraiableName0002"....\r
-    //\r
+    // \r
     UnicodeSPrint (VariableNameI, VariableNameSize, L"%s%04d", VariableName, VariableIndex + 1);\r
     Status = gRT->SetVariable (\r
                     VariableNameI,\r
@@ -2190,20 +2400,20 @@ ON_EXIT:
 }\r
 \r
 /**\r
-  Return the configuration value for the EFI IPsec driver.\r
+  Return the configuration value for the EFI IPsec driver. \r
 \r
   This function lookup the data entry from IPsec database or IKEv2 configuration\r
   information. The expected data type and unique identification are described in\r
-  DataType and Selector parameters.\r
+  DataType and Selector parameters.        \r
 \r
   @param[in]      This          Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
   @param[in]      DataType      The type of data to retrieve.\r
-  @param[in]      Selector      Pointer to an entry selector that is an identifier of the IPsec\r
+  @param[in]      Selector      Pointer to an entry selector that is an identifier of the IPsec \r
                                 configuration data entry.\r
   @param[in, out] DataSize      On output the size of data returned in Data.\r
-  @param[out]     Data          The buffer to return the contents of the IPsec configuration data.\r
-                                The type of the data buffer associated with the DataType.\r
-\r
+  @param[out]     Data          The buffer to return the contents of the IPsec configuration data. \r
+                                The type of the data buffer associated with the DataType. \r
\r
   @retval EFI_SUCCESS           The specified configuration data was obtained successfully.\r
   @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
                                 - This is NULL.\r
@@ -2243,26 +2453,26 @@ EfiIpSecConfigGetData (
 \r
 /**\r
   Set the security association, security policy and peer authorization configuration\r
-  information for the EFI IPsec driver.\r
+  information for the EFI IPsec driver. \r
 \r
   This function is used to set the IPsec configuration information of type DataType for\r
   the EFI IPsec driver.\r
   The IPsec configuration data has a unique selector/identifier separately to identify\r
   a data entry. The selector structure depends on DataType's definition.\r
   Using SetData() with a Data of NULL causes the IPsec configuration data entry identified\r
-  by DataType and Selector to be deleted.\r
+  by DataType and Selector to be deleted.        \r
 \r
   @param[in] This               Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
   @param[in] DataType           The type of data to be set.\r
-  @param[in] Selector           Pointer to an entry selector on operated configuration data\r
-                                specified by DataType. A NULL Selector causes the entire\r
+  @param[in] Selector           Pointer to an entry selector on operated configuration data \r
+                                specified by DataType. A NULL Selector causes the entire \r
                                 specified-type configuration information to be flushed.\r
-  @param[in] Data               The data buffer to be set. The structure of the data buffer is\r
+  @param[in] Data               The data buffer to be set. The structure of the data buffer is \r
                                 associated with the DataType.\r
   @param[in] InsertBefore       Pointer to one entry selector which describes the expected\r
                                 position the new data entry will be added. If InsertBefore is NULL,\r
                                 the new entry will be appended to the end of the database.\r
-\r
\r
   @retval EFI_SUCCESS           The specified configuration entry data was set successfully.\r
   @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
                                 - This is NULL.\r
@@ -2289,7 +2499,7 @@ EfiIpSecConfigSetData (
   if (DataType >= IPsecConfigDataTypeMaximum) {\r
     return EFI_UNSUPPORTED;\r
   }\r
-\r
+  \r
   Status = mSetPolicyEntry[DataType](Selector, Data, InsertBefore);\r
 \r
   if (!EFI_ERROR (Status) && !mSetBySelf) {\r
@@ -2303,30 +2513,30 @@ EfiIpSecConfigSetData (
 }\r
 \r
 /**\r
-  Enumerates the current selector for IPsec configuration data entry.\r
+  Enumerates the current selector for IPsec configuration data entry. \r
 \r
   This function is called multiple times to retrieve the entry Selector in IPsec\r
-  configuration database. On each call to GetNextSelector(), the next entry\r
+  configuration database. On each call to GetNextSelector(), the next entry \r
   Selector are retrieved into the output interface.\r
-\r
-  If the entire IPsec configuration database has been iterated, the error\r
\r
+  If the entire IPsec configuration database has been iterated, the error \r
   EFI_NOT_FOUND is returned.\r
-  If the Selector buffer is too small for the next Selector copy, an\r
-  EFI_BUFFER_TOO_SMALL error is returned, and SelectorSize is updated to reflect\r
+  If the Selector buffer is too small for the next Selector copy, an \r
+  EFI_BUFFER_TOO_SMALL error is returned, and SelectorSize is updated to reflect \r
   the size of buffer needed.\r
 \r
   On the initial call to GetNextSelector() to start the IPsec configuration database\r
-  search, a pointer to the buffer with all zero value is passed in Selector. Calls\r
-  to SetData() between calls to GetNextSelector may produce unpredictable results.\r
+  search, a pointer to the buffer with all zero value is passed in Selector. Calls \r
+  to SetData() between calls to GetNextSelector may produce unpredictable results.         \r
 \r
   @param[in]      This          Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
   @param[in]      DataType      The type of IPsec configuration data to retrieve.\r
   @param[in, out] SelectorSize  The size of the Selector buffer.\r
-  @param[in, out] Selector      On input, supplies the pointer to last Selector that was\r
+  @param[in, out] Selector      On input, supplies the pointer to last Selector that was \r
                                 returned by GetNextSelector().\r
                                 On output, returns one copy of the current entry Selector\r
-                                of a given DataType.\r
-\r
+                                of a given DataType. \r
\r
   @retval EFI_SUCCESS           The specified configuration data was obtained successfully.\r
   @retval EFI_INVALID_PARAMETER One or more of the followings are TRUE:\r
                                 - This is NULL.\r
@@ -2335,7 +2545,7 @@ EfiIpSecConfigSetData (
   @retval EFI_NOT_FOUND         The next configuration data entry was not found.\r
   @retval EFI_UNSUPPORTED       The specified DataType is not supported.\r
   @retval EFI_BUFFER_TOO_SMALL  The SelectorSize is too small for the result. This parameter\r
-                                has been updated with the size needed to complete the search\r
+                                has been updated with the size needed to complete the search \r
                                 request.\r
 \r
 **/\r
@@ -2365,7 +2575,7 @@ EfiIpSecConfigGetNextSelector (
   NET_LIST_FOR_EACH (Link, &mConfigData[DataType]) {\r
     CommonEntry = BASE_CR (Link, IPSEC_COMMON_POLICY_ENTRY, List);\r
 \r
-    if (IsFound || mIsZeroSelector[DataType](Selector)) {\r
+    if (IsFound || (BOOLEAN)(mIsZeroSelector[DataType](Selector))) {\r
       //\r
       // If found the appointed entry, then duplicate the next one and return,\r
       // or if the appointed entry is zero, then return the first one directly.\r
@@ -2384,14 +2594,14 @@ EfiIpSecConfigGetNextSelector (
 \r
 /**\r
   Register an event that is to be signaled whenever a configuration process on the\r
-  specified IPsec configuration information is done.\r
+  specified IPsec configuration information is done. \r
 \r
   The register function is not surpport now and always returns EFI_UNSUPPORTED.\r
-\r
+  \r
   @param[in] This               Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
   @param[in] DataType           The type of data to be registered the event for.\r
   @param[in] Event              The event to be registered.\r
-\r
\r
   @retval EFI_SUCCESS           The event is registered successfully.\r
   @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
   @retval EFI_ACCESS_DENIED     The Event is already registered for the DataType.\r
@@ -2412,16 +2622,16 @@ EfiIpSecConfigRegisterNotify (
 \r
 /**\r
   Remove the specified event that was previously registered on the specified IPsec\r
-  configuration data.\r
+  configuration data. \r
 \r
   This function is not support now and alwasy return EFI_UNSUPPORTED.\r
 \r
   @param[in] This               Pointer to the EFI_IPSEC_CONFIG_PROTOCOL instance.\r
   @param[in] DataType           The configuration data type to remove the registered event for.\r
   @param[in] Event              The event to be unregistered.\r
-\r
\r
   @retval EFI_SUCCESS           The event was removed successfully.\r
-  @retval EFI_NOT_FOUND         The Event specified by DataType could not be found in the\r
+  @retval EFI_NOT_FOUND         The Event specified by DataType could not be found in the \r
                                 database.\r
   @retval EFI_INVALID_PARAMETER This is NULL or Event is NULL.\r
   @retval EFI_UNSUPPORTED       The notify registration is unsupported, or the specified\r
@@ -2443,10 +2653,10 @@ EfiIpSecConfigUnregisterNotify (
   Copy whole data in specified EFI_SIPEC_CONFIG_SELECTOR and the Data to a buffer.\r
 \r
   This function is a caller defined function, and it is called by the IpSecVisitConfigData().\r
-  The orignal caller is IpSecConfigSave(), which calls the IpsecVisitConfigData() to\r
+  The orignal caller is IpSecConfigSave(), which calls the IpsecVisitConfigData() to \r
   copy all types of IPsec Config datas into one buffer and store this buffer into firmware in\r
   the form of several variables.\r
-\r
+  \r
   @param[in]      Type              A specified IPSEC_CONFIG_DATA_TYPE.\r
   @param[in]      Selector          Points to a EFI_IPSEC_CONFIG_SELECTOR to be copied\r
                                     to the buffer.\r
@@ -2474,12 +2684,12 @@ IpSecCopyPolicyEntry (
   IPSEC_VAR_ITEM_HEADER DataHeader;\r
   UINTN                 EntrySize;\r
   UINT8                 *TempPoint;\r
-\r
+  \r
   if (Type == IPsecConfigDataTypeSad) {\r
     //\r
-    // Don't save automatically-generated sa entry into variable.\r
+    // Don't save automatically-generated SA entry into variable.\r
     //\r
-    if (((EFI_IPSEC_SA_DATA *) Data)->ManualSet == FALSE) {\r
+    if (((EFI_IPSEC_SA_DATA2 *) Data)->ManualSet == FALSE) {\r
       return EFI_SUCCESS;\r
     }\r
   }\r
@@ -2490,7 +2700,7 @@ IpSecCopyPolicyEntry (
   EntrySize  = ALIGN_VARIABLE (EntrySize + SelectorSize);\r
   EntrySize  = ALIGN_VARIABLE (EntrySize + sizeof (SelectorHeader));\r
   EntrySize  = ALIGN_VARIABLE (EntrySize + DataSize);\r
-\r
+  \r
   //EntrySize = SelectorSize + DataSize + 2 * sizeof (SelectorHeader);\r
   if (Buffer->Capacity - Buffer->Size < EntrySize) {\r
     //\r
@@ -2498,8 +2708,8 @@ IpSecCopyPolicyEntry (
     //\r
     Buffer->Capacity += EntrySize;\r
     TempPoint         = AllocatePool (Buffer->Capacity);\r
-\r
-    if (Buffer->Ptr == NULL) {\r
+    \r
+    if (TempPoint == NULL) {\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
     //\r
@@ -2507,8 +2717,8 @@ IpSecCopyPolicyEntry (
     //\r
     CopyMem (TempPoint, Buffer->Ptr, Buffer->Size);\r
     FreePool (Buffer->Ptr);\r
-\r
-    Buffer->Ptr       =  TempPoint;\r
+    \r
+    Buffer->Ptr       =  TempPoint;    \r
   }\r
 \r
   mFixPolicyEntry[Type](Selector, Data);\r
@@ -2525,7 +2735,7 @@ IpSecCopyPolicyEntry (
     sizeof (SelectorHeader)\r
     );\r
   Buffer->Size  = ALIGN_VARIABLE (Buffer->Size + sizeof (SelectorHeader));\r
-\r
+  \r
   //\r
   // Copy the selector into buffer.\r
   //\r
@@ -2557,7 +2767,7 @@ IpSecCopyPolicyEntry (
     DataSize\r
     );\r
   Buffer->Size  = ALIGN_VARIABLE (Buffer->Size + DataSize);\r
-\r
+  \r
   mUnfixPolicyEntry[Type](Selector, Data);\r
 \r
   return EFI_SUCCESS;\r
@@ -2768,7 +2978,7 @@ IpSecConfigSave (
   Get the all IPSec configuration variables and store those variables\r
   to the internal data structure.\r
 \r
-  This founction is called by IpSecConfigInitialize() which is to intialize the\r
+  This founction is called by IpSecConfigInitialize() which is to intialize the \r
   IPsecConfiguration Protocol.\r
 \r
   @param[in]  Private            Point to IPSEC_PRIVATE_DATA.\r
@@ -2846,10 +3056,10 @@ IpSecConfigRestore (
       Header  = (IPSEC_VAR_ITEM_HEADER *) Ptr;\r
       Type    = (EFI_IPSEC_CONFIG_DATA_TYPE) (Header->Type & IPSEC_VAR_ITEM_HEADER_CONTENT_BIT);\r
       ASSERT (((Header->Type & 0x80) == IPSEC_VAR_ITEM_HEADER_LOGO_BIT) && (Type < IPsecConfigDataTypeMaximum));\r
-\r
+      \r
       Selector  = (EFI_IPSEC_CONFIG_SELECTOR *) ALIGN_POINTER (Header + 1, sizeof (UINTN));\r
       Header    = (IPSEC_VAR_ITEM_HEADER *) ALIGN_POINTER (\r
-                                              (UINT8 *) Selector + Header->Size,\r
+                                              (UINT8 *) Selector + Header->Size, \r
                                               sizeof (UINTN)\r
                                               );\r
       ASSERT (Header->Type == Type);\r