]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Common.c
MdeModulePkg: Update IP4 stack to support point-to-point link with 31-bit mask.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Common.c
index 89a371400b872894b092f6f2f983a9da334f243a..b582ccf13f66836768ed6e7e57865ef6af269e3e 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 \r
-Copyright (c) 2005 - 2006, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2005 - 2017, 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,14 +9,6 @@ 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
-Module Name:\r
-\r
-  Ip4Common.c\r
-\r
-Abstract:\r
-\r
-\r
 **/\r
 \r
 #include "Ip4Impl.h"\r
@@ -26,8 +18,8 @@ Abstract:
   Return the cast type (Unicast/Boradcast) specific to an\r
   interface. All the addresses are host byte ordered.\r
 \r
-  @param  IpAddr                The IP address to classify in host byte order\r
-  @param  IpIf                  The interface that IpAddr received from\r
+  @param[in]  IpAddr                The IP address to classify in host byte order\r
+  @param[in]  IpIf                  The interface that IpAddr received from\r
 \r
   @return The cast type of this IP address specific to the interface.\r
   @retval IP4_LOCAL_HOST        The IpAddr equals to the interface's address\r
@@ -63,11 +55,11 @@ Ip4GetNetCast (
   This isn't the same as link layer cast type. For example, DHCP\r
   server may send local broadcast to the local unicast MAC.\r
 \r
-  @param  IpSb                  The IP4 service binding instance that received the\r
-                                packet\r
-  @param  Dst                   The destination address in the packet (host byte\r
-                                order)\r
-  @param  Src                   The source address in the packet (host byte order)\r
+  @param[in]  IpSb                  The IP4 service binding instance that received the\r
+                                    packet\r
+  @param[in]  Dst                   The destination address in the packet (host byte\r
+                                    order)\r
+  @param[in]  Src                   The source address in the packet (host byte order)\r
 \r
   @return The cast type for the Dst, it will return on the first non-promiscuous\r
           cast type to a configured interface. If the packet doesn't match any of\r
@@ -135,8 +127,8 @@ Ip4GetHostCast (
 /**\r
   Find an interface whose configured IP address is Ip.\r
 \r
-  @param  IpSb                  The IP4 service binding instance\r
-  @param  Ip                    The Ip address (host byte order) to find\r
+  @param[in]  IpSb                  The IP4 service binding instance\r
+  @param[in]  Ip                    The Ip address (host byte order) to find\r
 \r
   @return The IP4_INTERFACE point if found, otherwise NULL\r
 \r
@@ -165,8 +157,8 @@ Ip4FindInterface (
 /**\r
   Find an interface that Ip is on that connected network.\r
 \r
-  @param  IpSb                  The IP4 service binding instance\r
-  @param  Ip                    The Ip address (host byte order) to find\r
+  @param[in]  IpSb                  The IP4 service binding instance\r
+  @param[in]  Ip                    The Ip address (host byte order) to find\r
 \r
   @return The IP4_INTERFACE point if found, otherwise NULL\r
 \r
@@ -195,9 +187,9 @@ Ip4FindNet (
 /**\r
   Find an interface of the service with the same Ip/Netmask pair.\r
 \r
-  @param  IpSb                  Ip4 service binding instance\r
-  @param  Ip                    The Ip adress to find (host byte order)\r
-  @param  Netmask               The network to find (host byte order)\r
+  @param[in]  IpSb                  Ip4 service binding instance\r
+  @param[in]  Ip                    The Ip adress to find (host byte order)\r
+  @param[in]  Netmask               The network to find (host byte order)\r
 \r
   @return The IP4_INTERFACE point if found, otherwise NULL\r
 \r
@@ -229,9 +221,9 @@ Ip4FindStationAddress (
   Mnp's McastIpToMac to find the MAC address in stead of\r
   hard code the NIC to be Ethernet.\r
 \r
-  @param  Mnp                   The Mnp instance to get the MAC address.\r
-  @param  Multicast             The multicast IP address to translate.\r
-  @param  Mac                   The buffer to hold the translated address.\r
+  @param[in]  Mnp                   The Mnp instance to get the MAC address.\r
+  @param[in]  Multicast             The multicast IP address to translate.\r
+  @param[out] Mac                   The buffer to hold the translated address.\r
 \r
   @retval EFI_SUCCESS if the multicast IP is successfully translated to a\r
                       multicast MAC address.\r
@@ -257,7 +249,7 @@ Ip4GetMulticastMac (
   In spite of its name, it can also be used to convert from\r
   host to network byte order.\r
 \r
-  @param  Head                  The IP head to convert\r
+  @param[in]  Head                  The IP head to convert\r
 \r
   @return Point to the converted IP head\r
 \r
@@ -278,150 +270,43 @@ Ip4NtohHead (
 \r
 \r
 /**\r
-  Set the Ip4 variable data.\r
-  \r
-  Save the list of all of the IPv4 addresses and subnet masks that are currently\r
-  being used to volatile variable storage.\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  IpSb                  Ip4 service binding instance\r
+  @param[in]  Ip                 The IP address to validate.\r
+  @param[in]  Netmask            The netmaks of the IP.\r
 \r
-  @retval EFI_SUCCESS           Successfully set variable.\r
-  @retval EFI_OUT_OF_RESOURCES  There are not enough resources to set the variable.\r
-  @retval other                 Set variable failed.\r
+  @retval TRUE                   The Ip/Netmask pair is valid.\r
+  @retval FALSE                  The Ip/Netmask pair is invalid.\r
 \r
 **/\r
-EFI_STATUS\r
-Ip4SetVariableData (\r
-  IN IP4_SERVICE  *IpSb\r
+BOOLEAN\r
+Ip4StationAddressValid (\r
+  IN IP4_ADDR               Ip,\r
+  IN IP4_ADDR               Netmask\r
   )\r
 {\r
-  UINT32                 NumConfiguredInstance;\r
-  LIST_ENTRY             *Entry;\r
-  UINTN                  VariableDataSize;\r
-  EFI_IP4_VARIABLE_DATA  *Ip4VariableData;\r
-  EFI_IP4_ADDRESS_PAIR   *Ip4AddressPair;\r
-  IP4_PROTOCOL           *IpInstance;\r
-  CHAR16                 *NewMacString;\r
-  EFI_STATUS             Status;\r
-\r
-  NumConfiguredInstance = 0;\r
-\r
   //\r
-  // Go through the children list to count the configured children.\r
+  // Only support the station address with 0.0.0.0/0 to enable DHCP client.\r
   //\r
-  NET_LIST_FOR_EACH (Entry, &IpSb->Children) {\r
-    IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);\r
-\r
-    if (IpInstance->State == IP4_STATE_CONFIGED) {\r
-      NumConfiguredInstance++;\r
-    }\r
+  if (Netmask == IP4_ALLZERO_ADDRESS) {\r
+    return (BOOLEAN) (Ip == IP4_ALLZERO_ADDRESS);\r
   }\r
 \r
   //\r
-  // Calculate the size of the Ip4VariableData. As there may be no IP child,\r
-  // we should add extra buffer for the address paris only if the number of configured\r
-  // children is more than 1.\r
+  // Only support the continuous net masks\r
   //\r
-  VariableDataSize = sizeof (EFI_IP4_VARIABLE_DATA);\r
-\r
-  if (NumConfiguredInstance > 1) {\r
-    VariableDataSize += sizeof (EFI_IP4_ADDRESS_PAIR) * (NumConfiguredInstance - 1);\r
-  }\r
-\r
-  Ip4VariableData = AllocatePool (VariableDataSize);\r
-  if (Ip4VariableData == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  Ip4VariableData->DriverHandle = IpSb->Image;\r
-  Ip4VariableData->AddressCount = NumConfiguredInstance;\r
-\r
-  Ip4AddressPair = &Ip4VariableData->AddressPairs[0];\r
-\r
-  //\r
-  // Go through the children list to fill the configured children's address pairs.\r
-  //\r
-  NET_LIST_FOR_EACH (Entry, &IpSb->Children) {\r
-    IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);\r
-\r
-    if (IpInstance->State == IP4_STATE_CONFIGED) {\r
-      Ip4AddressPair->InstanceHandle       = IpInstance->Handle;\r
-      EFI_IP4 (Ip4AddressPair->Ip4Address) = NTOHL (IpInstance->Interface->Ip);\r
-      EFI_IP4 (Ip4AddressPair->SubnetMask) = NTOHL (IpInstance->Interface->SubnetMask);\r
-\r
-      Ip4AddressPair++;\r
-    }\r
+  if (NetGetMaskLength (Netmask) == (IP4_MASK_MAX + 1)) {\r
+    return FALSE;\r
   }\r
 \r
   //\r
-  // Get the mac string.\r
+  // Station address can't be class D or class E address\r
   //\r
-  Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &NewMacString);\r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_ERROR;\r
+  if (NetGetIpClass (Ip) > IP4_ADDR_CLASSC) {\r
+    return FALSE;\r
   }\r
 \r
-  if (IpSb->MacString != NULL) {\r
-    //\r
-    // The variable is set already, we're going to update it.\r
-    //\r
-    if (StrCmp (IpSb->MacString, NewMacString) != 0) {\r
-      //\r
-      // The mac address is changed, delete the previous variable first.\r
-      //\r
-      gRT->SetVariable (\r
-             IpSb->MacString,\r
-             &gEfiIp4ServiceBindingProtocolGuid,\r
-             EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-             0,\r
-             NULL\r
-             );\r
-    }\r
-\r
-    gBS->FreePool (IpSb->MacString);\r
-  }\r
-\r
-  IpSb->MacString = NewMacString;\r
-\r
-  Status = gRT->SetVariable (\r
-                  IpSb->MacString,\r
-                  &gEfiIp4ServiceBindingProtocolGuid,\r
-                  EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-                  VariableDataSize,\r
-                  (VOID *) Ip4VariableData\r
-                  );\r
-\r
-ON_ERROR:\r
-\r
-  gBS->FreePool (Ip4VariableData);\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Clear the variable and free the resource.\r
-\r
-  @param  IpSb                  Ip4 service binding instance\r
-\r
-  @return None.\r
-\r
-**/\r
-VOID\r
-Ip4ClearVariableData (\r
-  IN IP4_SERVICE  *IpSb\r
-  )\r
-{\r
-  ASSERT (IpSb->MacString != NULL);\r
-\r
-  gRT->SetVariable (\r
-         IpSb->MacString,\r
-         &gEfiIp4ServiceBindingProtocolGuid,\r
-         EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-         0,\r
-         NULL\r
-         );\r
-\r
-  gBS->FreePool (IpSb->MacString);\r
-  IpSb->MacString = NULL;\r
+  return NetIp4IsUnicast (Ip, Netmask);\r
 }\r