]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Impl.c
MdeModulePkg: Update IP4 driver to check for NULL pointer before using.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Impl.c
index ce546c235d05f017a0802f872f0c92fe2fc181e8..6a26143e307d3e4effe40bc1f190fb0833afd3d0 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 \r
-Copyright (c) 2005 - 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2005 - 2018, 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
@@ -9,40 +9,367 @@ http://opensource.org/licenses/bsd-license.php
 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
 \r
+**/\r
+\r
+#include "Ip4Impl.h"\r
 \r
-Module Name:\r
+EFI_IPSEC2_PROTOCOL    *mIpSec = NULL;\r
 \r
-  Ip4Impl.c\r
+/**\r
+  Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.\r
 \r
-Abstract:\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
+  networks or drivers.\r
 \r
+  @param[in]   This          Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[out]  Ip4ModeData   Pointer to the EFI IPv4 Protocol mode data structure.\r
+  @param[out]  MnpConfigData Pointer to the managed network configuration data structure.\r
+  @param[out]  SnpModeData   Pointer to the simple network mode data structure.\r
+\r
+  @retval EFI_SUCCESS           The operation completed successfully.\r
+  @retval EFI_INVALID_PARAMETER This is NULL.\r
+  @retval EFI_OUT_OF_RESOURCES  The required mode data could not be allocated.\r
 \r
 **/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiIp4GetModeData (\r
+  IN  CONST EFI_IP4_PROTOCOL                *This,\r
+  OUT       EFI_IP4_MODE_DATA               *Ip4ModeData     OPTIONAL,\r
+  OUT       EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData   OPTIONAL,\r
+  OUT       EFI_SIMPLE_NETWORK_MODE         *SnpModeData     OPTIONAL\r
+  );\r
 \r
-#include "Ip4Impl.h"\r
+/**\r
+  Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\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
+  instance. Once the parameters have been reset (by calling this function with\r
+  IpConfigData set to NULL), no more traffic can be sent or received until these\r
+  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
+  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
+  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
+\r
+  @param[in]  This              Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  IpConfigData      Pointer to the EFI IPv4 Protocol configuration data structure.\r
+\r
+  @retval EFI_SUCCESS           The driver instance was successfully opened.\r
+  @retval EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                RARP, etc.) is not finished yet.\r
+  @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
+  @retval EFI_UNSUPPORTED       One or more of the following conditions is TRUE:\r
+                                A configuration protocol (DHCP, BOOTP, RARP, etc.) could\r
+                                not be located when clients choose to use the default IPv4\r
+                                address. This EFI IPv4 Protocol implementation does not\r
+                                support this requested filter or timeout setting.\r
+  @retval EFI_OUT_OF_RESOURCES  The EFI IPv4 Protocol driver instance data could not be allocated.\r
+  @retval EFI_ALREADY_STARTED   The interface is already open and must be stopped before the\r
+                                IPv4 address or subnet mask can be changed. The interface must\r
+                                also be stopped when switching to/from raw packet mode.\r
+  @retval EFI_DEVICE_ERROR      An unexpected system or network error occurred. The EFI IPv4\r
+                                Protocol driver instance is not opened.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiIp4Configure (\r
+  IN EFI_IP4_PROTOCOL       *This,\r
+  IN EFI_IP4_CONFIG_DATA    *IpConfigData       OPTIONAL\r
+  );\r
+\r
+/**\r
+  Joins and leaves multicast groups.\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
+  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
+  @param[in]  JoinFlag              Set to TRUE to join the multicast group session and FALSE to leave.\r
+  @param[in]  GroupAddress          Pointer to the IPv4 multicast address.\r
+\r
+  @retval EFI_SUCCESS           The operation completed successfully.\r
+  @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:\r
+                                - This is NULL.\r
+                                - JoinFlag is TRUE and GroupAddress is NULL.\r
+                                - GroupAddress is not NULL and *GroupAddress is\r
+                                not a multicast IPv4 address.\r
+  @retval EFI_NOT_STARTED       This instance has not been started.\r
+  @retval EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                RARP, etc.) is not finished yet.\r
+  @retval EFI_OUT_OF_RESOURCES  System resources could not be allocated.\r
+  @retval EFI_UNSUPPORTED       This EFI IPv4 Protocol implementation does not support multicast groups.\r
+  @retval EFI_ALREADY_STARTED   The group address is already in the group table (when\r
+                                JoinFlag is TRUE).\r
+  @retval EFI_NOT_FOUND         The group address is not in the group table (when JoinFlag is FALSE).\r
+  @retval EFI_DEVICE_ERROR      An unexpected system or network error occurred.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiIp4Groups (\r
+  IN EFI_IP4_PROTOCOL       *This,\r
+  IN BOOLEAN                JoinFlag,\r
+  IN EFI_IPv4_ADDRESS       *GroupAddress     OPTIONAL\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
+  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
+  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
+  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
+  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
+  copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its\r
+  instances. As a result, client modification to the routing table will be lost.\r
+\r
+  @param[in]  This                   Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  DeleteRoute            Set to TRUE to delete this route from the routing table. Set to\r
+                                     FALSE to add this route to the routing table. SubnetAddress\r
+                                     and SubnetMask are used as the key to each route entry.\r
+  @param[in]  SubnetAddress          The address of the subnet that needs to be routed.\r
+  @param[in]  SubnetMask             The subnet mask of SubnetAddress.\r
+  @param[in]  GatewayAddress         The unicast gateway IPv4 address for this route.\r
+\r
+  @retval EFI_SUCCESS            The operation completed successfully.\r
+  @retval EFI_NOT_STARTED        The driver instance has not been started.\r
+  @retval EFI_NO_MAPPING         When using the default address, configuration (DHCP, BOOTP,\r
+                                 RARP, etc.) is not finished yet.\r
+  @retval EFI_INVALID_PARAMETER  One or more of the following conditions is TRUE:\r
+                                 - This is NULL.\r
+                                 - SubnetAddress is NULL.\r
+                                 - SubnetMask is NULL.\r
+                                 - GatewayAddress is NULL.\r
+                                 - *SubnetAddress is not a valid subnet address.\r
+                                 - *SubnetMask is not a valid subnet mask.\r
+                                 - *GatewayAddress is not a valid unicast IPv4 address.\r
+  @retval EFI_OUT_OF_RESOURCES   Could not add the entry to the routing table.\r
+  @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
+EFI_STATUS\r
+EFIAPI\r
+EfiIp4Routes (\r
+  IN EFI_IP4_PROTOCOL       *This,\r
+  IN BOOLEAN                DeleteRoute,\r
+  IN EFI_IPv4_ADDRESS       *SubnetAddress,\r
+  IN EFI_IPv4_ADDRESS       *SubnetMask,\r
+  IN EFI_IPv4_ADDRESS       *GatewayAddress\r
+  );\r
+\r
+/**\r
+  Places outgoing data packets into the transmit queue.\r
+\r
+  The Transmit() function places a sending request in the transmit queue of this\r
+  EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some\r
+  errors occur, the event in the token will be signaled and the status is updated.\r
+\r
+  @param[in]  This  Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  Token Pointer to the transmit token.\r
+\r
+  @retval  EFI_SUCCESS           The data has been queued for transmission.\r
+  @retval  EFI_NOT_STARTED       This instance has not been started.\r
+  @retval  EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                 RARP, etc.) is not finished yet.\r
+  @retval  EFI_INVALID_PARAMETER One or more pameters are invalid.\r
+  @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
+  @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
+                                 short to transmit.\r
+  @retval  EFI_BAD_BUFFER_SIZE   The length of the IPv4 header + option length + total data length is\r
+                                 greater than MTU (or greater than the maximum packet size if\r
+                                 Token.Packet.TxData.OverrideData.\r
+                                 DoNotFragment is TRUE.)\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiIp4Transmit (\r
+  IN EFI_IP4_PROTOCOL         *This,\r
+  IN EFI_IP4_COMPLETION_TOKEN *Token\r
+  );\r
+\r
+/**\r
+  Places a receiving request into the receiving queue.\r
+\r
+  The Receive() function places a completion token into the receive packet queue.\r
+  This function is always asynchronous.\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
+  is signaled.\r
+\r
+  @param[in]  This  Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  Token Pointer to a token that is associated with the receive data descriptor.\r
+\r
+  @retval EFI_SUCCESS           The receive completion token was cached.\r
+  @retval EFI_NOT_STARTED       This EFI IPv4 Protocol instance has not been started.\r
+  @retval EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP, RARP, etc.)\r
+                                is not finished yet.\r
+  @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
+                                - This is NULL.\r
+                                - Token is NULL.\r
+                                - Token.Event is NULL.\r
+  @retval EFI_OUT_OF_RESOURCES  The receive completion token could not be queued due to a lack of system\r
+                                resources (usually memory).\r
+  @retval EFI_DEVICE_ERROR      An unexpected system or network error occurred.\r
+                                The EFI IPv4 Protocol instance has been reset to startup defaults.\r
+                                EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already\r
+                                in the receive queue.\r
+  @retval EFI_NOT_READY         The receive request could not be queued because the receive queue is full.\r
+  @retval EFI_ICMP_ERROR        An ICMP error packet was received.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiIp4Receive (\r
+  IN EFI_IP4_PROTOCOL         *This,\r
+  IN EFI_IP4_COMPLETION_TOKEN *Token\r
+  );\r
+\r
+/**\r
+  Abort an asynchronous transmit or receive request.\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
+  be signaled. If the token is not in one of the queues, which usually means the\r
+  asynchronous operation has completed, this function will not signal the token\r
+  and EFI_NOT_FOUND is returned.\r
+\r
+  @param[in]  This  Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  Token Pointer to a token that has been issued by\r
+                    EFI_IP4_PROTOCOL.Transmit() or\r
+                    EFI_IP4_PROTOCOL.Receive(). If NULL, all pending\r
+                    tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is\r
+                    defined in EFI_IP4_PROTOCOL.Transmit().\r
+\r
+  @retval EFI_SUCCESS           The asynchronous I/O request was aborted and\r
+                                Token.->Event was signaled. When Token is NULL, all\r
+                                pending requests were aborted and their events were signaled.\r
+  @retval EFI_INVALID_PARAMETER This is NULL.\r
+  @retval EFI_NOT_STARTED       This instance has not been started.\r
+  @retval EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                RARP, etc.) is not finished yet.\r
+  @retval EFI_NOT_FOUND         When Token is not NULL, the asynchronous I/O request was\r
+                                not found in the transmit or receive queue. It has either completed\r
+                                or was not issued by Transmit() and Receive().\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiIp4Cancel (\r
+  IN EFI_IP4_PROTOCOL         *This,\r
+  IN EFI_IP4_COMPLETION_TOKEN *Token    OPTIONAL\r
+  );\r
 \r
+/**\r
+  Polls for incoming data packets and processes outgoing data packets.\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
+  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
+  experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function\r
+  more often.\r
+\r
+  @param[in]  This               Pointer to the EFI_IP4_PROTOCOL instance.\r
+\r
+  @retval  EFI_SUCCESS           Incoming or outgoing data was processed.\r
+  @retval  EFI_NOT_STARTED       This EFI IPv4 Protocol instance has not been started.\r
+  @retval  EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                 RARP, etc.) is not finished yet.\r
+  @retval  EFI_INVALID_PARAMETER This is NULL.\r
+  @retval  EFI_DEVICE_ERROR      An unexpected system or network error occurred.\r
+  @retval  EFI_NOT_READY         No incoming or outgoing data is processed.\r
+  @retval  EFI_TIMEOUT           Data was dropped out of the transmit and/or receive queue.\r
+                                 Consider increasing the polling rate.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiIp4Poll (\r
+  IN EFI_IP4_PROTOCOL       *This\r
+  );\r
+\r
+EFI_IP4_PROTOCOL\r
+mEfiIp4ProtocolTemplete = {\r
+  EfiIp4GetModeData,\r
+  EfiIp4Configure,\r
+  EfiIp4Groups,\r
+  EfiIp4Routes,\r
+  EfiIp4Transmit,\r
+  EfiIp4Receive,\r
+  EfiIp4Cancel,\r
+  EfiIp4Poll\r
+};\r
 \r
 /**\r
-  Get the IP child's current operational data. This can\r
-  all be used to get the underlying MNP and SNP data.\r
+  Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.\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
+  networks or drivers.\r
 \r
-  @param  This                   The IP4 protocol instance\r
-  @param  Ip4ModeData            The IP4 operation data\r
-  @param  MnpConfigData          The MNP configure data\r
-  @param  SnpModeData            The SNP operation data\r
+  @param[in]   This          Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[out]  Ip4ModeData   Pointer to the EFI IPv4 Protocol mode data structure.\r
+  @param[out]  MnpConfigData Pointer to the managed network configuration data structure.\r
+  @param[out]  SnpModeData   Pointer to the simple network mode data structure.\r
 \r
-  @retval EFI_INVALID_PARAMETER  The parameter is invalid because This == NULL\r
-  @retval EFI_SUCCESS            The operational parameter is returned.\r
-  @retval Others                 Failed to retrieve the IP4 route table.\r
+  @retval EFI_SUCCESS           The operation completed successfully.\r
+  @retval EFI_INVALID_PARAMETER This is NULL.\r
+  @retval EFI_OUT_OF_RESOURCES  The required mode data could not be allocated.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 EfiIp4GetModeData (\r
   IN  CONST EFI_IP4_PROTOCOL                *This,\r
-  OUT       EFI_IP4_MODE_DATA               *Ip4ModeData,    OPTIONAL\r
-  OUT       EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData,  OPTIONAL\r
+  OUT       EFI_IP4_MODE_DATA               *Ip4ModeData     OPTIONAL,\r
+  OUT       EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData   OPTIONAL,\r
   OUT       EFI_SIMPLE_NETWORK_MODE         *SnpModeData     OPTIONAL\r
   )\r
 {\r
@@ -79,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
@@ -111,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
@@ -129,14 +455,14 @@ EfiIp4GetModeData (
   child to transmit/receive frames. By default, it configures MNP\r
   to receive unicast/multicast/broadcast. And it will enable/disable\r
   the promiscous receive according to whether there is IP child\r
-  enable that or not. If Force isn't false, it will iterate through\r
+  enable that or not. If Force is FALSE, it will iterate through\r
   all the IP children to check whether the promiscuous receive\r
   setting has been changed. If it hasn't been changed, it won't\r
-  reconfigure the MNP. If Force is true, the MNP is configured no\r
+  reconfigure the MNP. If Force is TRUE, the MNP is configured no\r
   matter whether that is changed or not.\r
 \r
-  @param  IpSb                   The IP4 service instance that is to be changed.\r
-  @param  Force                  Force the configuration or not.\r
+  @param[in]  IpSb               The IP4 service instance that is to be changed.\r
+  @param[in]  Force              Force the configuration or not.\r
 \r
   @retval EFI_SUCCESS            The MNP is successfully configured/reconfigured.\r
   @retval Others                 Configuration failed.\r
@@ -204,304 +530,17 @@ Ip4ServiceConfigMnp (
 }\r
 \r
 \r
-/**\r
-  The event handle for IP4 auto configuration. If IP is asked\r
-  to reconfigure the default address. The original default\r
-  interface and route table are removed as the default. If there\r
-  is active IP children using the default address, the interface\r
-  will remain valid until all the children have freed their\r
-  references. If IP is signalled when auto configuration is done,\r
-  it will configure the default interface and default route table\r
-  with the configuration information retrieved by IP4_CONFIGURE.\r
-\r
-  @param  Context                The IP4 service binding instance.\r
-\r
-  @return None\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-Ip4AutoConfigCallBackDpc (\r
-  IN VOID                   *Context\r
-  )\r
-{\r
-  EFI_IP4_CONFIG_PROTOCOL   *Ip4Config;\r
-  EFI_IP4_IPCONFIG_DATA     *Data;\r
-  EFI_IP4_ROUTE_TABLE       *RouteEntry;\r
-  IP4_SERVICE               *IpSb;\r
-  IP4_ROUTE_TABLE           *RouteTable;\r
-  IP4_INTERFACE             *IpIf;\r
-  EFI_STATUS                Status;\r
-  UINTN                     Len;\r
-  UINT32                    Index;\r
-\r
-  IpSb      = (IP4_SERVICE *) Context;\r
-  NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
-\r
-  Ip4Config = IpSb->Ip4Config;\r
-\r
-  //\r
-  // IP is asked to do the reconfiguration. If the default interface\r
-  // has been configured, release the default interface and route\r
-  // table, then create a new one. If there are some IP children\r
-  // using it, the interface won't be physically freed until all the\r
-  // children have released their reference to it. Also remember to\r
-  // restart the receive on the default address. IP4 driver only receive\r
-  // frames on the default address, and when the default interface is\r
-  // freed, Ip4AcceptFrame won't be informed.\r
-  //\r
-  if (IpSb->ActiveEvent == IpSb->ReconfigEvent) {\r
-\r
-    if (IpSb->DefaultInterface->Configured) {\r
-      IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);\r
-\r
-      if (IpIf == NULL) {\r
-        return;\r
-      }\r
-\r
-      RouteTable = Ip4CreateRouteTable ();\r
-\r
-      if (RouteTable == NULL) {\r
-        Ip4FreeInterface (IpIf, NULL);\r
-        return;\r
-      }\r
-\r
-      Ip4CancelReceive (IpSb->DefaultInterface);\r
-      Ip4FreeInterface (IpSb->DefaultInterface, NULL);\r
-      Ip4FreeRouteTable (IpSb->DefaultRouteTable);\r
-\r
-      IpSb->DefaultInterface  = IpIf;\r
-      InsertHeadList (&IpSb->Interfaces, &IpIf->Link);\r
-\r
-      IpSb->DefaultRouteTable = RouteTable;\r
-      Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);\r
-    }\r
-\r
-    Ip4Config->Stop (Ip4Config);\r
-    Ip4Config->Start (Ip4Config, IpSb->DoneEvent, IpSb->ReconfigEvent);\r
-    return ;\r
-  }\r
-\r
-  //\r
-  // Get the configure data in two steps: get the length then the data.\r
-  //\r
-  Len = 0;\r
-\r
-  if (Ip4Config->GetData (Ip4Config, &Len, NULL) != EFI_BUFFER_TOO_SMALL) {\r
-    return ;\r
-  }\r
-\r
-  Data = AllocatePool (Len);\r
-\r
-  if (Data == NULL) {\r
-    return ;\r
-  }\r
-\r
-  Status = Ip4Config->GetData (Ip4Config, &Len, Data);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_EXIT;\r
-  }\r
-\r
-  IpIf = IpSb->DefaultInterface;\r
-\r
-  //\r
-  // If the default address has been configured don't change it.\r
-  // This is unlikely to happen if EFI_IP4_CONFIG protocol has\r
-  // informed us to reconfigure each time it wants to change the\r
-  // configuration parameters.\r
-  //\r
-  if (IpIf->Configured) {\r
-    goto ON_EXIT;\r
-  }\r
-\r
-  //\r
-  // 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
-  if (EFI_ERROR (Status)) {\r
-    goto ON_EXIT;\r
-  }\r
-\r
-  Ip4AddRoute (\r
-    IpSb->DefaultRouteTable,\r
-    EFI_NTOHL (Data->StationAddress),\r
-    EFI_NTOHL (Data->SubnetMask),\r
-    IP4_ALLZERO_ADDRESS\r
-    );\r
-\r
-  //\r
-  // Add routes returned by EFI_IP4_CONFIG protocol.\r
-  //\r
-  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
-  }\r
-\r
-  IpSb->State = IP4_SERVICE_CONFIGED;\r
-\r
-  Ip4SetVariableData (IpSb);\r
-\r
-ON_EXIT:\r
-  gBS->FreePool (Data);\r
-}\r
-\r
-VOID\r
-EFIAPI\r
-Ip4AutoConfigCallBack (\r
-  IN EFI_EVENT              Event,\r
-  IN VOID                   *Context\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Request Ip4AutoConfigCallBackDpc as a DPC at TPL_CALLBACK\r
-\r
-Arguments:\r
-\r
-  Event   - The event that is signalled.\r
-  Context - The IP4 service binding instance.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
-{\r
-  IP4_SERVICE  *IpSb;\r
-\r
-  IpSb              = (IP4_SERVICE *) Context;\r
-  IpSb->ActiveEvent = Event;\r
-\r
-  //\r
-  // Request Ip4AutoConfigCallBackDpc as a DPC at TPL_CALLBACK\r
-  //\r
-  NetLibQueueDpc (TPL_CALLBACK, Ip4AutoConfigCallBackDpc, Context);\r
-}\r
-\r
-\r
-/**\r
-  Start the auto configuration for this IP service instance.\r
-  It will locates the EFI_IP4_CONFIG_PROTOCOL, then start the\r
-  auto configuration.\r
-\r
-  @param  IpSb                   The IP4 service instance to configure\r
-\r
-  @retval EFI_SUCCESS            The auto configuration is successfull started\r
-  @retval Others                 Failed to start auto configuration.\r
-\r
-**/\r
-EFI_STATUS\r
-Ip4StartAutoConfig (\r
-  IN IP4_SERVICE            *IpSb\r
-  )\r
-{\r
-  EFI_IP4_CONFIG_PROTOCOL   *Ip4Config;\r
-  EFI_STATUS                Status;\r
-\r
-  if (IpSb->State > IP4_SERVICE_UNSTARTED) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  //\r
-  // Create the DoneEvent and ReconfigEvent to call EFI_IP4_CONFIG\r
-  //\r
-  Status = gBS->CreateEvent (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_CALLBACK,\r
-                  Ip4AutoConfigCallBack,\r
-                  IpSb,\r
-                  &IpSb->DoneEvent\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  Status = gBS->CreateEvent (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                  Ip4AutoConfigCallBack,\r
-                  IpSb,\r
-                  &IpSb->ReconfigEvent\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto CLOSE_DONE_EVENT;\r
-  }\r
-\r
-  //\r
-  // Open the EFI_IP4_CONFIG protocol then start auto configure\r
-  //\r
-  Status = gBS->OpenProtocol (\r
-                  IpSb->Controller,\r
-                  &gEfiIp4ConfigProtocolGuid,\r
-                  (VOID **) &Ip4Config,\r
-                  IpSb->Image,\r
-                  IpSb->Controller,\r
-                  EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    Status = EFI_UNSUPPORTED;\r
-    goto CLOSE_RECONFIG_EVENT;\r
-  }\r
-\r
-  Status = Ip4Config->Start (Ip4Config, IpSb->DoneEvent, IpSb->ReconfigEvent);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    gBS->CloseProtocol (\r
-           IpSb->Controller,\r
-           &gEfiIp4ConfigProtocolGuid,\r
-           IpSb->Image,\r
-           IpSb->Controller\r
-           );\r
-\r
-    goto CLOSE_RECONFIG_EVENT;\r
-  }\r
-\r
-  IpSb->Ip4Config = Ip4Config;\r
-  IpSb->State     = IP4_SERVICE_STARTED;\r
-  return Status;\r
-\r
-CLOSE_RECONFIG_EVENT:\r
-  gBS->CloseEvent (IpSb->ReconfigEvent);\r
-  IpSb->ReconfigEvent = NULL;\r
-\r
-CLOSE_DONE_EVENT:\r
-  gBS->CloseEvent (IpSb->DoneEvent);\r
-  IpSb->DoneEvent = NULL;\r
-\r
-  return Status;\r
-}\r
-\r
-\r
 /**\r
   Intiialize the IP4_PROTOCOL structure to the unconfigured states.\r
 \r
   @param  IpSb                   The IP4 service instance.\r
   @param  IpInstance             The IP4 child instance.\r
 \r
-  @return None\r
-\r
 **/\r
 VOID\r
 Ip4InitProtocol (\r
-  IN IP4_SERVICE            *IpSb,\r
-  IN IP4_PROTOCOL           *IpInstance\r
+  IN     IP4_SERVICE            *IpSb,\r
+  IN OUT IP4_PROTOCOL           *IpInstance\r
   )\r
 {\r
   ASSERT ((IpSb != NULL) && (IpInstance != NULL));\r
@@ -511,6 +550,7 @@ Ip4InitProtocol (
   IpInstance->Signature = IP4_PROTOCOL_SIGNATURE;\r
   CopyMem (&IpInstance->Ip4Proto, &mEfiIp4ProtocolTemplete, sizeof (IpInstance->Ip4Proto));\r
   IpInstance->State     = IP4_STATE_UNCONFIGED;\r
+  IpInstance->InDestroy   = FALSE;\r
   IpInstance->Service   = IpSb;\r
 \r
   InitializeListHead (&IpInstance->Link);\r
@@ -530,8 +570,8 @@ Ip4InitProtocol (
   for the first time. The caller should validate the configuration\r
   before deliver them to it. It also don't do configure NULL.\r
 \r
-  @param  IpInstance             The IP4 child to configure.\r
-  @param  Config                 The configure data.\r
+  @param[in, out]  IpInstance         The IP4 child to configure.\r
+  @param[in]       Config             The configure data.\r
 \r
   @retval EFI_SUCCESS            The IP4 child is successfully configured.\r
   @retval EFI_DEVICE_ERROR       Failed to free the pending transive or to\r
@@ -540,12 +580,14 @@ Ip4InitProtocol (
                                  address, but the default address hasn't been\r
                                  configured. The IP4 child doesn't need to be\r
                                  reconfigured when default address is configured.\r
+  @retval EFI_OUT_OF_RESOURCES   No more memory space is available.\r
+  @retval other                  Other error occurs.\r
 \r
 **/\r
 EFI_STATUS\r
 Ip4ConfigProtocol (\r
-  IN  IP4_PROTOCOL        *IpInstance,\r
-  IN  EFI_IP4_CONFIG_DATA *Config\r
+  IN OUT IP4_PROTOCOL         *IpInstance,\r
+  IN     EFI_IP4_CONFIG_DATA  *Config\r
   )\r
 {\r
   IP4_SERVICE               *IpSb;\r
@@ -553,9 +595,14 @@ Ip4ConfigProtocol (
   EFI_STATUS                Status;\r
   IP4_ADDR                  Ip;\r
   IP4_ADDR                  Netmask;\r
+  EFI_ARP_PROTOCOL          *Arp;\r
+  EFI_IP4_CONFIG2_PROTOCOL  *Ip4Config2;\r
+  EFI_IP4_CONFIG2_POLICY    Policy;\r
 \r
   IpSb = IpInstance->Service;\r
 \r
+  Ip4Config2  = NULL;\r
+\r
   //\r
   // User is changing packet filters. It must be stopped\r
   // before the station address can be changed.\r
@@ -631,14 +678,26 @@ Ip4ConfigProtocol (
 \r
   } else {\r
     //\r
-    // Use the default address. If the default configuration hasn't\r
-    // been started, start it.\r
+    // Use the default address. Check the state.\r
     //\r
     if (IpSb->State == IP4_SERVICE_UNSTARTED) {\r
-      Status = Ip4StartAutoConfig (IpSb);\r
-\r
-      if (EFI_ERROR (Status)) {\r
-        goto ON_ERROR;\r
+      //\r
+      // Trigger the EFI_IP4_CONFIG2_PROTOCOL to retrieve the \r
+      // default IPv4 address if it is not available yet.\r
+      //\r
+      Policy = IpSb->Ip4Config2Instance.Policy;\r
+      if (Policy != Ip4Config2PolicyDhcp) {\r
+        Ip4Config2 = &IpSb->Ip4Config2Instance.Ip4Config2;\r
+        Policy = Ip4Config2PolicyDhcp;\r
+        Status= Ip4Config2->SetData (\r
+                              Ip4Config2,\r
+                              Ip4Config2DataTypePolicy,\r
+                              sizeof (EFI_IP4_CONFIG2_POLICY),\r
+                              &Policy\r
+                              );\r
+        if (EFI_ERROR (Status)) {\r
+          goto ON_ERROR;\r
+        }\r
       }\r
     }\r
 \r
@@ -657,6 +716,21 @@ 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
+      Ip4FreeInterface (IpIf, IpInstance);\r
+      goto ON_ERROR;\r
+    }\r
+  }\r
   InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink);\r
 \r
   CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));\r
@@ -683,10 +757,10 @@ ON_ERROR:
 /**\r
   Clean up the IP4 child, release all the resources used by it.\r
 \r
-  @param  IpInstance             The IP4 child to clean up.\r
+  @param[in]  IpInstance         The IP4 child to clean up.\r
 \r
-  @retval EFI_SUCCESS            The IP4 child is cleaned up\r
-  @retval EFI_DEVICE_ERROR       Some resources failed to be released\r
+  @retval EFI_SUCCESS            The IP4 child is cleaned up.\r
+  @retval EFI_DEVICE_ERROR       Some resources failed to be released.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -713,6 +787,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
@@ -727,13 +809,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
@@ -747,81 +829,48 @@ Ip4CleanProtocol (
 \r
 \r
 /**\r
-  Validate that Ip/Netmask pair is OK to be used as station\r
-  address. Only continuous netmasks are supported. and check\r
-  that StationAddress is a unicast address on the newtwork.\r
-\r
-  @param  Ip                     The IP address to validate\r
-  @param  Netmask                The netmaks of the IP\r
-\r
-  @retval TRUE                   The Ip/Netmask pair is valid\r
-  @retval FALSE                  The\r
-\r
-**/\r
-BOOLEAN\r
-Ip4StationAddressValid (\r
-  IN IP4_ADDR               Ip,\r
-  IN IP4_ADDR               Netmask\r
-  )\r
-{\r
-  IP4_ADDR                  NetBrdcastMask;\r
-  INTN                      Len;\r
-  INTN                      Type;\r
-\r
-  //\r
-  // Only support the station address with 0.0.0.0/0 to enable DHCP client.\r
-  //\r
-  if (Netmask == IP4_ALLZERO_ADDRESS) {\r
-    return (BOOLEAN) (Ip == IP4_ALLZERO_ADDRESS);\r
-  }\r
-\r
-  //\r
-  // Only support the continuous net masks\r
-  //\r
-  if ((Len = NetGetMaskLength (Netmask)) == IP4_MASK_NUM) {\r
-    return FALSE;\r
-  }\r
-\r
-  //\r
-  // Station address can't be class D or class E address\r
-  //\r
-  if ((Type = NetGetIpClass (Ip)) > IP4_ADDR_CLASSC) {\r
-    return FALSE;\r
-  }\r
-\r
-  //\r
-  // Station address can't be subnet broadcast/net broadcast address\r
-  //\r
-  if ((Ip == (Ip & Netmask)) || (Ip == (Ip | ~Netmask))) {\r
-    return FALSE;\r
-  }\r
-\r
-  NetBrdcastMask = gIp4AllMasks[MIN (Len, Type << 3)];\r
-\r
-  if (Ip == (Ip | ~NetBrdcastMask)) {\r
-    return FALSE;\r
-  }\r
-\r
-  return TRUE;\r
-}\r
-\r
-\r
-/**\r
-  Configure the EFI_IP4_PROTOCOL instance. If IpConfigData is NULL,\r
-  the instance is cleaned up. If the instance hasn't been configure\r
-  before, it will be initialized. Otherwise, the filter setting of\r
-  the instance is updated.\r
-\r
-  @param  This                   The IP4 child to configure\r
-  @param  IpConfigData           The configuration to apply. If NULL, clean it up.\r
-\r
-  @retval EFI_INVALID_PARAMETER  The parameter is invalid\r
-  @retval EFI_NO_MAPPING         The default address hasn't been configured and the\r
-                                 instance wants to use it.\r
-  @retval EFI_SUCCESS            The instance is configured.\r
+  Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\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
+  instance. Once the parameters have been reset (by calling this function with\r
+  IpConfigData set to NULL), no more traffic can be sent or received until these\r
+  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
+  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
+  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
+\r
+  @param[in]  This              Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  IpConfigData      Pointer to the EFI IPv4 Protocol configuration data structure.\r
+\r
+  @retval EFI_SUCCESS           The driver instance was successfully opened.\r
+  @retval EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                RARP, etc.) is not finished yet.\r
+  @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
+  @retval EFI_UNSUPPORTED       One or more of the following conditions is TRUE:\r
+                                A configuration protocol (DHCP, BOOTP, RARP, etc.) could\r
+                                not be located when clients choose to use the default IPv4\r
+                                address. This EFI IPv4 Protocol implementation does not\r
+                                support this requested filter or timeout setting.\r
+  @retval EFI_OUT_OF_RESOURCES  The EFI IPv4 Protocol driver instance data could not be allocated.\r
+  @retval EFI_ALREADY_STARTED   The interface is already open and must be stopped before the\r
+                                IPv4 address or subnet mask can be changed. The interface must\r
+                                also be stopped when switching to/from raw packet mode.\r
+  @retval EFI_DEVICE_ERROR      An unexpected system or network error occurred. The EFI IPv4\r
+                                Protocol driver instance is not opened.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 EfiIp4Configure (\r
@@ -851,14 +900,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
@@ -899,7 +940,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
@@ -913,8 +955,7 @@ EfiIp4Configure (
     Status = Ip4CleanProtocol (IpInstance);\r
 \r
     //\r
-    // Don't change the state if it is DESTORY, consider the following\r
-    // valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped,\r
+    // Consider the following 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
     //\r
@@ -929,11 +970,6 @@ EfiIp4Configure (
   //\r
   Ip4ServiceConfigMnp (IpInstance->Service, FALSE);\r
 \r
-  //\r
-  // Update the variable data.\r
-  //\r
-  Ip4SetVariableData (IpInstance->Service);\r
-\r
 ON_EXIT:\r
   gBS->RestoreTPL (OldTpl);\r
   return Status;\r
@@ -945,13 +981,13 @@ ON_EXIT:
   Change the IP4 child's multicast setting. The caller\r
   should make sure that the parameters is valid.\r
 \r
-  @param  IpInstance             The IP4 child to change the setting.\r
-  @param  JoinFlag               TRUE to join the group, otherwise leave it\r
-  @param  GroupAddress           The target group address\r
+  @param[in]  IpInstance             The IP4 child to change the setting.\r
+  @param[in]  JoinFlag               TRUE to join the group, otherwise leave it.\r
+  @param[in]  GroupAddress           The target group address.\r
 \r
-  @retval EFI_ALREADY_STARTED    Want to join the group, but already a member of it\r
+  @retval EFI_ALREADY_STARTED    Want to join the group, but already a member of it.\r
   @retval EFI_OUT_OF_RESOURCES   Failed to allocate some resources.\r
-  @retval EFI_DEVICE_ERROR       Failed to set the group configuraton\r
+  @retval EFI_DEVICE_ERROR       Failed to set the group configuraton.\r
   @retval EFI_SUCCESS            Successfully updated the group setting.\r
   @retval EFI_NOT_FOUND          Try to leave the group which it isn't a member.\r
 \r
@@ -973,6 +1009,10 @@ Ip4Groups (
   // host byte order\r
   //\r
   if (JoinFlag) {\r
+    //\r
+    // When JoinFlag is TRUE, GroupAddress shouldn't be NULL.\r
+    //\r
+    ASSERT (GroupAddress != NULL);\r
     CopyMem (&Group, GroupAddress, sizeof (IP4_ADDR));\r
 \r
     for (Index = 0; Index < IpInstance->GroupCount; Index++) {\r
@@ -988,12 +1028,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
@@ -1008,8 +1048,8 @@ Ip4Groups (
   // is decreamented each time an address is removed..\r
   //\r
   for (Index = IpInstance->GroupCount; Index > 0 ; Index--) {\r
+    ASSERT (IpInstance->Groups != NULL);\r
     Group = IpInstance->Groups[Index - 1];\r
-\r
     if ((GroupAddress == NULL) || EFI_IP4_EQUAL (&Group, GroupAddress)) {\r
       if (EFI_ERROR (Ip4LeaveGroup (IpInstance, NTOHL (Group)))) {\r
         return EFI_DEVICE_ERROR;\r
@@ -1021,7 +1061,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
@@ -1036,21 +1076,35 @@ Ip4Groups (
 \r
 \r
 /**\r
-  Change the IP4 child's multicast setting. If JoinFlag is true,\r
-  the child wants to join the group. Otherwise it wants to leave\r
-  the group. If JoinFlag is false, and GroupAddress is NULL,\r
-  it will leave all the groups which is a member.\r
-\r
-  @param  This                   The IP4 child to change the setting.\r
-  @param  JoinFlag               TRUE to join the group, otherwise leave it.\r
-  @param  GroupAddress           The target group address\r
-\r
-  @retval EFI_INVALID_PARAMETER  The parameters are invalid\r
-  @retval EFI_SUCCESS            The group setting has been changed.\r
-  @retval Otherwise              It failed to change the setting.\r
+  Joins and leaves multicast groups.\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
+  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
+  @param[in]  JoinFlag              Set to TRUE to join the multicast group session and FALSE to leave.\r
+  @param[in]  GroupAddress          Pointer to the IPv4 multicast address.\r
+\r
+  @retval EFI_SUCCESS           The operation completed successfully.\r
+  @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:\r
+                                - This is NULL.\r
+                                - JoinFlag is TRUE and GroupAddress is NULL.\r
+                                - GroupAddress is not NULL and *GroupAddress is\r
+                                not a multicast IPv4 address.\r
+  @retval EFI_NOT_STARTED       This instance has not been started.\r
+  @retval EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                RARP, etc.) is not finished yet.\r
+  @retval EFI_OUT_OF_RESOURCES  System resources could not be allocated.\r
+  @retval EFI_UNSUPPORTED       This EFI IPv4 Protocol implementation does not support multicast groups.\r
+  @retval EFI_ALREADY_STARTED   The group address is already in the group table (when\r
+                                JoinFlag is TRUE).\r
+  @retval EFI_NOT_FOUND         The group address is not in the group table (when JoinFlag is FALSE).\r
+  @retval EFI_DEVICE_ERROR      An unexpected system or network error occurred.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 EfiIp4Groups (\r
@@ -1098,21 +1152,55 @@ ON_EXIT:
 \r
 \r
 /**\r
-  Modify the IP child's route table. Each instance has its own\r
-  route table.\r
-\r
-  @param  This                   The IP4 child to modify the route\r
-  @param  DeleteRoute            TRUE to delete the route, otherwise add it\r
-  @param  SubnetAddress          The destination network\r
-  @param  SubnetMask             The destination network's mask\r
-  @param  GatewayAddress         The next hop address.\r
-\r
-  @retval EFI_INVALID_PARAMETER  The parameter is invalid.\r
-  @retval EFI_SUCCESS            The route table is successfully modified.\r
-  @retval Others                 Failed to modify the route table\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
+  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
+  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
+  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
+  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
+  copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its\r
+  instances. As a result, client modification to the routing table will be lost.\r
+\r
+  @param[in]  This                   Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  DeleteRoute            Set to TRUE to delete this route from the routing table. Set to\r
+                                     FALSE to add this route to the routing table. SubnetAddress\r
+                                     and SubnetMask are used as the key to each route entry.\r
+  @param[in]  SubnetAddress          The address of the subnet that needs to be routed.\r
+  @param[in]  SubnetMask             The subnet mask of SubnetAddress.\r
+  @param[in]  GatewayAddress         The unicast gateway IPv4 address for this route.\r
+\r
+  @retval EFI_SUCCESS            The operation completed successfully.\r
+  @retval EFI_NOT_STARTED        The driver instance has not been started.\r
+  @retval EFI_NO_MAPPING         When using the default address, configuration (DHCP, BOOTP,\r
+                                 RARP, etc.) is not finished yet.\r
+  @retval EFI_INVALID_PARAMETER  One or more of the following conditions is TRUE:\r
+                                 - This is NULL.\r
+                                 - SubnetAddress is NULL.\r
+                                 - SubnetMask is NULL.\r
+                                 - GatewayAddress is NULL.\r
+                                 - *SubnetAddress is not a valid subnet address.\r
+                                 - *SubnetMask is not a valid subnet mask.\r
+                                 - *GatewayAddress is not a valid unicast IPv4 address.\r
+  @retval EFI_OUT_OF_RESOURCES   Could not add the entry to the routing table.\r
+  @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
-STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 EfiIp4Routes (\r
@@ -1192,20 +1280,20 @@ ON_EXIT:
 \r
 /**\r
   Check whether the user's token or event has already\r
-  been enqueue on IP4's list.\r
+  been enqueued on IP4's list.\r
 \r
-  @param  Map                    The container of either user's transmit or receive\r
-                                 token.\r
-  @param  Item                   Current item to check against\r
-  @param  Context                The Token to check againist.\r
+  @param[in]  Map                    The container of either user's transmit or receive\r
+                                     token.\r
+  @param[in]  Item                   Current item to check against.\r
+  @param[in]  Context                The Token to check againist.\r
 \r
-  @retval EFI_ACCESS_DENIED      The token or event has already been enqueued in IP\r
+  @retval EFI_ACCESS_DENIED      The token or event has already been enqueued in IP.\r
   @retval EFI_SUCCESS            The current item isn't the same token/event as the\r
                                  context.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
+EFIAPI\r
 Ip4TokenExist (\r
   IN NET_MAP                *Map,\r
   IN NET_MAP_ITEM           *Item,\r
@@ -1225,23 +1313,23 @@ Ip4TokenExist (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Validate the user's token against current station address.\r
 \r
-  @param  Token                  User's token to validate\r
-  @param  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
-STATIC\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
@@ -1259,20 +1347,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
@@ -1280,6 +1355,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
@@ -1294,11 +1373,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
@@ -1360,13 +1460,11 @@ Ip4TxTokenValid (
   are bound together. Check the comments in Ip4Output for information\r
   about IP fragmentation.\r
 \r
-  @param  Context                The token's wrap\r
-\r
-  @return None\r
+  @param[in]  Context                The token's wrap.\r
 \r
 **/\r
-STATIC\r
 VOID\r
+EFIAPI\r
 Ip4FreeTxToken (\r
   IN VOID                   *Context\r
   )\r
@@ -1376,6 +1474,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
@@ -1392,10 +1497,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
@@ -1403,15 +1508,12 @@ Ip4FreeTxToken (
   The callback function to Ip4Output to update the transmit status.\r
 \r
   @param  Ip4Instance            The Ip4Instance that request the transmit.\r
-  @param  Packet                 The user's transmit request\r
-  @param  IoStatus               The result of the transmission\r
-  @param  Flag                   Not used during transmission\r
+  @param  Packet                 The user's transmit request.\r
+  @param  IoStatus               The result of the transmission.\r
+  @param  Flag                   Not used during transmission.\r
   @param  Context                The token's wrap.\r
 \r
-  @return None\r
-\r
 **/\r
-STATIC\r
 VOID\r
 Ip4OnPacketSent (\r
   IP4_PROTOCOL              *Ip4Instance,\r
@@ -1444,21 +1546,34 @@ Ip4OnPacketSent (
 \r
 \r
 /**\r
-  Transmit the user's data asynchronously. When transmission\r
-  completed,the Token's status is updated and its event signalled.\r
-\r
-  @param  This                   The IP4 child instance\r
-  @param  Token                  The user's transmit token, which contains user's\r
-                                 data, the result and an event to signal when\r
-                                 completed.\r
-\r
-  @retval EFI_INVALID_PARAMETER  The parameter is invalid.\r
-  @retval EFI_NOT_STARTED        The IP4 child hasn't been started.\r
-  @retval EFI_SUCCESS            The user's data has been successfully enqueued\r
-                                 for transmission.\r
+  Places outgoing data packets into the transmit queue.\r
+\r
+  The Transmit() function places a sending request in the transmit queue of this\r
+  EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some\r
+  errors occur, the event in the token will be signaled and the status is updated.\r
+\r
+  @param[in]  This  Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  Token Pointer to the transmit token.\r
+\r
+  @retval  EFI_SUCCESS           The data has been queued for transmission.\r
+  @retval  EFI_NOT_STARTED       This instance has not been started.\r
+  @retval  EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                 RARP, etc.) is not finished yet.\r
+  @retval  EFI_INVALID_PARAMETER One or more pameters are invalid.\r
+  @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
+  @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
+                                 short to transmit.\r
+  @retval  EFI_BAD_BUFFER_SIZE   The length of the IPv4 header + option length + total data length is\r
+                                 greater than MTU (or greater than the maximum packet size if\r
+                                 Token.Packet.TxData.OverrideData.\r
+                                 DoNotFragment is TRUE).\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 EfiIp4Transmit (\r
@@ -1479,6 +1594,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
@@ -1504,7 +1623,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
@@ -1524,37 +1643,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 (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
+    RawHdrLen = (UINT8) (RawHdrLen & 0x0f);\r
+    if (RawHdrLen < 5) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto ON_EXIT;\r
+    }\r
+\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
-  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
+    GateWay = IP4_ALLZERO_ADDRESS;\r
 \r
-    CopyMem (&Head.Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
-    CopyMem (&GateWay, &Override->GatewayAddress, sizeof (IP4_ADDR));\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
-    Head.Src = NTOHL (Head.Src);\r
-    GateWay  = NTOHL (GateWay);\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
@@ -1564,7 +1735,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
@@ -1583,7 +1754,7 @@ EfiIp4Transmit (
                         );\r
 \r
   if (Wrap->Packet == NULL) {\r
-    gBS->FreePool (Wrap);\r
+    FreePool (Wrap);\r
     goto ON_EXIT;\r
   }\r
 \r
@@ -1595,6 +1766,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
@@ -1610,8 +1788,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
@@ -1619,9 +1797,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
@@ -1629,22 +1822,37 @@ ON_EXIT:
 \r
 \r
 /**\r
-  Receive a packet for the upper layer. If there are packets\r
-  pending on the child's receive queue, the receive request\r
-  will be fulfilled immediately. Otherwise, the request is\r
-  enqueued. When receive request is completed, the status in\r
-  the Token is updated and its event is signalled.\r
-\r
-  @param  This                   The IP4 child to receive packet.\r
-  @param  Token                  The user's receive token\r
-\r
-  @retval EFI_INVALID_PARAMETER  The token is invalid.\r
-  @retval EFI_NOT_STARTED        The IP4 child hasn't been started\r
-  @retval EFI_ACCESS_DENIED      The token or event is already queued.\r
-  @retval EFI_SUCCESS            The receive request has been issued.\r
+  Places a receiving request into the receiving queue.\r
+\r
+  The Receive() function places a completion token into the receive packet queue.\r
+  This function is always asynchronous.\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
+  is signaled.\r
+\r
+  @param[in]  This  Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  Token Pointer to a token that is associated with the receive data descriptor.\r
+\r
+  @retval EFI_SUCCESS           The receive completion token was cached.\r
+  @retval EFI_NOT_STARTED       This EFI IPv4 Protocol instance has not been started.\r
+  @retval EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP, RARP, etc.)\r
+                                is not finished yet.\r
+  @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
+                                - This is NULL.\r
+                                - Token is NULL.\r
+                                - Token.Event is NULL.\r
+  @retval EFI_OUT_OF_RESOURCES  The receive completion token could not be queued due to a lack of system\r
+                                resources (usually memory).\r
+  @retval EFI_DEVICE_ERROR      An unexpected system or network error occurred.\r
+                                The EFI IPv4 Protocol instance has been reset to startup defaults.\r
+                                EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already\r
+                                in the receive queue.\r
+  @retval EFI_NOT_READY         The receive request could not be queued because the receive queue is full.\r
+  @retval EFI_ICMP_ERROR        An ICMP error packet was received.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 EfiIp4Receive (\r
@@ -1697,7 +1905,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
@@ -1714,16 +1922,16 @@ ON_EXIT:
   Because Ip4CancelPacket and other functions are all called in\r
   line, so, after Ip4CancelPacket returns, the Item has been freed.\r
 \r
-  @param  Map                    The IP4 child's transmit queue\r
-  @param  Item                   The current transmitted packet to test.\r
-  @param  Context                The user's token to cancel.\r
+  @param[in]  Map                The IP4 child's transmit queue.\r
+  @param[in]  Item               The current transmitted packet to test.\r
+  @param[in]  Context            The user's token to cancel.\r
 \r
   @retval EFI_SUCCESS            Continue to check the next Item.\r
   @retval EFI_ABORTED            The user's Token (Token != NULL) is cancelled.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
+EFIAPI\r
 Ip4CancelTxTokens (\r
   IN NET_MAP                *Map,\r
   IN NET_MAP_ITEM           *Item,\r
@@ -1768,9 +1976,9 @@ Ip4CancelTxTokens (
   Cancel the receive request. This is quiet simple, because\r
   it is only enqueued in our local receive map.\r
 \r
-  @param  Map                    The IP4 child's receive queue\r
-  @param  Item                   Current receive request to cancel.\r
-  @param  Context                The user's token to cancel\r
+  @param[in]  Map                The IP4 child's receive queue.\r
+  @param[in]  Item               Current receive request to cancel.\r
+  @param[in]  Context            The user's token to cancel.\r
 \r
   @retval EFI_SUCCESS            Continue to check the next receive request on the\r
                                  queue.\r
@@ -1778,8 +1986,8 @@ Ip4CancelTxTokens (
                                  cancelled.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
+EFIAPI\r
 Ip4CancelRxTokens (\r
   IN NET_MAP                *Map,\r
   IN NET_MAP_ITEM           *Item,\r
@@ -1813,13 +2021,13 @@ Ip4CancelRxTokens (
 /**\r
   Cancel the user's receive/transmit request.\r
 \r
-  @param  IpInstance             The IP4 child\r
-  @param  Token                  The token to cancel. If NULL, all token will be\r
+  @param[in]  IpInstance         The IP4 child.\r
+  @param[in]  Token              The token to cancel. If NULL, all token will be\r
                                  cancelled.\r
 \r
-  @retval EFI_SUCCESS            The token is cancelled\r
+  @retval EFI_SUCCESS            The token is cancelled.\r
   @retval EFI_NOT_FOUND          The token isn't found on either the\r
-                                 transmit/receive queue\r
+                                 transmit/receive queue.\r
   @retval EFI_DEVICE_ERROR       Not all token is cancelled when Token is NULL.\r
 \r
 **/\r
@@ -1855,7 +2063,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
@@ -1887,21 +2095,34 @@ Ip4Cancel (
 \r
 \r
 /**\r
-  Cancel the queued receive/transmit requests. If Token is NULL,\r
-  all the queued requests will be cancelled. It just validate\r
-  the parameter then pass them to Ip4Cancel.\r
-\r
-  @param  This                   The IP4 child to cancel the request\r
-  @param  Token                  The token to cancel, if NULL, cancel all.\r
-\r
-  @retval EFI_INVALID_PARAMETER  This is NULL\r
-  @retval EFI_NOT_STARTED        The IP4 child hasn't been configured.\r
-  @retval EFI_NO_MAPPING         The IP4 child is configured to use the default,\r
-                                 but the default address hasn't been acquired.\r
-  @retval EFI_SUCCESS            The Token is cancelled.\r
+  Abort an asynchronous transmit or receive request.\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
+  be signaled. If the token is not in one of the queues, which usually means the\r
+  asynchronous operation has completed, this function will not signal the token\r
+  and EFI_NOT_FOUND is returned.\r
+\r
+  @param[in]  This  Pointer to the EFI_IP4_PROTOCOL instance.\r
+  @param[in]  Token Pointer to a token that has been issued by\r
+                    EFI_IP4_PROTOCOL.Transmit() or\r
+                    EFI_IP4_PROTOCOL.Receive(). If NULL, all pending\r
+                    tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is\r
+                    defined in EFI_IP4_PROTOCOL.Transmit().\r
+\r
+  @retval EFI_SUCCESS           The asynchronous I/O request was aborted and\r
+                                Token.->Event was signaled. When Token is NULL, all\r
+                                pending requests were aborted and their events were signaled.\r
+  @retval EFI_INVALID_PARAMETER This is NULL.\r
+  @retval EFI_NOT_STARTED       This instance has not been started.\r
+  @retval EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                RARP, etc.) is not finished yet.\r
+  @retval EFI_NOT_FOUND         When Token is not NULL, the asynchronous I/O request was\r
+                                not found in the transmit or receive queue. It has either completed\r
+                                or was not issued by Transmit() and Receive().\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 EfiIp4Cancel (\r
@@ -1940,16 +2161,32 @@ ON_EXIT:
 \r
 \r
 /**\r
-  Poll the network stack. The EFI network stack is poll based. There\r
-  is no interrupt support for the network interface card.\r
-\r
-  @param  This                   The IP4 child to poll through\r
-\r
-  @retval EFI_INVALID_PARAMETER  The parameter is invalid\r
-  @retval EFI_NOT_STARTED        The IP4 child hasn't been configured.\r
+  Polls for incoming data packets and processes outgoing data packets.\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
+  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
+  experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function\r
+  more often.\r
+\r
+  @param[in]  This               Pointer to the EFI_IP4_PROTOCOL instance.\r
+\r
+  @retval  EFI_SUCCESS           Incoming or outgoing data was processed.\r
+  @retval  EFI_NOT_STARTED       This EFI IPv4 Protocol instance has not been started.\r
+  @retval  EFI_NO_MAPPING        When using the default address, configuration (DHCP, BOOTP,\r
+                                 RARP, etc.) is not finished yet.\r
+  @retval  EFI_INVALID_PARAMETER This is NULL.\r
+  @retval  EFI_DEVICE_ERROR      An unexpected system or network error occurred.\r
+  @retval  EFI_NOT_READY         No incoming or outgoing data is processed.\r
+  @retval  EFI_TIMEOUT           Data was dropped out of the transmit and/or receive queue.\r
+                                 Consider increasing the polling rate.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 EfiIp4Poll (\r
@@ -1978,34 +2215,22 @@ EfiIp4Poll (
   return Mnp->Poll (Mnp);\r
 }\r
 \r
-EFI_IP4_PROTOCOL\r
-mEfiIp4ProtocolTemplete = {\r
-  EfiIp4GetModeData,\r
-  EfiIp4Configure,\r
-  EfiIp4Groups,\r
-  EfiIp4Routes,\r
-  EfiIp4Transmit,\r
-  EfiIp4Receive,\r
-  EfiIp4Cancel,\r
-  EfiIp4Poll\r
-};\r
-\r
-\r
 /**\r
   Decrease the life of the transmitted packets. If it is\r
   decreased to zero, cancel the packet. This function is\r
-  called by Ip4packetTimerTicking which time out both the\r
+  called by Ip4PacketTimerTicking which time out both the\r
   received-but-not-delivered and transmitted-but-not-recycle\r
   packets.\r
 \r
-  @param  Map                    The IP4 child's transmit map.\r
-  @param  Item                   Current transmitted packet\r
-  @param  Context                Not used.\r
+  @param[in]  Map                    The IP4 child's transmit map.\r
+  @param[in]  Item                   Current transmitted packet.\r
+  @param[in]  Context                Not used.\r
 \r
-  @retval EFI_SUCCESS            Always returns EFI_SUCCESS\r
+  @retval EFI_SUCCESS            Always returns EFI_SUCCESS.\r
 \r
 **/\r
 EFI_STATUS\r
+EFIAPI\r
 Ip4SentPacketTicking (\r
   IN NET_MAP                *Map,\r
   IN NET_MAP_ITEM           *Item,\r
@@ -2024,17 +2249,13 @@ Ip4SentPacketTicking (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
-  The heart beat timer of IP4 service instance. It times out\r
-  all of its IP4 children's received-but-not-delivered and\r
-  transmitted-but-not-recycle packets, and provides time input\r
-  for its IGMP protocol.\r
-\r
-  @param  Event                  The IP4 service instance's heart beat timer.\r
-  @param  Context                The IP4 service instance.\r
+  This heart beat timer of IP4 service instance times out all of its IP4 children's \r
+  received-but-not-delivered and transmitted-but-not-recycle packets, and provides \r
+  time input for its IGMP protocol.\r
 \r
-  @return None\r
+  @param[in]  Event                  The IP4 service instance's heart beat timer.\r
+  @param[in]  Context                The IP4 service instance.\r
 \r
 **/\r
 VOID\r
@@ -2048,7 +2269,64 @@ Ip4TimerTicking (
 \r
   IpSb = (IP4_SERVICE *) Context;\r
   NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
-\r
+  \r
   Ip4PacketTimerTicking (IpSb);\r
   Ip4IgmpTicking (IpSb);\r
 }\r
+\r
+/**\r
+  This dedicated timer is used to poll underlying network media status. In case \r
+  of cable swap or wireless network switch, a new round auto configuration will \r
+  be initiated. The timer will signal the IP4 to run DHCP configuration again. \r
+  IP4 driver will free old IP address related resource, such as route table and \r
+  Interface, then initiate a DHCP process to acquire new IP, eventually create \r
+  route table for new IP address.\r
+\r
+  @param[in]  Event                  The IP4 service instance's heart beat timer.\r
+  @param[in]  Context                The IP4 service instance.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+Ip4TimerReconfigChecking (\r
+  IN EFI_EVENT              Event,\r
+  IN VOID                   *Context\r
+  )\r
+{\r
+  IP4_SERVICE               *IpSb;\r
+  BOOLEAN                   OldMediaPresent;\r
+  EFI_STATUS                Status;\r
+  EFI_SIMPLE_NETWORK_MODE   SnpModeData;\r
+\r
+  IpSb = (IP4_SERVICE *) Context;\r
+  NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
+  \r
+  OldMediaPresent = IpSb->MediaPresent;\r
+\r
+  //\r
+  // Get fresh mode data from MNP, since underlying media status may change. \r
+  // Here, it needs to mention that the MediaPresent can also be checked even if \r
+  // EFI_NOT_STARTED returned while this MNP child driver instance isn't configured.\r
+  //\r
+  Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &SnpModeData);\r
+  if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {\r
+    return;\r
+  }\r
+\r
+  IpSb->MediaPresent = SnpModeData.MediaPresent;\r
+  //\r
+  // Media transimit Unpresent to Present means new link movement is detected.\r
+  //\r
+  if (!OldMediaPresent && IpSb->MediaPresent && (IpSb->Ip4Config2Instance.Policy == Ip4Config2PolicyDhcp)) {\r
+    //\r
+    // Signal the IP4 to run the dhcp configuration again. IP4 driver will free\r
+    // old IP address related resource, such as route table and Interface, then \r
+    // initiate a DHCP round to acquire new IP, eventually \r
+    // create route table for new IP address.\r
+    //\r
+    if (IpSb->ReconfigEvent != NULL) {\r
+      Status = gBS->SignalEvent (IpSb->ReconfigEvent);\r
+      DispatchDpc ();\r
+    }\r
+  }\r
+}\r