]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/IScsiDxe/IScsiProto.c
Add code check to avoid access violation.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiProto.c
index d73a961465530e348c3294b30ed85d1e5621e905..0853f987675f527553c91455f22923399e1c43a3 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   The implementation of iSCSI protocol based on RFC3720.\r
 \r
-Copyright (c) 2004 - 2009, Intel Corporation.<BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2004 - 2011, 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
@@ -215,7 +215,7 @@ IScsiCreateConnection (
                   &Conn->TimeoutEvent\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (Conn);\r
+    FreePool (Conn);\r
     return NULL;\r
   }\r
 \r
@@ -246,7 +246,7 @@ IScsiCreateConnection (
             );\r
   if (EFI_ERROR (Status)) {\r
     gBS->CloseEvent (Conn->TimeoutEvent);\r
-    gBS->FreePool (Conn);\r
+    FreePool (Conn);\r
     Conn = NULL;\r
   }\r
 \r
@@ -266,7 +266,7 @@ IScsiDestroyConnection (
   Tcp4IoDestroySocket (&Conn->Tcp4Io);\r
   NetbufQueFlush (&Conn->RspQue);\r
   gBS->CloseEvent (Conn->TimeoutEvent);\r
-  gBS->FreePool (Conn);\r
+  FreePool (Conn);\r
 }\r
 \r
 /**\r
@@ -276,7 +276,9 @@ IScsiDestroyConnection (
 \r
   @retval EFI_SUCCESS          The iSCSI session login procedure finished.\r
   @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
+  @retval EFI_NO_MEDIA         There was a media error.\r
   @retval Others               Other errors as indicated.\r
+\r
 **/\r
 EFI_STATUS\r
 IScsiSessionLogin (\r
@@ -287,9 +289,24 @@ IScsiSessionLogin (
   ISCSI_SESSION     *Session;\r
   ISCSI_CONNECTION  *Conn;\r
   EFI_TCP4_PROTOCOL *Tcp4;\r
+  BOOLEAN           MediaPresent;\r
 \r
   Session = &Private->Session;\r
 \r
+  //\r
+  // Check media status before session login\r
+  //\r
+  MediaPresent = TRUE;\r
+  NetLibDetectMedia (Private->Controller, &MediaPresent);\r
+  if (!MediaPresent) {\r
+    return EFI_NO_MEDIA;\r
+  }\r
+\r
+  //\r
+  // Set session identifier\r
+  //\r
+  CopyMem (Session->Isid, Session->ConfigData.NvData.IsId, 6);\r
+\r
   //\r
   // Create a connection for the session.\r
   //\r
@@ -386,6 +403,7 @@ IScsiReceiveLoginRsp (
   //\r
   // A Login Response is received, process it.\r
   //\r
+  ASSERT (Pdu != NULL);\r
   Status = IScsiProcessLoginRsp (Conn, Pdu);\r
 \r
   NetbufFree (Pdu);\r
@@ -675,6 +693,14 @@ IScsiProcessLoginRsp (
   LoginRsp->MaxCmdSN  = NTOHL (LoginRsp->MaxCmdSN);\r
 \r
   if ((Conn->CurrentStage == ISCSI_SECURITY_NEGOTIATION) && (Conn->CHAPStep == ISCSI_CHAP_INITIAL)) {\r
+    //\r
+    // If the Login Request is a leading Login Request, the target MUST use\r
+    // the value presented in CmdSN as the target value for ExpCmdSN.\r
+    //\r
+    if ((Session->State == SESSION_STATE_FREE) && (Session->CmdSN != LoginRsp->ExpCmdSN)) {\r
+      return EFI_PROTOCOL_ERROR;\r
+    }\r
+\r
     //\r
     // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN\r
     // and ExpCmdSN.\r
@@ -868,6 +894,7 @@ IScsiUpdateTargetAddress (
   @param[in]  Arg The opaque parameter.\r
 **/\r
 VOID\r
+EFIAPI\r
 IScsiFreeNbufList (\r
   VOID *Arg\r
   )\r
@@ -875,7 +902,7 @@ IScsiFreeNbufList (
   ASSERT (Arg != NULL);\r
 \r
   NetbufFreeList ((LIST_ENTRY     *) Arg);\r
-  gBS->FreePool (Arg);\r
+  FreePool (Arg);\r
 }\r
 \r
 /**\r
@@ -884,6 +911,7 @@ IScsiFreeNbufList (
   @param[in]   Arg  The opaque parameter.\r
 **/\r
 VOID\r
+EFIAPI\r
 IScsiNbufExtFree (\r
   VOID *Arg\r
   )\r
@@ -944,7 +972,7 @@ IScsiReceivePdu (
   Len     = sizeof (ISCSI_BASIC_HEADER) + (HeaderDigest ? sizeof (UINT32) : 0);\r
   PduHdr  = NetbufAlloc (Len);\r
   if (PduHdr == NULL) {\r
-    gBS->FreePool (NbufList);\r
+    FreePool (NbufList);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
@@ -1134,7 +1162,7 @@ IScsiCheckOpParams (
   //\r
   KeyValueList = IScsiBuildKeyValueList (Data, Len);\r
   if (KeyValueList == NULL) {\r
-    gBS->FreePool (Data);\r
+    FreePool (Data);\r
     return Status;\r
   }\r
   //\r
@@ -1337,7 +1365,7 @@ ON_ERROR:
 \r
   IScsiFreeKeyValueList (KeyValueList);\r
 \r
-  gBS->FreePool (Data);\r
+  FreePool (Data);\r
 \r
   return Status;\r
 }\r
@@ -1485,7 +1513,7 @@ IScsiBuildKeyValueList (
       Data++;\r
       Len--;\r
     } else {\r
-      gBS->FreePool (KeyValuePair);\r
+      FreePool (KeyValuePair);\r
       goto ON_ERROR;\r
     }\r
 \r
@@ -1534,7 +1562,7 @@ IScsiGetValueByKeyFromList (
       Value = KeyValuePair->Value;\r
 \r
       RemoveEntryList (&KeyValuePair->List);\r
-      gBS->FreePool (KeyValuePair);\r
+      FreePool (KeyValuePair);\r
       break;\r
     }\r
   }\r
@@ -1559,10 +1587,10 @@ IScsiFreeKeyValueList (
     Entry         = NetListRemoveHead (KeyValueList);\r
     KeyValuePair  = NET_LIST_USER_STRUCT (Entry, ISCSI_KEY_VALUE_PAIR, List);\r
 \r
-    gBS->FreePool (KeyValuePair);\r
+    FreePool (KeyValuePair);\r
   }\r
 \r
-  gBS->FreePool (KeyValueList);\r
+  FreePool (KeyValueList);\r
 }\r
 \r
 /**\r
@@ -1678,7 +1706,7 @@ IScsiDelTcb (
 {\r
   RemoveEntryList (&Tcb->Link);\r
 \r
-  gBS->FreePool (Tcb);\r
+  FreePool (Tcb);\r
 }\r
 \r
 /**\r
@@ -1857,7 +1885,7 @@ IScsiNewScsiCmdPdu (
   CopyMem (ScsiCmd->Cdb, Packet->Cdb, sizeof (ScsiCmd->Cdb));\r
 \r
   if (Packet->CdbLength > 16) {\r
-    Header->Length  = NTOHS (Packet->CdbLength - 15);\r
+    Header->Length  = NTOHS ((UINT16) (Packet->CdbLength - 15));\r
     Header->Type    = ISCSI_AHS_TYPE_EXT_CDB;\r
 \r
     CopyMem (Header + 1, (UINT8 *) Packet->Cdb + 16, Packet->CdbLength - 16);\r
@@ -1969,7 +1997,7 @@ IScsiNewDataOutPdu (
   //\r
   PduHdr = NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT));\r
   if (PduHdr == NULL) {\r
-    gBS->FreePool (NbufList);\r
+    FreePool (NbufList);\r
     return NULL;\r
   }\r
   //\r
@@ -2667,21 +2695,10 @@ IScsiSessionInit (
   IN BOOLEAN            Recovery\r
   )\r
 {\r
-  UINT32  Random;\r
-\r
   if (!Recovery) {\r
     Session->Signature  = ISCSI_SESSION_SIGNATURE;\r
     Session->State      = SESSION_STATE_FREE;\r
 \r
-    Random              = NET_RANDOM (NetRandomInitSeed ());\r
-\r
-    Session->Isid[0]    = ISID_BYTE_0;\r
-    Session->Isid[1]    = ISID_BYTE_1;\r
-    Session->Isid[2]    = ISID_BYTE_2;\r
-    Session->Isid[3]    = ISID_BYTE_3;\r
-    Session->Isid[4]    = (UINT8) Random;\r
-    Session->Isid[5]    = (UINT8) (Random >> 8);\r
-\r
     InitializeListHead (&Session->Conns);\r
     InitializeListHead (&Session->TcbList);\r
   }\r