|
@@ -15,8 +15,6 @@ using Utilities;
|
|
|
|
|
|
namespace ISCSI.Server
|
|
|
{
|
|
|
- public delegate ushort GetNextTSIH();
|
|
|
-
|
|
|
|
|
|
|
|
|
|
|
@@ -242,7 +240,16 @@ namespace ISCSI.Server
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- ProcessPDU(pdu, state);
|
|
|
+ bool valid = ValidateCommandNumbering(pdu, state);
|
|
|
+ if (valid)
|
|
|
+ {
|
|
|
+ ProcessPDU(pdu, state);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ Log(Severity.Warning, "[{0}] Ignoring PDU with CmdSN outside of expected range", state.ConnectionIdentifier);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -268,238 +275,6 @@ namespace ISCSI.Server
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void ProcessPDU(ISCSIPDU pdu, ConnectionState state)
|
|
|
- {
|
|
|
- Socket clientSocket = state.ClientSocket;
|
|
|
-
|
|
|
- uint? cmdSN = PDUHelper.GetCmdSN(pdu);
|
|
|
- Log(Severity.Trace, "Entering ProcessPDU");
|
|
|
- Log(Severity.Verbose, "[{0}] Received PDU from initiator, Operation: {1}, Size: {2}, CmdSN: {3}", state.ConnectionIdentifier, (ISCSIOpCodeName)pdu.OpCode, pdu.Length, cmdSN);
|
|
|
-
|
|
|
-
|
|
|
- if (cmdSN.HasValue)
|
|
|
- {
|
|
|
- if (state.Session.CommandNumberingStarted)
|
|
|
- {
|
|
|
- if (cmdSN != state.Session.ExpCmdSN)
|
|
|
- {
|
|
|
- Log(Severity.Error, "[{0}] CmdSN outside of expected range", state.ConnectionIdentifier);
|
|
|
-
|
|
|
- Log(Severity.Trace, "Leaving ProcessPDU");
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- state.Session.ExpCmdSN = cmdSN.Value;
|
|
|
- state.Session.CommandNumberingStarted = true;
|
|
|
- }
|
|
|
-
|
|
|
- if (pdu is LogoutRequestPDU || pdu is TextRequestPDU || pdu is SCSICommandPDU || pdu is RejectPDU)
|
|
|
- {
|
|
|
- if (!pdu.ImmediateDelivery)
|
|
|
- {
|
|
|
- state.Session.ExpCmdSN++;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!state.Session.IsFullFeaturePhase)
|
|
|
- {
|
|
|
- if (pdu is LoginRequestPDU)
|
|
|
- {
|
|
|
- LoginRequestPDU request = (LoginRequestPDU)pdu;
|
|
|
- Log(Severity.Verbose, "[{0}] Login Request, current stage: {1}, next stage: {2}, parameters: {3}", state.ConnectionIdentifier, request.CurrentStage, request.NextStage, KeyValuePairUtils.ToString(request.LoginParameters));
|
|
|
- if (request.TSIH != 0)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- ConnectionState existingConnection = m_connectionManager.FindConnection(request.ISID, request.TSIH, request.CID);
|
|
|
- if (existingConnection != null)
|
|
|
- {
|
|
|
-
|
|
|
- Log(Severity.Verbose, "[{0}] Initiating implicit logout", state.ConnectionIdentifier);
|
|
|
-
|
|
|
- existingConnection.RunningSCSICommands.WaitUntilZero();
|
|
|
- SocketUtils.ReleaseSocket(existingConnection.ClientSocket);
|
|
|
- existingConnection.SendQueue.Stop();
|
|
|
- m_connectionManager.RemoveConnection(existingConnection);
|
|
|
- Log(Severity.Verbose, "[{0}] Implicit logout completed", state.ConnectionIdentifier);
|
|
|
- }
|
|
|
- }
|
|
|
- LoginResponsePDU response = ServerResponseHelper.GetLoginResponsePDU(request, m_targets, state.Session, state.ConnectionParameters, GetNextTSIH);
|
|
|
- if (state.Session.IsFullFeaturePhase)
|
|
|
- {
|
|
|
- state.Session.ISID = request.ISID;
|
|
|
- state.ConnectionParameters.CID = request.CID;
|
|
|
- m_connectionManager.AddConnection(state);
|
|
|
- }
|
|
|
- Log(Severity.Verbose, "[{0}] Login Response parameters: {1}", state.ConnectionIdentifier, KeyValuePairUtils.ToString(response.LoginParameters));
|
|
|
- state.SendQueue.Enqueue(response);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- Log(Severity.Error, "[{0}] Improper command during login phase, OpCode: 0x{1}", state.ConnectionIdentifier, pdu.OpCode.ToString("x"));
|
|
|
- if (state.Session.TSIH == 0)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- clientSocket.Close();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- LoginResponsePDU loginResponse = new LoginResponsePDU();
|
|
|
- loginResponse.TSIH = state.Session.TSIH;
|
|
|
- loginResponse.Status = LoginResponseStatusName.InvalidDuringLogon;
|
|
|
- state.SendQueue.Enqueue(loginResponse);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (pdu is TextRequestPDU)
|
|
|
- {
|
|
|
- TextRequestPDU request = (TextRequestPDU)pdu;
|
|
|
- TextResponsePDU response = ServerResponseHelper.GetTextResponsePDU(request, m_targets);
|
|
|
- state.SendQueue.Enqueue(response);
|
|
|
- }
|
|
|
- else if (pdu is LogoutRequestPDU)
|
|
|
- {
|
|
|
- Log(Severity.Verbose, "[{0}] Logour Request", state.ConnectionIdentifier);
|
|
|
- LogoutRequestPDU request = (LogoutRequestPDU)pdu;
|
|
|
- if (state.Session.IsDiscovery && request.ReasonCode != LogoutReasonCode.CloseTheSession)
|
|
|
- {
|
|
|
-
|
|
|
- RejectPDU reject = new RejectPDU();
|
|
|
- reject.Reason = RejectReason.ProtocolError;
|
|
|
- reject.Data = ByteReader.ReadBytes(pdu.GetBytes(), 0, 48);
|
|
|
- state.SendQueue.Enqueue(reject);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- List<ConnectionState> connectionsToClose = new List<ConnectionState>();
|
|
|
- if (request.ReasonCode == LogoutReasonCode.CloseTheSession)
|
|
|
- {
|
|
|
- connectionsToClose = m_connectionManager.GetSessionConnections(state.Session.ISID, state.Session.TSIH);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- ConnectionState existingConnection = m_connectionManager.FindConnection(state.Session.ISID, state.Session.TSIH, request.CID);
|
|
|
- if (existingConnection != null && existingConnection != state)
|
|
|
- {
|
|
|
- connectionsToClose.Add(existingConnection);
|
|
|
- }
|
|
|
- connectionsToClose.Add(state);
|
|
|
- }
|
|
|
-
|
|
|
- foreach (ConnectionState connection in connectionsToClose)
|
|
|
- {
|
|
|
-
|
|
|
- connection.RunningSCSICommands.WaitUntilZero();
|
|
|
- if (connection != state)
|
|
|
- {
|
|
|
- SocketUtils.ReleaseSocket(connection.ClientSocket);
|
|
|
- }
|
|
|
- m_connectionManager.RemoveConnection(connection);
|
|
|
- }
|
|
|
- LogoutResponsePDU response = ServerResponseHelper.GetLogoutResponsePDU(request);
|
|
|
- state.SendQueue.Enqueue(response);
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- else if (state.Session.IsDiscovery)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- Log(Severity.Error, "[{0}] Improper command during discovery session, OpCode: 0x{1}", state.ConnectionIdentifier, pdu.OpCode.ToString("x"));
|
|
|
- RejectPDU reject = new RejectPDU();
|
|
|
- reject.Reason = RejectReason.ProtocolError;
|
|
|
- reject.Data = ByteReader.ReadBytes(pdu.GetBytes(), 0, 48);
|
|
|
-
|
|
|
- state.SendQueue.Enqueue(reject);
|
|
|
- }
|
|
|
- else if (pdu is NOPOutPDU)
|
|
|
- {
|
|
|
- NOPOutPDU request = (NOPOutPDU)pdu;
|
|
|
- if (request.InitiatorTaskTag != 0xFFFFFFFF)
|
|
|
- {
|
|
|
- NOPInPDU response = ServerResponseHelper.GetNOPResponsePDU(request);
|
|
|
- state.SendQueue.Enqueue(response);
|
|
|
- }
|
|
|
- }
|
|
|
- else if (pdu is SCSIDataOutPDU || pdu is SCSICommandPDU)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- List<SCSICommandPDU> commandsToExecute = null;
|
|
|
- List<ReadyToTransferPDU> readyToTransferPDUs = new List<ReadyToTransferPDU>();
|
|
|
- if (pdu is SCSIDataOutPDU)
|
|
|
- {
|
|
|
- SCSIDataOutPDU request = (SCSIDataOutPDU)pdu;
|
|
|
- Log(Severity.Debug, "[{0}] SCSIDataOutPDU: Target transfer tag: {1}, LUN: {2}, Buffer offset: {3}, Data segment length: {4}, DataSN: {5}, Final: {6}", state.ConnectionIdentifier, request.TargetTransferTag, (ushort)request.LUN, request.BufferOffset, request.DataSegmentLength, request.DataSN, request.Final);
|
|
|
- try
|
|
|
- {
|
|
|
- readyToTransferPDUs = TargetResponseHelper.GetReadyToTransferPDUs(request, state.Target, state.Session, state.ConnectionParameters, out commandsToExecute);
|
|
|
- }
|
|
|
- catch (InvalidTargetTransferTagException ex)
|
|
|
- {
|
|
|
- Log(Severity.Error, "[{0}] Invalid TargetTransferTag: {1}", state.ConnectionIdentifier, ex.TargetTransferTag);
|
|
|
- RejectPDU reject = new RejectPDU();
|
|
|
- reject.InitiatorTaskTag = request.InitiatorTaskTag;
|
|
|
- reject.Reason = RejectReason.InvalidPDUField;
|
|
|
- reject.Data = ByteReader.ReadBytes(request.GetBytes(), 0, 48);
|
|
|
- state.SendQueue.Enqueue(reject);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- SCSICommandPDU command = (SCSICommandPDU)pdu;
|
|
|
- Log(Severity.Debug, "[{0}] SCSICommandPDU: CmdSN: {1}, LUN: {2}, Data segment length: {3}, Expected Data Transfer Length: {4}, Final: {5}", state.ConnectionIdentifier, command.CmdSN, (ushort)command.LUN, command.DataSegmentLength, command.ExpectedDataTransferLength, command.Final);
|
|
|
- readyToTransferPDUs = TargetResponseHelper.GetReadyToTransferPDUs(command, state.Target, state.Session, state.ConnectionParameters, out commandsToExecute);
|
|
|
- }
|
|
|
- foreach (ReadyToTransferPDU readyToTransferPDU in readyToTransferPDUs)
|
|
|
- {
|
|
|
- state.SendQueue.Enqueue(readyToTransferPDU);
|
|
|
- }
|
|
|
- if (commandsToExecute != null)
|
|
|
- {
|
|
|
- state.RunningSCSICommands.Add(commandsToExecute.Count);
|
|
|
- }
|
|
|
- foreach (SCSICommandPDU commandPDU in commandsToExecute)
|
|
|
- {
|
|
|
- Log(Severity.Debug, "[{0}] Queuing command: CmdSN: {1}", state.ConnectionIdentifier, commandPDU.CmdSN);
|
|
|
- state.Target.QueueCommand(commandPDU.CommandDescriptorBlock, commandPDU.LUN, commandPDU.Data, commandPDU, state.OnCommandCompleted);
|
|
|
- }
|
|
|
- }
|
|
|
- else if (pdu is LoginRequestPDU)
|
|
|
- {
|
|
|
- Log(Severity.Error, "[{0}] Protocol Error (Login request during full feature phase)", state.ConnectionIdentifier);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- RejectPDU reject = new RejectPDU();
|
|
|
- reject.Reason = RejectReason.ProtocolError;
|
|
|
- reject.Data = ByteReader.ReadBytes(pdu.GetBytes(), 0, 48);
|
|
|
-
|
|
|
- state.SendQueue.Enqueue(reject);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Log(Severity.Error, "[{0}] Unsupported command, OpCode: 0x{1}", state.ConnectionIdentifier, pdu.OpCode.ToString("x"));
|
|
|
- RejectPDU reject = new RejectPDU();
|
|
|
- reject.Reason = RejectReason.CommandNotSupported;
|
|
|
- reject.Data = ByteReader.ReadBytes(pdu.GetBytes(), 0, 48);
|
|
|
-
|
|
|
- state.SendQueue.Enqueue(reject);
|
|
|
- }
|
|
|
- }
|
|
|
- Log(Severity.Trace, "Leaving ProcessPDU");
|
|
|
- }
|
|
|
-
|
|
|
private void ProcessSendQueue(ConnectionState state)
|
|
|
{
|
|
|
while (true)
|