]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Impl.c
MdeModulePkg/Dhcp4Dxe: Check Media status before starting DHCP process.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Dhcp4Dxe / Dhcp4Impl.c
index 4f491b4bba3b3bed6fc4a124080d7bc6724e7fb5..87804145f4f6c4774e4d72a4a0862e239b9ec708 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   This file implement the EFI_DHCP4_PROTOCOL interface.\r
 \r
-Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 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
@@ -660,9 +660,7 @@ EfiDhcp4Configure (
     }\r
 \r
     CopyMem (&Ip, &Dhcp4CfgData->ClientAddress, sizeof (IP4_ADDR));\r
-\r
-    if ((Ip != 0) && !NetIp4IsUnicast (NTOHL (Ip), 0)) {\r
-\r
+    if (IP4_IS_LOCAL_BROADCAST(NTOHL (Ip))) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
   }\r
@@ -782,6 +780,7 @@ EfiDhcp4Start (
 {\r
   DHCP_PROTOCOL             *Instance;\r
   DHCP_SERVICE              *DhcpSb;\r
+  BOOLEAN                   MediaPresent;\r
   EFI_STATUS                Status;\r
   EFI_TPL                   OldTpl;\r
 \r
@@ -811,6 +810,16 @@ EfiDhcp4Start (
     goto ON_ERROR;\r
   }\r
 \r
+  //\r
+  // Check Media Satus.\r
+  //\r
+  MediaPresent = TRUE;\r
+  NetLibDetectMedia (DhcpSb->Controller, &MediaPresent);\r
+  if (!MediaPresent) {\r
+    Status = EFI_NO_MEDIA;\r
+    goto ON_ERROR;\r
+  }\r
+\r
   DhcpSb->IoStatus = EFI_ALREADY_STARTED;\r
 \r
   if (EFI_ERROR (Status = DhcpInitRequest (DhcpSb))) {\r
@@ -1188,14 +1197,16 @@ Dhcp4InstanceConfigUdpIo (
   )\r
 {\r
   DHCP_PROTOCOL                     *Instance;\r
+  DHCP_SERVICE                      *DhcpSb;\r
   EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN  *Token;\r
   EFI_UDP4_CONFIG_DATA              UdpConfigData;\r
   IP4_ADDR                          ClientAddr;\r
-  IP4_ADDR                          Ip;   \r
+  IP4_ADDR                          Ip; \r
   INTN                              Class; \r
-  IP4_ADDR                          SubnetMask;\r
+  IP4_ADDR                          SubnetMask;  \r
 \r
   Instance = (DHCP_PROTOCOL *) Context;\r
+  DhcpSb   = Instance->Service;\r
   Token    = Instance->Token;\r
 \r
   ZeroMem (&UdpConfigData, sizeof (EFI_UDP4_CONFIG_DATA));\r
@@ -1208,10 +1219,22 @@ Dhcp4InstanceConfigUdpIo (
   ClientAddr = EFI_NTOHL (Token->Packet->Dhcp4.Header.ClientAddr);\r
   Ip = HTONL (ClientAddr);\r
   CopyMem (&UdpConfigData.StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
-  \r
-  Class = NetGetIpClass (ClientAddr);\r
-  ASSERT (Class < IP4_ADDR_CLASSE);\r
-  SubnetMask = gIp4AllMasks[Class << 3];\r
+\r
+  if (DhcpSb->Netmask == 0) {\r
+    //\r
+    // The Dhcp4.TransmitReceive() API should be able to used at any time according to\r
+    // UEFI spec, while in classless addressing network, the netmask must be explicitly\r
+    // provided together with the station address.\r
+    // If the DHCP instance haven't be configured with a valid netmask, we could only\r
+    // compute it according to the classful addressing rule.\r
+    //\r
+    Class = NetGetIpClass (ClientAddr);\r
+    ASSERT (Class < IP4_ADDR_CLASSE);\r
+    SubnetMask = gIp4AllMasks[Class << 3];\r
+  } else {\r
+    SubnetMask = DhcpSb->Netmask;\r
+  }\r
+\r
   Ip = HTONL (SubnetMask);\r
   CopyMem (&UdpConfigData.SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
 \r
@@ -1485,8 +1508,6 @@ EfiDhcp4TransmitReceive (
   DHCP_SERVICE   *DhcpSb;\r
   EFI_IP_ADDRESS Gateway;\r
   IP4_ADDR       ClientAddr;\r
-  INTN           Class;\r
-  IP4_ADDR       SubnetMask;\r
 \r
   if ((This == NULL) || (Token == NULL) || (Token->Packet == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1579,11 +1600,8 @@ EfiDhcp4TransmitReceive (
   //\r
   // Get the gateway.\r
   //\r
-  Class = NetGetIpClass (ClientAddr);\r
-  ASSERT (Class < IP4_ADDR_CLASSE);\r
-  SubnetMask = gIp4AllMasks[Class << 3];\r
   ZeroMem (&Gateway, sizeof (Gateway));\r
-  if (!IP4_NET_EQUAL (ClientAddr, EndPoint.RemoteAddr.Addr[0], SubnetMask)) {\r
+  if (!IP4_NET_EQUAL (ClientAddr, EndPoint.RemoteAddr.Addr[0], DhcpSb->Netmask)) {\r
     CopyMem (&Gateway.v4, &Token->GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r
     Gateway.Addr[0] = NTOHL (Gateway.Addr[0]);\r
   }\r