blob: c5d5d4e86b430e0538a581637c2c31ebcd9ebc7e [file] [log] [blame]
Tom Josephe6361a22016-08-10 06:56:25 -05001#include "message_handler.hpp"
2
Vernon Mauery9e801a22018-10-12 13:20:49 -07003#include "command_table.hpp"
4#include "main.hpp"
5#include "message.hpp"
6#include "message_parsers.hpp"
7#include "sessions_manager.hpp"
8
Tom Josephe6361a22016-08-10 06:56:25 -05009#include <sys/socket.h>
10
11#include <iostream>
12#include <memory>
13#include <string>
14#include <vector>
15
Tom Josephe6361a22016-08-10 06:56:25 -050016namespace message
17{
18
19std::unique_ptr<Message> Handler::receive()
20{
21 std::vector<uint8_t> packet;
22 auto readStatus = 0;
23
24 // Read the packet
25 std::tie(readStatus, packet) = channel->read();
26
27 // Read of the packet failed
28 if (readStatus < 0)
29 {
30 std::cerr << "E> Error in Read : " << std::hex << readStatus << "\n";
31 return nullptr;
32 }
33
34 // Unflatten the packet
35 std::unique_ptr<Message> message;
36 std::tie(message, sessionHeader) = parser::unflatten(packet);
37
Vernon Maueryae1fda42018-10-15 12:55:34 -070038 auto session = std::get<session::Manager&>(singletonPool)
39 .getSession(message->bmcSessionID);
Tom Josephe6361a22016-08-10 06:56:25 -050040
41 sessionID = message->bmcSessionID;
42 message->rcSessionID = session->getRCSessionID();
43 session->updateLastTransactionTime();
44
45 return message;
46}
47
Vernon Mauery9e801a22018-10-12 13:20:49 -070048template <>
49std::unique_ptr<Message>
50 Handler::createResponse<PayloadType::IPMI>(std::vector<uint8_t>& output,
51 Message& inMessage)
Tom Josephe6361a22016-08-10 06:56:25 -050052{
53 auto outMessage = std::make_unique<Message>();
Tom Josephe6361a22016-08-10 06:56:25 -050054 outMessage->payloadType = PayloadType::IPMI;
55
Vernon Mauery9e801a22018-10-12 13:20:49 -070056 outMessage->payload.resize(sizeof(LAN::header::Response) + output.size() +
Tom Josephe6361a22016-08-10 06:56:25 -050057 sizeof(LAN::trailer::Response));
58
Vernon Mauery9e801a22018-10-12 13:20:49 -070059 auto reqHeader =
60 reinterpret_cast<LAN::header::Request*>(inMessage.payload.data());
61 auto respHeader =
62 reinterpret_cast<LAN::header::Response*>(outMessage->payload.data());
Tom Josephe6361a22016-08-10 06:56:25 -050063
64 // Add IPMI LAN Message Response Header
65 respHeader->rqaddr = reqHeader->rqaddr;
Vernon Mauery9e801a22018-10-12 13:20:49 -070066 respHeader->netfn = reqHeader->netfn | 0x04;
67 respHeader->cs = crc8bit(&(respHeader->rqaddr), 2);
Tom Josephe6361a22016-08-10 06:56:25 -050068 respHeader->rsaddr = reqHeader->rsaddr;
Vernon Mauery9e801a22018-10-12 13:20:49 -070069 respHeader->rqseq = reqHeader->rqseq;
70 respHeader->cmd = reqHeader->cmd;
Tom Josephe6361a22016-08-10 06:56:25 -050071
72 auto assembledSize = sizeof(LAN::header::Response);
73
74 // Copy the output by the execution of the command
75 std::copy(output.begin(), output.end(),
76 outMessage->payload.begin() + assembledSize);
77 assembledSize += output.size();
78
79 // Add the IPMI LAN Message Trailer
Vernon Mauery9e801a22018-10-12 13:20:49 -070080 auto trailer = reinterpret_cast<LAN::trailer::Response*>(
81 outMessage->payload.data() + assembledSize);
Tom Josephe6361a22016-08-10 06:56:25 -050082 trailer->checksum = crc8bit(&respHeader->rsaddr, assembledSize - 3);
83
84 return outMessage;
85}
86
87std::unique_ptr<Message> Handler::executeCommand(Message& inMessage)
88{
89 // Get the CommandID to map into the command table
90 auto command = getCommand(inMessage);
91 std::vector<uint8_t> output{};
92
93 if (inMessage.payloadType == PayloadType::IPMI)
94 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070095 if (inMessage.payload.size() <
96 (sizeof(LAN::header::Request) + sizeof(LAN::trailer::Request)))
Tom Josephe6361a22016-08-10 06:56:25 -050097 {
98 return nullptr;
99 }
100
101 auto start = inMessage.payload.begin() + sizeof(LAN::header::Request);
102 auto end = inMessage.payload.end() - sizeof(LAN::trailer::Request);
103 std::vector<uint8_t> inPayload(start, end);
104
Vernon Mauery9e801a22018-10-12 13:20:49 -0700105 output = std::get<command::Table&>(singletonPool)
106 .executeCommand(command, inPayload, *this);
Tom Josephe6361a22016-08-10 06:56:25 -0500107 }
108 else
109 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700110 output = std::get<command::Table&>(singletonPool)
111 .executeCommand(command, inMessage.payload, *this);
Tom Josephe6361a22016-08-10 06:56:25 -0500112 }
113
114 std::unique_ptr<Message> outMessage = nullptr;
115
116 switch (inMessage.payloadType)
117 {
118 case PayloadType::IPMI:
119 outMessage = createResponse<PayloadType::IPMI>(output, inMessage);
120 break;
121 case PayloadType::OPEN_SESSION_REQUEST:
122 outMessage = createResponse<PayloadType::OPEN_SESSION_RESPONSE>(
Vernon Mauery9e801a22018-10-12 13:20:49 -0700123 output, inMessage);
Tom Josephe6361a22016-08-10 06:56:25 -0500124 break;
125 case PayloadType::RAKP1:
126 outMessage = createResponse<PayloadType::RAKP2>(output, inMessage);
127 break;
128 case PayloadType::RAKP3:
129 outMessage = createResponse<PayloadType::RAKP4>(output, inMessage);
130 break;
Tom Joseph703adbf2017-03-31 10:50:22 +0530131 case PayloadType::SOL:
132 return outMessage;
133 break;
Tom Josephe6361a22016-08-10 06:56:25 -0500134 default:
135 break;
Tom Josephe6361a22016-08-10 06:56:25 -0500136 }
137
Tom Joseph8dfa0f02017-01-10 15:38:20 +0530138 outMessage->isPacketEncrypted = inMessage.isPacketEncrypted;
139 outMessage->isPacketAuthenticated = inMessage.isPacketAuthenticated;
140 outMessage->rcSessionID = inMessage.rcSessionID;
141 outMessage->bmcSessionID = inMessage.bmcSessionID;
142
Tom Josephe6361a22016-08-10 06:56:25 -0500143 return outMessage;
144}
145
146uint32_t Handler::getCommand(Message& message)
147{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700148 uint32_t command = 0;
Tom Josephe6361a22016-08-10 06:56:25 -0500149
150 command |= (static_cast<uint8_t>(message.payloadType) << 16);
151 if (message.payloadType == PayloadType::IPMI)
152 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700153 command |=
154 ((reinterpret_cast<LAN::header::Request*>(message.payload.data()))
155 ->netfn)
156 << 8;
157 command |=
158 (reinterpret_cast<LAN::header::Request*>(message.payload.data()))
159 ->cmd;
Tom Josephe6361a22016-08-10 06:56:25 -0500160 }
161
162 return command;
163}
164
Tom Joseph04b30382017-04-03 01:06:51 +0530165void Handler::send(Message& outMessage)
Tom Josephe6361a22016-08-10 06:56:25 -0500166{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700167 auto session =
Vernon Maueryae1fda42018-10-15 12:55:34 -0700168 std::get<session::Manager&>(singletonPool).getSession(sessionID);
Tom Josephe6361a22016-08-10 06:56:25 -0500169
170 // Flatten the packet
171 auto packet = parser::flatten(outMessage, sessionHeader, *session);
172
Tom Joseph04b30382017-04-03 01:06:51 +0530173 // Write the packet
Tom Josephe6361a22016-08-10 06:56:25 -0500174 auto writeStatus = channel->write(packet);
175 if (writeStatus < 0)
176 {
Tom Joseph04b30382017-04-03 01:06:51 +0530177 throw std::runtime_error("Error in writing to socket");
Tom Josephe6361a22016-08-10 06:56:25 -0500178 }
Tom Josephe6361a22016-08-10 06:56:25 -0500179}
180
Tom Josephff848492017-03-31 10:43:48 +0530181void Handler::setChannelInSession() const
182{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700183 auto session =
Vernon Maueryae1fda42018-10-15 12:55:34 -0700184 std::get<session::Manager&>(singletonPool).getSession(sessionID);
Tom Josephff848492017-03-31 10:43:48 +0530185
186 session->channelPtr = channel;
187}
188
Vernon Mauery70fd29c2017-11-30 13:11:43 -0800189void Handler::sendSOLPayload(const std::vector<uint8_t>& input)
Tom Joseph22596f22017-03-31 10:52:27 +0530190{
191 Message outMessage;
192
Vernon Mauery9e801a22018-10-12 13:20:49 -0700193 auto session =
Vernon Maueryae1fda42018-10-15 12:55:34 -0700194 std::get<session::Manager&>(singletonPool).getSession(sessionID);
Tom Joseph22596f22017-03-31 10:52:27 +0530195
196 outMessage.payloadType = PayloadType::SOL;
197 outMessage.payload = input;
198 outMessage.isPacketEncrypted = session->isCryptAlgoEnabled();
199 outMessage.isPacketAuthenticated = session->isIntegrityAlgoEnabled();
200 outMessage.rcSessionID = session->getRCSessionID();
201 outMessage.bmcSessionID = sessionID;
202
203 send(outMessage);
204}
205
Vernon Mauery9e801a22018-10-12 13:20:49 -0700206void Handler::sendUnsolicitedIPMIPayload(uint8_t netfn, uint8_t cmd,
Tom Joseph63d3e492017-03-31 11:01:08 +0530207 const std::vector<uint8_t>& output)
208{
209 Message outMessage;
210
Vernon Mauery9e801a22018-10-12 13:20:49 -0700211 auto session =
Vernon Maueryae1fda42018-10-15 12:55:34 -0700212 std::get<session::Manager&>(singletonPool).getSession(sessionID);
Tom Joseph63d3e492017-03-31 11:01:08 +0530213
214 outMessage.payloadType = PayloadType::IPMI;
215 outMessage.isPacketEncrypted = session->isCryptAlgoEnabled();
216 outMessage.isPacketAuthenticated = session->isIntegrityAlgoEnabled();
217 outMessage.rcSessionID = session->getRCSessionID();
218 outMessage.bmcSessionID = sessionID;
219
Vernon Mauery9e801a22018-10-12 13:20:49 -0700220 outMessage.payload.resize(sizeof(LAN::header::Request) + output.size() +
Tom Joseph63d3e492017-03-31 11:01:08 +0530221 sizeof(LAN::trailer::Request));
222
Vernon Mauery9e801a22018-10-12 13:20:49 -0700223 auto respHeader =
224 reinterpret_cast<LAN::header::Request*>(outMessage.payload.data());
Tom Joseph63d3e492017-03-31 11:01:08 +0530225
226 // Add IPMI LAN Message Request Header
227 respHeader->rsaddr = LAN::requesterBMCAddress;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700228 respHeader->netfn = (netfn << 0x02);
229 respHeader->cs = crc8bit(&(respHeader->rsaddr), 2);
Tom Joseph63d3e492017-03-31 11:01:08 +0530230 respHeader->rqaddr = LAN::responderBMCAddress;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700231 respHeader->rqseq = 0;
232 respHeader->cmd = cmd;
Tom Joseph63d3e492017-03-31 11:01:08 +0530233
234 auto assembledSize = sizeof(LAN::header::Request);
235
236 // Copy the output by the execution of the command
Vernon Mauery9e801a22018-10-12 13:20:49 -0700237 std::copy(output.begin(), output.end(),
Tom Joseph63d3e492017-03-31 11:01:08 +0530238 outMessage.payload.begin() + assembledSize);
239 assembledSize += output.size();
240
241 // Add the IPMI LAN Message Trailer
Vernon Mauery9e801a22018-10-12 13:20:49 -0700242 auto trailer = reinterpret_cast<LAN::trailer::Request*>(
243 outMessage.payload.data() + assembledSize);
Tom Joseph63d3e492017-03-31 11:01:08 +0530244
245 // Calculate the checksum for the field rqaddr in the header to the
246 // command data, 3 corresponds to size of the fields before rqaddr( rsaddr,
247 // netfn, cs).
248 trailer->checksum = crc8bit(&respHeader->rqaddr, assembledSize - 3);
249
250 send(outMessage);
251}
252
Vernon Mauery9e801a22018-10-12 13:20:49 -0700253} // namespace message