]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/UefiPxeBcDxe/PxeBcDhcp6.c
1. Update PXE driver to support PXEv6 boot cross subnet.
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcDhcp6.c
index 1eb64a6a0f54d4043f59bee2fef3b90fea952df0..b71998b405552c4ab08027840561511c3645afff 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Functions implementation related with DHCPv6 for UefiPxeBc Driver.\r
 \r
-  Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
 \r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
@@ -488,7 +488,7 @@ PxeBcParseDhcp6Packet (
   }\r
 \r
   //\r
-  // The offer with assigned client address is a proxy offer.\r
+  // The offer with assigned client address is NOT a proxy offer.\r
   // An ia_na option, embeded with valid ia_addr option and a status_code of success.\r
   //\r
   Option = Options[PXEBC_DHCP6_IDX_IA_NA];\r
@@ -1235,9 +1235,128 @@ PxeBcUnregisterIp6Address (
   }\r
 }\r
 \r
+/**\r
+  Check whether IP driver could route the message which will be sent to ServerIp address.\r
+  \r
+  This function will check the IP6 route table every 1 seconds until specified timeout is expired, if a valid\r
+  route is found in IP6 route table, the address will be filed in GatewayAddr and return.\r
+\r
+  @param[in]  Private             The pointer to PXEBC_PRIVATE_DATA.\r
+  @param[in]  TimeOutInSecond     Timeout value in seconds.\r
+  @param[out] GatewayAddr         Pointer to store the gateway IP address.\r
+\r
+  @retval     EFI_SUCCESS         Found a valid gateway address successfully.\r
+  @retval     EFI_TIMEOUT         The operation is time out.\r
+  @retval     Other               Unexpect error happened.\r
+  \r
+**/\r
+EFI_STATUS\r
+PxeBcCheckRouteTable (\r
+  IN  PXEBC_PRIVATE_DATA            *Private,\r
+  IN  UINTN                         TimeOutInSecond,\r
+  OUT EFI_IPv6_ADDRESS              *GatewayAddr\r
+  )\r
+{\r
+  EFI_STATUS                       Status;\r
+  EFI_IP6_PROTOCOL                 *Ip6;\r
+  EFI_IP6_MODE_DATA                Ip6ModeData;\r
+  UINTN                            Index;\r
+  EFI_EVENT                        TimeOutEvt;\r
+  UINTN                            RetryCount;\r
+  BOOLEAN                          GatewayIsFound;\r
+\r
+  ASSERT (GatewayAddr != NULL);\r
+  ASSERT (Private != NULL);\r
+\r
+  Ip6            = Private->Ip6;\r
+  GatewayIsFound = FALSE;\r
+  RetryCount     = 0;\r
+  TimeOutEvt     = NULL;\r
+  ZeroMem (GatewayAddr, sizeof (EFI_IPv6_ADDRESS));\r
+\r
+  while (TRUE) {\r
+    Status = Ip6->GetModeData (Ip6, &Ip6ModeData, NULL, NULL);\r
+    if (EFI_ERROR (Status)) {\r
+      goto ON_EXIT;\r
+    }\r
+\r
+    //\r
+    // Find out the gateway address which can route the message whcih send to ServerIp.\r
+    //\r
+    for (Index = 0; Index < Ip6ModeData.RouteCount; Index++) {\r
+      if (NetIp6IsNetEqual (&Private->ServerIp.v6, &Ip6ModeData.RouteTable[Index].Destination, Ip6ModeData.RouteTable[Index].PrefixLength)) {\r
+        IP6_COPY_ADDRESS (GatewayAddr, &Ip6ModeData.RouteTable[Index].Gateway);\r
+        GatewayIsFound = TRUE;\r
+        break;\r
+      }\r
+    }\r
+\r
+    if (Ip6ModeData.AddressList != NULL) {\r
+      FreePool (Ip6ModeData.AddressList);\r
+    }\r
+    if (Ip6ModeData.GroupTable != NULL) {\r
+      FreePool (Ip6ModeData.GroupTable);\r
+    }\r
+    if (Ip6ModeData.RouteTable != NULL) {\r
+      FreePool (Ip6ModeData.RouteTable);\r
+    }\r
+    if (Ip6ModeData.NeighborCache != NULL) {\r
+      FreePool (Ip6ModeData.NeighborCache);\r
+    }\r
+    if (Ip6ModeData.PrefixTable != NULL) {\r
+      FreePool (Ip6ModeData.PrefixTable);\r
+    }\r
+    if (Ip6ModeData.IcmpTypeList != NULL) {\r
+      FreePool (Ip6ModeData.IcmpTypeList);\r
+    }\r
+    \r
+    if (GatewayIsFound || RetryCount == TimeOutInSecond) {\r
+      break;\r
+    }\r
+    \r
+    RetryCount++;\r
+    \r
+    //\r
+    // Delay 1 second then recheck it again.\r
+    //\r
+    if (TimeOutEvt == NULL) {\r
+      Status = gBS->CreateEvent (\r
+                      EVT_TIMER,\r
+                      TPL_CALLBACK,\r
+                      NULL,\r
+                      NULL,\r
+                      &TimeOutEvt\r
+                      );\r
+      if (EFI_ERROR (Status)) {\r
+        goto ON_EXIT;\r
+      }\r
+    }\r
+\r
+    Status = gBS->SetTimer (TimeOutEvt, TimerRelative, TICKS_PER_SECOND);\r
+    if (EFI_ERROR (Status)) {\r
+      goto ON_EXIT;\r
+    }\r
+    while (EFI_ERROR (gBS->CheckEvent (TimeOutEvt))) {\r
+      Ip6->Poll (Ip6);\r
+    }\r
+  }\r
+  \r
+ON_EXIT:\r
+  if (TimeOutEvt != NULL) {\r
+    gBS->CloseEvent (TimeOutEvt);\r
+  }\r
+  \r
+  if (GatewayIsFound) {\r
+    Status = EFI_SUCCESS;\r
+  } else if (RetryCount == TimeOutInSecond) {\r
+    Status = EFI_TIMEOUT;\r
+  }\r
+\r
+  return Status; \r
+}\r
 \r
 /**\r
-  Register the ready address by Ip6Config protocol.\r
+  Register the ready station address and gateway by Ip6Config protocol.\r
 \r
   @param[in]  Private             The pointer to PXEBC_PRIVATE_DATA.\r
   @param[in]  Address             The pointer to the ready address.\r
@@ -1256,6 +1375,7 @@ PxeBcRegisterIp6Address (
   EFI_IP6_CONFIG_PROTOCOL          *Ip6Cfg;\r
   EFI_IP6_CONFIG_POLICY            Policy;\r
   EFI_IP6_CONFIG_MANUAL_ADDRESS    CfgAddr;\r
+  EFI_IPv6_ADDRESS                 GatewayAddr;\r
   UINTN                            DataSize;\r
   EFI_EVENT                        TimeOutEvt;\r
   EFI_EVENT                        MappedEvt;\r
@@ -1273,19 +1393,19 @@ PxeBcRegisterIp6Address (
   ZeroMem (&CfgAddr, sizeof (EFI_IP6_CONFIG_MANUAL_ADDRESS));\r
   CopyMem (&CfgAddr.Address, Address, sizeof (EFI_IPv6_ADDRESS));\r
 \r
+  Status = Ip6->Configure (Ip6, &Private->Ip6CfgData);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
   //\r
-  // Get and store the current policy of IP6 driver.\r
+  // Retrieve the gateway address from IP6 route table.\r
   //\r
-  Status = Ip6Cfg->GetData (\r
-                     Ip6Cfg,\r
-                     Ip6ConfigDataTypePolicy,\r
-                     &DataSize,\r
-                     &Private->Ip6Policy\r
-                     );\r
+  Status = PxeBcCheckRouteTable (Private, PXEBC_IP6_ROUTE_TABLE_TIMEOUT, &GatewayAddr);\r
   if (EFI_ERROR (Status)) {\r
     goto ON_EXIT;\r
   }\r
-\r
+  \r
   //\r
   // There is no channel between IP6 and PXE driver about address setting,\r
   // so it has to set the new address by Ip6ConfigProtocol manually.\r
@@ -1381,6 +1501,21 @@ PxeBcRegisterIp6Address (
     }\r
   }\r
 \r
+  //\r
+  // Set the default gateway address back if needed.\r
+  //\r
+  if (!NetIp6IsUnspecifiedAddr (&GatewayAddr)) {\r
+    Status = Ip6Cfg->SetData (\r
+                       Ip6Cfg,\r
+                       Ip6ConfigDataTypeGateway,\r
+                       sizeof (EFI_IPv6_ADDRESS),\r
+                       &GatewayAddr\r
+                       );\r
+    if (EFI_ERROR (Status)) {\r
+      goto ON_EXIT;\r
+    }\r
+  }\r
+\r
 ON_EXIT:\r
   if (MappedEvt != NULL) {\r
     Ip6Cfg->UnregisterDataNotify (\r
@@ -1396,6 +1531,100 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
+/**\r
+  Set the IP6 policy to Automatic.\r
+\r
+  @param[in]  Private             The pointer to PXEBC_PRIVATE_DATA.\r
+\r
+  @retval     EFI_SUCCESS         Switch the IP policy succesfully.\r
+  @retval     Others              Unexpect error happened.\r
+\r
+**/\r
+EFI_STATUS\r
+PxeBcSetIp6Policy (\r
+  IN PXEBC_PRIVATE_DATA            *Private\r
+  )\r
+{\r
+  EFI_IP6_CONFIG_POLICY            Policy;\r
+  EFI_STATUS                       Status;\r
+  EFI_IP6_CONFIG_PROTOCOL          *Ip6Cfg;\r
+  UINTN                            DataSize;\r
+\r
+  Ip6Cfg      = Private->Ip6Cfg;\r
+  DataSize    = sizeof (EFI_IP6_CONFIG_POLICY);\r
+\r
+  //\r
+  // Get and store the current policy of IP6 driver.\r
+  //\r
+  Status = Ip6Cfg->GetData (\r
+                     Ip6Cfg,\r
+                     Ip6ConfigDataTypePolicy,\r
+                     &DataSize,\r
+                     &Private->Ip6Policy\r
+                     );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (Private->Ip6Policy == Ip6ConfigPolicyManual) {\r
+    Policy = Ip6ConfigPolicyAutomatic;\r
+    Status = Ip6Cfg->SetData (\r
+                       Ip6Cfg,\r
+                       Ip6ConfigDataTypePolicy,\r
+                       sizeof(EFI_IP6_CONFIG_POLICY),\r
+                       &Policy\r
+                       );\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // There is no need to recover later.\r
+      //\r
+      Private->Ip6Policy = PXEBC_IP6_POLICY_MAX;\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  This function will register the station IP address and flush IP instance to start using the new IP address.\r
+  \r
+  @param[in]  Private             The pointer to PXEBC_PRIVATE_DATA.\r
+\r
+  @retval     EFI_SUCCESS         The new IP address has been configured successfully.\r
+  @retval     Others              Failed to configure the address.\r
+\r
+**/\r
+EFI_STATUS\r
+PxeBcSetIp6Address (\r
+  IN  PXEBC_PRIVATE_DATA              *Private\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_DHCP6_PROTOCOL          *Dhcp6;\r
+    \r
+  Dhcp6 = Private->Dhcp6;\r
+\r
+  CopyMem (&Private->StationIp.v6, &Private->TmpStationIp.v6, sizeof (EFI_IPv6_ADDRESS));\r
+  CopyMem (&Private->PxeBc.Mode->StationIp.v6, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));\r
+\r
+  Status = PxeBcRegisterIp6Address (Private, &Private->StationIp.v6);\r
+  if (EFI_ERROR (Status)) {\r
+    Dhcp6->Stop (Dhcp6);\r
+    return Status;\r
+  }\r
+\r
+  Status = PxeBcFlushStationIp (Private, &Private->StationIp, NULL);\r
+  if (EFI_ERROR (Status)) {\r
+    PxeBcUnregisterIp6Address (Private);\r
+    Dhcp6->Stop (Dhcp6);\r
+    return Status;\r
+  }\r
+\r
+  AsciiPrint ("\n  Station IP address is ");\r
+  PxeBcShowIp6Addr (&Private->StationIp.v6);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
 \r
 /**\r
   EFI_DHCP6_CALLBACK is provided by the consumer of the EFI DHCPv6 Protocol driver\r
@@ -1843,36 +2072,23 @@ PxeBcDhcp6Sarr (
   }\r
 \r
   ASSERT (Mode.Ia->State == Dhcp6Bound);\r
-  CopyMem (&Private->StationIp.v6, &Mode.Ia->IaAddress[0].IpAddress, sizeof (EFI_IPv6_ADDRESS));\r
-  CopyMem (&PxeMode->StationIp.v6, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));\r
-\r
-  Status = PxeBcRegisterIp6Address (Private, &Private->StationIp.v6);\r
-  if (EFI_ERROR (Status)) {\r
-    Dhcp6->Stop (Dhcp6);\r
-    return Status;\r
-  }\r
-\r
-  Status = PxeBcFlushStaionIp (Private, &Private->StationIp, NULL);\r
-  if (EFI_ERROR (Status)) {\r
-    PxeBcUnregisterIp6Address (Private);\r
-    Dhcp6->Stop (Dhcp6);\r
-    return Status;\r
-  }\r
+  //\r
+  // DHCP6 doesn't have an option to specify the router address on the subnet, the only way to get the\r
+  // router address in IP6 is the router discovery mechanism (the RS and RA, which only be handled when\r
+  // the IP policy is Automatic). So we just hold the station IP address here and leave the IP policy as\r
+  // Automatic, until we get the server IP address. This could let IP6 driver finish the router discovery \r
+  // to find a valid router address.\r
+  //\r
+  CopyMem (&Private->TmpStationIp.v6, &Mode.Ia->IaAddress[0].IpAddress, sizeof (EFI_IPv6_ADDRESS));\r
 \r
   //\r
   // Check the selected offer whether BINL retry is needed.\r
   //\r
   Status = PxeBcHandleDhcp6Offer (Private);\r
   if (EFI_ERROR (Status)) {\r
-    PxeBcUnregisterIp6Address (Private);\r
     Dhcp6->Stop (Dhcp6);\r
     return Status;\r
   }\r
-\r
-  AsciiPrint ("\n  Station IP address is ");\r
-\r
-  PxeBcShowIp6Addr (&Private->StationIp.v6);\r
-\r
+  \r
   return EFI_SUCCESS;\r
 }\r
-\r