]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Impl.c
Fix a bug in Ip4 driver that Ip4.Transmit() interface may return EFI_INVALID_PARAMETE...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Impl.c
index 84a39656f6a10e3e92dc4d37edc0e13dec4f8170..cd01685a304e5a6a4dc5940db0872e4d80dae7e2 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 \r
-Copyright (c) 2005 - 2009, Intel Corporation.<BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>\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
@@ -13,9 +13,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "Ip4Impl.h"\r
 \r
+EFI_IPSEC2_PROTOCOL    *mIpSec = NULL;\r
+\r
 /**\r
   Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.\r
-  \r
+\r
   The GetModeData() function returns the current operational mode data for this\r
   driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This\r
   function is used optionally to retrieve the operational mode data of underlying\r
@@ -39,10 +41,10 @@ EfiIp4GetModeData (
   OUT       EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData   OPTIONAL,\r
   OUT       EFI_SIMPLE_NETWORK_MODE         *SnpModeData     OPTIONAL\r
   );\r
-  \r
+\r
 /**\r
   Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\r
-  \r
+\r
   The Configure() function is used to set, change, or reset the operational\r
   parameters and filter settings for this EFI IPv4 Protocol instance. Until these\r
   parameters have been set, no network traffic can be sent or received by this\r
@@ -51,14 +53,14 @@ EfiIp4GetModeData (
   parameters have been set again. Each EFI IPv4 Protocol instance can be started\r
   and stopped independently of each other by enabling or disabling their receive\r
   filter settings with the Configure() function.\r
-  \r
+\r
   When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will\r
   be appended as an alias address into the addresses list in the EFI IPv4 Protocol\r
   driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL\r
   to retrieve the default IPv4 address if it is not available yet. Clients could\r
   frequently call GetModeData() to check the status to ensure that the default IPv4\r
   address is ready.\r
-  \r
+\r
   If operational parameters are reset or changed, any pending transmit and receive\r
   requests will be cancelled. Their completion token status will be set to EFI_ABORTED\r
   and their events will be signaled.\r
@@ -89,14 +91,14 @@ EfiIp4Configure (
   IN EFI_IP4_PROTOCOL       *This,\r
   IN EFI_IP4_CONFIG_DATA    *IpConfigData       OPTIONAL\r
   );\r
-  \r
+\r
 /**\r
   Joins and leaves multicast groups.\r
-  \r
+\r
   The Groups() function is used to join and leave multicast group sessions. Joining\r
   a group will enable reception of matching multicast packets. Leaving a group will\r
   disable the multicast packet reception.\r
-  \r
+\r
   If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.\r
 \r
   @param[in]  This                  Pointer to the EFI_IP4_PROTOCOL instance.\r
@@ -127,25 +129,25 @@ EfiIp4Groups (
   IN BOOLEAN                JoinFlag,\r
   IN EFI_IPv4_ADDRESS       *GroupAddress     OPTIONAL\r
   );\r
-  \r
+\r
 /**\r
   Adds and deletes routing table entries.\r
 \r
   The Routes() function adds a route to or deletes a route from the routing table.\r
-  \r
+\r
   Routes are determined by comparing the SubnetAddress with the destination IPv4\r
   address arithmetically AND-ed with the SubnetMask. The gateway address must be\r
   on the same subnet as the configured station address.\r
-  \r
+\r
   The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.\r
   The default route matches all destination IPv4 addresses that do not match any\r
   other routes.\r
-  \r
+\r
   A GatewayAddress that is zero is a nonroute. Packets are sent to the destination\r
   IP address if it can be found in the ARP cache or on the local subnet. One automatic\r
   nonroute entry will be inserted into the routing table for outgoing packets that\r
   are addressed to a local subnet (gateway address of 0.0.0.0).\r
-  \r
+\r
   Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI\r
   IPv4 Protocol instances that use the default IPv4 address will also have copies\r
   of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these\r
@@ -176,7 +178,7 @@ EfiIp4Groups (
   @retval EFI_NOT_FOUND          This route is not in the routing table (when DeleteRoute is TRUE).\r
   @retval EFI_ACCESS_DENIED      The route is already defined in the routing table (when\r
                                   DeleteRoute is FALSE).\r
-                                 \r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -187,7 +189,7 @@ EfiIp4Routes (
   IN EFI_IPv4_ADDRESS       *SubnetMask,\r
   IN EFI_IPv4_ADDRESS       *GatewayAddress\r
   );\r
-  \r
+\r
 /**\r
   Places outgoing data packets into the transmit queue.\r
 \r
@@ -206,7 +208,7 @@ EfiIp4Routes (
   @retval  EFI_ACCESS_DENIED     The transmit completion token with the same Token.Event\r
                                  was already in the transmit queue.\r
   @retval  EFI_NOT_READY         The completion token could not be queued because the transmit\r
-                                 queue is full. \r
+                                 queue is full.\r
   @retval  EFI_NOT_FOUND         Not route is found to destination address.\r
   @retval  EFI_OUT_OF_RESOURCES  Could not queue the transmit data.\r
   @retval  EFI_BUFFER_TOO_SMALL  Token.Packet.TxData.TotalDataLength is too\r
@@ -223,13 +225,13 @@ EfiIp4Transmit (
   IN EFI_IP4_PROTOCOL         *This,\r
   IN EFI_IP4_COMPLETION_TOKEN *Token\r
   );\r
-  \r
+\r
 /**\r
   Places a receiving request into the receiving queue.\r
-  \r
+\r
   The Receive() function places a completion token into the receive packet queue.\r
   This function is always asynchronous.\r
-  \r
+\r
   The Token.Event field in the completion token must be filled in by the caller\r
   and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol\r
   driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event\r
@@ -262,10 +264,10 @@ EfiIp4Receive (
   IN EFI_IP4_PROTOCOL         *This,\r
   IN EFI_IP4_COMPLETION_TOKEN *Token\r
   );\r
-  \r
+\r
 /**\r
   Abort an asynchronous transmit or receive request.\r
-  \r
+\r
   The Cancel() function is used to abort a pending transmit or receive request.\r
   If the token is in the transmit or receive request queues, after calling this\r
   function, Token->Status will be set to EFI_ABORTED and then Token->Event will\r
@@ -298,15 +300,15 @@ EfiIp4Cancel (
   IN EFI_IP4_PROTOCOL         *This,\r
   IN EFI_IP4_COMPLETION_TOKEN *Token    OPTIONAL\r
   );\r
-  \r
+\r
 /**\r
   Polls for incoming data packets and processes outgoing data packets.\r
-  \r
+\r
   The Poll() function polls for incoming data packets and processes outgoing data\r
   packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()\r
   function to increase the rate that data packets are moved between the communications\r
   device and the transmit and receive queues.\r
-  \r
+\r
   In some systems the periodic timer event may not poll the underlying communications\r
   device fast enough to transmit and/or receive all data packets without missing\r
   incoming packets or dropping outgoing packets. Drivers and applications that are\r
@@ -346,7 +348,7 @@ mEfiIp4ProtocolTemplete = {
 \r
 /**\r
   Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.\r
-  \r
+\r
   The GetModeData() function returns the current operational mode data for this\r
   driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This\r
   function is used optionally to retrieve the operational mode data of underlying\r
@@ -404,6 +406,8 @@ EfiIp4GetModeData (
     Ip4ModeData->RouteTable    = NULL;\r
     Ip4ModeData->RouteCount    = 0;\r
 \r
+    Ip4ModeData->MaxPacketSize = IpSb->MaxPacketSize;\r
+\r
     //\r
     // return the current station address for this IP child. So,\r
     // the user can get the default address through this. Some\r
@@ -436,16 +440,13 @@ EfiIp4GetModeData (
     }\r
   }\r
 \r
-  if (MnpConfigData != NULL) {\r
-    CopyMem (MnpConfigData, &IpSb->MnpConfigData, sizeof (*MnpConfigData));\r
-  }\r
-\r
-  if (SnpModeData != NULL) {\r
-    CopyMem (SnpModeData, &IpSb->SnpMode, sizeof (*SnpModeData));\r
-  }\r
+  //\r
+  // Get fresh mode data from MNP, since underlying media status may change\r
+  //\r
+  Status = IpSb->Mnp->GetModeData (IpSb->Mnp, MnpConfigData, SnpModeData);\r
 \r
   gBS->RestoreTPL (OldTpl);\r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 \r
@@ -557,6 +558,13 @@ Ip4AutoConfigCallBackDpc (
   EFI_STATUS                Status;\r
   UINTN                     Len;\r
   UINT32                    Index;\r
+  IP4_ADDR                  StationAddress;\r
+  IP4_ADDR                  SubnetMask;\r
+  IP4_ADDR                  SubnetAddress;\r
+  IP4_ADDR                  GatewayAddress;\r
+  IP4_PROTOCOL              *Ip4Instance;\r
+  EFI_ARP_PROTOCOL          *Arp;\r
+  LIST_ENTRY                *Entry;\r
 \r
   IpSb      = (IP4_SERVICE *) Context;\r
   NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
@@ -642,20 +650,40 @@ Ip4AutoConfigCallBackDpc (
   // Set the default interface's address, then add a directed\r
   // route for it, that is, the route whose nexthop is zero.\r
   //\r
-  Status = Ip4SetAddress (\r
-             IpIf,\r
-             EFI_NTOHL (Data->StationAddress),\r
-             EFI_NTOHL (Data->SubnetMask)\r
-             );\r
-\r
+  StationAddress = EFI_NTOHL (Data->StationAddress);\r
+  SubnetMask = EFI_NTOHL (Data->SubnetMask);\r
+  Status = Ip4SetAddress (IpIf, StationAddress, SubnetMask);\r
   if (EFI_ERROR (Status)) {\r
     goto ON_EXIT;\r
   }\r
 \r
+  if (IpIf->Arp != NULL) {\r
+    //   \r
+    // A non-NULL IpIf->Arp here means a new ARP child is created when setting default address, \r
+    // but some IP children may have referenced the default interface before it is configured,\r
+    // these IP instances also consume this ARP protocol so they need to open it BY_CHILD_CONTROLLER.\r
+    //\r
+    Arp = NULL;\r
+    NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
+      Ip4Instance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, AddrLink, IP4_PROTOCOL_SIGNATURE);\r
+      Status = gBS->OpenProtocol (\r
+                      IpIf->ArpHandle,\r
+                      &gEfiArpProtocolGuid,\r
+                      (VOID **) &Arp,\r
+                      gIp4DriverBinding.DriverBindingHandle,\r
+                      Ip4Instance->Handle,\r
+                      EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+                      );\r
+      if (EFI_ERROR (Status)) {\r
+        goto ON_EXIT;\r
+      }\r
+    }\r
+  }\r
+\r
   Ip4AddRoute (\r
     IpSb->DefaultRouteTable,\r
-    EFI_NTOHL (Data->StationAddress),\r
-    EFI_NTOHL (Data->SubnetMask),\r
+    StationAddress,\r
+    SubnetMask,\r
     IP4_ALLZERO_ADDRESS\r
     );\r
 \r
@@ -665,12 +693,10 @@ Ip4AutoConfigCallBackDpc (
   for (Index = 0; Index < Data->RouteTableSize; Index++) {\r
     RouteEntry = &Data->RouteTable[Index];\r
 \r
-    Ip4AddRoute (\r
-      IpSb->DefaultRouteTable,\r
-      EFI_NTOHL (RouteEntry->SubnetAddress),\r
-      EFI_NTOHL (RouteEntry->SubnetMask),\r
-      EFI_NTOHL (RouteEntry->GatewayAddress)\r
-      );\r
+    SubnetAddress = EFI_NTOHL (RouteEntry->SubnetAddress);\r
+    SubnetMask = EFI_NTOHL (RouteEntry->SubnetMask);\r
+    GatewayAddress = EFI_NTOHL (RouteEntry->GatewayAddress);\r
+    Ip4AddRoute (IpSb->DefaultRouteTable, SubnetAddress, SubnetMask, GatewayAddress);\r
   }\r
 \r
   IpSb->State = IP4_SERVICE_CONFIGED;\r
@@ -678,12 +704,12 @@ Ip4AutoConfigCallBackDpc (
   Ip4SetVariableData (IpSb);\r
 \r
 ON_EXIT:\r
-  gBS->FreePool (Data);\r
+  FreePool (Data);\r
 }\r
 \r
 /**\r
   Request Ip4AutoConfigCallBackDpc as a DPC at TPL_CALLBACK.\r
-  \r
+\r
   @param Event     The event that is signalled.\r
   @param Context   The IP4 service binding instance.\r
 \r
@@ -703,7 +729,7 @@ Ip4AutoConfigCallBack (
   //\r
   // Request Ip4AutoConfigCallBackDpc as a DPC at TPL_CALLBACK\r
   //\r
-  NetLibQueueDpc (TPL_CALLBACK, Ip4AutoConfigCallBackDpc, Context);\r
+  QueueDpc (TPL_CALLBACK, Ip4AutoConfigCallBackDpc, Context);\r
 }\r
 \r
 \r
@@ -867,6 +893,7 @@ Ip4ConfigProtocol (
   EFI_STATUS                Status;\r
   IP4_ADDR                  Ip;\r
   IP4_ADDR                  Netmask;\r
+  EFI_ARP_PROTOCOL          *Arp;\r
 \r
   IpSb = IpInstance->Service;\r
 \r
@@ -971,6 +998,20 @@ Ip4ConfigProtocol (
   }\r
 \r
   IpInstance->Interface = IpIf;\r
+  if (IpIf->Arp != NULL) {\r
+    Arp = NULL;\r
+    Status = gBS->OpenProtocol (\r
+                    IpIf->ArpHandle,\r
+                    &gEfiArpProtocolGuid,\r
+                    (VOID **) &Arp,\r
+                    gIp4DriverBinding.DriverBindingHandle,\r
+                    IpInstance->Handle,\r
+                    EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      goto ON_ERROR;\r
+    }\r
+  }\r
   InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink);\r
 \r
   CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));\r
@@ -1027,6 +1068,14 @@ Ip4CleanProtocol (
 \r
   if (IpInstance->Interface != NULL) {\r
     RemoveEntryList (&IpInstance->AddrLink);\r
+    if (IpInstance->Interface->Arp != NULL) {\r
+      gBS->CloseProtocol (\r
+             IpInstance->Interface->ArpHandle,\r
+             &gEfiArpProtocolGuid,\r
+             gIp4DriverBinding.DriverBindingHandle,\r
+             IpInstance->Handle\r
+             );\r
+    }\r
     Ip4FreeInterface (IpInstance->Interface, IpInstance);\r
     IpInstance->Interface = NULL;\r
   }\r
@@ -1041,13 +1090,13 @@ Ip4CleanProtocol (
   }\r
 \r
   if (IpInstance->EfiRouteTable != NULL) {\r
-    gBS->FreePool (IpInstance->EfiRouteTable);\r
+    FreePool (IpInstance->EfiRouteTable);\r
     IpInstance->EfiRouteTable = NULL;\r
     IpInstance->EfiRouteCount = 0;\r
   }\r
 \r
   if (IpInstance->Groups != NULL) {\r
-    gBS->FreePool (IpInstance->Groups);\r
+    FreePool (IpInstance->Groups);\r
     IpInstance->Groups      = NULL;\r
     IpInstance->GroupCount  = 0;\r
   }\r
@@ -1122,7 +1171,7 @@ Ip4StationAddressValid (
 \r
 /**\r
   Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\r
-  \r
+\r
   The Configure() function is used to set, change, or reset the operational\r
   parameters and filter settings for this EFI IPv4 Protocol instance. Until these\r
   parameters have been set, no network traffic can be sent or received by this\r
@@ -1131,14 +1180,14 @@ Ip4StationAddressValid (
   parameters have been set again. Each EFI IPv4 Protocol instance can be started\r
   and stopped independently of each other by enabling or disabling their receive\r
   filter settings with the Configure() function.\r
-  \r
+\r
   When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will\r
   be appended as an alias address into the addresses list in the EFI IPv4 Protocol\r
   driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL\r
   to retrieve the default IPv4 address if it is not available yet. Clients could\r
   frequently call GetModeData() to check the status to ensure that the default IPv4\r
   address is ready.\r
-  \r
+\r
   If operational parameters are reset or changed, any pending transmit and receive\r
   requests will be cancelled. Their completion token status will be set to EFI_ABORTED\r
   and their events will be signaled.\r
@@ -1192,14 +1241,6 @@ EfiIp4Configure (
   // Validate the configuration first.\r
   //\r
   if (IpConfigData != NULL) {\r
-    //\r
-    // This implementation doesn't support RawData\r
-    //\r
-    if (IpConfigData->RawData) {\r
-      Status = EFI_UNSUPPORTED;\r
-      goto ON_EXIT;\r
-    }\r
-\r
 \r
     CopyMem (&IpAddress, &IpConfigData->StationAddress, sizeof (IP4_ADDR));\r
     CopyMem (&SubnetMask, &IpConfigData->SubnetMask, sizeof (IP4_ADDR));\r
@@ -1240,7 +1281,8 @@ EfiIp4Configure (
       }\r
 \r
       if (Current->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
-        return EFI_NO_MAPPING;\r
+        Status = EFI_NO_MAPPING;\r
+        goto ON_EXIT;\r
       }\r
     }\r
   }\r
@@ -1254,7 +1296,7 @@ EfiIp4Configure (
     Status = Ip4CleanProtocol (IpInstance);\r
 \r
     //\r
-    // Don't change the state if it is DESTORY, consider the following\r
+    // Don't change the state if it is DESTROY, consider the following\r
     // valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped,\r
     // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED,\r
     // the unload fails miserably.\r
@@ -1314,7 +1356,7 @@ Ip4Groups (
   // host byte order\r
   //\r
   if (JoinFlag) {\r
-    //  \r
+    //\r
     // When JoinFlag is TRUE, GroupAddress shouldn't be NULL.\r
     //\r
     ASSERT (GroupAddress != NULL);\r
@@ -1333,12 +1375,12 @@ Ip4Groups (
     }\r
 \r
     if (EFI_ERROR (Ip4JoinGroup (IpInstance, NTOHL (Group)))) {\r
-      gBS->FreePool (Members);\r
+      FreePool (Members);\r
       return EFI_DEVICE_ERROR;\r
     }\r
 \r
     if (IpInstance->Groups != NULL) {\r
-      gBS->FreePool (IpInstance->Groups);\r
+      FreePool (IpInstance->Groups);\r
     }\r
 \r
     IpInstance->Groups = Members;\r
@@ -1366,7 +1408,7 @@ Ip4Groups (
       if (IpInstance->GroupCount == 0) {\r
         ASSERT (Index == 1);\r
 \r
-        gBS->FreePool (IpInstance->Groups);\r
+        FreePool (IpInstance->Groups);\r
         IpInstance->Groups = NULL;\r
       }\r
 \r
@@ -1382,11 +1424,11 @@ Ip4Groups (
 \r
 /**\r
   Joins and leaves multicast groups.\r
-  \r
+\r
   The Groups() function is used to join and leave multicast group sessions. Joining\r
   a group will enable reception of matching multicast packets. Leaving a group will\r
   disable the multicast packet reception.\r
-  \r
+\r
   If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.\r
 \r
   @param[in]  This                  Pointer to the EFI_IP4_PROTOCOL instance.\r
@@ -1460,20 +1502,20 @@ ON_EXIT:
   Adds and deletes routing table entries.\r
 \r
   The Routes() function adds a route to or deletes a route from the routing table.\r
-  \r
+\r
   Routes are determined by comparing the SubnetAddress with the destination IPv4\r
   address arithmetically AND-ed with the SubnetMask. The gateway address must be\r
   on the same subnet as the configured station address.\r
-  \r
+\r
   The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.\r
   The default route matches all destination IPv4 addresses that do not match any\r
   other routes.\r
-  \r
+\r
   A GatewayAddress that is zero is a nonroute. Packets are sent to the destination\r
   IP address if it can be found in the ARP cache or on the local subnet. One automatic\r
   nonroute entry will be inserted into the routing table for outgoing packets that\r
   are addressed to a local subnet (gateway address of 0.0.0.0).\r
-  \r
+\r
   Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI\r
   IPv4 Protocol instances that use the default IPv4 address will also have copies\r
   of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these\r
@@ -1504,7 +1546,7 @@ ON_EXIT:
   @retval EFI_NOT_FOUND          This route is not in the routing table (when DeleteRoute is TRUE).\r
   @retval EFI_ACCESS_DENIED      The route is already defined in the routing table (when\r
                                   DeleteRoute is FALSE).\r
-                                 \r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1598,6 +1640,7 @@ ON_EXIT:
 \r
 **/\r
 EFI_STATUS\r
+EFIAPI\r
 Ip4TokenExist (\r
   IN NET_MAP                *Map,\r
   IN NET_MAP_ITEM           *Item,\r
@@ -1617,22 +1660,23 @@ Ip4TokenExist (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Validate the user's token against current station address.\r
 \r
-  @param[in]  Token                  User's token to validate\r
-  @param[in]  IpIf                   The IP4 child's interface.\r
+  @param[in]  Token              User's token to validate.\r
+  @param[in]  IpIf               The IP4 child's interface.\r
+  @param[in]  RawData            Set to TRUE to send unformatted packets.\r
 \r
-  @retval EFI_INVALID_PARAMETER  Some parameters are invalid\r
+  @retval EFI_INVALID_PARAMETER  Some parameters are invalid.\r
   @retval EFI_BAD_BUFFER_SIZE    The user's option/data is too long.\r
-  @retval EFI_SUCCESS            The token is OK\r
+  @retval EFI_SUCCESS            The token is valid.\r
 \r
 **/\r
 EFI_STATUS\r
 Ip4TxTokenValid (\r
   IN EFI_IP4_COMPLETION_TOKEN   *Token,\r
-  IN IP4_INTERFACE              *IpIf\r
+  IN IP4_INTERFACE              *IpIf,\r
+  IN BOOLEAN                    RawData\r
   )\r
 {\r
   EFI_IP4_TRANSMIT_DATA     *TxData;\r
@@ -1650,20 +1694,7 @@ Ip4TxTokenValid (
   TxData = Token->Packet.TxData;\r
 \r
   //\r
-  // Check the IP options: no more than 40 bytes and format is OK\r
-  //\r
-  if (TxData->OptionsLength != 0) {\r
-    if ((TxData->OptionsLength > 40) || (TxData->OptionsBuffer == NULL)) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-\r
-    if (!Ip4OptionIsValid (TxData->OptionsBuffer, TxData->OptionsLength, FALSE)) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Check the fragment table: no empty fragment, and length isn't bogus\r
+  // Check the fragment table: no empty fragment, and length isn't bogus.\r
   //\r
   if ((TxData->TotalDataLength == 0) || (TxData->FragmentCount == 0)) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1671,6 +1702,10 @@ Ip4TxTokenValid (
 \r
   Offset = TxData->TotalDataLength;\r
 \r
+  if (Offset > IP4_MAX_PACKET_SIZE) {\r
+    return EFI_BAD_BUFFER_SIZE;\r
+  }\r
+\r
   for (Index = 0; Index < TxData->FragmentCount; Index++) {\r
     if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||\r
         (TxData->FragmentTable[Index].FragmentLength == 0)) {\r
@@ -1685,11 +1720,32 @@ Ip4TxTokenValid (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // NOTE that OptionsLength/OptionsBuffer/OverrideData are ignored if RawData\r
+  // is TRUE.\r
+  //\r
+  if (RawData) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Check the IP options: no more than 40 bytes and format is OK\r
+  //\r
+  if (TxData->OptionsLength != 0) {\r
+    if ((TxData->OptionsLength > 40) || (TxData->OptionsBuffer == NULL)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
+    if (!Ip4OptionIsValid (TxData->OptionsBuffer, TxData->OptionsLength, FALSE)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  }\r
+\r
   //\r
   // Check the source and gateway: they must be a valid unicast.\r
   // Gateway must also be on the connected network.\r
   //\r
-  if (TxData->OverrideData) {\r
+  if (TxData->OverrideData != NULL) {\r
     Override = TxData->OverrideData;\r
 \r
     CopyMem (&Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
@@ -1755,6 +1811,7 @@ Ip4TxTokenValid (
 \r
 **/\r
 VOID\r
+EFIAPI\r
 Ip4FreeTxToken (\r
   IN VOID                   *Context\r
   )\r
@@ -1764,6 +1821,13 @@ Ip4FreeTxToken (
 \r
   Wrap = (IP4_TXTOKEN_WRAP *) Context;\r
 \r
+  //\r
+  // Signal IpSecRecycleEvent to inform IPsec free the memory\r
+  //\r
+  if (Wrap->IpSecRecycleSignal != NULL) {\r
+    gBS->SignalEvent (Wrap->IpSecRecycleSignal);\r
+  }\r
+\r
   //\r
   // Find the token in the instance's map. EfiIp4Transmit put the\r
   // token to the map. If that failed, NetMapFindKey will return NULL.\r
@@ -1780,10 +1844,10 @@ Ip4FreeTxToken (
     //\r
     // Dispatch the DPC queued by the NotifyFunction of Token->Event.\r
     //\r
-    NetLibDispatchDpc ();\r
+    DispatchDpc ();\r
   }\r
 \r
-  gBS->FreePool (Wrap);\r
+  FreePool (Wrap);\r
 }\r
 \r
 \r
@@ -1846,7 +1910,7 @@ Ip4OnPacketSent (
   @retval  EFI_ACCESS_DENIED     The transmit completion token with the same Token.Event\r
                                  was already in the transmit queue.\r
   @retval  EFI_NOT_READY         The completion token could not be queued because the transmit\r
-                                 queue is full. \r
+                                 queue is full.\r
   @retval  EFI_NOT_FOUND         Not route is found to destination address.\r
   @retval  EFI_OUT_OF_RESOURCES  Could not queue the transmit data.\r
   @retval  EFI_BUFFER_TOO_SMALL  Token.Packet.TxData.TotalDataLength is too\r
@@ -1877,6 +1941,10 @@ EfiIp4Transmit (
   EFI_TPL                   OldTpl;\r
   BOOLEAN                   DontFragment;\r
   UINT32                    HeadLen;\r
+  UINT8                     RawHdrLen;\r
+  UINT32                    OptionsLength;\r
+  UINT8                     *OptionsBuffer;\r
+  VOID                      *FirstFragment;\r
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1902,7 +1970,7 @@ EfiIp4Transmit (
   //\r
   // make sure that token is properly formated\r
   //\r
-  Status = Ip4TxTokenValid (Token, IpIf);\r
+  Status = Ip4TxTokenValid (Token, IpIf, Config->RawData);\r
 \r
   if (EFI_ERROR (Status)) {\r
     goto ON_EXIT;\r
@@ -1922,37 +1990,89 @@ EfiIp4Transmit (
   //\r
   TxData = Token->Packet.TxData;\r
 \r
-  CopyMem (&Head.Dst, &TxData->DestinationAddress, sizeof (IP4_ADDR));\r
-  Head.Dst = NTOHL (Head.Dst);\r
+  FirstFragment = NULL;\r
 \r
-  if (TxData->OverrideData) {\r
-    Override      = TxData->OverrideData;\r
-    Head.Protocol = Override->Protocol;\r
-    Head.Tos      = Override->TypeOfService;\r
-    Head.Ttl      = Override->TimeToLive;\r
-    DontFragment  = Override->DoNotFragment;\r
+  if (Config->RawData) {\r
+    //\r
+    // When RawData is TRUE, first buffer in FragmentTable points to a raw\r
+    // IPv4 fragment including IPv4 header and options.\r
+    //\r
+    FirstFragment = TxData->FragmentTable[0].FragmentBuffer;\r
+    CopyMem (&RawHdrLen, FirstFragment, sizeof (UINT8));\r
 \r
-    CopyMem (&Head.Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
-    CopyMem (&GateWay, &Override->GatewayAddress, sizeof (IP4_ADDR));\r
+    RawHdrLen = (UINT8) (RawHdrLen & 0x0f);\r
+    if (RawHdrLen < 5) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto ON_EXIT;\r
+    }\r
 \r
-    Head.Src = NTOHL (Head.Src);\r
-    GateWay  = NTOHL (GateWay);\r
+    RawHdrLen = (UINT8) (RawHdrLen << 2);\r
+    \r
+    CopyMem (&Head, FirstFragment, IP4_MIN_HEADLEN);\r
+\r
+    Ip4NtohHead (&Head);\r
+    HeadLen      = 0;\r
+    DontFragment = IP4_DO_NOT_FRAGMENT (Head.Fragment);\r
+\r
+    if (!DontFragment) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto ON_EXIT;\r
+    }\r
+\r
+    GateWay = IP4_ALLZERO_ADDRESS;\r
+\r
+    //\r
+    // Get IPv4 options from first fragment.\r
+    //\r
+    if (RawHdrLen == IP4_MIN_HEADLEN) {\r
+      OptionsLength = 0;\r
+      OptionsBuffer = NULL;\r
+    } else {\r
+      OptionsLength = RawHdrLen - IP4_MIN_HEADLEN;\r
+      OptionsBuffer = (UINT8 *) FirstFragment + IP4_MIN_HEADLEN;\r
+    }\r
+\r
+    //\r
+    // Trim off IPv4 header and options from first fragment.\r
+    //\r
+    TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment + RawHdrLen;\r
+    TxData->FragmentTable[0].FragmentLength = TxData->FragmentTable[0].FragmentLength - RawHdrLen;\r
   } else {\r
-    Head.Src      = IpIf->Ip;\r
-    GateWay       = IP4_ALLZERO_ADDRESS;\r
-    Head.Protocol = Config->DefaultProtocol;\r
-    Head.Tos      = Config->TypeOfService;\r
-    Head.Ttl      = Config->TimeToLive;\r
-    DontFragment  = Config->DoNotFragment;\r
-  }\r
+    CopyMem (&Head.Dst, &TxData->DestinationAddress, sizeof (IP4_ADDR));\r
+    Head.Dst = NTOHL (Head.Dst);\r
 \r
-  Head.Fragment = IP4_HEAD_FRAGMENT_FIELD (DontFragment, FALSE, 0);\r
-  HeadLen       = sizeof (IP4_HEAD) + ((TxData->OptionsLength + 3) &~0x03);\r
+    if (TxData->OverrideData != NULL) {\r
+      Override      = TxData->OverrideData;\r
+      Head.Protocol = Override->Protocol;\r
+      Head.Tos      = Override->TypeOfService;\r
+      Head.Ttl      = Override->TimeToLive;\r
+      DontFragment  = Override->DoNotFragment;\r
+\r
+      CopyMem (&Head.Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
+      CopyMem (&GateWay, &Override->GatewayAddress, sizeof (IP4_ADDR));\r
+\r
+      Head.Src = NTOHL (Head.Src);\r
+      GateWay  = NTOHL (GateWay);\r
+    } else {\r
+      Head.Src      = IpIf->Ip;\r
+      GateWay       = IP4_ALLZERO_ADDRESS;\r
+      Head.Protocol = Config->DefaultProtocol;\r
+      Head.Tos      = Config->TypeOfService;\r
+      Head.Ttl      = Config->TimeToLive;\r
+      DontFragment  = Config->DoNotFragment;\r
+    }\r
+\r
+    Head.Fragment = IP4_HEAD_FRAGMENT_FIELD (DontFragment, FALSE, 0);\r
+    HeadLen       = (TxData->OptionsLength + 3) & (~0x03);\r
+\r
+    OptionsLength = TxData->OptionsLength;\r
+    OptionsBuffer = (UINT8 *) (TxData->OptionsBuffer);\r
+  }\r
 \r
   //\r
   // If don't fragment and fragment needed, return error\r
   //\r
-  if (DontFragment && (TxData->TotalDataLength + HeadLen > IpSb->SnpMode.MaxPacketSize)) {\r
+  if (DontFragment && (TxData->TotalDataLength + HeadLen > IpSb->MaxPacketSize)) {\r
     Status = EFI_BAD_BUFFER_SIZE;\r
     goto ON_EXIT;\r
   }\r
@@ -1962,7 +2082,7 @@ EfiIp4Transmit (
   // a IP4_TXTOKEN_WRAP and the data in a netbuf\r
   //\r
   Status = EFI_OUT_OF_RESOURCES;\r
-  Wrap   = AllocatePool (sizeof (IP4_TXTOKEN_WRAP));\r
+  Wrap   = AllocateZeroPool (sizeof (IP4_TXTOKEN_WRAP));\r
   if (Wrap == NULL) {\r
     goto ON_EXIT;\r
   }\r
@@ -1981,7 +2101,7 @@ EfiIp4Transmit (
                         );\r
 \r
   if (Wrap->Packet == NULL) {\r
-    gBS->FreePool (Wrap);\r
+    FreePool (Wrap);\r
     goto ON_EXIT;\r
   }\r
 \r
@@ -1993,6 +2113,13 @@ EfiIp4Transmit (
     // free the IP4_TXTOKEN_WRAP. Now, the token wrap hasn't been\r
     // enqueued.\r
     //\r
+    if (Config->RawData) {\r
+      //\r
+      // Restore pointer of first fragment in RawData mode.\r
+      //\r
+      TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment;\r
+    }\r
+\r
     NetbufFree (Wrap->Packet);\r
     goto ON_EXIT;\r
   }\r
@@ -2008,8 +2135,8 @@ EfiIp4Transmit (
              IpInstance,\r
              Wrap->Packet,\r
              &Head,\r
-             TxData->OptionsBuffer,\r
-             TxData->OptionsLength,\r
+             OptionsBuffer,\r
+             OptionsLength,\r
              GateWay,\r
              Ip4OnPacketSent,\r
              Wrap\r
@@ -2017,9 +2144,24 @@ EfiIp4Transmit (
 \r
   if (EFI_ERROR (Status)) {\r
     Wrap->Sent = FALSE;\r
+\r
+    if (Config->RawData) {\r
+      //\r
+      // Restore pointer of first fragment in RawData mode.\r
+      //\r
+      TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment;\r
+    }\r
+\r
     NetbufFree (Wrap->Packet);\r
   }\r
 \r
+  if (Config->RawData) {\r
+    //\r
+    // Restore pointer of first fragment in RawData mode.\r
+    //\r
+    TxData->FragmentTable[0].FragmentBuffer = (UINT8 *) FirstFragment;\r
+  }\r
+\r
 ON_EXIT:\r
   gBS->RestoreTPL (OldTpl);\r
   return Status;\r
@@ -2028,10 +2170,10 @@ ON_EXIT:
 \r
 /**\r
   Places a receiving request into the receiving queue.\r
-  \r
+\r
   The Receive() function places a completion token into the receive packet queue.\r
   This function is always asynchronous.\r
-  \r
+\r
   The Token.Event field in the completion token must be filled in by the caller\r
   and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol\r
   driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event\r
@@ -2110,7 +2252,7 @@ EfiIp4Receive (
   // Dispatch the DPC queued by the NotifyFunction of this instane's receive\r
   // event.\r
   //\r
-  NetLibDispatchDpc ();\r
+  DispatchDpc ();\r
 \r
 ON_EXIT:\r
   gBS->RestoreTPL (OldTpl);\r
@@ -2136,6 +2278,7 @@ ON_EXIT:
 \r
 **/\r
 EFI_STATUS\r
+EFIAPI\r
 Ip4CancelTxTokens (\r
   IN NET_MAP                *Map,\r
   IN NET_MAP_ITEM           *Item,\r
@@ -2191,6 +2334,7 @@ Ip4CancelTxTokens (
 \r
 **/\r
 EFI_STATUS\r
+EFIAPI\r
 Ip4CancelRxTokens (\r
   IN NET_MAP                *Map,\r
   IN NET_MAP_ITEM           *Item,\r
@@ -2266,7 +2410,7 @@ Ip4Cancel (
   // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's\r
   // events.\r
   //\r
-  NetLibDispatchDpc ();\r
+  DispatchDpc ();\r
   if (EFI_ERROR (Status)) {\r
     if ((Token != NULL) && (Status == EFI_ABORTED)) {\r
       return EFI_SUCCESS;\r
@@ -2299,7 +2443,7 @@ Ip4Cancel (
 \r
 /**\r
   Abort an asynchronous transmit or receive request.\r
-  \r
+\r
   The Cancel() function is used to abort a pending transmit or receive request.\r
   If the token is in the transmit or receive request queues, after calling this\r
   function, Token->Status will be set to EFI_ABORTED and then Token->Event will\r
@@ -2365,12 +2509,12 @@ ON_EXIT:
 \r
 /**\r
   Polls for incoming data packets and processes outgoing data packets.\r
-  \r
+\r
   The Poll() function polls for incoming data packets and processes outgoing data\r
   packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()\r
   function to increase the rate that data packets are moved between the communications\r
   device and the transmit and receive queues.\r
-  \r
+\r
   In some systems the periodic timer event may not poll the underlying communications\r
   device fast enough to transmit and/or receive all data packets without missing\r
   incoming packets or dropping outgoing packets. Drivers and applications that are\r
@@ -2433,6 +2577,7 @@ EfiIp4Poll (
 \r
 **/\r
 EFI_STATUS\r
+EFIAPI\r
 Ip4SentPacketTicking (\r
   IN NET_MAP                *Map,\r
   IN NET_MAP_ITEM           *Item,\r