]> 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 2fb4f4c1ca2abd3d7dc820fc5798445cc44abf4a..6a26143e307d3e4effe40bc1f190fb0833afd3d0 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 \r
 /** @file\r
 \r
-Copyright (c) 2005 - 2015, Intel Corporation. All rights reserved.<BR>\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
 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
@@ -550,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->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
   IpInstance->Service   = IpSb;\r
 \r
   InitializeListHead (&IpInstance->Link);\r
@@ -563,7 +564,6 @@ Ip4InitProtocol (
 }\r
 \r
 \r
 }\r
 \r
 \r
-\r
 /**\r
   Configure the IP4 child. If the child is already configured,\r
   change the configuration parameter. Otherwise configure it\r
 /**\r
   Configure the IP4 child. If the child is already configured,\r
   change the configuration parameter. Otherwise configure it\r
@@ -596,9 +596,13 @@ Ip4ConfigProtocol (
   IP4_ADDR                  Ip;\r
   IP4_ADDR                  Netmask;\r
   EFI_ARP_PROTOCOL          *Arp;\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
 \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
   //\r
   // User is changing packet filters. It must be stopped\r
   // before the station address can be changed.\r
@@ -674,14 +678,26 @@ Ip4ConfigProtocol (
 \r
   } else {\r
     //\r
 \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
     //\r
     if (IpSb->State == IP4_SERVICE_UNSTARTED) {\r
-      Status = Ip4StartAutoConfig (&IpSb->Ip4Config2Instance);\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
       }\r
     }\r
 \r
@@ -711,6 +727,7 @@ Ip4ConfigProtocol (
                     EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
                     );\r
     if (EFI_ERROR (Status)) {\r
                     EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
                     );\r
     if (EFI_ERROR (Status)) {\r
+      Ip4FreeInterface (IpIf, IpInstance);\r
       goto ON_ERROR;\r
     }\r
   }\r
       goto ON_ERROR;\r
     }\r
   }\r
@@ -811,66 +828,6 @@ Ip4CleanProtocol (
 }\r
 \r
 \r
 }\r
 \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[in]  Ip                 The IP address to validate.\r
-  @param[in]  Netmask            The netmaks of the IP.\r
-\r
-  @retval TRUE                   The Ip/Netmask pair is valid.\r
-  @retval FALSE                  The Ip/Netmask pair is invalid.\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
   Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\r
 \r
 /**\r
   Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\r
 \r
@@ -998,8 +955,7 @@ EfiIp4Configure (
     Status = Ip4CleanProtocol (IpInstance);\r
 \r
     //\r
     Status = Ip4CleanProtocol (IpInstance);\r
 \r
     //\r
-    // Don't change the state if it is DESTROY, 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
     // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED,\r
     // the unload fails miserably.\r
     //\r
@@ -1092,8 +1048,8 @@ Ip4Groups (
   // is decreamented each time an address is removed..\r
   //\r
   for (Index = IpInstance->GroupCount; Index > 0 ; Index--) {\r
   // 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
     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
     if ((GroupAddress == NULL) || EFI_IP4_EQUAL (&Group, GroupAddress)) {\r
       if (EFI_ERROR (Ip4LeaveGroup (IpInstance, NTOHL (Group)))) {\r
         return EFI_DEVICE_ERROR;\r
@@ -2293,12 +2249,10 @@ Ip4SentPacketTicking (
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\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
+  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
   @param[in]  Event                  The IP4 service instance's heart beat timer.\r
   @param[in]  Context                The IP4 service instance.\r
 \r
   @param[in]  Event                  The IP4 service instance's heart beat timer.\r
   @param[in]  Context                The IP4 service instance.\r
@@ -2315,7 +2269,64 @@ Ip4TimerTicking (
 \r
   IpSb = (IP4_SERVICE *) Context;\r
   NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
 \r
   IpSb = (IP4_SERVICE *) Context;\r
   NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
-\r
+  \r
   Ip4PacketTimerTicking (IpSb);\r
   Ip4IgmpTicking (IpSb);\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