]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/IScsiDxe/IScsiProto.c
MdeModulePkg/FrameBufferBltLib: Fix copying of unaligned memory
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiProto.c
index 02953c3bac5b4d80293c918bb3ff93e2af822d4a..38e4af26bcf8f227fc78fea42b959b23548dbad3 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   The implementation of iSCSI protocol based on RFC3720.\r
 \r
-Copyright (c) 2004 - 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 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
@@ -138,7 +138,11 @@ IScsiConnLogin (
   //\r
   // Start the timer, and wait Timeout seconds to establish the TCP connection.\r
   //\r
-  Status = gBS->SetTimer (Conn->TimeoutEvent, TimerRelative, Timeout * TICKS_PER_MS);\r
+  Status = gBS->SetTimer (\r
+                  Conn->TimeoutEvent,\r
+                  TimerRelative,\r
+                  MultU64x32 (Timeout, TICKS_PER_MS)\r
+                  );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -228,7 +232,7 @@ IScsiCreateConnection (
   Conn->PartialRspRcvd  = FALSE;\r
   Conn->ParamNegotiated = FALSE;\r
   Conn->Cid             = Session->NextCid++;\r
-  Conn->Ipv6Flag        = mPrivate->Ipv6Flag;\r
+  Conn->Ipv6Flag        = NvData->IpMode == IP_MODE_IP6 || Session->ConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6;\r
 \r
   Status = gBS->CreateEvent (\r
                   EVT_TIMER,\r
@@ -251,6 +255,23 @@ IScsiCreateConnection (
   Conn->HeaderDigest              = IScsiDigestNone;\r
   Conn->DataDigest                = IScsiDigestNone;\r
 \r
+  if (NvData->DnsMode) {\r
+    //\r
+    // perform dns process if target address expressed by domain name.\r
+    //\r
+    if (!Conn->Ipv6Flag) {\r
+      Status = IScsiDns4 (Private->Image, Private->Controller, NvData);\r
+    } else {\r
+      Status = IScsiDns6 (Private->Image, Private->Controller, NvData);\r
+    }\r
+\r
+    if (EFI_ERROR(Status)) {\r
+      DEBUG ((EFI_D_ERROR, "The configuration of Target address or DNS server address is invalid!\n"));\r
+      FreePool (Conn);\r
+      return NULL;\r
+    }\r
+  }\r
+\r
   if (!Conn->Ipv6Flag) {\r
     Tcp4IoConfig = &TcpIoConfig.Tcp4IoConfigData;\r
     \r
@@ -259,15 +280,16 @@ IScsiCreateConnection (
     CopyMem (&Tcp4IoConfig->Gateway, &NvData->Gateway, sizeof (EFI_IPv4_ADDRESS));\r
     CopyMem (&Tcp4IoConfig->RemoteIp, &NvData->TargetIp, sizeof (EFI_IPv4_ADDRESS));\r
 \r
-    Tcp4IoConfig->RemotePort = NvData->TargetPort;\r
-    Tcp4IoConfig->ActiveFlag = TRUE;\r
-\r
+    Tcp4IoConfig->RemotePort  = NvData->TargetPort;\r
+    Tcp4IoConfig->ActiveFlag  = TRUE;\r
+    Tcp4IoConfig->StationPort = 0;\r
   } else {\r
     Tcp6IoConfig = &TcpIoConfig.Tcp6IoConfigData;\r
   \r
     CopyMem (&Tcp6IoConfig->RemoteIp, &NvData->TargetIp, sizeof (EFI_IPv6_ADDRESS));\r
-    Tcp6IoConfig->RemotePort = NvData->TargetPort;\r
-    Tcp6IoConfig->ActiveFlag = TRUE;\r
+    Tcp6IoConfig->RemotePort  = NvData->TargetPort;\r
+    Tcp6IoConfig->ActiveFlag  = TRUE;\r
+    Tcp6IoConfig->StationPort = 0;\r
   }\r
 \r
   //\r
@@ -422,14 +444,14 @@ IScsiSessionLogin (
   VOID              *Tcp;\r
   EFI_GUID          *ProtocolGuid;\r
   UINT8             RetryCount;\r
-  BOOLEAN           MediaPresent;\r
+  EFI_STATUS        MediaStatus;\r
 \r
   //\r
   // Check media status before session login.\r
   //\r
-  MediaPresent = TRUE;\r
-  NetLibDetectMedia (Session->Private->Controller, &MediaPresent);\r
-  if (!MediaPresent) {\r
+  MediaStatus = EFI_SUCCESS;\r
+  NetLibDetectMediaWaitTimeout (Session->Private->Controller, ISCSI_CHECK_MEDIA_LOGIN_WAITING_TIME, &MediaStatus);\r
+  if (MediaStatus != EFI_SUCCESS) {\r
     return EFI_NO_MEDIA;\r
   }\r
 \r
@@ -471,7 +493,7 @@ IScsiSessionLogin (
   if (!EFI_ERROR (Status)) {\r
     Session->State = SESSION_STATE_LOGGED_IN;\r
 \r
-    if (!mPrivate->Ipv6Flag) {\r
+    if (!Conn->Ipv6Flag) {\r
       ProtocolGuid = &gEfiTcp4ProtocolGuid;      \r
     } else {\r
       ProtocolGuid = &gEfiTcp6ProtocolGuid;\r
@@ -488,7 +510,7 @@ IScsiSessionLogin (
 \r
     ASSERT_EFI_ERROR (Status);\r
 \r
-    if (mPrivate->Ipv6Flag) {\r
+    if (Conn->Ipv6Flag) {\r
       Status = IScsiGetIp6NicInfo (Conn);\r
     }\r
   }\r
@@ -603,6 +625,8 @@ IScsiReceiveLoginRsp (
   EFI_STATUS  Status;\r
   NET_BUF     *Pdu;\r
 \r
+  Pdu = NULL;\r
+\r
   //\r
   // Receive the iSCSI login response.\r
   //\r
@@ -728,7 +752,10 @@ IScsiPrepareLoginReq (
   }\r
 \r
   LoginReq = (ISCSI_LOGIN_REQUEST *) NetbufAllocSpace (Nbuf, sizeof (ISCSI_LOGIN_REQUEST), NET_BUF_TAIL);\r
-  ASSERT (LoginReq != NULL);\r
+  if (LoginReq == NULL) {\r
+    NetbufFree (Nbuf);\r
+    return NULL;\r
+  }\r
   ZeroMem (LoginReq, sizeof (ISCSI_LOGIN_REQUEST));\r
 \r
   //\r
@@ -776,7 +803,7 @@ IScsiPrepareLoginReq (
 \r
   case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION:\r
     //\r
-    // Only negotiate the paramter once.\r
+    // Only negotiate the parameter once.\r
     //\r
     if (!Conn->ParamNegotiated) {\r
       IScsiFillOpParams (Conn, Nbuf);\r
@@ -1059,12 +1086,13 @@ IScsiUpdateTargetAddress (
   IN     UINT32                Len\r
   )\r
 {\r
-  LIST_ENTRY      *KeyValueList;\r
-  CHAR8           *TargetAddress;\r
-  CHAR8           *IpStr;\r
-  EFI_STATUS      Status;\r
-  UINTN           Number;\r
-  UINT8           IpMode;\r
+  LIST_ENTRY                   *KeyValueList;\r
+  CHAR8                        *TargetAddress;\r
+  CHAR8                        *IpStr;\r
+  EFI_STATUS                   Status;\r
+  UINTN                        Number;\r
+  UINT8                        IpMode;\r
+  ISCSI_SESSION_CONFIG_NVDATA  *NvData;\r
 \r
   KeyValueList = IScsiBuildKeyValueList (Data, Len);\r
   if (KeyValueList == NULL) {\r
@@ -1072,31 +1100,68 @@ IScsiUpdateTargetAddress (
   }\r
 \r
   Status = EFI_NOT_FOUND;\r
-\r
+  NvData = &Session->ConfigData->SessionConfigData;\r
\r
   while (TRUE) {\r
     TargetAddress = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_TARGET_ADDRESS);\r
     if (TargetAddress == NULL) {\r
       break;\r
     }\r
 \r
-    if (!NET_IS_DIGIT (TargetAddress[0])) {\r
+    //\r
+    // RFC 3720 defines format of the TargetAddress=domainname[:port][,portal-group-tag]\r
+    // The domainname can be specified as either a DNS host name, adotted-decimal IPv4 address,\r
+    // or a bracketed IPv6 address as specified in [RFC2732].\r
+    //\r
+    if (NET_IS_DIGIT (TargetAddress[0])) {\r
       //\r
-      // The domainname of the target may be presented in three formats: a DNS host name,\r
-      // a dotted-decimal IPv4 address, or a bracketed IPv6 address. Only accept dotted\r
-      // IPv4 address.\r
+      // The domainname of the target is presented in a dotted-decimal IPv4 address format.\r
       //\r
-      continue;\r
-    }\r
+      IpStr = TargetAddress;\r
+\r
+      while ((*TargetAddress != '\0') && (*TargetAddress != ':') && (*TargetAddress != ',')) {\r
+        //\r
+        // NULL, ':', or ',' ends the IPv4 string.\r
+        //\r
+        TargetAddress++;\r
+      }\r
+    } else if (*TargetAddress == ISCSI_REDIRECT_ADDR_START_DELIMITER){\r
+      //\r
+      // The domainname of the target is presented in a bracketed IPv6 address format.\r
+      //\r
+      TargetAddress ++;\r
+      IpStr = TargetAddress;\r
+      while ((*TargetAddress != '\0') && (*TargetAddress != ISCSI_REDIRECT_ADDR_END_DELIMITER)) {\r
+        //\r
+        // ']' ends the IPv6 string.\r
+        //\r
+        TargetAddress++;\r
+      }\r
+\r
+      if (*TargetAddress != ISCSI_REDIRECT_ADDR_END_DELIMITER) {\r
+        continue;\r
+      }\r
 \r
-    IpStr = TargetAddress;\r
+      *TargetAddress = '\0';\r
+      TargetAddress ++;\r
 \r
-    while ((*TargetAddress != 0) && (*TargetAddress != ':') && (*TargetAddress != ',')) {\r
+    } else {\r
       //\r
-      // NULL, ':', or ',' ends the IPv4 string.\r
+      // The domainname of the target is presented in the format of a DNS host name.\r
       //\r
-      TargetAddress++;\r
+      IpStr = TargetAddress;\r
+\r
+      while ((*TargetAddress != '\0') && (*TargetAddress != ':') && (*TargetAddress != ',')) {\r
+        TargetAddress++;\r
+      }\r
+      NvData->DnsMode = TRUE;\r
     }\r
 \r
+    //\r
+    // Save the origial user setting which specifies the proxy/virtual iSCSI target.\r
+    //\r
+    NvData->OriginalTargetPort = NvData->TargetPort;\r
+\r
     if (*TargetAddress == ',') {\r
       //\r
       // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent\r
@@ -1112,33 +1177,51 @@ IScsiUpdateTargetAddress (
       if (Number > 0xFFFF) {\r
         continue;\r
       } else {\r
-        Session->ConfigData->SessionConfigData.TargetPort = (UINT16) Number;\r
+        NvData->TargetPort = (UINT16) Number;\r
       }\r
     } else {\r
       //\r
-      // The string only contains the IPv4 address. Use the well-known port.\r
+      // The string only contains the Target address. Use the well-known port.\r
       //\r
-      Session->ConfigData->SessionConfigData.TargetPort = ISCSI_WELL_KNOWN_PORT;\r
+      NvData->TargetPort = ISCSI_WELL_KNOWN_PORT;\r
     }\r
+\r
+    //\r
+    // Save the origial user setting which specifies the proxy/virtual iSCSI target.\r
+    //    \r
+    CopyMem (&NvData->OriginalTargetIp, &NvData->TargetIp, sizeof (EFI_IP_ADDRESS));\r
+\r
     //\r
     // Update the target IP address.\r
     //\r
-    if (Session->ConfigData->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) {\r
-      IpMode = Session->ConfigData->SessionConfigData.IpMode;\r
+    if (NvData->IpMode < IP_MODE_AUTOCONFIG) {\r
+      IpMode = NvData->IpMode;\r
     } else {\r
       IpMode = Session->ConfigData->AutoConfigureMode;\r
     }\r
 \r
-    Status = IScsiAsciiStrToIp (\r
-               IpStr,\r
-               IpMode,\r
-               &Session->ConfigData->SessionConfigData.TargetIp\r
-               );\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      continue;\r
+    if (NvData->DnsMode) {\r
+      //\r
+      // Target address is expressed as URL format, just save it and\r
+      // do DNS resolution when creating a TCP connection.\r
+      //\r
+      if (AsciiStrSize (IpStr) > sizeof (Session->ConfigData->SessionConfigData.TargetUrl)){\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
+      CopyMem (&Session->ConfigData->SessionConfigData.TargetUrl, IpStr, AsciiStrSize (IpStr));\r
     } else {\r
-      break;\r
+      Status = IScsiAsciiStrToIp (\r
+                 IpStr,\r
+                 IpMode,\r
+                 &Session->ConfigData->SessionConfigData.TargetIp\r
+                 );\r
+\r
+      if (EFI_ERROR (Status)) {\r
+        continue;\r
+      } else {\r
+        NvData->RedirectFlag = TRUE;\r
+        break;\r
+      }\r
     }\r
   }\r
 \r
@@ -1242,7 +1325,10 @@ IScsiReceivePdu (
   }\r
 \r
   Header = NetbufAllocSpace (PduHdr, Len, NET_BUF_TAIL);\r
-  ASSERT (Header != NULL);\r
+  if (Header == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ON_EXIT;\r
+  }\r
   InsertTailList (NbufList, &PduHdr->List);\r
 \r
   //\r
@@ -2090,7 +2176,7 @@ IScsiNewDataSegment (
 \r
   @param[in]  Packet The EXT SCSI PASS THRU request packet containing the SCSI command.\r
   @param[in]  Lun    The LUN.\r
-  @param[in]  Tcb    The tcb assocated with this SCSI command.\r
+  @param[in]  Tcb    The tcb associated with this SCSI command.\r
 \r
   @return The  created iSCSI SCSI command PDU.\r
   @retval NULL Other errors as indicated.\r
@@ -2313,7 +2399,10 @@ IScsiNewDataOutPdu (
   InsertTailList (NbufList, &PduHdr->List);\r
 \r
   DataOutHdr  = (ISCSI_SCSI_DATA_OUT *) NetbufAllocSpace (PduHdr, sizeof (ISCSI_SCSI_DATA_OUT), NET_BUF_TAIL);\r
-  ASSERT (DataOutHdr != NULL);\r
+  if (DataOutHdr == NULL) {\r
+    IScsiFreeNbufList (NbufList);\r
+    return NULL;\r
+  }\r
   XferContext = &Tcb->XferContext;\r
 \r
   ZeroMem (DataOutHdr, sizeof (ISCSI_SCSI_DATA_OUT));\r
@@ -2819,6 +2908,7 @@ IScsiOnNopInRcvd (
   @retval EFI_DEVICE_ERROR     Session state was not as required.\r
   @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
   @retval EFI_PROTOCOL_ERROR   There is no such data in the net buffer.\r
+  @retval EFI_NOT_READY        The target can not accept new commands.\r
   @retval Others               Other errors as indicated.\r
 \r
 **/\r
@@ -2997,15 +3087,6 @@ ON_EXIT:
     IScsiDelTcb (Tcb);\r
   }\r
 \r
-  if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) {\r
-    //\r
-    // Reinstate the session.\r
-    //\r
-    if (EFI_ERROR (IScsiSessionReinstatement (Session))) {\r
-      Status = EFI_DEVICE_ERROR;\r
-    }\r
-  }\r
-\r
   return Status;\r
 }\r
 \r