]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/Udp6Dxe/Udp6Impl.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / NetworkPkg / Udp6Dxe / Udp6Impl.c
index 7ed50411c66b5bff32865117b3c6196598377e68..d0966ab06090a890551717a1e3a33b9eeb6ed7cc 100644 (file)
@@ -1,15 +1,9 @@
 /** @file\r
   Udp6 driver's whole implementation.\r
 \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
@@ -57,6 +51,9 @@ Udp6FindInstanceByPort (
   interface. It's called to signal the udp TxToken when the IpIo layer completes\r
   transmitting of the udp datagram.\r
 \r
+  If Context is NULL, then ASSERT().\r
+  If NotifyData is NULL, then ASSERT().\r
+\r
   @param[in]  Status            The completion status of the output udp datagram.\r
   @param[in]  Context           Pointer to the context data.\r
   @param[in]  Sender            Specify a EFI_IP6_PROTOCOL for sending.\r
@@ -66,15 +63,19 @@ Udp6FindInstanceByPort (
 VOID\r
 EFIAPI\r
 Udp6DgramSent (\r
-  IN EFI_STATUS        Status,\r
-  IN VOID              *Context,\r
-  IN IP_IO_IP_PROTOCOL Sender,\r
-  IN VOID              *NotifyData\r
+  IN EFI_STATUS         Status,\r
+  IN VOID               *Context,\r
+  IN IP_IO_IP_PROTOCOL  Sender,\r
+  IN VOID               *NotifyData\r
   );\r
 \r
 /**\r
   This function processes the received datagram passed up by the IpIo layer.\r
 \r
+  If NetSession is NULL, then ASSERT().\r
+  If Packet is NULL, then ASSERT().\r
+  If Context is NULL, then ASSERT().\r
+\r
   @param[in]  Status            The status of this udp datagram.\r
   @param[in]  IcmpError         The IcmpError code, only available when Status is\r
                                 EFI_ICMP_ERROR.\r
@@ -95,7 +96,7 @@ Udp6DgramRcvd (
   );\r
 \r
 /**\r
-  This function cancle the token specified by Arg in the Map.\r
+  This function cancel the token specified by Arg in the Map.\r
 \r
   @param[in]  Map             Pointer to the NET_MAP.\r
   @param[in]  Item            Pointer to the NET_MAP_ITEM.\r
@@ -158,7 +159,8 @@ Udp6RecycleRxDataWrap (
   @param[in]  RxData             Pointer to the EFI_UDP6_RECEIVE_DATA of this\r
                                  datagram.\r
 \r
-  @return Pointer to the structure wrapping the RxData and the Packet.\r
+  @return Pointer to the structure wrapping the RxData and the Packet. NULL will\r
+          be returned if any error occurs.\r
 \r
 **/\r
 UDP6_RXDATA_WRAP *\r
@@ -199,10 +201,10 @@ Udp6DeliverDgram (
   );\r
 \r
 /**\r
-  This function demultiplexes the received udp datagram to the apropriate instances.\r
+  This function demultiplexes the received udp datagram to the appropriate instances.\r
 \r
   @param[in]  Udp6Service        Pointer to the udp service context data.\r
-  @param[in]  NetSession         Pointer to the EFI_NET_SESSION_DATA abstrated from\r
+  @param[in]  NetSession         Pointer to the EFI_NET_SESSION_DATA abstracted from\r
                                  the received datagram.\r
   @param[in]  Packet             Pointer to the buffer containing the received udp\r
                                  datagram.\r
@@ -262,8 +264,8 @@ Udp6SendPortUnreach (
 **/\r
 NET_MAP_ITEM *\r
 Udp6MapMultiCastAddr (\r
-  IN  NET_MAP               *Map,\r
-  IN  VOID                  *Key\r
+  IN  NET_MAP  *Map,\r
+  IN  VOID     *Key\r
   );\r
 \r
 /**\r
@@ -315,10 +317,10 @@ Udp6CreateService (
     &mIp6IoDefaultIpConfigData,\r
     sizeof (EFI_IP6_CONFIG_DATA)\r
     );\r
-  OpenData.RcvdContext           = (VOID *) Udp6Service;\r
-  OpenData.SndContext            = NULL;\r
-  OpenData.PktRcvdNotify         = Udp6DgramRcvd;\r
-  OpenData.PktSentNotify         = Udp6DgramSent;\r
+  OpenData.RcvdContext   = (VOID *)Udp6Service;\r
+  OpenData.SndContext    = NULL;\r
+  OpenData.PktRcvdNotify = Udp6DgramRcvd;\r
+  OpenData.PktSentNotify = Udp6DgramSent;\r
 \r
   //\r
   // Configure and start the IpIo.\r
@@ -364,11 +366,10 @@ ON_ERROR:
 \r
   IpIoDestroy (Udp6Service->IpIo);\r
   Udp6Service->IpIo = NULL;\r
-  \r
+\r
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Clean the Udp service context data.\r
 \r
@@ -390,11 +391,10 @@ Udp6CleanService (
   //\r
   IpIoDestroy (Udp6Service->IpIo);\r
   Udp6Service->IpIo = NULL;\r
-  \r
+\r
   ZeroMem (Udp6Service, sizeof (UDP6_SERVICE_DATA));\r
 }\r
 \r
-\r
 /**\r
   This function checks and times out the I/O datagrams listed in the\r
   UDP6_SERVICE_DATA which is specified by the input parameter Context.\r
@@ -419,7 +419,7 @@ Udp6CheckTimeout (
   LIST_ENTRY          *NextEntry;\r
   UDP6_RXDATA_WRAP    *Wrap;\r
 \r
-  Udp6Service = (UDP6_SERVICE_DATA *) Context;\r
+  Udp6Service = (UDP6_SERVICE_DATA *)Context;\r
   NET_CHECK_SIGNATURE (Udp6Service, UDP6_SERVICE_DATA_SIGNATURE);\r
 \r
   NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) {\r
@@ -446,7 +446,7 @@ Udp6CheckTimeout (
         //\r
         // Remove this RxData if it timeouts.\r
         //\r
-        Udp6RecycleRxDataWrap (NULL, (VOID *) Wrap);\r
+        Udp6RecycleRxDataWrap (NULL, (VOID *)Wrap);\r
       } else {\r
         Wrap->TimeoutTick -= UDP6_TIMEOUT_INTERVAL / 10;\r
       }\r
@@ -454,9 +454,8 @@ Udp6CheckTimeout (
   }\r
 }\r
 \r
-\r
 /**\r
-  This function intializes the new created udp instance.\r
+  This function initializes the new created udp instance.\r
 \r
   @param[in]       Udp6Service      Pointer to the UDP6_SERVICE_DATA.\r
   @param[in, out]  Instance         Pointer to the un-initialized UDP6_INSTANCE_DATA.\r
@@ -498,7 +497,6 @@ Udp6InitInstance (
   Instance->InDestroy   = FALSE;\r
 }\r
 \r
-\r
 /**\r
   This function cleans the udp instance.\r
 \r
@@ -515,7 +513,6 @@ Udp6CleanInstance (
   NetMapClean (&Instance->TxTokens);\r
 }\r
 \r
-\r
 /**\r
   This function finds the udp instance by the specified <Address, Port> pair.\r
 \r
@@ -556,7 +553,8 @@ Udp6FindInstanceByPort (
 \r
     if (EFI_IP6_EQUAL (&ConfigData->StationAddress, Address) &&\r
         (ConfigData->StationPort == Port)\r
-        ) {\r
+        )\r
+    {\r
       //\r
       // If both the address and the port are the same, return TRUE.\r
       //\r
@@ -570,10 +568,9 @@ Udp6FindInstanceByPort (
   return FALSE;\r
 }\r
 \r
-\r
 /**\r
   This function tries to bind the udp instance according to the configured port\r
-  allocation stragety.\r
+  allocation strategy.\r
 \r
   @param[in]  InstanceList       Pointer to the head of the list linking the udp\r
                                  instances.\r
@@ -602,10 +599,10 @@ Udp6Bind (
   StationAddress = &ConfigData->StationAddress;\r
 \r
   if (ConfigData->StationPort != 0) {\r
-\r
     if (!ConfigData->AllowDuplicatePort &&\r
         Udp6FindInstanceByPort (InstanceList, StationAddress, ConfigData->StationPort)\r
-        ) {\r
+        )\r
+    {\r
       //\r
       // Do not allow duplicate ports and the port is already used by other instance.\r
       //\r
@@ -621,11 +618,9 @@ Udp6Bind (
       //\r
       ConfigData->StationPort = mUdp6RandomPort;\r
     } else {\r
-\r
       StartPort = mUdp6RandomPort;\r
 \r
       while (Udp6FindInstanceByPort (InstanceList, StationAddress, mUdp6RandomPort)) {\r
-\r
         mUdp6RandomPort++;\r
         if (mUdp6RandomPort == 0) {\r
           mUdp6RandomPort = UDP6_PORT_KNOWN;\r
@@ -647,10 +642,10 @@ Udp6Bind (
       mUdp6RandomPort = UDP6_PORT_KNOWN;\r
     }\r
   }\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   This function is used to check whether the NewConfigData has any un-reconfigurable\r
   parameters changed compared to the OldConfigData.\r
@@ -672,7 +667,8 @@ Udp6IsReconfigurable (
   if ((NewConfigData->AcceptAnyPort != OldConfigData->AcceptAnyPort) ||\r
       (NewConfigData->AcceptPromiscuous != OldConfigData->AcceptPromiscuous) ||\r
       (NewConfigData->AllowDuplicatePort != OldConfigData->AllowDuplicatePort)\r
-      ) {\r
+      )\r
+  {\r
     //\r
     // The receiving filter parameters cannot be changed.\r
     //\r
@@ -681,7 +677,8 @@ Udp6IsReconfigurable (
 \r
   if ((!NewConfigData->AcceptAnyPort) &&\r
       (NewConfigData->StationPort != OldConfigData->StationPort)\r
-      ) {\r
+      )\r
+  {\r
     //\r
     // The port is not changeable.\r
     //\r
@@ -695,7 +692,6 @@ Udp6IsReconfigurable (
     return FALSE;\r
   }\r
 \r
-\r
   if (!EFI_IP6_EQUAL (&NewConfigData->RemoteAddress, &OldConfigData->RemoteAddress)) {\r
     //\r
     // The remoteaddress is not the same.\r
@@ -705,7 +701,8 @@ Udp6IsReconfigurable (
 \r
   if (!NetIp6IsUnspecifiedAddr (&NewConfigData->RemoteAddress) &&\r
       (NewConfigData->RemotePort != OldConfigData->RemotePort)\r
-      ) {\r
+      )\r
+  {\r
     //\r
     // The RemotePort differs if it's designated in the configdata.\r
     //\r
@@ -718,7 +715,6 @@ Udp6IsReconfigurable (
   return TRUE;\r
 }\r
 \r
-\r
 /**\r
   This function builds the Ip6 configdata from the Udp6ConfigData.\r
 \r
@@ -728,8 +724,8 @@ Udp6IsReconfigurable (
 **/\r
 VOID\r
 Udp6BuildIp6ConfigData (\r
-  IN EFI_UDP6_CONFIG_DATA      *Udp6ConfigData,\r
-  IN OUT EFI_IP6_CONFIG_DATA   *Ip6ConfigData\r
+  IN EFI_UDP6_CONFIG_DATA     *Udp6ConfigData,\r
+  IN OUT EFI_IP6_CONFIG_DATA  *Ip6ConfigData\r
   )\r
 {\r
   CopyMem (\r
@@ -737,17 +733,16 @@ Udp6BuildIp6ConfigData (
     &mIp6IoDefaultIpConfigData,\r
     sizeof (EFI_IP6_CONFIG_DATA)\r
     );\r
-  Ip6ConfigData->DefaultProtocol      = EFI_IP_PROTO_UDP;\r
-  Ip6ConfigData->AcceptPromiscuous    = Udp6ConfigData->AcceptPromiscuous;\r
+  Ip6ConfigData->DefaultProtocol   = EFI_IP_PROTO_UDP;\r
+  Ip6ConfigData->AcceptPromiscuous = Udp6ConfigData->AcceptPromiscuous;\r
   IP6_COPY_ADDRESS (&Ip6ConfigData->StationAddress, &Udp6ConfigData->StationAddress);\r
   IP6_COPY_ADDRESS (&Ip6ConfigData->DestinationAddress, &Udp6ConfigData->RemoteAddress);\r
   //\r
   // Use the -1 magic number to disable the receiving process of the ip instance.\r
   //\r
-  Ip6ConfigData->ReceiveTimeout    = (UINT32) (-1);\r
+  Ip6ConfigData->ReceiveTimeout = (UINT32)(-1);\r
 }\r
 \r
-\r
 /**\r
   This function validates the TxToken. It returns the error code according to the spec.\r
 \r
@@ -789,7 +784,6 @@ Udp6ValidateTxToken (
   EFI_UDP6_CONFIG_DATA    *ConfigData;\r
   EFI_UDP6_SESSION_DATA   *UdpSessionData;\r
 \r
-\r
   if (TxToken->Event == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -802,10 +796,10 @@ Udp6ValidateTxToken (
 \r
   TotalLen = 0;\r
   for (Index = 0; Index < TxData->FragmentCount; Index++) {\r
-\r
     if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||\r
         (TxData->FragmentTable[Index].FragmentLength == 0)\r
-        ) {\r
+        )\r
+    {\r
       //\r
       // If the FragmentBuffer is NULL, or the FragmentLeng is zero.\r
       //\r
@@ -827,26 +821,27 @@ Udp6ValidateTxToken (
   UdpSessionData = TxData->UdpSessionData;\r
 \r
   if (UdpSessionData != NULL) {\r
-\r
     if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) {\r
       //\r
-      // Ambiguous; no avalaible DestinationPort for this token.\r
+      // Ambiguous; no available DestinationPort for this token.\r
       //\r
       return EFI_INVALID_PARAMETER;\r
     }\r
 \r
     if (NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress) &&\r
         NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress)\r
-        ) {\r
+        )\r
+    {\r
       //\r
-      // The DestinationAddress is not specificed.\r
+      // The DestinationAddress is not specified.\r
       //\r
       return EFI_INVALID_PARAMETER;\r
     }\r
 \r
     if (!NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress) &&\r
         !NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress)\r
-        ) {\r
+        )\r
+    {\r
       //\r
       // The ConfigData.RemoteAddress is not zero and the UdpSessionData.DestinationAddress\r
       // is not zero too.\r
@@ -868,7 +863,6 @@ Udp6ValidateTxToken (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   This function checks whether the specified Token duplicates the one in the Map.\r
 \r
@@ -893,8 +887,8 @@ Udp6TokenExist (
   EFI_UDP6_COMPLETION_TOKEN  *Token;\r
   EFI_UDP6_COMPLETION_TOKEN  *TokenInItem;\r
 \r
-  Token       = (EFI_UDP6_COMPLETION_TOKEN *) Context;\r
-  TokenInItem = (EFI_UDP6_COMPLETION_TOKEN *) Item->Key;\r
+  Token       = (EFI_UDP6_COMPLETION_TOKEN *)Context;\r
+  TokenInItem = (EFI_UDP6_COMPLETION_TOKEN *)Item->Key;\r
 \r
   if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {\r
     //\r
@@ -907,13 +901,12 @@ Udp6TokenExist (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   This function calculates the checksum for the Packet, utilizing the pre-calculated\r
   pseudo HeadSum to reduce some overhead.\r
 \r
   @param[in]  Packet           Pointer to the NET_BUF contains the udp datagram.\r
-  @param[in]  HeadSum          Checksum of the pseudo header, execpt the length\r
+  @param[in]  HeadSum          Checksum of the pseudo header, except the length\r
                                field.\r
 \r
   @return The 16-bit checksum of this udp datagram.\r
@@ -921,21 +914,20 @@ Udp6TokenExist (
 **/\r
 UINT16\r
 Udp6Checksum (\r
-  IN NET_BUF *Packet,\r
-  IN UINT16  HeadSum\r
+  IN NET_BUF  *Packet,\r
+  IN UINT16   HeadSum\r
   )\r
 {\r
   UINT16  Checksum;\r
 \r
-  Checksum  = NetbufChecksum (Packet);\r
-  Checksum  = NetAddChecksum (Checksum, HeadSum);\r
+  Checksum = NetbufChecksum (Packet);\r
+  Checksum = NetAddChecksum (Checksum, HeadSum);\r
 \r
-  Checksum  = NetAddChecksum (Checksum, HTONS ((UINT16) Packet->TotalSize));\r
-  Checksum  = (UINT16) (~Checksum);\r
+  Checksum = NetAddChecksum (Checksum, HTONS ((UINT16)Packet->TotalSize));\r
+  Checksum = (UINT16)(~Checksum);\r
   return Checksum;\r
 }\r
 \r
-\r
 /**\r
   This function removes the specified Token from the TokenMap.\r
 \r
@@ -957,7 +949,7 @@ Udp6RemoveToken (
   //\r
   // Find the Token first.\r
   //\r
-  Item = NetMapFindKey (TokenMap, (VOID *) Token);\r
+  Item = NetMapFindKey (TokenMap, (VOID *)Token);\r
 \r
   if (Item != NULL) {\r
     //\r
@@ -967,15 +959,18 @@ Udp6RemoveToken (
 \r
     return EFI_SUCCESS;\r
   }\r
+\r
   return EFI_NOT_FOUND;\r
 }\r
 \r
-\r
 /**\r
   This function is the packet transmitting notify function registered to the IpIo\r
   interface. It's called to signal the udp TxToken when IpIo layer completes the\r
   transmitting of the udp datagram.\r
 \r
+  If Context is NULL, then ASSERT().\r
+  If NotifyData is NULL, then ASSERT().\r
+\r
   @param[in]  Status            The completion status of the output udp datagram.\r
   @param[in]  Context           Pointer to the context data.\r
   @param[in]  Sender            Specify a EFI_IP6_PROTOCOL for sending.\r
@@ -985,17 +980,19 @@ Udp6RemoveToken (
 VOID\r
 EFIAPI\r
 Udp6DgramSent (\r
-  IN EFI_STATUS        Status,\r
-  IN VOID              *Context,\r
-  IN IP_IO_IP_PROTOCOL Sender,\r
-  IN VOID              *NotifyData\r
+  IN EFI_STATUS         Status,\r
+  IN VOID               *Context,\r
+  IN IP_IO_IP_PROTOCOL  Sender,\r
+  IN VOID               *NotifyData\r
   )\r
 {\r
   UDP6_INSTANCE_DATA         *Instance;\r
   EFI_UDP6_COMPLETION_TOKEN  *Token;\r
 \r
-  Instance = (UDP6_INSTANCE_DATA *) Context;\r
-  Token    = (EFI_UDP6_COMPLETION_TOKEN *) NotifyData;\r
+  ASSERT (Context != NULL && NotifyData != NULL);\r
+\r
+  Instance = (UDP6_INSTANCE_DATA *)Context;\r
+  Token    = (EFI_UDP6_COMPLETION_TOKEN *)NotifyData;\r
 \r
   if (Udp6RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) {\r
     //\r
@@ -1007,10 +1004,13 @@ Udp6DgramSent (
   }\r
 }\r
 \r
-\r
 /**\r
   This function processes the received datagram passed up by the IpIo layer.\r
 \r
+  If NetSession is NULL, then ASSERT().\r
+  If Packet is NULL, then ASSERT().\r
+  If Context is NULL, then ASSERT().\r
+\r
   @param[in]  Status            The status of this udp datagram.\r
   @param[in]  IcmpError         The IcmpError code, only available when Status is\r
                                 EFI_ICMP_ERROR.\r
@@ -1030,22 +1030,22 @@ Udp6DgramRcvd (
   IN VOID                  *Context\r
   )\r
 {\r
+  ASSERT (NetSession != NULL && Packet != NULL && Context != NULL);\r
   NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE);\r
 \r
   //\r
   // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.\r
   //\r
   if (Status == EFI_SUCCESS) {\r
-\r
     //\r
     // Demultiplex the received datagram.\r
     //\r
-    Udp6Demultiplex ((UDP6_SERVICE_DATA *) Context, NetSession, Packet);\r
+    Udp6Demultiplex ((UDP6_SERVICE_DATA *)Context, NetSession, Packet);\r
   } else {\r
     //\r
     // Handle the ICMP6 Error packet.\r
     //\r
-    Udp6IcmpHandler ((UDP6_SERVICE_DATA *) Context, IcmpError, NetSession, Packet);\r
+    Udp6IcmpHandler ((UDP6_SERVICE_DATA *)Context, IcmpError, NetSession, Packet);\r
   }\r
 \r
   //\r
@@ -1055,7 +1055,6 @@ Udp6DgramRcvd (
   DispatchDpc ();\r
 }\r
 \r
-\r
 /**\r
   This function removes the multicast group specified by Arg from the Map.\r
 \r
@@ -1084,7 +1083,8 @@ Udp6LeaveGroup (
 \r
   if ((McastIp != NULL) &&\r
       !EFI_IP6_EQUAL (McastIp, ((EFI_IPv6_ADDRESS *)Item->Key))\r
-      ) {\r
+      )\r
+  {\r
     //\r
     // McastIp is not NULL and the multicast address contained in the Item\r
     // is not the same as McastIp.\r
@@ -1109,9 +1109,8 @@ Udp6LeaveGroup (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
-  This function cancle the token specified by Arg in the Map.\r
+  This function cancel the token specified by Arg in the Map.\r
 \r
   @param[in]  Map             Pointer to the NET_MAP.\r
   @param[in]  Item            Pointer to the NET_MAP_ITEM.\r
@@ -1149,15 +1148,15 @@ Udp6CancelTokens (
     // will invoke Udp6DgramSent, the token will be signaled and this Item will\r
     // be removed from the Map there.\r
     //\r
-    Packet  = (NET_BUF *) (Item->Value);\r
-    IpIo    = (IP_IO *) (*((UINTN *) &Packet->ProtoData[0]));\r
+    Packet = (NET_BUF *)(Item->Value);\r
+    IpIo   = (IP_IO *)(*((UINTN *)&Packet->ProtoData[0]));\r
 \r
     IpIoCancelTxToken (IpIo, Packet);\r
   } else {\r
     //\r
     // The token is a receive token. Abort it and remove it from the Map.\r
     //\r
-    TokenToCancel = (EFI_UDP6_COMPLETION_TOKEN *) Item->Key;\r
+    TokenToCancel = (EFI_UDP6_COMPLETION_TOKEN *)Item->Key;\r
     NetMapRemoveItem (Map, Item, NULL);\r
 \r
     TokenToCancel->Status = EFI_ABORTED;\r
@@ -1171,7 +1170,6 @@ Udp6CancelTokens (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   This function removes all the Wrap datas in the RcvdDgramQue.\r
 \r
@@ -1194,12 +1192,10 @@ Udp6FlushRcvdDgram (
     //\r
     // The Wrap will be removed from the RcvdDgramQue by this function call.\r
     //\r
-    Udp6RecycleRxDataWrap (NULL, (VOID *) Wrap);\r
+    Udp6RecycleRxDataWrap (NULL, (VOID *)Wrap);\r
   }\r
 }\r
 \r
-\r
-\r
 /**\r
   Cancel Udp6 tokens from the Udp6 instance.\r
 \r
@@ -1247,15 +1243,15 @@ Udp6InstanceCancelToken (
     return EFI_NOT_FOUND;\r
   }\r
 \r
-  ASSERT ((Token != NULL) ||\r
-          ((0 == NetMapGetCount (&Instance->TxTokens)) &&\r
-          (0 == NetMapGetCount (&Instance->RxTokens)))\r
-          );\r
+  ASSERT (\r
+    (Token != NULL) ||\r
+    ((0 == NetMapGetCount (&Instance->TxTokens)) &&\r
+     (0 == NetMapGetCount (&Instance->RxTokens)))\r
+    );\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   This function checks if the received udp datagram matches with the Instance.\r
 \r
@@ -1264,7 +1260,7 @@ Udp6InstanceCancelToken (
                                  from the received udp datagram.\r
 \r
   @retval TRUE     The udp datagram matches the receiving requirements of the Instance.\r
-  @retval FALSE    The udp datagram does not matche the receiving requirements of the Instance.\r
+  @retval FALSE    The udp datagram does not match the receiving requirements of the Instance.\r
 \r
 **/\r
 BOOLEAN\r
@@ -1287,7 +1283,8 @@ Udp6MatchDgram (
 \r
   if ((!ConfigData->AcceptAnyPort && (Udp6Session->DestinationPort != ConfigData->StationPort)) ||\r
       ((ConfigData->RemotePort != 0) && (Udp6Session->SourcePort != ConfigData->RemotePort))\r
-      ) {\r
+      )\r
+  {\r
     //\r
     // The local port or the remote port doesn't match.\r
     //\r
@@ -1296,7 +1293,8 @@ Udp6MatchDgram (
 \r
   if (!NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress) &&\r
       !EFI_IP6_EQUAL (&ConfigData->RemoteAddress, &Udp6Session->SourceAddress)\r
-      ) {\r
+      )\r
+  {\r
     //\r
     // This datagram doesn't come from the instance's specified sender.\r
     //\r
@@ -1305,7 +1303,8 @@ Udp6MatchDgram (
 \r
   if (NetIp6IsUnspecifiedAddr (&ConfigData->StationAddress) ||\r
       EFI_IP6_EQUAL (&Udp6Session->DestinationAddress, &ConfigData->StationAddress)\r
-      ) {\r
+      )\r
+  {\r
     //\r
     // The instance is configured to receive datagrams destinated to any station IP or\r
     // the destination address of this datagram matches the configured station IP.\r
@@ -1317,7 +1316,8 @@ Udp6MatchDgram (
 \r
   if (IP6_IS_MULTICAST (&Destination) &&\r
       (NULL != Udp6MapMultiCastAddr (&Instance->McastIps, &Destination))\r
-      ) {\r
+      )\r
+  {\r
     //\r
     // It's a multicast packet and the multicast address is accepted by this instance.\r
     //\r
@@ -1327,7 +1327,6 @@ Udp6MatchDgram (
   return FALSE;\r
 }\r
 \r
-\r
 /**\r
   This function removes the Wrap specified by Context and release relevant resources.\r
 \r
@@ -1344,7 +1343,7 @@ Udp6RecycleRxDataWrap (
 {\r
   UDP6_RXDATA_WRAP  *Wrap;\r
 \r
-  Wrap = (UDP6_RXDATA_WRAP *) Context;\r
+  Wrap = (UDP6_RXDATA_WRAP *)Context;\r
 \r
   //\r
   // Remove the Wrap from the list it belongs to.\r
@@ -1364,7 +1363,6 @@ Udp6RecycleRxDataWrap (
   FreePool (Wrap);\r
 }\r
 \r
-\r
 /**\r
   This function wraps the Packet into RxData.\r
 \r
@@ -1374,7 +1372,8 @@ Udp6RecycleRxDataWrap (
   @param[in]  RxData             Pointer to the EFI_UDP6_RECEIVE_DATA of this\r
                                  datagram.\r
 \r
-  @return Pointer to the structure wrapping the RxData and the Packet.\r
+  @return Pointer to the structure wrapping the RxData and the Packet. NULL will\r
+          be returned if any error occurs.\r
 \r
 **/\r
 UDP6_RXDATA_WRAP *\r
@@ -1384,21 +1383,23 @@ Udp6WrapRxData (
   IN EFI_UDP6_RECEIVE_DATA  *RxData\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  UDP6_RXDATA_WRAP      *Wrap;\r
+  EFI_STATUS        Status;\r
+  UDP6_RXDATA_WRAP  *Wrap;\r
 \r
   //\r
   // Allocate buffer for the Wrap.\r
   //\r
-  Wrap = AllocateZeroPool (sizeof (UDP6_RXDATA_WRAP) +\r
-         (Packet->BlockOpNum - 1) * sizeof (EFI_UDP6_FRAGMENT_DATA));\r
+  Wrap = AllocateZeroPool (\r
+           sizeof (UDP6_RXDATA_WRAP) +\r
+           (Packet->BlockOpNum - 1) * sizeof (EFI_UDP6_FRAGMENT_DATA)\r
+           );\r
   if (Wrap == NULL) {\r
     return NULL;\r
   }\r
 \r
   InitializeListHead (&Wrap->Link);\r
 \r
-  CopyMem (&Wrap->RxData, RxData, sizeof(EFI_UDP6_RECEIVE_DATA));\r
+  CopyMem (&Wrap->RxData, RxData, sizeof (EFI_UDP6_RECEIVE_DATA));\r
   //\r
   // Create the Recycle event.\r
   //\r
@@ -1420,7 +1421,6 @@ Udp6WrapRxData (
   return Wrap;\r
 }\r
 \r
-\r
 /**\r
   This function enqueues the received datagram into the instances' receiving queues.\r
 \r
@@ -1477,7 +1477,6 @@ Udp6EnqueueDgram (
   return Enqueued;\r
 }\r
 \r
-\r
 /**\r
   This function delivers the received datagrams to the specified instance.\r
 \r
@@ -1497,8 +1496,8 @@ Udp6InstanceDeliverDgram (
 \r
   if (!IsListEmpty (&Instance->RcvdDgramQue) &&\r
       !NetMapIsEmpty (&Instance->RxTokens)\r
-      ) {\r
-\r
+      )\r
+  {\r
     Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP6_RXDATA_WRAP, Link);\r
 \r
     if (NET_BUF_SHARED (Wrap->Packet)) {\r
@@ -1517,7 +1516,7 @@ Udp6InstanceDeliverDgram (
 \r
     NetListRemoveHead (&Instance->RcvdDgramQue);\r
 \r
-    Token = (EFI_UDP6_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);\r
+    Token = (EFI_UDP6_COMPLETION_TOKEN *)NetMapRemoveHead (&Instance->RxTokens, NULL);\r
 \r
     //\r
     // Build the FragmentTable and set the FragmentCount in RxData.\r
@@ -1527,7 +1526,7 @@ Udp6InstanceDeliverDgram (
 \r
     NetbufBuildExt (\r
       Wrap->Packet,\r
-      (NET_FRAGMENT *) RxData->FragmentTable,\r
+      (NET_FRAGMENT *)RxData->FragmentTable,\r
       &RxData->FragmentCount\r
       );\r
 \r
@@ -1542,7 +1541,6 @@ Udp6InstanceDeliverDgram (
   }\r
 }\r
 \r
-\r
 /**\r
   This function delivers the datagrams enqueued in the instances.\r
 \r
@@ -1574,12 +1572,11 @@ Udp6DeliverDgram (
   }\r
 }\r
 \r
-\r
 /**\r
   This function demultiplexes the received udp datagram to the appropriate instances.\r
 \r
   @param[in]  Udp6Service        Pointer to the udp service context data.\r
-  @param[in]  NetSession         Pointer to the EFI_NET_SESSION_DATA abstrated from\r
+  @param[in]  NetSession         Pointer to the EFI_NET_SESSION_DATA abstracted from\r
                                  the received datagram.\r
   @param[in]  Packet             Pointer to the buffer containing the received udp\r
                                  datagram.\r
@@ -1592,22 +1589,26 @@ Udp6Demultiplex (
   IN NET_BUF               *Packet\r
   )\r
 {\r
-  EFI_UDP_HEADER        *Udp6Header;\r
+  EFI_UDP_HEADER         *Udp6Header;\r
   UINT16                 HeadSum;\r
   EFI_UDP6_RECEIVE_DATA  RxData;\r
   EFI_UDP6_SESSION_DATA  *Udp6Session;\r
   UINTN                  Enqueued;\r
 \r
-  if (Packet->TotalSize < sizeof (EFI_UDP_HEADER)) {\r
+  if (Packet->TotalSize < UDP6_HEADER_SIZE) {\r
     NetbufFree (Packet);\r
     return;\r
   }\r
-  \r
+\r
   //\r
   // Get the datagram header from the packet buffer.\r
   //\r
-  Udp6Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL);\r
+  Udp6Header = (EFI_UDP_HEADER *)NetbufGetByte (Packet, 0, NULL);\r
   ASSERT (Udp6Header != NULL);\r
+  if (Udp6Header == NULL) {\r
+    NetbufFree (Packet);\r
+    return;\r
+  }\r
 \r
   if (Udp6Header->Checksum != 0) {\r
     //\r
@@ -1641,7 +1642,7 @@ Udp6Demultiplex (
   //\r
   NetbufTrim (Packet, UDP6_HEADER_SIZE, TRUE);\r
 \r
-  RxData.DataLength = (UINT32) Packet->TotalSize;\r
+  RxData.DataLength = (UINT32)Packet->TotalSize;\r
 \r
   //\r
   // Try to enqueue this datagram into the instances.\r
@@ -1668,7 +1669,6 @@ Udp6Demultiplex (
   }\r
 }\r
 \r
-\r
 /**\r
   This function builds and sends out a icmp port unreachable message.\r
 \r
@@ -1704,20 +1704,23 @@ Udp6SendPortUnreach (
   //\r
   if (NetSession->IpVersion == IP_VERSION_6) {\r
     if (NetIp6IsUnspecifiedAddr (&NetSession->Dest.v6) ||\r
-      IP6_IS_MULTICAST (&NetSession->Dest.v6)\r
-      ) {\r
+        IP6_IS_MULTICAST (&NetSession->Dest.v6)\r
+        )\r
+    {\r
       goto EXIT;\r
     }\r
   }\r
 \r
-\r
-  IpSender    = IpIoFindSender (&IpIo, NetSession->IpVersion, &NetSession->Dest);\r
+  IpSender = IpIoFindSender (&IpIo, NetSession->IpVersion, &NetSession->Dest);\r
 \r
   //\r
   // Get the Ipv6 Mode Data.\r
   //\r
   Ip6ModeData = AllocateZeroPool (sizeof (EFI_IP6_MODE_DATA));\r
   ASSERT (Ip6ModeData != NULL);\r
+  if (Ip6ModeData == NULL) {\r
+    goto EXIT;\r
+  }\r
 \r
   //\r
   // If not finding the related IpSender use the default IpIo to send out\r
@@ -1739,11 +1742,12 @@ Udp6SendPortUnreach (
   if (EFI_ERROR (Status)) {\r
     goto EXIT;\r
   }\r
+\r
   //\r
   // The ICMP6 packet length, includes whole invoking packet and ICMP6 error header.\r
   //\r
   Len = NetSession->IpHdrLen +\r
-        NTOHS(((EFI_UDP_HEADER *) Udp6Header)->Length) +\r
+        NTOHS (((EFI_UDP_HEADER *)Udp6Header)->Length) +\r
         sizeof (IP6_ICMP_ERROR_HEAD);\r
 \r
   //\r
@@ -1764,8 +1768,11 @@ Udp6SendPortUnreach (
   //\r
   // Allocate space for the IP6_ICMP_ERROR_HEAD.\r
   //\r
-  IcmpErrHdr = (IP6_ICMP_ERROR_HEAD *) NetbufAllocSpace (Packet, Len, FALSE);\r
+  IcmpErrHdr = (IP6_ICMP_ERROR_HEAD *)NetbufAllocSpace (Packet, Len, FALSE);\r
   ASSERT (IcmpErrHdr != NULL);\r
+  if (IcmpErrHdr == NULL) {\r
+    goto EXIT;\r
+  }\r
 \r
   //\r
   // Set the required fields for the icmp port unreachable message.\r
@@ -1781,8 +1788,8 @@ Udp6SendPortUnreach (
   // the length of EFI_IP6_HEADER, so when using the length of IP6_ICMP_ERROR_HEAD\r
   // for pointer movement that fact should be considered.\r
   //\r
-  Ptr = (VOID *) &IcmpErrHdr->Head;\r
-  Ptr = (UINT8 *) (UINTN) ((UINTN) Ptr + sizeof (IP6_ICMP_ERROR_HEAD) - sizeof (EFI_IP6_HEADER));\r
+  Ptr = (VOID *)&IcmpErrHdr->Head;\r
+  Ptr = (UINT8 *)(UINTN)((UINTN)Ptr + sizeof (IP6_ICMP_ERROR_HEAD) - sizeof (EFI_IP6_HEADER));\r
   CopyMem (Ptr, NetSession->IpHdr.Ip6Hdr, NetSession->IpHdrLen);\r
   CopyMem (\r
     Ptr + NetSession->IpHdrLen,\r
@@ -1791,16 +1798,16 @@ Udp6SendPortUnreach (
     );\r
 \r
   //\r
-  // Set the checksum as zero, and IP6 driver will calcuate it with pseudo header.\r
+  // Set the checksum as zero, and IP6 driver will calculate it with pseudo header.\r
   //\r
   IcmpErrHdr->Head.Checksum = 0;\r
 \r
   //\r
   // Fill the override data.\r
   //\r
-  Override.Ip6OverrideData.FlowLabel     = 0;\r
-  Override.Ip6OverrideData.HopLimit      = 255;\r
-  Override.Ip6OverrideData.Protocol      = IP6_ICMP;\r
+  Override.Ip6OverrideData.FlowLabel = 0;\r
+  Override.Ip6OverrideData.HopLimit  = 255;\r
+  Override.Ip6OverrideData.Protocol  = IP6_ICMP;\r
 \r
   //\r
   // Send out this icmp packet.\r
@@ -1815,7 +1822,6 @@ EXIT:
   }\r
 }\r
 \r
-\r
 /**\r
   This function handles the received Icmp Error message and de-multiplexes it to the\r
   instance.\r
@@ -1840,13 +1846,17 @@ Udp6IcmpHandler (
   LIST_ENTRY             *Entry;\r
   UDP6_INSTANCE_DATA     *Instance;\r
 \r
-  if (Packet->TotalSize < sizeof (EFI_UDP_HEADER)) {\r
+  if (Packet->TotalSize < UDP6_HEADER_SIZE) {\r
     NetbufFree (Packet);\r
     return;\r
   }\r
-  \r
-  Udp6Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL);\r
+\r
+  Udp6Header = (EFI_UDP_HEADER *)NetbufGetByte (Packet, 0, NULL);\r
   ASSERT (Udp6Header != NULL);\r
+  if (Udp6Header == NULL) {\r
+    NetbufFree (Packet);\r
+    return;\r
+  }\r
 \r
   IP6_COPY_ADDRESS (&Udp6Session.SourceAddress, &NetSession->Source);\r
   IP6_COPY_ADDRESS (&Udp6Session.DestinationAddress, &NetSession->Dest);\r
@@ -1886,7 +1896,6 @@ Udp6IcmpHandler (
   NetbufFree (Packet);\r
 }\r
 \r
-\r
 /**\r
   This function reports the received ICMP error.\r
 \r
@@ -1911,7 +1920,7 @@ Udp6ReportIcmpError (
     //\r
     // Try to get a RxToken from the RxTokens map.\r
     //\r
-    Token = (EFI_UDP6_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);\r
+    Token = (EFI_UDP6_COMPLETION_TOKEN *)NetMapRemoveHead (&Instance->RxTokens, NULL);\r
 \r
     if (Token != NULL) {\r
       //\r
@@ -1928,7 +1937,6 @@ Udp6ReportIcmpError (
   }\r
 }\r
 \r
-\r
 /**\r
   This function is a dummy ext-free function for the NET_BUF created for the output\r
   udp datagram.\r
@@ -1942,7 +1950,7 @@ Udp6NetVectorExtFree (
   IN VOID  *Context\r
   )\r
 {\r
-} \r
+}\r
 \r
 /**\r
   Find the key in the netmap.\r
@@ -1955,8 +1963,8 @@ Udp6NetVectorExtFree (
 **/\r
 NET_MAP_ITEM *\r
 Udp6MapMultiCastAddr (\r
-  IN  NET_MAP               *Map,\r
-  IN  VOID                  *Key\r
+  IN  NET_MAP  *Map,\r
+  IN  VOID     *Key\r
   )\r
 {\r
   LIST_ENTRY        *Entry;\r
@@ -1965,12 +1973,11 @@ Udp6MapMultiCastAddr (
 \r
   ASSERT (Map != NULL);\r
   NET_LIST_FOR_EACH (Entry, &Map->Used) {\r
-    Item  = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
-    Addr  = (EFI_IPv6_ADDRESS *) Item->Key;\r
+    Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
+    Addr = (EFI_IPv6_ADDRESS *)Item->Key;\r
     if (EFI_IP6_EQUAL (Addr, Key)) {\r
       return Item;\r
     }\r
   }\r
   return NULL;\r
 }\r
-\r